2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2006-07-14 21:37:45 +02:00
|
|
|
* (c)2003-2006 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "interface.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "listen.h"
|
|
|
|
#include "playlist.h"
|
|
|
|
#include "permission.h"
|
2004-11-16 02:41:05 +01:00
|
|
|
#include "sig_handlers.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
2004-04-01 05:48:51 +02:00
|
|
|
#include <sys/select.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
#include <sys/time.h>
|
2004-04-01 05:48:51 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2004-06-04 02:49:33 +02:00
|
|
|
#define GREETING "OK MPD"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-11-14 06:54:00 +01:00
|
|
|
#define INTERFACE_MAX_BUFFER_LENGTH (40960)
|
|
|
|
#define INTERFACE_LIST_MODE_BEGIN "command_list_begin"
|
|
|
|
#define INTERFACE_LIST_OK_MODE_BEGIN "command_list_ok_begin"
|
|
|
|
#define INTERFACE_LIST_MODE_END "command_list_end"
|
|
|
|
#define INTERFACE_DEFAULT_OUT_BUFFER_SIZE (4096)
|
|
|
|
#define INTERFACE_TIMEOUT_DEFAULT (60)
|
|
|
|
#define INTERFACE_MAX_CONNECTIONS_DEFAULT (10)
|
2004-10-28 07:14:55 +02:00
|
|
|
#define INTERFACE_MAX_COMMAND_LIST_DEFAULT (2048*1024)
|
2005-04-27 17:54:26 +02:00
|
|
|
#define INTERFACE_MAX_OUTPUT_BUFFER_SIZE_DEFAULT (8192*1024)
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2004-10-31 16:37:54 +01:00
|
|
|
/* set this to zero to indicate we have no possible interfaces */
|
2006-07-20 18:02:40 +02:00
|
|
|
static int interface_max_connections = 0; /*INTERFACE_MAX_CONNECTIONS_DEFAULT; */
|
2004-10-28 07:14:55 +02:00
|
|
|
static int interface_timeout = INTERFACE_TIMEOUT_DEFAULT;
|
2006-07-20 18:02:40 +02:00
|
|
|
static size_t interface_max_command_list_size =
|
|
|
|
INTERFACE_MAX_COMMAND_LIST_DEFAULT;
|
2004-10-28 07:14:55 +02:00
|
|
|
static size_t interface_max_output_buffer_size =
|
2006-07-20 18:02:40 +02:00
|
|
|
INTERFACE_MAX_OUTPUT_BUFFER_SIZE_DEFAULT;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
typedef struct _Interface {
|
2004-11-14 06:38:49 +01:00
|
|
|
char buffer[INTERFACE_MAX_BUFFER_LENGTH];
|
2004-02-24 00:41:20 +01:00
|
|
|
int bufferLength;
|
2004-11-14 06:38:49 +01:00
|
|
|
int bufferPos;
|
2006-07-20 18:02:40 +02:00
|
|
|
int fd; /* file descriptor */
|
|
|
|
FILE *fp; /* file pointer */
|
|
|
|
int open; /* open/used */
|
2006-07-17 23:46:32 +02:00
|
|
|
int permission;
|
2004-02-24 00:41:20 +01:00
|
|
|
time_t lastTime;
|
2006-07-20 18:02:40 +02:00
|
|
|
List *commandList; /* for when in list mode */
|
|
|
|
int commandListOK; /* print OK after each command execution */
|
|
|
|
size_t commandListSize; /* mem commandList consumes */
|
|
|
|
List *bufferList; /* for output if client is slow */
|
|
|
|
size_t outputBufferSize; /* mem bufferList consumes */
|
|
|
|
int expired; /* set whether this interface should be closed on next
|
|
|
|
check of old interfaces */
|
|
|
|
int num; /* interface number */
|
|
|
|
char *outBuffer;
|
2004-02-24 00:41:20 +01:00
|
|
|
int outBuflen;
|
|
|
|
int outBufSize;
|
|
|
|
} Interface;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static Interface *interfaces = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-14 21:06:25 +02:00
|
|
|
static void flushInterfaceBuffer(Interface * interface);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-14 21:06:25 +02:00
|
|
|
static void printInterfaceOutBuffer(Interface * interface);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void openInterface(Interface * interface, int fd)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int flags;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
assert(interface->open == 0);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
interface->bufferLength = 0;
|
2004-11-14 06:38:49 +01:00
|
|
|
interface->bufferPos = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
interface->fd = fd;
|
|
|
|
/* fcntl(interface->fd,F_SETOWN,(int)getpid()); */
|
2006-07-20 18:02:40 +02:00
|
|
|
while ((flags = fcntl(fd, F_GETFL)) < 0 && errno == EINTR) ;
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
while (fcntl(interface->fd, F_SETFL, flags) < 0 && errno == EINTR) ;
|
|
|
|
while ((interface->fp = fdopen(fd, "rw")) == NULL && errno == EINTR) ;
|
2004-02-24 00:41:20 +01:00
|
|
|
interface->open = 1;
|
|
|
|
interface->lastTime = time(NULL);
|
|
|
|
interface->commandList = NULL;
|
|
|
|
interface->bufferList = NULL;
|
|
|
|
interface->expired = 0;
|
|
|
|
interface->outputBufferSize = 0;
|
|
|
|
interface->outBuflen = 0;
|
|
|
|
|
|
|
|
interface->permission = getDefaultPermissions();
|
|
|
|
|
|
|
|
interface->outBufSize = INTERFACE_DEFAULT_OUT_BUFFER_SIZE;
|
|
|
|
#ifdef SO_SNDBUF
|
|
|
|
{
|
|
|
|
int getSize;
|
2006-07-17 23:46:32 +02:00
|
|
|
unsigned int sockOptLen = sizeof(int);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (getsockopt(interface->fd, SOL_SOCKET, SO_SNDBUF,
|
|
|
|
(char *)&getSize, &sockOptLen) < 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
DEBUG("problem getting sockets send buffer size\n");
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (getSize <= 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
DEBUG("sockets send buffer size is not positive\n");
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
interface->outBufSize = getSize;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
interface->outBuffer = malloc(interface->outBufSize);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2004-06-04 02:49:33 +02:00
|
|
|
myfprintf(interface->fp, "%s %s\n", GREETING, VERSION);
|
2004-02-24 00:41:20 +01:00
|
|
|
printInterfaceOutBuffer(interface);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void closeInterface(Interface * interface)
|
|
|
|
{
|
|
|
|
if (!interface->open)
|
|
|
|
return;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
interface->open = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (fclose(interface->fp) && errno == EINTR) ;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (interface->commandList)
|
|
|
|
freeList(interface->commandList);
|
|
|
|
if (interface->bufferList)
|
|
|
|
freeList(interface->bufferList);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
free(interface->outBuffer);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
SECURE("interface %i: closed\n", interface->num);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void openAInterface(int fd, struct sockaddr *addr)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < interface_max_connections && interfaces[i].open; i++) ;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (i == interface_max_connections) {
|
2004-02-24 00:41:20 +01:00
|
|
|
ERROR("Max Connections Reached!\n");
|
2006-07-20 18:02:40 +02:00
|
|
|
while (close(fd) && errno == EINTR) ;
|
|
|
|
} else {
|
|
|
|
SECURE("interface %i: opened from ", i);
|
|
|
|
switch (addr->sa_family) {
|
2004-02-24 00:41:20 +01:00
|
|
|
case AF_INET:
|
2004-02-25 20:13:10 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
char *host = inet_ntoa(((struct sockaddr_in *)
|
|
|
|
addr)->sin_addr);
|
|
|
|
if (host) {
|
|
|
|
SECURE("%s\n", host);
|
|
|
|
} else {
|
2004-02-25 20:13:10 +01:00
|
|
|
SECURE("error getting ipv4 address\n");
|
|
|
|
}
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
char host[INET6_ADDRSTRLEN + 1];
|
|
|
|
memset(host, 0, INET6_ADDRSTRLEN + 1);
|
|
|
|
if (inet_ntop(AF_INET6, (void *)
|
|
|
|
&(((struct sockaddr_in6 *)addr)->
|
|
|
|
sin6_addr), host,
|
|
|
|
INET6_ADDRSTRLEN)) {
|
|
|
|
SECURE("%s\n", host);
|
|
|
|
} else {
|
2004-02-25 20:13:10 +01:00
|
|
|
SECURE("error getting ipv6 address\n");
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case AF_UNIX:
|
|
|
|
SECURE("local connection\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SECURE("unknown\n");
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
openInterface(&(interfaces[i]), fd);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int processLineOfInput(Interface * interface)
|
|
|
|
{
|
2004-11-14 06:38:49 +01:00
|
|
|
int ret = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *line = interface->buffer + interface->bufferPos;
|
2004-11-14 06:38:49 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (interface->bufferLength - interface->bufferPos > 1) {
|
|
|
|
if (interface->buffer[interface->bufferLength - 2] == '\r') {
|
|
|
|
interface->buffer[interface->bufferLength - 2] = '\0';
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-11-14 06:38:49 +01:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (interface->commandList) {
|
|
|
|
if (strcmp(line, INTERFACE_LIST_MODE_END) == 0) {
|
2004-11-14 06:38:49 +01:00
|
|
|
DEBUG("interface %i: process command "
|
2006-07-20 18:02:40 +02:00
|
|
|
"list\n", interface->num);
|
|
|
|
ret = processListOfCommands(interface->fp,
|
|
|
|
&(interface->permission),
|
|
|
|
&(interface->expired),
|
|
|
|
interface->commandListOK,
|
|
|
|
interface->commandList);
|
2004-11-14 06:38:49 +01:00
|
|
|
DEBUG("interface %i: process command "
|
2006-07-20 18:02:40 +02:00
|
|
|
"list returned %i\n", interface->num, ret);
|
|
|
|
if (ret == 0)
|
|
|
|
commandSuccess(interface->fp);
|
|
|
|
else if (ret == COMMAND_RETURN_CLOSE
|
|
|
|
|| interface->expired) {
|
|
|
|
|
2004-11-14 06:38:49 +01:00
|
|
|
closeInterface(interface);
|
|
|
|
}
|
|
|
|
printInterfaceOutBuffer(interface);
|
|
|
|
|
|
|
|
freeList(interface->commandList);
|
|
|
|
interface->commandList = NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
interface->commandListSize += sizeof(ListNode);
|
|
|
|
interface->commandListSize += strlen(line) + 1;
|
|
|
|
if (interface->commandListSize >
|
|
|
|
interface_max_command_list_size) {
|
2004-11-14 06:38:49 +01:00
|
|
|
ERROR("interface %i: command "
|
2006-07-20 18:02:40 +02:00
|
|
|
"list size (%lli) is "
|
|
|
|
"larger than the max "
|
|
|
|
"(%lli)\n",
|
|
|
|
interface->num,
|
|
|
|
(long long)interface->
|
|
|
|
commandListSize, (long long)
|
|
|
|
interface_max_command_list_size);
|
2004-11-14 06:38:49 +01:00
|
|
|
closeInterface(interface);
|
2006-05-10 04:17:47 +02:00
|
|
|
ret = COMMAND_RETURN_CLOSE;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2004-11-14 06:38:49 +01:00
|
|
|
insertInListWithoutKey(interface->commandList,
|
2006-07-20 18:02:40 +02:00
|
|
|
strdup(line));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
if (strcmp(line, INTERFACE_LIST_MODE_BEGIN) == 0) {
|
2004-11-14 06:38:49 +01:00
|
|
|
interface->commandList = makeList(free, 1);
|
|
|
|
interface->commandListSize = sizeof(List);
|
2006-07-20 18:02:40 +02:00
|
|
|
interface->commandListOK = 0;
|
2004-11-14 06:38:49 +01:00
|
|
|
ret = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (strcmp(line, INTERFACE_LIST_OK_MODE_BEGIN) == 0) {
|
2004-11-14 06:38:49 +01:00
|
|
|
interface->commandList = makeList(free, 1);
|
|
|
|
interface->commandListSize = sizeof(List);
|
2006-07-20 18:02:40 +02:00
|
|
|
interface->commandListOK = 1;
|
2004-11-14 06:38:49 +01:00
|
|
|
ret = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2004-11-14 06:38:49 +01:00
|
|
|
DEBUG("interface %i: process command \"%s\"\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
interface->num, line);
|
2004-11-14 06:38:49 +01:00
|
|
|
ret = processCommand(interface->fp,
|
2006-07-20 18:02:40 +02:00
|
|
|
&(interface->permission), line);
|
2004-11-14 06:38:49 +01:00
|
|
|
DEBUG("interface %i: command returned %i\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
interface->num, ret);
|
|
|
|
if (ret == 0)
|
|
|
|
commandSuccess(interface->fp);
|
|
|
|
else if (ret == COMMAND_RETURN_CLOSE
|
|
|
|
|| interface->expired) {
|
2004-11-14 06:38:49 +01:00
|
|
|
closeInterface(interface);
|
|
|
|
}
|
|
|
|
printInterfaceOutBuffer(interface);
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-11-14 06:38:49 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int processBytesRead(Interface * interface, int bytesRead)
|
|
|
|
{
|
2005-02-07 05:01:41 +01:00
|
|
|
int ret = 0;
|
2004-11-14 06:38:49 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (bytesRead > 0) {
|
2004-11-14 06:38:49 +01:00
|
|
|
interface->bufferLength++;
|
|
|
|
bytesRead--;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (interface->buffer[interface->bufferLength - 1] == '\n') {
|
|
|
|
interface->buffer[interface->bufferLength - 1] = '\0';
|
2006-07-14 20:47:55 +02:00
|
|
|
ret = processLineOfInput(interface);
|
2004-11-14 06:38:49 +01:00
|
|
|
interface->bufferPos = interface->bufferLength;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (interface->bufferLength == INTERFACE_MAX_BUFFER_LENGTH) {
|
|
|
|
if (interface->bufferPos == 0) {
|
2004-11-14 06:38:49 +01:00
|
|
|
ERROR("interface %i: buffer overflow\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
interface->num);
|
2004-11-14 06:54:00 +01:00
|
|
|
closeInterface(interface);
|
2004-11-14 06:38:49 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
interface->bufferLength -= interface->bufferPos;
|
|
|
|
memmove(interface->buffer,
|
|
|
|
interface->buffer + interface->bufferPos,
|
|
|
|
interface->bufferLength);
|
2004-11-14 06:38:49 +01:00
|
|
|
interface->bufferPos = 0;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ret == COMMAND_RETURN_KILL || ret == COMMAND_RETURN_CLOSE) {
|
|
|
|
return ret;
|
2005-08-18 18:46:41 +02:00
|
|
|
}
|
|
|
|
|
2004-11-14 06:38:49 +01:00
|
|
|
}
|
|
|
|
|
2005-02-07 05:01:41 +01:00
|
|
|
return ret;
|
2004-11-14 06:38:49 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int interfaceReadInput(Interface * interface)
|
|
|
|
{
|
2004-11-16 02:41:05 +01:00
|
|
|
int bytesRead;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
bytesRead = read(interface->fd,
|
|
|
|
interface->buffer + interface->bufferLength,
|
|
|
|
INTERFACE_MAX_BUFFER_LENGTH - interface->bufferLength);
|
2004-11-16 02:41:05 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (bytesRead > 0)
|
|
|
|
return processBytesRead(interface, bytesRead);
|
|
|
|
else if (bytesRead == 0 || (bytesRead < 0 && errno != EINTR)) {
|
2004-11-16 02:41:05 +01:00
|
|
|
closeInterface(interface);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
return 0;
|
2004-11-14 06:38:49 +01:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void addInterfacesReadyToReadAndListenSocketToFdSet(fd_set * fds,
|
|
|
|
int *fdmax)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
FD_ZERO(fds);
|
2004-11-03 20:42:54 +01:00
|
|
|
addListenSocketsToFdSet(fds, fdmax);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < interface_max_connections; i++) {
|
|
|
|
if (interfaces[i].open && !interfaces[i].expired
|
|
|
|
&& !interfaces[i].bufferList) {
|
|
|
|
FD_SET(interfaces[i].fd, fds);
|
|
|
|
if (*fdmax < interfaces[i].fd)
|
|
|
|
*fdmax = interfaces[i].fd;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void addInterfacesForBufferFlushToFdSet(fd_set * fds, int *fdmax)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
FD_ZERO(fds);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < interface_max_connections; i++) {
|
|
|
|
if (interfaces[i].open && !interfaces[i].expired
|
|
|
|
&& interfaces[i].bufferList) {
|
|
|
|
FD_SET(interfaces[i].fd, fds);
|
|
|
|
if (*fdmax < interfaces[i].fd)
|
|
|
|
*fdmax = interfaces[i].fd;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void closeNextErroredInterface(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
fd_set fds;
|
|
|
|
struct timeval tv;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < interface_max_connections; i++) {
|
|
|
|
if (interfaces[i].open) {
|
2004-02-24 00:41:20 +01:00
|
|
|
FD_ZERO(&fds);
|
2006-07-20 18:02:40 +02:00
|
|
|
FD_SET(interfaces[i].fd, &fds);
|
|
|
|
if (select(FD_SETSIZE, &fds, NULL, NULL, &tv) < 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
closeInterface(&interfaces[i]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int doIOForInterfaces(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
fd_set rfds;
|
|
|
|
fd_set wfds;
|
|
|
|
struct timeval tv;
|
|
|
|
int i;
|
|
|
|
int selret;
|
2005-03-12 23:38:49 +01:00
|
|
|
int fdmax;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (1) {
|
2005-03-12 23:38:49 +01:00
|
|
|
fdmax = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
addInterfacesReadyToReadAndListenSocketToFdSet(&rfds, &fdmax);
|
|
|
|
addInterfacesForBufferFlushToFdSet(&wfds, &fdmax);
|
2005-03-12 23:38:49 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
selret = select(fdmax + 1, &rfds, &wfds, NULL, &tv);
|
2005-03-12 23:38:49 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (selret == 0 || (selret < 0 && errno == EINTR))
|
|
|
|
break;
|
2005-03-12 23:38:49 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (selret < 0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
closeNextErroredInterface();
|
|
|
|
continue;
|
|
|
|
}
|
2005-03-12 23:38:49 +01:00
|
|
|
|
|
|
|
getConnections(&rfds);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < interface_max_connections; i++) {
|
|
|
|
if (interfaces[i].open
|
|
|
|
&& FD_ISSET(interfaces[i].fd, &rfds)) {
|
|
|
|
if (COMMAND_RETURN_KILL ==
|
|
|
|
interfaceReadInput(&(interfaces[i]))) {
|
2004-02-24 00:41:20 +01:00
|
|
|
return COMMAND_RETURN_KILL;
|
|
|
|
}
|
|
|
|
interfaces[i].lastTime = time(NULL);
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (interfaces[i].open
|
|
|
|
&& FD_ISSET(interfaces[i].fd, &wfds)) {
|
2004-02-24 00:41:20 +01:00
|
|
|
flushInterfaceBuffer(&interfaces[i]);
|
|
|
|
interfaces[i].lastTime = time(NULL);
|
|
|
|
}
|
|
|
|
}
|
2005-03-12 23:38:49 +01:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void initInterfaces(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
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) {
|
|
|
|
interface_timeout = strtol(param->value, &test, 10);
|
|
|
|
if (*test != '\0' || interface_timeout <= 0) {
|
2004-10-28 07:14:55 +02:00
|
|
|
ERROR("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
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
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) {
|
2004-10-28 07:14:55 +02:00
|
|
|
interface_max_connections = strtol(param->value, &test, 10);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != '\0' || interface_max_connections <= 0) {
|
2004-10-28 07:14:55 +02:00
|
|
|
ERROR("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
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
interface_max_connections = INTERFACE_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) {
|
|
|
|
interface_max_command_list_size = strtoll(param->value,
|
|
|
|
&test, 10);
|
|
|
|
if (*test != '\0' || interface_max_command_list_size <= 0) {
|
2004-10-28 07:14:55 +02:00
|
|
|
ERROR("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
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
interface_max_command_list_size *= 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) {
|
2004-10-28 07:14:55 +02:00
|
|
|
interface_max_output_buffer_size = strtoll(param->value, &test,
|
2006-07-20 18:02:40 +02:00
|
|
|
10);
|
|
|
|
if (*test != '\0' || interface_max_output_buffer_size <= 0) {
|
2004-10-28 07:14:55 +02:00
|
|
|
ERROR("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
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
interface_max_output_buffer_size *= 1024;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
interfaces = malloc(sizeof(Interface) * interface_max_connections);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < interface_max_connections; i++) {
|
2004-02-24 00:41:20 +01:00
|
|
|
interfaces[i].open = 0;
|
|
|
|
interfaces[i].num = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void closeAllInterfaces(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
fflush(NULL);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < interface_max_connections; i++) {
|
|
|
|
if (interfaces[i].open) {
|
2004-02-24 00:41:20 +01:00
|
|
|
closeInterface(&(interfaces[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void freeAllInterfaces(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
closeAllInterfaces();
|
|
|
|
|
|
|
|
free(interfaces);
|
2004-07-12 16:58:26 +02:00
|
|
|
|
|
|
|
interface_max_connections = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void closeOldInterfaces(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < interface_max_connections; i++) {
|
|
|
|
if (interfaces[i].open) {
|
|
|
|
if (interfaces[i].expired) {
|
|
|
|
DEBUG("interface %i: expired\n", i);
|
2005-03-19 13:33:34 +01:00
|
|
|
closeInterface(&(interfaces[i]));
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (time(NULL) - interfaces[i].lastTime >
|
|
|
|
interface_timeout) {
|
|
|
|
DEBUG("interface %i: timeout\n", i);
|
2005-03-19 13:33:34 +01:00
|
|
|
closeInterface(&(interfaces[i]));
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void flushInterfaceBuffer(Interface * interface)
|
|
|
|
{
|
|
|
|
ListNode *node = NULL;
|
|
|
|
char *str;
|
2004-02-24 00:41:20 +01:00
|
|
|
int ret = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while ((node = interface->bufferList->firstNode)) {
|
2004-02-24 00:41:20 +01:00
|
|
|
str = (char *)node->data;
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((ret = write(interface->fd, str, strlen(str))) < 0)
|
|
|
|
break;
|
|
|
|
else if (ret < strlen(str)) {
|
|
|
|
interface->outputBufferSize -= ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
str = strdup(&str[ret]);
|
|
|
|
free(node->data);
|
|
|
|
node->data = str;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
interface->outputBufferSize -= strlen(str) + 1;
|
|
|
|
interface->outputBufferSize -= sizeof(ListNode);
|
|
|
|
deleteNodeFromList(interface->bufferList, node);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
interface->lastTime = time(NULL);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!interface->bufferList->firstNode) {
|
|
|
|
DEBUG("interface %i: buffer empty\n", interface->num);
|
2004-02-24 00:41:20 +01:00
|
|
|
freeList(interface->bufferList);
|
|
|
|
interface->bufferList = NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (ret < 0 && errno != EAGAIN && errno != EINTR) {
|
2004-02-24 00:41:20 +01:00
|
|
|
/* cause interface to close */
|
|
|
|
DEBUG("interface %i: problems flushing buffer\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
interface->num);
|
2004-02-24 00:41:20 +01:00
|
|
|
freeList(interface->bufferList);
|
|
|
|
interface->bufferList = NULL;
|
|
|
|
interface->expired = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int interfacePrintWithFD(int fd, char *buffer, int buflen)
|
|
|
|
{
|
2004-06-15 20:06:21 +02:00
|
|
|
static int i = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
int copylen;
|
2006-07-20 18:02:40 +02:00
|
|
|
Interface *interface;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (i >= interface_max_connections ||
|
|
|
|
!interfaces[i].open || interfaces[i].fd != fd) {
|
|
|
|
for (i = 0; i < interface_max_connections; i++) {
|
|
|
|
if (interfaces[i].open && interfaces[i].fd == fd)
|
|
|
|
break;
|
2004-06-15 20:06:21 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (i == interface_max_connections)
|
|
|
|
return -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if fd isn't found or interfaces is going to be closed, do nothing */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (interfaces[i].expired)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
interface = interfaces + i;
|
|
|
|
|
|
|
|
while (buflen > 0 && !interface->expired) {
|
|
|
|
copylen = buflen >
|
|
|
|
interface->outBufSize - interface->outBuflen ?
|
|
|
|
interface->outBufSize - interface->outBuflen : buflen;
|
|
|
|
memcpy(interface->outBuffer + interface->outBuflen, buffer,
|
|
|
|
copylen);
|
|
|
|
buflen -= copylen;
|
|
|
|
interface->outBuflen += copylen;
|
|
|
|
buffer += copylen;
|
|
|
|
if (interface->outBuflen >= interface->outBufSize) {
|
2004-02-24 00:41:20 +01:00
|
|
|
printInterfaceOutBuffer(interface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void printInterfaceOutBuffer(Interface * interface)
|
|
|
|
{
|
|
|
|
char *buffer;
|
2004-02-24 00:41:20 +01:00
|
|
|
int ret;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!interface->open || interface->expired || !interface->outBuflen) {
|
2004-02-24 00:41:20 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (interface->bufferList) {
|
|
|
|
interface->outputBufferSize += sizeof(ListNode);
|
|
|
|
interface->outputBufferSize += interface->outBuflen + 1;
|
|
|
|
if (interface->outputBufferSize >
|
|
|
|
interface_max_output_buffer_size) {
|
2004-02-24 00:41:20 +01:00
|
|
|
ERROR("interface %i: output buffer size (%lli) is "
|
2006-07-20 18:02:40 +02:00
|
|
|
"larger than the max (%lli)\n",
|
|
|
|
interface->num,
|
|
|
|
(long long)interface->outputBufferSize,
|
|
|
|
(long long)interface_max_output_buffer_size);
|
2004-02-24 00:41:20 +01:00
|
|
|
/* cause interface to close */
|
|
|
|
freeList(interface->bufferList);
|
|
|
|
interface->bufferList = NULL;
|
|
|
|
interface->expired = 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
buffer = malloc(interface->outBuflen + 1);
|
|
|
|
memcpy(buffer, interface->outBuffer,
|
|
|
|
interface->outBuflen);
|
2004-02-24 00:41:20 +01:00
|
|
|
buffer[interface->outBuflen] = '\0';
|
2006-07-20 18:02:40 +02:00
|
|
|
insertInListWithoutKey(interface->bufferList,
|
|
|
|
(void *)buffer);
|
2004-02-24 00:41:20 +01:00
|
|
|
flushInterfaceBuffer(interface);
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
if ((ret = write(interface->fd, interface->outBuffer,
|
|
|
|
interface->outBuflen)) < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR) {
|
|
|
|
buffer = malloc(interface->outBuflen + 1);
|
|
|
|
memcpy(buffer, interface->outBuffer,
|
|
|
|
interface->outBuflen);
|
2004-02-24 00:41:20 +01:00
|
|
|
buffer[interface->outBuflen] = '\0';
|
2004-11-11 03:59:16 +01:00
|
|
|
interface->bufferList = makeList(free, 1);
|
2004-02-24 00:41:20 +01:00
|
|
|
insertInListWithoutKey(interface->bufferList,
|
2006-07-20 18:02:40 +02:00
|
|
|
(void *)buffer);
|
|
|
|
} else {
|
2004-02-24 00:41:20 +01:00
|
|
|
DEBUG("interface %i: problems writing\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
interface->num);
|
2004-02-24 00:41:20 +01:00
|
|
|
interface->expired = 1;
|
|
|
|
return;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (ret < interface->outBuflen) {
|
|
|
|
buffer = malloc(interface->outBuflen - ret + 1);
|
|
|
|
memcpy(buffer, interface->outBuffer + ret,
|
|
|
|
interface->outBuflen - ret);
|
|
|
|
buffer[interface->outBuflen - ret] = '\0';
|
2004-11-11 03:59:16 +01:00
|
|
|
interface->bufferList = makeList(free, 1);
|
2006-07-20 18:02:40 +02:00
|
|
|
insertInListWithoutKey(interface->bufferList, buffer);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
/* if we needed to create buffer, initialize bufferSize info */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (interface->bufferList) {
|
|
|
|
DEBUG("interface %i: buffer created\n", interface->num);
|
2004-02-24 00:41:20 +01:00
|
|
|
interface->outputBufferSize = sizeof(List);
|
2006-07-20 18:02:40 +02:00
|
|
|
interface->outputBufferSize += sizeof(ListNode);
|
|
|
|
interface->outputBufferSize += strlen((char *)
|
|
|
|
interface->
|
|
|
|
bufferList->
|
|
|
|
firstNode->data) +
|
|
|
|
1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface->outBuflen = 0;
|
|
|
|
}
|