ipv4_prefix_to_mask: enhanced to handle all possible prefixes

This commit is contained in:
Nikos Mavrogiannopoulos
2015-05-07 08:50:57 +02:00
parent 353e5018ef
commit acde2a1163
2 changed files with 17 additions and 35 deletions
+13 -22
View File
@@ -28,6 +28,8 @@
/* for recvmsg */
#include <netinet/in.h>
#include <netinet/ip.h>
/* for inet_ntop */
#include <arpa/inet.h>
#include "common.h"
@@ -266,28 +268,17 @@ int ip_cmp(const struct sockaddr_storage *s1, const struct sockaddr_storage *s2)
*/
char* ipv4_prefix_to_mask(void *pool, unsigned prefix)
{
switch (prefix) {
case 8:
return talloc_strdup(pool, "255.0.0.0");
case 16:
return talloc_strdup(pool, "255.255.0.0");
case 24:
return talloc_strdup(pool, "255.255.255.0");
case 25:
return talloc_strdup(pool, "255.255.255.128");
case 26:
return talloc_strdup(pool, "255.255.255.192");
case 27:
return talloc_strdup(pool, "255.255.255.224");
case 28:
return talloc_strdup(pool, "255.255.255.240");
case 29:
return talloc_strdup(pool, "255.255.255.248");
case 30:
return talloc_strdup(pool, "255.255.255.252");
default:
return NULL;
}
struct in_addr in;
char str[MAX_IP_STR];
if (prefix == 0)
return NULL;
in.s_addr = ntohl(((uint32_t)0xFFFFFFFF) << (32 - prefix));
if (inet_ntop(AF_INET, &in, str, sizeof(str)) == NULL)
return NULL;
return talloc_strdup(pool, str);
}
/* Sends message + socketfd */
+4 -13
View File
@@ -48,19 +48,10 @@ int ip_cmp(const struct sockaddr_storage *s1, const struct sockaddr_storage *s2)
char* ipv4_prefix_to_mask(void *pool, unsigned prefix);
inline static int valid_ipv6_prefix(unsigned prefix)
{
switch (prefix) {
case 16:
case 32:
case 48:
case 64:
case 80:
case 96:
case 112:
case 128:
return 1;
default:
return 0;
}
if (prefix > 10 && prefix <= 128)
return 1;
else
return 0;
}
typedef size_t (*pack_func)(const void*, uint8_t *);