summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authormillert <>2004-12-20 21:20:56 +0000
committermillert <>2004-12-20 21:20:56 +0000
commit2573b4031e211c42eadf47a4e30eae1ee1a516ea (patch)
tree9243d3f53001294313765812d9dc83c6a25a3c82 /src/lib
parent8d1da2e74a2ad861ec64a6439e30200a09d5db48 (diff)
downloadopenbsd-2573b4031e211c42eadf47a4e30eae1ee1a516ea.tar.gz
openbsd-2573b4031e211c42eadf47a4e30eae1ee1a516ea.tar.bz2
openbsd-2573b4031e211c42eadf47a4e30eae1ee1a516ea.zip
Add back EXAMPLE section and scopeid info which are from KAME, not the
RFC text.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libc/net/getaddrinfo.3132
1 files changed, 131 insertions, 1 deletions
diff --git a/src/lib/libc/net/getaddrinfo.3 b/src/lib/libc/net/getaddrinfo.3
index 2209100c8b..92f9f1f809 100644
--- a/src/lib/libc/net/getaddrinfo.3
+++ b/src/lib/libc/net/getaddrinfo.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: getaddrinfo.3,v 1.31 2004/12/20 21:13:00 millert Exp $ 1.\" $OpenBSD: getaddrinfo.3,v 1.32 2004/12/20 21:20:56 millert Exp $
2.\" 2.\"
3.\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") 3.\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
4.\" Copyright (C) 2000, 2001 Internet Software Consortium. 4.\" Copyright (C) 2000, 2001 Internet Software Consortium.
@@ -212,6 +212,37 @@ structure in the list, the
212member points to a filled-in socket address structure of length 212member points to a filled-in socket address structure of length
213.Fa ai_addrlen . 213.Fa ai_addrlen .
214.Pp 214.Pp
215This implementation of
216.Fn getaddrinfo
217allows experimental numeric IPv6 address notation with scope identifier.
218By appending the percent character and scope identifier to addresses,
219you can fill the
220.Li sin6_scope_id
221field for addresses.
222This would make management of scoped address easier,
223and allows cut-and-paste input of scoped address.
224.Pp
225At this moment the code supports only link-local addresses with the format.
226Scope identifier is hardcoded to the name of the hardware interface associated
227with the link
228.Po
229such as
230.Li ne0
231.Pc .
232An example is
233.Dq Li fe80::1%ne0 ,
234which means
235.Do
236.Li fe80::1
237on the link associated with the
238.Li ne0
239interface
240.Dc .
241.Pp
242The IPv6 implementation is still very experimental and non-standard.
243The current implementation assumes a one-to-one relationship between
244the interface and link, which is not necessarily true from the specification.
245.Pp
215All of the information returned by 246All of the information returned by
216.Fn getaddrinfo 247.Fn getaddrinfo
217is dynamically allocated: the 248is dynamically allocated: the
@@ -247,6 +278,105 @@ are
247.Fn getaddrinfo 278.Fn getaddrinfo
248returns 279returns
249.Dv EAI_NONAME . 280.Dv EAI_NONAME .
281.Sh EXAMPLES
282The following code tries to connect to
283.Dq Li www.kame.net
284service
285.Dq Li http .
286via stream socket.
287It loops through all the addresses available, regardless of address family.
288If the destination resolves to an IPv4 address, it will use
289.Dv AF_INET
290socket.
291Similarly, if it resolves to IPv6,
292.Dv AF_INET6
293socket is used.
294Observe that there is no hardcoded reference to a particular address family.
295The code works even if
296.Nm getaddrinfo
297returns addresses that are not IPv4/v6.
298.Bd -literal -offset indent
299struct addrinfo hints, *res, *res0;
300int error;
301int s;
302const char *cause = NULL;
303
304memset(&hints, 0, sizeof(hints));
305hints.ai_family = PF_UNSPEC;
306hints.ai_socktype = SOCK_STREAM;
307error = getaddrinfo("www.kame.net", "http", &hints, &res0);
308if (error) {
309 errx(1, "%s", gai_strerror(error));
310 /*NOTREACHED*/
311}
312s = -1;
313for (res = res0; res; res = res->ai_next) {
314 s = socket(res->ai_family, res->ai_socktype,
315 res->ai_protocol);
316 if (s < 0) {
317 cause = "socket";
318 continue;
319 }
320
321 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
322 cause = "connect";
323 close(s);
324 s = -1;
325 continue;
326 }
327
328 break; /* okay we got one */
329}
330if (s < 0) {
331 err(1, "%s", cause);
332 /*NOTREACHED*/
333}
334freeaddrinfo(res0);
335.Ed
336.Pp
337The following example tries to open a wildcard listening socket onto service
338.Dq Li http ,
339for all the address families available.
340.Bd -literal -offset indent
341struct addrinfo hints, *res, *res0;
342int error;
343int s[MAXSOCK];
344int nsock;
345const char *cause = NULL;
346
347memset(&hints, 0, sizeof(hints));
348hints.ai_family = PF_UNSPEC;
349hints.ai_socktype = SOCK_STREAM;
350hints.ai_flags = AI_PASSIVE;
351error = getaddrinfo(NULL, "http", &hints, &res0);
352if (error) {
353 errx(1, "%s", gai_strerror(error));
354 /*NOTREACHED*/
355}
356nsock = 0;
357for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
358 s[nsock] = socket(res->ai_family, res->ai_socktype,
359 res->ai_protocol);
360 if (s[nsock] < 0) {
361 cause = "socket";
362 continue;
363 }
364
365 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
366 cause = "bind";
367 close(s[nsock]);
368 continue;
369 }
370 (void) listen(s[nsock], 5);
371
372 nsock++;
373}
374if (nsock == 0) {
375 err(1, "%s", cause);
376 /*NOTREACHED*/
377}
378freeaddrinfo(res0);
379.Ed
250.Sh SEE ALSO 380.Sh SEE ALSO
251.Xr bind 2 , 381.Xr bind 2 ,
252.Xr connect 2 , 382.Xr connect 2 ,