A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... 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 "lwip/opt.h"
00036 
00037 #include "lwip/inet.h"
00038 
00039 #ifdef __cplusplus
00040 extern "C" {
00041 #endif
00042 
00043 #ifdef PACK_STRUCT_USE_INCLUDES
00044 #  include "arch/bpstruct.h"
00045 #endif
00046 PACK_STRUCT_BEGIN
00047 struct ip_addr {
00048   PACK_STRUCT_FIELD(u32_t addr);
00049 } PACK_STRUCT_STRUCT;
00050 PACK_STRUCT_END
00051 #ifdef PACK_STRUCT_USE_INCLUDES
00052 #  include "arch/epstruct.h"
00053 #endif
00054 
00055 /*
00056  * struct ipaddr2 is used in the definition of the ARP packet format in
00057  * order to support compilers that don't have structure packing.
00058  */
00059 #ifdef PACK_STRUCT_USE_INCLUDES
00060 #  include "arch/bpstruct.h"
00061 #endif
00062 PACK_STRUCT_BEGIN
00063 struct ip_addr2 {
00064   PACK_STRUCT_FIELD(u16_t addrw[2]);
00065 } PACK_STRUCT_STRUCT;
00066 PACK_STRUCT_END
00067 #ifdef PACK_STRUCT_USE_INCLUDES
00068 #  include "arch/epstruct.h"
00069 #endif
00070 
00071 struct netif;
00072 
00073 extern const struct ip_addr ip_addr_any;
00074 extern const struct ip_addr ip_addr_broadcast;
00075 
00076 /** IP_ADDR_ can be used as a fixed IP address
00077  *  for the wildcard and the broadcast address
00078  */
00079 #define IP_ADDR_ANY         ((struct ip_addr *)&ip_addr_any)
00080 #define IP_ADDR_BROADCAST   ((struct ip_addr *)&ip_addr_broadcast)
00081 
00082 /* Definitions of the bits in an Internet address integer.
00083 
00084    On subnets, host and network parts are found according to
00085    the subnet mask, not these masks.  */
00086 
00087 #define IN_CLASSA(a)        ((((u32_t)(a)) & 0x80000000UL) == 0)
00088 #define IN_CLASSA_NET       0xff000000
00089 #define IN_CLASSA_NSHIFT    24
00090 #define IN_CLASSA_HOST      (0xffffffff & ~IN_CLASSA_NET)
00091 #define IN_CLASSA_MAX       128
00092 
00093 #define IN_CLASSB(a)        ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
00094 #define IN_CLASSB_NET       0xffff0000
00095 #define IN_CLASSB_NSHIFT    16
00096 #define IN_CLASSB_HOST      (0xffffffff & ~IN_CLASSB_NET)
00097 #define IN_CLASSB_MAX       65536
00098 
00099 #define IN_CLASSC(a)        ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
00100 #define IN_CLASSC_NET       0xffffff00
00101 #define IN_CLASSC_NSHIFT    8
00102 #define IN_CLASSC_HOST      (0xffffffff & ~IN_CLASSC_NET)
00103 
00104 #define IN_CLASSD(a)        (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
00105 #define IN_CLASSD_NET       0xf0000000          /* These ones aren't really */
00106 #define IN_CLASSD_NSHIFT    28                  /*   net and host fields, but */
00107 #define IN_CLASSD_HOST      0x0fffffff          /*   routing needn't know. */
00108 #define IN_MULTICAST(a)     IN_CLASSD(a)
00109 
00110 #define IN_EXPERIMENTAL(a)  (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
00111 #define IN_BADCLASS(a)      (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
00112 
00113 #define IN_LOOPBACKNET      127                 /* official! */
00114 
00115 #define IP4_ADDR(ipaddr, a,b,c,d) \
00116         (ipaddr)->addr = htonl(((u32_t)((a) & 0xff) << 24) | \
00117                                ((u32_t)((b) & 0xff) << 16) | \
00118                                ((u32_t)((c) & 0xff) << 8) | \
00119                                 (u32_t)((d) & 0xff))
00120 
00121 #define ip_addr_set(dest, src) (dest)->addr = \
00122                                ((src) == NULL? 0:\
00123                                (src)->addr)
00124 /**
00125  * Determine if two address are on the same network.
00126  *
00127  * @arg addr1 IP address 1
00128  * @arg addr2 IP address 2
00129  * @arg mask network identifier mask
00130  * @return !0 if the network identifiers of both address match
00131  */
00132 #define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
00133                                               (mask)->addr) == \
00134                                              ((addr2)->addr & \
00135                                               (mask)->addr))
00136 #define ip_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
00137 
00138 #define ip_addr_isany(addr1) ((addr1) == NULL || (addr1)->addr == 0)
00139 
00140 u8_t ip_addr_isbroadcast(struct ip_addr *, struct netif *);
00141 
00142 #define ip_addr_ismulticast(addr1) (((addr1)->addr & ntohl(0xf0000000UL)) == ntohl(0xe0000000UL))
00143 
00144 #define ip_addr_islinklocal(addr1) (((addr1)->addr & ntohl(0xffff0000UL)) == ntohl(0xa9fe0000UL))
00145 
00146 #define ip_addr_debug_print(debug, ipaddr) \
00147   LWIP_DEBUGF(debug, ("%"U16_F".%"U16_F".%"U16_F".%"U16_F,              \
00148                       ipaddr != NULL ?                                  \
00149                       (u16_t)(ntohl((ipaddr)->addr) >> 24) & 0xff : 0,  \
00150                       ipaddr != NULL ?                                  \
00151                       (u16_t)(ntohl((ipaddr)->addr) >> 16) & 0xff : 0,  \
00152                       ipaddr != NULL ?                                  \
00153                       (u16_t)(ntohl((ipaddr)->addr) >> 8) & 0xff : 0,   \
00154                       ipaddr != NULL ?                                  \
00155                       (u16_t)ntohl((ipaddr)->addr) & 0xff : 0))
00156 
00157 /* These are cast to u16_t, with the intent that they are often arguments
00158  * to printf using the U16_F format from cc.h. */
00159 #define ip4_addr1(ipaddr) ((u16_t)(ntohl((ipaddr)->addr) >> 24) & 0xff)
00160 #define ip4_addr2(ipaddr) ((u16_t)(ntohl((ipaddr)->addr) >> 16) & 0xff)
00161 #define ip4_addr3(ipaddr) ((u16_t)(ntohl((ipaddr)->addr) >> 8) & 0xff)
00162 #define ip4_addr4(ipaddr) ((u16_t)(ntohl((ipaddr)->addr)) & 0xff)
00163 
00164 /**
00165  * Same as inet_ntoa() but takes a struct ip_addr*
00166  */
00167 #define ip_ntoa(addr)  ((addr != NULL) ? inet_ntoa(*((struct in_addr*)(addr))) : "NULL")
00168 
00169 #ifdef __cplusplus
00170 }
00171 #endif
00172 
00173 #endif /* __LWIP_IP_ADDR_H__ */