clamd/tcpserver.c
b151ef55
 /*
5c4d94a9
  *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
b151ef55
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  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
30738099
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
b151ef55
  */
 
b3483842
 #ifdef	_MSC_VER
 #include <winsock.h>
 #endif
 
24791abc
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
b151ef55
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
b3483842
 #ifndef	C_WINDOWS
b151ef55
 #include <sys/socket.h>
 #include <netinet/in.h>
e8217f5a
 #include <arpa/inet.h>
b3483842
 #endif
b151ef55
 #include <errno.h>
b3483842
 #ifndef	C_WINDOWS
d3472eb6
 #include <netdb.h>
b3483842
 #endif
b151ef55
 
e303cf29
 #include "libclamav/clamav.h"
 
 #include "shared/options.h"
 #include "shared/cfgparser.h"
 #include "shared/output.h"
 #include "shared/network.h"
 
b151ef55
 #include "others.h"
 #include "server.h"
 
e303cf29
 int tcpserver(const struct cfgstruct *copt)
b151ef55
 {
 	struct sockaddr_in server;
 	int sockfd, backlog;
13847a2d
 	struct cfgstruct *taddr;
b21c85ab
 	struct hostent he;
 	char *estr, buf[1024];
4be012e9
 	int true = 1;
b151ef55
 
     memset((char *) &server, 0, sizeof(server));
     server.sin_family = AF_INET;
     server.sin_port = htons(cfgopt(copt, "TCPSocket")->numarg);
13847a2d
 
5c4d94a9
     if((taddr = cfgopt(copt, "TCPAddr"))->enabled) {
b21c85ab
 	if(r_gethostbyname(taddr->strarg, &he, buf, sizeof(buf)) == -1) {
 	    logg("!r_gethostbyname(%s) error: %s\n", taddr->strarg, strerror(errno));
e303cf29
 	    return -1;
d3472eb6
 	}
b21c85ab
 	server.sin_addr = *(struct in_addr *) he.h_addr_list[0];
d3472eb6
     } else
4cd4319e
 	server.sin_addr.s_addr = INADDR_ANY;
b151ef55
 
 
     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
 	estr = strerror(errno);
 	logg("!socket() error: %s\n", estr);
e303cf29
 	return -1;
b151ef55
     }
 
4be012e9
     if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &true, sizeof(true)) == -1) {
 	logg("!setsocktopt(SO_REUSEADDR) error: %s\n", strerror(errno));
     }
 
b151ef55
     if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1) {
 	estr = strerror(errno);
 	logg("!bind() error: %s\n", estr);
e303cf29
 	close(sockfd);
 	return -1;
4cd4319e
     } else {
5c4d94a9
 	if(taddr->enabled)
596b1e64
 	    logg("Bound to address %s on tcp port %d\n", taddr->strarg, cfgopt(copt, "TCPSocket")->numarg);
4cd4319e
 	else
596b1e64
 	    logg("Bound to tcp port %d\n", cfgopt(copt, "TCPSocket")->numarg);
4cd4319e
     }
b151ef55
 
5c4d94a9
     backlog = cfgopt(copt, "MaxConnectionQueueLength")->numarg;
b151ef55
     logg("Setting connection queue length to %d\n", backlog);
 
     if(listen(sockfd, backlog) == -1) {
 	estr = strerror(errno);
 	logg("!listen() error: %s\n", estr);
e303cf29
 	close(sockfd);
 	return -1;
b151ef55
     }
 
596b1e64
     return sockfd;
b151ef55
 }