-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip6.cpp
60 lines (54 loc) · 1.02 KB
/
ip6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "ip6.h"
#include "addr.h"
#include "ip.h"
#include <stdlib.h>
int
ip6_pton(const char *p, ip6_addr_t *ip6)
{
uint16_t data[8], *u = (uint16_t *)ip6->data;
int i, j, n, z = -1;
char *ep;
long l;
if (*p == ':')
p++;
for (n = 0; n < 8; n++) {
l = strtol(p, &ep, 16);
if (ep == p) {
if (ep[0] == ':' && z == -1) {
z = n;
p++;
} else if (ep[0] == '\0') {
break;
} else {
return (-1);
}
} else if (ep[0] == '.' && n <= 6) {
if (ip_pton(p, (ip_addr_t *)(data + n)) < 0)
return (-1);
n += 2;
ep = ""; /* XXX */
break;
} else if (l >= 0 && l <= 0xffff) {
data[n] = htons((uint16_t)l);
if (ep[0] == '\0') {
n++;
break;
} else if (ep[0] != ':' || ep[1] == '\0')
return (-1);
p = ep + 1;
} else
return (-1);
}
if (n == 0 || *ep != '\0' || (z == -1 && n != 8))
return (-1);
for (i = 0; i < z; i++) {
u[i] = data[i];
}
while (i < 8 - (n - z - 1)) {
u[i++] = 0;
}
for (j = z + 1; i < 8; i++, j++) {
u[i] = data[j];
}
return (0);
}