182ff14e316da66983f75cc4625379645c67f622
[awesomized/libmemcached] / libmemcached / memcached_version.c
1 #include "common.h"
2
3 const char * memcached_lib_version(void)
4 {
5 return LIBMEMCACHED_VERSION_STRING;
6 }
7
8 static inline memcached_return memcached_version_binary(memcached_st *ptr);
9 static inline memcached_return memcached_version_textual(memcached_st *ptr);
10
11 memcached_return memcached_version(memcached_st *ptr)
12 {
13 if (ptr->flags & MEM_BINARY_PROTOCOL)
14 return memcached_version_binary(ptr);
15 else
16 return memcached_version_textual(ptr);
17 }
18
19 static inline memcached_return memcached_version_textual(memcached_st *ptr)
20 {
21 unsigned int x;
22 size_t send_length;
23 memcached_return rc;
24 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
25 char *response_ptr;
26 char *command= "version\r\n";
27
28 send_length= strlen(command);
29
30 rc= MEMCACHED_SUCCESS;
31 for (x= 0; x < ptr->number_of_hosts; x++)
32 {
33 memcached_return rrc;
34
35 rrc= memcached_do(&ptr->hosts[x], command, send_length, 1);
36 if (rrc != MEMCACHED_SUCCESS)
37 {
38 rc= MEMCACHED_SOME_ERRORS;
39 continue;
40 }
41
42 rrc= memcached_response(&ptr->hosts[x], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
43 if (rrc != MEMCACHED_SUCCESS)
44 rc= MEMCACHED_SOME_ERRORS;
45
46 /* Find the space, and then move one past it to copy version */
47 response_ptr= index(buffer, ' ');
48 response_ptr++;
49
50 ptr->hosts[x].major_version= (uint8_t)strtol(response_ptr, (char **)NULL, 10);
51 response_ptr= index(response_ptr, '.');
52 response_ptr++;
53 ptr->hosts[x].minor_version= (uint8_t)strtol(response_ptr, (char **)NULL, 10);
54 response_ptr= index(response_ptr, '.');
55 response_ptr++;
56 ptr->hosts[x].micro_version= (uint8_t)strtol(response_ptr, (char **)NULL, 10);
57 }
58
59 return rc;
60 }
61
62 static inline memcached_return memcached_version_binary(memcached_st *ptr)
63 {
64 memcached_return rc;
65 unsigned int x;
66 protocol_binary_request_version request= {0};
67 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
68 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_VERSION;
69 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
70
71 rc= MEMCACHED_SUCCESS;
72 for (x= 0; x < ptr->number_of_hosts; x++)
73 {
74 memcached_return rrc;
75
76 rrc= memcached_do(&ptr->hosts[x], request.bytes, sizeof(request.bytes), 1);
77 if (rrc != MEMCACHED_SUCCESS)
78 {
79 memcached_io_reset(&ptr->hosts[x]);
80 rc= MEMCACHED_SOME_ERRORS;
81 continue;
82 }
83 }
84
85 for (x= 0; x < ptr->number_of_hosts; x++)
86 if (memcached_server_response_count(&ptr->hosts[x]) > 0)
87 {
88 memcached_return rrc;
89 char buffer[32];
90 char *p;
91
92 rrc= memcached_response(&ptr->hosts[x], buffer, sizeof(buffer), NULL);
93 if (rrc != MEMCACHED_SUCCESS)
94 {
95 memcached_io_reset(&ptr->hosts[x]);
96 rc= MEMCACHED_SOME_ERRORS;
97 }
98
99 ptr->hosts[x].major_version= (uint8_t)strtol(buffer, &p, 10);
100 ptr->hosts[x].minor_version= (uint8_t)strtol(p + 1, &p, 10);
101 ptr->hosts[x].micro_version= (uint8_t)strtol(p + 1, NULL, 10);
102 }
103
104 return rc;
105 }