aboutsummaryrefslogblamecommitdiffstats
path: root/glougloud/glougloud.c
blob: eab250cece67f0cad005aa42ecfa470c0b1e304f (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 [-vi]", __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);
		gg_server_stop(ggserv_probes);
		gg_server_stop(ggserv_analysers);
  }
}

int
main(int argc, char **argv)
{
	struct event *ev_sigint, *ev_sigterm, *ev_sigchld, *ev_sighup;
	int loglevel = 0;
	int op;

	while ((op = getopt(argc, argv, "hv")) != -1) {
		switch (op) {
			case 'h':
				usage();
				/* NOTREACHED */
			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, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT,
	                                prb_handle_conn, prb_handle_packet, NULL);
	ggserv_analysers = gg_server_start(ev_base, "127.0.0.1", GLOUGLOU_ANALY_DEFAULT_PORT,
	                                cli_handle_conn, cli_handle_packet, NULL);

	event_base_dispatch(ev_base);

  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;
}