clamd/tcpserver.c
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
 
064b4a0c
 int tcpserver(const struct optstruct *opts)
e3aaff8e
 {
7a997ac9
     struct sockaddr_in server;
     int sockfd, backlog;
a168a378
     char *estr;
7a997ac9
     int true = 1;
e3aaff8e
 
7a997ac9
     if (cfg_tcpsock(opts, &server, INADDR_ANY) == -1) {
 	return -1;
     }
e3aaff8e
 
     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
 	estr = strerror(errno);
e979398c
 	logg("!TCP: socket() error: %s\n", estr);
bd8603aa
 	return -1;
e3aaff8e
     }
 
0ee809e8
     if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &true, sizeof(true)) == -1) {
e979398c
 	logg("!TCP: setsocktopt(SO_REUSEADDR) error: %s\n", strerror(errno));
0ee809e8
     }
 
e3aaff8e
     if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1) {
 	estr = strerror(errno);
e979398c
 	logg("!TCP: bind() error: %s\n", estr);
a9d3aa14
 	closesocket(sockfd);
bd8603aa
 	return -1;
8139fd99
     } else {
bfd89d7c
 	const struct optstruct *taddr = optget(opts, "TCPAddr");
81837459
 	if(taddr->enabled)
a168a378
 	    logg("#TCP: Bound to address %s on port %u\n", taddr->strarg, (unsigned int) optget(opts, "TCPSocket")->numarg);
8139fd99
 	else
a168a378
 	    logg("#TCP: Bound to port %u\n", (unsigned int) optget(opts, "TCPSocket")->numarg);
8139fd99
     }
e3aaff8e
 
064b4a0c
     backlog = optget(opts, "MaxConnectionQueueLength")->numarg;
e979398c
     logg("#TCP: Setting connection queue length to %d\n", backlog);
e3aaff8e
 
     if(listen(sockfd, backlog) == -1) {
 	estr = strerror(errno);
e979398c
 	logg("!TCP: listen() error: %s\n", estr);
a9d3aa14
 	closesocket(sockfd);
bd8603aa
 	return -1;
e3aaff8e
     }
 
57358cc8
     return sockfd;
e3aaff8e
 }