clamav/clamd/tcpserver.c

148 lines
4.2 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;
char host[NI_MAXHOST], serv[NI_MAXSERV];
2014-01-25 21:50:33 -05:00
int *sockets;
int sockfd, backlog;
2014-01-25 21:50:33 -05:00
int *t;
char *estr, port[10];
int yes = 1;
int res;
unsigned int i=0;
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
#ifdef AI_ADDRCONFIG
hints.ai_flags |= AI_ADDRCONFIG;
#endif
2014-01-25 21:50:33 -05:00
if ((res = getaddrinfo(ipaddr, port, &hints, &info))) {
logg("!TCP: getaddrinfo failed: %s\n", gai_strerror(res));
2014-01-25 21:50:33 -05:00
return -1;
2003-09-29 11:44:52 +00:00
}
2003-07-29 15:48:06 +00:00
for (p = info; p != NULL; p = p->ai_next, i++) {
2014-01-25 21:50:33 -05:00
t = realloc(sockets, sizeof(int) * (*nlsockets + 1));
if (!(t)) {
for (i=0; i < *nlsockets; i++)
close(sockets[i]);
2014-01-25 21:50:33 -05:00
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));
}
#ifdef IPV6_V6ONLY
if (p->ai_family == AF_INET6 &&
setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) == -1) {
estr = strerror(errno);
logg("!TCP: setsocktopt(IPV6_V6ONLY) error: %s\n", estr);
}
#endif /* IPV6_V6ONLY */
2014-11-06 11:51:21 -05:00
#ifdef HAVE_GETNAMEINFO
if ((res = getnameinfo(p->ai_addr, p->ai_addrlen, host, sizeof(host),
serv, sizeof(serv), NI_NUMERICHOST|NI_NUMERICSERV))) {
logg("!TCP: getnameinfo failed: %s\n", gai_strerror(res));
host[0] = '\0';
serv[0] = '\0';
}
2014-11-06 11:51:21 -05:00
#else
strncpy(host, ipaddr, sizeof(host));
host[sizeof(host)-1] = '\0';
snprintf(serv, sizeof(serv), "%u", (unsigned int)(optget(opts, "TCPSocket")->numarg));
#endif
2014-01-25 21:50:33 -05:00
if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
estr = strerror(errno);
logg("!TCP: Cannot bind to [%s]:%s: %s\n", host, serv, estr);
2014-01-25 21:50:33 -05:00
closesocket(sockfd);
2003-07-29 15:48:06 +00:00
2014-01-25 21:50:33 -05:00
continue;
}
logg("#TCP: Bound to [%s]:%s\n", host, serv);
2014-01-25 21:50:33 -05:00
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: Cannot listen on [%s]:%s: %s\n", host, serv, estr);
2014-01-25 21:50:33 -05:00
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
}