1570af302Sopenharmony_ci// zero compression for the last field in an ipv6 address is (probably) allowed
2570af302Sopenharmony_ci// https://tools.ietf.org/html/rfc4291#section-2.2
3570af302Sopenharmony_ci// but further fields shouldnt buffer overflow
4570af302Sopenharmony_ci#include <stdio.h>
5570af302Sopenharmony_ci#include <string.h>
6570af302Sopenharmony_ci#include <arpa/inet.h>
7570af302Sopenharmony_ci#include "test.h"
8570af302Sopenharmony_ci
9570af302Sopenharmony_cistatic void txt(char *s, unsigned char *buf)
10570af302Sopenharmony_ci{
11570af302Sopenharmony_ci	int i;
12570af302Sopenharmony_ci	sprintf(s, "%04x", buf[0]<<8 | buf[1]);
13570af302Sopenharmony_ci	for (i=1; i<8; i++)
14570af302Sopenharmony_ci		sprintf(s+5*i, ":%04x", buf[2*i]<<8 | buf[2*i+1]);
15570af302Sopenharmony_ci}
16570af302Sopenharmony_ci
17570af302Sopenharmony_ciint main(void)
18570af302Sopenharmony_ci{
19570af302Sopenharmony_ci	char s[50], sw[50];
20570af302Sopenharmony_ci	unsigned char buf[16];
21570af302Sopenharmony_ci	unsigned char want[16] = {0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,0};
22570af302Sopenharmony_ci	char *addr;
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci	addr = "1:2:3:4:5:6:7::";
25570af302Sopenharmony_ci	if (inet_pton(AF_INET6, addr, buf)!=1 || memcmp(buf, want, 16)!=0) {
26570af302Sopenharmony_ci		txt(s, buf);
27570af302Sopenharmony_ci		txt(sw, want);
28570af302Sopenharmony_ci		t_error("inet_pton(%s) returned %s, wanted %s\n",
29570af302Sopenharmony_ci			addr, s, sw);
30570af302Sopenharmony_ci	}
31570af302Sopenharmony_ci
32570af302Sopenharmony_ci	addr = "1:2:3:4:5:6:7::9:10:11:12:13:14:15:16:17:18:19:20";
33570af302Sopenharmony_ci	if (inet_pton(AF_INET6, addr, buf)!=0) {
34570af302Sopenharmony_ci		txt(s, buf);
35570af302Sopenharmony_ci		t_error("inet_pton(%s) returned %s, wanted a failure\n",
36570af302Sopenharmony_ci			addr, s);
37570af302Sopenharmony_ci	}
38570af302Sopenharmony_ci	return t_status;
39570af302Sopenharmony_ci}
40