Move bitmask array from ipv4_addr_set_netmask_bits() to ip_get_subnet_mask()

Use it in subnet_mask_lookup_init().

svn path=/trunk/; revision=50527
This commit is contained in:
Jakub Zawadzki 2013-07-12 05:59:56 +00:00
parent 4dcc156cf3
commit 7c9c79ca68
4 changed files with 26 additions and 77 deletions

View File

@ -31,6 +31,26 @@
#include "tvbuff.h"
#include "addr_and_mask.h"
guint32
ip_get_subnet_mask(const guint32 mask_length)
{
static guint32 masks[33] = {
0x00000000,
0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff,
};
g_assert(mask_length <= 32);
return masks[mask_length];
}
/*
* These routines return the length of the address in bytes on success
* and -1 if the prefix length is too long.

View File

@ -46,6 +46,8 @@ extern int ipv4_addr_and_mask(tvbuff_t *tvb, int offset, guint8 *addr,
extern int ipv6_addr_and_mask(tvbuff_t *tvb, int offset,
struct e_in6_addr *addr, guint32 prefix_len);
guint32 ip_get_subnet_mask(const guint32 mask_length);
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -120,6 +120,7 @@
#include "report_err.h"
#include "packet.h"
#include "addr_and_mask.h"
#include "ipv6-utils.h"
#include "addr_resolv.h"
#include "filesystem.h"
@ -2301,67 +2302,6 @@ subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* na
have_subnet_entry = TRUE;
}
static guint32
get_subnet_mask(const guint32 mask_length) {
static guint32 masks[SUBNETLENGTHSIZE];
static gboolean initialised = FALSE;
if(!initialised) {
memset(masks, 0, sizeof(masks));
initialised = TRUE;
/* XXX There must be a better way to do this than
* hand-coding the values, but I can't seem to
* come up with one!
*/
inet_pton(AF_INET, "128.0.0.0", &masks[0]);
inet_pton(AF_INET, "192.0.0.0", &masks[1]);
inet_pton(AF_INET, "224.0.0.0", &masks[2]);
inet_pton(AF_INET, "240.0.0.0", &masks[3]);
inet_pton(AF_INET, "248.0.0.0", &masks[4]);
inet_pton(AF_INET, "252.0.0.0", &masks[5]);
inet_pton(AF_INET, "254.0.0.0", &masks[6]);
inet_pton(AF_INET, "255.0.0.0", &masks[7]);
inet_pton(AF_INET, "255.128.0.0", &masks[8]);
inet_pton(AF_INET, "255.192.0.0", &masks[9]);
inet_pton(AF_INET, "255.224.0.0", &masks[10]);
inet_pton(AF_INET, "255.240.0.0", &masks[11]);
inet_pton(AF_INET, "255.248.0.0", &masks[12]);
inet_pton(AF_INET, "255.252.0.0", &masks[13]);
inet_pton(AF_INET, "255.254.0.0", &masks[14]);
inet_pton(AF_INET, "255.255.0.0", &masks[15]);
inet_pton(AF_INET, "255.255.128.0", &masks[16]);
inet_pton(AF_INET, "255.255.192.0", &masks[17]);
inet_pton(AF_INET, "255.255.224.0", &masks[18]);
inet_pton(AF_INET, "255.255.240.0", &masks[19]);
inet_pton(AF_INET, "255.255.248.0", &masks[20]);
inet_pton(AF_INET, "255.255.252.0", &masks[21]);
inet_pton(AF_INET, "255.255.254.0", &masks[22]);
inet_pton(AF_INET, "255.255.255.0", &masks[23]);
inet_pton(AF_INET, "255.255.255.128", &masks[24]);
inet_pton(AF_INET, "255.255.255.192", &masks[25]);
inet_pton(AF_INET, "255.255.255.224", &masks[26]);
inet_pton(AF_INET, "255.255.255.240", &masks[27]);
inet_pton(AF_INET, "255.255.255.248", &masks[28]);
inet_pton(AF_INET, "255.255.255.252", &masks[29]);
inet_pton(AF_INET, "255.255.255.254", &masks[30]);
inet_pton(AF_INET, "255.255.255.255", &masks[31]);
}
if(mask_length == 0 || mask_length > SUBNETLENGTHSIZE) {
g_assert_not_reached();
return 0;
} else {
return masks[mask_length - 1];
}
}
static void
subnet_name_lookup_init(void)
{
@ -2373,7 +2313,7 @@ subnet_name_lookup_init(void)
subnet_length_entries[i].subnet_addresses = NULL;
subnet_length_entries[i].mask_length = length;
subnet_length_entries[i].mask = get_subnet_mask(length);
subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
}
subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);

View File

@ -33,6 +33,7 @@
#include "ipv4.h"
#include "packet.h" /* for ip_to_str */
#include "addr_and_mask.h"
ipv4_addr*
@ -65,21 +66,7 @@ ipv4_addr_set_net_order_addr(ipv4_addr *ipv4, const guint32 new_addr)
void
ipv4_addr_set_netmask_bits(ipv4_addr *ipv4, const guint new_nmask_bits)
{
static guint32 bitmasks[33] = {
0x00000000,
0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff,
};
g_assert(new_nmask_bits <= 32);
ipv4->nmask = bitmasks[new_nmask_bits];
ipv4->nmask = ip_get_subnet_mask(new_nmask_bits);
}
guint32