Push version parsing into the parser.
[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
51 bool errors_happened= false;
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;
63 if (memcached_failed(rrc= memcached_vdo(instance, vector, 1, true)))
64 {
65 errors_happened= true;
66 (void)memcached_set_error(*instance, rrc, MEMCACHED_AT);
67 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
68 }
69 else if (memcached_failed(rrc= memcached_response(instance, NULL)))
70 {
71 errors_happened= true;
72 memcached_set_error(*instance, rrc, MEMCACHED_AT);
73 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
74 }
75 }
76
77 return errors_happened ? MEMCACHED_SOME_ERRORS : MEMCACHED_SUCCESS;
78 }
79
80 static inline memcached_return_t memcached_version_binary(memcached_st *ptr)
81 {
82 protocol_binary_request_version request= {};
83 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
84 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_VERSION;
85 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
86
87 libmemcached_io_vector_st vector[]=
88 {
89 { request.bytes, sizeof(request.bytes) }
90 };
91
92 bool errors_happened= false;
93 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
94 {
95 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
96
97 if (instance->major_version != UINT8_MAX)
98 {
99 continue;
100 }
101
102 memcached_return_t rrc= memcached_vdo(instance, vector, 1, true);
103 if (memcached_failed(rrc))
104 {
105 memcached_io_reset(instance);
106 errors_happened= true;
107 continue;
108 }
109 }
110
111 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
112 {
113 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
114
115 if (instance->major_version != UINT8_MAX)
116 {
117 continue;
118 }
119
120 if (memcached_server_response_count(instance) > 0)
121 {
122 char buffer[32];
123 char *p;
124
125 memcached_return_t rrc= memcached_response(instance, buffer, sizeof(buffer), NULL);
126 if (memcached_failed(rrc))
127 {
128 memcached_io_reset(instance);
129 errors_happened= true;
130 continue;
131 }
132
133 long int version= strtol(buffer, &p, 10);
134 if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0)
135 {
136 memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
137 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
138 errors_happened= true;
139 continue;
140 }
141 instance->major_version= uint8_t(version);
142
143 version= strtol(p +1, &p, 10);
144 if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
145 {
146 memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
147 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
148 errors_happened= true;
149 continue;
150 }
151 instance->minor_version= uint8_t(version);
152
153 version= strtol(p + 1, NULL, 10);
154 if (errno == ERANGE)
155 {
156 memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
157 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
158 errors_happened= true;
159 continue;
160 }
161 instance->micro_version= uint8_t(version);
162 }
163 }
164
165 return errors_happened ? MEMCACHED_SOME_ERRORS : MEMCACHED_SUCCESS;
166 }
167
168 memcached_return_t memcached_version(memcached_st *ptr)
169 {
170 memcached_return_t rc;
171 if (memcached_failed(rc= initialize_query(ptr, true)))
172 {
173 return rc;
174 }
175
176 if (memcached_is_udp(ptr))
177 {
178 return MEMCACHED_NOT_SUPPORTED;
179 }
180
181 if (memcached_is_binary(ptr))
182 {
183 return memcached_version_binary(ptr);
184 }
185
186 return memcached_version_textual(ptr);
187 }