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