47
47
memcached_return_t memcached_version(memcached_st *ptr)
49
memcached_return_t rc;
50
if (memcached_failed(rc= initialize_query(ptr)))
49
55
if (ptr->flags.use_udp)
50
57
return MEMCACHED_NOT_SUPPORTED;
52
memcached_return_t rc;
54
60
if (ptr->flags.binary_protocol)
55
62
rc= memcached_version_binary(ptr);
57
66
rc= memcached_version_textual(ptr);
62
72
static inline memcached_return_t memcached_version_textual(memcached_st *ptr)
65
memcached_return_t rc;
66
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
68
const char *command= "version\r\n";
70
send_length= sizeof("version\r\n") -1;
72
rc= MEMCACHED_SUCCESS;
74
memcached_return_t rc= MEMCACHED_SUCCESS;
73
75
for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
75
memcached_return_t rrc;
76
memcached_server_write_instance_st instance=
77
memcached_server_instance_fetch(ptr, x);
77
memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
79
79
// Optimization, we only fetch version once.
80
80
if (instance->major_version != UINT8_MAX)
83
rrc= memcached_do(instance, command, send_length, true);
84
if (rrc != MEMCACHED_SUCCESS)
85
memcached_return_t rrc= memcached_do(instance, memcached_literal_param("version\r\n"), true);
86
if (memcached_failed(rrc))
88
(void)memcached_set_error(*instance, rrc, MEMCACHED_AT);
86
89
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
87
90
rc= MEMCACHED_SOME_ERRORS;
94
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
91
95
rrc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
92
if (rrc != MEMCACHED_SUCCESS)
96
if (memcached_failed(rrc))
98
memcached_set_error(*instance, rrc, MEMCACHED_AT);
94
99
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
95
100
rc= MEMCACHED_SOME_ERRORS;
99
104
/* Find the space, and then move one past it to copy version */
100
response_ptr= index(buffer, ' ');
103
instance->major_version= (uint8_t)strtol(response_ptr, (char **)NULL, 10);
106
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
107
rc= MEMCACHED_SOME_ERRORS;
111
response_ptr= index(response_ptr, '.');
114
instance->minor_version= (uint8_t)strtol(response_ptr, (char **)NULL, 10);
117
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
118
rc= MEMCACHED_SOME_ERRORS;
122
response_ptr= index(response_ptr, '.');
124
instance->micro_version= (uint8_t)strtol(response_ptr, (char **)NULL, 10);
127
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
128
rc= MEMCACHED_SOME_ERRORS;
105
char *response_ptr= index(buffer, ' ');
108
long int version= strtol(response_ptr, (char **)NULL, 10);
109
if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0)
111
memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
112
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
113
rc= MEMCACHED_SOME_ERRORS;
116
instance->major_version= uint8_t(version);
118
response_ptr= index(response_ptr, '.');
121
version= strtol(response_ptr, (char **)NULL, 10);
122
if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
124
memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse minor version"));
125
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
126
rc= MEMCACHED_SOME_ERRORS;
129
instance->minor_version= uint8_t(version);
131
response_ptr= index(response_ptr, '.');
134
version= strtol(response_ptr, (char **)NULL, 10);
135
if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
137
memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
138
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
139
rc= MEMCACHED_SOME_ERRORS;
142
instance->micro_version= uint8_t(version);
136
148
static inline memcached_return_t memcached_version_binary(memcached_st *ptr)
138
memcached_return_t rc;
139
150
protocol_binary_request_version request= {};
140
151
request.message.header.request.magic= PROTOCOL_BINARY_REQ;
141
152
request.message.header.request.opcode= PROTOCOL_BINARY_CMD_VERSION;
142
153
request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
144
rc= MEMCACHED_SUCCESS;
155
memcached_return_t rc= MEMCACHED_SUCCESS;
145
156
for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
147
memcached_return_t rrc;
149
memcached_server_write_instance_st instance=
150
memcached_server_instance_fetch(ptr, x);
158
memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
152
160
if (instance->major_version != UINT8_MAX)
155
rrc= memcached_do(instance, request.bytes, sizeof(request.bytes), true);
156
if (rrc != MEMCACHED_SUCCESS)
165
memcached_return_t rrc= memcached_do(instance, request.bytes, sizeof(request.bytes), true);
166
if (memcached_failed(rrc))
158
168
memcached_io_reset(instance);
159
169
rc= MEMCACHED_SOME_ERRORS;
167
177
memcached_server_instance_fetch(ptr, x);
169
179
if (instance->major_version != UINT8_MAX)
172
184
if (memcached_server_response_count(instance) > 0)
174
memcached_return_t rrc;
178
rrc= memcached_response(instance, buffer, sizeof(buffer), NULL);
179
if (rrc != MEMCACHED_SUCCESS)
189
memcached_return_t rrc= memcached_response(instance, buffer, sizeof(buffer), NULL);
190
if (memcached_failed(rrc))
181
192
memcached_io_reset(instance);
182
193
rc= MEMCACHED_SOME_ERRORS;
186
instance->major_version= (uint8_t)strtol(buffer, &p, 10);
189
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
190
rc= MEMCACHED_SOME_ERRORS;
194
instance->minor_version= (uint8_t)strtol(p + 1, &p, 10);
197
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
198
rc= MEMCACHED_SOME_ERRORS;
202
instance->micro_version= (uint8_t)strtol(p + 1, NULL, 10);
205
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
206
rc= MEMCACHED_SOME_ERRORS;
197
long int version= strtol(buffer, &p, 10);
198
if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX or version == 0)
200
memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse major version"));
201
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
202
rc= MEMCACHED_SOME_ERRORS;
205
instance->major_version= uint8_t(version);
207
version= strtol(p +1, &p, 10);
208
if (version == LONG_MIN or version == LONG_MAX or errno == EINVAL or version > UINT8_MAX)
210
memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
211
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
212
rc= MEMCACHED_SOME_ERRORS;
215
instance->minor_version= uint8_t(version);
217
version= strtol(p + 1, NULL, 10);
220
memcached_set_error(*instance, MEMCACHED_PROTOCOL_ERROR, MEMCACHED_AT, memcached_literal_param("strtol() failed to parse micro version"));
221
instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
222
rc= MEMCACHED_SOME_ERRORS;
225
instance->micro_version= uint8_t(version);