2
* Copyright (C) 2007-2009 Sourcefire, Inc.
4
* Authors: Tomasz Kojm, Török Edvin
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License version 2 as
8
* published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
#include "clamav-config.h"
27
#include <sys/types.h>
32
#include <sys/socket.h>
33
#include <netinet/in.h>
34
#include <arpa/inet.h>
39
#include "libclamav/clamav.h"
41
#include "shared/optparser.h"
42
#include "shared/output.h"
43
#include "shared/misc.h"
47
#include "tcpserver.h"
49
int tcpserver(int **lsockets, unsigned int *nlsockets, char *ipaddr, const struct optstruct *opts)
51
struct addrinfo hints, *info, *p;
52
char host[NI_MAXHOST], serv[NI_MAXSERV];
63
snprintf(port, sizeof(port), "%lld", optget(opts, "TCPSocket")->numarg);
65
memset(&hints, 0x00, sizeof(struct addrinfo));
66
hints.ai_family = AF_UNSPEC;
67
hints.ai_socktype = SOCK_STREAM;
68
hints.ai_flags = AI_PASSIVE;
71
hints.ai_flags |= AI_ADDRCONFIG;
74
if ((res = getaddrinfo(ipaddr, port, &hints, &info))) {
75
logg("!TCP: getaddrinfo failed: %s\n", gai_strerror(res));
79
for (p = info; p != NULL; p = p->ai_next, i++) {
80
t = realloc(sockets, sizeof(int) * (*nlsockets + 1));
82
for (i=0; i < *nlsockets; i++)
89
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
90
estr = strerror(errno);
91
logg("!TCP: socket() error: %s\n", estr);
95
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &yes, sizeof(yes)) == -1) {
96
logg("!TCP: setsocktopt(SO_REUSEADDR) error: %s\n", strerror(errno));
100
if (p->ai_family == AF_INET6 &&
101
setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) == -1) {
102
estr = strerror(errno);
103
logg("!TCP: setsocktopt(IPV6_V6ONLY) error: %s\n", estr);
105
#endif /* IPV6_V6ONLY */
107
#ifdef HAVE_GETNAMEINFO
108
if ((res = getnameinfo(p->ai_addr, p->ai_addrlen, host, sizeof(host),
109
serv, sizeof(serv), NI_NUMERICHOST|NI_NUMERICSERV))) {
110
logg("!TCP: getnameinfo failed: %s\n", gai_strerror(res));
115
strncpy(host, ipaddr, sizeof(host));
116
host[sizeof(host)-1] = '\0';
117
snprintf(serv, sizeof(serv), "%u", (unsigned int)(optget(opts, "TCPSocket")->numarg));
119
if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
120
estr = strerror(errno);
121
logg("!TCP: Cannot bind to [%s]:%s: %s\n", host, serv, estr);
126
logg("#TCP: Bound to [%s]:%s\n", host, serv);
128
backlog = optget(opts, "MaxConnectionQueueLength")->numarg;
129
logg("#TCP: Setting connection queue length to %d\n", backlog);
131
if(listen(sockfd, backlog) == -1) {
132
estr = strerror(errno);
133
logg("!TCP: Cannot listen on [%s]:%s: %s\n", host, serv, estr);
139
sockets[*nlsockets] = sockfd;