Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Tue Nov 09 20:54:15 2010 +0000
Revision:
0:ac1725ba162c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
segundo 0:ac1725ba162c 1 /*
segundo 0:ac1725ba162c 2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
segundo 0:ac1725ba162c 3 * All rights reserved.
segundo 0:ac1725ba162c 4 *
segundo 0:ac1725ba162c 5 * Redistribution and use in source and binary forms, with or without modification,
segundo 0:ac1725ba162c 6 * are permitted provided that the following conditions are met:
segundo 0:ac1725ba162c 7 *
segundo 0:ac1725ba162c 8 * 1. Redistributions of source code must retain the above copyright notice,
segundo 0:ac1725ba162c 9 * this list of conditions and the following disclaimer.
segundo 0:ac1725ba162c 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
segundo 0:ac1725ba162c 11 * this list of conditions and the following disclaimer in the documentation
segundo 0:ac1725ba162c 12 * and/or other materials provided with the distribution.
segundo 0:ac1725ba162c 13 * 3. The name of the author may not be used to endorse or promote products
segundo 0:ac1725ba162c 14 * derived from this software without specific prior written permission.
segundo 0:ac1725ba162c 15 *
segundo 0:ac1725ba162c 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
segundo 0:ac1725ba162c 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
segundo 0:ac1725ba162c 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
segundo 0:ac1725ba162c 19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
segundo 0:ac1725ba162c 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
segundo 0:ac1725ba162c 21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
segundo 0:ac1725ba162c 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
segundo 0:ac1725ba162c 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
segundo 0:ac1725ba162c 24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
segundo 0:ac1725ba162c 25 * OF SUCH DAMAGE.
segundo 0:ac1725ba162c 26 *
segundo 0:ac1725ba162c 27 * This file is part of the lwIP TCP/IP stack.
segundo 0:ac1725ba162c 28 *
segundo 0:ac1725ba162c 29 * Author: Adam Dunkels <adam@sics.se>
segundo 0:ac1725ba162c 30 *
segundo 0:ac1725ba162c 31 */
segundo 0:ac1725ba162c 32 #ifndef __LWIP_IP_ADDR_H__
segundo 0:ac1725ba162c 33 #define __LWIP_IP_ADDR_H__
segundo 0:ac1725ba162c 34
segundo 0:ac1725ba162c 35 #include "lwip/opt.h"
segundo 0:ac1725ba162c 36 #include "lwip/def.h"
segundo 0:ac1725ba162c 37
segundo 0:ac1725ba162c 38 #ifdef __cplusplus
segundo 0:ac1725ba162c 39 extern "C" {
segundo 0:ac1725ba162c 40 #endif
segundo 0:ac1725ba162c 41
segundo 0:ac1725ba162c 42 /* This is the aligned version of ip_addr_t,
segundo 0:ac1725ba162c 43 used as local variable, on the stack, etc. */
segundo 0:ac1725ba162c 44 struct ip_addr {
segundo 0:ac1725ba162c 45 u32_t addr;
segundo 0:ac1725ba162c 46 };
segundo 0:ac1725ba162c 47
segundo 0:ac1725ba162c 48 /* This is the packed version of ip_addr_t,
segundo 0:ac1725ba162c 49 used in network headers that are itself packed */
segundo 0:ac1725ba162c 50 #ifdef PACK_STRUCT_USE_INCLUDES
segundo 0:ac1725ba162c 51 # include "arch/bpstruct.h"
segundo 0:ac1725ba162c 52 #endif
segundo 0:ac1725ba162c 53 PACK_STRUCT_BEGIN
segundo 0:ac1725ba162c 54 struct ip_addr_packed {
segundo 0:ac1725ba162c 55 PACK_STRUCT_FIELD(u32_t addr);
segundo 0:ac1725ba162c 56 } PACK_STRUCT_STRUCT;
segundo 0:ac1725ba162c 57 PACK_STRUCT_END
segundo 0:ac1725ba162c 58 #ifdef PACK_STRUCT_USE_INCLUDES
segundo 0:ac1725ba162c 59 # include "arch/epstruct.h"
segundo 0:ac1725ba162c 60 #endif
segundo 0:ac1725ba162c 61
segundo 0:ac1725ba162c 62 /** ip_addr_t uses a struct for convenience only, so that the same defines can
segundo 0:ac1725ba162c 63 * operate both on ip_addr_t as well as on ip_addr_p_t. */
segundo 0:ac1725ba162c 64 typedef struct ip_addr ip_addr_t;
segundo 0:ac1725ba162c 65 typedef struct ip_addr_packed ip_addr_p_t;
segundo 0:ac1725ba162c 66
segundo 0:ac1725ba162c 67 /*
segundo 0:ac1725ba162c 68 * struct ipaddr2 is used in the definition of the ARP packet format in
segundo 0:ac1725ba162c 69 * order to support compilers that don't have structure packing.
segundo 0:ac1725ba162c 70 */
segundo 0:ac1725ba162c 71 #ifdef PACK_STRUCT_USE_INCLUDES
segundo 0:ac1725ba162c 72 # include "arch/bpstruct.h"
segundo 0:ac1725ba162c 73 #endif
segundo 0:ac1725ba162c 74 PACK_STRUCT_BEGIN
segundo 0:ac1725ba162c 75 struct ip_addr2 {
segundo 0:ac1725ba162c 76 PACK_STRUCT_FIELD(u16_t addrw[2]);
segundo 0:ac1725ba162c 77 } PACK_STRUCT_STRUCT;
segundo 0:ac1725ba162c 78 PACK_STRUCT_END
segundo 0:ac1725ba162c 79 #ifdef PACK_STRUCT_USE_INCLUDES
segundo 0:ac1725ba162c 80 # include "arch/epstruct.h"
segundo 0:ac1725ba162c 81 #endif
segundo 0:ac1725ba162c 82
segundo 0:ac1725ba162c 83 /* Forward declaration to not include netif.h */
segundo 0:ac1725ba162c 84 struct netif;
segundo 0:ac1725ba162c 85
segundo 0:ac1725ba162c 86 extern const ip_addr_t ip_addr_any;
segundo 0:ac1725ba162c 87 extern const ip_addr_t ip_addr_broadcast;
segundo 0:ac1725ba162c 88
segundo 0:ac1725ba162c 89 /** IP_ADDR_ can be used as a fixed IP address
segundo 0:ac1725ba162c 90 * for the wildcard and the broadcast address
segundo 0:ac1725ba162c 91 */
segundo 0:ac1725ba162c 92 #define IP_ADDR_ANY ((ip_addr_t *)&ip_addr_any)
segundo 0:ac1725ba162c 93 #define IP_ADDR_BROADCAST ((ip_addr_t *)&ip_addr_broadcast)
segundo 0:ac1725ba162c 94
segundo 0:ac1725ba162c 95 /** 255.255.255.255 */
segundo 0:ac1725ba162c 96 #define IPADDR_NONE ((u32_t)0xffffffffUL)
segundo 0:ac1725ba162c 97 /** 127.0.0.1 */
segundo 0:ac1725ba162c 98 #define IPADDR_LOOPBACK ((u32_t)0x7f000001UL)
segundo 0:ac1725ba162c 99 /** 0.0.0.0 */
segundo 0:ac1725ba162c 100 #define IPADDR_ANY ((u32_t)0x00000000UL)
segundo 0:ac1725ba162c 101 /** 255.255.255.255 */
segundo 0:ac1725ba162c 102 #define IPADDR_BROADCAST ((u32_t)0xffffffffUL)
segundo 0:ac1725ba162c 103
segundo 0:ac1725ba162c 104 /* Definitions of the bits in an Internet address integer.
segundo 0:ac1725ba162c 105
segundo 0:ac1725ba162c 106 On subnets, host and network parts are found according to
segundo 0:ac1725ba162c 107 the subnet mask, not these masks. */
segundo 0:ac1725ba162c 108 #define IP_CLASSA(a) ((((u32_t)(a)) & 0x80000000UL) == 0)
segundo 0:ac1725ba162c 109 #define IP_CLASSA_NET 0xff000000
segundo 0:ac1725ba162c 110 #define IP_CLASSA_NSHIFT 24
segundo 0:ac1725ba162c 111 #define IP_CLASSA_HOST (0xffffffff & ~IP_CLASSA_NET)
segundo 0:ac1725ba162c 112 #define IP_CLASSA_MAX 128
segundo 0:ac1725ba162c 113
segundo 0:ac1725ba162c 114 #define IP_CLASSB(a) ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
segundo 0:ac1725ba162c 115 #define IP_CLASSB_NET 0xffff0000
segundo 0:ac1725ba162c 116 #define IP_CLASSB_NSHIFT 16
segundo 0:ac1725ba162c 117 #define IP_CLASSB_HOST (0xffffffff & ~IP_CLASSB_NET)
segundo 0:ac1725ba162c 118 #define IP_CLASSB_MAX 65536
segundo 0:ac1725ba162c 119
segundo 0:ac1725ba162c 120 #define IP_CLASSC(a) ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
segundo 0:ac1725ba162c 121 #define IP_CLASSC_NET 0xffffff00
segundo 0:ac1725ba162c 122 #define IP_CLASSC_NSHIFT 8
segundo 0:ac1725ba162c 123 #define IP_CLASSC_HOST (0xffffffff & ~IP_CLASSC_NET)
segundo 0:ac1725ba162c 124
segundo 0:ac1725ba162c 125 #define IP_CLASSD(a) (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
segundo 0:ac1725ba162c 126 #define IP_CLASSD_NET 0xf0000000 /* These ones aren't really */
segundo 0:ac1725ba162c 127 #define IP_CLASSD_NSHIFT 28 /* net and host fields, but */
segundo 0:ac1725ba162c 128 #define IP_CLASSD_HOST 0x0fffffff /* routing needn't know. */
segundo 0:ac1725ba162c 129 #define IP_MULTICAST(a) IP_CLASSD(a)
segundo 0:ac1725ba162c 130
segundo 0:ac1725ba162c 131 #define IP_EXPERIMENTAL(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
segundo 0:ac1725ba162c 132 #define IP_BADCLASS(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
segundo 0:ac1725ba162c 133
segundo 0:ac1725ba162c 134 #define IP_LOOPBACKNET 127 /* official! */
segundo 0:ac1725ba162c 135
segundo 0:ac1725ba162c 136
segundo 0:ac1725ba162c 137 #if BYTE_ORDER == BIG_ENDIAN
segundo 0:ac1725ba162c 138 /** Set an IP address given by the four byte-parts */
segundo 0:ac1725ba162c 139 #define IP4_ADDR(ipaddr, a,b,c,d) \
segundo 0:ac1725ba162c 140 (ipaddr)->addr = ((u32_t)((a) & 0xff) << 24) | \
segundo 0:ac1725ba162c 141 ((u32_t)((b) & 0xff) << 16) | \
segundo 0:ac1725ba162c 142 ((u32_t)((c) & 0xff) << 8) | \
segundo 0:ac1725ba162c 143 (u32_t)((d) & 0xff)
segundo 0:ac1725ba162c 144 #else
segundo 0:ac1725ba162c 145 /** Set an IP address given by the four byte-parts.
segundo 0:ac1725ba162c 146 Little-endian version that prevents the use of htonl. */
segundo 0:ac1725ba162c 147 #define IP4_ADDR(ipaddr, a,b,c,d) \
segundo 0:ac1725ba162c 148 (ipaddr)->addr = ((u32_t)((d) & 0xff) << 24) | \
segundo 0:ac1725ba162c 149 ((u32_t)((c) & 0xff) << 16) | \
segundo 0:ac1725ba162c 150 ((u32_t)((b) & 0xff) << 8) | \
segundo 0:ac1725ba162c 151 (u32_t)((a) & 0xff)
segundo 0:ac1725ba162c 152 #endif
segundo 0:ac1725ba162c 153
segundo 0:ac1725ba162c 154 /** MEMCPY-like copying of IP addresses where addresses are known to be
segundo 0:ac1725ba162c 155 * 16-bit-aligned if the port is correctly configured (so a port could define
segundo 0:ac1725ba162c 156 * this to copying 2 u16_t's) - no NULL-pointer-checking needed. */
segundo 0:ac1725ba162c 157 #ifndef IPADDR2_COPY
segundo 0:ac1725ba162c 158 #define IPADDR2_COPY(dest, src) SMEMCPY(dest, src, sizeof(ip_addr_t))
segundo 0:ac1725ba162c 159 #endif
segundo 0:ac1725ba162c 160
segundo 0:ac1725ba162c 161 /** Copy IP address - faster than ip_addr_set: no NULL check */
segundo 0:ac1725ba162c 162 #define ip_addr_copy(dest, src) ((dest).addr = (src).addr)
segundo 0:ac1725ba162c 163 /** Safely copy one IP address to another (src may be NULL) */
segundo 0:ac1725ba162c 164 #define ip_addr_set(dest, src) ((dest)->addr = \
segundo 0:ac1725ba162c 165 ((src) == NULL ? 0 : \
segundo 0:ac1725ba162c 166 (src)->addr))
segundo 0:ac1725ba162c 167 /** Set complete address to zero */
segundo 0:ac1725ba162c 168 #define ip_addr_set_zero(ipaddr) ((ipaddr)->addr = 0)
segundo 0:ac1725ba162c 169 /** Set address to IPADDR_ANY (no need for htonl()) */
segundo 0:ac1725ba162c 170 #define ip_addr_set_any(ipaddr) ((ipaddr)->addr = IPADDR_ANY)
segundo 0:ac1725ba162c 171 /** Set address to loopback address */
segundo 0:ac1725ba162c 172 #define ip_addr_set_loopback(ipaddr) ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
segundo 0:ac1725ba162c 173 /** Safely copy one IP address to another and change byte order
segundo 0:ac1725ba162c 174 * from host- to network-order. */
segundo 0:ac1725ba162c 175 #define ip_addr_set_hton(dest, src) ((dest)->addr = \
segundo 0:ac1725ba162c 176 ((src) == NULL ? 0:\
segundo 0:ac1725ba162c 177 htonl((src)->addr)))
segundo 0:ac1725ba162c 178 /** IPv4 only: set the IP address given as an u32_t */
segundo 0:ac1725ba162c 179 #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
segundo 0:ac1725ba162c 180 /** IPv4 only: get the IP address as an u32_t */
segundo 0:ac1725ba162c 181 #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
segundo 0:ac1725ba162c 182
segundo 0:ac1725ba162c 183 /** Get the network address by combining host address with netmask */
segundo 0:ac1725ba162c 184 #define ip_addr_get_network(target, host, netmask) ((target)->addr = ((host)->addr) & ((netmask)->addr))
segundo 0:ac1725ba162c 185
segundo 0:ac1725ba162c 186 /**
segundo 0:ac1725ba162c 187 * Determine if two address are on the same network.
segundo 0:ac1725ba162c 188 *
segundo 0:ac1725ba162c 189 * @arg addr1 IP address 1
segundo 0:ac1725ba162c 190 * @arg addr2 IP address 2
segundo 0:ac1725ba162c 191 * @arg mask network identifier mask
segundo 0:ac1725ba162c 192 * @return !0 if the network identifiers of both address match
segundo 0:ac1725ba162c 193 */
segundo 0:ac1725ba162c 194 #define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
segundo 0:ac1725ba162c 195 (mask)->addr) == \
segundo 0:ac1725ba162c 196 ((addr2)->addr & \
segundo 0:ac1725ba162c 197 (mask)->addr))
segundo 0:ac1725ba162c 198 #define ip_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
segundo 0:ac1725ba162c 199
segundo 0:ac1725ba162c 200 #define ip_addr_isany(addr1) ((addr1) == NULL || (addr1)->addr == IPADDR_ANY)
segundo 0:ac1725ba162c 201
segundo 0:ac1725ba162c 202 #define ip_addr_isbroadcast(ipaddr, netif) ip4_addr_isbroadcast((ipaddr)->addr, (netif))
segundo 0:ac1725ba162c 203 u8_t ip4_addr_isbroadcast(u32_t addr, const struct netif *netif);
segundo 0:ac1725ba162c 204
segundo 0:ac1725ba162c 205 #define ip_addr_netmask_valid(netmask) ip4_addr_netmask_valid((netmask)->addr)
segundo 0:ac1725ba162c 206 u8_t ip4_addr_netmask_valid(u32_t netmask);
segundo 0:ac1725ba162c 207
segundo 0:ac1725ba162c 208 #define ip_addr_ismulticast(addr1) (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))
segundo 0:ac1725ba162c 209
segundo 0:ac1725ba162c 210 #define ip_addr_islinklocal(addr1) (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))
segundo 0:ac1725ba162c 211
segundo 0:ac1725ba162c 212 #define ip_addr_debug_print(debug, ipaddr) \
segundo 0:ac1725ba162c 213 LWIP_DEBUGF(debug, ("%"U16_F".%"U16_F".%"U16_F".%"U16_F, \
segundo 0:ac1725ba162c 214 ipaddr != NULL ? ip4_addr1_16(ipaddr) : 0, \
segundo 0:ac1725ba162c 215 ipaddr != NULL ? ip4_addr2_16(ipaddr) : 0, \
segundo 0:ac1725ba162c 216 ipaddr != NULL ? ip4_addr3_16(ipaddr) : 0, \
segundo 0:ac1725ba162c 217 ipaddr != NULL ? ip4_addr4_16(ipaddr) : 0))
segundo 0:ac1725ba162c 218
segundo 0:ac1725ba162c 219 /* Get one byte from the 4-byte address */
segundo 0:ac1725ba162c 220 #define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0])
segundo 0:ac1725ba162c 221 #define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1])
segundo 0:ac1725ba162c 222 #define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2])
segundo 0:ac1725ba162c 223 #define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3])
segundo 0:ac1725ba162c 224 /* These are cast to u16_t, with the intent that they are often arguments
segundo 0:ac1725ba162c 225 * to printf using the U16_F format from cc.h. */
segundo 0:ac1725ba162c 226 #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr))
segundo 0:ac1725ba162c 227 #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr))
segundo 0:ac1725ba162c 228 #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr))
segundo 0:ac1725ba162c 229 #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr))
segundo 0:ac1725ba162c 230
segundo 0:ac1725ba162c 231 /** For backwards compatibility */
segundo 0:ac1725ba162c 232 #define ip_ntoa(ipaddr) ipaddr_ntoa(ipaddr)
segundo 0:ac1725ba162c 233
segundo 0:ac1725ba162c 234 u32_t ipaddr_addr(const char *cp);
segundo 0:ac1725ba162c 235 int ipaddr_aton(const char *cp, ip_addr_t *addr);
segundo 0:ac1725ba162c 236 /** returns ptr to static buffer; not reentrant! */
segundo 0:ac1725ba162c 237 char *ipaddr_ntoa(const ip_addr_t *addr);
segundo 0:ac1725ba162c 238 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen);
segundo 0:ac1725ba162c 239
segundo 0:ac1725ba162c 240 #ifdef __cplusplus
segundo 0:ac1725ba162c 241 }
segundo 0:ac1725ba162c 242 #endif
segundo 0:ac1725ba162c 243
segundo 0:ac1725ba162c 244 #endif /* __LWIP_IP_ADDR_H__ */