As suggested by Anders: back out 37112.
[obnox/wireshark/wip.git] / epan / ipv4.c
1 /* ipv4.c
2  *
3  * IPv4 address class. They understand how to take netmasks into consideration
4  * during equivalence testing.
5  *
6  * Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
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.
18  *
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.
23  *
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.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <glib.h>
34 #include <stdio.h>
35
36 #include "ipv4.h"
37 #include "packet.h" /* for ip_to_str */
38
39
40 ipv4_addr*
41 ipv4_addr_new(void)
42 {
43         ipv4_addr       *ipv4;
44
45         ipv4 = g_new(ipv4_addr, 1);
46         return ipv4;
47 }
48
49 void
50 ipv4_addr_free(ipv4_addr *ipv4)
51 {
52         g_free(ipv4);
53 }
54
55 void
56 ipv4_addr_set_host_order_addr(ipv4_addr *ipv4, const guint32 new_addr)
57 {
58         ipv4->addr = new_addr;
59 }
60
61 void
62 ipv4_addr_set_net_order_addr(ipv4_addr *ipv4, const guint32 new_addr)
63 {
64         ipv4->addr = g_ntohl(new_addr);
65 }
66
67 void
68 ipv4_addr_set_netmask_bits(ipv4_addr *ipv4, const guint new_nmask_bits)
69 {
70         static guint32 bitmasks[33] = {
71                 0x00000000,
72                 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
73                 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
74                 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
75                 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
76                 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
77                 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
78                 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
79                 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff,
80         };
81
82         g_assert(new_nmask_bits <= 32);
83
84         ipv4->nmask = bitmasks[new_nmask_bits];
85 }
86
87 guint32
88 ipv4_get_net_order_addr(ipv4_addr *ipv4)
89 {
90         return g_htonl(ipv4->addr);
91 }
92
93 guint32
94 ipv4_get_host_order_addr(ipv4_addr *ipv4)
95 {
96         return ipv4->addr;
97 }
98
99 /* We're assuming the buffer is at least MAX_IP_STR_LEN (16 bytes) */
100 void
101 ipv4_addr_str_buf(const ipv4_addr *ipv4, gchar *buf)
102 {
103         guint32 ipv4_host_order = g_htonl(ipv4->addr);
104         ip_to_str_buf((guint8*)&ipv4_host_order, buf, MAX_IP_STR_LEN);
105 }
106
107
108
109 /*
110  * w.x.y.z/32 eq w.x.y.0/24    TRUE
111  */
112
113 /* Returns TRUE if equal, FALSE if not */
114 gboolean
115 ipv4_addr_eq(ipv4_addr *a, ipv4_addr *b)
116 {
117         guint32 val_a, val_b, nmask;
118
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);
123 }
124
125 gboolean
126 ipv4_addr_gt(ipv4_addr *a, ipv4_addr *b)
127 {
128         guint32 val_a, val_b, nmask;
129
130         nmask = MIN(a->nmask, b->nmask);
131         val_a = a->addr & nmask;
132         val_b = b->addr & nmask;
133
134         return (val_a > val_b);
135 }
136
137 gboolean
138 ipv4_addr_ge(ipv4_addr *a, ipv4_addr *b)
139 {
140         guint32 val_a, val_b, nmask;
141
142         nmask = MIN(a->nmask, b->nmask);
143         val_a = a->addr & nmask;
144         val_b = b->addr & nmask;
145
146         return (val_a >= val_b);
147 }
148
149 gboolean
150 ipv4_addr_lt(ipv4_addr *a, ipv4_addr *b)
151 {
152         guint32 val_a, val_b, nmask;
153
154         nmask = MIN(a->nmask, b->nmask);
155         val_a = a->addr & nmask;
156         val_b = b->addr & nmask;
157
158         return (val_a < val_b);
159 }
160
161 gboolean
162 ipv4_addr_le(ipv4_addr *a, ipv4_addr *b)
163 {
164         guint32 val_a, val_b, nmask;
165
166         nmask = MIN(a->nmask, b->nmask);
167         val_a = a->addr & nmask;
168         val_b = b->addr & nmask;
169
170         return (val_a <= val_b);
171 }