aboutsummaryrefslogtreecommitdiffstats
path: root/glougloud/glougloud.c
blob: d5d3cadbff8830320b8b7a2689ca97d7898a48d0 (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
#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>

struct gg_server *ggserv_probes;
struct gg_server *ggserv_clients;
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)
{
	//log_info("got signal %d", 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;
	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 */
		}
	}

	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_clients = 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_clients, 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;
}