Skip to content

Commit 9e721c6

Browse files
committed
codelab: fix forward and swap
1 parent cd84c62 commit 9e721c6

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

codelab/forward.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#include <net/netmap.h>
1616
#define NETMAP_WITH_LIBS
1717
#include <net/netmap_user.h>
18-
#include <netinet/ether.h>
18+
#include <sys/socket.h>
19+
#include <netinet/in.h>
20+
#include <netinet/if_ether.h>
1921
#include <netinet/ip.h>
2022
#include <netinet/udp.h>
2123
#include <netinet/tcp.h>
@@ -51,7 +53,7 @@ static inline int
5153
pkt_select(const char *buf, int udp_port)
5254
{
5355
struct ether_header *ethh;
54-
struct iphdr *iph;
56+
struct ip *iph;
5557
struct udphdr *udph;
5658

5759
if (udp_port == 0) {
@@ -63,15 +65,15 @@ pkt_select(const char *buf, int udp_port)
6365
/* Filter out non-IP traffic. */
6466
return 0;
6567
}
66-
iph = (struct iphdr *)(ethh + 1);
67-
if (iph->protocol != IPPROTO_UDP) {
68+
iph = (struct ip *)(ethh + 1);
69+
if (iph->ip_p != IPPROTO_UDP) {
6870
/* Filter out non-UDP traffic. */
6971
return 0;
7072
}
7173
udph = (struct udphdr *)(iph + 1);
7274

7375
/* Match the destination port. */
74-
if (udph->dest != htons(udp_port)) {
76+
if (udph->uh_dport != htons(udp_port)) {
7577
return 0;
7678
}
7779

codelab/swap.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#include <net/netmap.h>
1616
#define NETMAP_WITH_LIBS
1717
#include <net/netmap_user.h>
18-
#include <netinet/ether.h>
18+
#include <sys/socket.h>
19+
#include <netinet/in.h>
20+
#include <netinet/if_ether.h>
1921
#include <netinet/ip.h>
2022
#include <netinet/udp.h>
2123
#include <netinet/tcp.h>
@@ -53,7 +55,7 @@ static inline int
5355
pkt_udp_port_swap(char *buf)
5456
{
5557
struct ether_header *ethh;
56-
struct iphdr *iph;
58+
struct ip *iph;
5759
struct udphdr *udph;
5860
uint16_t tmp;
5961

@@ -62,15 +64,15 @@ pkt_udp_port_swap(char *buf)
6264
/* Filter out non-IP traffic. */
6365
return 0;
6466
}
65-
iph = (struct iphdr *)(ethh + 1);
66-
if (iph->protocol != IPPROTO_UDP) {
67+
iph = (struct ip *)(ethh + 1);
68+
if (iph->ip_p != IPPROTO_UDP) {
6769
/* Filter out non-UDP traffic. */
6870
return 0;
6971
}
7072
udph = (struct udphdr *)(iph + 1);
71-
tmp = udph->source;
72-
udph->source = udph->dest;
73-
udph->dest = tmp;
73+
tmp = udph->uh_sport;
74+
udph->uh_sport = udph->uh_dport;
75+
udph->uh_dport = tmp;
7476

7577
return 1;
7678
}

0 commit comments

Comments
 (0)