1/*
2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 *    this list of conditions and the following disclaimer in the documentation
12 *    and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of the lwIP TCP/IP stack.
28 *
29 * Author: Adam Dunkels <adam@sics.se>
30 *
31 */
32#ifndef __LWIP_IP_ADDR_H__
33#define __LWIP_IP_ADDR_H__
34
35#include "lwip/opt.h"
36
37#include <arpa/inet.h>
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43#ifdef PACK_STRUCT_USE_INCLUDES
44#include "arch/bpstruct.h"
45#endif
46    PACK_STRUCT_BEGIN struct ip_addr {
47        PACK_STRUCT_FIELD(u32_t addr);
48    } PACK_STRUCT_STRUCT;
49     PACK_STRUCT_END
50#ifdef PACK_STRUCT_USE_INCLUDES
51#include "arch/epstruct.h"
52#endif
53
54// XXX: In lwIP 1.3.1 this is all the same.
55typedef struct ip_addr ip_addr_t;
56typedef struct ip_addr ip_addr_p_t;
57
58/*
59 * struct ipaddr2 is used in the definition of the ARP packet format in
60 * order to support compilers that don't have structure packing.
61 */
62#ifdef PACK_STRUCT_USE_INCLUDES
63#include "arch/bpstruct.h"
64#endif
65     PACK_STRUCT_BEGIN struct ip_addr2 {
66        PACK_STRUCT_FIELD(u16_t addrw[2]);
67    } PACK_STRUCT_STRUCT;
68     PACK_STRUCT_END
69#ifdef PACK_STRUCT_USE_INCLUDES
70#include "arch/epstruct.h"
71#endif
72      struct netif;
73
74    extern const struct ip_addr ip_addr_any;
75        extern const struct ip_addr ip_addr_broadcast;
76
77/** IP_ADDR_ can be used as a fixed IP address
78 *  for the wildcard and the broadcast address
79 */
80#define IP_ADDR_ANY         ((struct ip_addr *)&ip_addr_any)
81#define IP_ADDR_BROADCAST   ((struct ip_addr *)&ip_addr_broadcast)
82
83/** 255.255.255.255 */
84#define IPADDR_NONE         ((u32_t)0xffffffffUL)
85/** 127.0.0.1 */
86#define IPADDR_LOOPBACK     ((u32_t)0x7f000001UL)
87/** 0.0.0.0 */
88#define IPADDR_ANY          ((u32_t)0x00000000UL)
89/** 255.255.255.255 */
90#define IPADDR_BROADCAST    ((u32_t)0xffffffffUL)
91
92#define IP4_ADDR(ipaddr, a,b,c,d) \
93        (ipaddr)->addr = htonl(((u32_t)((a) & 0xff) << 24) | \
94                               ((u32_t)((b) & 0xff) << 16) | \
95                               ((u32_t)((c) & 0xff) << 8) | \
96                                (u32_t)((d) & 0xff))
97
98#define ip_addr_set(dest, src) (dest)->addr = \
99                               ((src) == NULL? 0:\
100                               (src)->addr)
101
102/** IPv4 only: set the IP address given as an u32_t */
103#define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
104/** IPv4 only: get the IP address as an u32_t */
105#define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
106
107/**
108 * Determine if two address are on the same network.
109 *
110 * @arg addr1 IP address 1
111 * @arg addr2 IP address 2
112 * @arg mask network identifier mask
113 * @return !0 if the network identifiers of both address match
114 */
115#define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
116                                              (mask)->addr) == \
117                                             ((addr2)->addr & \
118                                              (mask)->addr))
119#define ip_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
120
121#define ip_addr_isany(addr1) ((addr1) == NULL || (addr1)->addr == 0)
122
123    u8_t ip_addr_isbroadcast(struct ip_addr *, struct netif *);
124
125#define ip_addr_ismulticast(addr1) (((addr1)->addr & ntohl(0xf0000000UL)) == ntohl(0xe0000000UL))
126
127#define ip_addr_islinklocal(addr1) (((addr1)->addr & ntohl(0xffff0000UL)) == ntohl(0xa9fe0000UL))
128
129#define ip_addr_debug_print(debug, ipaddr) \
130  LWIP_DEBUGF(debug, ("%" U16_F ".%" U16_F ".%" U16_F ".%" U16_F,          \
131                      ipaddr != NULL ?                                  \
132                      (u16_t)(ntohl((ipaddr)->addr) >> 24) & 0xff : 0,  \
133                      ipaddr != NULL ?                                  \
134                      (u16_t)(ntohl((ipaddr)->addr) >> 16) & 0xff : 0,  \
135                      ipaddr != NULL ?                                  \
136                      (u16_t)(ntohl((ipaddr)->addr) >> 8) & 0xff : 0,   \
137                      ipaddr != NULL ?                                  \
138                      (u16_t)ntohl((ipaddr)->addr) & 0xff : 0))
139
140/* These are cast to u16_t, with the intent that they are often arguments
141 * to printf using the U16_F format from cc.h. */
142#define ip4_addr1(ipaddr) ((u16_t)(ntohl((ipaddr)->addr) >> 24) & 0xff)
143#define ip4_addr2(ipaddr) ((u16_t)(ntohl((ipaddr)->addr) >> 16) & 0xff)
144#define ip4_addr3(ipaddr) ((u16_t)(ntohl((ipaddr)->addr) >> 8) & 0xff)
145#define ip4_addr4(ipaddr) ((u16_t)(ntohl((ipaddr)->addr)) & 0xff)
146
147#ifdef __cplusplus
148}
149#endif
150#endif                          /* __LWIP_IP_ADDR_H__ */
151