3 * IPv4 address class. They understand how to take netmasks into consideration
4 * during equivalence testing.
6 * Gilbert Ramirez <gram@alumni.rice.edu>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include "addr_and_mask.h"
38 ipv4_addr_and_mask_new(void)
40 ipv4_addr_and_mask *ipv4;
42 ipv4 = g_new(ipv4_addr_and_mask, 1);
47 ipv4_addr_and_mask_free(ipv4_addr_and_mask *ipv4)
53 ipv4_addr_and_mask_set_host_order_addr(ipv4_addr_and_mask *ipv4, const guint32 new_addr)
55 ipv4->addr = new_addr;
59 ipv4_addr_and_mask_set_net_order_addr(ipv4_addr_and_mask *ipv4, const guint32 new_addr)
61 ipv4->addr = g_ntohl(new_addr);
65 ipv4_addr_and_mask_set_netmask_bits(ipv4_addr_and_mask *ipv4, const guint new_nmask_bits)
67 ipv4->nmask = ip_get_subnet_mask(new_nmask_bits);
71 ipv4_get_net_order_addr(ipv4_addr_and_mask *ipv4)
73 return g_htonl(ipv4->addr);
77 ipv4_get_host_order_addr(ipv4_addr_and_mask *ipv4)
82 /* We're assuming the buffer is at least MAX_IP_STR_LEN (16 bytes) */
84 ipv4_addr_and_mask_str_buf(const ipv4_addr_and_mask *ipv4, gchar *buf)
86 guint32 ipv4_host_order = g_htonl(ipv4->addr);
87 ip_to_str_buf((guint8*)&ipv4_host_order, buf, MAX_IP_STR_LEN);
93 * w.x.y.z/32 eq w.x.y.0/24 TRUE
96 /* Returns TRUE if equal, FALSE if not */
98 ipv4_addr_and_mask_eq(const ipv4_addr_and_mask *a, const ipv4_addr_and_mask *b)
100 guint32 val_a, val_b, nmask;
102 nmask = MIN(a->nmask, b->nmask);
103 val_a = a->addr & nmask;
104 val_b = b->addr & nmask;
105 return (val_a == val_b);
109 ipv4_addr_and_mask_gt(const ipv4_addr_and_mask *a, const ipv4_addr_and_mask *b)
111 guint32 val_a, val_b, nmask;
113 nmask = MIN(a->nmask, b->nmask);
114 val_a = a->addr & nmask;
115 val_b = b->addr & nmask;
117 return (val_a > val_b);
121 ipv4_addr_and_mask_ge(const ipv4_addr_and_mask *a, const ipv4_addr_and_mask *b)
123 guint32 val_a, val_b, nmask;
125 nmask = MIN(a->nmask, b->nmask);
126 val_a = a->addr & nmask;
127 val_b = b->addr & nmask;
129 return (val_a >= val_b);
133 ipv4_addr_and_mask_lt(const ipv4_addr_and_mask *a, const ipv4_addr_and_mask *b)
135 guint32 val_a, val_b, nmask;
137 nmask = MIN(a->nmask, b->nmask);
138 val_a = a->addr & nmask;
139 val_b = b->addr & nmask;
141 return (val_a < val_b);
145 ipv4_addr_and_mask_le(const ipv4_addr_and_mask *a, const ipv4_addr_and_mask *b)
147 guint32 val_a, val_b, nmask;
149 nmask = MIN(a->nmask, b->nmask);
150 val_a = a->addr & nmask;
151 val_b = b->addr & nmask;
153 return (val_a <= val_b);
157 * Editor modelines - http://www.wireshark.org/tools/modelines.html
162 * indent-tabs-mode: t
165 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
166 * :indentSize=8:tabSize=8:noTabs=false: