VirtualBox

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

Last change on this file since 49907 was 49326, checked in by vboxsync, 11 years ago

IPRT: exposes RTNetStrToMAC (resolves todo in misc/getopt.cpp).

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