2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2008-08-28 20:02:43 +02:00
|
|
|
#include "client.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "command.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "listen.h"
|
|
|
|
#include "permission.h"
|
2006-07-30 05:43:38 +02:00
|
|
|
#include "sllist.h"
|
2006-07-30 05:57:29 +02:00
|
|
|
#include "utils.h"
|
2007-01-11 21:41:17 +01:00
|
|
|
#include "ioops.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2008-04-12 11:46:11 +02:00
|
|
|
#include "main_notify.h"
|
2008-08-28 20:03:48 +02:00
|
|
|
#include "dlist.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:19:26 +02:00
|
|
|
#include "../config.h"
|
|
|
|
|
2006-08-10 23:15:06 +02:00
|
|
|
#define GREETING "OK MPD " PROTOCOL_VERSION "\n"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
#define CLIENT_MAX_BUFFER_LENGTH (40960)
|
|
|
|
#define CLIENT_LIST_MODE_BEGIN "command_list_begin"
|
|
|
|
#define CLIENT_LIST_OK_MODE_BEGIN "command_list_ok_begin"
|
|
|
|
#define CLIENT_LIST_MODE_END "command_list_end"
|
|
|
|
#define CLIENT_DEFAULT_OUT_BUFFER_SIZE (4096)
|
|
|
|
#define CLIENT_TIMEOUT_DEFAULT (60)
|
|
|
|
#define CLIENT_MAX_CONNECTIONS_DEFAULT (10)
|
|
|
|
#define CLIENT_MAX_COMMAND_LIST_DEFAULT (2048*1024)
|
|
|
|
#define CLIENT_MAX_OUTPUT_BUFFER_SIZE_DEFAULT (8192*1024)
|
|
|
|
|
|
|
|
/* set this to zero to indicate we have no possible clients */
|
|
|
|
static unsigned int client_max_connections; /*CLIENT_MAX_CONNECTIONS_DEFAULT; */
|
|
|
|
static int client_timeout = CLIENT_TIMEOUT_DEFAULT;
|
|
|
|
static size_t client_max_command_list_size =
|
|
|
|
CLIENT_MAX_COMMAND_LIST_DEFAULT;
|
|
|
|
static size_t client_max_output_buffer_size =
|
|
|
|
CLIENT_MAX_OUTPUT_BUFFER_SIZE_DEFAULT;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
/* maybe make conf option for this, or... 32 might be good enough */
|
2008-08-28 20:02:59 +02:00
|
|
|
static long int client_list_cache_size = 32;
|
2006-07-30 05:43:38 +02:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
/* shared globally between all clients: */
|
2007-01-14 04:07:53 +01:00
|
|
|
static struct strnode *list_cache;
|
|
|
|
static struct strnode *list_cache_head;
|
|
|
|
static struct strnode *list_cache_tail;
|
2006-07-30 05:43:38 +02:00
|
|
|
|
2008-08-28 20:02:58 +02:00
|
|
|
struct client {
|
2008-08-28 20:03:48 +02:00
|
|
|
struct list_head siblings;
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
char buffer[CLIENT_MAX_BUFFER_LENGTH];
|
2008-03-26 11:38:07 +01:00
|
|
|
size_t bufferLength;
|
|
|
|
size_t bufferPos;
|
2008-08-28 20:20:10 +02:00
|
|
|
int fd; /* file descriptor; -1 if expired */
|
2006-07-17 23:46:32 +02:00
|
|
|
int permission;
|
2004-02-24 00:41:20 +01:00
|
|
|
time_t lastTime;
|
2006-07-30 05:43:38 +02:00
|
|
|
struct strnode *cmd_list; /* for when in list mode */
|
|
|
|
struct strnode *cmd_list_tail; /* for when in list mode */
|
|
|
|
int cmd_list_OK; /* print OK after each command execution */
|
2008-03-26 11:38:07 +01:00
|
|
|
size_t cmd_list_size; /* mem cmd_list consumes */
|
2006-07-30 05:43:38 +02:00
|
|
|
int cmd_list_dup; /* has the cmd_list been copied to private space? */
|
|
|
|
struct sllnode *deferred_send; /* for output if client is slow */
|
2008-03-26 11:38:07 +01:00
|
|
|
size_t deferred_bytes; /* mem deferred_send consumes */
|
2008-08-28 20:02:59 +02:00
|
|
|
unsigned int num; /* client number */
|
2006-07-30 05:43:38 +02:00
|
|
|
|
|
|
|
char *send_buf;
|
2008-03-26 11:38:07 +01:00
|
|
|
size_t send_buf_used; /* bytes used this instance */
|
|
|
|
size_t send_buf_size; /* bytes usable this instance */
|
|
|
|
size_t send_buf_alloc; /* bytes actually allocated */
|
2008-08-28 20:02:58 +02:00
|
|
|
};
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:03:48 +02:00
|
|
|
static LIST_HEAD(clients);
|
|
|
|
static unsigned num_clients;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void client_write_deferred(struct client *client);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void client_write_output(struct client *client);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
#ifdef SO_SNDBUF
|
2008-08-28 20:02:59 +02:00
|
|
|
static size_t get_default_snd_buf_size(struct client *client)
|
2006-07-30 05:43:38 +02:00
|
|
|
{
|
|
|
|
int new_size;
|
|
|
|
socklen_t sockOptLen = sizeof(int);
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
if (getsockopt(client->fd, SOL_SOCKET, SO_SNDBUF,
|
2006-07-30 05:43:38 +02:00
|
|
|
(char *)&new_size, &sockOptLen) < 0) {
|
|
|
|
DEBUG("problem getting sockets send buffer size\n");
|
2008-08-28 20:02:59 +02:00
|
|
|
return CLIENT_DEFAULT_OUT_BUFFER_SIZE;
|
2006-07-30 05:43:38 +02:00
|
|
|
}
|
|
|
|
if (new_size > 0)
|
2008-03-26 11:38:07 +01:00
|
|
|
return (size_t)new_size;
|
2006-07-30 05:43:38 +02:00
|
|
|
DEBUG("sockets send buffer size is not positive\n");
|
2008-08-28 20:02:59 +02:00
|
|
|
return CLIENT_DEFAULT_OUT_BUFFER_SIZE;
|
2006-07-30 05:43:38 +02:00
|
|
|
}
|
|
|
|
#else /* !SO_SNDBUF */
|
2008-08-28 20:02:59 +02:00
|
|
|
static size_t get_default_snd_buf_size(struct client *client)
|
2006-07-30 05:43:38 +02:00
|
|
|
{
|
2008-08-28 20:02:59 +02:00
|
|
|
return CLIENT_DEFAULT_OUT_BUFFER_SIZE;
|
2006-07-30 05:43:38 +02:00
|
|
|
}
|
|
|
|
#endif /* !SO_SNDBUF */
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void set_send_buf_size(struct client *client)
|
2006-07-30 05:43:38 +02:00
|
|
|
{
|
2008-08-28 20:02:59 +02:00
|
|
|
size_t new_size = get_default_snd_buf_size(client);
|
|
|
|
if (client->send_buf_size != new_size) {
|
|
|
|
client->send_buf_size = new_size;
|
2006-07-30 05:43:38 +02:00
|
|
|
/* don't resize to get smaller, only bigger */
|
2008-08-28 20:02:59 +02:00
|
|
|
if (client->send_buf_alloc < new_size) {
|
|
|
|
if (client->send_buf)
|
|
|
|
free(client->send_buf);
|
|
|
|
client->send_buf = xmalloc(new_size);
|
|
|
|
client->send_buf_alloc = new_size;
|
2006-07-30 05:43:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:37:11 +02:00
|
|
|
int client_is_expired(const struct client *client)
|
2008-08-28 20:20:10 +02:00
|
|
|
{
|
2008-08-28 20:20:10 +02:00
|
|
|
return client->fd < 0;
|
2008-08-28 20:20:10 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 19:16:34 +02:00
|
|
|
int client_get_permission(const struct client *client)
|
|
|
|
{
|
|
|
|
return client->permission;
|
|
|
|
}
|
|
|
|
|
|
|
|
void client_set_permission(struct client *client, int permission)
|
|
|
|
{
|
|
|
|
client->permission = permission;
|
|
|
|
}
|
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
static inline void client_set_expired(struct client *client)
|
|
|
|
{
|
2008-08-28 20:20:10 +02:00
|
|
|
if (client->fd >= 0) {
|
|
|
|
xclose(client->fd);
|
|
|
|
client->fd = -1;
|
|
|
|
}
|
2008-08-28 20:20:10 +02:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void client_init(struct client *client, int fd)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-28 20:03:48 +02:00
|
|
|
static unsigned int next_client_num;
|
2008-08-28 20:02:59 +02:00
|
|
|
|
2008-08-28 20:23:22 +02:00
|
|
|
assert(fd >= 0);
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
client->cmd_list_size = 0;
|
|
|
|
client->cmd_list_dup = 0;
|
|
|
|
client->cmd_list_OK = -1;
|
|
|
|
client->bufferLength = 0;
|
|
|
|
client->bufferPos = 0;
|
|
|
|
client->fd = fd;
|
2008-03-26 11:39:03 +01:00
|
|
|
set_nonblocking(fd);
|
2008-08-28 20:02:59 +02:00
|
|
|
client->lastTime = time(NULL);
|
|
|
|
client->cmd_list = NULL;
|
|
|
|
client->cmd_list_tail = NULL;
|
|
|
|
client->deferred_send = NULL;
|
|
|
|
client->deferred_bytes = 0;
|
2008-08-28 20:03:48 +02:00
|
|
|
client->num = next_client_num++;
|
2008-08-28 20:02:59 +02:00
|
|
|
client->send_buf_used = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
client->permission = getDefaultPermissions();
|
|
|
|
set_send_buf_size(client);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
xwrite(fd, GREETING, strlen(GREETING));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_cmd_list(struct strnode *list)
|
|
|
|
{
|
|
|
|
struct strnode *tmp = list;
|
|
|
|
|
|
|
|
while (tmp) {
|
|
|
|
struct strnode *next = tmp->next;
|
|
|
|
if (tmp >= list_cache_head && tmp <= list_cache_tail) {
|
|
|
|
/* inside list_cache[] array */
|
|
|
|
tmp->data = NULL;
|
|
|
|
tmp->next = NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2006-07-30 05:43:38 +02:00
|
|
|
free(tmp);
|
|
|
|
tmp = next;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-30 05:43:38 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void cmd_list_clone(struct client *client)
|
2006-07-30 05:43:38 +02:00
|
|
|
{
|
2008-08-28 20:02:59 +02:00
|
|
|
struct strnode *new = dup_strlist(client->cmd_list);
|
|
|
|
free_cmd_list(client->cmd_list);
|
|
|
|
client->cmd_list = new;
|
|
|
|
client->cmd_list_dup = 1;
|
2006-07-30 05:43:38 +02:00
|
|
|
|
|
|
|
/* new tail */
|
|
|
|
while (new && new->next)
|
|
|
|
new = new->next;
|
2008-08-28 20:02:59 +02:00
|
|
|
client->cmd_list_tail = new;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void new_cmd_list_ptr(struct client *client, char *s, const int size)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-30 05:43:38 +02:00
|
|
|
int i;
|
|
|
|
struct strnode *new;
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
if (!client->cmd_list_dup) {
|
|
|
|
for (i = client_list_cache_size - 1; i >= 0; --i) {
|
2006-07-30 05:43:38 +02:00
|
|
|
if (list_cache[i].data)
|
|
|
|
continue;
|
|
|
|
new = &(list_cache[i]);
|
|
|
|
new->data = s;
|
|
|
|
/* implied in free_cmd_list() and init: */
|
|
|
|
/* last->next->next = NULL; */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
/* allocate from the heap */
|
2008-08-28 20:02:59 +02:00
|
|
|
new = client->cmd_list_dup ? new_strnode_dup(s, size)
|
2006-07-30 05:43:38 +02:00
|
|
|
: new_strnode(s);
|
|
|
|
out:
|
2008-08-28 20:02:59 +02:00
|
|
|
if (client->cmd_list) {
|
|
|
|
client->cmd_list_tail->next = new;
|
|
|
|
client->cmd_list_tail = new;
|
2006-07-30 05:43:38 +02:00
|
|
|
} else
|
2008-08-28 20:02:59 +02:00
|
|
|
client->cmd_list = client->cmd_list_tail = new;
|
2006-07-30 05:43:38 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void client_close(struct client *client)
|
2006-07-30 05:43:38 +02:00
|
|
|
{
|
|
|
|
struct sllnode *buf;
|
2008-08-28 20:03:48 +02:00
|
|
|
|
|
|
|
assert(num_clients > 0);
|
|
|
|
assert(!list_empty(&clients));
|
|
|
|
list_del(&client->siblings);
|
|
|
|
--num_clients;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-06 15:31:55 +02:00
|
|
|
client_set_expired(client);
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
if (client->cmd_list) {
|
|
|
|
free_cmd_list(client->cmd_list);
|
|
|
|
client->cmd_list = NULL;
|
2006-07-30 05:43:38 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
if ((buf = client->deferred_send)) {
|
2006-07-30 05:43:38 +02:00
|
|
|
do {
|
|
|
|
struct sllnode *prev = buf;
|
|
|
|
buf = buf->next;
|
|
|
|
free(prev);
|
|
|
|
} while (buf);
|
2008-08-28 20:02:59 +02:00
|
|
|
client->deferred_send = NULL;
|
2006-07-30 05:43:38 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:03:48 +02:00
|
|
|
if (client->send_buf)
|
|
|
|
free(client->send_buf);
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
SECURE("client %i: closed\n", client->num);
|
2008-08-28 20:03:48 +02:00
|
|
|
free(client);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
static const char *
|
|
|
|
sockaddr_to_tmp_string(const struct sockaddr *addr)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-28 20:03:03 +02:00
|
|
|
const char *hostname;
|
|
|
|
|
|
|
|
switch (addr->sa_family) {
|
2008-04-12 06:07:24 +02:00
|
|
|
#ifdef HAVE_TCP
|
2008-08-28 20:03:03 +02:00
|
|
|
case AF_INET:
|
|
|
|
hostname = (const char *)inet_ntoa(((const struct sockaddr_in *)
|
|
|
|
addr)->sin_addr);
|
|
|
|
if (!hostname)
|
|
|
|
hostname = "error getting ipv4 address";
|
|
|
|
break;
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef HAVE_IPV6
|
2008-08-28 20:03:03 +02:00
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
static char host[INET6_ADDRSTRLEN + 1];
|
|
|
|
memset(host, 0, INET6_ADDRSTRLEN + 1);
|
|
|
|
if (inet_ntop(AF_INET6, (const void *)
|
|
|
|
&(((const struct sockaddr_in6 *)addr)->
|
|
|
|
sin6_addr), host,
|
|
|
|
INET6_ADDRSTRLEN)) {
|
|
|
|
hostname = (const char *)host;
|
|
|
|
} else {
|
|
|
|
hostname = "error getting ipv6 address";
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2008-08-28 20:03:03 +02:00
|
|
|
}
|
|
|
|
break;
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|
2008-04-12 06:07:24 +02:00
|
|
|
#endif /* HAVE_TCP */
|
|
|
|
#ifdef HAVE_UN
|
2008-08-28 20:03:03 +02:00
|
|
|
case AF_UNIX:
|
|
|
|
hostname = "local connection";
|
|
|
|
break;
|
2008-04-12 06:07:24 +02:00
|
|
|
#endif /* HAVE_UN */
|
2008-08-28 20:03:03 +02:00
|
|
|
default:
|
|
|
|
hostname = "unknown";
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2008-08-28 20:03:48 +02:00
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
return hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
void client_new(int fd, const struct sockaddr *addr)
|
|
|
|
{
|
|
|
|
struct client *client;
|
|
|
|
|
|
|
|
if (num_clients >= client_max_connections) {
|
|
|
|
ERROR("Max Connections Reached!\n");
|
|
|
|
xclose(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-28 20:03:48 +02:00
|
|
|
client = xcalloc(1, sizeof(*client));
|
|
|
|
list_add(&client->siblings, &clients);
|
|
|
|
++num_clients;
|
|
|
|
client_init(client, fd);
|
2008-08-28 20:20:10 +02:00
|
|
|
SECURE("client %i: opened from %s\n", client->num,
|
|
|
|
sockaddr_to_tmp_string(addr));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static int client_process_line(struct client *client)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-14 06:38:49 +01:00
|
|
|
int ret = 1;
|
2008-08-28 20:02:59 +02:00
|
|
|
char *line = client->buffer + client->bufferPos;
|
|
|
|
|
|
|
|
if (client->cmd_list_OK >= 0) {
|
|
|
|
if (strcmp(line, CLIENT_LIST_MODE_END) == 0) {
|
|
|
|
DEBUG("client %i: process command "
|
|
|
|
"list\n", client->num);
|
2008-08-29 09:36:42 +02:00
|
|
|
ret = processListOfCommands(client,
|
2008-08-28 20:02:59 +02:00
|
|
|
client->cmd_list_OK,
|
|
|
|
client->cmd_list);
|
|
|
|
DEBUG("client %i: process command "
|
|
|
|
"list returned %i\n", client->num, ret);
|
2008-08-28 20:20:10 +02:00
|
|
|
|
2008-08-29 09:36:40 +02:00
|
|
|
if (ret == COMMAND_RETURN_CLOSE ||
|
2008-09-10 11:42:26 +02:00
|
|
|
client_is_expired(client))
|
2008-08-29 09:36:40 +02:00
|
|
|
return COMMAND_RETURN_CLOSE;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ret == 0)
|
2008-09-07 13:51:59 +02:00
|
|
|
command_success(client);
|
2004-11-14 06:38:49 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
client_write_output(client);
|
|
|
|
free_cmd_list(client->cmd_list);
|
|
|
|
client->cmd_list = NULL;
|
|
|
|
client->cmd_list_OK = -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2006-07-30 05:43:38 +02:00
|
|
|
size_t len = strlen(line) + 1;
|
2008-08-28 20:02:59 +02:00
|
|
|
client->cmd_list_size += len;
|
|
|
|
if (client->cmd_list_size >
|
|
|
|
client_max_command_list_size) {
|
|
|
|
ERROR("client %i: command "
|
2008-03-26 11:38:07 +01:00
|
|
|
"list size (%lu) is "
|
2006-07-20 18:02:40 +02:00
|
|
|
"larger than the max "
|
2008-03-26 11:38:07 +01:00
|
|
|
"(%lu)\n",
|
2008-08-28 20:02:59 +02:00
|
|
|
client->num,
|
|
|
|
(unsigned long)client->cmd_list_size,
|
2008-03-26 11:38:26 +01:00
|
|
|
(unsigned long)
|
2008-08-28 20:02:59 +02:00
|
|
|
client_max_command_list_size);
|
2008-09-10 11:42:26 +02:00
|
|
|
return COMMAND_RETURN_CLOSE;
|
2006-07-30 05:43:38 +02:00
|
|
|
} else
|
2008-08-28 20:02:59 +02:00
|
|
|
new_cmd_list_ptr(client, line, len);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2008-08-28 20:02:59 +02:00
|
|
|
if (strcmp(line, CLIENT_LIST_MODE_BEGIN) == 0) {
|
|
|
|
client->cmd_list_OK = 0;
|
2004-11-14 06:38:49 +01:00
|
|
|
ret = 1;
|
2008-08-28 20:02:59 +02:00
|
|
|
} else if (strcmp(line, CLIENT_LIST_OK_MODE_BEGIN) == 0) {
|
|
|
|
client->cmd_list_OK = 1;
|
2004-11-14 06:38:49 +01:00
|
|
|
ret = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2008-08-28 20:02:59 +02:00
|
|
|
DEBUG("client %i: process command \"%s\"\n",
|
|
|
|
client->num, line);
|
2008-09-07 19:17:25 +02:00
|
|
|
ret = processCommand(client, line);
|
2008-08-28 20:02:59 +02:00
|
|
|
DEBUG("client %i: command returned %i\n",
|
|
|
|
client->num, ret);
|
2008-08-29 09:36:40 +02:00
|
|
|
|
|
|
|
if (ret == COMMAND_RETURN_CLOSE ||
|
2008-09-10 11:42:26 +02:00
|
|
|
client_is_expired(client))
|
2008-08-29 09:36:40 +02:00
|
|
|
return COMMAND_RETURN_CLOSE;
|
|
|
|
|
|
|
|
if (ret == 0)
|
2008-09-07 13:51:59 +02:00
|
|
|
command_success(client);
|
2008-08-29 09:36:40 +02:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
client_write_output(client);
|
2004-11-14 06:38:49 +01:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-11-14 06:38:49 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static int client_input_received(struct client *client, int bytesRead)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-10 11:42:30 +02:00
|
|
|
int ret;
|
2008-08-28 20:02:59 +02:00
|
|
|
char *buf_tail = &(client->buffer[client->bufferLength - 1]);
|
2004-11-14 06:38:49 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (bytesRead > 0) {
|
2008-08-28 20:02:59 +02:00
|
|
|
client->bufferLength++;
|
2004-11-14 06:38:49 +01:00
|
|
|
bytesRead--;
|
2006-07-30 05:43:38 +02:00
|
|
|
buf_tail++;
|
|
|
|
if (*buf_tail == '\n') {
|
|
|
|
*buf_tail = '\0';
|
2008-08-28 20:02:59 +02:00
|
|
|
if (client->bufferLength > client->bufferPos) {
|
2006-07-30 05:43:38 +02:00
|
|
|
if (*(buf_tail - 1) == '\r')
|
|
|
|
*(buf_tail - 1) = '\0';
|
|
|
|
}
|
2008-08-28 20:02:59 +02:00
|
|
|
ret = client_process_line(client);
|
2008-09-10 11:41:33 +02:00
|
|
|
if (ret == COMMAND_RETURN_KILL ||
|
|
|
|
ret == COMMAND_RETURN_CLOSE)
|
|
|
|
return ret;
|
2008-09-17 22:02:13 +02:00
|
|
|
if (client_is_expired(client))
|
|
|
|
return COMMAND_RETURN_CLOSE;
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
client->bufferPos = client->bufferLength;
|
2004-11-14 06:38:49 +01:00
|
|
|
}
|
2008-08-28 20:02:59 +02:00
|
|
|
if (client->bufferLength == CLIENT_MAX_BUFFER_LENGTH) {
|
|
|
|
if (client->bufferPos == 0) {
|
|
|
|
ERROR("client %i: buffer overflow\n",
|
|
|
|
client->num);
|
2008-09-10 11:42:26 +02:00
|
|
|
return COMMAND_RETURN_CLOSE;
|
2004-11-14 06:38:49 +01:00
|
|
|
}
|
2008-08-28 20:02:59 +02:00
|
|
|
if (client->cmd_list_OK >= 0 &&
|
|
|
|
client->cmd_list &&
|
|
|
|
!client->cmd_list_dup)
|
|
|
|
cmd_list_clone(client);
|
|
|
|
assert(client->bufferLength >= client->bufferPos
|
2008-03-26 11:38:26 +01:00
|
|
|
&& "bufferLength >= bufferPos");
|
2008-08-28 20:02:59 +02:00
|
|
|
client->bufferLength -= client->bufferPos;
|
|
|
|
memmove(client->buffer,
|
|
|
|
client->buffer + client->bufferPos,
|
|
|
|
client->bufferLength);
|
|
|
|
client->bufferPos = 0;
|
2004-11-14 06:38:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-10 11:42:30 +02:00
|
|
|
return 0;
|
2004-11-14 06:38:49 +01:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static int client_read(struct client *client)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-16 02:41:05 +01:00
|
|
|
int bytesRead;
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
bytesRead = read(client->fd,
|
|
|
|
client->buffer + client->bufferLength,
|
|
|
|
CLIENT_MAX_BUFFER_LENGTH - client->bufferLength);
|
2004-11-16 02:41:05 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (bytesRead > 0)
|
2008-08-28 20:02:59 +02:00
|
|
|
return client_input_received(client, bytesRead);
|
2008-09-10 11:43:09 +02:00
|
|
|
else if (bytesRead < 0 && errno == EINTR)
|
|
|
|
/* try again later, after select() */
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2008-09-10 11:43:09 +02:00
|
|
|
else
|
|
|
|
/* peer disconnected or I/O error */
|
|
|
|
return COMMAND_RETURN_CLOSE;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void client_manager_register_read_fd(fd_set * fds, int *fdmax)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-28 20:03:48 +02:00
|
|
|
struct client *client;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
FD_ZERO(fds);
|
2004-11-03 20:42:54 +01:00
|
|
|
addListenSocketsToFdSet(fds, fdmax);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:03:48 +02:00
|
|
|
list_for_each_entry(client, &clients, siblings) {
|
2008-08-28 20:20:10 +02:00
|
|
|
if (!client_is_expired(client) && !client->deferred_send) {
|
2008-08-28 20:03:48 +02:00
|
|
|
FD_SET(client->fd, fds);
|
|
|
|
if (*fdmax < client->fd)
|
|
|
|
*fdmax = client->fd;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void client_manager_register_write_fd(fd_set * fds, int *fdmax)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-28 20:03:48 +02:00
|
|
|
struct client *client;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
FD_ZERO(fds);
|
|
|
|
|
2008-08-28 20:03:48 +02:00
|
|
|
list_for_each_entry(client, &clients, siblings) {
|
2008-08-28 20:20:10 +02:00
|
|
|
if (client->fd >= 0 && !client_is_expired(client)
|
2008-08-28 20:03:48 +02:00
|
|
|
&& client->deferred_send) {
|
|
|
|
FD_SET(client->fd, fds);
|
|
|
|
if (*fdmax < client->fd)
|
|
|
|
*fdmax = client->fd;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-28 20:03:02 +02:00
|
|
|
int client_manager_io(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
fd_set rfds;
|
|
|
|
fd_set wfds;
|
2007-01-11 21:41:17 +01:00
|
|
|
fd_set efds;
|
2008-08-28 20:03:48 +02:00
|
|
|
struct client *client, *n;
|
2008-09-10 11:41:34 +02:00
|
|
|
int ret;
|
2008-08-28 20:20:10 +02:00
|
|
|
int fdmax = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
FD_ZERO( &efds );
|
|
|
|
client_manager_register_read_fd(&rfds, &fdmax);
|
|
|
|
client_manager_register_write_fd(&wfds, &fdmax);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
registered_IO_add_fds(&fdmax, &rfds, &wfds, &efds);
|
2005-03-12 23:38:49 +01:00
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
main_notify_lock();
|
2008-09-10 11:41:34 +02:00
|
|
|
ret = select(fdmax + 1, &rfds, &wfds, &efds, NULL);
|
2008-08-28 20:20:10 +02:00
|
|
|
main_notify_unlock();
|
2007-01-11 21:41:17 +01:00
|
|
|
|
2008-09-10 11:41:34 +02:00
|
|
|
if (ret < 0) {
|
2008-08-28 20:20:10 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
return 0;
|
2007-01-11 21:41:17 +01:00
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
FATAL("select() failed: %s\n", strerror(errno));
|
|
|
|
}
|
2005-03-12 23:38:49 +01:00
|
|
|
|
2008-09-10 11:41:34 +02:00
|
|
|
registered_IO_consume_fds(&ret, &rfds, &wfds, &efds);
|
2008-08-28 20:20:10 +02:00
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
getConnections(&rfds);
|
2005-03-12 23:38:49 +01:00
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
list_for_each_entry_safe(client, n, &clients, siblings) {
|
|
|
|
if (FD_ISSET(client->fd, &rfds)) {
|
2008-09-10 11:42:26 +02:00
|
|
|
ret = client_read(client);
|
|
|
|
if (ret == COMMAND_RETURN_KILL)
|
2008-08-28 20:20:10 +02:00
|
|
|
return COMMAND_RETURN_KILL;
|
2008-09-10 11:42:26 +02:00
|
|
|
if (ret == COMMAND_RETURN_CLOSE) {
|
|
|
|
client_close(client);
|
|
|
|
continue;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2008-09-10 11:42:26 +02:00
|
|
|
|
|
|
|
assert(!client_is_expired(client));
|
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
client->lastTime = time(NULL);
|
|
|
|
}
|
2008-09-06 15:31:55 +02:00
|
|
|
if (!client_is_expired(client) &&
|
|
|
|
FD_ISSET(client->fd, &wfds)) {
|
2008-08-28 20:20:10 +02:00
|
|
|
client_write_deferred(client);
|
|
|
|
client->lastTime = time(NULL);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-28 20:20:10 +02:00
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:03:02 +02:00
|
|
|
void client_manager_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
char *test;
|
|
|
|
ConfigParam *param;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
param = getConfigParam(CONF_CONN_TIMEOUT);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
2008-08-28 20:02:59 +02:00
|
|
|
client_timeout = strtol(param->value, &test, 10);
|
|
|
|
if (*test != '\0' || client_timeout <= 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("connection timeout \"%s\" is not a positive "
|
2006-07-20 18:02:40 +02:00
|
|
|
"integer, line %i\n", CONF_CONN_TIMEOUT,
|
|
|
|
param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
param = getConfigParam(CONF_MAX_CONN);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
2008-08-28 20:02:59 +02:00
|
|
|
client_max_connections = strtol(param->value, &test, 10);
|
|
|
|
if (*test != '\0' || client_max_connections <= 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("max connections \"%s\" is not a positive integer"
|
2006-07-20 18:02:40 +02:00
|
|
|
", line %i\n", param->value, param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2008-08-28 20:02:59 +02:00
|
|
|
client_max_connections = CLIENT_MAX_CONNECTIONS_DEFAULT;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
param = getConfigParam(CONF_MAX_COMMAND_LIST_SIZE);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
2008-03-26 11:38:26 +01:00
|
|
|
long tmp = strtol(param->value, &test, 10);
|
|
|
|
if (*test != '\0' || tmp <= 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("max command list size \"%s\" is not a positive "
|
2006-07-20 18:02:40 +02:00
|
|
|
"integer, line %i\n", param->value, param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2008-08-28 20:02:59 +02:00
|
|
|
client_max_command_list_size = tmp * 1024;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
param = getConfigParam(CONF_MAX_OUTPUT_BUFFER_SIZE);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
2008-03-26 11:38:26 +01:00
|
|
|
long tmp = strtol(param->value, &test, 10);
|
|
|
|
if (*test != '\0' || tmp <= 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("max output buffer size \"%s\" is not a positive "
|
2006-07-20 18:02:40 +02:00
|
|
|
"integer, line %i\n", param->value, param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2008-08-28 20:02:59 +02:00
|
|
|
client_max_output_buffer_size = tmp * 1024;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
list_cache = xcalloc(client_list_cache_size, sizeof(struct strnode));
|
2006-07-30 05:43:38 +02:00
|
|
|
list_cache_head = &(list_cache[0]);
|
2008-08-28 20:02:59 +02:00
|
|
|
list_cache_tail = &(list_cache[client_list_cache_size - 1]);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void client_close_all(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-28 20:03:48 +02:00
|
|
|
struct client *client, *n;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(client, n, &clients, siblings)
|
|
|
|
client_close(client);
|
|
|
|
num_clients = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
free(list_cache);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:03:02 +02:00
|
|
|
void client_manager_deinit(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-28 20:02:59 +02:00
|
|
|
client_close_all();
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
client_max_connections = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:03:02 +02:00
|
|
|
void client_manager_expire(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-28 20:03:48 +02:00
|
|
|
struct client *client, *n;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(client, n, &clients, siblings) {
|
2008-08-28 20:20:10 +02:00
|
|
|
if (client_is_expired(client)) {
|
2008-08-28 20:03:48 +02:00
|
|
|
DEBUG("client %i: expired\n", client->num);
|
|
|
|
client_close(client);
|
|
|
|
} else if (time(NULL) - client->lastTime >
|
|
|
|
client_timeout) {
|
|
|
|
DEBUG("client %i: timeout\n", client->num);
|
|
|
|
client_close(client);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
static void client_write_deferred(struct client *client)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-30 05:43:38 +02:00
|
|
|
struct sllnode *buf;
|
2008-03-26 11:38:07 +01:00
|
|
|
ssize_t ret = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
buf = client->deferred_send;
|
2006-07-30 05:43:38 +02:00
|
|
|
while (buf) {
|
2008-08-28 20:23:22 +02:00
|
|
|
assert(buf->size > 0);
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
ret = write(client->fd, buf->data, buf->size);
|
2006-07-30 05:43:38 +02:00
|
|
|
if (ret < 0)
|
2006-07-20 18:02:40 +02:00
|
|
|
break;
|
2008-03-26 11:38:07 +01:00
|
|
|
else if ((size_t)ret < buf->size) {
|
2008-08-28 20:02:59 +02:00
|
|
|
assert(client->deferred_bytes >= (size_t)ret);
|
|
|
|
client->deferred_bytes -= ret;
|
2006-08-01 06:18:41 +02:00
|
|
|
buf->data = (char *)buf->data + ret;
|
2006-07-30 05:43:38 +02:00
|
|
|
buf->size -= ret;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2006-07-30 05:43:38 +02:00
|
|
|
struct sllnode *tmp = buf;
|
2008-03-26 11:38:26 +01:00
|
|
|
size_t decr = (buf->size + sizeof(struct sllnode));
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
assert(client->deferred_bytes >= decr);
|
|
|
|
client->deferred_bytes -= decr;
|
2006-07-30 05:43:38 +02:00
|
|
|
buf = buf->next;
|
|
|
|
free(tmp);
|
2008-08-28 20:02:59 +02:00
|
|
|
client->deferred_send = buf;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2008-08-28 20:02:59 +02:00
|
|
|
client->lastTime = time(NULL);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
if (!client->deferred_send) {
|
|
|
|
DEBUG("client %i: buffer empty %lu\n", client->num,
|
|
|
|
(unsigned long)client->deferred_bytes);
|
|
|
|
assert(client->deferred_bytes == 0);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (ret < 0 && errno != EAGAIN && errno != EINTR) {
|
2008-08-28 20:02:59 +02:00
|
|
|
/* cause client to close */
|
|
|
|
DEBUG("client %i: problems flushing buffer\n",
|
|
|
|
client->num);
|
2008-08-28 20:20:10 +02:00
|
|
|
client_set_expired(client);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-28 20:03:51 +02:00
|
|
|
static void client_defer_output(struct client *client,
|
|
|
|
const void *data, size_t length)
|
|
|
|
{
|
2008-08-28 20:03:56 +02:00
|
|
|
struct sllnode **buf_r;
|
2008-08-28 20:03:51 +02:00
|
|
|
|
2008-08-28 20:23:22 +02:00
|
|
|
assert(length > 0);
|
2008-08-28 20:03:51 +02:00
|
|
|
|
|
|
|
client->deferred_bytes += sizeof(struct sllnode) + length;
|
|
|
|
if (client->deferred_bytes > client_max_output_buffer_size) {
|
|
|
|
ERROR("client %i: output buffer size (%lu) is "
|
|
|
|
"larger than the max (%lu)\n",
|
|
|
|
client->num,
|
|
|
|
(unsigned long)client->deferred_bytes,
|
|
|
|
(unsigned long)client_max_output_buffer_size);
|
|
|
|
/* cause client to close */
|
2008-08-28 20:20:10 +02:00
|
|
|
client_set_expired(client);
|
2008-08-28 20:03:54 +02:00
|
|
|
return;
|
2008-08-28 20:03:51 +02:00
|
|
|
}
|
2008-08-28 20:03:54 +02:00
|
|
|
|
2008-08-28 20:03:56 +02:00
|
|
|
buf_r = &client->deferred_send;
|
|
|
|
while (*buf_r != NULL)
|
|
|
|
buf_r = &(*buf_r)->next;
|
|
|
|
*buf_r = new_sllnode(data, length);
|
2008-08-28 20:03:51 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:24:51 +02:00
|
|
|
static void client_write_direct(struct client *client,
|
|
|
|
const char *data, size_t length)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-03-26 11:38:07 +01:00
|
|
|
ssize_t ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:23:22 +02:00
|
|
|
assert(length > 0);
|
2008-08-28 20:03:58 +02:00
|
|
|
assert(client->deferred_send == NULL);
|
|
|
|
|
|
|
|
if ((ret = write(client->fd, data, length)) < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR) {
|
2008-08-28 20:20:04 +02:00
|
|
|
client_defer_output(client, data, length);
|
2008-08-28 20:03:58 +02:00
|
|
|
} else {
|
|
|
|
DEBUG("client %i: problems writing\n", client->num);
|
2008-08-28 20:20:10 +02:00
|
|
|
client_set_expired(client);
|
2008-08-28 20:03:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if ((size_t)ret < client->send_buf_used) {
|
2008-08-28 20:20:04 +02:00
|
|
|
client_defer_output(client, data + ret, length - ret);
|
2008-08-28 20:03:58 +02:00
|
|
|
}
|
|
|
|
|
2008-08-28 20:20:04 +02:00
|
|
|
if (client->deferred_send)
|
2008-08-28 20:03:58 +02:00
|
|
|
DEBUG("client %i: buffer created\n", client->num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void client_write_output(struct client *client)
|
|
|
|
{
|
2008-08-28 20:20:10 +02:00
|
|
|
if (client_is_expired(client) || !client->send_buf_used)
|
2004-02-24 00:41:20 +01:00
|
|
|
return;
|
|
|
|
|
2008-08-28 20:03:51 +02:00
|
|
|
if (client->deferred_send != NULL)
|
|
|
|
client_defer_output(client, client->send_buf,
|
|
|
|
client->send_buf_used);
|
2008-08-28 20:03:58 +02:00
|
|
|
else
|
2008-09-07 13:24:51 +02:00
|
|
|
client_write_direct(client, client->send_buf,
|
|
|
|
client->send_buf_used);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-28 20:02:59 +02:00
|
|
|
client->send_buf_used = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2007-01-11 21:41:17 +01:00
|
|
|
|
2008-09-07 13:24:51 +02:00
|
|
|
void client_write(struct client *client, const char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
size_t copylen;
|
|
|
|
|
|
|
|
/* if the client is going to be closed, do nothing */
|
|
|
|
if (client_is_expired(client))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (buflen > 0 && !client_is_expired(client)) {
|
|
|
|
size_t left;
|
|
|
|
|
|
|
|
assert(client->send_buf_size >= client->send_buf_used);
|
|
|
|
left = client->send_buf_size - client->send_buf_used;
|
|
|
|
|
|
|
|
copylen = buflen > left ? left : buflen;
|
|
|
|
memcpy(client->send_buf + client->send_buf_used, buffer,
|
|
|
|
copylen);
|
|
|
|
buflen -= copylen;
|
|
|
|
client->send_buf_used += copylen;
|
|
|
|
buffer += copylen;
|
|
|
|
if (client->send_buf_used >= client->send_buf_size)
|
|
|
|
client_write_output(client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void client_puts(struct client *client, const char *s)
|
|
|
|
{
|
|
|
|
client_write(client, s, strlen(s));
|
|
|
|
}
|
2008-09-07 13:25:54 +02:00
|
|
|
|
|
|
|
void client_vprintf(struct client *client, const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
va_list tmp;
|
|
|
|
int length;
|
|
|
|
char *buffer;
|
|
|
|
|
|
|
|
va_copy(tmp, args);
|
|
|
|
length = vsnprintf(NULL, 0, fmt, tmp);
|
|
|
|
va_end(tmp);
|
|
|
|
|
|
|
|
if (length <= 0)
|
|
|
|
/* wtf.. */
|
|
|
|
return;
|
|
|
|
|
|
|
|
buffer = xmalloc(length + 1);
|
|
|
|
vsnprintf(buffer, length + 1, fmt, args);
|
|
|
|
client_write(client, buffer, length);
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
mpd_fprintf void client_printf(struct client *client, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
client_vprintf(client, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|