aboutsummaryrefslogtreecommitdiffstats
path: root/gg_sniff/gg_sniff.c
blob: 8ee6de1c9c2f784bd863d3f1fca92a84ebf86dd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#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 <netdb.h>
#include <pcap.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>
#include <libggnet.h>
#include "gg_sniff.h"

#if defined(__OpenBSD__)
#include "pcap-int.h"
#endif

#define GG_SNIFF_USER "_gg_sniff"
#define GG_SNIFF_LOGFILE "/var/log/ggsniff.log"

struct event_base *_ev_base = NULL;

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

  fprintf(stderr, "usage: %s [-hv] [-f filter] [-i interface] [ip [port]]\n", __progname);
  exit(1);
}

static void
sig_handler(int sig, short why, void *data)
{
	gg_log_info("got signal %d", sig);
	if (sig == SIGINT || sig == SIGTERM)
	  if (_ev_base)
      event_base_loopexit(_ev_base, NULL);
}

int
main(int argc, char **argv)
{
  struct gg_client *ggcli = NULL;
  struct ggnet *net = NULL;
	struct event *ev_sigint, *ev_sigterm, *ev_sigchld, *ev_sighup;
	char ggserv_ip[30] = "127.0.0.1";
	char *iface = NULL;
	char *filter = NULL;
	int ggserv_port = GLOUGLOU_PROBE_DEFAULT_PORT;
	int pcap_init = 0;
	int loglevel = 0;
	int active = 0;
	int retval = -1;
	int op;

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

	while ((op = getopt(argc, argv, "af:hi:v")) != -1) {
		switch (op) {
      case 'a':
        active = 1;
        break;
			case 'f':
                filter = strndup(optarg, 256);
                break;
			case 'h':
				usage();
				/* NOTREACHED */
			case 'i':
        iface = strndup(optarg, 30);
        break;
			case 'v':
				loglevel++;
				break;
			default:
				usage();
				/* NOTREACHED */
		}
	}
	switch (argc - optind) {
  case 2: ggserv_port = atoi(argv[optind+1]);
  case 1: strncpy(ggserv_ip, argv[optind], sizeof(ggserv_ip));
  case 0:
    break;
  default:
    usage();
    /* NOTREACHED */
  }

	gg_log_init(GG_SNIFF_LOGFILE, loglevel);
  gg_log_warn("Starting gg_sniff");

	_ev_base = event_base_new();

  net = ggnet_new(GGNET_MANAGE_CONNID_TRUE);
  if (!net)
    goto quit;
	ggcli = gg_client_connect(_ev_base, ggserv_ip, ggserv_port, NULL, NULL, NULL);
	if (!ggcli)
	  goto quit;
  pcap_init = ggsniff_pcap_init(_ev_base, ggcli, net, iface, active, filter);
	if (!pcap_init)
	  goto quit;

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

	droppriv(GG_SNIFF_USER, 1, NULL);

	gg_log_info("entering event loop");
	event_base_dispatch(_ev_base);
	retval = 0;

quit:
  if (pcap_init)
    ggsniff_pcap_shutdown();
  if (ggcli)
    gg_client_disconnect(ggcli);
  if (net)
    ggnet_free(net);
  
	gg_log_warn("exiting");

	gg_log_shutdown();

	exit(retval);
}