xref: /haiku/headers/private/kernel/boot/net/NetDefs.h (revision 30743318f1e634793b92f2f8202c16ea36b9a796)
1 /*
2  * Copyright 2005, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3  * Copyright 2010, Andreas Färber <andreas.faerber@web.de>
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 #ifndef _BOOT_NET_DEFS_H
7 #define _BOOT_NET_DEFS_H
8 
9 
10 #include <string.h>
11 
12 #include <ByteOrder.h>
13 #include <SupportDefs.h>
14 
15 #include <util/kernel_cpp.h>
16 
17 
18 // Ethernet
19 
20 #define ETH_ALEN			6
21 #define ETHERTYPE_IP		0x0800	// IP
22 #define ETHERTYPE_ARP		0x0806	// Address resolution
23 
24 #define ETHER_MIN_TRANSFER_UNIT	46
25 #define ETHER_MAX_TRANSFER_UNIT	1500
26 
27 struct mac_addr_t {
28 	mac_addr_t() {}
29 
30 	mac_addr_t(uint8 *address)
31 	{
32 		memcpy(this->address, address, ETH_ALEN);
33 	}
34 
35 	mac_addr_t(const mac_addr_t& other)
36 	{
37 		memcpy(address, other.address, sizeof(address));
38 	}
39 
40 	uint64 ToUInt64() const
41 	{
42 		return ((uint64)address[0] << 40)
43 			| ((uint64)address[1] << 32)
44 			| ((uint64)address[2] << 24)
45 			| ((uint64)address[3] << 16)
46 			| ((uint64)address[4] << 8)
47 			| (uint64)address[5];
48 	}
49 
50 	uint8 operator[](int index)
51 	{
52 		return address[index];
53 	}
54 
55 	mac_addr_t& operator=(const mac_addr_t& other)
56 	{
57 		memcpy(address, other.address, sizeof(address));
58 		return *this;
59 	}
60 
61 	bool operator==(const mac_addr_t& other) const
62 	{
63 		return memcmp(address, other.address, sizeof(address)) == 0;
64 	}
65 
66 	bool operator!=(const mac_addr_t& other) const
67 	{
68 		return !(*this == other);
69 	}
70 
71 	uint8 address[ETH_ALEN];
72 } __attribute__ ((__packed__));
73 
74 extern const mac_addr_t kBroadcastMACAddress;
75 extern const mac_addr_t kNoMACAddress;
76 
77 // 10/100 Mb/s ethernet header
78 struct ether_header {
79 	mac_addr_t	destination;	/* destination eth addr */
80 	mac_addr_t	source;			/* source ether addr    */
81 	uint16		type;			/* packet type ID field */
82 } __attribute__ ((__packed__));
83 
84 
85 // #pragma mark -
86 
87 // Address Resolution Protocol (ARP)
88 
89 typedef uint32 ip_addr_t;
90 
91 // ARP protocol opcodes
92 #define ARPOP_REQUEST   1               /* ARP request.  */
93 #define ARPOP_REPLY     2               /* ARP reply.  */
94 #define ARPOP_RREQUEST  3               /* RARP request.  */
95 #define ARPOP_RREPLY    4               /* RARP reply.  */
96 #define ARPOP_InREQUEST 8               /* InARP request.  */
97 #define ARPOP_InREPLY   9               /* InARP reply.  */
98 #define ARPOP_NAK       10              /* (ATM)ARP NAK.  */
99 
100 // ARP header for IP over ethernet (RFC 826)
101 struct arp_header {
102 	uint16	hardware_format;	/* Format of hardware address.  */
103 	uint16	protocol_format;	/* Format of protocol address.  */
104 	uint8	hardware_length;	/* Length of hardware address.  */
105 	uint8	protocol_length;	/* Length of protocol address.  */
106 	uint16	opcode;				/* ARP opcode (command).  */
107 
108 	// IP over ethernet
109 	mac_addr_t	sender_mac;		/* Sender hardware address.  */
110     ip_addr_t	sender_ip;		/* Sender IP address.  */
111 	mac_addr_t	target_mac;		/* Target hardware address.  */
112     ip_addr_t	target_ip;		/* Target IP address.  */
113 } __attribute__ ((__packed__));
114 
115 // ARP protocol HARDWARE identifiers.
116 #define ARPHRD_ETHER    1               /* Ethernet 10/100Mbps.  */
117 
118 
119 // #pragma mark -
120 
121 // Internet Protocol (IP)
122 
123 #define INADDR_ANY              ((ip_addr_t) 0x00000000)
124 	/* Address to send to all hosts.  */
125 #define INADDR_BROADCAST        ((ip_addr_t) 0xffffffff)
126 	/* Address indicating an error return.  */
127 #define INADDR_NONE             ((ip_addr_t) 0xffffffff)
128 
129 // IP packet header (no options
130 struct ip_header {
131 #if __BYTE_ORDER == __LITTLE_ENDIAN
132 	uint8 		header_length:4;	// header length
133 	uint8		version:4;			// IP protocol version
134 #endif
135 #if __BYTE_ORDER == __BIG_ENDIAN
136 	uint8		version:4;			// IP protocol version
137 	uint8 		header_length:4;	// header length
138 #endif
139 	uint8		type_of_service;	// type of service
140 	uint16		total_length;		// total IP packet length
141 	uint16		identifier;			// fragment identification
142 	uint16		fragment_offset;	// fragment offset and flags (0xe000)
143 	uint8		time_to_live;		// time to live
144 	uint8		protocol;			// protocol
145 	uint16		checksum;			// checksum (header)
146 	ip_addr_t	source;				// source IP address
147 	ip_addr_t	destination;		// destination IP address
148 } __attribute__ ((__packed__));
149 
150 // IP protocol version 4
151 #define	IP_PROTOCOL_VERSION_4		4
152 
153 // fragment flags/offset mask
154 #define IP_DONT_FRAGMENT			0x4000	/* dont fragment flag */
155 #define IP_FRAGMENT_OFFSET_MASK		0x1fff	/* mask for fragment offset */
156 
157 // Internet implementation parameters.
158 #define IP_MAX_TIME_TO_LIVE			255		/* maximum time to live */
159 #define IP_DEFAULT_TIME_TO_LIVE		64		/* default ttl, from RFC 1340 */
160 
161 // IP protocols
162 #define IPPROTO_TCP					6
163 #define IPPROTO_UDP					17
164 
165 
166 // #pragma mark -
167 
168 // User Datagram Protocol (UDP)
169 
170 // UDP header (RFC 768)
171 struct udp_header {
172 	uint16	source;			// source port
173 	uint16	destination;	// destination port
174 	uint16	length;			// length of UDP packet (header + data)
175 	uint16	checksum;		// checksum
176 } __attribute__ ((__packed__));
177 
178 
179 // Transmission Control Protocol (TCP)
180 
181 // TCP header (RFC 793, RFC 3168)
182 struct tcp_header {
183 	uint16	source;			// source port
184 	uint16	destination;	// destination port
185 	uint32	seqNumber;		// sequence number
186 	uint32	ackNumber;		// acknowledgment number
187 #if __BYTE_ORDER == __BIG_ENDIAN
188 	uint8	dataOffset : 4;	// data offset
189 	uint8	reserved : 4;	// reserved
190 #elif __BYTE_ORDER == __LITTLE_ENDIAN
191 	uint8	reserved : 4;
192 	uint8	dataOffset : 4;
193 #endif
194 	uint8	flags;			// ACK, SYN, FIN, etc.
195 	uint16	window;			// window size
196 	uint16	checksum;		// checksum
197 	uint16	urgentPointer;	// urgent pointer
198 } __attribute__ ((__packed__));
199 
200 
201 // #pragma mark -
202 
203 // NetService
204 
205 // net service names
206 extern const char *const kEthernetServiceName;
207 extern const char *const kARPServiceName;
208 extern const char *const kIPServiceName;
209 extern const char *const kUDPServiceName;
210 
211 class NetService {
212 public:
213 	NetService(const char *name);
214 	virtual ~NetService();
215 
216 	const char *NetServiceName();
217 
218 	virtual int CountSubNetServices() const;
219 	virtual NetService *SubNetServiceAt(int index) const;
220 	virtual NetService *FindSubNetService(const char *name) const;
221 
222 	template<typename ServiceType>
223 	ServiceType *FindSubNetService(const char *name) const
224 	{
225 		// We should actually use dynamic_cast<>(), but we better spare us the
226 		// RTTI stuff.
227 		if (NetService *service = FindSubNetService(name))
228 			return static_cast<ServiceType*>(service);
229 		return NULL;
230 	}
231 
232 private:
233 	const char	*fName;
234 };
235 
236 
237 #endif	// _BOOT_NET_DEFS_H
238