From Michal Labedzki via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9333 :
[metze/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27  */
28
29 #include "config.h"
30
31 #include <glib.h>
32 #include <stdio.h>
33
34 #include "ipv4.h"
35 #include "packet.h" /* for ip_to_str */
36 #include "addr_and_mask.h"
37
38
39 ipv4_addr*
40 ipv4_addr_new(void)
41 {
42         ipv4_addr       *ipv4;
43
44         ipv4 = g_new(ipv4_addr, 1);
45         return ipv4;
46 }
47
48 void
49 ipv4_addr_free(ipv4_addr *ipv4)
50 {
51         g_free(ipv4);
52 }
53
54 void
55 ipv4_addr_set_host_order_addr(ipv4_addr *ipv4, const guint32 new_addr)
56 {
57         ipv4->addr = new_addr;
58 }
59
60 void
61 ipv4_addr_set_net_order_addr(ipv4_addr *ipv4, const guint32 new_addr)
62 {
63         ipv4->addr = g_ntohl(new_addr);
64 }
65
66 void
67 ipv4_addr_set_netmask_bits(ipv4_addr *ipv4, const guint new_nmask_bits)
68 {
69         ipv4->nmask = ip_get_subnet_mask(new_nmask_bits);
70 }
71
72 guint32
73 ipv4_get_net_order_addr(ipv4_addr *ipv4)
74 {
75         return g_htonl(ipv4->addr);
76 }
77
78 guint32
79 ipv4_get_host_order_addr(ipv4_addr *ipv4)
80 {
81         return ipv4->addr;
82 }
83
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)
87 {
88         guint32 ipv4_host_order = g_htonl(ipv4->addr);
89         ip_to_str_buf((guint8*)&ipv4_host_order, buf, MAX_IP_STR_LEN);
90 }
91
92
93
94 /*
95  * w.x.y.z/32 eq w.x.y.0/24    TRUE
96  */
97
98 /* Returns TRUE if equal, FALSE if not */
99 gboolean
100 ipv4_addr_eq(const ipv4_addr *a, const ipv4_addr *b)
101 {
102         guint32 val_a, val_b, nmask;
103
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);
108 }
109
110 gboolean
111 ipv4_addr_gt(const ipv4_addr *a, const ipv4_addr *b)
112 {
113         guint32 val_a, val_b, nmask;
114
115         nmask = MIN(a->nmask, b->nmask);
116         val_a = a->addr & nmask;
117         val_b = b->addr & nmask;
118
119         return (val_a > val_b);
120 }
121
122 gboolean
123 ipv4_addr_ge(const ipv4_addr *a, const ipv4_addr *b)
124 {
125         guint32 val_a, val_b, nmask;
126
127         nmask = MIN(a->nmask, b->nmask);
128         val_a = a->addr & nmask;
129         val_b = b->addr & nmask;
130
131         return (val_a >= val_b);
132 }
133
134 gboolean
135 ipv4_addr_lt(const ipv4_addr *a, const 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_le(const ipv4_addr *a, const 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 }