VirtualBox

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

Last change on this file since 66098 was 65579, checked in by vboxsync, 8 years ago

IPRT: RTNetMaskToPrefixIPv4 and RTNetPrefixToMaskIPv4.

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