diff options
Diffstat (limited to 'src/ip6.c')
-rw-r--r-- | src/ip6.c | 164 |
1 files changed, 90 insertions, 74 deletions
@@ -1,7 +1,7 @@ -#include "fmt.h" #include "byte.h" -#include "scan.h" +#include "fmt.h" #include "ip.h" +#include "scan.h" #include "str.h" /** @@ -19,7 +19,7 @@ @return int length of address (ok > 0) */ -unsigned int ip6_fmt(char *s,char ip[16]) +unsigned int ip6_fmt(char *s, char ip[16]) { unsigned int len; unsigned int i; @@ -34,17 +34,15 @@ unsigned int ip6_fmt(char *s,char ip[16]) for (j = 0; j < 16; j += 2) { if (j == 12 && ip6_isv4mapped(ip)) { - len += ip4_fmt(s,ip+12); + len += ip4_fmt(s, ip + 12); break; } - temp = ((unsigned long) (unsigned char) ip[j] << 8) + - (unsigned long) (unsigned char) ip[j+1]; + temp = ((unsigned long)(unsigned char)ip[j] << 8) + (unsigned long)(unsigned char)ip[j + 1]; temp0 = 0; if (!compressing && j < 16) - temp0 = ((unsigned long) (unsigned char) ip[j+2] << 8) + - (unsigned long) (unsigned char) ip[j+3]; + temp0 = ((unsigned long)(unsigned char)ip[j + 2] << 8) + (unsigned long)(unsigned char)ip[j + 3]; if (temp == 0 && temp0 == 0 && !compressed) { if (!compressing) { @@ -61,7 +59,7 @@ unsigned int ip6_fmt(char *s,char ip[16]) if (s) *s++ = ':'; ++len; } - i = fmt_xlong(s,temp); + i = fmt_xlong(s, temp); len += i; if (s) s += i; if (j < 14) { @@ -70,7 +68,10 @@ unsigned int ip6_fmt(char *s,char ip[16]) } } } - if (compressing) { *s++ = ':'; ++len; } + if (compressing) { + *s++ = ':'; + ++len; + } return len; } @@ -83,7 +84,7 @@ unsigned int ip6_fmt(char *s,char ip[16]) @return int length of address (ok > 0) */ -unsigned int ip6_fmt_flat(char *s,char ip[16]) +unsigned int ip6_fmt_flat(char *s, char ip[16]) { int i; for (i = 0; i < 16; i++) { @@ -102,7 +103,7 @@ unsigned int ip6_fmt_flat(char *s,char ip[16]) @return int length of address */ -unsigned int ia6_fmt(char *s,char ip[16]) +unsigned int ia6_fmt(char *s, char ip[16]) { unsigned int i; unsigned int len; @@ -112,12 +113,22 @@ unsigned int ia6_fmt(char *s,char ip[16]) len = 0; for (j = 15; j >= 0; j--) { - i = fmt_str(s,&data[ip[j] & 0x0f]); len += i; if (s) s += i; - i = fmt_str(s,"."); len += i; if (s) s += i; - i = fmt_str(s,&data[ip[j] >> 4 & 0x0f]); len += i; if (s) s += i; - i = fmt_str(s,"."); len += i; if (s) s += i; + i = fmt_str(s, &data[ip[j] & 0x0f]); + len += i; + if (s) s += i; + i = fmt_str(s, "."); + len += i; + if (s) s += i; + i = fmt_str(s, &data[ip[j] >> 4 & 0x0f]); + len += i; + if (s) s += i; + i = fmt_str(s, "."); + len += i; + if (s) s += i; } - i = fmt_str(s,"ip6.arpa."); len += i; if (s) s += i; + i = fmt_str(s, "ip6.arpa."); + len += i; + if (s) s += i; return len; } @@ -131,7 +142,7 @@ unsigned int ia6_fmt(char *s,char ip[16]) @return int length of address (ok > 0) */ -unsigned int ip6_scan_flat(const char *s,char ip[16]) +unsigned int ip6_scan_flat(const char *s, char ip[16]) { int i, tmp; @@ -154,7 +165,7 @@ unsigned int ip6_scan_flat(const char *s,char ip[16]) @return int length of ip6_address/ip */ -unsigned int ip6_scan(const char *s,char ip[16]) +unsigned int ip6_scan(const char *s, char ip[16]) { unsigned int i, j; unsigned int len = 0; @@ -164,61 +175,63 @@ unsigned int ip6_scan(const char *s,char ip[16]) int prefixlen = 0; int suffixlen = 0; - /* Always return IPv4 as IPv4-mapped IPv6 address */ - if ((i = ip4_scan(s,ip+12))) { - for (len = 0; len < 12; ++len) - ip[len] = V4mappedprefix[len]; + /* Always return IPv4 as IPv4-mapped IPv6 address */ + if ((i = ip4_scan(s, ip + 12))) { + for (len = 0; len < 12; ++len) ip[len] = V4mappedprefix[len]; return i; - if (byte_equal(ip+12,4,V4localnet)) { - byte_copy(ip,16,V6localnet); + if (byte_equal(ip + 12, 4, V4localnet)) { + byte_copy(ip, 16, V6localnet); return 16; } } - byte_zero(ip,16); + byte_zero(ip, 16); for (;;) { if (*s == ':') { len++; - if (s[1] == ':') { /* Found "::", skip to part 2 */ - s += 2; len++; + if (s[1] == ':') { /* Found "::", skip to part 2 */ + s += 2; + len++; break; } s++; } - i = scan_xlong((char *)s,&u); + i = scan_xlong((char *)s, &u); if (!i) return 0; if (prefixlen == 12 && s[i] == '.') { /* the last 4 bytes may be written as IPv4 address */ - i = ip4_scan(s,ip+12); - if (i) - return i+len; + i = ip4_scan(s, ip + 12); + if (i) + return i + len; else return 0; } ip[prefixlen++] = (u >> 8); ip[prefixlen++] = (u & 255); - s += i; len += i; + s += i; + len += i; if (prefixlen == 16) return len; } -/* part 2, after "::" */ + /* part 2, after "::" */ for (;;) { if (*s == ':') { if (suffixlen == 0) break; s++; len++; - } else if (suffixlen != 0) break; + } else if (suffixlen != 0) + break; - i = scan_xlong((char *)s,&u); + i = scan_xlong((char *)s, &u); if (!i) { len--; break; } if (suffixlen + prefixlen <= 12 && s[i] == '.') { - j = ip4_scan(s,suffix+suffixlen); + j = ip4_scan(s, suffix + suffixlen); if (j) { suffixlen += 4; len += j; @@ -229,12 +242,12 @@ unsigned int ip6_scan(const char *s,char ip[16]) suffix[suffixlen++] = (u >> 8); suffix[suffixlen++] = (u & 255); - s += i; len += i; + s += i; + len += i; if (prefixlen + suffixlen == 16) break; } - for (i = 0; i < suffixlen; i++) - ip[16 - suffixlen + i] = suffix[i]; + for (i = 0; i < suffixlen; i++) ip[16 - suffixlen + i] = suffix[i]; return len; } @@ -247,12 +260,12 @@ unsigned int ip6_scan(const char *s,char ip[16]) @return int length of ip_address (ok > 0) */ -unsigned int ip6_scanbracket(const char *s,char ip[16]) +unsigned int ip6_scanbracket(const char *s, char ip[16]) { unsigned int len; if (*s != '[') return 0; - len = ip6_scan(s + 1,ip); + len = ip6_scan(s + 1, ip); if (!len) return 0; if (s[len + 1] != ']') return 0; return len + 2; @@ -267,24 +280,27 @@ unsigned int ip6_scanbracket(const char *s,char ip[16]) @return int length of ip6_address/ip */ -unsigned int ip6_ifscan(char *s,char ip[16],stralloc *ifname) +unsigned int ip6_ifscan(char *s, char ip[16], stralloc *ifname) { - int i; + int i; int j = 0; - int k = 0; - if (!stralloc_copys(ifname,"0")) return 0; - - if ((j = str_chr(s,'%'))) { - if ((i = str_chr(s+j+1,' '))) k = i; - else if ((i = str_chr(s+j+1,'\n'))) k = i; - else if ((i = str_chr(s+j+1,'\t'))) k = i; - if (k) s[j+k+1] = '\0'; /* input might contain trailing chars */ - if (!stralloc_copys(ifname,s+j+1)) return 0; + int k = 0; + if (!stralloc_copys(ifname, "0")) return 0; + + if ((j = str_chr(s, '%'))) { + if ((i = str_chr(s + j + 1, ' '))) + k = i; + else if ((i = str_chr(s + j + 1, '\n'))) + k = i; + else if ((i = str_chr(s + j + 1, '\t'))) + k = i; + if (k) s[j + k + 1] = '\0'; /* input might contain trailing chars */ + if (!stralloc_copys(ifname, s + j + 1)) return 0; s[j] = 0; } if (!stralloc_0(ifname)) return 0; - return ip6_scan(s,ip); + return ip6_scan(s, ip); } /** @@ -296,17 +312,17 @@ unsigned int ip6_ifscan(char *s,char ip[16],stralloc *ifname) @return int length of ip6_address/ip */ -unsigned int ip6_cidr(char *s,char ip[16],unsigned long *plen) +unsigned int ip6_cidr(char *s, char ip[16], unsigned long *plen) { unsigned int j = 0; *plen = 128UL; - j = str_chr(s,'/'); + j = str_chr(s, '/'); if (s[j] == '/') { - s[j] = 0; - j = scan_ulong(s+j+1,plen); + s[j] = 0; + j = scan_ulong(s + j + 1, plen); } - return ip6_scan((const char *)s,ip); + return ip6_scan((const char *)s, ip); } /** @@ -317,39 +333,39 @@ unsigned int ip6_cidr(char *s,char ip[16],unsigned long *plen) @return n: number of bytes, if ok; -1: memory shortage */ -unsigned int ip6_bytestring(stralloc *ipstring,char ip[16],int prefix) +unsigned int ip6_bytestring(stralloc *ipstring, char ip[16], int prefix) { int i, j, n = 0; unsigned char lowbyte, highbyte; - if (!stralloc_readyplus(ipstring,128)) return -1; - if (!stralloc_copys(ipstring,"")) return -1; + if (!stralloc_readyplus(ipstring, 128)) return -1; + if (!stralloc_copys(ipstring, "")) return -1; for (i = 0; i < 16; i++) { - lowbyte = (unsigned char) (ip[i]) & 0x0f; - highbyte = (unsigned char) (ip[i] >> 4) & 0x0f; + lowbyte = (unsigned char)(ip[i]) & 0x0f; + highbyte = (unsigned char)(ip[i] >> 4) & 0x0f; for (j = 3; j >= 0; j--) { if (highbyte & (1 << j)) { - n++; - if (!stralloc_cats(ipstring,"1")) return -1; + n++; + if (!stralloc_cats(ipstring, "1")) return -1; } else { - n++; - if (!stralloc_cats(ipstring,"0")) return -1; + n++; + if (!stralloc_cats(ipstring, "0")) return -1; } prefix--; - if (!prefix) goto DONE; + if (!prefix) goto DONE; } for (j = 3; j >= 0; j--) { if (lowbyte & (1 << j)) { - n++; - if (!stralloc_cats(ipstring,"1")) return -1; + n++; + if (!stralloc_cats(ipstring, "1")) return -1; } else { - n++; - if (!stralloc_cats(ipstring,"0")) return -1; + n++; + if (!stralloc_cats(ipstring, "0")) return -1; } prefix--; - if (!prefix) goto DONE; + if (!prefix) goto DONE; } } |