2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "libmemcachedprotocol/common.h"
19 #include <sys/types.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 ** **********************************************************************
33 ** **********************************************************************
37 * The default function to receive data from the client. This function
38 * just wraps the recv function to receive from a socket.
39 * See man -s3socket recv for more information.
41 * @param cookie cookie indentifying a client, not used
42 * @param sock socket to read from
43 * @param buf the destination buffer
44 * @param nbytes the number of bytes to read
45 * @return the number of bytes transferred of -1 upon error
47 static ssize_t
default_recv(const void *cookie
, memcached_socket_t sock
, void *buf
, size_t nbytes
) {
49 return recv(sock
, buf
, nbytes
, 0);
53 * The default function to send data to the server. This function
54 * just wraps the send function to send through a socket.
55 * See man -s3socket send for more information.
57 * @param cookie cookie indentifying a client, not used
58 * @param sock socket to send to
59 * @param buf the source buffer
60 * @param nbytes the number of bytes to send
61 * @return the number of bytes transferred of -1 upon error
63 static ssize_t
default_send(const void *cookie
, memcached_socket_t fd
, const void *buf
,
66 return send(fd
, buf
, nbytes
, MSG_NOSIGNAL
);
70 * Try to drain the output buffers without blocking
72 * @param client the client to drain
73 * @return false if an error occured (connection should be shut down)
74 * true otherwise (please note that there may be more data to
75 * left in the buffer to send)
77 static bool drain_output(struct memcached_protocol_client_st
*client
) {
78 if (client
->is_verbose
) {
79 fprintf(stderr
, "%s:%d %s mute:%d output:%s length:%d\n", __FILE__
, __LINE__
, __func__
,
80 (int) client
->mute
, client
->output
? "yes" : "no",
81 client
->output
? (int) (client
->output
->nbytes
- client
->output
->offset
) : 0);
84 /* Do we have pending data to send? */
85 while (client
->output
) {
87 client
->root
->send(client
, client
->sock
, client
->output
->data
+ client
->output
->offset
,
88 client
->output
->nbytes
- client
->output
->offset
);
91 if (get_socket_errno() == EWOULDBLOCK
) {
93 } else if (get_socket_errno() != EINTR
) {
94 client
->error
= get_socket_errno();
98 client
->output
->offset
+= (size_t) len
;
99 if (client
->output
->offset
== client
->output
->nbytes
) {
100 /* This was the complete buffer */
101 struct chunk_st
*old
= client
->output
;
102 client
->output
= client
->output
->next
;
103 if (client
->output
== NULL
) {
104 client
->output_tail
= NULL
;
106 cache_free(client
->root
->buffer_cache
, old
);
115 * Allocate an output buffer and chain it into the output list
117 * @param client the client that needs the buffer
118 * @return pointer to the new chunk if the allocation succeeds, NULL otherwise
120 static struct chunk_st
*allocate_output_chunk(struct memcached_protocol_client_st
*client
) {
121 struct chunk_st
*ret
= cache_alloc(client
->root
->buffer_cache
);
127 ret
->offset
= ret
->nbytes
= 0;
129 ret
->size
= CHUNK_BUFFERSIZE
;
130 ret
->data
= (void *) (ret
+ 1);
131 if (client
->output
== NULL
) {
132 client
->output
= client
->output_tail
= ret
;
134 client
->output_tail
->next
= ret
;
135 client
->output_tail
= ret
;
142 * Spool data into the send-buffer for a client.
144 * @param client the client to spool the data for
145 * @param data the data to spool
146 * @param length the number of bytes of data to spool
147 * @return PROTOCOL_BINARY_RESPONSE_SUCCESS if success,
148 * PROTOCOL_BINARY_RESPONSE_ENOMEM if we failed to allocate memory
150 static protocol_binary_response_status
spool_output(struct memcached_protocol_client_st
*client
,
151 const void *data
, size_t length
) {
152 if (client
->is_verbose
) {
153 fprintf(stderr
, "%s:%d %s mute:%d length:%d\n", __FILE__
, __LINE__
, __func__
,
154 (int) client
->mute
, (int) length
);
158 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
163 struct chunk_st
*chunk
= client
->output
;
164 while (offset
< length
) {
165 if (chunk
== NULL
|| (chunk
->size
- chunk
->nbytes
) == 0) {
166 if ((chunk
= allocate_output_chunk(client
)) == NULL
) {
167 return PROTOCOL_BINARY_RESPONSE_ENOMEM
;
171 size_t bulk
= length
- offset
;
172 if (bulk
> chunk
->size
- chunk
->nbytes
) {
173 bulk
= chunk
->size
- chunk
->nbytes
;
176 memcpy(chunk
->data
+ chunk
->nbytes
, data
, bulk
);
177 chunk
->nbytes
+= bulk
;
181 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
185 * Try to determine the protocol used on this connection.
186 * If the first byte contains the magic byte PROTOCOL_BINARY_REQ we should
187 * be using the binary protocol on the connection. I implemented the support
188 * for the ASCII protocol by wrapping into the simple interface (aka v1),
189 * so the implementors needs to provide an implementation of that interface
192 static memcached_protocol_event_t
determine_protocol(struct memcached_protocol_client_st
*client
,
193 ssize_t
*length
, void **endptr
) {
194 if (*client
->root
->input_buffer
== (uint8_t) PROTOCOL_BINARY_REQ
) {
195 if (client
->is_verbose
) {
196 fprintf(stderr
, "%s:%d PROTOCOL: memcached_binary_protocol_process_data\n", __FILE__
,
199 client
->work
= memcached_binary_protocol_process_data
;
200 } else if (client
->root
->callback
->interface_version
== 1) {
201 if (client
->is_verbose
) {
202 fprintf(stderr
, "%s:%d PROTOCOL: memcached_ascii_protocol_process_data\n", __FILE__
,
207 * The ASCII protocol can only be used if the implementors provide
208 * an implementation for the version 1 of the interface..
210 * @todo I should allow the implementors to provide an implementation
211 * for version 0 and 1 at the same time and set the preferred
212 * interface to use...
214 client
->work
= memcached_ascii_protocol_process_data
;
216 if (client
->is_verbose
) {
217 fprintf(stderr
, "%s:%d PROTOCOL: Unsupported protocol\n", __FILE__
, __LINE__
);
220 /* Let's just output a warning the way it is supposed to look like
221 * in the ASCII protocol...
223 const char *err
= "CLIENT_ERROR: Unsupported protocol\r\n";
224 client
->root
->spool(client
, err
, strlen(err
));
225 client
->root
->drain(client
);
227 return MEMCACHED_PROTOCOL_ERROR_EVENT
; /* Unsupported protocol */
230 return client
->work(client
, length
, endptr
);
234 ** **********************************************************************
235 ** * PUBLIC INTERFACE
236 ** * See protocol_handler.h for function description
237 ** **********************************************************************
239 struct memcached_protocol_st
*memcached_protocol_create_instance(void) {
240 struct memcached_protocol_st
*ret
= calloc(1, sizeof(*ret
));
242 ret
->recv
= default_recv
;
243 ret
->send
= default_send
;
244 ret
->drain
= drain_output
;
245 ret
->spool
= spool_output
;
246 ret
->input_buffer_size
= 1 * 1024 * 1024;
247 ret
->input_buffer
= malloc(ret
->input_buffer_size
);
248 if (ret
->input_buffer
== NULL
) {
256 cache_create("protocol_handler", CHUNK_BUFFERSIZE
+ sizeof(struct chunk_st
), 0, NULL
, NULL
);
257 if (ret
->buffer_cache
== NULL
) {
258 free(ret
->input_buffer
);
267 void memcached_protocol_destroy_instance(struct memcached_protocol_st
*instance
) {
268 cache_destroy(instance
->buffer_cache
);
269 free(instance
->input_buffer
);
273 struct memcached_protocol_client_st
*
274 memcached_protocol_create_client(struct memcached_protocol_st
*instance
, memcached_socket_t sock
) {
275 struct memcached_protocol_client_st
*ret
= calloc(1, sizeof(memcached_protocol_client_st
));
277 ret
->root
= instance
;
279 ret
->work
= determine_protocol
;
285 void memcached_protocol_client_destroy(struct memcached_protocol_client_st
*client
) {
289 void memcached_protocol_client_set_verbose(struct memcached_protocol_client_st
*client
, bool arg
) {
291 client
->is_verbose
= arg
;
295 memcached_protocol_event_t
296 memcached_protocol_client_work(struct memcached_protocol_client_st
*client
) {
297 /* Try to send data and read from the socket */
298 bool more_data
= true;
300 ssize_t len
= client
->root
->recv(client
, client
->sock
,
301 client
->root
->input_buffer
+ client
->input_buffer_offset
,
302 client
->root
->input_buffer_size
- client
->input_buffer_offset
);
305 /* Do we have the complete packet? */
306 if (client
->input_buffer_offset
> 0) {
307 memcpy(client
->root
->input_buffer
, client
->input_buffer
, client
->input_buffer_offset
);
308 len
+= (ssize_t
) client
->input_buffer_offset
;
310 /* @todo use buffer-cache! */
311 free(client
->input_buffer
);
312 client
->input_buffer_offset
= 0;
316 memcached_protocol_event_t events
= client
->work(client
, &len
, &endptr
);
317 if (events
== MEMCACHED_PROTOCOL_ERROR_EVENT
) {
318 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
322 /* save the data for later on */
323 /* @todo use buffer-cache */
324 client
->input_buffer
= malloc((size_t) len
);
325 if (client
->input_buffer
== NULL
) {
326 client
->error
= ENOMEM
;
327 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
329 memcpy(client
->input_buffer
, endptr
, (size_t) len
);
330 client
->input_buffer_offset
= (size_t) len
;
333 } else if (len
== 0) {
334 /* Connection closed */
335 drain_output(client
);
336 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
338 if (get_socket_errno() != EWOULDBLOCK
) {
339 client
->error
= get_socket_errno();
340 /* mark this client as terminated! */
341 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
347 if (!drain_output(client
)) {
348 return MEMCACHED_PROTOCOL_ERROR_EVENT
;
351 memcached_protocol_event_t ret
= MEMCACHED_PROTOCOL_READ_EVENT
;
352 if (client
->output
) {
353 ret
|= MEMCACHED_PROTOCOL_READ_EVENT
;