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;
32  MemoryBlock::set(&m_address, 0, sizeof(m_address));
33 }
34 
36 {
37 }
38 
40 {
42 
43  m_server.registerDirectory(this, "/ethernet");
44  m_server.registerFile(new EthernetAddress(m_server.getNextInode(), this), "/ethernet/address");
45 
46  return FileSystem::Success;
47 }
48 
50 {
51  MemoryBlock::copy(address, &m_address, sizeof(Ethernet::Address));
52  return FileSystem::Success;
53 }
54 
56 {
57  const FileSystem::Result result = m_device.setAddress(address);
58  if (result == FileSystem::Success)
59  {
60  MemoryBlock::copy(&m_address, address, sizeof(Ethernet::Address));
61  }
62 
63  return result;
64 }
65 
66 void Ethernet::setARP(::ARP *arp)
67 {
68  m_arp = arp;
69 }
70 
71 void Ethernet::setIP(::IPV4 *ip)
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  {
100  return FileSystem::RetryAgain;
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 
154 Log & operator << (Log &log, const Ethernet::Address & addr)
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 }
IPV4::process
virtual FileSystem::Result process(const NetworkQueue::Packet *pkt, const Size offset)
Process incoming network packet.
Definition: IPV4.cpp:210
NetworkProtocol
Network protocol abstraction class.
Definition: NetworkProtocol.h:39
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
Ethernet::process
virtual FileSystem::Result process(const NetworkQueue::Packet *pkt, const Size offset)
Process incoming network packet.
Definition: Ethernet.cpp:126
Ethernet::getAddress
virtual FileSystem::Result getAddress(Address *address)
Retrieve Ethernet address.
Definition: Ethernet.cpp:49
Ethernet::m_arp
::ARP * m_arp
ARP protocol.
Definition: Ethernet.h:179
MemoryBlock::set
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Definition: MemoryBlock.cpp:25
readBe16
const u16 readBe16(const void *data)
Read 16-bit big endian integer.
Definition: ByteOrder.h:398
operator<<
Log & operator<<(Log &log, const Ethernet::Address &addr)
Definition: Ethernet.cpp:154
FileSystemServer::getNextInode
u32 getNextInode()
Get next unused inode.
Definition: FileSystemServer.cpp:66
writeBe16
void writeBe16(void *data, const u16 input)
Write 16-bit big endian integer.
Definition: ByteOrder.h:471
ARP::process
virtual FileSystem::Result process(const NetworkQueue::Packet *pkt, const Size offset)
Process incoming network packet.
Definition: ARP.cpp:246
String
Abstraction of strings.
Definition: String.h:41
Ethernet.h
NetworkDevice
Network Device abstract class.
Definition: NetworkDevice.h:41
Ethernet::setARP
void setARP(::ARP *arp)
Set ARP instance.
Definition: Ethernet.cpp:66
Ethernet::initialize
virtual FileSystem::Result initialize()
Perform initialization.
Definition: Ethernet.cpp:39
Ethernet::ARP
@ ARP
Address resolution protocol.
Definition: Ethernet.h:79
FileSystem::InvalidArgument
@ InvalidArgument
Definition: FileSystem.h:55
Ethernet::setAddress
virtual FileSystem::Result setAddress(const Address *address)
Set Ethernet address.
Definition: Ethernet.cpp:55
NetworkQueue::Packet::data
u8 * data
Definition: NetworkQueue.h:53
MemoryBlock.h
NetworkQueue::Packet::size
Size size
Definition: NetworkQueue.h:52
NetworkProtocol::IPV4
@ IPV4
Definition: NetworkProtocol.h:49
FileSystem::Success
@ Success
Definition: FileSystem.h:54
EthernetAddress
Ethernet address file.
Definition: EthernetAddress.h:36
NetworkQueue::get
Packet * get()
Get unused packet.
Definition: NetworkQueue.cpp:72
Ethernet::m_address
Address m_address
Current ethernet address.
Definition: Ethernet.h:176
Log
Logging class.
Definition: Log.h:96
NetworkProtocol::m_device
NetworkDevice & m_device
Network device instance.
Definition: NetworkProtocol.h:123
Log::append
void append(const char *str)
Append to buffered output.
Definition: Log.cpp:53
FileSystem::NotSupported
@ NotSupported
Definition: FileSystem.h:61
EthernetAddress.h
DEBUG
#define DEBUG(msg)
Output a debug message to standard output.
Definition: Log.h:89
Ethernet::m_ipv4
::IPV4 * m_ipv4
IPV4 protocol.
Definition: Ethernet.h:182
Ethernet::Address
Ethernet network address.
Definition: Ethernet.h:52
NetworkDevice.h
NetworkServer
Networking server.
Definition: NetworkServer.h:40
u16
unsigned short u16
Unsigned 16-bit number.
Definition: Types.h:56
NetworkDevice::getTransmitQueue
NetworkQueue * getTransmitQueue()
Get transmit queue.
Definition: NetworkDevice.cpp:79
ByteOrder.h
Ethernet::Header::type
u16 type
payload type
Definition: Ethernet.h:68
Ethernet::Address::addr
u8 addr[6]
Definition: Ethernet.h:54
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
ICMP.h
Ethernet::Header
Ethernet network packet header.
Definition: Ethernet.h:64
NetworkDevice::getAddress
virtual FileSystem::Result getAddress(Ethernet::Address *address)=0
Read ethernet address.
IPV4
Internet Protocol Version 4.
Definition: IPV4.h:40
FileSystemServer::registerDirectory
FileSystem::Result registerDirectory(Directory *dir, const char *path)
Register a new Directory.
Definition: FileSystemServer.cpp:138
Ethernet::Header::source
Address source
packet source address
Definition: Ethernet.h:67
Ethernet::toString
static const String toString(const Address address)
Convert address to string.
Definition: Ethernet.cpp:76
NetworkProtocol::m_server
NetworkServer & m_server
Network server instance.
Definition: NetworkProtocol.h:120
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
FileSystem::RetryAgain
@ RetryAgain
Definition: FileSystem.h:57
NetworkServer.h
NetworkProtocol::ARP
@ ARP
Definition: NetworkProtocol.h:50
NetworkDevice::setAddress
virtual FileSystem::Result setAddress(const Ethernet::Address *address)=0
Set ethernet address.
Ethernet::Header
struct Ethernet::Header Header
Ethernet network packet header.
Ethernet::~Ethernet
virtual ~Ethernet()
Destructor.
Definition: Ethernet.cpp:35
Ethernet::setIP
void setIP(::IPV4 *ip)
Set IPV4 instance.
Definition: Ethernet.cpp:71
Ethernet::Address
struct Ethernet::Address Address
Ethernet network address.
NetworkQueue::Packet
Represents a network packet.
Definition: NetworkQueue.h:50
ARP
Address Resolution Protocol.
Definition: ARP.h:42
type
u8 type
Definition: IntelACPI.h:63
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Ethernet::IPV4
@ IPV4
Internet protocol v4.
Definition: Ethernet.h:77
Number::Hex
@ Hex
Decimal: 0-10.
Definition: Types.h:171
NetworkProtocol::Ethernet
@ Ethernet
Definition: NetworkProtocol.h:48
NetworkProtocol::Identifier
Identifier
List of known network protocol identifiers.
Definition: NetworkProtocol.h:46
Ethernet::Header::destination
Address destination
packet destination address
Definition: Ethernet.h:66
Ethernet::getTransmitPacket
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
FileSystemServer::registerFile
FileSystem::Result registerFile(File *file, const char *path)
Register a new File.
Definition: FileSystemServer.cpp:115