e3aaff8e |
/* |
086eab5c |
* Copyright (C) 2007-2009 Sourcefire, Inc.
*
* Authors: Tomasz Kojm, Török Edvin |
e3aaff8e |
*
* This program is free software; you can redistribute it and/or modify |
bb34cb31 |
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. |
e3aaff8e |
*
* 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 |
48b7b4a7 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA. |
e3aaff8e |
*/
|
98ac8d19 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
e3aaff8e |
#include <stdio.h>
#include <string.h>
#include <sys/types.h> |
ad3c01bf |
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif |
e0bb54d7 |
#ifndef _WIN32 |
e3aaff8e |
#include <sys/socket.h>
#include <netinet/in.h> |
2d70a403 |
#include <arpa/inet.h> |
f8f80da9 |
#include <netdb.h> |
67118e92 |
#endif |
e0bb54d7 |
#include <errno.h> |
e3aaff8e |
|
bd8603aa |
#include "libclamav/clamav.h"
|
064b4a0c |
#include "shared/optparser.h" |
bd8603aa |
#include "shared/output.h" |
3b074c78 |
#include "shared/misc.h" |
bd8603aa |
|
e3aaff8e |
#include "others.h"
#include "server.h" |
fc83da82 |
#include "tcpserver.h" |
a9d3aa14 |
|
c9d06cf3 |
int tcpserver(int **lsockets, unsigned int *nlsockets, char *ipaddr, const struct optstruct *opts) |
e3aaff8e |
{ |
c9d06cf3 |
struct addrinfo hints, *info, *p;
int *sockets; |
7a997ac9 |
int sockfd, backlog; |
c9d06cf3 |
int *t;
char *estr, port[10];
int yes = 1;
int res; |
575dc809 |
unsigned int i=0; |
e3aaff8e |
|
c9d06cf3 |
sockets = *lsockets; |
e3aaff8e |
|
c9d06cf3 |
snprintf(port, sizeof(port), "%lld", optget(opts, "TCPSocket")->numarg); |
e3aaff8e |
|
c9d06cf3 |
memset(&hints, 0x00, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; |
0ee809e8 |
|
575dc809 |
#if C_LINUX
if (!(ipaddr)) {
/*
* By default, getaddrinfo() will return 0.0.0.0 if NULL is passed in as the first parameter.
* Binding to 0.0.0.0 will prevent us from also binding IPv6 ::0 (errno = EADDRINUSE). However,
* if we bind to ::0 (or shorthand, ::), then Linux will bind to both IPv4 and IPv6.
*/
ipaddr = "::";
}
#endif
|
c9d06cf3 |
if ((res = getaddrinfo(ipaddr, port, &hints, &info))) {
logg("!TCP: getaddrinfo: %s\n", gai_strerror(res));
return -1; |
8139fd99 |
} |
e3aaff8e |
|
575dc809 |
for (p = info; p != NULL; p = p->ai_next, i++) { |
c9d06cf3 |
t = realloc(sockets, sizeof(int) * (*nlsockets + 1));
if (!(t)) { |
2af457a0 |
for (i=0; i < *nlsockets; i++)
close(sockets[i]);
|
c9d06cf3 |
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); |
575dc809 |
if (ipaddr || i == 0)
logg("!TCP: bind() error when trying to listen on [%s]:%s: %s\n", ipaddr, port, estr); |
c9d06cf3 |
closesocket(sockfd); |
e3aaff8e |
|
c9d06cf3 |
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)++; |
e3aaff8e |
}
|
c9d06cf3 |
freeaddrinfo(info);
*lsockets = sockets;
return 0; |
e3aaff8e |
} |