FreeNOS
IPV4.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <FreeNOS/System.h>
19#include <String.h>
20#include <List.h>
21#include <ListIterator.h>
22#include <ByteOrder.h>
23#include "NetworkServer.h"
24#include "NetworkDevice.h"
25#include "IPV4.h"
26#include "IPV4Address.h"
27#include "UDP.h"
28#include "ICMP.h"
29
31 NetworkDevice &device,
32 NetworkProtocol &parent)
33 : NetworkProtocol(server, device, parent)
34{
35 m_address = 0;
36 m_icmp = 0;
37 m_udp = 0;
38 m_id = 1;
39}
40
42{
43}
44
46{
47 DEBUG("");
48
49 m_server.registerDirectory(this, "/ipv4");
50 m_server.registerFile(new IPV4Address(m_server.getNextInode(), this), "/ipv4/address");
51
53}
54
56{
57 m_icmp = icmp;
58}
59
61{
62 m_arp = arp;
63}
64
66{
67 m_udp = udp;
68}
69
75
77{
78 m_address = *address;
80}
81
82const String IPV4::toString(const Address address)
83{
84 String s;
85
86 s << ((address >> 24) & 0xff) << "."
87 << ((address >> 16) & 0xff) << "."
88 << ((address >> 8) & 0xff) << "."
89 << ((address & 0xff));
90
91 return s;
92}
93
94const IPV4::Address IPV4::toAddress(const char *address)
95{
96 const String input = address;
97 const List<String> lst = input.split('.');
98 Size shift = 24;
99 IPV4::Address addr = 0;
100
101 // The address must be 4 bytes
102 if (lst.count() != 4)
103 {
104 return ZERO;
105 }
106
107 // Extract bytes in dot format (xxx.xxx.xxx.xxx)
108 for (ListIterator<String> i(lst); i.hasCurrent(); i++)
109 {
110 String & s = i.current();
111 u8 byte = s.toLong();
112 addr |= (byte << shift);
113 shift -= 8;
114 }
115
116 // Done
117 return addr;
118}
119
121{
122 switch (id)
123 {
125 return IPV4::ICMP;
127 return IPV4::UDP;
128 default:
129 return IPV4::TCP;
130 }
131}
132
134 const void *address,
135 const Size addressSize,
136 const NetworkProtocol::Identifier protocol,
137 const Size payloadSize)
138{
139 Ethernet::Address ethAddr;
140
141 // Find the ethernet address using ARP first
142 FileSystem::Result result = m_arp->lookupAddress((const IPV4::Address *)address, &ethAddr);
143 if (result != FileSystem::Success)
144 {
145 if (result != FileSystem::RetryAgain)
146 {
147 ERROR("failed to perform ARP lookup: result = " << (int) result);
148 }
149 return result;
150 }
151
152 // Get a fresh ethernet packet
153 result = m_parent.getTransmitPacket(pkt, &ethAddr, sizeof(ethAddr),
154 NetworkProtocol::IPV4, payloadSize);
155 if (result != FileSystem::Success)
156 {
157 if (result != FileSystem::RetryAgain)
158 {
159 ERROR("failed to get transmit packet: result = " << (int) result);
160 }
161 return result;
162 }
163
164 // Fill IP header
165 Header *hdr = (Header *) ((*pkt)->data + (*pkt)->size);
166 hdr->versionIHL = (sizeof(Header) / sizeof(u32)) | (4 << 4);
167 hdr->typeOfService = 0;
168 hdr->timeToLive = 64;
169 hdr->protocol = getProtocolByIdentifier(protocol);
170 writeBe16(&hdr->length, payloadSize + sizeof(Header));
172 writeBe16(&hdr->fragmentOffset, 0x4000); // dont fragment flag
173 writeBe32(&hdr->source, m_address);
174 writeBe32(&hdr->destination, *(u32 *)address);
175 hdr->checksum = 0;
176 hdr->checksum = checksum(hdr, sizeof(Header));
177 (*pkt)->size += sizeof(Header);
178 m_id++;
179
180 // Success
181 return FileSystem::Success;
182}
183
184const u16 IPV4::checksum(const void *buffer, const Size length)
185{
186 const u16 *ptr = (const u16 *) buffer;
187 Size len = length;
188 uint sum = 0;
189
190 // Calculate sum of the buffer
191 while (len > 1)
192 {
193 sum += read16(ptr);
194 ptr++;
195 len -= 2;
196 }
197
198 // Add left-over byte, if any
199 if (len > 0)
200 sum += (readBe16(ptr) << 8);
201
202 // Enforce 16-bit checksum
203 while (sum >> 16)
204 sum = (sum >> 16) + (sum & 0xFFFF);
205
206 // Convert to one's complement
207 return (~sum);
208}
209
211 const Size offset)
212{
213 DEBUG("");
214
215 const Header *hdr = (const Header *) (pkt->data + offset);
216 const u32 destination = readBe32(&hdr->destination);
217
218 if (destination != m_address && destination != 0xffffffff && m_address != 0)
219 {
220 DEBUG("dropped packet for " << *IPV4::toString(destination));
222 }
223
224 switch (hdr->protocol)
225 {
226 case ICMP:
227 return m_icmp->process(pkt, offset + sizeof(Header));
228
229 case UDP:
230 return m_udp->process(pkt, offset + sizeof(Header));
231
232 default:
233 break;
234 }
235
237}
u32 length
Definition IntelACPI.h:1
u8 checksum
Definition IntelACPI.h:1
Address Resolution Protocol.
Definition ARP.h:43
FileSystem::Result lookupAddress(const IPV4::Address *ipAddr, Ethernet::Address *ethAddr)
Lookup Ethernet address for an IP.
Definition ARP.cpp:86
u32 getNextInode()
Get next unused inode.
FileSystem::Result registerDirectory(Directory *dir, const char *path)
Register a new Directory.
FileSystem::Result registerFile(File *file, const char *path)
Register a new File.
Internet Control Message Protocol (ICMP)
Definition ICMP.h:43
virtual FileSystem::Result process(const NetworkQueue::Packet *pkt, const Size offset)
Process incoming network packet.
Definition ICMP.cpp:48
IPV4 address file.
Definition IPV4Address.h:37
virtual FileSystem::Result initialize()
Perform initialization.
Definition IPV4.cpp:45
Protocol getProtocolByIdentifier(const NetworkProtocol::Identifier id) const
Convert protocol identifier.
Definition IPV4.cpp:120
static const String toString(const Address address)
Convert address to string.
Definition IPV4.cpp:82
u32 Address
IP-address.
Definition IPV4.h:47
virtual ~IPV4()
Destructor.
Definition IPV4.cpp:41
void setARP(::ARP *arp)
Set ARP instance.
Definition IPV4.cpp:60
void setUDP(::UDP *udp)
Set UDP instance.
Definition IPV4.cpp:65
virtual FileSystem::Result getTransmitPacket(NetworkQueue::Packet **pkt, const void *address, const Size addressSize, const Identifier protocol, const Size payloadSize)
Get a new packet for transmission.
Definition IPV4.cpp:133
::ICMP * m_icmp
ICMP instance.
Definition IPV4.h:234
Address m_address
Current IP address.
Definition IPV4.h:231
virtual FileSystem::Result process(const NetworkQueue::Packet *pkt, const Size offset)
Process incoming network packet.
Definition IPV4.cpp:210
u16 m_id
Packet ID for IPV4.
Definition IPV4.h:243
Protocol
Protocol types.
Definition IPV4.h:53
@ TCP
Definition IPV4.h:57
@ ICMP
Definition IPV4.h:54
@ UDP
Definition IPV4.h:56
virtual FileSystem::Result getAddress(Address *address)
Get current IP address.
Definition IPV4.cpp:70
static const u16 checksum(const void *buffer, const Size length)
Calculate IP checksum.
Definition IPV4.cpp:184
virtual FileSystem::Result setAddress(const Address *address)
Set current IP address.
Definition IPV4.cpp:76
::UDP * m_udp
UDP instance.
Definition IPV4.h:240
static const Address toAddress(const char *address)
Convert string to IPV4 address.
Definition IPV4.cpp:94
::ARP * m_arp
ARP instance.
Definition IPV4.h:237
void setICMP(::ICMP *icmp)
Set ICMP instance.
Definition IPV4.cpp:55
Iterate through a List.
virtual bool hasCurrent() const
Check if there is a current item on the List.
Simple linked list template class.
Definition List.h:37
Size count() const
Get the number of items on the list.
Definition List.h:402
Network Device abstract class.
Network protocol abstraction class.
Identifier
List of known network protocol identifiers.
NetworkProtocol & m_parent
Parent upper-layer protocol instance.
virtual FileSystem::Result getTransmitPacket(NetworkQueue::Packet **pkt, const void *address, const Size addressSize, const Identifier protocol, const Size payloadSize)
Get a new packet for transmission.
NetworkServer & m_server
Network server instance.
Networking server.
Abstraction of strings.
Definition String.h:42
long toLong(const Number::Base base=Number::Dec) const
Convert the String to a signed long integer.
Definition String.cpp:456
List< String > split(const char delimiter) const
Split the String into parts separated by a delimiter.
Definition String.cpp:408
User Datagram Protocol (UDP)
Definition UDP.h:42
virtual FileSystem::Result process(const NetworkQueue::Packet *pkt, const Size offset)
Process incoming network packet.
Definition UDP.cpp:117
void writeBe32(void *data, const u32 input)
Write 32-bit big endian integer.
Definition ByteOrder.h:459
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
#define ERROR(msg)
Output an error message.
Definition Log.h:61
const u16 readBe16(const void *data)
Read 16-bit big endian integer.
Definition ByteOrder.h:398
const u32 readBe32(const void *data)
Read 32-bit big endian integer.
Definition ByteOrder.h:384
unsigned int uint
Unsigned integer number.
Definition Types.h:44
const u16 read16(const void *data)
Read 16-bit integer (no conversion)
Definition ByteOrder.h:256
unsigned short u16
Unsigned 16-bit number.
Definition Types.h:56
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
void writeBe16(void *data, const u16 input)
Write 16-bit big endian integer.
Definition ByteOrder.h:471
#define DEBUG(msg)
Output a debug message to standard output.
Definition Log.h:89
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
Result
Result code for filesystem Actions.
Definition FileSystem.h:53
@ InvalidArgument
Definition FileSystem.h:55
Ethernet network address.
Definition Ethernet.h:53
IP network packet header.
Definition IPV4.h:67
u8 versionIHL
Version and header length (IHL)
Definition IPV4.h:68
u16 identification
Definition IPV4.h:71
u16 checksum
Definition IPV4.h:75
Address destination
Definition IPV4.h:77
u8 timeToLive
Definition IPV4.h:73
u8 typeOfService
Definition IPV4.h:69
u16 length
Definition IPV4.h:70
Address source
Definition IPV4.h:76
u8 protocol
Definition IPV4.h:74
u16 fragmentOffset
Definition IPV4.h:72
Represents a network packet.