FreeNOS
Ethernet.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 <ByteOrder.h>
19#include <MemoryBlock.h>
20#include "NetworkServer.h"
21#include "NetworkDevice.h"
22#include "Ethernet.h"
23#include "EthernetAddress.h"
24#include "ICMP.h"
25
27 NetworkDevice &device)
28 : NetworkProtocol(server, device, *this)
29{
30 m_arp = ZERO;
31 m_ipv4 = ZERO;
33}
34
38
48
54
56{
57 const FileSystem::Result result = m_device.setAddress(address);
58 if (result == FileSystem::Success)
59 {
61 }
62
63 return result;
64}
65
67{
68 m_arp = arp;
69}
70
72{
73 m_ipv4 = ip;
74}
75
77{
78 String s;
79
80 s << Number::Hex;
81 s << address.addr[0] << ":"
82 << address.addr[1] << ":"
83 << address.addr[2] << ":"
84 << address.addr[3] << ":"
85 << address.addr[4] << ":"
86 << address.addr[5];
87
88 return s;
89}
90
92 const void *address,
93 const Size addressSize,
94 const NetworkProtocol::Identifier protocol,
95 const Size payloadSize)
96{
97 *pkt = m_device.getTransmitQueue()->get();
98 if (!*pkt)
99 {
101 }
102
103 Ethernet::Header *ether = (Ethernet::Header *) ((*pkt)->data + (*pkt)->size);
104 MemoryBlock::copy(&ether->source, &m_address, sizeof(Address));
105 MemoryBlock::copy(&ether->destination, address, sizeof(Address));
106
107 switch (protocol)
108 {
110 writeBe16(&ether->type, IPV4);
111 break;
112
114 writeBe16(&ether->type, ARP);
115 break;
116
117 default:
118 ERROR("unsupported protocol: " << (int) protocol);
120 }
121
122 (*pkt)->size += sizeof(Ethernet::Header);
123 return FileSystem::Success;
124}
125
127 const Size offset)
128{
129 const Ethernet::Header *ether = (const Ethernet::Header *) (pkt->data + offset);
130 const u16 type = readBe16(&ether->type);
131
132 DEBUG("packet: size = " << pkt->size << " payload = " << *pkt);
133
134 switch (type)
135 {
136 case Ethernet::ARP:
137 if (m_arp)
138 return m_arp->process(pkt, offset + sizeof(Ethernet::Header));
139 break;
140
141 case Ethernet::IPV4:
142 if (m_ipv4)
143 return m_ipv4->process(pkt, offset + sizeof(Ethernet::Header));
144 break;
145
146 default:
147 DEBUG("dropped unknown ethernet type: " << (int) type);
148 break;
149 }
150
152}
153
155{
156 String str;
157
158 str << Number::Hex;
159
160 for (Size i = 0; i < sizeof(Ethernet::Address); i++)
161 str << addr.addr[i] << ":";
162
163 log.append(*str);
164
165 return log;
166}
u8 type
Definition IntelACPI.h:0
Address Resolution Protocol.
Definition ARP.h:43
virtual FileSystem::Result process(const NetworkQueue::Packet *pkt, const Size offset)
Process incoming network packet.
Definition ARP.cpp:246
Ethernet address file.
void setARP(::ARP *arp)
Set ARP instance.
Definition Ethernet.cpp:66
virtual FileSystem::Result setAddress(const Address *address)
Set Ethernet address.
Definition Ethernet.cpp:55
static const String toString(const Address address)
Convert address to string.
Definition Ethernet.cpp:76
::ARP * m_arp
ARP protocol.
Definition Ethernet.h:179
virtual FileSystem::Result getAddress(Address *address)
Retrieve Ethernet address.
Definition Ethernet.cpp:49
void setIP(::IPV4 *ip)
Set IPV4 instance.
Definition Ethernet.cpp:71
virtual FileSystem::Result initialize()
Perform initialization.
Definition Ethernet.cpp:39
virtual ~Ethernet()
Destructor.
Definition Ethernet.cpp:35
virtual FileSystem::Result process(const NetworkQueue::Packet *pkt, const Size offset)
Process incoming network packet.
Definition Ethernet.cpp:126
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 Ethernet.cpp:91
@ IPV4
Internet protocol v4.
Definition Ethernet.h:77
@ ARP
Address resolution protocol.
Definition Ethernet.h:79
::IPV4 * m_ipv4
IPV4 protocol.
Definition Ethernet.h:182
Address m_address
Current ethernet address.
Definition Ethernet.h:176
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 Protocol Version 4.
Definition IPV4.h:41
virtual FileSystem::Result process(const NetworkQueue::Packet *pkt, const Size offset)
Process incoming network packet.
Definition IPV4.cpp:210
Logging class.
Definition Log.h:97
void append(const char *str)
Append to buffered output.
Definition Log.cpp:53
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Network Device abstract class.
virtual FileSystem::Result setAddress(const Ethernet::Address *address)=0
Set ethernet address.
NetworkQueue * getTransmitQueue()
Get transmit queue.
virtual FileSystem::Result getAddress(Ethernet::Address *address)=0
Read ethernet address.
Network protocol abstraction class.
Identifier
List of known network protocol identifiers.
NetworkDevice & m_device
Network device instance.
NetworkServer & m_server
Network server instance.
Packet * get()
Get unused packet.
Networking server.
Abstraction of strings.
Definition String.h:42
Log & operator<<(Log &log, const Ethernet::Address &addr)
Definition Ethernet.cpp:154
#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
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
Result
Result code for filesystem Actions.
Definition FileSystem.h:53
@ InvalidArgument
Definition FileSystem.h:55
@ Hex
Decimal: 0-10.
Definition Types.h:171
Ethernet network address.
Definition Ethernet.h:53
Ethernet network packet header.
Definition Ethernet.h:65
u16 type
payload type
Definition Ethernet.h:68
Address destination
packet destination address
Definition Ethernet.h:66
Address source
packet source address
Definition Ethernet.h:67
Represents a network packet.