1/**
2 * @file
3 * This is the IPv4 address tools implementation.
4 *
5 */
6
7/*
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright notice,
15 *    this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 *    this list of conditions and the following disclaimer in the documentation
18 *    and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
32 *
33 * This file is part of the lwIP TCP/IP stack.
34 *
35 * Author: Adam Dunkels <adam@sics.se>
36 *
37 */
38
39#include "lwip/opt.h"
40#include "lwip/ip_addr.h"
41#include "lwip/inet.h"
42#include "lwip/netif.h"
43
44#define IP_ADDR_ANY_VALUE 0x00000000UL
45#define IP_ADDR_BROADCAST_VALUE 0xffffffffUL
46
47/* used by IP_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
48const struct ip_addr ip_addr_any = { IP_ADDR_ANY_VALUE };
49const struct ip_addr ip_addr_broadcast = { IP_ADDR_BROADCAST_VALUE };
50
51/**
52 * Determine if an address is a broadcast address on a network interface
53 *
54 * @param addr address to be checked
55 * @param netif the network interface against which the address is checked
56 * @return returns non-zero if the address is a broadcast address
57 */
58u8_t ip_addr_isbroadcast(struct ip_addr *addr, struct netif *netif)
59{
60    u32_t addr2test;
61
62    addr2test = addr->addr;
63    /* all ones (broadcast) or all zeroes (old skool broadcast) */
64    if ((~addr2test == IP_ADDR_ANY_VALUE) || (addr2test == IP_ADDR_ANY_VALUE))
65        return 1;
66    /* no broadcast support on this network interface? */
67    else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0)
68        /* the given address cannot be a broadcast address
69         * nor can we check against any broadcast addresses */
70        return 0;
71    /* address matches network interface address exactly? => no broadcast */
72    else if (addr2test == netif->ip_addr.addr)
73        return 0;
74    /*  on the same (sub) network... */
75    else if (ip_addr_netcmp(addr, &(netif->ip_addr), &(netif->netmask))
76             /* ...and host identifier bits are all ones? =>... */
77             && ((addr2test & ~netif->netmask.addr) ==
78                 (IP_ADDR_BROADCAST_VALUE & ~netif->netmask.addr)))
79        /* => network broadcast address */
80        return 1;
81    else
82        return 0;
83}
84