]>
git.saurik.com Git - apple/libc.git/blob - tests/net.c
2 #include <sys/socket.h>
4 #include <netinet/in.h>
8 #include <darwintest.h>
10 T_DECL(link_ntoa_basic
, "link_ntoa converts to proper string")
12 // u_char sdl_len; /* Total length of sockaddr */
13 // u_char sdl_family; /* AF_LINK */
14 // u_short sdl_index; /* if != 0, system given index for interface */
15 // u_char sdl_type; /* interface type */
16 // u_char sdl_nlen; /* interface name length, no trailing 0 reqd. */
17 // u_char sdl_alen; /* link level address length */
18 // u_char sdl_slen; /* link layer selector length */
19 // char sdl_data[12]; /* minimum work area, can be larger;
20 // contains both if name and ll address */
22 struct sockaddr_dl sad
;
23 bzero(&sad
, sizeof(sad
));
26 sad
.sdl_data
[0] = 'l';
27 sad
.sdl_data
[1] = 'e';
28 sad
.sdl_data
[2] = '0';
29 sad
.sdl_data
[3] = 0x01;
30 sad
.sdl_data
[4] = 0x80;
31 sad
.sdl_data
[5] = 0xc2;
32 sad
.sdl_data
[6] = 0x00;
33 sad
.sdl_data
[7] = 0x00;
34 sad
.sdl_data
[8] = 0x02;
35 sad
.sdl_data
[9] = 0xaa;
36 sad
.sdl_data
[10] = 0xbb;
37 sad
.sdl_data
[11] = 0xcc;
40 char *foo
= link_ntoa(&sad
);
42 T_EXPECT_EQ_STR("le0:1.80.c2.0.0.2", foo
, NULL
);
45 T_DECL(link_ntoa_overflow
, "link_ntoa try to overflow")
48 struct sockaddr_dl
*sad
;
49 sad
= (struct sockaddr_dl
*)&sockraw
;
50 bzero(sad
, sizeof(*sad
));
53 sad
->sdl_data
[0] = 'l';
54 sad
->sdl_data
[1] = 'e';
55 sad
->sdl_data
[2] = '0';
56 sad
->sdl_data
[3] = 0x11;
57 sad
->sdl_data
[4] = 0x80;
58 sad
->sdl_data
[5] = 0xc2;
59 sad
->sdl_data
[6] = 0x11;
60 sad
->sdl_data
[7] = 0x11;
61 sad
->sdl_data
[8] = 0xa2;
62 sad
->sdl_data
[9] = 0xaa;
63 sad
->sdl_data
[10] = 0xbb;
64 sad
->sdl_data
[11] = 0xcc;
78 /* set the length to something that will fit in the buffer */
81 char *foo
= link_ntoa(sad
);
86 /* this string should be 66 bytes long and exceed the buffer */
91 T_EXPECT_EQ_STR("", foo
, NULL
);
93 T_EXPECT_EQ(over
, foo
[64], "did not overflow");
94 T_EXPECT_EQ(over2
, foo
[65], "did not overflow either");
97 T_DECL(inet_ntop
, "inet_ntop")
99 char *addresses4
[] = { "1.2.3.4", "10.0.0.1", "2.2.2.2" };
100 char *addresses6
[] = {
101 "2001:db8:85a3::8a2e:370:7334",
104 for (int i
= 0; i
< sizeof(addresses4
)/sizeof(addresses4
[0]); i
++){
105 struct in_addr addr4
;
107 T_EXPECT_EQ(inet_pton(AF_INET
, addresses4
[i
], &addr4
), 1, "inet_pton(AF_INET, %s)", addresses4
[i
]);
108 char *str
= inet_ntop(AF_INET
, &addr4
, buf
, sizeof(buf
));
109 T_EXPECT_NOTNULL(str
, "inet_ntop(AF_INET) of %s", addresses4
[i
]);
110 T_EXPECT_EQ_STR(str
, addresses4
[i
], "round-trip of %s", addresses4
[i
]);
112 for (int i
= 0; i
< sizeof(addresses6
)/sizeof(addresses6
[0]); i
++){
113 struct in6_addr addr6
;
115 T_EXPECT_EQ(inet_pton(AF_INET6
, addresses6
[i
], &addr6
), 1, "inet_pton(AF_INET6, %s)", addresses6
[i
]);
116 char *str
= inet_ntop(AF_INET6
, &addr6
, buf
, sizeof(buf
));
117 T_EXPECT_NOTNULL(str
, "inet_ntop(AF_INET) of %s", addresses6
[i
]);
118 T_EXPECT_EQ_STR(str
, addresses6
[i
], "round-trip of %s", addresses6
[i
]);