clamav/clamd/tcpserver.c

96 lines
2.6 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"
int tcpserver(const struct optstruct *opts)
2003-07-29 15:48:06 +00:00
{
struct sockaddr_in server;
int sockfd, backlog;
2009-05-05 14:14:40 +00:00
char *estr;
int true = 1;
2003-07-29 15:48:06 +00:00
if (cfg_tcpsock(opts, &server, INADDR_ANY) == -1) {
logg("!TCP: Couldn't configure socket, check your configuration\n");
return -1;
}
2003-07-29 15:48:06 +00:00
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
estr = strerror(errno);
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) {
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);
logg("!TCP: bind() error: %s\n", estr);
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");
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
backlog = optget(opts, "MaxConnectionQueueLength")->numarg;
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);
logg("!TCP: listen() error: %s\n", estr);
closesocket(sockfd);
2006-09-05 20:45:39 +00:00
return -1;
2003-07-29 15:48:06 +00:00
}
return sockfd;
2003-07-29 15:48:06 +00:00
}