-
Notifications
You must be signed in to change notification settings - Fork 1
/
udpCallback.c
316 lines (275 loc) · 7 KB
/
udpCallback.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* udpReplay
* Copyright (c) 2016 Tupone Alfredo
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the license found in the file
* named COPYING that should have accompanied this file.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "udpCallback.h"
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <netinet/ip.h>
#include <netinet/udp.h>
#ifdef HAVE_NET_ETHERNET_H
#include <net/ethernet.h>
#endif
#ifdef HAVE_SYS_ETHERNET_H
#include <sys/ethernet.h>
#endif
#include <pcap/vlan.h>
#include "asterix.h"
static int udpSocket;
struct sockaddr_in sockaddr;
static int start_loop = 1;
char *dvalue = NULL;
char *pvalue = NULL;
int flood = 0;
useconds_t floodTime = 1000;
int loop = 0;
useconds_t loopTime = 1000;
int oneByOne = 0;
long int countToFlood = 0;
int asterixTime = 0;
int setMulticastTTL = 0;
long multicastTTLValue = 0;
int setBroadcast = 0;
int datalink;
void waitBeforeSending(struct timeval actual_delta)
{
static struct timeval start_delta;
struct timeval delta_time;
useconds_t useconds;
if (start_loop) {
start_loop = 0;
memcpy(&start_delta, &actual_delta, sizeof(struct timeval));
return;
}
delta_time.tv_sec = actual_delta.tv_sec - start_delta.tv_sec;
delta_time.tv_usec = actual_delta.tv_usec - start_delta.tv_usec;
/* Normalize usec */
if (delta_time.tv_usec < 0)
do {
delta_time.tv_usec += 1000000;
--delta_time.tv_sec;
} while (delta_time.tv_usec < 0);
else
while (delta_time.tv_usec >= 1000000) {
delta_time.tv_usec -= 1000000;
++delta_time.tv_sec;
}
if (delta_time.tv_sec >= 0) {
useconds = delta_time.tv_usec + 1000000 * delta_time.tv_sec;
if (useconds > 0) {
int result = usleep(useconds);
if (result != 0) {
perror("usleep Failed");
return;
}
}
}
}
void waitALittle()
{
int result = usleep(floodTime);
if (result != 0) {
perror("usleep Failed");
return;
}
}
int waitToLoop()
{
int result = usleep(loopTime);
if (result != 0) {
perror("usleep Failed");
}
return result;
}
static void callback_handler(u_char *user __attribute__((unused)),
const struct pcap_pkthdr *h,
const u_char *bytes)
{
unsigned int caplen = h->caplen;
unsigned int len = h->len;
struct ether_header eth_hdr;
struct ip ip_hdr;
struct udphdr udp_hdr;
struct vlan_tag vlan_hdr;
uint16_t protocol;
unsigned int ipLen;
unsigned int dataLen;
int result;
ssize_t byteCount;
struct timeval now_tod;
struct timeval now_pcap;
struct timeval actual_delta;
char s[12];
char *sResult;
char *endPtr;
/*
* No way to do something with truncated packet
*/
if (caplen != len)
return;
if (datalink == DLT_EN10MB) {
/* Copy the ethernet header */
if (len < sizeof(eth_hdr))
return;
memcpy(ð_hdr, bytes, sizeof(eth_hdr));
bytes += sizeof(eth_hdr);
len -= sizeof(eth_hdr);
protocol = ntohs(eth_hdr.ether_type);
/* Check for VLAN data */
if (protocol == ETHERTYPE_VLAN)
{
/* Copy the vlan header */
if (len < sizeof(vlan_hdr))
return;
memcpy(&vlan_hdr, bytes, sizeof(vlan_hdr));
bytes += sizeof(vlan_hdr);
len -= sizeof(vlan_hdr);
protocol = ntohs(vlan_hdr.vlan_tci);
}
/* Discard non IP datagram */
if (protocol != ETHERTYPE_IP)
return;
}
/* Copy the IPv4 header */
if (len < sizeof(ip_hdr))
return;
memcpy(&ip_hdr, bytes, sizeof(ip_hdr));
ipLen = ip_hdr.ip_hl * 4;
if (len < ipLen)
return;
bytes += ipLen;
len -= ipLen;
/* Discard non UDP datagram */
if (ip_hdr.ip_p != IPPROTO_UDP)
return;
/* Reject broadcast datagram */
if (ip_hdr.ip_dst.s_addr == INADDR_BROADCAST)
return;
/* Copy the UDP header */
if (len < sizeof(udp_hdr))
return;
memcpy(&udp_hdr, bytes, sizeof(udp_hdr));
bytes += sizeof(udp_hdr);
len -= sizeof(udp_hdr);
/* Discard uncomplete UDP datagram */
#ifdef HAVE_STRUCT_UDPHDR_UH_ULEN
dataLen = ntohs(udp_hdr.uh_ulen) - sizeof(udp_hdr);
#else
dataLen = ntohs(udp_hdr.len) - sizeof(udp_hdr);
#endif
if (len < dataLen)
return;
if (flood) {
waitALittle();
} else if (oneByOne) {
if (countToFlood > 0) {
countToFlood--;
waitALittle();
} else {
printf("Press <Enter> to send next datagram ->");
sResult = fgets(s, sizeof(s), stdin);
if (sResult == NULL)
return;
countToFlood = strtol(s, &endPtr, 0);
if (endPtr == s)
countToFlood = 0;
if (countToFlood > 0)
countToFlood--;
else
countToFlood = 0;
}
} else {
/* Getting actual time and pcap time */
result = gettimeofday(&now_tod, NULL);
if (result != 0) {
perror("Get Time of Day Failed");
return;
}
now_pcap = h->ts;
actual_delta.tv_sec = now_pcap.tv_sec - now_tod.tv_sec;
actual_delta.tv_usec = now_pcap.tv_usec - now_tod.tv_usec;
/* Wait, if necessary, following the interdatagram delay */
waitBeforeSending(actual_delta);
}
/* Set destination */
if (!dvalue)
sockaddr.sin_addr.s_addr = ip_hdr.ip_dst.s_addr;
if (!pvalue) {
#ifdef HAVE_STRUCT_UDPHDR_UH_DPORT
sockaddr.sin_port = udp_hdr.uh_dport;
#else
sockaddr.sin_port = udp_hdr.dest;
#endif
}
if (asterixTime)
{
unsigned int tod = now_tod.tv_sec;
tod %= 86400;
tod *= 128;
tod += now_tod.tv_usec * 128 / 1000000;
bytes = fixAsterixTOD(bytes, dataLen, tod);
}
/* Send UDP data */
byteCount = sendto(udpSocket, bytes, dataLen, 0,
(struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (byteCount < 0) {
perror("UDP sendto failed");
}
}
void replayAll(pcap_t *pcap) {
int result;
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (udpSocket == -1) {
perror("UDP Socket failed");
return;
}
if (setMulticastTTL) {
u_char ttl = multicastTTLValue;
setsockopt(udpSocket, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
}
if (setBroadcast) {
int brd = 1;
setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, &brd, sizeof(brd));
}
result = pcap_loop(pcap, -1, callback_handler, NULL);
if (result == -1) {
pcap_perror(pcap, "Error during pcap_loop\n");
} else if (result == -2) {
printf("pcap_breakloop() called before any packets were processed.\n");
} else if (result != 0) {
printf("Result (%d) from pcap_loop() not foreseen\n", result);
}
}
// Local Variables: ***
// mode: C ***
// tab-width: 2 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: nil ***
// End: ***
// ex: shiftwidth=2 tabstop=2