1 |
583 |
jeremybenn |
/*
|
2 |
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
6 |
|
|
* are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
9 |
|
|
* this list of conditions and the following disclaimer.
|
10 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
11 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
12 |
|
|
* and/or other materials provided with the distribution.
|
13 |
|
|
* 3. The name of the author may not be used to endorse or promote products
|
14 |
|
|
* derived from this software without specific prior written permission.
|
15 |
|
|
*
|
16 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
17 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
18 |
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
19 |
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
20 |
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
21 |
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
22 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
23 |
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
24 |
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
25 |
|
|
* OF SUCH DAMAGE.
|
26 |
|
|
*
|
27 |
|
|
* This file is part of the lwIP TCP/IP stack.
|
28 |
|
|
*
|
29 |
|
|
* Author: Adam Dunkels <adam@sics.se>
|
30 |
|
|
*
|
31 |
|
|
*/
|
32 |
|
|
#ifndef __LWIP_IP_ADDR_H__
|
33 |
|
|
#define __LWIP_IP_ADDR_H__
|
34 |
|
|
|
35 |
|
|
#include "lwip/arch.h"
|
36 |
|
|
|
37 |
|
|
#ifdef PACK_STRUCT_USE_INCLUDES
|
38 |
|
|
# include "arch/bpstruct.h"
|
39 |
|
|
#endif
|
40 |
|
|
PACK_STRUCT_BEGIN
|
41 |
|
|
struct ip_addr {
|
42 |
|
|
PACK_STRUCT_FIELD(u32_t addr);
|
43 |
|
|
} PACK_STRUCT_STRUCT;
|
44 |
|
|
PACK_STRUCT_END
|
45 |
|
|
#ifdef PACK_STRUCT_USE_INCLUDES
|
46 |
|
|
# include "arch/epstruct.h"
|
47 |
|
|
#endif
|
48 |
|
|
|
49 |
|
|
#ifdef PACK_STRUCT_USE_INCLUDES
|
50 |
|
|
# include "arch/bpstruct.h"
|
51 |
|
|
#endif
|
52 |
|
|
PACK_STRUCT_BEGIN
|
53 |
|
|
struct ip_addr2 {
|
54 |
|
|
PACK_STRUCT_FIELD(u16_t addrw[2]);
|
55 |
|
|
} PACK_STRUCT_STRUCT;
|
56 |
|
|
PACK_STRUCT_END
|
57 |
|
|
#ifdef PACK_STRUCT_USE_INCLUDES
|
58 |
|
|
# include "arch/epstruct.h"
|
59 |
|
|
#endif
|
60 |
|
|
|
61 |
|
|
/* For compatibility with BSD code */
|
62 |
|
|
struct in_addr {
|
63 |
|
|
u32_t s_addr;
|
64 |
|
|
};
|
65 |
|
|
|
66 |
|
|
struct netif;
|
67 |
|
|
|
68 |
|
|
extern const struct ip_addr ip_addr_any;
|
69 |
|
|
extern const struct ip_addr ip_addr_broadcast;
|
70 |
|
|
|
71 |
|
|
/** IP_ADDR_ can be used as a fixed IP address
|
72 |
|
|
* for the wildcard and the broadcast address
|
73 |
|
|
*/
|
74 |
|
|
#define IP_ADDR_ANY ((struct ip_addr *)&ip_addr_any)
|
75 |
|
|
#define IP_ADDR_BROADCAST ((struct ip_addr *)&ip_addr_broadcast)
|
76 |
|
|
|
77 |
|
|
#define INADDR_NONE ((u32_t) 0xffffffff) /* 255.255.255.255 */
|
78 |
|
|
#define INADDR_LOOPBACK ((u32_t) 0x7f000001) /* 127.0.0.1 */
|
79 |
|
|
|
80 |
|
|
/* Definitions of the bits in an Internet address integer.
|
81 |
|
|
|
82 |
|
|
On subnets, host and network parts are found according to
|
83 |
|
|
the subnet mask, not these masks. */
|
84 |
|
|
|
85 |
|
|
#define IN_CLASSA(a) ((((u32_t)(a)) & 0x80000000) == 0)
|
86 |
|
|
#define IN_CLASSA_NET 0xff000000
|
87 |
|
|
#define IN_CLASSA_NSHIFT 24
|
88 |
|
|
#define IN_CLASSA_HOST (0xffffffff & ~IN_CLASSA_NET)
|
89 |
|
|
#define IN_CLASSA_MAX 128
|
90 |
|
|
|
91 |
|
|
#define IN_CLASSB(a) ((((u32_t)(a)) & 0xc0000000) == 0x80000000)
|
92 |
|
|
#define IN_CLASSB_NET 0xffff0000
|
93 |
|
|
#define IN_CLASSB_NSHIFT 16
|
94 |
|
|
#define IN_CLASSB_HOST (0xffffffff & ~IN_CLASSB_NET)
|
95 |
|
|
#define IN_CLASSB_MAX 65536
|
96 |
|
|
|
97 |
|
|
#define IN_CLASSC(a) ((((u32_t)(a)) & 0xe0000000) == 0xc0000000)
|
98 |
|
|
#define IN_CLASSC_NET 0xffffff00
|
99 |
|
|
#define IN_CLASSC_NSHIFT 8
|
100 |
|
|
#define IN_CLASSC_HOST (0xffffffff & ~IN_CLASSC_NET)
|
101 |
|
|
|
102 |
|
|
#define IN_CLASSD(a) (((u32_t)(a) & 0xf0000000) == 0xe0000000)
|
103 |
|
|
#define IN_CLASSD_NET 0xf0000000 /* These ones aren't really */
|
104 |
|
|
#define IN_CLASSD_NSHIFT 28 /* net and host fields, but */
|
105 |
|
|
#define IN_CLASSD_HOST 0x0fffffff /* routing needn't know. */
|
106 |
|
|
#define IN_MULTICAST(a) IN_CLASSD(a)
|
107 |
|
|
|
108 |
|
|
#define IN_EXPERIMENTAL(a) (((u32_t)(a) & 0xf0000000) == 0xf0000000)
|
109 |
|
|
#define IN_BADCLASS(a) (((u32_t)(a) & 0xf0000000) == 0xf0000000)
|
110 |
|
|
|
111 |
|
|
#define IN_LOOPBACKNET 127 /* official! */
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
#define IP4_ADDR(ipaddr, a,b,c,d) (ipaddr)->addr = htonl(((u32_t)(a & 0xff) << 24) | ((u32_t)(b & 0xff) << 16) | \
|
115 |
|
|
((u32_t)(c & 0xff) << 8) | (u32_t)(d & 0xff))
|
116 |
|
|
|
117 |
|
|
#define ip_addr_set(dest, src) (dest)->addr = \
|
118 |
|
|
((src) == NULL? 0:\
|
119 |
|
|
(src)->addr)
|
120 |
|
|
/**
|
121 |
|
|
* Determine if two address are on the same network.
|
122 |
|
|
*
|
123 |
|
|
* @arg addr1 IP address 1
|
124 |
|
|
* @arg addr2 IP address 2
|
125 |
|
|
* @arg mask network identifier mask
|
126 |
|
|
* @return !0 if the network identifiers of both address match
|
127 |
|
|
*/
|
128 |
|
|
#define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
|
129 |
|
|
(mask)->addr) == \
|
130 |
|
|
((addr2)->addr & \
|
131 |
|
|
(mask)->addr))
|
132 |
|
|
#define ip_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
|
133 |
|
|
|
134 |
|
|
#define ip_addr_isany(addr1) ((addr1) == NULL || (addr1)->addr == 0)
|
135 |
|
|
|
136 |
|
|
u8_t ip_addr_isbroadcast(struct ip_addr *, struct netif *);
|
137 |
|
|
|
138 |
|
|
#define ip_addr_ismulticast(addr1) (((addr1)->addr & ntohl(0xf0000000)) == ntohl(0xe0000000))
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
#define ip_addr_debug_print(debug, ipaddr) LWIP_DEBUGF(debug, ("%"U16_F".%"U16_F".%"U16_F".%"U16_F, \
|
142 |
|
|
ipaddr?(u16_t)(ntohl((ipaddr)->addr) >> 24) & 0xff:0, \
|
143 |
|
|
ipaddr?(u16_t)(ntohl((ipaddr)->addr) >> 16) & 0xff:0, \
|
144 |
|
|
ipaddr?(u16_t)(ntohl((ipaddr)->addr) >> 8) & 0xff:0, \
|
145 |
|
|
ipaddr?(u16_t)ntohl((ipaddr)->addr) & 0xff:0U))
|
146 |
|
|
|
147 |
|
|
/* cast to unsigned int, as it is used as argument to printf functions
|
148 |
|
|
* which expect integer arguments. CSi: use cc.h formatters (conversion chars)! */
|
149 |
|
|
#define ip4_addr1(ipaddr) ((u16_t)(ntohl((ipaddr)->addr) >> 24) & 0xff)
|
150 |
|
|
#define ip4_addr2(ipaddr) ((u16_t)(ntohl((ipaddr)->addr) >> 16) & 0xff)
|
151 |
|
|
#define ip4_addr3(ipaddr) ((u16_t)(ntohl((ipaddr)->addr) >> 8) & 0xff)
|
152 |
|
|
#define ip4_addr4(ipaddr) ((u16_t)(ntohl((ipaddr)->addr)) & 0xff)
|
153 |
|
|
#endif /* __LWIP_IP_ADDR_H__ */
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
|