aboutsummaryrefslogtreecommitdiffstats
path: root/src/memcached.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rambler-co.ru>2009-10-02 17:09:38 +0400
committerVsevolod Stakhov <vsevolod@rambler-co.ru>2009-10-02 17:09:38 +0400
commitf3ad9c6f1e91c9912dbe730fdec350b5fc908672 (patch)
tree005e5568431db09becaa9b67a33dfc11f80bba7f /src/memcached.c
parente6a1d22de250c10992b484635fd95a03f197f779 (diff)
downloadrspamd-f3ad9c6f1e91c9912dbe730fdec350b5fc908672.tar.gz
rspamd-f3ad9c6f1e91c9912dbe730fdec350b5fc908672.zip
* Retab, no functional changes
Diffstat (limited to 'src/memcached.c')
-rw-r--r--src/memcached.c292
1 files changed, 147 insertions, 145 deletions
diff --git a/src/memcached.c b/src/memcached.c
index 2d369c87c..fc7152940 100644
--- a/src/memcached.c
+++ b/src/memcached.c
@@ -23,7 +23,7 @@
*/
#ifdef _THREAD_SAFE
-#include <pthread.h>
+# include <pthread.h>
#endif
#include <stdarg.h>
@@ -64,24 +64,23 @@
#define MAX_RETRIES 3
/* Header for udp protocol */
-struct memc_udp_header
-{
- uint16_t req_id;
- uint16_t seq_num;
- uint16_t dg_sent;
- uint16_t unused;
+struct memc_udp_header {
+ uint16_t req_id;
+ uint16_t seq_num;
+ uint16_t dg_sent;
+ uint16_t unused;
};
-static void socket_callback (int fd, short what, void *arg);
-static int memc_parse_header (char *buf, size_t *len, char **end);
+static void socket_callback (int fd, short what, void *arg);
+static int memc_parse_header (char *buf, size_t * len, char **end);
/*
* Write to syslog if OPT_DEBUG is specified
*/
static void
-memc_log (const memcached_ctx_t *ctx, int line, const char *fmt, ...)
+memc_log (const memcached_ctx_t * ctx, int line, const char *fmt, ...)
{
- va_list args;
+ va_list args;
if (ctx->options & MEMC_OPT_DEBUG) {
va_start (args, fmt);
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "memc_debug(%d): host: %s, port: %d", line, inet_ntoa (ctx->addr), ntohs (ctx->port));
@@ -94,13 +93,13 @@ memc_log (const memcached_ctx_t *ctx, int line, const char *fmt, ...)
* Callback for write command
*/
static void
-write_handler (int fd, short what, memcached_ctx_t *ctx)
+write_handler (int fd, short what, memcached_ctx_t * ctx)
{
- char read_buf[READ_BUFSIZ];
- int retries;
- ssize_t r;
- struct memc_udp_header header;
- struct iovec iov[4];
+ char read_buf[READ_BUFSIZ];
+ int retries;
+ ssize_t r;
+ struct memc_udp_header header;
+ struct iovec iov[4];
/* Write something to memcached */
if (what == EV_WRITE) {
@@ -159,7 +158,7 @@ write_handler (int fd, short what, memcached_ctx_t *ctx)
ctx->callback (ctx, SERVER_ERROR, ctx->callback_data);
}
if (header.req_id != ctx->count && retries < MAX_RETRIES) {
- retries ++;
+ retries++;
/* Not our reply packet */
continue;
}
@@ -195,16 +194,16 @@ write_handler (int fd, short what, memcached_ctx_t *ctx)
* Callback for read command
*/
static void
-read_handler (int fd, short what, memcached_ctx_t *ctx)
+read_handler (int fd, short what, memcached_ctx_t * ctx)
{
- char read_buf[READ_BUFSIZ];
- char *p;
- ssize_t r;
- size_t datalen;
- struct memc_udp_header header;
- struct iovec iov[2];
- int retries = 0, t;
-
+ char read_buf[READ_BUFSIZ];
+ char *p;
+ ssize_t r;
+ size_t datalen;
+ struct memc_udp_header header;
+ struct iovec iov[2];
+ int retries = 0, t;
+
if (what == EV_WRITE) {
/* Send command to memcached */
if (ctx->protocol == UDP_TEXT) {
@@ -297,9 +296,8 @@ read_handler (int fd, short what, memcached_ctx_t *ctx)
else {
p = read_buf;
}
-
- if (strncmp (ctx->param->buf + ctx->param->bufpos + r - sizeof (END_TRAILER) - sizeof (CRLF) + 2,
- END_TRAILER, sizeof (END_TRAILER) - 1) == 0) {
+
+ if (strncmp (ctx->param->buf + ctx->param->bufpos + r - sizeof (END_TRAILER) - sizeof (CRLF) + 2, END_TRAILER, sizeof (END_TRAILER) - 1) == 0) {
r -= sizeof (END_TRAILER) - sizeof (CRLF) - 2;
memcpy (ctx->param->buf + ctx->param->bufpos, p, r);
event_del (&ctx->mem_ev);
@@ -323,20 +321,20 @@ read_handler (int fd, short what, memcached_ctx_t *ctx)
event_del (&ctx->mem_ev);
ctx->callback (ctx, SERVER_TIMEOUT, ctx->callback_data);
}
-
+
}
/*
* Callback for delete command
*/
static void
-delete_handler (int fd, short what, memcached_ctx_t *ctx)
+delete_handler (int fd, short what, memcached_ctx_t * ctx)
{
- char read_buf[READ_BUFSIZ];
- int retries;
- ssize_t r;
- struct memc_udp_header header;
- struct iovec iov[2];
+ char read_buf[READ_BUFSIZ];
+ int retries;
+ ssize_t r;
+ struct memc_udp_header header;
+ struct iovec iov[2];
/* Write something to memcached */
if (what == EV_WRITE) {
@@ -382,7 +380,7 @@ delete_handler (int fd, short what, memcached_ctx_t *ctx)
return;
}
if (header.req_id != ctx->count && retries < MAX_RETRIES) {
- retries ++;
+ retries++;
/* Not our reply packet */
continue;
}
@@ -416,36 +414,36 @@ delete_handler (int fd, short what, memcached_ctx_t *ctx)
static void
socket_callback (int fd, short what, void *arg)
{
- memcached_ctx_t *ctx = (memcached_ctx_t *)arg;
+ memcached_ctx_t *ctx = (memcached_ctx_t *) arg;
switch (ctx->op) {
- case CMD_NULL:
- /* Do nothing here */
- break;
- case CMD_CONNECT:
- /* We have write readiness after connect call, so reinit event */
- ctx->cmd = "connect";
- if (what == EV_WRITE) {
- event_del (&ctx->mem_ev);
- event_set (&ctx->mem_ev, ctx->sock, EV_READ | EV_PERSIST | EV_TIMEOUT, socket_callback, (void *)ctx);
- event_add (&ctx->mem_ev, NULL);
- ctx->callback (ctx, OK, ctx->callback_data);
- ctx->alive = 1;
- }
- else {
- ctx->callback (ctx, SERVER_TIMEOUT, ctx->callback_data);
- ctx->alive = 0;
- }
- break;
- case CMD_WRITE:
- write_handler (fd, what, ctx);
- break;
- case CMD_READ:
- read_handler (fd, what, ctx);
- break;
- case CMD_DELETE:
- delete_handler (fd, what, ctx);
- break;
+ case CMD_NULL:
+ /* Do nothing here */
+ break;
+ case CMD_CONNECT:
+ /* We have write readiness after connect call, so reinit event */
+ ctx->cmd = "connect";
+ if (what == EV_WRITE) {
+ event_del (&ctx->mem_ev);
+ event_set (&ctx->mem_ev, ctx->sock, EV_READ | EV_PERSIST | EV_TIMEOUT, socket_callback, (void *)ctx);
+ event_add (&ctx->mem_ev, NULL);
+ ctx->callback (ctx, OK, ctx->callback_data);
+ ctx->alive = 1;
+ }
+ else {
+ ctx->callback (ctx, SERVER_TIMEOUT, ctx->callback_data);
+ ctx->alive = 0;
+ }
+ break;
+ case CMD_WRITE:
+ write_handler (fd, what, ctx);
+ break;
+ case CMD_READ:
+ read_handler (fd, what, ctx);
+ break;
+ case CMD_DELETE:
+ delete_handler (fd, what, ctx);
+ break;
}
}
@@ -453,7 +451,7 @@ socket_callback (int fd, short what, void *arg)
* Common callback function for memcached operations if no user's callback is specified
*/
static void
-common_memc_callback (memcached_ctx_t *ctx, memc_error_t error, void *data)
+common_memc_callback (memcached_ctx_t * ctx, memc_error_t error, void *data)
{
memc_log (ctx, __LINE__, "common_memc_callback: result of memc command '%s' is '%s'", ctx->cmd, memc_strerror (error));
}
@@ -462,10 +460,10 @@ common_memc_callback (memcached_ctx_t *ctx, memc_error_t error, void *data)
* Make socket for udp connection
*/
static int
-memc_make_udp_sock (memcached_ctx_t *ctx)
+memc_make_udp_sock (memcached_ctx_t * ctx)
{
- struct sockaddr_in sc;
- int ofl;
+ struct sockaddr_in sc;
+ int ofl;
bzero (&sc, sizeof (struct sockaddr_in *));
sc.sin_family = AF_INET;
@@ -480,8 +478,8 @@ memc_make_udp_sock (memcached_ctx_t *ctx)
}
/* set nonblocking */
- ofl = fcntl(ctx->sock, F_GETFL, 0);
- fcntl(ctx->sock, F_SETFL, ofl | O_NONBLOCK);
+ ofl = fcntl (ctx->sock, F_GETFL, 0);
+ fcntl (ctx->sock, F_SETFL, ofl | O_NONBLOCK);
/*
* Call connect to set default destination for datagrams
@@ -490,17 +488,17 @@ memc_make_udp_sock (memcached_ctx_t *ctx)
ctx->op = CMD_CONNECT;
event_set (&ctx->mem_ev, ctx->sock, EV_WRITE | EV_TIMEOUT, socket_callback, (void *)ctx);
event_add (&ctx->mem_ev, NULL);
- return connect (ctx->sock, (struct sockaddr*)&sc, sizeof (struct sockaddr_in));
+ return connect (ctx->sock, (struct sockaddr *)&sc, sizeof (struct sockaddr_in));
}
/*
* Make socket for tcp connection
*/
static int
-memc_make_tcp_sock (memcached_ctx_t *ctx)
+memc_make_tcp_sock (memcached_ctx_t * ctx)
{
- struct sockaddr_in sc;
- int ofl, r;
+ struct sockaddr_in sc;
+ int ofl, r;
bzero (&sc, sizeof (struct sockaddr_in *));
sc.sin_family = AF_INET;
@@ -515,10 +513,10 @@ memc_make_tcp_sock (memcached_ctx_t *ctx)
}
/* set nonblocking */
- ofl = fcntl(ctx->sock, F_GETFL, 0);
- fcntl(ctx->sock, F_SETFL, ofl | O_NONBLOCK);
-
- if ((r = connect (ctx->sock, (struct sockaddr*)&sc, sizeof (struct sockaddr_in))) == -1) {
+ ofl = fcntl (ctx->sock, F_GETFL, 0);
+ fcntl (ctx->sock, F_SETFL, ofl | O_NONBLOCK);
+
+ if ((r = connect (ctx->sock, (struct sockaddr *)&sc, sizeof (struct sockaddr_in))) == -1) {
if (errno != EINPROGRESS) {
close (ctx->sock);
ctx->sock = -1;
@@ -536,10 +534,10 @@ memc_make_tcp_sock (memcached_ctx_t *ctx)
* Parse VALUE reply from server and set len argument to value returned by memcached
*/
static int
-memc_parse_header (char *buf, size_t *len, char **end)
+memc_parse_header (char *buf, size_t * len, char **end)
{
- char *p, *c;
- int i;
+ char *p, *c;
+ int i;
/* VALUE <key> <flags> <bytes> [<cas unique>]\r\n */
c = strstr (buf, CRLF);
@@ -547,10 +545,10 @@ memc_parse_header (char *buf, size_t *len, char **end)
return -1;
}
*end = c + sizeof (CRLF) - 1;
-
+
if (strncmp (buf, "VALUE ", sizeof ("VALUE ") - 1) == 0) {
p = buf + sizeof ("VALUE ") - 1;
-
+
/* Read bytes value and ignore all other fields, such as flags and key */
for (i = 0; i < 2; i++) {
while (p++ < c && *p != ' ');
@@ -575,7 +573,7 @@ memc_parse_header (char *buf, size_t *len, char **end)
* Common read command handler for memcached
*/
memc_error_t
-memc_read (memcached_ctx_t *ctx, const char *cmd, memcached_param_t *param)
+memc_read (memcached_ctx_t * ctx, const char *cmd, memcached_param_t * param)
{
ctx->cmd = cmd;
ctx->op = CMD_READ;
@@ -590,7 +588,7 @@ memc_read (memcached_ctx_t *ctx, const char *cmd, memcached_param_t *param)
* Common write command handler for memcached
*/
memc_error_t
-memc_write (memcached_ctx_t *ctx, const char *cmd, memcached_param_t *param, int expire)
+memc_write (memcached_ctx_t * ctx, const char *cmd, memcached_param_t * param, int expire)
{
ctx->cmd = cmd;
ctx->op = CMD_WRITE;
@@ -601,11 +599,12 @@ memc_write (memcached_ctx_t *ctx, const char *cmd, memcached_param_t *param, int
return OK;
}
+
/*
* Delete command handler
*/
memc_error_t
-memc_delete (memcached_ctx_t *ctx, memcached_param_t *param)
+memc_delete (memcached_ctx_t * ctx, memcached_param_t * param)
{
ctx->cmd = "delete";
ctx->op = CMD_DELETE;
@@ -621,11 +620,11 @@ memc_delete (memcached_ctx_t *ctx, memcached_param_t *param)
* writing is done to each memcached server
*/
memc_error_t
-memc_write_mirror (memcached_ctx_t *ctx, size_t memcached_num, const char *cmd, memcached_param_t *param, int expire)
+memc_write_mirror (memcached_ctx_t * ctx, size_t memcached_num, const char *cmd, memcached_param_t * param, int expire)
{
- memc_error_t r, result = OK;
+ memc_error_t r, result = OK;
- while (memcached_num --) {
+ while (memcached_num--) {
if (ctx[memcached_num].alive == 1) {
r = memc_write (&ctx[memcached_num], cmd, param, expire);
if (r != OK) {
@@ -644,11 +643,11 @@ memc_write_mirror (memcached_ctx_t *ctx, size_t memcached_num, const char *cmd,
* reading is done from first active memcached server
*/
memc_error_t
-memc_read_mirror (memcached_ctx_t *ctx, size_t memcached_num, const char *cmd, memcached_param_t *param)
+memc_read_mirror (memcached_ctx_t * ctx, size_t memcached_num, const char *cmd, memcached_param_t * param)
{
- memc_error_t r, result = OK;
+ memc_error_t r, result = OK;
- while (memcached_num --) {
+ while (memcached_num--) {
if (ctx[memcached_num].alive == 1) {
r = memc_read (&ctx[memcached_num], cmd, param);
if (r != OK) {
@@ -675,11 +674,11 @@ memc_read_mirror (memcached_ctx_t *ctx, size_t memcached_num, const char *cmd, m
* deleting is done for each active memcached server
*/
memc_error_t
-memc_delete_mirror (memcached_ctx_t *ctx, size_t memcached_num, const char *cmd, memcached_param_t *param)
+memc_delete_mirror (memcached_ctx_t * ctx, size_t memcached_num, const char *cmd, memcached_param_t * param)
{
- memc_error_t r, result = OK;
+ memc_error_t r, result = OK;
- while (memcached_num --) {
+ while (memcached_num--) {
if (ctx[memcached_num].alive == 1) {
r = memc_delete (&ctx[memcached_num], param);
if (r != OK) {
@@ -699,8 +698,8 @@ memc_delete_mirror (memcached_ctx_t *ctx, size_t memcached_num, const char *cmd,
/*
* Initialize memcached context for specified protocol
*/
-int
-memc_init_ctx (memcached_ctx_t *ctx)
+int
+memc_init_ctx (memcached_ctx_t * ctx)
{
if (ctx == NULL) {
return -1;
@@ -715,26 +714,27 @@ memc_init_ctx (memcached_ctx_t *ctx)
}
switch (ctx->protocol) {
- case UDP_TEXT:
- return memc_make_udp_sock (ctx);
- break;
- case TCP_TEXT:
- return memc_make_tcp_sock (ctx);
- break;
+ case UDP_TEXT:
+ return memc_make_udp_sock (ctx);
+ break;
+ case TCP_TEXT:
+ return memc_make_tcp_sock (ctx);
+ break;
/* Not implemented */
- case UDP_BIN:
- case TCP_BIN:
- default:
- return -1;
+ case UDP_BIN:
+ case TCP_BIN:
+ default:
+ return -1;
}
}
+
/*
* Mirror init
*/
-int
-memc_init_ctx_mirror (memcached_ctx_t *ctx, size_t memcached_num)
+int
+memc_init_ctx_mirror (memcached_ctx_t * ctx, size_t memcached_num)
{
- int r, result = -1;
+ int r, result = -1;
while (memcached_num--) {
if (ctx[memcached_num].alive == 1) {
r = memc_init_ctx (&ctx[memcached_num]);
@@ -755,7 +755,7 @@ memc_init_ctx_mirror (memcached_ctx_t *ctx, size_t memcached_num)
* Close context connection
*/
int
-memc_close_ctx (memcached_ctx_t *ctx)
+memc_close_ctx (memcached_ctx_t * ctx)
{
if (ctx != NULL && ctx->sock != -1) {
event_del (&ctx->mem_ev);
@@ -764,13 +764,14 @@ memc_close_ctx (memcached_ctx_t *ctx)
return -1;
}
+
/*
* Mirror close
*/
-int
-memc_close_ctx_mirror (memcached_ctx_t *ctx, size_t memcached_num)
+int
+memc_close_ctx_mirror (memcached_ctx_t * ctx, size_t memcached_num)
{
- int r = 0;
+ int r = 0;
while (memcached_num--) {
if (ctx[memcached_num].alive == 1) {
r = memc_close_ctx (&ctx[memcached_num]);
@@ -785,38 +786,39 @@ memc_close_ctx_mirror (memcached_ctx_t *ctx, size_t memcached_num)
}
-const char * memc_strerror (memc_error_t err)
+const char *
+memc_strerror (memc_error_t err)
{
- const char *p;
+ const char *p;
switch (err) {
- case OK:
- p = "Ok";
- break;
- case BAD_COMMAND:
- p = "Bad command";
- break;
- case CLIENT_ERROR:
- p = "Client error";
- break;
- case SERVER_ERROR:
- p = "Server error";
- break;
- case SERVER_TIMEOUT:
- p = "Server timeout";
- break;
- case NOT_EXISTS:
- p = "Key not found";
- break;
- case EXISTS:
- p = "Key already exists";
- break;
- case WRONG_LENGTH:
- p = "Wrong result length";
- break;
- default:
- p = "Unknown error";
- break;
+ case OK:
+ p = "Ok";
+ break;
+ case BAD_COMMAND:
+ p = "Bad command";
+ break;
+ case CLIENT_ERROR:
+ p = "Client error";
+ break;
+ case SERVER_ERROR:
+ p = "Server error";
+ break;
+ case SERVER_TIMEOUT:
+ p = "Server timeout";
+ break;
+ case NOT_EXISTS:
+ p = "Key not found";
+ break;
+ case EXISTS:
+ p = "Key already exists";
+ break;
+ case WRONG_LENGTH:
+ p = "Wrong result length";
+ break;
+ default:
+ p = "Unknown error";
+ break;
}
return p;