dev

Dependents:   EthernetInterface

Fork of lwip by mbed official

Committer:
yihui
Date:
Mon Mar 02 08:19:15 2015 +0000
Revision:
19:6c91d95ef874
Parent:
0:51ac1d130fd4
minor changes

Who changed what in which revision?

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