aboutsummaryrefslogtreecommitdiffstats
path: root/libglouglou/libggnet_traceroute.c
blob: f00fd72c3248124d0407a85173c1c10282f98405 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Traceroute using libevent, libdnet and libpcap
 *
 * 2012 Laurent Ghigonis <laurent@p1sec.com>
 *
 * Inspired from jtrace (http://monkey.org/~jose/software/jtrace/)
 *   Copyright (c) 2003-2004 Jose Nazario <jose@monkey.org>
 *   All rights reserved.
 */


#include <sys/param.h>
#include <sys/time.h>
#include <sys/types.h>

#include <netinet/in.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "libggnet_traceroute.h"

static void _req_free(struct ggnet_traceroute_req *);
static void _cb_recv(evutil_socket_t, short, void *);
static void _cb_send(evutil_socket_t, short, void *);
static int pcap_dloff(pcap_t *); // XXX move to libggnet_utils ?

struct ggnet_traceroute *
ggnet_traceroute_new(struct event_base *ev_base, char *iface)
{
  struct ggnet_traceroute *ggtr;
	char ebuff[PCAP_ERRBUF_SIZE];

  ggtr = calloc(1, sizeof(struct ggnet_traceroute));
  if (!ggtr) {
    printf("could not allocate ggnet_traceroute\n");
    exit(1);
  }
  ggtr->ev_base = ev_base;
  ggtr->pkt_ip = ip_open();
  if (!ggtr->pkt_ip) {
    printf("ip_open() failed\n");
    ggnet_traceroute_free(ggtr);
    return NULL;
  }
	ggtr->pkt_rand = rand_open();


  /* XXX get iface from route_open, route_get and pcap_lookupnet.
   * will need to do so in ggnet_traceroute_trace() so user privs, so will need
   * to prefetch the routes / interfaces, maybe */

	ggtr->pcap = pcap_open_live(iface, 1500, 1, 500, ebuff);
	if (ggtr->pcap == NULL)
		err(1, "pcap_open_live()");
	ggtr->pcap_dllen = pcap_dloff(ggtr->pcap);
	ggtr->pcap_fd = pcap_fileno(ggtr->pcap);

  return ggtr;
}

void
ggnet_traceroute_free(struct ggnet_traceroute *ggtr)
{
  struct ggnet_traceroute_req *req;

  if (ggtr->pcap)
    pcap_close(ggtr->pcap);
  if (ggtr->pkt_ip)
    ip_close(ggtr->pkt_ip);
  if (ggtr->pkt_rand)
    rand_close(ggtr->pkt_rand);
  LIST_FOREACH(req, &ggtr->req_list, entry)
    _req_free(req);
  free(ggtr);
}

struct ggnet_traceroute_req *
ggnet_traceroute_trace(struct ggnet_traceroute *ggtr,
                       struct in_addr *ip,
                       void (*cb_usr)(struct in_addr *, struct ggnet_traceroute_req *, void *),
                       void *data)
{
  struct ggnet_traceroute_req *req;

  req = calloc(1, sizeof(struct ggnet_traceroute_req));
  if (!req) {
    printf("could not allocate ggnet_traceroute_req\n");
    exit(1);
  }

  addr_aton("127.0.0.1", &req->srcip);
  addr_aton("127.0.0.1", &req->ip);

  req->ev_recv = event_new(ggtr->ev_base,
                           ggtr->pcap_fd, EV_READ, _cb_recv, req);
  event_add(req->ev_recv, NULL);
  req->ev_send = event_new(ggtr->ev_base,
                           ggtr->pcap_fd, EV_WRITE, _cb_send, req);
  event_add(req->ev_send, NULL);

  LIST_INSERT_HEAD(&ggtr->req_list, req, entry);
  return req;
}

void
ggnet_traceroute_cancel(struct ggnet_traceroute *ggtr,
                        struct ggnet_traceroute_req *req)
{
  LIST_REMOVE(req, entry);
  ggtr->req_pending--;
  _req_free(req);
}

static void
_req_free(struct ggnet_traceroute_req *req)
{
  if (req->ev_recv)
    event_free(req->ev_recv);
  if (req->ev_send)
    event_free(req->ev_send);
  free(req);
}

static void
_cb_recv(evutil_socket_t fd, short what, void *arg)
{
  struct ggnet_traceroute_req *req;

  req = arg;
  printf("cbggnet_traceroute cb_recv %s\n", addr_ntoa(&req->ip));
}

static void
_cb_send(evutil_socket_t fd, short what, void *arg)
{
  struct ggnet_traceroute_req *req;

  req = arg;
  printf("cbggnet_traceroute cb_send %s\n", addr_ntoa(&req->ip));
}

static int
pcap_dloff(pcap_t *pd)
{
	int i;

	i = pcap_datalink(pd);
	
	switch (i) {
	case DLT_EN10MB:
		i = 14;
		break;
	case DLT_IEEE802:
		i = 22;
		break;
	case DLT_FDDI:
		i = 21;
		break;
	case DLT_NULL:
		i = 4;
		break;
	default:
		i = -1;
		break;
	}
	return (i);
}