clamav-milter/connpool.c
4c237bcf
 /*
56163e7d
  *  Copyright (C) 2015 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
4c237bcf
  *  Copyright (C)2008 Sourcefire, Inc.
  *
  *  Author: aCaB <acab@clamav.net>
  *
  *  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.
  *
  *  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.
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
 #include <arpa/inet.h>
 #include <sys/types.h>
 #include <sys/socket.h>
1e5deac0
 #include <sys/time.h>
4c237bcf
 #include <sys/un.h>
 #include <netdb.h>
 #include <unistd.h>
 #include <time.h>
d5a65a34
 #include <netdb.h>
4c237bcf
 
278dc6b3
 #include "shared/optparser.h"
4c237bcf
 #include "shared/output.h"
11195c0b
 #include "shared/misc.h"
4c237bcf
 
 #include "connpool.h"
 #include "netcode.h"
 
 #define SETGAI(k, v) {(k)->gai = (void *)(v);} while(0)
 #define FREESRV(k) { if((k).gai) freeaddrinfo((k).gai); else if((k).server) free((k).server); } while(0)
 
1e5deac0
 #if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
 #define _UNUSED_ __attribute__ ((__unused__))
 #else
 #define _UNUSED_
 #endif
4c237bcf
 
1e5deac0
 struct CPOOL *cp = NULL;
 static pthread_cond_t mon_cond = PTHREAD_COND_INITIALIZER;
 static int quitting = 1;
 static pthread_t probe_th;
d5a65a34
 
4c237bcf
 static int cpool_addunix(char *path) {
     struct sockaddr_un *srv;
     struct CP_ENTRY *cpe = &cp->pool[cp->entries-1];
 
58481352
     if(!cli_is_abspath(path)) {
4c237bcf
 	logg("!Unix clamd socket must be an absolute path\n");
 	return 1;
     }
     if(!(srv = (struct sockaddr_un *)malloc(sizeof(*srv)))) {
 	logg("!Out of memory allocating unix socket space\n");
 	return 1;
     }
 
     srv->sun_family = AF_UNIX;
     strncpy(srv->sun_path, path, sizeof(srv->sun_path));
     srv->sun_path[sizeof(srv->sun_path)-1]='\0';
     cpe->type = 0;
     cpe->dead = 1;
     cpe->local = 1;
     cpe->last_poll = 0;
     cpe->server = (struct sockaddr *)srv;
     cpe->socklen = sizeof(*srv);
     SETGAI(cpe, NULL);
     if(!cp->local_cpe) cp->local_cpe = cpe;
     logg("*Local socket unix:%s added to the pool (slot %d)\n", srv->sun_path, cp->entries);
     return 0;
 }
 
 
 static int islocal(struct sockaddr *sa, socklen_t addrlen) {
     int s = socket(sa->sa_family, SOCK_STREAM, 0);
     int ret;
739aa0a9
     if (s < 0) return 0;
4c237bcf
     ret = (bind(s, sa, addrlen) == 0);
     close(s);
     return ret;
 }
 
 
 static int cpool_addtcp(char *addr, char *port) {
4432aeb7
     struct addrinfo hints, *res, *res2;
4c237bcf
     struct CP_ENTRY *cpe = (struct CP_ENTRY *)&cp->pool[cp->entries-1];
 
     memset(&hints, 0, sizeof(hints));
     hints.ai_family = AF_UNSPEC;
     hints.ai_socktype = SOCK_STREAM;
 
     if(getaddrinfo(addr, port ? port : "3310", &hints, &res)) {
 	logg("^Can't resolve hostname %s\n", addr ? addr : "");
 	return 1;
     }
     cpe->type = 1;
     cpe->dead = 1;
 
     memset(&hints, 0, sizeof(hints));
     hints.ai_flags = AI_PASSIVE;
     hints.ai_socktype = SOCK_STREAM;
     hints.ai_family = AF_UNSPEC;
     if(!getaddrinfo(addr, NULL, &hints, &res2)) {
 	cpe->local = islocal(res2->ai_addr, res2->ai_addrlen);
 	freeaddrinfo(res2);
     } else cpe->local = 0;
     cpe->last_poll = 0;
     cpe->server = res->ai_addr;
     cpe->socklen = res->ai_addrlen;
     SETGAI(cpe, res);
     logg("*%s socket tcp:%s:%s added to the pool (slot %d)\n", cpe->local ? "Local" : "Remote", addr ? addr : "localhost", port ? port : "3310", cp->entries);
     return 0;
 }
 
 
278dc6b3
 static int addslot(void) {
4c237bcf
     struct CP_ENTRY *cpe;
 
     if(!(cpe = realloc(cp->pool, (cp->entries + 1) * sizeof(struct CP_ENTRY)))) {
 	logg("!Out of memory while initializing the connection pool\n");
 	cpool_free();
 	return 1;
     }
     if(cp->local_cpe)
 	cp->local_cpe = (struct CP_ENTRY *)((char *)cp->local_cpe + ((char *)cpe - (char *)cp->pool));
     memset(&cpe[cp->entries], 0, sizeof(*cpe));
     cp->pool = cpe;
     cp->entries++;
     return 0;
 }
1e5deac0
 
 
 /* Probe strategy:
 - wake up every minute
 - probe alive if last check > 15 min
 - probe dead if (last check > 2 min || no clamd available)
 */
4c237bcf
 
278dc6b3
 static void cpool_probe(void) {
4c237bcf
     unsigned int i, dead=0;
     struct CP_ENTRY *cpe = cp->pool;
1e5deac0
     time_t now = time(NULL);
4c237bcf
 
     for(i=1; i<=cp->entries; i++) {
1e5deac0
 	if((cpe->dead && (cpe->last_poll < now - 120 || !cp->alive)) || cpe->last_poll < now - 15*60*60) {
 	    cpe->last_poll = time(NULL);
4c237bcf
 	    nc_ping_entry(cpe);
 	    logg("*Probe for slot %u returned: %s\n", i, cpe->dead ? "failed" : "success");
 	}
 	dead += cpe->dead;
 	cpe++;
     }
     cp->alive = cp->entries - dead;
1e5deac0
 
4c237bcf
     if(!cp->alive)
1e5deac0
 	logg("^No clamd server appears to be available\n");
 }
 
 
278dc6b3
 static void *cpool_mon(_UNUSED_ void *v) {
1e5deac0
     pthread_mutex_t conv;
 
     pthread_mutex_init(&conv, NULL);
     pthread_mutex_lock(&conv);
 
     while(!quitting) {
 	struct timespec t;
 
 	cpool_probe();
 	t.tv_sec = time(NULL) + 60;
 	t.tv_nsec = 0;
 	pthread_cond_timedwait(&mon_cond, &conv, &t);
     }
     pthread_mutex_unlock(&conv);
     pthread_mutex_destroy(&conv);
     return NULL;
4c237bcf
 }
 
 
278dc6b3
 void cpool_init(struct optstruct *opts) {
     const struct optstruct *opt;
4c237bcf
     int failed = 0;
 
     if(!(cp=calloc(sizeof(*cp), 1))) {
 	logg("!Out of memory while initializing the connection pool");
 	return;
     }
 
     cp->local_cpe = NULL;
 
278dc6b3
     if((opt = optget(opts, "ClamdSocket"))->enabled) {
 	while(opt) {
 	    char *socktype = opt->strarg;
4c237bcf
 
 	    if(addslot()) return;
 	    if(!strncasecmp(socktype, "unix:", 5)) {
 		failed = cpool_addunix(socktype+5);
 	    } else if(!strncasecmp(socktype, "tcp:", 4)) {
 		char *port = strrchr(socktype+4, ':');
 		if(port) {
 		    *port='\0';
 		    port++;
 		}
 		failed = cpool_addtcp(socktype+4, port);
 	    } else {
 		logg("!Failed to parse ClamdSocket directive '%s'\n", socktype);
 		failed = 1;
 	    }
 	    if(failed) break;
278dc6b3
 	    opt = opt->nextarg;
4c237bcf
 	}
 	if(failed) {
 	    cpool_free();
 	    return;
 	}
     }
 
     if(!cp->entries) {
 	logg("!No ClamdSocket specified\n");
 	cpool_free();
 	return;
     }
1e5deac0
     quitting = 0;
     pthread_create(&probe_th, NULL, cpool_mon, NULL);
     srand(time(NULL));
4c237bcf
 }
 
 
 void cpool_free(void) {
     unsigned int i;
1e5deac0
 
     if(!quitting) {
 	logg("*Killing the monitor and stopping\n");
2a760698
 	quitting = 1;
1e5deac0
 	pthread_cond_signal(&mon_cond);
 	pthread_join(probe_th, NULL);
     }
 
f7203529
     if(cp) {
 	if(cp->pool) {
 	    for(i=0; i<cp->entries; i++)
 		FREESRV(cp->pool[i]);
 	    free(cp->pool);
 	}
 	free(cp);
 	cp = NULL;
     }
4c237bcf
 }
 
 
d5a65a34
 struct CP_ENTRY *cpool_get_rand(int *s) {
4c237bcf
     unsigned int start, i;
     struct CP_ENTRY *cpe;
 
d5a65a34
     if(cp->alive) {
 	start = rand() % cp->entries;
 	for(i=0; i<cp->entries; i++) {
 	    cpe = &cp->pool[(i+start) % cp->entries];
 	    if(cpe->dead) continue;
 	    if(cpe->local && cp->local_cpe && !cp->local_cpe->dead)
 		cpe = cp->local_cpe;
 	    if((*s = nc_connect_entry(cpe)) == -1) {
 		cpe->dead = 1;
 		continue;
 	    }
 	    return cpe;
 	}
4c237bcf
     }
1e5deac0
     pthread_cond_signal(&mon_cond);
4c237bcf
     return NULL;
 }
 
 
 /*
  * Local Variables:
  * mode: c
  * c-basic-offset: 4
  * tab-width: 8
  * End: 
  * vim: set cindent smartindent autoindent softtabstop=4 shiftwidth=4 tabstop=8: 
  */