2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 static void test_addr(void) {
25 const struct sockaddr_in *sin4;
26 struct sockaddr_in s4;
27 struct sockaddr_un su;
29 static const struct addrinfo pref = {
30 .ai_flags = AI_PASSIVE,
32 .ai_socktype = SOCK_STREAM,
35 struct sockaddr_in a1 = {
36 .sin_family = AF_INET,
37 .sin_port = ntohs(25),
38 .sin_addr = { .s_addr = 0}
40 struct addrinfo p1 = {
42 .ai_socktype = SOCK_STREAM,
43 .ai_protocol = IPPROTO_TCP,
44 .ai_addrlen = sizeof a1,
45 .ai_addr = (struct sockaddr *)&a1,
48 struct sockaddr_in a2 = {
49 .sin_family = AF_INET,
50 .sin_port = ntohs(119),
51 .sin_addr = { .s_addr = htonl(0x7F000001) }
53 struct addrinfo p2 = {
55 .ai_socktype = SOCK_STREAM,
56 .ai_protocol = IPPROTO_TCP,
57 .ai_addrlen = sizeof a2,
58 .ai_addr = (struct sockaddr *)&a2,
61 insist(addrinfocmp(&p1, &p2) < 0);
66 ai = get_address(&a, &pref, &name);
68 check_integer(ai->ai_family, PF_INET);
69 check_integer(ai->ai_socktype, SOCK_STREAM);
70 check_integer(ai->ai_protocol, IPPROTO_TCP);
71 check_integer(ai->ai_addrlen, sizeof(struct sockaddr_in));
72 sin4 = (const struct sockaddr_in *)ai->ai_addr;
73 check_integer(sin4->sin_family, AF_INET);
74 check_integer(sin4->sin_addr.s_addr, 0);
75 check_integer(ntohs(sin4->sin_port), 25);
76 check_string(name, "host * service smtp");
77 insist(addrinfocmp(ai, &p1) == 0);
78 insist(addrinfocmp(ai, &p2) < 0);
83 ai = get_address(&a, &pref, &name);
85 check_integer(ai->ai_family, PF_INET);
86 check_integer(ai->ai_socktype, SOCK_STREAM);
87 check_integer(ai->ai_protocol, IPPROTO_TCP);
88 check_integer(ai->ai_addrlen, sizeof(struct sockaddr_in));
89 sin4 = (const struct sockaddr_in *)ai->ai_addr;
90 check_integer(sin4->sin_family, AF_INET);
91 check_integer(ntohl(sin4->sin_addr.s_addr), 0x7F000001);
92 check_integer(ntohs(sin4->sin_port), 119);
93 check_string(name, "host localhost service nntp");
94 insist(addrinfocmp(ai, &p2) == 0);
95 insist(addrinfocmp(ai, &p1) > 0);
98 s[0] = "no.such.domain.really.i.mean.it.greenend.org.uk";
100 insist(get_address(&a, &pref, &name) == 0);
106 insist(get_address(&a, &pref, &name) == 0);
108 memset(&s4, 0, sizeof s4);
109 s4.sin_family = AF_INET;
110 s4.sin_addr.s_addr = 0;
112 check_string(format_sockaddr((struct sockaddr *)&s4),
114 check_integer(multicast((struct sockaddr *)&s4), 0);
115 s4.sin_addr.s_addr = htonl(0x7F000001);
116 s4.sin_port = htons(1000);
117 check_string(format_sockaddr((struct sockaddr *)&s4),
118 "127.0.0.1 port 1000");
119 check_integer(multicast((struct sockaddr *)&s4), 0);
120 s4.sin_addr.s_addr = htonl(0xE0000001);
121 check_string(format_sockaddr((struct sockaddr *)&s4),
122 "224.0.0.1 port 1000");
123 check_integer(multicast((struct sockaddr *)&s4), 1);
125 memset(&su, 0, sizeof su);
126 su.sun_family = AF_UNIX;
127 strcpy(su.sun_path, "/wibble/wobble");
128 check_string(format_sockaddr((struct sockaddr *)&su),
130 check_integer(multicast((struct sockaddr *)&su), 0);