Removed trailing whitespaces from .h and .c files using the
[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: ipv4.c,v 1.4 2002/08/28 20:40:44 jmayer Exp $
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@zing.org>
12  * Copyright 1998 Gerald Combs
13  *
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <glib.h>
35 #include <stdio.h>
36
37 #include "ipv4.h"
38 #include "packet.h" /* for ip_to_str */
39
40 static guint32 create_nmask(gint net_bits);
41
42 ipv4_addr*
43 ipv4_addr_new(void)
44 {
45         ipv4_addr       *ipv4;
46
47         ipv4 = g_new(ipv4_addr, 1);
48         return ipv4;
49 }
50
51 void
52 ipv4_addr_free(ipv4_addr *ipv4)
53 {
54         if (ipv4)
55                 g_free(ipv4);
56 }
57
58 void
59 ipv4_addr_set_host_order_addr(ipv4_addr *ipv4, guint32 new_addr)
60 {
61         ipv4->addr = new_addr;
62 }
63
64 void
65 ipv4_addr_set_net_order_addr(ipv4_addr *ipv4, guint32 new_addr)
66 {
67         ipv4->addr = g_ntohl(new_addr);
68 }
69
70 void
71 ipv4_addr_set_netmask_bits(ipv4_addr *ipv4, guint new_nmask_bits)
72 {
73 /*      ipv4->nmask_bits = new_nmask_bits;*/
74         ipv4->nmask = create_nmask(new_nmask_bits);
75 }
76
77 guint32
78 ipv4_get_net_order_addr(ipv4_addr *ipv4)
79 {
80         return g_htonl(ipv4->addr);
81 }
82
83 guint32
84 ipv4_get_host_order_addr(ipv4_addr *ipv4)
85 {
86         return ipv4->addr;
87 }
88
89 gchar*
90 ipv4_addr_str(ipv4_addr *ipv4)
91 {
92         guint32 ipv4_host_order = g_htonl(ipv4->addr);
93         return ip_to_str((gchar*)&ipv4_host_order);
94 }
95
96 static guint32
97 create_nmask(gint net_bits)
98 {
99         static guint32 bitmasks[33] = {
100                 0x00000000,
101                 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
102                 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
103                 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
104                 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
105                 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
106                 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
107                 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
108                 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff,
109         };
110
111         g_assert(net_bits <= 32);
112
113         return bitmasks[net_bits];
114 }
115
116
117
118 /*
119  * w.x.y.z/32 eq w.x.y.0/24    TRUE
120  */
121
122 /* Returns TRUE if equal, FALSE if not */
123 gboolean
124 ipv4_addr_eq(ipv4_addr *a, ipv4_addr *b)
125 {
126         guint32 val_a, val_b, nmask;
127
128         nmask = MIN(a->nmask, b->nmask);
129         val_a = a->addr & nmask;
130         val_b = b->addr & nmask;
131         return (val_a == val_b);
132 }
133
134 gboolean
135 ipv4_addr_gt(ipv4_addr *a, ipv4_addr *b)
136 {
137         guint32 val_a, val_b, nmask;
138
139         nmask = MIN(a->nmask, b->nmask);
140         val_a = a->addr & nmask;
141         val_b = b->addr & nmask;
142
143         return (val_a > val_b);
144 }
145
146 gboolean
147 ipv4_addr_ge(ipv4_addr *a, ipv4_addr *b)
148 {
149         guint32 val_a, val_b, nmask;
150
151         nmask = MIN(a->nmask, b->nmask);
152         val_a = a->addr & nmask;
153         val_b = b->addr & nmask;
154
155         return (val_a >= val_b);
156 }
157
158 gboolean
159 ipv4_addr_lt(ipv4_addr *a, ipv4_addr *b)
160 {
161         guint32 val_a, val_b, nmask;
162
163         nmask = MIN(a->nmask, b->nmask);
164         val_a = a->addr & nmask;
165         val_b = b->addr & nmask;
166
167         return (val_a < val_b);
168 }
169
170 gboolean
171 ipv4_addr_le(ipv4_addr *a, ipv4_addr *b)
172 {
173         guint32 val_a, val_b, nmask;
174
175         nmask = MIN(a->nmask, b->nmask);
176         val_a = a->addr & nmask;
177         val_b = b->addr & nmask;
178
179         return (val_a <= val_b);
180 }