-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathudp_padding_server.c
More file actions
79 lines (65 loc) · 1.84 KB
/
Copy pathudp_padding_server.c
File metadata and controls
79 lines (65 loc) · 1.84 KB
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
/*
* Issue #481 UDP test: Detect Ethernet padding bytes in received UDP data.
* Receives UDP packets and prints byte count and hex dump.
* If padding is not stripped, extra bytes will appear in recv output.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "ff_api.h"
#include "ff_config.h"
#include "ff_log.h"
#define RECV_BUF 2048
#define TEST_PORT 15321
static int sockfd = -1;
static int recv_count = 0;
static int
loop(void *arg)
{
char buf[RECV_BUF];
struct sockaddr_in from;
socklen_t fromlen = sizeof(from);
ssize_t n = ff_recvfrom(sockfd, buf, RECV_BUF, 0,
(struct linux_sockaddr *)&from, &fromlen);
if (n > 0) {
recv_count++;
int i;
printf("[RECV] pkt #%d: %zd bytes hex=", recv_count, n);
for (i = 0; i < n && i < 64; i++)
printf("%02x", (unsigned char)buf[i]);
if (n > 64)
printf("...(truncated)");
printf("\n");
ff_sendto(sockfd, buf, n, 0,
(struct linux_sockaddr *)&from, fromlen);
}
return 0;
}
int
main(int argc, char *argv[])
{
ff_init(argc, argv);
sockfd = ff_socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
printf("[MAIN] ff_socket failed: %s\n", strerror(errno));
exit(1);
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(TEST_PORT);
addr.sin_addr.s_addr = INADDR_ANY;
if (ff_bind(sockfd, (struct linux_sockaddr *)&addr, sizeof(addr)) < 0) {
printf("[MAIN] ff_bind failed: %s\n", strerror(errno));
exit(1);
}
printf("[MAIN] UDP server listening on port %d fd=%d\n", TEST_PORT, sockfd);
ff_run(loop, NULL);
return 0;
}