enhanced ipv6_prefix_to_mask

This commit is contained in:
Nikos Mavrogiannopoulos
2015-05-07 09:29:18 +02:00
parent 689c5ad563
commit 39ac437727
3 changed files with 25 additions and 37 deletions
+24 -1
View File
@@ -271,7 +271,7 @@ char* ipv4_prefix_to_mask(void *pool, unsigned prefix)
struct in_addr in;
char str[MAX_IP_STR];
if (prefix == 0)
if (prefix == 0 || prefix > 32)
return NULL;
in.s_addr = ntohl(((uint32_t)0xFFFFFFFF) << (32 - prefix));
@@ -281,6 +281,29 @@ char* ipv4_prefix_to_mask(void *pool, unsigned prefix)
return talloc_strdup(pool, str);
}
char* ipv6_prefix_to_mask(char buf[MAX_IP_STR], unsigned prefix)
{
struct in6_addr in6;
int i, j;
if (prefix == 0 || prefix > 128)
return NULL;
memset(&in6, 0x0, sizeof(in6));
for (i = prefix, j = 0; i > 0; i -= 8, j++) {
if (i >= 8) {
in6.s6_addr[j] = 0xff;
} else {
in6.s6_addr[j] = (unsigned long)(0xffU << ( 8 - i ));
}
}
if (inet_ntop(AF_INET6, &in6, buf, MAX_IP_STR) == NULL)
return NULL;
return buf;
}
/* Sends message + socketfd */
int send_socket_msg(void *pool, int fd, uint8_t cmd,
int socketfd,
+1
View File
@@ -46,6 +46,7 @@ ssize_t force_read_timeout(int sockfd, void *buf, size_t len, unsigned sec);
ssize_t recv_timeout(int sockfd, void *buf, size_t len, unsigned sec);
int ip_cmp(const struct sockaddr_storage *s1, const struct sockaddr_storage *s2);
char* ipv4_prefix_to_mask(void *pool, unsigned prefix);
char* ipv6_prefix_to_mask(char buf[MAX_IP_STR], unsigned prefix);
inline static int valid_ipv6_prefix(unsigned prefix)
{
if (prefix > 10 && prefix <= 128)
-36
View File
@@ -303,42 +303,6 @@ fail:
return ret;
}
/* returns an allocated string with the mask to apply for the prefix
*/
static
char* ipv6_prefix_to_mask(char buf[MAX_IP_STR], unsigned prefix)
{
switch (prefix) {
case 16:
strlcpy(buf, "ffff::", MAX_IP_STR);
break;
case 32:
strlcpy(buf, "ffff:ffff::", MAX_IP_STR);
break;
case 48:
strlcpy(buf, "ffff:ffff:ffff::", MAX_IP_STR);
break;
case 64:
strlcpy(buf, "ffff:ffff:ffff:ffff::", MAX_IP_STR);
break;
case 80:
strlcpy(buf, "ffff:ffff:ffff:ffff:ffff::", MAX_IP_STR);
break;
case 96:
strlcpy(buf, "ffff:ffff:ffff:ffff:ffff:ffff::", MAX_IP_STR);
break;
case 112:
strlcpy(buf, "ffff:ffff:ffff:ffff:ffff:ffff:ffff::", MAX_IP_STR);
break;
case 128:
strlcpy(buf, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", MAX_IP_STR);
break;
default:
return NULL;
}
return buf;
}
static
int get_ipv6_lease(main_server_st* s, struct proc_st* proc)
{