clamav/clamd/tcpserver.c

123 lines
3.4 KiB
C
Raw Normal View History

2003-07-29 15:48:06 +00:00
/*
* Copyright (C) 2007-2009 Sourcefire, Inc.
*
* Authors: Tomasz Kojm, Török Edvin
2003-07-29 15:48:06 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
2003-07-29 15:48:06 +00:00
*
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
2003-07-29 15:48:06 +00:00
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
2003-07-29 15:48:06 +00:00
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifndef _WIN32
2003-07-29 15:48:06 +00:00
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
#include <errno.h>
2003-07-29 15:48:06 +00:00
2006-09-05 20:45:39 +00:00
#include "libclamav/clamav.h"
#include "shared/optparser.h"
2006-09-05 20:45:39 +00:00
#include "shared/output.h"
2010-01-28 23:36:37 +01:00
#include "shared/misc.h"
2006-09-05 20:45:39 +00:00
2003-07-29 15:48:06 +00:00
#include "others.h"
#include "server.h"
#include "tcpserver.h"
2014-01-25 21:50:33 -05:00
int tcpserver(int **lsockets, unsigned int *nlsockets, char *ipaddr, const struct optstruct *opts)
2003-07-29 15:48:06 +00:00
{
2014-01-25 21:50:33 -05:00
struct addrinfo hints, *info, *p;
int *sockets;
int sockfd, backlog;
2014-01-25 21:50:33 -05:00
int *t;
char *estr, port[10];
int yes = 1;
int res;
2003-07-29 15:48:06 +00:00
2014-01-25 21:50:33 -05:00
sockets = *lsockets;
2003-07-29 15:48:06 +00:00
2014-01-25 21:50:33 -05:00
snprintf(port, sizeof(port), "%lld", optget(opts, "TCPSocket")->numarg);
2003-07-29 15:48:06 +00:00
2014-01-25 21:50:33 -05:00
memset(&hints, 0x00, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
2003-10-27 21:17:16 +00:00
2014-01-25 21:50:33 -05:00
if ((res = getaddrinfo(ipaddr, port, &hints, &info))) {
logg("!TCP: getaddrinfo: %s\n", gai_strerror(res));
return -1;
2003-09-29 11:44:52 +00:00
}
2003-07-29 15:48:06 +00:00
2014-01-25 21:50:33 -05:00
for (p = info; p != NULL; p = p->ai_next) {
t = realloc(sockets, sizeof(int) * (*nlsockets + 1));
if (!(t)) {
return -1;
}
sockets = t;
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
estr = strerror(errno);
logg("!TCP: socket() error: %s\n", estr);
continue;
}
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &yes, sizeof(yes)) == -1) {
logg("!TCP: setsocktopt(SO_REUSEADDR) error: %s\n", strerror(errno));
}
if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
estr = strerror(errno);
logg("!TCP: bind() error when trying to listen on [%s]:%s: %s\n", ipaddr, port, estr);
closesocket(sockfd);
2003-07-29 15:48:06 +00:00
2014-01-25 21:50:33 -05:00
continue;
} else {
if((ipaddr))
logg("#TCP: Bound to address %s on port %u\n", ipaddr, (unsigned int) optget(opts, "TCPSocket")->numarg);
else
logg("#TCP: Bound to port %u\n", (unsigned int) optget(opts, "TCPSocket")->numarg);
}
backlog = optget(opts, "MaxConnectionQueueLength")->numarg;
logg("#TCP: Setting connection queue length to %d\n", backlog);
if(listen(sockfd, backlog) == -1) {
estr = strerror(errno);
logg("!TCP: listen() error: %s\n", estr);
closesocket(sockfd);
continue;
}
sockets[*nlsockets] = sockfd;
(*nlsockets)++;
2003-07-29 15:48:06 +00:00
}
2014-01-25 21:50:33 -05:00
freeaddrinfo(info);
*lsockets = sockets;
return 0;
2003-07-29 15:48:06 +00:00
}