aboutsummaryrefslogblamecommitdiffstats
path: root/glougloud/glougloud.c
blob: 86d629043ed90863559d1f0a74c8e1b69de913f0 (plain) (tree)


















                        

                                   
                                
                                   

                           




                                                                                






                        
                                
 


                                                                                  




                                           
                                                  
                                            





                                                   
                                                                      



                                                         
                         

               
                                                               



                                                
                                 
                                                                              

                                      
                                                                                    

                                      
                                                           

                                      
                                                              
                                      








                                                


                                        
                                          
 
                                   
 

                                   









                                                                       
                                                                        
                                                                  
                                                                                 
                                                                  


                                     

                                         
 
                 
 



                                                           
                 




                                                           
                 




                                                                                    

                                                    




                                                                                    

                                                 

 
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/ioctl.h>

#include <net/if.h>
#include <netinet/in.h>

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>

#include <libglouglou.h>

#define GLOUGLOUD_USER "_glougloud"

struct gg_server *ggserv_probes;
struct gg_server *ggserv_analysers;
struct event_base *ev_base;

int prb_handle_conn(struct gg_server *, struct gg_user *);
int cli_handle_conn(struct gg_server *, struct gg_user *);
int prb_handle_packet(struct gg_server *, struct gg_user *, struct gg_packet *);
int cli_handle_packet(struct gg_server *, struct gg_user *, struct gg_packet *);

#if defined(__OPENBSD__)
void __dead
#else
void
#endif
usage(void)
{
	extern char *__progname;

	fprintf(stderr, "usage: %s [-hv] [-l probes_ip] [-L analysers_ip]\n"
			"\t\t[-p probes_port] [-P analysers_port]\n", __progname);
	exit(1);
}

static void
sig_handler(int sig, short why, void *data)
{
	printf("glougloud: got signal %d\n", sig);
	if (sig == SIGINT || sig == SIGTERM)
		event_base_loopexit(ev_base, NULL);
}

int
main(int argc, char **argv)
{
	struct event *ev_sigint, *ev_sigterm, *ev_sigchld, *ev_sighup;
	char probes_ip[30] = "127.0.0.1";
	char analysers_ip[30] = "127.0.0.1";
	int probes_port = GLOUGLOU_PROBE_DEFAULT_PORT;
	int analysers_port = GLOUGLOU_ANALY_DEFAULT_PORT;
	int loglevel = 0;
	int op;

	while ((op = getopt(argc, argv, "hl:L:p:P:v")) != -1) {
		switch (op) {
			case 'h':
				usage();
				/* NOTREACHED */
			case 'l':
				strncpy(probes_ip, optarg, sizeof(probes_ip));
				break;
			case 'L':
				strncpy(analysers_ip, optarg, sizeof(analysers_ip));
				break;
			case 'p':
				probes_port = atoi(optarg);
				break;
			case 'P':
				analysers_port = atoi(optarg);
				break;
			case 'v':
				loglevel++;
				break;
			default:
				usage();
				/* NOTREACHED */
		}
	}

	if (geteuid() != 0)
		errx(1, "must be root");

	droppriv(GLOUGLOUD_USER, 1, NULL);

	gg_verbosity_set(loglevel);

	ev_base = event_base_new();

	ev_sigint = evsignal_new(ev_base, SIGINT, sig_handler, NULL);
	ev_sigterm = evsignal_new(ev_base, SIGTERM, sig_handler, NULL);
	ev_sigchld = evsignal_new(ev_base, SIGCHLD, sig_handler, NULL);
	ev_sighup = evsignal_new(ev_base, SIGHUP, sig_handler, NULL);
	evsignal_add(ev_sigint, NULL);
	evsignal_add(ev_sigterm, NULL);
	evsignal_add(ev_sigchld, NULL);
	evsignal_add(ev_sighup, NULL);
	signal(SIGPIPE, SIG_IGN);

	ggserv_probes = gg_server_start(ev_base, probes_ip, probes_port,
			prb_handle_conn, prb_handle_packet, NULL);
	ggserv_analysers = gg_server_start(ev_base, analysers_ip, analysers_port,
			cli_handle_conn, cli_handle_packet, NULL);

	event_base_dispatch(ev_base);

	gg_server_stop(ggserv_probes);
	gg_server_stop(ggserv_analysers);

	return 0;
}

int
prb_handle_conn(struct gg_server *srv, struct gg_user *usr)
{
	return 0;
}

int
cli_handle_conn(struct gg_server *srv, struct gg_user *usr)
{
	return 0;
}

int
prb_handle_packet(struct gg_server *srv, struct gg_user *usr, struct gg_packet *pkt)
{
	gg_server_send(ggserv_analysers, pkt, NULL);
	return 0;
}

int
cli_handle_packet(struct gg_server *srv, struct gg_user *usr, struct gg_packet *pkt)
{
	gg_server_send(ggserv_probes, pkt, NULL);
	return 0;
}