2003-07-29 15:48:06 +00:00
|
|
|
/*
|
2009-02-13 10:55:45 +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
|
2007-03-31 20:31:04 +00:00
|
|
|
* 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
|
2006-04-09 19:59:28 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
2003-07-29 15:48:06 +00:00
|
|
|
*/
|
|
|
|
|
2006-09-13 22:06:13 +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>
|
2006-12-20 15:33:55 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2009-09-30 00:54:04 +02:00
|
|
|
#ifndef _WIN32
|
2003-07-29 15:48:06 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2003-12-02 22:48:56 +00:00
|
|
|
#include <arpa/inet.h>
|
2003-12-05 23:45:11 +00:00
|
|
|
#include <netdb.h>
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2009-09-30 00:54:04 +02:00
|
|
|
#include <errno.h>
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2006-09-05 20:45:39 +00:00
|
|
|
#include "libclamav/clamav.h"
|
|
|
|
|
2008-12-17 21:42:54 +00:00
|
|
|
#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"
|
2007-02-11 00:41:13 +00:00
|
|
|
#include "tcpserver.h"
|
2008-05-30 10:27:24 +00:00
|
|
|
|
2008-12-17 21:42:54 +00:00
|
|
|
int tcpserver(const struct optstruct *opts)
|
2003-07-29 15:48:06 +00:00
|
|
|
{
|
2009-02-12 21:14:51 +00:00
|
|
|
struct sockaddr_in server;
|
|
|
|
int sockfd, backlog;
|
2009-05-05 14:14:40 +00:00
|
|
|
char *estr;
|
2009-02-12 21:14:51 +00:00
|
|
|
int true = 1;
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2009-02-12 21:14:51 +00:00
|
|
|
if (cfg_tcpsock(opts, &server, INADDR_ANY) == -1) {
|
2010-10-18 20:02:53 +02:00
|
|
|
logg("!TCP: Couldn't configure socket, check your configuration\n");
|
2009-02-12 21:14:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
|
|
|
estr = strerror(errno);
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!TCP: socket() error: %s\n", estr);
|
2006-09-05 20:45:39 +00:00
|
|
|
return -1;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
2003-10-27 21:17:16 +00:00
|
|
|
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &true, sizeof(true)) == -1) {
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!TCP: setsocktopt(SO_REUSEADDR) error: %s\n", strerror(errno));
|
2003-10-27 21:17:16 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 14:39:19 -05:00
|
|
|
if(bind(sockfd, (struct sockaddr *) &server, (socklen_t)sizeof(struct sockaddr_in)) == -1) {
|
2003-07-29 15:48:06 +00:00
|
|
|
estr = strerror(errno);
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!TCP: bind() error: %s\n", estr);
|
2008-05-30 10:27:24 +00:00
|
|
|
closesocket(sockfd);
|
2006-09-05 20:45:39 +00:00
|
|
|
return -1;
|
2003-09-29 11:44:52 +00:00
|
|
|
} else {
|
2009-02-13 11:05:14 +00:00
|
|
|
const struct optstruct *taddr = optget(opts, "TCPAddr");
|
2005-06-01 00:42:33 +00:00
|
|
|
if(taddr->enabled)
|
2009-05-05 14:14:40 +00:00
|
|
|
logg("#TCP: Bound to address %s on port %u\n", taddr->strarg, (unsigned int) optget(opts, "TCPSocket")->numarg);
|
2003-09-29 11:44:52 +00:00
|
|
|
else
|
2009-05-05 14:14:40 +00:00
|
|
|
logg("#TCP: Bound to port %u\n", (unsigned int) optget(opts, "TCPSocket")->numarg);
|
2003-09-29 11:44:52 +00:00
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2008-12-17 21:42:54 +00:00
|
|
|
backlog = optget(opts, "MaxConnectionQueueLength")->numarg;
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("#TCP: Setting connection queue length to %d\n", backlog);
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
if(listen(sockfd, backlog) == -1) {
|
|
|
|
estr = strerror(errno);
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!TCP: listen() error: %s\n", estr);
|
2008-05-30 10:27:24 +00:00
|
|
|
closesocket(sockfd);
|
2006-09-05 20:45:39 +00:00
|
|
|
return -1;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
2005-06-22 15:54:28 +00:00
|
|
|
return sockfd;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|