VirtualBox

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

Last change on this file since 84341 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.2 KB
Line 
1/** @file
2 * IPRT - Network Protocols.
3 */
4
5/*
6 * Copyright (C) 2008-2020 Oracle Corporation
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
26#ifndef IPRT_INCLUDED_net_h
27#define IPRT_INCLUDED_net_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/cdefs.h>
33#include <iprt/types.h>
34#include <iprt/assert.h>
35
36
37RT_C_DECLS_BEGIN
38
39/** @defgroup grp_rt_net RTNet - Network Protocols
40 * @ingroup grp_rt
41 * @{
42 */
43
44/**
45 * Converts an stringified Ethernet MAC address into the RTMAC representation.
46 *
47 * @todo This should be move to some generic part of the runtime.
48 *
49 * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
50 * failure.
51 *
52 * @param pszAddr The address string to convert.
53 * @param pMacAddr Where to store the result.
54 */
55RTDECL(int) RTNetStrToMacAddr(const char *pszAddr, PRTMAC pMacAddr);
56
57/**
58 * IPv4 address.
59 */
60typedef RTUINT32U RTNETADDRIPV4;
61AssertCompileSize(RTNETADDRIPV4, 4);
62/** Pointer to a IPv4 address. */
63typedef RTNETADDRIPV4 *PRTNETADDRIPV4;
64/** Pointer to a const IPv4 address. */
65typedef RTNETADDRIPV4 const *PCRTNETADDRIPV4;
66
67/**
68 * Tests if the given string is an IPv4 address.
69 *
70 * @returns boolean.
71 * @param pcszAddr String which may be an IPv4 address.
72 */
73RTDECL(bool) RTNetIsIPv4AddrStr(const char *pcszAddr);
74
75/**
76 * Tests if the given string is a wildcard IPv4 address.
77 *
78 * @returns boolean.
79 * @param pcszAddr String which may be an IPv4 address.
80 */
81RTDECL(bool) RTNetStrIsIPv4AddrAny(const char *pcszAddr);
82
83/**
84 * Parses dotted-decimal IPv4 address into RTNETADDRIPV4 representation.
85 *
86 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
87 * failure.
88 *
89 * @param pcszAddr The value to convert.
90 * @param ppszNext Where to store the pointer to the first char
91 * following the address. (Optional)
92 * @param pAddr Where to store the result.
93 */
94RTDECL(int) RTNetStrToIPv4AddrEx(const char *pcszAddr, PRTNETADDRIPV4 pAddr, char **ppszNext);
95
96/**
97 * Parses dotted-decimal IPv4 address into RTNETADDRIPV4 representation.
98 * Leading and trailing whitespace is ignored.
99 *
100 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
101 * failure.
102 *
103 * @param pcszAddr The value to convert.
104 * @param pAddr Where to store the result.
105 */
106RTDECL(int) RTNetStrToIPv4Addr(const char *pcszAddr, PRTNETADDRIPV4 pAddr);
107
108/**
109 * Parses dotted-decimal IPv4 CIDR notation into RTNETADDRIPV4
110 * representation and prefix length. Missing prefix specification is
111 * treated as exact address specification (prefix length 32). Leading
112 * and trailing whitespace is ignored.
113 *
114 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
115 * failure.
116 *
117 * @param pcszAddr The value to convert.
118 * @param pAddr Where to store the address.
119 * @param piPrefix Where to store the prefix length;
120 */
121RTDECL(int) RTNetStrToIPv4Cidr(const char *pcszAddr, PRTNETADDRIPV4 pAddr, int *piPrefix);
122
123/**
124 * Verifies that RTNETADDRIPV4 is a valid contiguous netmask and
125 * computes its prefix length.
126 *
127 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
128 * failure.
129 *
130 * @param pMask The netmask to verify and convert.
131 * @param piPrefix Where to store the prefix length. (Optional)
132 */
133RTDECL(int) RTNetMaskToPrefixIPv4(PCRTNETADDRIPV4 pMask, int *piPrefix);
134
135/**
136 * Computes netmask corresponding to the prefix length.
137 *
138 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
139 * failure.
140 *
141 * @param iPrefix The prefix to convert.
142 * @param pMask Where to store the netmask.
143 */
144RTDECL(int) RTNetPrefixToMaskIPv4(int iPrefix, PRTNETADDRIPV4 pMask);
145
146
147/**
148 * IPv6 address.
149 */
150typedef RTUINT128U RTNETADDRIPV6;
151AssertCompileSize(RTNETADDRIPV6, 16);
152/** Pointer to a IPv6 address. */
153typedef RTNETADDRIPV6 *PRTNETADDRIPV6;
154/** Pointer to a const IPv6 address. */
155typedef RTNETADDRIPV6 const *PCRTNETADDRIPV6;
156
157/**
158 * Tests if the given string is a valid IPv6 address.
159 *
160 * @returns @c true if it is, @c false if not.
161 * @param pszAddress String which may be an IPv6 address.
162 */
163RTDECL(bool) RTNetIsIPv6AddrStr(const char *pszAddress);
164
165/**
166 * Tests if the given string is a wildcard IPv6 address.
167 *
168 * @returns @c true if it is, @c false if not.
169 * @param pszAddress String which may be an IPv6 address.
170 */
171RTDECL(bool) RTNetStrIsIPv6AddrAny(const char *pszAddress);
172
173/**
174 * Parses IPv6 address into RTNETADDRIPV6 representation.
175 *
176 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
177 * failure.
178 *
179 * @param pcszAddr The value to convert.
180 * @param ppszNext Where to store the pointer to the first char
181 * following the address. (Optional)
182 * @param pAddr Where to store the result.
183 */
184RTDECL(int) RTNetStrToIPv6AddrEx(const char *pcszAddr, PRTNETADDRIPV6 pAddr, char **ppszNext);
185
186/**
187 * Parses IPv6 address into RTNETADDRIPV6 representation.
188 * Leading and trailing whitespace is ignored.
189 *
190 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
191 * failure.
192 *
193 * @param pcszAddr The value to convert.
194 * @param ppszZone Where to store the pointer to the first char
195 * of the zone id. NULL is stored if there is
196 * no zone id.
197 * @param pAddr Where to store the result.
198 */
199RTDECL(int) RTNetStrToIPv6Addr(const char *pcszAddr, PRTNETADDRIPV6 pAddr, char **ppszZone);
200
201/**
202 * Verifies that RTNETADDRIPV6 is a valid contiguous netmask and
203 * computes its prefix length.
204 *
205 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
206 * failure.
207 *
208 * @param pMask The netmask to verify and convert.
209 * @param piPrefix Where to store the prefix length. (Optional)
210 */
211RTDECL(int) RTNetMaskToPrefixIPv6(PCRTNETADDRIPV6 pMask, int *piPrefix);
212
213/**
214 * Computes netmask corresponding to the prefix length.
215 *
216 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
217 * failure.
218 *
219 * @param iPrefix The prefix to convert.
220 * @param pMask Where to store the netmask.
221 */
222RTDECL(int) RTNetPrefixToMaskIPv6(int iPrefix, PRTNETADDRIPV6 pMask);
223
224
225/**
226 * IPX address.
227 */
228#pragma pack(1)
229typedef struct RTNETADDRIPX
230{
231 /** The network ID. */
232 uint32_t Network;
233 /** The node ID. (Defaults to the MAC address apparently.) */
234 RTMAC Node;
235} RTNETADDRIPX;
236#pragma pack()
237AssertCompileSize(RTNETADDRIPX, 4+6);
238/** Pointer to an IPX address. */
239typedef RTNETADDRIPX *PRTNETADDRIPX;
240/** Pointer to a const IPX address. */
241typedef RTNETADDRIPX const *PCRTNETADDRIPX;
242
243/**
244 * Network address union.
245 *
246 * @remarks The size of this structure may change in the future.
247 */
248typedef union RTNETADDRU
249{
250 /** 64-bit view. */
251 uint64_t au64[2];
252 /** 32-bit view. */
253 uint32_t au32[4];
254 /** 16-bit view. */
255 uint16_t au16[8];
256 /** 8-bit view. */
257 uint8_t au8[16];
258 /** IPv4 view. */
259 RTNETADDRIPV4 IPv4;
260#ifndef IPv6 /* Work around X11 and RDP defining IPv6 to 1. */
261 /** IPv6 view. */
262 RTNETADDRIPV6 IPv6;
263#endif
264 /** IPX view. */
265 RTNETADDRIPX Ipx;
266 /** MAC address view. */
267 RTMAC Mac;
268} RTNETADDRU;
269AssertCompileSize(RTNETADDRU, 16);
270/** Pointer to an address union. */
271typedef RTNETADDRU *PRTNETADDRU;
272/** Pointer to a const address union. */
273typedef RTNETADDRU const *PCRTNETADDRU;
274
275/**
276 * Network address type.
277 *
278 * @remarks The value assignments may change in the future.
279 */
280typedef enum RTNETADDRTYPE
281{
282 /** The invalid 0 entry. */
283 RTNETADDRTYPE_INVALID = 0,
284 /** IP version 4. */
285 RTNETADDRTYPE_IPV4,
286 /** IP version 6. */
287 RTNETADDRTYPE_IPV6,
288 /** IPX. */
289 RTNETADDRTYPE_IPX,
290 /** MAC address. */
291 RTNETADDRTYPE_MAC,
292 /** The end of the valid values. */
293 RTNETADDRTYPE_END,
294 /** The usual 32-bit hack. */
295 RTNETADDRTYPE_32_BIT_HACK = 0x7fffffff
296} RTNETADDRTYPE;
297/** Pointer to a network address type. */
298typedef RTNETADDRTYPE *PRTNETADDRTYPE;
299/** Pointer to a const network address type. */
300typedef RTNETADDRTYPE const *PCRTNETADDRTYPE;
301
302/**
303 * Network address.
304 *
305 * @remarks The size and type values may change.
306 */
307typedef struct RTNETADDR
308{
309 /** The address union. */
310 RTNETADDRU uAddr;
311 /** Indicates which view of @a u that is valid. */
312 RTNETADDRTYPE enmType;
313 /** The port number for IPv4 and IPv6 addresses. This is set to
314 * RTNETADDR_NA_PORT if not applicable. */
315 uint32_t uPort;
316} RTNETADDR;
317/** Pointer to a network address. */
318typedef RTNETADDR *PRTNETADDR;
319/** Pointer to a const network address. */
320typedef RTNETADDR const *PCRTNETADDR;
321
322/** The not applicable value of RTNETADDR::uPort value use to inid. */
323#define RTNETADDR_PORT_NA UINT32_MAX
324
325/**
326 * Ethernet header.
327 */
328#pragma pack(1)
329typedef struct RTNETETHERHDR
330{
331 RTMAC DstMac;
332 RTMAC SrcMac;
333 /** Ethernet frame type or frame size, depending on the kind of ethernet.
334 * This is big endian on the wire. */
335 uint16_t EtherType;
336} RTNETETHERHDR;
337#pragma pack()
338AssertCompileSize(RTNETETHERHDR, 14);
339/** Pointer to an ethernet header. */
340typedef RTNETETHERHDR *PRTNETETHERHDR;
341/** Pointer to a const ethernet header. */
342typedef RTNETETHERHDR const *PCRTNETETHERHDR;
343
344/** @name EtherType (RTNETETHERHDR::EtherType)
345 * @{ */
346#define RTNET_ETHERTYPE_IPV4 UINT16_C(0x0800)
347#define RTNET_ETHERTYPE_ARP UINT16_C(0x0806)
348#define RTNET_ETHERTYPE_IPV6 UINT16_C(0x86dd)
349#define RTNET_ETHERTYPE_VLAN UINT16_C(0x8100)
350#define RTNET_ETHERTYPE_IPX_1 UINT16_C(0x8037)
351#define RTNET_ETHERTYPE_IPX_2 UINT16_C(0x8137)
352#define RTNET_ETHERTYPE_IPX_3 UINT16_C(0x8138)
353/** @} */
354
355
356/**
357 * IPv4 header.
358 * All is bigendian on the wire.
359 */
360#pragma pack(1)
361typedef struct RTNETIPV4
362{
363#ifdef RT_BIG_ENDIAN
364 unsigned int ip_v : 4;
365 unsigned int ip_hl : 4;
366 unsigned int ip_tos : 8;
367 unsigned int ip_len : 16;
368#else
369 /** 00:0 - Header length given as a 32-bit word count. */
370 unsigned int ip_hl : 4;
371 /** 00:4 - Header version. */
372 unsigned int ip_v : 4;
373 /** 01 - Type of service. */
374 unsigned int ip_tos : 8;
375 /** 02 - Total length (header + data). */
376 unsigned int ip_len : 16;
377#endif
378 /** 04 - Packet idenficiation. */
379 uint16_t ip_id;
380 /** 06 - Offset if fragmented. */
381 uint16_t ip_off;
382 /** 08 - Time to live. */
383 uint8_t ip_ttl;
384 /** 09 - Protocol. */
385 uint8_t ip_p;
386 /** 0a - Header check sum. */
387 uint16_t ip_sum;
388 /** 0c - Source address. */
389 RTNETADDRIPV4 ip_src;
390 /** 10 - Destination address. */
391 RTNETADDRIPV4 ip_dst;
392 /** 14 - Options (optional). */
393 uint32_t ip_options[1];
394} RTNETIPV4;
395#pragma pack()
396AssertCompileSize(RTNETIPV4, 6 * 4);
397/** Pointer to a IPv4 header. */
398typedef RTNETIPV4 *PRTNETIPV4;
399/** Pointer to a const IPv4 header. */
400typedef RTNETIPV4 const *PCRTNETIPV4;
401
402/** The minimum IPv4 header length (in bytes).
403 * Up to and including RTNETIPV4::ip_dst. */
404#define RTNETIPV4_MIN_LEN (20)
405
406
407/** @name IPv4 Protocol Numbers
408 * @{ */
409/** IPv4: ICMP */
410#define RTNETIPV4_PROT_ICMP (1)
411/** IPv4: TCP */
412#define RTNETIPV4_PROT_TCP (6)
413/** IPv4: UDP */
414#define RTNETIPV4_PROT_UDP (17)
415/** @} */
416
417/** @name Common IPv4 Port Assignments
418 * @{
419 */
420/** Boostrap Protocol / DHCP) Server. */
421#define RTNETIPV4_PORT_BOOTPS (67)
422/** Boostrap Protocol / DHCP) Client. */
423#define RTNETIPV4_PORT_BOOTPC (68)
424/** @} */
425
426/** @name IPv4 Flags
427 * @{ */
428/** IPv4: Don't fragment */
429#define RTNETIPV4_FLAGS_DF (0x4000)
430/** IPv4: More fragments */
431#define RTNETIPV4_FLAGS_MF (0x2000)
432/** @} */
433
434RTDECL(uint16_t) RTNetIPv4HdrChecksum(PCRTNETIPV4 pIpHdr);
435RTDECL(bool) RTNetIPv4IsHdrValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax, bool fChecksum);
436RTDECL(uint32_t) RTNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr);
437RTDECL(uint32_t) RTNetIPv4PseudoChecksumBits(RTNETADDRIPV4 SrcAddr, RTNETADDRIPV4 DstAddr, uint8_t bProtocol, uint16_t cbPkt);
438RTDECL(uint32_t) RTNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t u32Sum, bool *pfOdd);
439RTDECL(uint16_t) RTNetIPv4FinalizeChecksum(uint32_t u32Sum);
440
441
442/**
443 * IPv6 header.
444 * All is bigendian on the wire.
445 */
446#pragma pack(1)
447typedef struct RTNETIPV6
448{
449 /** Version (4 bits), Traffic Class (8 bits) and Flow Lable (20 bits).
450 * @todo this is probably mislabeled - ip6_flow vs. ip6_vfc, fix later. */
451 uint32_t ip6_vfc;
452 /** 04 - Payload length, including extension headers. */
453 uint16_t ip6_plen;
454 /** 06 - Next header type (RTNETIPV4_PROT_XXX). */
455 uint8_t ip6_nxt;
456 /** 07 - Hop limit. */
457 uint8_t ip6_hlim;
458 /** xx - Source address. */
459 RTNETADDRIPV6 ip6_src;
460 /** xx - Destination address. */
461 RTNETADDRIPV6 ip6_dst;
462} RTNETIPV6;
463#pragma pack()
464AssertCompileSize(RTNETIPV6, 8 + 16 + 16);
465/** Pointer to a IPv6 header. */
466typedef RTNETIPV6 *PRTNETIPV6;
467/** Pointer to a const IPv6 header. */
468typedef RTNETIPV6 const *PCRTNETIPV6;
469
470/** The minimum IPv6 header length (in bytes).
471 * Up to and including RTNETIPV6::ip6_dst. */
472#define RTNETIPV6_MIN_LEN (40)
473#define RTNETIPV6_ICMPV6_ND_WITH_LLA_OPT_MIN_LEN (32)
474
475RTDECL(uint32_t) RTNetIPv6PseudoChecksum(PCRTNETIPV6 pIpHdr);
476RTDECL(uint32_t) RTNetIPv6PseudoChecksumEx(PCRTNETIPV6 pIpHdr, uint8_t bProtocol, uint16_t cbPkt);
477RTDECL(uint32_t) RTNetIPv6PseudoChecksumBits(PCRTNETADDRIPV6 pSrcAddr, PCRTNETADDRIPV6 pDstAddr,
478 uint8_t bProtocol, uint16_t cbPkt);
479
480
481/**
482 * UDP header.
483 */
484#pragma pack(1)
485typedef struct RTNETUDP
486{
487 /** The source port. */
488 uint16_t uh_sport;
489 /** The destination port. */
490 uint16_t uh_dport;
491 /** The length of the UDP header and associated data. */
492 uint16_t uh_ulen;
493 /** The checksum of the pseudo header, the UDP header and the data. */
494 uint16_t uh_sum;
495} RTNETUDP;
496#pragma pack()
497AssertCompileSize(RTNETUDP, 8);
498/** Pointer to an UDP header. */
499typedef RTNETUDP *PRTNETUDP;
500/** Pointer to a const UDP header. */
501typedef RTNETUDP const *PCRTNETUDP;
502
503/** The minimum UDP packet length (in bytes). (RTNETUDP::uh_ulen) */
504#define RTNETUDP_MIN_LEN (8)
505
506RTDECL(uint16_t) RTNetUDPChecksum(uint32_t u32Sum, PCRTNETUDP pUdpHdr);
507RTDECL(uint32_t) RTNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t u32Sum);
508RTDECL(uint16_t) RTNetIPv4UDPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData);
509RTDECL(bool) RTNetIPv4IsUDPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, size_t cbPktMax);
510RTDECL(bool) RTNetIPv4IsUDPValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData, size_t cbPktMax, bool fChecksum);
511
512
513/**
514 * IPv4 BOOTP / DHCP packet.
515 */
516#pragma pack(1)
517typedef struct RTNETBOOTP
518{
519 /** 00 - The packet opcode (RTNETBOOTP_OP_*). */
520 uint8_t bp_op;
521 /** 01 - Hardware address type. Same as RTNETARPHDR::ar_htype. */
522 uint8_t bp_htype;
523 /** 02 - Hardware address length. */
524 uint8_t bp_hlen;
525 /** 03 - Gateway hops. */
526 uint8_t bp_hops;
527 /** 04 - Transaction ID. */
528 uint32_t bp_xid;
529 /** 08 - Seconds since boot started. */
530 uint16_t bp_secs;
531 /** 0a - Unused (BOOTP) / Flags (DHCP) (RTNET_DHCP_FLAGS_*). */
532 uint16_t bp_flags;
533 /** 0c - Client IPv4 address. */
534 RTNETADDRIPV4 bp_ciaddr;
535 /** 10 - Your IPv4 address. */
536 RTNETADDRIPV4 bp_yiaddr;
537 /** 14 - Server IPv4 address. */
538 RTNETADDRIPV4 bp_siaddr;
539 /** 18 - Gateway IPv4 address. */
540 RTNETADDRIPV4 bp_giaddr;
541 /** 1c - Client hardware address. */
542 union
543 {
544 uint8_t au8[16];
545 RTMAC Mac;
546 } bp_chaddr;
547 /** 2c - Server name. */
548 uint8_t bp_sname[64];
549 /** 6c - File name / more DHCP options. */
550 uint8_t bp_file[128];
551 /** ec - Vendor specific area (BOOTP) / Options (DHCP).
552 * @remark This is really 312 bytes in the DHCP version. */
553 union
554 {
555 uint8_t au8[128];
556 struct DHCP
557 {
558 /** ec - The DHCP cookie (RTNET_DHCP_COOKIE). */
559 uint32_t dhcp_cookie;
560 /** f0 - The DHCP options. */
561 uint8_t dhcp_opts[124];
562 } Dhcp;
563 } bp_vend;
564
565} RTNETBOOTP;
566#pragma pack()
567AssertCompileSize(RTNETBOOTP, 0xec + 128);
568/** Pointer to a BOOTP / DHCP packet. */
569typedef RTNETBOOTP *PRTNETBOOTP;
570/** Pointer to a const BOOTP / DHCP packet. */
571typedef RTNETBOOTP const *PCRTNETBOOTP;
572
573/** Minimum BOOTP packet length. For quick validation, no standard thing really. */
574#define RTNETBOOTP_MIN_LEN 0xec
575/** Minimum DHCP packet length. For quick validation, no standard thing really. */
576#define RTNETBOOTP_DHCP_MIN_LEN 0xf1
577
578/** The normal size of the a DHCP packet (i.e. a RTNETBOOTP).
579 * Same as RTNET_DHCP_OPT_SIZE, just expressed differently. */
580#define RTNET_DHCP_NORMAL_SIZE (0xec + 4 + RTNET_DHCP_OPT_SIZE)
581/** The normal size of RTNETBOOTP::bp_vend::Dhcp::dhcp_opts. */
582#define RTNET_DHCP_OPT_SIZE (312 - 4)
583
584/** @name BOOTP packet opcode values
585 * @{ */
586#define RTNETBOOTP_OP_REQUEST 1
587#define RTNETBOOTP_OP_REPLY 2
588/** @} */
589
590/** @name DHCP flags (RTNETBOOTP::bp_flags)
591 * @{ */
592#define RTNET_DHCP_FLAGS_NO_BROADCAST UINT16_C(0x8000) /** @todo check test!!! */
593/** @} */
594
595/** The DHCP cookie (network endian). */
596#define RTNET_DHCP_COOKIE UINT32_C(0x63825363)
597
598/**
599 * An IPv4 DHCP option header.
600 */
601typedef struct RTNETDHCPOPT
602{
603 /** 00 - The DHCP option. */
604 uint8_t dhcp_opt;
605 /** 01 - The data length (excluding this header). */
606 uint8_t dhcp_len;
607 /* 02 - The option data follows here, optional and of variable length. */
608} RTNETDHCPOPT;
609AssertCompileSize(RTNETDHCPOPT, 2);
610/** Pointer to a DHCP option header. */
611typedef RTNETDHCPOPT *PRTNETDHCPOPT;
612/** Pointer to a const DHCP option header. */
613typedef RTNETDHCPOPT const *PCRTNETDHCPOPT;
614
615/** @name DHCP options
616 * @{ */
617/** 1 byte padding, this has no dhcp_len field. */
618#define RTNET_DHCP_OPT_PAD 0
619
620/** The subnet mask. */
621#define RTNET_DHCP_OPT_SUBNET_MASK 1
622/** The time offset. */
623#define RTNET_DHCP_OPT_TIME_OFFSET 2
624/** The routers for the subnet. */
625#define RTNET_DHCP_OPT_ROUTERS 3
626/** Domain Name Server. */
627#define RTNET_DHCP_OPT_DNS 6
628/** Host name. */
629#define RTNET_DHCP_OPT_HOST_NAME 12
630/** Domain name. */
631#define RTNET_DHCP_OPT_DOMAIN_NAME 15
632
633/** The requested address. */
634#define RTNET_DHCP_OPT_REQ_ADDR 50
635/** The lease time in seconds. */
636#define RTNET_DHCP_OPT_LEASE_TIME 51
637/** Option overload.
638 * Indicates that the bp_file and/or bp_sname holds contains DHCP options. */
639#define RTNET_DHCP_OPT_OPTION_OVERLOAD 52
640/** Have a 8-bit message type value as data, see RTNET_DHCP_MT_*. */
641#define RTNET_DHCP_OPT_MSG_TYPE 53
642/** Server ID. */
643#define RTNET_DHCP_OPT_SERVER_ID 54
644/** Parameter request list. */
645#define RTNET_DHCP_OPT_PARAM_REQ_LIST 55
646/** The maximum DHCP message size a client is willing to accept. */
647#define RTNET_DHCP_OPT_MAX_DHCP_MSG_SIZE 57
648/** Client ID. */
649#define RTNET_DHCP_OPT_CLIENT_ID 61
650/** TFTP server name. */
651#define RTNET_DHCP_OPT_TFTP_SERVER_NAME 66
652/** Bootfile name. */
653#define RTNET_DHCP_OPT_BOOTFILE_NAME 67
654
655/** Marks the end of the DHCP options, this has no dhcp_len field. */
656#define RTNET_DHCP_OPT_END 255
657/** @} */
658
659/** @name DHCP Option overload flags (option 52)
660 * @{ */
661#define RTNET_DHCP_OPTION_OVERLOAD_FILE 1
662#define RTNET_DHCP_OPTION_OVERLOAD_SNAME 2
663#define RTNET_DHCP_OPTION_OVERLOAD_MASK 3
664/** @} */
665
666/** @name DHCP Message Types (option 53)
667 * @{ */
668#define RTNET_DHCP_MT_DISCOVER 1
669#define RTNET_DHCP_MT_OFFER 2
670#define RTNET_DHCP_MT_REQUEST 3
671#define RTNET_DHCP_MT_DECLINE 4
672#define RTNET_DHCP_MT_ACK 5
673#define RTNET_DHCP_MT_NAC 6
674#define RTNET_DHCP_MT_RELEASE 7
675#define RTNET_DHCP_MT_INFORM 8
676/** @} */
677
678/** @name DHCP Flags
679 * @{ */
680#define RTNET_DHCP_FLAG_BROADCAST 0x8000
681/** @} */
682
683RTDECL(bool) RTNetIPv4IsDHCPValid(PCRTNETUDP pUdpHdr, PCRTNETBOOTP pDhcp, size_t cbDhcp, uint8_t *pMsgType);
684
685
686/**
687 * IPv4 DHCP packet.
688 * @deprecated Use RTNETBOOTP.
689 */
690#pragma pack(1)
691typedef struct RTNETDHCP
692{
693 /** 00 - The packet opcode. */
694 uint8_t Op;
695 /** Hardware address type. */
696 uint8_t HType;
697 /** Hardware address length. */
698 uint8_t HLen;
699 uint8_t Hops;
700 uint32_t XID;
701 uint16_t Secs;
702 uint16_t Flags;
703 /** Client IPv4 address. */
704 RTNETADDRIPV4 CIAddr;
705 /** Your IPv4 address. */
706 RTNETADDRIPV4 YIAddr;
707 /** Server IPv4 address. */
708 RTNETADDRIPV4 SIAddr;
709 /** Gateway IPv4 address. */
710 RTNETADDRIPV4 GIAddr;
711 /** Client hardware address. */
712 uint8_t CHAddr[16];
713 /** Server name. */
714 uint8_t SName[64];
715 uint8_t File[128];
716 uint8_t abMagic[4];
717 uint8_t DhcpOpt;
718 uint8_t DhcpLen; /* 1 */
719 uint8_t DhcpReq;
720 uint8_t abOptions[57];
721} RTNETDHCP;
722#pragma pack()
723/** @todo AssertCompileSize(RTNETDHCP, ); */
724/** Pointer to a DHCP packet. */
725typedef RTNETDHCP *PRTNETDHCP;
726/** Pointer to a const DHCP packet. */
727typedef RTNETDHCP const *PCRTNETDHCP;
728
729
730/**
731 * TCP packet.
732 */
733#pragma pack(1)
734typedef struct RTNETTCP
735{
736 /** 00 - The source port. */
737 uint16_t th_sport;
738 /** 02 - The destination port. */
739 uint16_t th_dport;
740 /** 04 - The sequence number. */
741 uint32_t th_seq;
742 /** 08 - The acknowledgement number. */
743 uint32_t th_ack;
744#ifdef RT_BIG_ENDIAN
745 unsigned int th_win : 16;
746 unsigned int th_flags : 8;
747 unsigned int th_off : 4;
748 unsigned int th_x2 : 4;
749#else
750 /** 0c:0 - Reserved. */
751 unsigned int th_x2 : 4;
752 /** 0c:4 - The data offset given as a dword count from the start of this header. */
753 unsigned int th_off : 4;
754 /** 0d - flags. */
755 unsigned int th_flags : 8;
756 /** 0e - The window. */
757 unsigned int th_win : 16;
758#endif
759 /** 10 - The checksum of the pseudo header, the TCP header and the data. */
760 uint16_t th_sum;
761 /** 12 - The urgent pointer. */
762 uint16_t th_urp;
763 /* (options follows here and then the data (aka text).) */
764} RTNETTCP;
765#pragma pack()
766AssertCompileSize(RTNETTCP, 20);
767/** Pointer to a TCP packet. */
768typedef RTNETTCP *PRTNETTCP;
769/** Pointer to a const TCP packet. */
770typedef RTNETTCP const *PCRTNETTCP;
771
772/** The minimum TCP header length (in bytes). (RTNETTCP::th_off * 4) */
773#define RTNETTCP_MIN_LEN (20)
774
775/** @name TCP flags (RTNETTCP::th_flags)
776 * @{ */
777#define RTNETTCP_F_FIN 0x01
778#define RTNETTCP_F_SYN 0x02
779#define RTNETTCP_F_RST 0x04
780#define RTNETTCP_F_PSH 0x08
781#define RTNETTCP_F_ACK 0x10
782#define RTNETTCP_F_URG 0x20
783#define RTNETTCP_F_ECE 0x40
784#define RTNETTCP_F_CWR 0x80
785/** @} */
786
787RTDECL(uint16_t) RTNetTCPChecksum(uint32_t u32Sum, PCRTNETTCP pTcpHdr, void const *pvData, size_t cbData);
788RTDECL(uint32_t) RTNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t u32Sum);
789RTDECL(uint16_t) RTNetIPv4TCPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, void const *pvData);
790RTDECL(bool) RTNetIPv4IsTCPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, size_t cbPktMax);
791RTDECL(bool) RTNetIPv4IsTCPValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, void const *pvData,
792 size_t cbPktMax, bool fChecksum);
793
794
795/**
796 * IPv4 ICMP packet header.
797 */
798#pragma pack(1)
799typedef struct RTNETICMPV4HDR
800{
801 /** 00 - The ICMP message type. */
802 uint8_t icmp_type;
803 /** 01 - Type specific code that further qualifies the message. */
804 uint8_t icmp_code;
805 /** 02 - Checksum of the ICMP message. */
806 uint16_t icmp_cksum;
807} RTNETICMPV4HDR;
808#pragma pack()
809AssertCompileSize(RTNETICMPV4HDR, 4);
810/** Pointer to an ICMP packet header. */
811typedef RTNETICMPV4HDR *PRTNETICMPV4HDR;
812/** Pointer to a const ICMP packet header. */
813typedef RTNETICMPV4HDR const *PCRTNETICMPV4HDR;
814
815/** @name ICMP (v4) message types.
816 * @{ */
817#define RTNETICMPV4_TYPE_ECHO_REPLY 0
818#define RTNETICMPV4_TYPE_ECHO_REQUEST 8
819#define RTNETICMPV4_TYPE_TRACEROUTE 30
820/** @} */
821
822/**
823 * IPv4 ICMP ECHO Reply & Request packet.
824 */
825#pragma pack(1)
826typedef struct RTNETICMPV4ECHO
827{
828 /** 00 - The ICMP header. */
829 RTNETICMPV4HDR Hdr;
830 /** 04 - The identifier to help the requestor match up the reply.
831 * Can be 0. Typically fixed value. */
832 uint16_t icmp_id;
833 /** 06 - The sequence number to help the requestor match up the reply.
834 * Can be 0. Typically incrementing between requests. */
835 uint16_t icmp_seq;
836 /** 08 - Variable length data that is to be returned unmodified in the reply. */
837 uint8_t icmp_data[1];
838} RTNETICMPV4ECHO;
839#pragma pack()
840AssertCompileSize(RTNETICMPV4ECHO, 9);
841/** Pointer to an ICMP ECHO packet. */
842typedef RTNETICMPV4ECHO *PRTNETICMPV4ECHO;
843/** Pointer to a const ICMP ECHO packet. */
844typedef RTNETICMPV4ECHO const *PCRTNETICMPV4ECHO;
845
846/**
847 * IPv4 ICMP TRACEROUTE packet.
848 * This is an reply to an IP packet with the traceroute option set.
849 */
850#pragma pack(1)
851typedef struct RTNETICMPV4TRACEROUTE
852{
853 /** 00 - The ICMP header. */
854 RTNETICMPV4HDR Hdr;
855 /** 04 - Identifier copied from the traceroute option's ID number. */
856 uint16_t icmp_id;
857 /** 06 - Unused. (Possibly an icmp_seq?) */
858 uint16_t icmp_void;
859 /** 08 - Outbound hop count. From the IP packet causing this message. */
860 uint16_t icmp_ohc;
861 /** 0a - Return hop count. From the IP packet causing this message. */
862 uint16_t icmp_rhc;
863 /** 0c - Output link speed, 0 if not known. */
864 uint32_t icmp_speed;
865 /** 10 - Output link MTU, 0 if not known. */
866 uint32_t icmp_mtu;
867} RTNETICMPV4TRACEROUTE;
868#pragma pack()
869AssertCompileSize(RTNETICMPV4TRACEROUTE, 20);
870/** Pointer to an ICMP TRACEROUTE packet. */
871typedef RTNETICMPV4TRACEROUTE *PRTNETICMPV4TRACEROUTE;
872/** Pointer to a const ICMP TRACEROUTE packet. */
873typedef RTNETICMPV4TRACEROUTE const *PCRTNETICMPV4TRACEROUTE;
874
875/** @todo add more ICMPv4 as needed. */
876
877/**
878 * IPv4 ICMP union packet.
879 */
880typedef union RTNETICMPV4
881{
882 RTNETICMPV4HDR Hdr;
883 RTNETICMPV4ECHO Echo;
884 RTNETICMPV4TRACEROUTE Traceroute;
885} RTNETICMPV4;
886/** Pointer to an ICMP union packet. */
887typedef RTNETICMPV4 *PRTNETICMPV4;
888/** Pointer to a const ICMP union packet. */
889typedef RTNETICMPV4 const *PCRTNETICMPV4;
890
891
892/**
893 * IPv6 ICMP packet header.
894 */
895#pragma pack(1)
896typedef struct RTNETICMPV6HDR
897{
898 /** 00 - The ICMPv6 message type. */
899 uint8_t icmp6_type;
900 /** 01 - Type specific code that further qualifies the message. */
901 uint8_t icmp6_code;
902 /** 02 - Checksum of the ICMPv6 message. */
903 uint16_t icmp6_cksum;
904} RTNETICMPV6HDR;
905#pragma pack()
906AssertCompileSize(RTNETICMPV6HDR, 4);
907/** Pointer to an ICMPv6 packet header. */
908typedef RTNETICMPV6HDR *PRTNETICMPV6HDR;
909/** Pointer to a const ICMP packet header. */
910typedef RTNETICMPV6HDR const *PCRTNETICMPV6HDR;
911
912#define RTNETIPV6_PROT_ICMPV6 (58)
913
914/** @name Internet Control Message Protocol version 6 (ICMPv6) message types.
915 * @{ */
916#define RTNETIPV6_ICMP_TYPE_RS 133
917#define RTNETIPV6_ICMP_TYPE_RA 134
918#define RTNETIPV6_ICMP_TYPE_NS 135
919#define RTNETIPV6_ICMP_TYPE_NA 136
920#define RTNETIPV6_ICMP_TYPE_RDR 137
921/** @} */
922
923/** @name Neighbor Discovery option types
924 * @{ */
925#define RTNETIPV6_ICMP_ND_SLLA_OPT (1)
926#define RTNETIPV6_ICMP_ND_TLLA_OPT (2)
927/** @} */
928
929/** ICMPv6 ND Source/Target Link Layer Address option */
930#pragma pack(1)
931typedef struct RTNETNDP_LLA_OPT
932{
933 uint8_t type;
934 uint8_t len;
935 RTMAC lla;
936} RTNETNDP_LLA_OPT;
937#pragma pack()
938
939AssertCompileSize(RTNETNDP_LLA_OPT, 1+1+6);
940
941typedef RTNETNDP_LLA_OPT *PRTNETNDP_LLA_OPT;
942typedef RTNETNDP_LLA_OPT const *PCRTNETNDP_LLA_OPT;
943
944/** ICMPv6 ND Neighbor Sollicitation */
945#pragma pack(1)
946typedef struct RTNETNDP
947{
948 /** 00 - The ICMPv6 header. */
949 RTNETICMPV6HDR Hdr;
950 /** 04 - reserved */
951 uint32_t reserved;
952 /** 08 - target address */
953 RTNETADDRIPV6 target_address;
954} RTNETNDP;
955#pragma pack()
956AssertCompileSize(RTNETNDP, 4+4+16);
957/** Pointer to a NDP ND packet. */
958typedef RTNETNDP *PRTNETNDP;
959/** Pointer to a const NDP NS packet. */
960typedef RTNETNDP const *PCRTNETNDP;
961
962
963/**
964 * Ethernet ARP header.
965 */
966#pragma pack(1)
967typedef struct RTNETARPHDR
968{
969 /** The hardware type. */
970 uint16_t ar_htype;
971 /** The protocol type (ethertype). */
972 uint16_t ar_ptype;
973 /** The hardware address length. */
974 uint8_t ar_hlen;
975 /** The protocol address length. */
976 uint8_t ar_plen;
977 /** The operation. */
978 uint16_t ar_oper;
979} RTNETARPHDR;
980#pragma pack()
981AssertCompileSize(RTNETARPHDR, 8);
982/** Pointer to an ethernet ARP header. */
983typedef RTNETARPHDR *PRTNETARPHDR;
984/** Pointer to a const ethernet ARP header. */
985typedef RTNETARPHDR const *PCRTNETARPHDR;
986
987/** ARP hardware type - ethernet. */
988#define RTNET_ARP_ETHER UINT16_C(1)
989
990/** @name ARP operations
991 * @{ */
992#define RTNET_ARPOP_REQUEST UINT16_C(1) /**< Request hardware address given a protocol address (ARP). */
993#define RTNET_ARPOP_REPLY UINT16_C(2)
994#define RTNET_ARPOP_REVREQUEST UINT16_C(3) /**< Request protocol address given a hardware address (RARP). */
995#define RTNET_ARPOP_REVREPLY UINT16_C(4)
996#define RTNET_ARPOP_INVREQUEST UINT16_C(8) /**< Inverse ARP. */
997#define RTNET_ARPOP_INVREPLY UINT16_C(9)
998/** Check if an ARP operation is a request or not. */
999#define RTNET_ARPOP_IS_REQUEST(Op) ((Op) & 1)
1000/** Check if an ARP operation is a reply or not. */
1001#define RTNET_ARPOP_IS_REPLY(Op) (!RTNET_ARPOP_IS_REQUEST(Op))
1002/** @} */
1003
1004
1005/**
1006 * Ethernet IPv4 + 6-byte MAC ARP request packet.
1007 */
1008#pragma pack(1)
1009typedef struct RTNETARPIPV4
1010{
1011 /** ARP header. */
1012 RTNETARPHDR Hdr;
1013 /** The sender hardware address. */
1014 RTMAC ar_sha;
1015 /** The sender protocol address. */
1016 RTNETADDRIPV4 ar_spa;
1017 /** The target hardware address. */
1018 RTMAC ar_tha;
1019 /** The target protocol address. */
1020 RTNETADDRIPV4 ar_tpa;
1021} RTNETARPIPV4;
1022#pragma pack()
1023AssertCompileSize(RTNETARPIPV4, 8+6+4+6+4);
1024/** Pointer to an ethernet IPv4+MAC ARP request packet. */
1025typedef RTNETARPIPV4 *PRTNETARPIPV4;
1026/** Pointer to a const ethernet IPv4+MAC ARP request packet. */
1027typedef RTNETARPIPV4 const *PCRTNETARPIPV4;
1028
1029
1030/** @} */
1031
1032RT_C_DECLS_END
1033
1034#endif /* !IPRT_INCLUDED_net_h */
1035
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