-
Notifications
You must be signed in to change notification settings - Fork 1
/
routines.c
143 lines (120 loc) · 3.21 KB
/
routines.c
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
/*
* routines.c
*
* Created on: 2009-11-16
* Author: proton
*/
#include <sys/time.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h> /* The L2 protocols */
#include <libnet.h>
int
my_libnet_open_link(libnet_t *l)
{
struct ifreq ifr;
int n = 1;
if (l == NULL)
{
return (-1);
}
l->fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (l->fd == -1)
{
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"socket: %s", strerror(errno));
goto bad;
}
memset(&ifr, 0, sizeof (ifr));
strncpy(ifr.ifr_name, l->device, sizeof (ifr.ifr_name) -1);
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
if (ioctl(l->fd, SIOCGIFHWADDR, &ifr) < 0 )
{
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"SIOCGIFHWADDR: %s", strerror(errno));
goto bad;
}
switch (ifr.ifr_hwaddr.sa_family)
{
//{{BEGIN_PROTON_ADDED_CODE}}
case ARPHRD_IEEE80211_RADIOTAP:
l->link_type = 0x123; //What ever
l->link_offset = 0x20; //Don't know what is it, but we don't use libnet_build* funcs.
break;
//{{END_PROTON_ADDED_CODE}}
default:
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"unknown physical layer type 0x%x\n",
ifr.ifr_hwaddr.sa_family);
goto bad;
}
if (setsockopt(l->fd, SOL_SOCKET, SO_BROADCAST, &n, sizeof(n)) == -1)
{
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"%s: set SO_BROADCAST failed: %s\n",
__func__, strerror(errno));
goto bad;
}
return (1);
bad:
if (l->fd >= 0)
{
close(l->fd);
}
return (-1);
}
libnet_t *
my_libnet_init(int injection_type, char *device, char *err_buf)
{
libnet_t *l = NULL;
if (getuid() && geteuid())
{
snprintf(err_buf, LIBNET_ERRBUF_SIZE,
"%s(): UID or EUID of 0 required\n", __func__);
goto bad;
}
l = (libnet_t *)malloc(sizeof (libnet_t));
if (l == NULL)
{
snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s(): malloc(): %s\n", __func__,
strerror(errno));
goto bad;
}
memset(l, 0, sizeof (*l));
l->injection_type = injection_type;
l->ptag_state = LIBNET_PTAG_INITIALIZER;
l->device = (device ? strdup(device) : NULL);
strncpy(l->label, LIBNET_LABEL_DEFAULT, LIBNET_LABEL_SIZE);
l->label[sizeof(l->label)] = '\0';
switch (l->injection_type)
{
case LIBNET_LINK:
case LIBNET_LINK_ADV:
if (libnet_select_device(l) == -1)
{
snprintf(err_buf, LIBNET_ERRBUF_SIZE, l->err_buf);
goto bad;
}
if (my_libnet_open_link(l) == -1)
{
snprintf(err_buf, LIBNET_ERRBUF_SIZE, l->err_buf);
goto bad;
}
break;
default:
snprintf(err_buf, LIBNET_ERRBUF_SIZE,
"%s(): unsupported injection type\n", __func__);
goto bad;
break;
}
return (l);
bad:
if (l)
{
libnet_destroy(l);
}
return (NULL);
}