clamd/tcpserver.c
e3aaff8e
 /*
81837459
  *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
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
  */
 
67118e92
 #ifdef	_MSC_VER
 #include <winsock.h>
 #endif
 
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
67118e92
 #ifndef	C_WINDOWS
e3aaff8e
 #include <sys/socket.h>
 #include <netinet/in.h>
2d70a403
 #include <arpa/inet.h>
67118e92
 #endif
e3aaff8e
 #include <errno.h>
67118e92
 #ifndef	C_WINDOWS
f8f80da9
 #include <netdb.h>
67118e92
 #endif
e3aaff8e
 
bd8603aa
 #include "libclamav/clamav.h"
 
 #include "shared/options.h"
 #include "shared/cfgparser.h"
 #include "shared/output.h"
 #include "shared/network.h"
 
e3aaff8e
 #include "others.h"
 #include "server.h"
fc83da82
 #include "tcpserver.h"
e3aaff8e
 
bd8603aa
 int tcpserver(const struct cfgstruct *copt)
e3aaff8e
 {
 	struct sockaddr_in server;
 	int sockfd, backlog;
1095156a
 	const struct cfgstruct *taddr;
a7282c2f
 	struct hostent he;
 	char *estr, buf[1024];
0ee809e8
 	int true = 1;
e3aaff8e
 
     memset((char *) &server, 0, sizeof(server));
     server.sin_family = AF_INET;
     server.sin_port = htons(cfgopt(copt, "TCPSocket")->numarg);
d71dd823
 
81837459
     if((taddr = cfgopt(copt, "TCPAddr"))->enabled) {
a7282c2f
 	if(r_gethostbyname(taddr->strarg, &he, buf, sizeof(buf)) == -1) {
 	    logg("!r_gethostbyname(%s) error: %s\n", taddr->strarg, strerror(errno));
bd8603aa
 	    return -1;
f8f80da9
 	}
a7282c2f
 	server.sin_addr = *(struct in_addr *) he.h_addr_list[0];
f8f80da9
     } else
8139fd99
 	server.sin_addr.s_addr = INADDR_ANY;
e3aaff8e
 
 
     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
 	estr = strerror(errno);
 	logg("!socket() error: %s\n", estr);
bd8603aa
 	return -1;
e3aaff8e
     }
 
0ee809e8
     if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &true, sizeof(true)) == -1) {
 	logg("!setsocktopt(SO_REUSEADDR) error: %s\n", strerror(errno));
     }
 
e3aaff8e
     if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1) {
 	estr = strerror(errno);
 	logg("!bind() error: %s\n", estr);
bd8603aa
 	close(sockfd);
 	return -1;
8139fd99
     } else {
81837459
 	if(taddr->enabled)
57358cc8
 	    logg("Bound to address %s on tcp port %d\n", taddr->strarg, cfgopt(copt, "TCPSocket")->numarg);
8139fd99
 	else
57358cc8
 	    logg("Bound to tcp port %d\n", cfgopt(copt, "TCPSocket")->numarg);
8139fd99
     }
e3aaff8e
 
81837459
     backlog = cfgopt(copt, "MaxConnectionQueueLength")->numarg;
e3aaff8e
     logg("Setting connection queue length to %d\n", backlog);
 
     if(listen(sockfd, backlog) == -1) {
 	estr = strerror(errno);
 	logg("!listen() error: %s\n", estr);
bd8603aa
 	close(sockfd);
 	return -1;
e3aaff8e
     }
 
57358cc8
     return sockfd;
e3aaff8e
 }