Add "class" that understands IPv4 addresses and subnet masks.
[obnox/wireshark/wip.git] / 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@xiexie.org>
7  *
8  * $Id: ipv4.c,v 1.1 1999/11/15 06:32:14 gram Exp $
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@unicom.net>
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 #ifdef HAVE_NETINET_IN_H
38 # include <netinet/in.h>
39 #endif
40
41 #include "ipv4.h"
42
43 static guint32 create_nmask(gint net_bits);
44
45 ipv4_addr*
46 ipv4_addr_new(void)
47 {
48         ipv4_addr       *ipv4;
49
50         ipv4 = g_new(ipv4_addr, 1);
51         return ipv4;
52 }
53
54 void
55 ipv4_addr_free(ipv4_addr *ipv4)
56 {
57         if (ipv4)
58                 g_free(ipv4);
59 }
60
61 void
62 ipv4_addr_set_host_order_addr(ipv4_addr *ipv4, guint32 new_addr)
63 {
64         ipv4->addr = new_addr;
65 }
66
67 void
68 ipv4_addr_set_net_order_addr(ipv4_addr *ipv4, guint32 new_addr)
69 {
70         ipv4->addr = ntohl(new_addr);
71 }
72
73 void
74 ipv4_addr_set_netmask_bits(ipv4_addr *ipv4, guint new_nmask_bits)
75 {
76 /*      ipv4->nmask_bits = new_nmask_bits;*/
77         ipv4->nmask = create_nmask(new_nmask_bits);
78 }
79
80 guint32
81 ipv4_get_net_order_addr(ipv4_addr *ipv4)
82 {
83         return htonl(ipv4->addr);
84 }
85
86 guint32
87 ipv4_get_host_order_addr(ipv4_addr *ipv4)
88 {
89         return ipv4->addr;
90 }
91
92
93 static guint32
94 create_nmask(gint net_bits)
95 {
96         static guint32 bitmasks[33] = {
97                 0x00000000,
98                 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
99                 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
100                 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
101                 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
102                 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
103                 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
104                 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
105                 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff,
106         };
107
108         g_assert(net_bits <= 32);
109
110         return bitmasks[net_bits];
111 }
112
113
114
115 /*
116  * w.x.y.z/32 eq w.x.y.0/24    TRUE
117  */
118
119 /* Returns TRUE if equal, FALSE if not */
120 gboolean
121 ipv4_addr_eq(ipv4_addr *a, ipv4_addr *b)
122 {
123         guint32 val_a, val_b, nmask;
124
125         nmask = MIN(a->nmask, b->nmask);
126         val_a = a->addr & nmask;
127         val_b = b->addr & nmask;
128         return (val_a == val_b);
129 }
130
131 gboolean
132 ipv4_addr_gt(ipv4_addr *a, ipv4_addr *b)
133 {
134         guint32 val_a, val_b, nmask;
135
136         nmask = MIN(a->nmask, b->nmask);
137         val_a = a->addr & nmask;
138         val_b = b->addr & nmask;
139
140         return (val_a > val_b);
141 }
142
143 gboolean
144 ipv4_addr_ge(ipv4_addr *a, ipv4_addr *b)
145 {
146         guint32 val_a, val_b, nmask;
147
148         nmask = MIN(a->nmask, b->nmask);
149         val_a = a->addr & nmask;
150         val_b = b->addr & nmask;
151
152         return (val_a >= val_b);
153 }
154
155 gboolean
156 ipv4_addr_lt(ipv4_addr *a, ipv4_addr *b)
157 {
158         guint32 val_a, val_b, nmask;
159
160         nmask = MIN(a->nmask, b->nmask);
161         val_a = a->addr & nmask;
162         val_b = b->addr & nmask;
163
164         return (val_a < val_b);
165 }
166
167 gboolean
168 ipv4_addr_le(ipv4_addr *a, ipv4_addr *b)
169 {
170         guint32 val_a, val_b, nmask;
171
172         nmask = MIN(a->nmask, b->nmask);
173         val_a = a->addr & nmask;
174         val_b = b->addr & nmask;
175
176         return (val_a <= val_b);
177 }