Refactor test framework to run pre/post just once.
[m6w6/libmemcached] / libmemcached / 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_t memcached_version_binary(memcached_st *ptr);
9 static inline memcached_return_t memcached_version_textual(memcached_st *ptr);
10
11 memcached_return_t memcached_version(memcached_st *ptr)
12 {
13 if (ptr->flags.use_udp)
14 return MEMCACHED_NOT_SUPPORTED;
15
16 if (ptr->flags.binary_protocol)
17 return memcached_version_binary(ptr);
18 else
19 return memcached_version_textual(ptr);
20 }
21
22 static inline memcached_return_t memcached_version_textual(memcached_st *ptr)
23 {
24 unsigned int x;
25 size_t send_length;
26 memcached_return_t rc;
27 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
28 char *response_ptr;
29 const char *command= "version\r\n";
30
31 send_length= strlen(command);
32
33 rc= MEMCACHED_SUCCESS;
34 for (x= 0; x < memcached_server_count(ptr); x++)
35 {
36 memcached_return_t rrc;
37 memcached_server_instance_st *instance=
38 memcached_server_instance_fetch(ptr, x);
39
40 rrc= memcached_do(instance, command, send_length, 1);
41 if (rrc != MEMCACHED_SUCCESS)
42 {
43 rc= MEMCACHED_SOME_ERRORS;
44 continue;
45 }
46
47 rrc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
48 if (rrc != MEMCACHED_SUCCESS)
49 {
50 rc= MEMCACHED_SOME_ERRORS;
51 continue;
52 }
53
54 /* Find the space, and then move one past it to copy version */
55 response_ptr= index(buffer, ' ');
56 response_ptr++;
57
58 instance->major_version= (uint8_t)strtol(response_ptr, (char **)NULL, 10);
59 response_ptr= index(response_ptr, '.');
60 response_ptr++;
61 instance->minor_version= (uint8_t)strtol(response_ptr, (char **)NULL, 10);
62 response_ptr= index(response_ptr, '.');
63 response_ptr++;
64 instance->micro_version= (uint8_t)strtol(response_ptr, (char **)NULL, 10);
65 }
66
67 return rc;
68 }
69
70 static inline memcached_return_t memcached_version_binary(memcached_st *ptr)
71 {
72 memcached_return_t rc;
73 unsigned int x;
74 protocol_binary_request_version request= { .bytes= {0}};
75 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
76 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_VERSION;
77 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
78
79 rc= MEMCACHED_SUCCESS;
80 for (x= 0; x < memcached_server_count(ptr); x++)
81 {
82 memcached_return_t rrc;
83
84 memcached_server_instance_st *instance=
85 memcached_server_instance_fetch(ptr, x);
86
87 rrc= memcached_do(instance, request.bytes, sizeof(request.bytes), 1);
88 if (rrc != MEMCACHED_SUCCESS)
89 {
90 memcached_io_reset(instance);
91 rc= MEMCACHED_SOME_ERRORS;
92 continue;
93 }
94 }
95
96 for (x= 0; x < memcached_server_count(ptr); x++)
97 {
98 memcached_server_instance_st *instance=
99 memcached_server_instance_fetch(ptr, x);
100
101 if (memcached_server_response_count(instance) > 0)
102 {
103 memcached_return_t rrc;
104 char buffer[32];
105 char *p;
106
107 rrc= memcached_response(instance, buffer, sizeof(buffer), NULL);
108 if (rrc != MEMCACHED_SUCCESS)
109 {
110 memcached_io_reset(instance);
111 rc= MEMCACHED_SOME_ERRORS;
112 continue;
113 }
114
115 instance->major_version= (uint8_t)strtol(buffer, &p, 10);
116 instance->minor_version= (uint8_t)strtol(p + 1, &p, 10);
117 instance->micro_version= (uint8_t)strtol(p + 1, NULL, 10);
118 }
119 }
120
121 return rc;
122 }