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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 #include "addr_and_mask.h"
44 ipv4
= g_new(ipv4_addr
, 1);
49 ipv4_addr_free(ipv4_addr
*ipv4
)
55 ipv4_addr_set_host_order_addr(ipv4_addr
*ipv4
, const guint32 new_addr
)
57 ipv4
->addr
= new_addr
;
61 ipv4_addr_set_net_order_addr(ipv4_addr
*ipv4
, const guint32 new_addr
)
63 ipv4
->addr
= g_ntohl(new_addr
);
67 ipv4_addr_set_netmask_bits(ipv4_addr
*ipv4
, const guint new_nmask_bits
)
69 ipv4
->nmask
= ip_get_subnet_mask(new_nmask_bits
);
73 ipv4_get_net_order_addr(ipv4_addr
*ipv4
)
75 return g_htonl(ipv4
->addr
);
79 ipv4_get_host_order_addr(ipv4_addr
*ipv4
)
84 /* We're assuming the buffer is at least MAX_IP_STR_LEN (16 bytes) */
86 ipv4_addr_str_buf(const ipv4_addr
*ipv4
, gchar
*buf
)
88 guint32 ipv4_host_order
= g_htonl(ipv4
->addr
);
89 ip_to_str_buf((guint8
*)&ipv4_host_order
, buf
, MAX_IP_STR_LEN
);
95 * w.x.y.z/32 eq w.x.y.0/24 TRUE
98 /* Returns TRUE if equal, FALSE if not */
100 ipv4_addr_eq(const ipv4_addr
*a
, const ipv4_addr
*b
)
102 guint32 val_a
, val_b
, nmask
;
104 nmask
= MIN(a
->nmask
, b
->nmask
);
105 val_a
= a
->addr
& nmask
;
106 val_b
= b
->addr
& nmask
;
107 return (val_a
== val_b
);
111 ipv4_addr_gt(const ipv4_addr
*a
, const ipv4_addr
*b
)
113 guint32 val_a
, val_b
, nmask
;
115 nmask
= MIN(a
->nmask
, b
->nmask
);
116 val_a
= a
->addr
& nmask
;
117 val_b
= b
->addr
& nmask
;
119 return (val_a
> val_b
);
123 ipv4_addr_ge(const ipv4_addr
*a
, const ipv4_addr
*b
)
125 guint32 val_a
, val_b
, nmask
;
127 nmask
= MIN(a
->nmask
, b
->nmask
);
128 val_a
= a
->addr
& nmask
;
129 val_b
= b
->addr
& nmask
;
131 return (val_a
>= val_b
);
135 ipv4_addr_lt(const ipv4_addr
*a
, const ipv4_addr
*b
)
137 guint32 val_a
, val_b
, nmask
;
139 nmask
= MIN(a
->nmask
, b
->nmask
);
140 val_a
= a
->addr
& nmask
;
141 val_b
= b
->addr
& nmask
;
143 return (val_a
< val_b
);
147 ipv4_addr_le(const ipv4_addr
*a
, const ipv4_addr
*b
)
149 guint32 val_a
, val_b
, nmask
;
151 nmask
= MIN(a
->nmask
, b
->nmask
);
152 val_a
= a
->addr
& nmask
;
153 val_b
= b
->addr
& nmask
;
155 return (val_a
<= val_b
);