NetBurner 3.5.0
PDF Version
 
ip.h
Go to the documentation of this file.
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
17#ifndef _NB_IP_H
18#define _NB_IP_H
19
20// NB Definitions
21#include <predef.h>
22
23#ifndef NB_NET_TYPES_H
24#include "nettypes.h"
25#endif
26
27#ifndef _NB_BUFFERS_H
28#include "buffers.h"
29#endif
31// #define IP_DEBUG ( 0 )
32
33/* Definitions */
34
38#define IP_VERSION_MASK (0xF0)
39#define IP_VERSION_IPv4 (0x40)
40#define IP_VERSION_IPv6 (0x60)
46#define IP_HEADER_LENGTH_MASK (0x0F)
47#define IP_HEADER_LENGTH_NO_OPTIONS (0x05)
53#define IP_FLAGS_MASK (0xE000)
54#define IP_FRAGMENT_OFFSET_MASK (0x1FFF)
60#define IP_PROTOCOL_ICMP (0x01)
61#define IP_PROTOCOL_IGMP (0x02)
62#define IP_PROTOCOL_INTERNET (0x04)
63#define IP_PROTOCOL_TCP (0x06)
64#define IP_PROTOCOL_UDP (0x11)
70#define ICMP_TYPE_ECHO_REPLY (0)
71#define ICMP_CODE_ECHO_REPLY (0)
72#define ICMP_TYPE_DEST_UNREACHABLE (3)
73#define ICMP_CODE_PORT_UNREACHABLE (3)
74#define ICMP_TYPE_SOURCE_QUENCH (4)
75#define ICMP_CODE_SOURCE_QUENCH (0)
76#define ICMP_TYPE_REDIRECT_MESSAGE (5)
77#define ICMP_TYPE_ECHO_REQUEST (8)
78#define ICMP_CODE_ECHO_REQUEST (0)
84#define IANA_ECHO_PORT (7)
85#define IANA_DISCARD_PORT (9)
86#define IANA_SSH_PORT (22)
87#define IANA_TELNET_PORT (23)
88//#define IANA_TFTP_PORT (69) /**< @b */
89#define IANA_NETBIOS_NAME_SERVICE_PORT (137)
92#define ICMP_PING_DATA_SIZE (32)
94/* Data Structures */
95
102typedef struct _IpHeaderIPv4
103{
105 uint8_t diffServNEcn;
106 beuint16_t totalLength;
107 beuint16_t identification;
109 uint8_t timeToLive;
110 uint8_t protocol;
112 beuint16_t headerChecksum;
113 beuint32_t sourceAddress;
116} __attribute__((packed)) IpHeaderIPv4;
117
119typedef struct
120{
121 uint8_t bVerHdrLen;
122 uint8_t bTos;
123 beuint16_t wLength;
124 beuint16_t wpktId;
125 beuint16_t wFlags_Frag;
126 uint8_t bTTL;
127 uint8_t proto;
131 uint16_t hCSum;
134 uint8_t DATA[];
135} __attribute__((packed)) IPPKT;
136
137typedef IPPKT *PIPPKT;
138
140typedef struct
141{
142 beuint16_t srcPort;
143 beuint16_t dstPort;
144 beuint16_t UdpLen;
148 uint16_t UdpCSum;
149 uint8_t DATA[];
150} __attribute__((packed)) UDPPKT;
151typedef UDPPKT *PUDPPKT;
152
163typedef struct
164{
165 beuint16_t proto;
166 beuint16_t len;
171typedef PsudeoHeader *PPSUH;
172
177extern uint8_t bTTL_Default;
179extern uint16_t wArpLifetime;
181extern BOOL bQuietStart;
184/* "C" Internal Functions */
185
187void IpProcessEthernetPacket(PoolPtr poolPtr, uint16_t packetSizeInBytes);
188
189extern "C"
190{
191 uint16_t GetSum(puint16_t addr, uint16_t count);
192 uint16_t GetSum20(puint32_t addr);
193 uint16_t GetSumHdr(PsudeoHeader &hdr, puint16_t addr, uint16_t count);
194}
195
196BOOL IsMyIp4(IPADDR4 ip, int ifc = -1);
200IPADDR4 GetSrcIpwIf4(int ifn, const IPADDR4 dst);
202#ifdef IPV6
203BOOL IsMyIp6(const IPADDR &ip, int ifc = -1);
205inline BOOL IsMyIp(const IPADDR &ip, int ifc = -1)
206{
207 return IsMyIp6(ip, ifc);
208};
209
210IPADDR GetSrcIp6(const IPADDR &dst);
212inline IPADDR GetSrcIp(const IPADDR &dst)
213{
214 return GetSrcIp6(dst);
215};
216
217IPADDR GetSrcIp6wIf(int ifn, const IPADDR &dst);
219inline IPADDR GetSrcIpwIf(int ifn, const IPADDR &dst)
220{
221 return GetSrcIp6wIf(ifn, dst);
222};
223
224#else
226inline BOOL IsMyIp(IPADDR4 ip, int ifc = -1)
227{
228 return IsMyIp4(ip, ifc);
229};
231inline IPADDR4 GetSrcIp(IPADDR4 dst)
232{
233 return GetSrcIp4(dst);
234};
236inline IPADDR4 GetSrcIpwIf(int ifn, IPADDR4 dst)
237{
238 return GetSrcIpwIf4(ifn, dst);
239};
240
241#endif
242
243/* "C++" Internal Functions */
244
245void SendICMPError(PoolPtr pBadPacket, uint8_t type, uint8_t code);
247void FixHeaderAndSend(PoolPtr p, PIPPKT pIp);
249void FixHeaderAndSendViaInterface(PoolPtr p, PIPPKT pIp, int Interface);
260inline PIPPKT GetIpPkt(PoolPtr p)
261{
262 return (p == NULL) ? NULL : (PIPPKT)(p->pData + 14);
263};
264
266inline PIPPKT GetIpPkt(PEFRAME pFrame)
267{
268 return (PIPPKT)(pFrame->pData);
269};
270
272inline PIPPKT GetIpPkt(PVLEFRAME pFrame)
273{
274 return (PIPPKT)(pFrame->pData);
275};
276
279{
280 if (pIp == NULL) { return NULL; }
281 if (pIp->bVerHdrLen == 0x45) { return (PUDPPKT)pIp->DATA; }
282 return (PUDPPKT)(pIp->DATA + (((pIp->bVerHdrLen & 0XF) * 4) - 20));
283}
284
287/* "C++" Runtime Library Functions */
288
297
298/*
299 *******************************************************************************
300 *
301 * UPD fragment packet pointer for user defined function.
302 *
303 * Parameters:
304 * pp - Packet pointer from network buffer pool
305 *
306 * Return:
307 * None
308 *
309 * Notes:
310 * This is only called for packets with the fragmentation flag set
311 *
312 *******************************************************************************
313 */
314typedef void(FragmentProcessFunction)(PoolPtr pp);
315extern FragmentProcessFunction *pFragFunc;
316
317/*
318 *******************************************************************************
319 *
320 * Promiscuous packet pointer for user defined function.
321 *
322 * Parameters:
323 * pp - Packet pointer from network buffer pool
324 *
325 * Return:
326 * None
327 *
328 * Notes:
329 * This is only called for packets that fail the IP address == my address.
330 * This function must free the buffers it receives!
331 *
332 *******************************************************************************
333 */
334typedef void(PromisciousPacketFunc)(PoolPtr pp);
335extern PromisciousPacketFunc *pPromisciousPacketFunc;
336
337/*
338 *******************************************************************************
339 *
340 * UDP task packet function pointer
341 *
342 * Parameters:
343 * pkt - UDP packet
344 * ifn - Interface number
345 *
346 * Return:
347 * None
348 *
349 * Notes:
350 * None
351 *
352 *******************************************************************************
353 */
354class UDPPacket;
355typedef int(TaskPacketFunc)(UDPPacket &pkt, int ifn);
356extern TaskPacketFunc *pTaskPacketFunc;
357
358/*
359 *******************************************************************************
360 *
361 * Display buffer for diagnostic purposes
362 *
363 * Parameters:
364 * rp - Packet pointer from network buffer pool
365 *
366 * Return:
367 * None
368 *
369 * Notes:
370 * Uses iprintf
371 *
372 *******************************************************************************
373 */
374void ShowIPBuffer(PoolPtr rp);
375
376/*
377 *******************************************************************************
378 *
379 * Sends a "ping" packet and waits the specified timeout for a response.
380 *
381 * Ping uses the primary network interface (usually the Ethernet)
382 * PingViaInterface allows the selection of network interface
383 * SendPing does not wait for a response.
384 *
385 * Parameters:
386 * to - Destination address
387 * id - Identifier
388 * seq - Sequence number
389 * maxwaitticks - Wait time in ticks
390 *
391 * Return:
392 * >=0 Ticks wait for the response
393 * -1 Timeout
394 * -2 Error other than timeout.
395 * Notes:
396 * Sends ICMP echo request with specified identifier, sequence number and
397 * 32 bytes of ASCII data ('a' (0x61) through 0xFF ).
398 * Expects echo reply with same data
399 *
400 *******************************************************************************
401 */
402int Ping4(IPADDR4 to, uint16_t id, uint16_t seq, uint16_t maxwaitticks);
403int Ping4ViaInterface(IPADDR4 to, uint16_t id, uint16_t seq, uint16_t wait, int interface);
404
405#ifdef IPV6
406int Ping6(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t maxwaitticks, int size = 32);
407int Ping6ViaInterface(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t wait, int interface, int size = 32);
408inline int Ping(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t maxwaitticks, int size = 32)
409{
410 return Ping6(to, id, seq, maxwaitticks, size);
411};
412inline int PingViaInterface(const IPADDR &to, uint16_t id, uint16_t seq, uint16_t wait, int interface, int size = 32)
413{
414 return Ping6ViaInterface(to, id, seq, wait, interface, size);
415};
416#else
417inline int Ping(IPADDR4 to, uint16_t id, uint16_t seq, uint16_t maxwaitticks)
418{
419 return Ping4(to, id, seq, maxwaitticks);
420};
421inline int PingViaInterface(IPADDR4 to, uint16_t id, uint16_t seq, uint16_t wait, int interface)
422{
423 return Ping4ViaInterface(to, id, seq, wait, interface);
424};
425#endif
426
427BOOL IsLocal4(IPADDR4 ip, int ifc);
428int GetProperInterface4(IPADDR4 dst);
429
430#if defined MULTIHOME || defined AUTOIP
431int AddInterface(IPADDR4 addr, IPADDR4 mask, IPADDR4 gateway, int root_if);
432int GetMultiHomeInterface(IPADDR4 ipa, int norgif);
433int GetLocalIPInterface4(IPADDR4 ipa);
434#ifdef IPV6
435int GetLocalIPInterface6(const IPADDR &ipa);
436inline int GetLocalIPInterface(const IPADDR &ipa)
437{
438 return GetLocalIPInterface6(ipa);
439};
440#else
441inline int GetLocalIPInterface(IPADDR4 ipa)
442{
443 return GetLocalIPInterface4(ipa);
444};
445#endif
446#endif
447
448 /*
449 *******************************************************************************
450 *
451 * Deprecated and Backward Compatibility Runtime Library Functions
452 *
453 *******************************************************************************
454 */
455
456#ifndef IPV6
457#ifndef IPV4ONLY
458#error Got to pick an IP version
459#endif
460#endif
461
462// Get a random port value for random return port values
463uint16_t GetEphemeralPort();
464
465#endif /* #ifndef _NB_IP_H */
466
467
NetBurner Buffers API.
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:208
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
UDP Packet Class.
Definition udp.h:81
void FixHeaderAndSend(PoolPtr p, PIPPKT pIp)
Complete header and send on primary network interface.
IPADDR GetSrcIp(const IPADDR &dst)
Get source IP address for this destination.
Definition ip.h:212
BOOL bQuietStart
Quiet start.
void FixHeaderAndSendViaInterface(PoolPtr p, PIPPKT pIp, int Interface)
Complete header and send on an interface.
int AddInterface(IPADDR4 addr, IPADDR4 mask, IPADDR4 gateway, int root_if)
Add an interface with an interface number.
Definition multihome.h:151
struct _IpHeaderIPv4 IpHeaderIPv4
IP Header (IPv4)
void InitializeStack()
Initializes the IP stack.
PIPPKT GetIpPkt(PoolPtr p)
Get IP Packet pointer from network buffer pool buffer.
Definition ip.h:260
uint16_t wArpLifetime
ARP lifetime in seconds.
void SendICMPError(PoolPtr pBadPacket, uint8_t type, uint8_t code)
Send ICMP error.
IPADDR GetSrcIpwIf(int ifn, const IPADDR &dst)
Get source IP address for an interface.
Definition ip.h:219
IPADDR4 GetSrcIp4(IPADDR4 dst)
Get source IP address for this destination.
uint16_t GetSum20(puint32_t addr)
Checksum.
BOOL IsMyIp(const IPADDR &ip, int ifc=-1)
Is this my IP address?
Definition ip.h:205
IPADDR4 GetSrcIpwIf4(int ifn, const IPADDR4 dst)
uint16_t GetSumHdr(PsudeoHeader &hdr, puint16_t addr, uint16_t count)
Checksum using pseudo-header.
PUDPPKT GetUdpPkt(PIPPKT pIp)
Get UPD packet pointer from IP packet pointer.
Definition ip.h:278
void IpProcessEthernetPacket(PoolPtr poolPtr, uint16_t packetSizeInBytes)
IP packet insertion for processing.
uint16_t GetSum(puint16_t addr, uint16_t count)
Checksum.
BOOL IsMyIp4(IPADDR4 ip, int ifc=-1)
Is this my IP address?
uint8_t bTTL_Default
Default number of hops (time to live)
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.
IP Header (IPv4)
Definition ip.h:103
uint8_t diffServNEcn
Service and congestion related IPPKT::bTos.
Definition ip.h:105
uint8_t versionNLength
Version and header length nibbles IPPKT::bVerHdrLen.
Definition ip.h:104
beuint16_t identification
Fragment identification IPPKT::wpktId.
Definition ip.h:107
beuint32_t destinationAddress
IP destination address 4 8-bit octets IPPKT::ipDst.
Definition ip.h:114
beuint16_t totalLength
Datagram size including IP header IPPKT::wLength.
Definition ip.h:106
beuint32_t sourceAddress
IP address of source 4 8-bit octets IPPKT::ipSrc.
Definition ip.h:113
beuint16_t headerChecksum
16-bit one's complement of the one's complement sum of all 16-bit words in the header IPPKT::hCSum
Definition ip.h:112
uint8_t protocol
IANA protocol number IPPKT::proto.
Definition ip.h:110
beuint16_t flagsNFragmentOffset
Flags and fragment offset fields IPPKT::wFlags_Frag.
Definition ip.h:108
uint8_t timeToLive
Usually hop count IPPKT::bTTL.
Definition ip.h:109
Internal IPv4 Header.
Definition ip.h:120
uint16_t hCSum
Checksum Do not make a big endian var, checksumming is treated as native endian.
Definition ip.h:131
IPADDR4 ipSrc
Definition ip.h:132
uint8_t proto
Definition ip.h:127
beuint16_t wLength
Definition ip.h:123
uint8_t bTTL
Definition ip.h:126
uint8_t bTos
Definition ip.h:122
beuint16_t wpktId
Definition ip.h:124
uint8_t bVerHdrLen
Definition ip.h:121
IPADDR4 ipDst
Definition ip.h:133
beuint16_t wFlags_Frag
Definition ip.h:125
UDP IPv4 Pseudo-header segment (bits 0 through 95)
Definition ip.h:164
IPADDR4 dstip
Destination IP address.
Definition ip.h:168
beuint16_t proto
Protocol (UDP 0x11)
Definition ip.h:165
beuint16_t len
UDP header and data.
Definition ip.h:166
IPADDR4 srcip
Source IP address.
Definition ip.h:167
Internal UDP Header.
Definition ip.h:141
beuint16_t dstPort
Destination port number.
Definition ip.h:143
beuint16_t srcPort
Source port number.
Definition ip.h:142
uint16_t UdpCSum
Checksum (zero in IPv4) Do not make big endian, see IPPKT.
Definition ip.h:148
beuint16_t UdpLen
Datagram length including header.
Definition ip.h:144