HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / ipv4.c
blob25e64bed770bb7f169421065d72dc82c3f8472ec
1 /* ipv4.c
3 * IPv4 address class. They understand how to take netmasks into consideration
4 * during equivalence testing.
6 * Gilbert Ramirez <gram@alumni.rice.edu>
8 * $Id$
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.
29 #include "config.h"
31 #include <glib.h>
32 #include <stdio.h>
34 #include "ipv4.h"
35 #include "to_str.h"
36 #include "addr_and_mask.h"
39 ipv4_addr*
40 ipv4_addr_new(void)
42 ipv4_addr *ipv4;
44 ipv4 = g_new(ipv4_addr, 1);
45 return ipv4;
48 void
49 ipv4_addr_free(ipv4_addr *ipv4)
51 g_free(ipv4);
54 void
55 ipv4_addr_set_host_order_addr(ipv4_addr *ipv4, const guint32 new_addr)
57 ipv4->addr = new_addr;
60 void
61 ipv4_addr_set_net_order_addr(ipv4_addr *ipv4, const guint32 new_addr)
63 ipv4->addr = g_ntohl(new_addr);
66 void
67 ipv4_addr_set_netmask_bits(ipv4_addr *ipv4, const guint new_nmask_bits)
69 ipv4->nmask = ip_get_subnet_mask(new_nmask_bits);
72 guint32
73 ipv4_get_net_order_addr(ipv4_addr *ipv4)
75 return g_htonl(ipv4->addr);
78 guint32
79 ipv4_get_host_order_addr(ipv4_addr *ipv4)
81 return ipv4->addr;
84 /* We're assuming the buffer is at least MAX_IP_STR_LEN (16 bytes) */
85 void
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 */
99 gboolean
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);
110 gboolean
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);
122 gboolean
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);
134 gboolean
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);
146 gboolean
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);