Skip to content

Commit 0769915

Browse files
net: Extracted IP4 Header
1 parent 935a6d8 commit 0769915

3 files changed

Lines changed: 60 additions & 30 deletions

File tree

api/net/ip4/header.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
#pragma once
18+
19+
#ifndef NET_IP4_HEADER_HPP
20+
#define NET_IP4_HEADER_HPP
21+
22+
namespace net {
23+
namespace ip4 {
24+
25+
/** IP4 header representation */
26+
struct Header {
27+
uint8_t version_ihl;
28+
uint8_t tos;
29+
uint16_t tot_len;
30+
uint16_t id;
31+
uint16_t frag_off_flags;
32+
uint8_t ttl;
33+
uint8_t protocol;
34+
uint16_t check;
35+
Addr saddr;
36+
Addr daddr;
37+
};
38+
39+
}
40+
}
41+
42+
#endif
43+

api/net/ip4/ip4.hpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
#include <iostream>
2222

2323
#include "addr.hpp"
24+
#include "header.hpp"
2425
#include <common>
25-
#include <net/ethernet.hpp>
26+
#include <net/ethernet/ethernet.hpp>
2627
#include <net/inet.hpp>
2728

2829
namespace net {
@@ -34,48 +35,33 @@ namespace net {
3435
/** IP4 layer */
3536
class IP4 {
3637
public:
37-
using addr = ip4::Addr;
38+
using addr = ip4::Addr;
39+
using ip_header = ip4::Header;
3840

3941
/** Initialize. Sets a dummy linklayer out. */
4042
explicit IP4(Inet<LinkLayer, IP4>&) noexcept;
4143

4244
/** Known transport layer protocols. */
4345
enum proto { IP4_ICMP=1, IP4_UDP=17, IP4_TCP=6 };
4446

45-
46-
4747
static const addr INADDR_ANY;
4848
static const addr INADDR_BCAST;
4949

50-
/** IP4 header representation */
51-
struct ip_header {
52-
uint8_t version_ihl;
53-
uint8_t tos;
54-
uint16_t tot_len;
55-
uint16_t id;
56-
uint16_t frag_off_flags;
57-
uint8_t ttl;
58-
uint8_t protocol;
59-
uint16_t check;
60-
addr saddr;
61-
addr daddr;
62-
};
63-
6450
/**
6551
* The full header including IP
6652
*
6753
* @Note: This might be removed if we decide to isolate layers more
6854
*/
6955
struct full_header {
70-
uint8_t link_hdr[sizeof(typename LinkLayer::header)];
71-
ip_header ip_hdr;
56+
uint8_t link_hdr[sizeof(typename LinkLayer::header)];
57+
ip4::Header ip_hdr;
7258
};
7359

7460
/*
7561
Maximum Datagram Data Size
7662
*/
7763
inline constexpr uint16_t MDDS() const
78-
{ return stack_.MTU() - sizeof(ip_header); }
64+
{ return stack_.MTU() - sizeof(ip4::Header); }
7965

8066
/** Upstream: Input from link layer */
8167
void bottom(Packet_ptr);
@@ -107,7 +93,7 @@ namespace net {
10793
void transmit(Packet_ptr);
10894

10995
/** Compute the IP4 header checksum */
110-
uint16_t checksum(ip_header*);
96+
uint16_t checksum(ip4::Header*);
11197

11298
/**
11399
* \brief

src/net/ip4/ip4.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#define DEBUG // Allow debugging
1919
#define DEBUG2 // Allow debug lvl 2
20-
#include <os>
20+
2121
#include <net/ip4/ip4.hpp>
2222
#include <net/ip4/packet_ip4.hpp>
2323
#include <net/packet.hpp>
@@ -41,12 +41,13 @@ namespace net {
4141

4242
void IP4::bottom(Packet_ptr pckt) {
4343
debug2("<IP4 handler> got the data.\n");
44+
// Cast to IP4 Packet
45+
auto packet = std::static_pointer_cast<net::PacketIP4>(pckt);
4446

4547
// Stat increment packets received
4648
packets_rx_++;
4749

48-
auto data = pckt->buffer();
49-
ip_header* hdr = &reinterpret_cast<full_header*>(data)->ip_hdr;
50+
ip_header* hdr = &packet->ip_header();
5051

5152
// Drop if my ip address doesn't match destination ip address or broadcast
5253
if(UNLIKELY(hdr->daddr != local_ip() and
@@ -61,14 +62,14 @@ namespace net {
6162
switch(hdr->protocol){
6263
case IP4_ICMP:
6364
debug2("\t Type: ICMP\n");
64-
icmp_handler_(pckt);
65+
icmp_handler_(packet);
6566
break;
6667
case IP4_UDP:
6768
debug2("\t Type: UDP\n");
68-
udp_handler_(pckt);
69+
udp_handler_(packet);
6970
break;
7071
case IP4_TCP:
71-
tcp_handler_(pckt);
72+
tcp_handler_(packet);
7273
debug2("\t Type: TCP\n");
7374
break;
7475
default:
@@ -77,8 +78,8 @@ namespace net {
7778
}
7879
}
7980

80-
uint16_t IP4::checksum(ip_header* hdr) {
81-
return net::checksum(reinterpret_cast<uint16_t*>(hdr), sizeof(ip_header));
81+
uint16_t IP4::checksum(ip4::Header* hdr) {
82+
return net::checksum(reinterpret_cast<uint16_t*>(hdr), sizeof(ip4::Header));
8283
}
8384

8485
void IP4::transmit(Packet_ptr pckt) {

0 commit comments

Comments
 (0)