Merge in trunk
[awesomized/libmemcached] / libmemcached / version.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2010 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37 #include <libmemcached/common.h>
38
39 const char * memcached_lib_version(void)
40 {
41 return LIBMEMCACHED_VERSION_STRING;
42 }
43
44 static inline memcached_return_t memcached_version_textual(memcached_st *ptr)
45 {
46 libmemcached_io_vector_st vector[]=
47 {
48 { memcached_literal_param("version\r\n") },
49 };
50 memcached_return_t rc= MEMCACHED_SUCCESS;
51
52 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
53 {
54 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
55
56 // Optimization, we only fetch version once.
57 if (instance->major_version != UINT8_MAX)
58 {
59 continue;
60 }
61
62 memcached_return_t rrc= memcached_vdo(instance, vector, 1, true);
63 if (memcached_failed(rrc))
64 {
65 (void)memcached_set_error(*instance, rrc, MEMCACHED_AT);
66 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
67 rc= MEMCACHED_SOME_ERRORS;
68 continue;
69 }
70
71 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
72 rrc= memcached_response(instance, buffer, sizeof(buffer), NULL);
73 if (memcached_failed(rrc))
74 {
75 memcached_set_error(*instance, rrc, MEMCACHED_AT);
76 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
77 rc= MEMCACHED_SOME_ERRORS;
78 continue;
79 }
80
81 /* Find the space, and then move one past it to copy version */
82 char *response_ptr= index(buffer, ' ');
83 response_ptr++;
84
85 long int version= strtol(response_ptr, (char **)NULL, 10);
86 if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0)
87 {
88 memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
89 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
90 rc= MEMCACHED_SOME_ERRORS;
91 continue;
92 }
93 instance->major_version= uint8_t(version);
94
95 response_ptr= index(response_ptr, '.');
96 response_ptr++;
97
98 version= strtol(response_ptr, (char **)NULL, 10);
99 if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
100 {
101 memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version"));
102 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
103 rc= MEMCACHED_SOME_ERRORS;
104 continue;
105 }
106 instance->minor_version= uint8_t(version);
107
108 response_ptr= index(response_ptr, '.');
109 response_ptr++;
110
111 version= strtol(response_ptr, (char **)NULL, 10);
112 if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
113 {
114 memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
115 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
116 rc= MEMCACHED_SOME_ERRORS;
117 continue;
118 }
119 instance->micro_version= uint8_t(version);
120 }
121
122 return rc;
123 }
124
125 static inline memcached_return_t memcached_version_binary(memcached_st *ptr)
126 {
127 protocol_binary_request_version request= {};
128 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
129 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_VERSION;
130 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
131
132 libmemcached_io_vector_st vector[]=
133 {
134 { request.bytes, sizeof(request.bytes) }
135 };
136
137 memcached_return_t rc= MEMCACHED_SUCCESS;
138 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
139 {
140 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
141
142 if (instance->major_version != UINT8_MAX)
143 {
144 continue;
145 }
146
147 memcached_return_t rrc= memcached_vdo(instance, vector, 1, true);
148 if (memcached_failed(rrc))
149 {
150 memcached_io_reset(instance);
151 rc= MEMCACHED_SOME_ERRORS;
152 continue;
153 }
154 }
155
156 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
157 {
158 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
159
160 if (instance->major_version != UINT8_MAX)
161 {
162 continue;
163 }
164
165 if (memcached_server_response_count(instance) > 0)
166 {
167 char buffer[32];
168 char *p;
169
170 memcached_return_t rrc= memcached_response(instance, buffer, sizeof(buffer), NULL);
171 if (memcached_failed(rrc))
172 {
173 memcached_io_reset(instance);
174 rc= MEMCACHED_SOME_ERRORS;
175 continue;
176 }
177
178 long int version= strtol(buffer, &p, 10);
179 if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0)
180 {
181 memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
182 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
183 rc= MEMCACHED_SOME_ERRORS;
184 continue;
185 }
186 instance->major_version= uint8_t(version);
187
188 version= strtol(p +1, &p, 10);
189 if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
190 {
191 memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
192 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
193 rc= MEMCACHED_SOME_ERRORS;
194 continue;
195 }
196 instance->minor_version= uint8_t(version);
197
198 version= strtol(p + 1, NULL, 10);
199 if (errno == ERANGE)
200 {
201 memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
202 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
203 rc= MEMCACHED_SOME_ERRORS;
204 continue;
205 }
206 instance->micro_version= uint8_t(version);
207 }
208 }
209
210 return rc;
211 }
212
213 memcached_return_t memcached_version(memcached_st *ptr)
214 {
215 memcached_return_t rc;
216 if (memcached_failed(rc= initialize_query(ptr, true)))
217 {
218 return rc;
219 }
220
221 if (memcached_is_udp(ptr))
222 {
223 return MEMCACHED_NOT_SUPPORTED;
224 }
225
226 if (memcached_is_binary(ptr))
227 {
228 rc= memcached_version_binary(ptr);
229 }
230 else
231 {
232 rc= memcached_version_textual(ptr);
233 }
234
235 return rc;
236 }