hattori&ide

Dependencies:   mbed

Committer:
hattori_atsushi
Date:
Sun Dec 18 08:16:01 2022 +0000
Revision:
0:f77369cabd75
hattori

Who changed what in which revision?

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