Host library for controlling a WiConnect enabled Wi-Fi module.

Dependents:   wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ip_addr.h Source File

ip_addr.h

00001 /*
00002  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  *    this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright notice,
00011  *    this list of conditions and the following disclaimer in the documentation
00012  *    and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00017  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00018  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00019  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00021  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00024  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00025  * OF SUCH DAMAGE.
00026  *
00027  * This file is part of the lwIP TCP/IP stack.
00028  *
00029  * Author: Adam Dunkels <adam@sics.se>
00030  *
00031  */
00032 #ifndef __LWIP_IP_ADDR_H__
00033 #define __LWIP_IP_ADDR_H__
00034 
00035 #include "opt.h"
00036 
00037 #ifdef __cplusplus
00038 extern "C" {
00039 #endif
00040 
00041 /* This is the aligned version of ip_addr_t,
00042    used as local variable, on the stack, etc. */
00043 struct ip_addr {
00044   u32_t addr;
00045 };
00046 
00047 /* This is the packed version of ip_addr_t,
00048    used in network headers that are itself packed */
00049 #ifdef PACK_STRUCT_USE_INCLUDES
00050 #  include "arch/bpstruct.h"
00051 #endif
00052 PACK_STRUCT_BEGIN
00053 struct ip_addr_packed {
00054   PACK_STRUCT_FIELD(u32_t addr);
00055 } PACK_STRUCT_STRUCT;
00056 PACK_STRUCT_END
00057 #ifdef PACK_STRUCT_USE_INCLUDES
00058 #  include "arch/epstruct.h"
00059 #endif
00060 
00061 /** ip_addr_t uses a struct for convenience only, so that the same defines can
00062  * operate both on ip_addr_t as well as on ip_addr_p_t. */
00063 typedef struct ip_addr ip_addr_t;
00064 typedef struct ip_addr_packed ip_addr_p_t;
00065 
00066 /*
00067  * struct ipaddr2 is used in the definition of the ARP packet format in
00068  * order to support compilers that don't have structure packing.
00069  */
00070 #ifdef PACK_STRUCT_USE_INCLUDES
00071 #  include "arch/bpstruct.h"
00072 #endif
00073 PACK_STRUCT_BEGIN
00074 struct ip_addr2 {
00075   PACK_STRUCT_FIELD(u16_t addrw[2]);
00076 } PACK_STRUCT_STRUCT;
00077 PACK_STRUCT_END
00078 #ifdef PACK_STRUCT_USE_INCLUDES
00079 #  include "arch/epstruct.h"
00080 #endif
00081 
00082 /* Forward declaration to not include netif.h */
00083 struct netif;
00084 
00085 extern const ip_addr_t ip_addr_any;
00086 extern const ip_addr_t ip_addr_broadcast;
00087 
00088 /** IP_ADDR_ can be used as a fixed IP address
00089  *  for the wildcard and the broadcast address
00090  */
00091 #define IP_ADDR_ANY         ((ip_addr_t *)&ip_addr_any)
00092 #define IP_ADDR_BROADCAST   ((ip_addr_t *)&ip_addr_broadcast)
00093 
00094 /** 255.255.255.255 */
00095 #define IPADDR_NONE         ((u32_t)0xffffffffUL)
00096 /** 127.0.0.1 */
00097 #define IPADDR_LOOPBACK     ((u32_t)0x7f000001UL)
00098 /** 0.0.0.0 */
00099 #define IPADDR_ANY          ((u32_t)0x00000000UL)
00100 /** 255.255.255.255 */
00101 #define IPADDR_BROADCAST    ((u32_t)0xffffffffUL)
00102 
00103 /* Definitions of the bits in an Internet address integer.
00104 
00105    On subnets, host and network parts are found according to
00106    the subnet mask, not these masks.  */
00107 #define IP_CLASSA(a)        ((((u32_t)(a)) & 0x80000000UL) == 0)
00108 #define IP_CLASSA_NET       0xff000000
00109 #define IP_CLASSA_NSHIFT    24
00110 #define IP_CLASSA_HOST      (0xffffffff & ~IP_CLASSA_NET)
00111 #define IP_CLASSA_MAX       128
00112 
00113 #define IP_CLASSB(a)        ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
00114 #define IP_CLASSB_NET       0xffff0000
00115 #define IP_CLASSB_NSHIFT    16
00116 #define IP_CLASSB_HOST      (0xffffffff & ~IP_CLASSB_NET)
00117 #define IP_CLASSB_MAX       65536
00118 
00119 #define IP_CLASSC(a)        ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
00120 #define IP_CLASSC_NET       0xffffff00
00121 #define IP_CLASSC_NSHIFT    8
00122 #define IP_CLASSC_HOST      (0xffffffff & ~IP_CLASSC_NET)
00123 
00124 #define IP_CLASSD(a)        (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
00125 #define IP_CLASSD_NET       0xf0000000          /* These ones aren't really */
00126 #define IP_CLASSD_NSHIFT    28                  /*   net and host fields, but */
00127 #define IP_CLASSD_HOST      0x0fffffff          /*   routing needn't know. */
00128 #define IP_MULTICAST(a)     IP_CLASSD(a)
00129 
00130 #define IP_EXPERIMENTAL(a)  (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
00131 #define IP_BADCLASS(a)      (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
00132 
00133 #define IP_LOOPBACKNET      127                 /* official! */
00134 
00135 
00136 #if BYTE_ORDER == BIG_ENDIAN
00137 /** Set an IP address given by the four byte-parts */
00138 #define IP4_ADDR(ipaddr, a,b,c,d) \
00139         (ipaddr)->addr = ((u32_t)((a) & 0xff) << 24) | \
00140                          ((u32_t)((b) & 0xff) << 16) | \
00141                          ((u32_t)((c) & 0xff) << 8)  | \
00142                           (u32_t)((d) & 0xff)
00143 #else
00144 /** Set an IP address given by the four byte-parts.
00145     Little-endian version that prevents the use of htonl. */
00146 #define IP4_ADDR(ipaddr, a,b,c,d) \
00147         (ipaddr)->addr = ((u32_t)((d) & 0xff) << 24) | \
00148                          ((u32_t)((c) & 0xff) << 16) | \
00149                          ((u32_t)((b) & 0xff) << 8)  | \
00150                           (u32_t)((a) & 0xff)
00151 #endif
00152 
00153 /** MEMCPY-like copying of IP addresses where addresses are known to be
00154  * 16-bit-aligned if the port is correctly configured (so a port could define
00155  * this to copying 2 u16_t's) - no NULL-pointer-checking needed. */
00156 #ifndef IPADDR2_COPY
00157 #define IPADDR2_COPY(dest, src) SMEMCPY(dest, src, sizeof(ip_addr_t))
00158 #endif
00159 
00160 /** Copy IP address - faster than ip_addr_set: no NULL check */
00161 #define ip_addr_copy(dest, src) ((dest).addr = (src).addr)
00162 /** Safely copy one IP address to another (src may be NULL) */
00163 #define ip_addr_set(dest, src) ((dest)->addr = \
00164                                     ((src) == NULL ? 0 : \
00165                                     (src)->addr))
00166 /** Set complete address to zero */
00167 #define ip_addr_set_zero(ipaddr)      ((ipaddr)->addr = 0)
00168 /** Set address to IPADDR_ANY (no need for htonl()) */
00169 #define ip_addr_set_any(ipaddr)       ((ipaddr)->addr = IPADDR_ANY)
00170 /** Set address to loopback address */
00171 #define ip_addr_set_loopback(ipaddr)  ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
00172 /** Safely copy one IP address to another and change byte order
00173  * from host- to network-order. */
00174 #define ip_addr_set_hton(dest, src) ((dest)->addr = \
00175                                ((src) == NULL ? 0:\
00176                                htonl((src)->addr)))
00177 /** IPv4 only: set the IP address given as an u32_t */
00178 #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
00179 /** IPv4 only: get the IP address as an u32_t */
00180 #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
00181 
00182 /** Get the network address by combining host address with netmask */
00183 #define ip_addr_get_network(target, host, netmask) ((target)->addr = ((host)->addr) & ((netmask)->addr))
00184 
00185 /**
00186  * Determine if two address are on the same network.
00187  *
00188  * @arg addr1 IP address 1
00189  * @arg addr2 IP address 2
00190  * @arg mask network identifier mask
00191  * @return !0 if the network identifiers of both address match
00192  */
00193 #define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
00194                                               (mask)->addr) == \
00195                                              ((addr2)->addr & \
00196                                               (mask)->addr))
00197 #define ip_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
00198 
00199 #define ip_addr_isany(addr1) ((addr1) == NULL || (addr1)->addr == IPADDR_ANY)
00200 
00201 #define ip_addr_isbroadcast(ipaddr, netif) ip4_addr_isbroadcast((ipaddr)->addr, (netif))
00202 u8_t ip4_addr_isbroadcast(u32_t addr, const struct netif *netif);
00203 
00204 #define ip_addr_netmask_valid(netmask) ip4_addr_netmask_valid((netmask)->addr)
00205 u8_t ip4_addr_netmask_valid(u32_t netmask);
00206 
00207 #define ip_addr_ismulticast(addr1) (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))
00208 
00209 #define ip_addr_islinklocal(addr1) (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))
00210 
00211 //#define ip_addr_debug_print(debug, ipaddr) \
00212 //  LWIP_DEBUGF(debug, ("%"U16_F".%"U16_F".%"U16_F".%"U16_F,             \
00213 //                      ipaddr != NULL ? ip4_addr1_16(ipaddr) : 0,       \
00214 //                      ipaddr != NULL ? ip4_addr2_16(ipaddr) : 0,       \
00215 //                      ipaddr != NULL ? ip4_addr3_16(ipaddr) : 0,       \
00216 //                      ipaddr != NULL ? ip4_addr4_16(ipaddr) : 0))
00217 
00218 /* Get one byte from the 4-byte address */
00219 #define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0])
00220 #define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1])
00221 #define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2])
00222 #define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3])
00223 /* These are cast to u16_t, with the intent that they are often arguments
00224  * to printf using the U16_F format from cc.h. */
00225 #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr))
00226 #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr))
00227 #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr))
00228 #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr))
00229 
00230 /** For backwards compatibility */
00231 #define ip_ntoa(ipaddr)  ipaddr_ntoa(ipaddr)
00232 
00233 u32_t ipaddr_addr(const char *cp);
00234 int ipaddr_aton(const char *cp, ip_addr_t *addr);
00235 /** returns ptr to static buffer; not reentrant! */
00236 char *ipaddr_ntoa(const ip_addr_t *addr);
00237 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen);
00238 
00239 #ifdef __cplusplus
00240 }
00241 #endif
00242 
00243 #endif /* __LWIP_IP_ADDR_H__ */