3 * IPv4 address class. They understand how to take netmasks into consideration
4 * during equivalence testing.
6 * Gilbert Ramirez <gram@alumni.rice.edu>
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald@wireshark.org>
12 * Copyright 1998 Gerald Combs
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include "packet.h" /* for ip_to_str */
45 ipv4 = g_new(ipv4_addr, 1);
50 ipv4_addr_free(ipv4_addr *ipv4)
57 ipv4_addr_set_host_order_addr(ipv4_addr *ipv4, guint32 new_addr)
59 ipv4->addr = new_addr;
63 ipv4_addr_set_net_order_addr(ipv4_addr *ipv4, guint32 new_addr)
65 ipv4->addr = g_ntohl(new_addr);
69 ipv4_addr_set_netmask_bits(ipv4_addr *ipv4, guint new_nmask_bits)
71 static guint32 bitmasks[33] = {
73 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
74 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
75 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
76 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
77 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
78 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
79 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
80 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff,
83 g_assert(new_nmask_bits <= 32);
85 ipv4->nmask = bitmasks[new_nmask_bits];
89 ipv4_get_net_order_addr(ipv4_addr *ipv4)
91 return g_htonl(ipv4->addr);
95 ipv4_get_host_order_addr(ipv4_addr *ipv4)
101 ipv4_addr_str_buf(const ipv4_addr *ipv4, gchar *buf)
103 guint32 ipv4_host_order = g_htonl(ipv4->addr);
104 ip_to_str_buf((guint8*)&ipv4_host_order, buf);
110 * w.x.y.z/32 eq w.x.y.0/24 TRUE
113 /* Returns TRUE if equal, FALSE if not */
115 ipv4_addr_eq(ipv4_addr *a, ipv4_addr *b)
117 guint32 val_a, val_b, nmask;
119 nmask = MIN(a->nmask, b->nmask);
120 val_a = a->addr & nmask;
121 val_b = b->addr & nmask;
122 return (val_a == val_b);
126 ipv4_addr_gt(ipv4_addr *a, ipv4_addr *b)
128 guint32 val_a, val_b, nmask;
130 nmask = MIN(a->nmask, b->nmask);
131 val_a = a->addr & nmask;
132 val_b = b->addr & nmask;
134 return (val_a > val_b);
138 ipv4_addr_ge(ipv4_addr *a, ipv4_addr *b)
140 guint32 val_a, val_b, nmask;
142 nmask = MIN(a->nmask, b->nmask);
143 val_a = a->addr & nmask;
144 val_b = b->addr & nmask;
146 return (val_a >= val_b);
150 ipv4_addr_lt(ipv4_addr *a, ipv4_addr *b)
152 guint32 val_a, val_b, nmask;
154 nmask = MIN(a->nmask, b->nmask);
155 val_a = a->addr & nmask;
156 val_b = b->addr & nmask;
158 return (val_a < val_b);
162 ipv4_addr_le(ipv4_addr *a, ipv4_addr *b)
164 guint32 val_a, val_b, nmask;
166 nmask = MIN(a->nmask, b->nmask);
167 val_a = a->addr & nmask;
168 val_b = b->addr & nmask;
170 return (val_a <= val_b);