diff options
author | Mike Belopuhov <mikeb@cvs.openbsd.org> | 2011-01-17 17:16:44 +0000 |
---|---|---|
committer | Mike Belopuhov <mikeb@cvs.openbsd.org> | 2011-01-17 17:16:44 +0000 |
commit | d427546c43cc129b4ae15ba4603fe445e5b009c2 (patch) | |
tree | 74c484dd266a0dfd3b4d3cb85f63f9c45d4bfe48 | |
parent | b4594c9759476528df1b498b3b717500416aeb4a (diff) |
move mask2prefixlen functions to the util module; ok reyk
-rw-r--r-- | sbin/iked/iked.h | 4 | ||||
-rw-r--r-- | sbin/iked/parse.y | 67 | ||||
-rw-r--r-- | sbin/iked/util.c | 63 |
3 files changed, 68 insertions, 66 deletions
diff --git a/sbin/iked/iked.h b/sbin/iked/iked.h index 4bc56ab642c..7ce4f439465 100644 --- a/sbin/iked/iked.h +++ b/sbin/iked/iked.h @@ -1,4 +1,4 @@ -/* $OpenBSD: iked.h,v 1.27 2011/01/12 14:35:45 mikeb Exp $ */ +/* $OpenBSD: iked.h,v 1.28 2011/01/17 17:16:43 mikeb Exp $ */ /* $vantronix: iked.h,v 1.61 2010/06/03 07:57:33 reyk Exp $ */ /* @@ -737,6 +737,8 @@ void print_hexval(u_int8_t *, off_t, size_t); const char * print_bits(u_short, char *); int sockaddr_cmp(struct sockaddr *, struct sockaddr *, int); +u_int8_t mask2prefixlen(struct sockaddr *); +u_int8_t mask2prefixlen6(struct sockaddr *); struct in6_addr * prefixlen2mask6(u_int8_t, u_int32_t *); u_int32_t diff --git a/sbin/iked/parse.y b/sbin/iked/parse.y index 1efb79feb74..e9b34fb67cb 100644 --- a/sbin/iked/parse.y +++ b/sbin/iked/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.18 2010/12/23 16:39:38 mikeb Exp $ */ +/* $OpenBSD: parse.y,v 1.19 2011/01/17 17:16:43 mikeb Exp $ */ /* $vantronix: parse.y,v 1.22 2010/06/03 11:08:34 reyk Exp $ */ /* @@ -271,8 +271,6 @@ struct ipsec_addr_wrap *host_v4(const char *, int); struct ipsec_addr_wrap *host_dns(const char *, int); struct ipsec_addr_wrap *host_if(const char *, int); struct ipsec_addr_wrap *host_any(void); -u_int8_t mask2prefixlen(struct sockaddr_in *); -u_int8_t mask2prefixlen6(struct sockaddr_in6 *); void ifa_load(void); int ifa_exists(const char *); struct ipsec_addr_wrap *ifa_lookup(const char *ifa_name); @@ -1793,65 +1791,6 @@ host_any(void) return (ipa); } -u_int8_t -mask2prefixlen(struct sockaddr_in *sa_in) -{ - in_addr_t ina = sa_in->sin_addr.s_addr; - - if (ina == 0) - return (0); - else - return (33 - ffs(ntohl(ina))); -} - -u_int8_t -mask2prefixlen6(struct sockaddr_in6 *sa_in6) -{ - u_int8_t l = 0, *ap, *ep; - - /* - * sin6_len is the size of the sockaddr so substract the offset of - * the possibly truncated sin6_addr struct. - */ - ap = (u_int8_t *)&sa_in6->sin6_addr; - ep = (u_int8_t *)sa_in6 + sa_in6->sin6_len; - for (; ap < ep; ap++) { - /* this "beauty" is adopted from sbin/route/show.c ... */ - switch (*ap) { - case 0xff: - l += 8; - break; - case 0xfe: - l += 7; - return (l); - case 0xfc: - l += 6; - return (l); - case 0xf8: - l += 5; - return (l); - case 0xf0: - l += 4; - return (l); - case 0xe0: - l += 3; - return (l); - case 0xc0: - l += 2; - return (l); - case 0x80: - l += 1; - return (l); - case 0x00: - return (l); - default: - fatalx("non continguous inet6 netmask"); - } - } - - return (l); -} - /* interface lookup routintes */ struct ipsec_addr_wrap *iftab; @@ -1882,12 +1821,12 @@ ifa_load(void) sa_in = (struct sockaddr_in *)ifa->ifa_addr; memcpy(&n->address, sa_in, sizeof(*sa_in)); sa_in = (struct sockaddr_in *)ifa->ifa_netmask; - n->mask = mask2prefixlen(sa_in); + n->mask = mask2prefixlen((struct sockaddr *)sa_in); } else if (n->af == AF_INET6) { sa_in6 = (struct sockaddr_in6 *)ifa->ifa_addr; memcpy(&n->address, sa_in6, sizeof(*sa_in6)); sa_in6 = (struct sockaddr_in6 *)ifa->ifa_netmask; - n->mask = mask2prefixlen6(sa_in6); + n->mask = mask2prefixlen6((struct sockaddr *)sa_in6); } n->next = NULL; n->tail = n; diff --git a/sbin/iked/util.c b/sbin/iked/util.c index 863692ae0dd..77c2e911c41 100644 --- a/sbin/iked/util.c +++ b/sbin/iked/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.10 2010/12/22 17:53:54 reyk Exp $ */ +/* $OpenBSD: util.c,v 1.11 2011/01/17 17:16:43 mikeb Exp $ */ /* $vantronix: util.c,v 1.39 2010/06/02 12:22:58 reyk Exp $ */ /* @@ -482,6 +482,67 @@ print_bits(u_short v, char *bits) return (buf[idx]); } +u_int8_t +mask2prefixlen(struct sockaddr *sa) +{ + struct sockaddr_in *sa_in = (struct sockaddr_in *)sa; + in_addr_t ina = sa_in->sin_addr.s_addr; + + if (ina == 0) + return (0); + else + return (33 - ffs(ntohl(ina))); +} + +u_int8_t +mask2prefixlen6(struct sockaddr *sa) +{ + struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)sa; + u_int8_t l = 0, *ap, *ep; + + /* + * sin6_len is the size of the sockaddr so substract the offset of + * the possibly truncated sin6_addr struct. + */ + ap = (u_int8_t *)&sa_in6->sin6_addr; + ep = (u_int8_t *)sa_in6 + sa_in6->sin6_len; + for (; ap < ep; ap++) { + /* this "beauty" is adopted from sbin/route/show.c ... */ + switch (*ap) { + case 0xff: + l += 8; + break; + case 0xfe: + l += 7; + return (l); + case 0xfc: + l += 6; + return (l); + case 0xf8: + l += 5; + return (l); + case 0xf0: + l += 4; + return (l); + case 0xe0: + l += 3; + return (l); + case 0xc0: + l += 2; + return (l); + case 0x80: + l += 1; + return (l); + case 0x00: + return (l); + default: + return (0); + } + } + + return (l); +} + u_int32_t prefixlen2mask(u_int8_t prefixlen) { |