VirtualBox

source: vbox/trunk/include/iprt/net.h@ 11025

Last change on this file since 11025 was 11025, checked in by vboxsync, 16 years ago

iprt/net.h: Added some ICMP (v4) bits.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1/** @file
2 * IPRT - Network Protocols.
3 */
4
5/*
6 * Copyright (C) 2008 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 *
25 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_net_h
31#define ___iprt_net_h
32
33#include <iprt/cdefs.h>
34#include <iprt/types.h>
35#include <iprt/assert.h>
36
37
38__BEGIN_DECLS
39
40/** @defgroup grp_rt_net RTNet - Network Protocols
41 * @ingroup grp_rt
42 * @{
43 */
44
45/**
46 * IPv4 address.
47 */
48typedef RTUINT32U RTNETADDRIPV4;
49AssertCompileSize(RTNETADDRIPV4, 4);
50/** Pointer to a IPv4 address. */
51typedef RTNETADDRIPV4 *PRTNETADDRIPV4;
52/** Pointer to a const IPv4 address. */
53typedef RTNETADDRIPV4 const *PCRTNETADDRIPV4;
54
55/**
56 * IPv6 address.
57 */
58typedef RTUINT128U RTNETADDRIPV6;
59AssertCompileSize(RTNETADDRIPV6, 16);
60/** Pointer to a IPv4 address. */
61typedef RTNETADDRIPV6 *PRTNETADDRIPV6;
62/** Pointer to a const IPv4 address. */
63typedef RTNETADDRIPV6 const *PCRTNETADDRIPV6;
64
65/**
66 * IPX address.
67 */
68#pragma pack(1)
69typedef struct RTNETADDRIPX
70{
71 /** The network ID. */
72 uint32_t Network;
73 /** The node ID. (Defaults to the MAC address apparently.) */
74 RTMAC Node;
75} RTNETADDRIPX;
76#pragma pack(0)
77AssertCompileSize(RTNETADDRIPX, 4+6);
78/** Pointer to an IPX address. */
79typedef RTNETADDRIPX *PRTNETADDRIPX;
80/** Pointer to a const IPX address. */
81typedef RTNETADDRIPX const *PCRTNETADDRIPX;
82
83/**
84 * Address union.
85 */
86typedef union RTNETADDRU
87{
88 /** 64-bit view. */
89 uint64_t au64[2];
90 /** 32-bit view. */
91 uint32_t au32[4];
92 /** 16-bit view. */
93 uint16_t au16[8];
94 /** 8-bit view. */
95 uint8_t au8[16];
96 /** IPv4 view. */
97 RTNETADDRIPV4 IPv4;
98 /** IPv6 view. */
99 RTNETADDRIPV6 IPv6;
100 /** IPX view. */
101 RTNETADDRIPX Ipx;
102 /** MAC address view. */
103 RTMAC Mac;
104} RTNETADDRU;
105AssertCompileSize(RTNETADDRU, 16);
106/** Pointer to an address union. */
107typedef RTNETADDRU *PRTNETADDRU;
108/** Pointer to a const address union. */
109typedef RTNETADDRU const *PCRTNETADDRU;
110
111
112/**
113 * Ethernet header.
114 */
115#pragma pack(1)
116typedef struct RTNETETHERHDR
117{
118 RTMAC DstMac;
119 RTMAC SrcMac;
120 /** Ethernet frame type or frame size, depending on the kind of ethernet.
121 * This is big endian on the wire. */
122 uint16_t EtherType;
123} RTNETETHERHDR;
124#pragma pack()
125AssertCompileSize(RTNETETHERHDR, 14);
126/** Pointer to an ethernet header. */
127typedef RTNETETHERHDR *PRTNETETHERHDR;
128/** Pointer to a const ethernet header. */
129typedef RTNETETHERHDR const *PCRTNETETHERHDR;
130
131/** @name EtherType (RTNETETHERHDR::EtherType)
132 * @{ */
133#define RTNET_ETHERTYPE_IPV4 UINT16_C(0x0800)
134#define RTNET_ETHERTYPE_ARP UINT16_C(0x0806)
135#define RTNET_ETHERTYPE_IPV6 UINT16_C(0x86dd)
136/** @} */
137
138
139/**
140 * IPv4 header.
141 * All is bigendian on the wire.
142 */
143#pragma pack(1)
144typedef struct RTNETIPV4
145{
146#ifdef RT_BIG_ENDIAN
147 unsigned int ip_v : 4;
148 unsigned int ip_hl : 4;
149 unsigned int ip_tos : 8;
150 unsigned int ip_len : 16;
151#else
152 /** 00:0 - Header length given as a 32-bit word count. */
153 unsigned int ip_hl : 4;
154 /** 00:4 - Header version. */
155 unsigned int ip_v : 4;
156 /** 01 - Type of service. */
157 unsigned int ip_tos : 8;
158 /** 02 - Total length (header + data). */
159 unsigned int ip_len : 16;
160#endif
161 /** 04 - Packet idenficiation. */
162 uint16_t ip_id;
163 /** 06 - Offset if fragmented. */
164 uint16_t ip_off;
165 /** 08 - Time to live. */
166 uint8_t ip_ttl;
167 /** 09 - Protocol. */
168 uint8_t ip_p;
169 /** 0a - Header check sum. */
170 uint16_t ip_sum;
171 /** 0c - Source address. */
172 RTNETADDRIPV4 ip_src;
173 /** 10 - Destination address. */
174 RTNETADDRIPV4 ip_dst;
175 /** 14 - Options (optional). */
176 uint32_t ip_options[1];
177} RTNETIPV4;
178#pragma pack(0)
179AssertCompileSize(RTNETIPV4, 6 * 4);
180/** Pointer to a IPv4 header. */
181typedef RTNETIPV4 *PRTNETIPV4;
182/** Pointer to a const IPv4 header. */
183typedef RTNETIPV4 const *PCRTNETIPV4;
184
185/** The minimum IPv4 header length (in bytes).
186 * Up to and including RTNETIPV4::ip_dst. */
187#define RTNETIPV4_MIN_LEN (20)
188
189
190/** @name IPv4 Protocol Numbers
191 * @{ */
192/** IPv4: ICMP */
193#define RTNETIPV4_PROT_ICMP (0)
194/** IPv4: TCP */
195#define RTNETIPV4_PROT_TCP (6)
196/** IPv4: UDP */
197#define RTNETIPV4_PROT_UDP (17)
198/** @} */
199
200RTDECL(uint16_t) RTNetIPv4HdrChecksum(PCRTNETIPV4 pIpHdr);
201RTDECL(bool) RTNetIPv4IsHdrValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax);
202RTDECL(uint32_t) RTNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr);
203RTDECL(uint32_t) RTNetIPv4PseudoChecksumBits(RTNETADDRIPV4 SrcAddr, RTNETADDRIPV4 DstAddr, uint8_t bProtocol, uint16_t cbPkt);
204RTDECL(uint32_t) RTNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t u32Sum, bool *pfOdd);
205RTDECL(uint16_t) RTNetIPv4FinalizeChecksum(uint32_t u32Sum);
206
207
208/**
209 * UDP header.
210 */
211#pragma pack(1)
212typedef struct RTNETUDP
213{
214 /** The source port. */
215 uint16_t uh_sport;
216 /** The destination port. */
217 uint16_t uh_dport;
218 /** The length of the UDP header and associated data. */
219 uint16_t uh_ulen;
220 /** The checksum of the pseudo header, the UDP header and the data. */
221 uint16_t uh_sum;
222} RTNETUDP;
223#pragma pack(0)
224AssertCompileSize(RTNETUDP, 8);
225/** Pointer to an UDP header. */
226typedef RTNETUDP *PRTNETUDP;
227/** Pointer to a const UDP header. */
228typedef RTNETUDP const *PCRTNETUDP;
229
230/** The minimum UDP packet length (in bytes). (RTNETUDP::uh_ulen) */
231#define RTNETUDP_MIN_LEN (8)
232
233RTDECL(uint32_t) RTNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t u32Sum);
234RTDECL(uint16_t) RTNetIPv4UDPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData);
235RTDECL(bool) RTNetIPv4IsUDPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, size_t cbPktMax);
236RTDECL(bool) RTNetIPv4IsUDPValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData, size_t cbPktMax);
237
238
239/**
240 * IPv4 DHCP packet.
241 */
242#pragma pack(1)
243typedef struct RTNETDHCP
244{
245 uint8_t Op;
246 /** Hardware address type. */
247 uint8_t HType;
248 /** Hardware address length. */
249 uint8_t HLen;
250 uint8_t Hops;
251 uint32_t XID;
252 uint16_t Secs;
253 uint16_t Flags;
254 /** Client IPv4 address. */
255 RTNETADDRIPV4 CIAddr;
256 /** Your IPv4 address. */
257 RTNETADDRIPV4 YIAddr;
258 /** Server IPv4 address. */
259 RTNETADDRIPV4 SIAddr;
260 /** Gateway IPv4 address. */
261 RTNETADDRIPV4 GIAddr;
262 /** Client hardware address. */
263 uint8_t CHAddr[16];
264 /** Server name. */
265 uint8_t SName[64];
266 uint8_t File[128];
267 uint8_t abMagic[4];
268 uint8_t DhcpOpt;
269 uint8_t DhcpLen; /* 1 */
270 uint8_t DhcpReq;
271 uint8_t abOptions[57];
272} RTNETDHCP;
273#pragma pack(0)
274/** @todo AssertCompileSize(RTNETDHCP, ); */
275/** Pointer to a DHCP packet. */
276typedef RTNETDHCP *PRTNETDHCP;
277/** Pointer to a const DHCP packet. */
278typedef RTNETDHCP const *PCRTNETDHCP;
279
280
281/**
282 * TCP packet.
283 */
284#pragma pack(1)
285typedef struct RTNETTCP
286{
287 /** 00 - The source port. */
288 uint16_t th_sport;
289 /** 02 - The destination port. */
290 uint16_t th_dport;
291 /** 04 - The sequence number. */
292 uint32_t th_seq;
293 /** 08 - The acknowledgement number. */
294 uint32_t th_ack;
295#ifdef RT_BIG_ENDIAN
296 unsigned int th_win : 16;
297 unsigned int th_flags : 8;
298 unsigned int th_off : 4;
299 unsigned int th_x2 : 4;
300#else
301 /** 0c:0 - Reserved. */
302 unsigned int th_x2 : 4;
303 /** 0c:4 - The data offset given as a dword count from the start of this header. */
304 unsigned int th_off : 4;
305 /** 0d - flags. */
306 unsigned int th_flags : 8;
307 /** 0e - The window. */
308 unsigned int th_win : 16;
309#endif
310 /** 10 - The checksum of the pseudo header, the TCP header and the data. */
311 uint16_t th_sum;
312 /** 12 - The urgent pointer. */
313 uint16_t th_urp;
314 /* (options follows here and then the data (aka text).) */
315} RTNETTCP;
316#pragma pack(0)
317AssertCompileSize(RTNETTCP, 20);
318/** Pointer to a TCP packet. */
319typedef RTNETTCP *PRTNETTCP;
320/** Pointer to a const TCP packet. */
321typedef RTNETTCP const *PCRTNETTCP;
322
323/** The minimum TCP header length (in bytes). (RTNETTCP::th_off * 4) */
324#define RTNETTCP_MIN_LEN (20)
325
326RTDECL(uint32_t) RTNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t u32Sum);
327
328
329/**
330 * IPv4 ICMP packet header.
331 */
332#pragma pack(1)
333typedef struct RTNETICMPV4HDR
334{
335 /** 00 - The ICMP message type. */
336 uint8_t icmp_type;
337 /** 01 - Type specific code that further qualifies the message. */
338 uint8_t icmp_code;
339 /** 02 - Checksum of the ICMP message. */
340 uint16_t icmp_cksum;
341} RTNETICMPV4HDR;
342#pragma pack(0)
343AssertCompileSize(RTNETICMPV4HDR, 4);
344/** Pointer to an ICMP packet header. */
345typedef RTNETICMPV4HDR *PRTNETICMPV4HDR;
346/** Pointer to a const ICMP packet header. */
347typedef RTNETICMPV4HDR const *PCRTNETICMPV4HDR;
348
349/** @name ICMP (v4) message types.
350 * @{ */
351#define RTNETICMPV4_TYPE_ECHO_REPLY 0
352#define RTNETICMPV4_TYPE_ECHO_REQUEST 8
353#define RTNETICMPV4_TYPE_TRACEROUTE 30
354/** @} */
355
356/**
357 * IPv4 ICMP ECHO Reply & Request packet.
358 */
359#pragma pack(1)
360typedef struct RTNETICMPV4ECHO
361{
362 /** 00 - The ICMP header. */
363 RTNETICMPV4HDR Hdr;
364 /** 04 - The identifier to help the requestor match up the reply.
365 * Can be 0. Typically fixed value. */
366 uint16_t icmp_id;
367 /** 06 - The sequence number to help the requestor match up the reply.
368 * Can be 0. Typically incrementing between requests. */
369 uint16_t icmp_seq;
370 /** 08 - Variable length data that is to be returned unmodified in the reply. */
371 uint8_t icmp_data[1];
372} RTNETICMPV4ECHO;
373#pragma pack(0)
374AssertCompileSize(RTNETICMPV4ECHO, 9);
375/** Pointer to an ICMP ECHO packet. */
376typedef RTNETICMPV4ECHO *PRTNETICMPV4ECHO;
377/** Pointer to a const ICMP ECHO packet. */
378typedef RTNETICMPV4ECHO const *PCRTNETICMPV4ECHO;
379
380/**
381 * IPv4 ICMP TRACEROUTE packet.
382 * This is an reply to an IP packet with the traceroute option set.
383 */
384#pragma pack(1)
385typedef struct RTNETICMPV4TRACEROUTE
386{
387 /** 00 - The ICMP header. */
388 RTNETICMPV4HDR Hdr;
389 /** 04 - Identifier copied from the traceroute option's ID number. */
390 uint16_t icmp_id;
391 /** 06 - Unused. (Possibly an icmp_seq?) */
392 uint16_t icmp_void;
393 /** 08 - Outbound hop count. From the IP packet causing this message. */
394 uint16_t icmp_ohc;
395 /** 0a - Return hop count. From the IP packet causing this message. */
396 uint16_t icmp_rhc;
397 /** 0c - Output link speed, 0 if not known. */
398 uint32_t icmp_speed;
399 /** 10 - Output link MTU, 0 if not known. */
400 uint32_t icmp_mtu;
401} RTNETICMPV4TRACEROUTE;
402#pragma pack(0)
403AssertCompileSize(RTNETICMPV4TRACEROUTE, 20);
404/** Pointer to an ICMP TRACEROUTE packet. */
405typedef RTNETICMPV4TRACEROUTE *PRTNETICMPV4TRACEROUTE;
406/** Pointer to a const ICMP TRACEROUTE packet. */
407typedef RTNETICMPV4TRACEROUTE const *PCRTNETICMPV4TRACEROUTE;
408
409/** @todo add more ICMPv4 as needed. */
410
411/**
412 * IPv4 ICMP union packet.
413 */
414typedef union RTNETICMPV4
415{
416 RTNETICMPV4HDR Hdr;
417 RTNETICMPV4ECHO Echo;
418 RTNETICMPV4TRACEROUTE Traceroute;
419} RTNETICMPV4;
420/** Pointer to an ICMP union packet. */
421typedef RTNETICMPV4 *PRTNETICMPV4;
422/** Pointer to a const ICMP union packet. */
423typedef RTNETICMPV4 const *PCRTNETICMPV4;
424
425
426/** @todo add ICMPv6 when needed. */
427
428
429/**
430 * Ethernet ARP header.
431 */
432#pragma pack(1)
433typedef struct RTNETARPHDR
434{
435 /** The hardware type. */
436 uint16_t ar_htype;
437 /** The protocol type (ethertype). */
438 uint16_t ar_ptype;
439 /** The hardware address length. */
440 uint8_t ar_hlen;
441 /** The protocol address length. */
442 uint8_t ar_plen;
443 /** The operation. */
444 uint16_t ar_oper;
445} RTNETARPHDR;
446#pragma pack(0)
447AssertCompileSize(RTNETARPHDR, 8);
448/** Pointer to an ethernet ARP header. */
449typedef RTNETARPHDR *PRTNETARPHDR;
450/** Pointer to a const ethernet ARP header. */
451typedef RTNETARPHDR const *PCRTNETARPHDR;
452
453/** ARP hardware type - ethernet. */
454#define RTNET_ARP_ETHER UINT16_C(1)
455
456/** @name ARP operations
457 * @{ */
458#define RTNET_ARPOP_REQUEST UINT16_C(1) /**< Request hardward address given a protocol address (ARP). */
459#define RTNET_ARPOP_REPLY UINT16_C(2)
460#define RTNET_ARPOP_REVREQUEST UINT16_C(3) /**< Request protocol address given a hardware address (RARP). */
461#define RTNET_ARPOP_REVREPLY UINT16_C(4)
462#define RTNET_ARPOP_INVREQUEST UINT16_C(8) /**< Inverse ARP. */
463#define RTNET_ARPOP_INVREPLY UINT16_C(9)
464/** Check if an ARP operation is a request or not. */
465#define RTNET_ARPOP_IS_REQUEST(Op) ((Op) & 1)
466/** Check if an ARP operation is a reply or not. */
467#define RTNET_ARPOP_IS_REPLY(Op) (!RTNET_ARPOP_IS_REQUEST(Op))
468/** @} */
469
470
471/**
472 * Ethernet IPv4 + 6-byte MAC ARP request packet.
473 */
474#pragma pack(1)
475typedef struct RTNETARPIPV4
476{
477 /** ARP header. */
478 RTNETARPHDR Hdr;
479 /** The sender hardware address. */
480 RTMAC ar_sha;
481 /** The sender protocol address. */
482 RTNETADDRIPV4 ar_spa;
483 /** The target hardware address. */
484 RTMAC ar_tha;
485 /** The arget protocol address. */
486 RTNETADDRIPV4 ar_tpa;
487} RTNETARPIPV4;
488#pragma pack(0)
489AssertCompileSize(RTNETARPIPV4, 8+6+4+6+4);
490/** Pointer to an ethernet IPv4+MAC ARP request packet. */
491typedef RTNETARPIPV4 *PRTNETARPIPV4;
492/** Pointer to a const ethernet IPv4+MAC ARP request packet. */
493typedef RTNETARPIPV4 const *PCRTNETARPIPV4;
494
495
496/** @todo RTNETNDP (IPv6)*/
497
498
499/** @} */
500
501__END_DECLS
502
503#endif
504
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette