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