Dependents:   RTnoV3 RTnoV3_LED RTnoV3_Template RTnoV3_ADC ... more

Committer:
sherckuith
Date:
Sat Dec 31 11:25:27 2011 +0000
Revision:
0:479ce5546098
Ethernet

Who changed what in which revision?

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