forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_inet_pton.c
42 lines (34 loc) · 1.36 KB
/
test_inet_pton.c
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
#include "../lib/unp.h"
#ifndef AF_INET6
#define AF_INET6 AF_MAX+1 /* just to let this compile */
#endif
int inet_pton(int, const char *, void *);
int
main(int argc, char **argv)
{
int i;
char buff[100];
/*
* Make certain that we can test the difference between 0.0.0.0
* being acceptable for AF_INET (but not acceptable for AF_INET6)
* and 0::0 being OK for AF_INET6 (but not OK for AF_INET).
* This way a server can be coded as protocol independent (IPv4 or
* IPv6) but let the user specify the local IP address as either
* 0.0.0.0 or 0::0 as an indirect way of telling the server when
* it starts, which protocol to use (but still allowing the server
* to bind the wildcard address).
*/
if ( (i = inet_pton(AF_INET, "0.0.0.0", buff)) != 1) /* should be OK */
printf("AF_INET, 0.0.0.0 returned: %d\n", i);
if ( (i = inet_pton(AF_INET6, "0.0.0.0", buff)) != 0)
printf("AF_INET6, 0.0.0.0 returned: %d\n", i);
if ( (i = inet_pton(AF_INET6, "0::0", buff)) != 1) /* should be OK */
printf("AF_INET6, 0::0 returned: %d\n", i);
if ( (i = inet_pton(AF_INET, "0::0", buff)) != 0)
printf("AF_INET, 0::0 returned: %d\n", i);
printf("inet_pton(AF_INET6, \"1.2.3.4\", buff) returns %d\n",
inet_pton(AF_INET6, "1.2.3.4", buff));
printf("inet_pton(AF_INET6, \"::1.2.3.4\", buff) returns %d\n",
inet_pton(AF_INET6, "::1.2.3.4", buff));
exit(0);
}