Skip to content

Commit 5323a6d

Browse files
sink
0 parents  commit 5323a6d

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CC=gcc
2+
CFLAGS=-Wall -g -Werror -DSOLUTION
3+
PROGS=sink
4+
5+
all: $(PROGS)
6+
7+
sink: sink.o
8+
forward: forward.o
9+
swap: swap.o
10+
fe: fe.o
11+
12+
clean:
13+
-rm *.o $(PROGS)

sink.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* This program opens a netmap port and starts receiving packets,
3+
* counting all the UDP packets with a destination port specified
4+
* by command-line option.
5+
*/
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <unistd.h>
9+
#include <signal.h>
10+
#include <errno.h>
11+
#include <poll.h>
12+
#include <net/if.h>
13+
#include <stdint.h>
14+
#include <net/netmap.h>
15+
#define NETMAP_WITH_LIBS
16+
#include <net/netmap_user.h>
17+
#include <netinet/ether.h>
18+
#include <netinet/ip.h>
19+
#include <netinet/udp.h>
20+
#include <netinet/tcp.h>
21+
22+
static int stop = 0;
23+
24+
static void
25+
sigint_handler(int signum)
26+
{
27+
stop = 1;
28+
}
29+
30+
static inline int
31+
udp_port_match(const char *buf, unsigned len, int udp_port)
32+
{
33+
struct ether_header *ethh;
34+
struct iphdr *iph;
35+
struct udphdr *udph;
36+
37+
ethh = (struct ether_header *)buf;
38+
if (ethh->ether_type != htons(ETHERTYPE_IP)) {
39+
/* Filter out non-IP traffic. */
40+
return 0;
41+
}
42+
iph = (struct iphdr *)(ethh + 1);
43+
if (iph->protocol != IPPROTO_UDP) {
44+
/* Filter out non-UDP traffic. */
45+
return 0;
46+
}
47+
udph = (struct udphdr *)(iph + 1);
48+
49+
/* Match the destination port. */
50+
if (udph->dest == htons(udp_port)) {
51+
return 1;
52+
}
53+
54+
return 0;
55+
}
56+
57+
static int
58+
main_loop(const char *netmap_port, int udp_port)
59+
{
60+
61+
while (!stop) {
62+
}
63+
64+
65+
return 0;
66+
}
67+
68+
static void
69+
usage(char **argv)
70+
{
71+
printf("usage: %s [-h] [-p UDP_PORT] [-i NETMAP_PORT]\n", argv[0]);
72+
exit(EXIT_SUCCESS);
73+
}
74+
75+
int
76+
main(int argc, char **argv)
77+
{
78+
const char *netmap_port = NULL;
79+
int udp_port = 8000;
80+
struct sigaction sa;
81+
int opt;
82+
int ret;
83+
84+
while ((opt = getopt(argc, argv, "hi:p:")) != -1) {
85+
switch (opt) {
86+
case 'h':
87+
usage(argv);
88+
return 0;
89+
90+
case 'i':
91+
netmap_port = optarg;
92+
break;
93+
94+
case 'p':
95+
udp_port = atoi(optarg);
96+
if (udp_port <= 0 || udp_port >= 65535) {
97+
printf(" invalid UDP port %s\n", optarg);
98+
usage(argv);
99+
}
100+
break;
101+
102+
default:
103+
printf(" unrecognized option '-%c'\n", opt);
104+
usage(argv);
105+
return -1;
106+
}
107+
}
108+
109+
if (netmap_port == NULL) {
110+
printf(" missing netmap port\n");
111+
usage(argv);
112+
}
113+
114+
/* Register Ctrl-C handler. */
115+
sa.sa_handler = sigint_handler;
116+
sigemptyset(&sa.sa_mask);
117+
sa.sa_flags = SA_RESTART;
118+
ret = sigaction(SIGINT, &sa, NULL);
119+
if (ret) {
120+
perror("sigaction(SIGINT)");
121+
exit(EXIT_FAILURE);
122+
}
123+
124+
printf("Port : %s\n", netmap_port);
125+
printf("UDP port: %d\n", udp_port);
126+
127+
main_loop(netmap_port, udp_port);
128+
129+
return 0;
130+
}

0 commit comments

Comments
 (0)