-
Notifications
You must be signed in to change notification settings - Fork 8
/
raw-receiver.c
106 lines (93 loc) · 2.51 KB
/
raw-receiver.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
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
// The packet length
#define PCKT_LEN 81920
// listen port from the command line arguments
int main(int argc, char *argv[])
{
int sd;
// No data/payload just datagram
char buffer[PCKT_LEN];
// Our own headers' structures
struct iphdr *ip = (struct iphdr *) buffer;
struct udphdr *udp = (struct udphdr *) (buffer + sizeof(struct iphdr));
// Source and destination addresses: IP and port
struct sockaddr_in lin;
int one = 1;
const int *val = &one;
memset(buffer, 0, PCKT_LEN);
if(argc != 2)
{
printf("- Invalid parameters!!!\n");
printf("- Usage %s <listen port>\n", argv[0]);
exit(-1);
}
// Create a raw socket with UDP protocol
sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if(sd < 0)
{
perror("socket() error");
// If something wrong just exit
exit(-1);
}
else
printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");
bzero((char *)&lin,sizeof(lin));
// The source is redundant, may be used later if needed
// The address family
lin.sin_family = AF_INET;
// Port numbers
lin.sin_port = htons(atoi(argv[1]));
if (bind(sd,(struct sockaddr *)&lin,sizeof(lin)) < 0)
{
fprintf(stderr, "ERROR binding socket.\n");
exit(1);
}
int payload_size = atoi(argv[5]);
int packet_len = sizeof(struct iphdr) + sizeof(struct udphdr) + payload_size;
// Inform the kernel do not fill up the packet structure. we will build our own...
if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
{
perror("setsockopt() error");
exit(-1);
}
else
printf("setsockopt() is OK.\n");
printf("Trying...\n");
printf("Using raw socket and UDP protocol\n");
printf("Using listen port: %u.\n", atoi(argv[1]));
ssize_t recv_len = 0;
int num_recv = 0;
struct timeval start, now;
gettimeofday(&start, NULL);
while(1)
{
ssize_t len = recv(sd, buffer, sizeof(buffer), 0);
if(len < 0)
{
perror("recv() error");
exit(-1);
}
recv_len += len;
num_recv++;
long *payload = (long *) (buffer + sizeof(struct iphdr) + sizeof(struct udphdr));
if (*payload == 9998)
break;
}
gettimeofday(&now, NULL);
double rate_now;
long duration_now;
duration_now = (now.tv_sec - start.tv_sec);
duration_now *= 1000000;
duration_now += now.tv_usec - start.tv_usec;
rate_now = recv_len * 8 * 1000000;
rate_now = rate_now/duration_now;
printf("receive %d packets, bytes: %ld, rate: %lf bps\n", num_recv, recv_len, rate_now);
close(sd);
return 0;
}