7 /* determine all host internet interface addresses
9 /* #include <inet_addr_host.h>
11 /* int inet_addr_host(addr_list, hostname)
12 /* INET_ADDR_LIST *addr_list;
13 /* const char *hostname;
15 /* inet_addr_host() determines all interface addresses of the
16 /* named host. The host may be specified as a symbolic name,
17 /* or as a numerical address. An empty host expands as the
18 /* wild-card address. Address results are appended to
19 /* the specified address list. The result value is the number
20 /* of addresses appended to the list.
22 /* Fatal errors: out of memory.
24 /* This code uses the name service, so it talks to the network,
25 /* and that may not be desirable.
27 /* inet_addr_list(3) address list management
31 /* The Secure Mailer license must be distributed with this software.
34 /* IBM T.J. Watson Research
36 /* Yorktown Heights, NY 10598, USA
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <sys/socket.h>
50 /* Utility library. */
53 #include <inet_addr_list.h>
54 #include <inet_addr_host.h>
55 #include <myaddrinfo.h>
56 #include <sock_addr.h>
57 #include <inet_proto.h>
60 /* inet_addr_host - look up address list for host */
62 int inet_addr_host(INET_ADDR_LIST
*addr_list
, const char *hostname
)
64 const char *myname
= "inet_addr_host";
66 struct addrinfo
*res0
;
72 int initial_count
= addr_list
->used
;
73 INET_PROTO_INFO
*proto_info
;
76 * The use of square brackets around an IPv6 addresses is required, even
77 * though we don't enforce it as it'd make the code unnecessarily
80 * XXX AIX 5.1 getaddrinfo() does not allow "0" as service, regardless of
81 * whether or not a host is specified.
86 } else if (*hostname
== '['
87 && hostname
[(hostnamelen
= strlen(hostname
)) - 1] == ']') {
88 hname
= mystrndup(hostname
+ 1, hostnamelen
- 2);
95 proto_info
= inet_proto_info();
96 if ((aierr
= hostname_to_sockaddr(hname
, serv
, SOCK_STREAM
, &res0
)) == 0) {
97 for (res
= res0
; res
; res
= res
->ai_next
) {
102 if (strchr((char *) proto_info
->sa_family_list
, res
->ai_family
) == 0) {
103 msg_info("%s: skipping address family %d for host \"%s\"",
104 myname
, res
->ai_family
, hostname
);
109 * On Linux systems it is not unusual for user-land to be out of
110 * sync with kernel-land. When this is the case we try to be
111 * helpful and filter out address families that the library
112 * claims to understand but that are not supported by the kernel.
114 if ((sock
= socket(res
->ai_family
, SOCK_STREAM
, 0)) < 0) {
115 msg_warn("%s: skipping address family %d: %m",
116 myname
, res
->ai_family
);
120 msg_warn("%s: close socket: %m", myname
);
122 inet_addr_list_append(addr_list
, res
->ai_addr
);
126 if (hname
&& hname
!= hostname
)
127 myfree((char *) hname
);
129 return (addr_list
->used
- initial_count
);
136 #include <msg_vstream.h>
137 #include <sock_addr.h>
139 int main(int argc
, char **argv
)
142 struct sockaddr_storage
*sa
;
143 MAI_HOSTADDR_STR hostaddr
;
144 INET_PROTO_INFO
*proto_info
;
146 msg_vstream_init(argv
[0], VSTREAM_ERR
);
149 msg_fatal("usage: %s protocols hostname...", argv
[0]);
151 proto_info
= inet_proto_init(argv
[0], argv
[1]);
154 while (--argc
&& *++argv
) {
155 inet_addr_list_init(&list
);
156 if (inet_addr_host(&list
, *argv
) == 0)
157 msg_fatal("not found: %s", *argv
);
159 for (sa
= list
.addrs
; sa
< list
.addrs
+ list
.used
; sa
++) {
160 SOCKADDR_TO_HOSTADDR(SOCK_ADDR_PTR(sa
), SOCK_ADDR_LEN(sa
),
161 &hostaddr
, (MAI_SERVPORT_STR
*) 0, 0);
162 vstream_printf("%s\t%s\n", *argv
, hostaddr
.buf
);
164 vstream_fflush(VSTREAM_OUT
);
165 inet_addr_list_free(&list
);