ucspi-ssl  0.99e
TLS encryption for IPv6 communication
ip6_compactaddr.c
Go to the documentation of this file.
1 #include "fmt.h"
2 #include "byte.h"
3 #include "ip4.h"
4 #include "ip6.h"
5 #include "hexconversion.h"
6 
15 unsigned int ip6_compactaddr(char *s,char ip[16])
16 {
17  unsigned long len,temp, k, pos0 = 0,len0 = 0, pos1 = 0, compr = 0;
18 // const unsigned char V4mappedprefix[12] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff};
19 
20  for (k=0; k<16; k+=2) {
21  if (ip[k] == 0 && ip[k+1] == 0) {
22  if (!compr) {
23  compr=1;
24  pos1=k;
25  }
26  if (k == 14) { k = 16; goto last; }
27  } else if (compr) {
28  last:
29  if ((temp=k-pos1) > len0) {
30  len0 = temp;
31  pos0 = pos1;
32  }
33  compr=0;
34  }
35  }
36 
37  for (len=0,k=0; k<16; k+=2) {
38  if (k == 12 && (byte_equal(ip,12,V4mappedprefix))) {
39  len += ip4_fmt(s,ip+12);
40  break;
41  }
42  if (pos0 == k && len0) {
43  if (k == 0) { ++len; if (s) *s++ = ':'; }
44  ++len; if (s) *s++ = ':';
45  k += len0-2;
46  continue;
47  }
48  temp = ((unsigned long) (unsigned char) ip[k] << 8) +
49  (unsigned long) (unsigned char) ip[k+1];
50  temp = fmt_xlong(s,temp); len += temp; if (s) s += temp;
51  if (k<14) { ++len; if (s) *s++ = ':'; }
52  }
53 
54  return len;
55 }
unsigned int fmt_xlong(char *, unsigned long)
unsigned int ip4_fmt(char *, const char *)
const unsigned char V4mappedprefix[12]
unsigned int ip6_compactaddr(char *s, char ip[16])
#define byte_equal(s, n, t)
Definition: byte.h:13