MeterLogger
Data Structures | Macros | Typedefs | Functions | Variables
ip_addr.h File Reference
#include "lwip/opt.h"
#include "lwip/def.h"
Include dependency graph for ip_addr.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ip_addr
 
struct  ip_addr_packed
 
struct  ip_addr2
 
struct  ip_info
 

Macros

#define IP_ADDR_ANY   ((ip_addr_t *)&ip_addr_any)
 
#define IP_ADDR_BROADCAST   ((ip_addr_t *)&ip_addr_broadcast)
 
#define IPADDR_NONE   ((u32_t)0xffffffffUL)
 
#define IPADDR_LOOPBACK   ((u32_t)0x7f000001UL)
 
#define IPADDR_ANY   ((u32_t)0x00000000UL)
 
#define IPADDR_BROADCAST   ((u32_t)0xffffffffUL)
 
#define IP_CLASSA(a)   ((((u32_t)(a)) & 0x80000000UL) == 0)
 
#define IP_CLASSA_NET   0xff000000
 
#define IP_CLASSA_NSHIFT   24
 
#define IP_CLASSA_HOST   (0xffffffff & ~IP_CLASSA_NET)
 
#define IP_CLASSA_MAX   128
 
#define IP_CLASSB(a)   ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
 
#define IP_CLASSB_NET   0xffff0000
 
#define IP_CLASSB_NSHIFT   16
 
#define IP_CLASSB_HOST   (0xffffffff & ~IP_CLASSB_NET)
 
#define IP_CLASSB_MAX   65536
 
#define IP_CLASSC(a)   ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
 
#define IP_CLASSC_NET   0xffffff00
 
#define IP_CLASSC_NSHIFT   8
 
#define IP_CLASSC_HOST   (0xffffffff & ~IP_CLASSC_NET)
 
#define IP_CLASSD(a)   (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
 
#define IP_CLASSD_NET   0xf0000000 /* These ones aren't really */
 
#define IP_CLASSD_NSHIFT   28 /* net and host fields, but */
 
#define IP_CLASSD_HOST   0x0fffffff /* routing needn't know. */
 
#define IP_MULTICAST(a)   IP_CLASSD(a)
 
#define IP_EXPERIMENTAL(a)   (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
 
#define IP_BADCLASS(a)   (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
 
#define IP_LOOPBACKNET   127 /* official! */
 
#define IP4_ADDR(ipaddr, a, b, c, d)
 
#define IPADDR2_COPY(dest, src)   SMEMCPY(dest, src, sizeof(ip_addr_t))
 
#define ip_addr_copy(dest, src)   ((dest).addr = (src).addr)
 
#define ip_addr_set(dest, src)
 
#define ip_addr_set_zero(ipaddr)   ((ipaddr)->addr = 0)
 
#define ip_addr_set_any(ipaddr)   ((ipaddr)->addr = IPADDR_ANY)
 
#define ip_addr_set_loopback(ipaddr)   ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
 
#define ip_addr_set_hton(dest, src)
 
#define ip4_addr_set_u32(dest_ipaddr, src_u32)   ((dest_ipaddr)->addr = (src_u32))
 
#define ip4_addr_get_u32(src_ipaddr)   ((src_ipaddr)->addr)
 
#define ip_addr_get_network(target, host, netmask)   ((target)->addr = ((host)->addr) & ((netmask)->addr))
 
#define ip_addr_netcmp(addr1, addr2, mask)
 
#define ip_addr_cmp(addr1, addr2)   ((addr1)->addr == (addr2)->addr)
 
#define ip_addr_isany(addr1)   ((addr1) == NULL || (addr1)->addr == IPADDR_ANY)
 
#define ip_addr_isbroadcast(ipaddr, netif)   ip4_addr_isbroadcast((ipaddr)->addr, (netif))
 
#define ip_addr_netmask_valid(netmask)   ip4_addr_netmask_valid((netmask)->addr)
 
#define ip_addr_ismulticast(addr1)   (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))
 
#define ip_addr_islinklocal(addr1)   (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))
 
#define ip_addr_debug_print(debug, ipaddr)
 
#define ip4_addr1(ipaddr)   (((u8_t*)(ipaddr))[0])
 
#define ip4_addr2(ipaddr)   (((u8_t*)(ipaddr))[1])
 
#define ip4_addr3(ipaddr)   (((u8_t*)(ipaddr))[2])
 
#define ip4_addr4(ipaddr)   (((u8_t*)(ipaddr))[3])
 
#define ip4_addr1_16(ipaddr)   ((u16_t)ip4_addr1(ipaddr))
 
#define ip4_addr2_16(ipaddr)   ((u16_t)ip4_addr2(ipaddr))
 
#define ip4_addr3_16(ipaddr)   ((u16_t)ip4_addr3(ipaddr))
 
#define ip4_addr4_16(ipaddr)   ((u16_t)ip4_addr4(ipaddr))
 
#define ip_ntoa(ipaddr)   ipaddr_ntoa(ipaddr)
 
#define IP2STR(ipaddr)
 
#define IPSTR   "%d.%d.%d.%d"
 

Typedefs

typedef typedefPACK_STRUCT_END struct ip_addr ip_addr_t
 
typedef struct ip_addr_packed ip_addr_p_t
 

Functions

u8_t ip4_addr_isbroadcast (u32_t addr, const struct netif *netif) ICACHE_FLASH_ATTR
 
u8_t ip4_addr_netmask_valid (u32_t netmask) ICACHE_FLASH_ATTR
 
u32_t ipaddr_addr (const char *cp) ICACHE_FLASH_ATTR
 
int ipaddr_aton (const char *cp, ip_addr_t *addr) ICACHE_FLASH_ATTR
 
char * ipaddr_ntoa (const ip_addr_t *addr) ICACHE_FLASH_ATTR
 
char * ipaddr_ntoa_r (const ip_addr_t *addr, char *buf, int buflen) ICACHE_FLASH_ATTR
 

Variables

PACK_STRUCT_BEGIN struct ip_addr_packed PACK_STRUCT_STRUCT
 
const ip_addr_t ip_addr_any
 
const ip_addr_t ip_addr_broadcast
 

Macro Definition Documentation

◆ IP2STR

#define IP2STR (   ipaddr)
Value:
ip4_addr1_16(ipaddr), \
ip4_addr2_16(ipaddr), \
ip4_addr3_16(ipaddr), \
ip4_addr4_16(ipaddr)
#define ip4_addr1_16(ipaddr)
Definition: ip_addr.h:226

Definition at line 240 of file ip_addr.h.

Referenced by acl_check_packet(), and wifi_softap_ip_config().

◆ IP4_ADDR

#define IP4_ADDR (   ipaddr,
  a,
  b,
  c,
 
)
Value:
(ipaddr)->addr = ((u32_t)((a) & 0xff) << 24) | \
((u32_t)((b) & 0xff) << 16) | \
((u32_t)((c) & 0xff) << 8) | \
(u32_t)((d) & 0xff)
unsigned long u32_t
Definition: cc.h:56

Set an IP address given by the four byte-parts

Definition at line 139 of file ip_addr.h.

Referenced by dhcps_start(), espconn_connect(), espconn_find_connection(), espconn_tcp_client(), espconn_udp_sendto(), espconn_udp_sent(), netif_init(), and wifi_softap_ip_config().

◆ ip4_addr1

#define ip4_addr1 (   ipaddr)    (((u8_t*)(ipaddr))[0])

Definition at line 220 of file ip_addr.h.

Referenced by add_offer_options(), and captdnsRecv().

◆ ip4_addr1_16

#define ip4_addr1_16 (   ipaddr)    ((u16_t)ip4_addr1(ipaddr))

◆ ip4_addr2

#define ip4_addr2 (   ipaddr)    (((u8_t*)(ipaddr))[1])

Definition at line 221 of file ip_addr.h.

Referenced by add_offer_options(), and captdnsRecv().

◆ ip4_addr2_16

#define ip4_addr2_16 (   ipaddr)    ((u16_t)ip4_addr2(ipaddr))

◆ ip4_addr3

#define ip4_addr3 (   ipaddr)    (((u8_t*)(ipaddr))[2])

Definition at line 222 of file ip_addr.h.

Referenced by add_offer_options(), captdnsRecv(), and wifi_softap_ip_config().

◆ ip4_addr3_16

#define ip4_addr3_16 (   ipaddr)    ((u16_t)ip4_addr3(ipaddr))

◆ ip4_addr4

#define ip4_addr4 (   ipaddr)    (((u8_t*)(ipaddr))[3])

Definition at line 223 of file ip_addr.h.

Referenced by add_offer_options(), captdnsRecv(), patch_netif_ap(), and wifi_softap_ip_config().

◆ ip4_addr4_16

#define ip4_addr4_16 (   ipaddr)    ((u16_t)ip4_addr4(ipaddr))

◆ ip4_addr_get_u32

#define ip4_addr_get_u32 (   src_ipaddr)    ((src_ipaddr)->addr)

IPv4 only: get the IP address as an u32_t

Definition at line 181 of file ip_addr.h.

Referenced by inet_chksum_pseudo(), inet_chksum_pseudo_partial(), ip4_addr_isbroadcast(), ip_input(), ip_output_if(), ipaddr_addr(), and ipaddr_ntoa_r().

◆ ip4_addr_set_u32

#define ip4_addr_set_u32 (   dest_ipaddr,
  src_u32 
)    ((dest_ipaddr)->addr = (src_u32))

IPv4 only: set the IP address given as an u32_t

Definition at line 179 of file ip_addr.h.

Referenced by ip4_addr_isbroadcast(), and ipaddr_aton().

◆ IP_ADDR_ANY

#define IP_ADDR_ANY   ((ip_addr_t *)&ip_addr_any)

IP_ADDR_ can be used as a fixed IP address for the wildcard and the broadcast address

Definition at line 92 of file ip_addr.h.

Referenced by espconn_tcp_client(), espconn_tcp_server(), and espconn_udp_server().

◆ IP_ADDR_BROADCAST

#define IP_ADDR_BROADCAST   ((ip_addr_t *)&ip_addr_broadcast)

Definition at line 93 of file ip_addr.h.

◆ ip_addr_cmp

#define ip_addr_cmp (   addr1,
  addr2 
)    ((addr1)->addr == (addr2)->addr)

Definition at line 198 of file ip_addr.h.

Referenced by ip_input(), ip_output_if(), netif_set_ipaddr(), and wifi_softap_ip_config().

◆ ip_addr_copy

#define ip_addr_copy (   dest,
  src 
)    ((dest).addr = (src).addr)

Copy IP address - faster than ip_addr_set: no NULL check

Definition at line 162 of file ip_addr.h.

Referenced by ip_input(), and ip_output_if().

◆ ip_addr_debug_print

#define ip_addr_debug_print (   debug,
  ipaddr 
)
Value:
LWIP_DEBUGF(debug, ("%"U16_F".%"U16_F".%"U16_F".%"U16_F, \
ipaddr != NULL ? ip4_addr1_16(ipaddr) : 0, \
ipaddr != NULL ? ip4_addr2_16(ipaddr) : 0, \
ipaddr != NULL ? ip4_addr3_16(ipaddr) : 0, \
ipaddr != NULL ? ip4_addr4_16(ipaddr) : 0))
#define ip4_addr3_16(ipaddr)
Definition: ip_addr.h:228
#define U16_F
Definition: cc.h:61
#define NULL
Definition: def.h:47
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:94
#define ip4_addr4_16(ipaddr)
Definition: ip_addr.h:229
#define ip4_addr1_16(ipaddr)
Definition: ip_addr.h:226
#define ip4_addr2_16(ipaddr)
Definition: ip_addr.h:227

Definition at line 212 of file ip_addr.h.

Referenced by netif_add().

◆ ip_addr_get_network

#define ip_addr_get_network (   target,
  host,
  netmask 
)    ((target)->addr = ((host)->addr) & ((netmask)->addr))

Get the network address by combining host address with netmask

Definition at line 184 of file ip_addr.h.

◆ ip_addr_isany

#define ip_addr_isany (   addr1)    ((addr1) == NULL || (addr1)->addr == IPADDR_ANY)

Definition at line 200 of file ip_addr.h.

Referenced by ip_input(), ip_output_if(), ip_router(), netif_set_ipaddr(), and parse_msg().

◆ ip_addr_isbroadcast

#define ip_addr_isbroadcast (   ipaddr,
  netif 
)    ip4_addr_isbroadcast((ipaddr)->addr, (netif))

Definition at line 202 of file ip_addr.h.

Referenced by ip_input(), and ip_route().

◆ ip_addr_islinklocal

#define ip_addr_islinklocal (   addr1)    (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))

Definition at line 210 of file ip_addr.h.

Referenced by ip_router(), and netif_set_ipaddr().

◆ ip_addr_ismulticast

#define ip_addr_ismulticast (   addr1)    (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))

Definition at line 208 of file ip_addr.h.

Referenced by ip_input().

◆ ip_addr_netcmp

#define ip_addr_netcmp (   addr1,
  addr2,
  mask 
)
Value:
(((addr1)->addr & \
(mask)->addr) == \
((addr2)->addr & \
(mask)->addr))

Determine if two address are on the same network.

  • addr1 IP address 1
  • addr2 IP address 2
  • mask network identifier mask
    Returns
    !0 if the network identifiers of both address match

Definition at line 194 of file ip_addr.h.

Referenced by espconn_udp_recv(), ip4_addr_isbroadcast(), ip_route(), and ip_router().

◆ ip_addr_netmask_valid

#define ip_addr_netmask_valid (   netmask)    ip4_addr_netmask_valid((netmask)->addr)

Definition at line 205 of file ip_addr.h.

◆ ip_addr_set

#define ip_addr_set (   dest,
  src 
)
Value:
((dest)->addr = \
((src) == NULL ? 0 : \
(src)->addr))
#define NULL
Definition: def.h:47

Safely copy one IP address to another (src may be NULL)

Definition at line 164 of file ip_addr.h.

Referenced by netif_set_gw(), netif_set_ipaddr(), and netif_set_netmask().

◆ ip_addr_set_any

#define ip_addr_set_any (   ipaddr)    ((ipaddr)->addr = IPADDR_ANY)

Set address to IPADDR_ANY (no need for htonl())

Definition at line 170 of file ip_addr.h.

Referenced by ip_input().

◆ ip_addr_set_hton

#define ip_addr_set_hton (   dest,
  src 
)
Value:
((dest)->addr = \
((src) == NULL ? 0:\
htonl((src)->addr)))
#define NULL
Definition: def.h:47

Safely copy one IP address to another and change byte order from host- to network-order.

Definition at line 175 of file ip_addr.h.

◆ ip_addr_set_loopback

#define ip_addr_set_loopback (   ipaddr)    ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))

Set address to loopback address

Definition at line 172 of file ip_addr.h.

◆ ip_addr_set_zero

#define ip_addr_set_zero (   ipaddr)    ((ipaddr)->addr = 0)

Set complete address to zero

Definition at line 168 of file ip_addr.h.

Referenced by netif_add().

◆ IP_BADCLASS

#define IP_BADCLASS (   a)    (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)

Definition at line 132 of file ip_addr.h.

◆ IP_CLASSA

#define IP_CLASSA (   a)    ((((u32_t)(a)) & 0x80000000UL) == 0)

Definition at line 108 of file ip_addr.h.

◆ IP_CLASSA_HOST

#define IP_CLASSA_HOST   (0xffffffff & ~IP_CLASSA_NET)

Definition at line 111 of file ip_addr.h.

◆ IP_CLASSA_MAX

#define IP_CLASSA_MAX   128

Definition at line 112 of file ip_addr.h.

◆ IP_CLASSA_NET

#define IP_CLASSA_NET   0xff000000

Definition at line 109 of file ip_addr.h.

◆ IP_CLASSA_NSHIFT

#define IP_CLASSA_NSHIFT   24

Definition at line 110 of file ip_addr.h.

◆ IP_CLASSB

#define IP_CLASSB (   a)    ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)

Definition at line 114 of file ip_addr.h.

◆ IP_CLASSB_HOST

#define IP_CLASSB_HOST   (0xffffffff & ~IP_CLASSB_NET)

Definition at line 117 of file ip_addr.h.

◆ IP_CLASSB_MAX

#define IP_CLASSB_MAX   65536

Definition at line 118 of file ip_addr.h.

◆ IP_CLASSB_NET

#define IP_CLASSB_NET   0xffff0000

Definition at line 115 of file ip_addr.h.

◆ IP_CLASSB_NSHIFT

#define IP_CLASSB_NSHIFT   16

Definition at line 116 of file ip_addr.h.

◆ IP_CLASSC

#define IP_CLASSC (   a)    ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)

Definition at line 120 of file ip_addr.h.

◆ IP_CLASSC_HOST

#define IP_CLASSC_HOST   (0xffffffff & ~IP_CLASSC_NET)

Definition at line 123 of file ip_addr.h.

◆ IP_CLASSC_NET

#define IP_CLASSC_NET   0xffffff00

Definition at line 121 of file ip_addr.h.

◆ IP_CLASSC_NSHIFT

#define IP_CLASSC_NSHIFT   8

Definition at line 122 of file ip_addr.h.

◆ IP_CLASSD

#define IP_CLASSD (   a)    (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)

Definition at line 125 of file ip_addr.h.

◆ IP_CLASSD_HOST

#define IP_CLASSD_HOST   0x0fffffff /* routing needn't know. */

Definition at line 128 of file ip_addr.h.

◆ IP_CLASSD_NET

#define IP_CLASSD_NET   0xf0000000 /* These ones aren't really */

Definition at line 126 of file ip_addr.h.

◆ IP_CLASSD_NSHIFT

#define IP_CLASSD_NSHIFT   28 /* net and host fields, but */

Definition at line 127 of file ip_addr.h.

◆ IP_EXPERIMENTAL

#define IP_EXPERIMENTAL (   a)    (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)

Definition at line 131 of file ip_addr.h.

◆ IP_LOOPBACKNET

#define IP_LOOPBACKNET   127 /* official! */

Definition at line 134 of file ip_addr.h.

◆ IP_MULTICAST

#define IP_MULTICAST (   a)    IP_CLASSD(a)

Definition at line 129 of file ip_addr.h.

◆ ip_ntoa

#define ip_ntoa (   ipaddr)    ipaddr_ntoa(ipaddr)

For backwards compatibility

Definition at line 232 of file ip_addr.h.

◆ IPADDR2_COPY

#define IPADDR2_COPY (   dest,
  src 
)    SMEMCPY(dest, src, sizeof(ip_addr_t))

MEMCPY-like copying of IP addresses where addresses are known to be 16-bit-aligned if the port is correctly configured (so a port could define this to copying 2 u16_t's) - no NULL-pointer-checking needed.

Definition at line 158 of file ip_addr.h.

◆ IPADDR_ANY

#define IPADDR_ANY   ((u32_t)0x00000000UL)

0.0.0.0

Definition at line 100 of file ip_addr.h.

Referenced by espconn_find_connection(), and ip4_addr_isbroadcast().

◆ IPADDR_BROADCAST

#define IPADDR_BROADCAST   ((u32_t)0xffffffffUL)

255.255.255.255

Definition at line 102 of file ip_addr.h.

Referenced by acl_check_packet(), and ip4_addr_isbroadcast().

◆ IPADDR_LOOPBACK

#define IPADDR_LOOPBACK   ((u32_t)0x7f000001UL)

127.0.0.1

Definition at line 98 of file ip_addr.h.

◆ IPADDR_NONE

#define IPADDR_NONE   ((u32_t)0xffffffffUL)

255.255.255.255

Definition at line 96 of file ip_addr.h.

Referenced by ipaddr_addr().

◆ IPSTR

#define IPSTR   "%d.%d.%d.%d"

Definition at line 245 of file ip_addr.h.

Referenced by wifi_softap_ip_config().

Typedef Documentation

◆ ip_addr_p_t

typedef struct ip_addr_packed ip_addr_p_t

Definition at line 65 of file ip_addr.h.

◆ ip_addr_t

typedef typedefPACK_STRUCT_END struct ip_addr ip_addr_t

ip_addr_t uses a struct for convenience only, so that the same defines can operate both on ip_addr_t as well as on ip_addr_p_t.

Definition at line 64 of file ip_addr.h.

Function Documentation

◆ ip4_addr_isbroadcast()

u8_t ip4_addr_isbroadcast ( u32_t  addr,
const struct netif netif 
)

Determine if an address is a broadcast address on a network interface

Parameters
addraddress to be checked
netifthe network interface against which the address is checked
Returns
returns non-zero if the address is a broadcast address

Definition at line 55 of file ip_addr.c.

References netif::flags, ip4_addr_get_u32, ip4_addr_set_u32, netif::ip_addr, ip_addr_netcmp, IPADDR_ANY, IPADDR_BROADCAST, NETIF_FLAG_BROADCAST, and netif::netmask.

56 {
57  ip_addr_t ipaddr;
58  ip4_addr_set_u32(&ipaddr, addr);
59 
60  /* all ones (broadcast) or all zeroes (old skool broadcast) */
61  if ((~addr == IPADDR_ANY) ||
62  (addr == IPADDR_ANY)) {
63  return 1;
64  /* no broadcast support on this network interface? */
65  } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) {
66  /* the given address cannot be a broadcast address
67  * nor can we check against any broadcast addresses */
68  return 0;
69  /* address matches network interface address exactly? => no broadcast */
70  } else if (addr == ip4_addr_get_u32(&netif->ip_addr)) {
71  return 0;
72  /* on the same (sub) network... */
73  } else if (ip_addr_netcmp(&ipaddr, &(netif->ip_addr), &(netif->netmask))
74  /* ...and host identifier bits are all ones? =>... */
75  && ((addr & ~ip4_addr_get_u32(&netif->netmask)) ==
76  (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask)))) {
77  /* => network broadcast address */
78  return 1;
79  } else {
80  return 0;
81  }
82 }
#define ip4_addr_get_u32(src_ipaddr)
Definition: ip_addr.h:181
#define ip_addr_netcmp(addr1, addr2, mask)
Definition: ip_addr.h:194
#define IPADDR_ANY
Definition: ip_addr.h:100
u8_t flags
Definition: netif.h:193
#define NETIF_FLAG_BROADCAST
Definition: netif.h:72
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
#define IPADDR_BROADCAST
Definition: ip_addr.h:102
ip_addr_t ip_addr
Definition: netif.h:144
ip_addr_t netmask
Definition: netif.h:145
#define ip4_addr_set_u32(dest_ipaddr, src_u32)
Definition: ip_addr.h:179

◆ ip4_addr_netmask_valid()

u8_t ip4_addr_netmask_valid ( u32_t  netmask)

Checks if a netmask is valid (starting with ones, then only zeros)

Parameters
netmaskthe IPv4 netmask to check (in network byte order!)
Returns
1 if the netmask is valid, 0 if it is not

Definition at line 90 of file ip_addr.c.

References lwip_htonl().

91 {
92  u32_t mask;
93  u32_t nm_hostorder = lwip_htonl(netmask);
94 
95  /* first, check for the first zero */
96  for (mask = 1U << 31 ; mask != 0; mask >>= 1) {
97  if ((nm_hostorder & mask) == 0) {
98  break;
99  }
100  }
101  /* then check that there is no one */
102  for (; mask != 0; mask >>= 1) {
103  if ((nm_hostorder & mask) != 0) {
104  /* there is a one after the first zero -> invalid */
105  return 0;
106  }
107  }
108  /* no one after the first zero -> valid */
109  return 1;
110 }
u32_t lwip_htonl(u32_t n)
Definition: def.c:88
unsigned long u32_t
Definition: cc.h:56
ip_addr_t netmask
Definition: netif.h:145
Here is the call graph for this function:

◆ ipaddr_addr()

u32_t ipaddr_addr ( const char *  cp)

Ascii internet address interpretation routine. The value returned is in network order.

Parameters
cpIP address in ascii represenation (e.g. "127.0.0.1")
Returns
ip address in network order

Definition at line 130 of file ip_addr.c.

131 {
132  ip_addr_t val;
133 
134  if (ipaddr_aton(cp, &val)) {
135  return ip4_addr_get_u32(&val);
136  }
137  return (IPADDR_NONE);
138 }
#define ip4_addr_get_u32(src_ipaddr)
Definition: ip_addr.h:181
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
int ipaddr_aton(const char *cp, ip_addr_t *addr)
Definition: ip_addr.c:152
#define IPADDR_NONE
Definition: ip_addr.h:96

◆ ipaddr_aton()

int ipaddr_aton ( const char *  cp,
ip_addr_t addr 
)

Check whether "cp" is a valid ascii representation of an Internet address and convert to a binary address. Returns 1 if the address is valid, 0 if not. This replaces inet_addr, the return value from which cannot distinguish between failure and a local broadcast address.

Parameters
cpIP address in ascii represenation (e.g. "127.0.0.1")
addrpointer to which to save the ip address in network order
Returns
1 if cp could be converted to addr, 0 on failure

Definition at line 152 of file ip_addr.c.

References htonl, ip4_addr_set_u32, islower, isspace, isxdigit, and LWIP_ASSERT.

Referenced by ipaddr_addr().

153 {
154  u32_t val;
155  u8_t base;
156  char c;
157  char ch;
158  unsigned long cutoff;
159  int cutlim;
160  u32_t parts[4];
161  u32_t *pp = parts;
162 
163  c = *cp;
164  for (;;) {
165  /*
166  * Collect number up to ``.''.
167  * Values are specified as for C:
168  * 0x=hex, 0=octal, 1-9=decimal.
169  */
170  if (!isdigit(c))
171  return (0);
172  val = 0;
173  base = 10;
174  if (c == '0') {
175  c = *++cp;
176  if (c == 'x' || c == 'X') {
177  base = 16;
178  c = *++cp;
179  } else
180  base = 8;
181  }
182 
183  cutoff =(unsigned long)0xffffffff / (unsigned long)base;
184  cutlim =(unsigned long)0xffffffff % (unsigned long)base;
185 
186  for (;;) {
187  if (isdigit(c)) {
188  ch = (int)(c - '0');
189 
190  if (val > cutoff || (val == cutoff && ch > cutlim))
191  return (0);
192 
193  val = (val * base) + (int)(c - '0');
194  c = *++cp;
195  } else if (base == 16 && isxdigit(c)) {
196  ch = (int)(c + 10 - (islower(c) ? 'a' : 'A'));
197 
198  if (val > cutoff || (val == cutoff && ch > cutlim))
199  return (0);
200 
201  val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
202  c = *++cp;
203  } else
204  break;
205  }
206  if (c == '.') {
207  /*
208  * Internet format:
209  * a.b.c.d
210  * a.b.c (with c treated as 16 bits)
211  * a.b (with b treated as 24 bits)
212  */
213  if (pp >= parts + 3) {
214  return (0);
215  }
216  *pp++ = val;
217  c = *++cp;
218  } else
219  break;
220  }
221  /*
222  * Check for trailing characters.
223  */
224  if (c != '\0' && !isspace(c)) {
225  return (0);
226  }
227  /*
228  * Concoct the address according to
229  * the number of parts specified.
230  */
231  switch (pp - parts + 1) {
232 
233  case 0:
234  return (0); /* initial nondigit */
235 
236  case 1: /* a -- 32 bits */
237  break;
238 
239  case 2: /* a.b -- 8.24 bits */
240  if ((val > 0xffffffUL) || (parts[0] > 0xff)) {
241  return (0);
242  }
243  val |= parts[0] << 24;
244  break;
245 
246  case 3: /* a.b.c -- 8.8.16 bits */
247  if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff)) {
248  return (0);
249  }
250  val |= (parts[0] << 24) | (parts[1] << 16);
251  break;
252 
253  case 4: /* a.b.c.d -- 8.8.8.8 bits */
254  if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff)) {
255  return (0);
256  }
257  val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
258  break;
259  default:
260  LWIP_ASSERT("unhandled", 0);
261  break;
262  }
263  if (addr) {
264  ip4_addr_set_u32(addr, htonl(val));
265  }
266  return (1);
267 }
#define isspace(c)
Definition: ip_addr.c:119
#define isxdigit(c)
Definition: ip_addr.c:117
unsigned long u32_t
Definition: cc.h:56
#define islower(c)
Definition: ip_addr.c:118
unsigned char u8_t
Definition: cc.h:52
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:65
#define ip4_addr_set_u32(dest_ipaddr, src_u32)
Definition: ip_addr.h:179
#define htonl(x)
Definition: def.h:83
Here is the caller graph for this function:

◆ ipaddr_ntoa()

char* ipaddr_ntoa ( const ip_addr_t addr)

returns ptr to static buffer; not reentrant!

Convert numeric IP address into decimal dotted ASCII representation. returns ptr to static buffer; not reentrant!

Parameters
addrip address in network order to convert
Returns
pointer to a global static (!) buffer that holds the ASCII represenation of addr

Definition at line 278 of file ip_addr.c.

References ipaddr_ntoa_r().

279 {
280  static char str[16];
281  return ipaddr_ntoa_r(addr, str, 16);
282 }
char * ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
Definition: ip_addr.c:293
Here is the call graph for this function:

◆ ipaddr_ntoa_r()

char* ipaddr_ntoa_r ( const ip_addr_t addr,
char *  buf,
int  buflen 
)

Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.

Parameters
addrip address in network order to convert
buftarget buffer where the string is stored
buflenlength of buf
Returns
either pointer to buf which now holds the ASCII representation of addr or NULL if buf was too small

Definition at line 293 of file ip_addr.c.

References ip4_addr_get_u32, and NULL.

Referenced by ipaddr_ntoa().

294 {
295  u32_t s_addr;
296  char inv[3];
297  char *rp;
298  u8_t *ap;
299  u8_t rem;
300  u8_t n;
301  u8_t i;
302  int len = 0;
303 
304  s_addr = ip4_addr_get_u32(addr);
305 
306  rp = buf;
307  ap = (u8_t *)&s_addr;
308  for(n = 0; n < 4; n++) {
309  i = 0;
310  do {
311  rem = *ap % (u8_t)10;
312  *ap /= (u8_t)10;
313  inv[i++] = '0' + rem;
314  } while(*ap);
315  while(i--) {
316  if (len++ >= buflen) {
317  return NULL;
318  }
319  *rp++ = inv[i];
320  }
321  if (len++ >= buflen) {
322  return NULL;
323  }
324  *rp++ = '.';
325  ap++;
326  }
327  *--rp = 0;
328  return buf;
329 }
#define ip4_addr_get_u32(src_ipaddr)
Definition: ip_addr.h:181
#define NULL
Definition: def.h:47
unsigned long u32_t
Definition: cc.h:56
unsigned char u8_t
Definition: cc.h:52
Here is the caller graph for this function:

Variable Documentation

◆ ip_addr_any

const ip_addr_t ip_addr_any

◆ ip_addr_broadcast

const ip_addr_t ip_addr_broadcast

◆ PACK_STRUCT_STRUCT

PACK_STRUCT_BEGIN struct ip_addr2 PACK_STRUCT_STRUCT