1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2010 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
37 #include <libmemcached/common.h>
39 const char * memcached_lib_version(void)
41 return LIBMEMCACHED_VERSION_STRING
;
44 static inline memcached_return_t
memcached_version_binary(memcached_st
*ptr
);
45 static inline memcached_return_t
memcached_version_textual(memcached_st
*ptr
);
47 memcached_return_t
memcached_version(memcached_st
*ptr
)
49 memcached_return_t rc
;
50 if (memcached_failed(rc
= initialize_query(ptr
)))
55 if (ptr
->flags
.use_udp
)
57 return MEMCACHED_NOT_SUPPORTED
;
60 if (ptr
->flags
.binary_protocol
)
62 rc
= memcached_version_binary(ptr
);
66 rc
= memcached_version_textual(ptr
);
72 static inline memcached_return_t
memcached_version_textual(memcached_st
*ptr
)
74 struct libmemcached_io_vector_st vector
[]=
76 { memcached_literal_param("version\r\n") },
78 memcached_return_t rc
= MEMCACHED_SUCCESS
;
80 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
82 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
84 // Optimization, we only fetch version once.
85 if (instance
->major_version
!= UINT8_MAX
)
90 memcached_return_t rrc
= memcached_vdo(instance
, vector
, 1, true);
91 if (memcached_failed(rrc
))
93 (void)memcached_set_error(*instance
, rrc
, MEMCACHED_AT
);
94 instance
->major_version
= instance
->minor_version
= instance
->micro_version
= UINT8_MAX
;
95 rc
= MEMCACHED_SOME_ERRORS
;
99 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
100 rrc
= memcached_response(instance
, buffer
, sizeof(buffer
), NULL
);
101 if (memcached_failed(rrc
))
103 memcached_set_error(*instance
, rrc
, MEMCACHED_AT
);
104 instance
->major_version
= instance
->minor_version
= instance
->micro_version
= UINT8_MAX
;
105 rc
= MEMCACHED_SOME_ERRORS
;
109 /* Find the space, and then move one past it to copy version */
110 char *response_ptr
= index(buffer
, ' ');
113 long int version
= strtol(response_ptr
, (char **)NULL
, 10);
114 if (version
== LONG_MIN
or version
== LONG_MAX
or errno
== EINVAL
or version
> UINT8_MAX
or version
== 0)
116 memcached_set_error(*instance
, MEMCACHED_PROTOCOL_ERROR
, MEMCACHED_AT
, memcached_literal_param("strtol() failed to parse major version"));
117 instance
->major_version
= instance
->minor_version
= instance
->micro_version
= UINT8_MAX
;
118 rc
= MEMCACHED_SOME_ERRORS
;
121 instance
->major_version
= uint8_t(version
);
123 response_ptr
= index(response_ptr
, '.');
126 version
= strtol(response_ptr
, (char **)NULL
, 10);
127 if (version
== LONG_MIN
or version
== LONG_MAX
or errno
== EINVAL
or version
> UINT8_MAX
)
129 memcached_set_error(*instance
, MEMCACHED_PROTOCOL_ERROR
, MEMCACHED_AT
, memcached_literal_param("strtol() failed to parse minor version"));
130 instance
->major_version
= instance
->minor_version
= instance
->micro_version
= UINT8_MAX
;
131 rc
= MEMCACHED_SOME_ERRORS
;
134 instance
->minor_version
= uint8_t(version
);
136 response_ptr
= index(response_ptr
, '.');
139 version
= strtol(response_ptr
, (char **)NULL
, 10);
140 if (version
== LONG_MIN
or version
== LONG_MAX
or errno
== EINVAL
or version
> UINT8_MAX
)
142 memcached_set_error(*instance
, MEMCACHED_PROTOCOL_ERROR
, MEMCACHED_AT
, memcached_literal_param("strtol() failed to parse micro version"));
143 instance
->major_version
= instance
->minor_version
= instance
->micro_version
= UINT8_MAX
;
144 rc
= MEMCACHED_SOME_ERRORS
;
147 instance
->micro_version
= uint8_t(version
);
153 static inline memcached_return_t
memcached_version_binary(memcached_st
*ptr
)
155 protocol_binary_request_version request
= {};
156 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
157 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_VERSION
;
158 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
160 struct libmemcached_io_vector_st vector
[]=
162 { request
.bytes
, sizeof(request
.bytes
) }
165 memcached_return_t rc
= MEMCACHED_SUCCESS
;
166 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
168 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
170 if (instance
->major_version
!= UINT8_MAX
)
175 memcached_return_t rrc
= memcached_vdo(instance
, vector
, 1, true);
176 if (memcached_failed(rrc
))
178 memcached_io_reset(instance
);
179 rc
= MEMCACHED_SOME_ERRORS
;
184 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
186 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
188 if (instance
->major_version
!= UINT8_MAX
)
193 if (memcached_server_response_count(instance
) > 0)
198 memcached_return_t rrc
= memcached_response(instance
, buffer
, sizeof(buffer
), NULL
);
199 if (memcached_failed(rrc
))
201 memcached_io_reset(instance
);
202 rc
= MEMCACHED_SOME_ERRORS
;
206 long int version
= strtol(buffer
, &p
, 10);
207 if (version
== LONG_MIN
or version
== LONG_MAX
or errno
== EINVAL
or version
> UINT8_MAX
or version
== 0)
209 memcached_set_error(*instance
, MEMCACHED_PROTOCOL_ERROR
, MEMCACHED_AT
, memcached_literal_param("strtol() failed to parse major version"));
210 instance
->major_version
= instance
->minor_version
= instance
->micro_version
= UINT8_MAX
;
211 rc
= MEMCACHED_SOME_ERRORS
;
214 instance
->major_version
= uint8_t(version
);
216 version
= strtol(p
+1, &p
, 10);
217 if (version
== LONG_MIN
or version
== LONG_MAX
or errno
== EINVAL
or version
> UINT8_MAX
)
219 memcached_set_error(*instance
, MEMCACHED_PROTOCOL_ERROR
, MEMCACHED_AT
, memcached_literal_param("strtol() failed to parse micro version"));
220 instance
->major_version
= instance
->minor_version
= instance
->micro_version
= UINT8_MAX
;
221 rc
= MEMCACHED_SOME_ERRORS
;
224 instance
->minor_version
= uint8_t(version
);
226 version
= strtol(p
+ 1, NULL
, 10);
229 memcached_set_error(*instance
, MEMCACHED_PROTOCOL_ERROR
, MEMCACHED_AT
, memcached_literal_param("strtol() failed to parse micro version"));
230 instance
->major_version
= instance
->minor_version
= instance
->micro_version
= UINT8_MAX
;
231 rc
= MEMCACHED_SOME_ERRORS
;
234 instance
->micro_version
= uint8_t(version
);