diff options
| author | millert <> | 2004-12-20 21:20:56 +0000 |
|---|---|---|
| committer | millert <> | 2004-12-20 21:20:56 +0000 |
| commit | 2573b4031e211c42eadf47a4e30eae1ee1a516ea (patch) | |
| tree | 9243d3f53001294313765812d9dc83c6a25a3c82 /src | |
| parent | 8d1da2e74a2ad861ec64a6439e30200a09d5db48 (diff) | |
| download | openbsd-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')
| -rw-r--r-- | src/lib/libc/net/getaddrinfo.3 | 132 |
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 | |||
| 212 | member points to a filled-in socket address structure of length | 212 | member points to a filled-in socket address structure of length |
| 213 | .Fa ai_addrlen . | 213 | .Fa ai_addrlen . |
| 214 | .Pp | 214 | .Pp |
| 215 | This implementation of | ||
| 216 | .Fn getaddrinfo | ||
| 217 | allows experimental numeric IPv6 address notation with scope identifier. | ||
| 218 | By appending the percent character and scope identifier to addresses, | ||
| 219 | you can fill the | ||
| 220 | .Li sin6_scope_id | ||
| 221 | field for addresses. | ||
| 222 | This would make management of scoped address easier, | ||
| 223 | and allows cut-and-paste input of scoped address. | ||
| 224 | .Pp | ||
| 225 | At this moment the code supports only link-local addresses with the format. | ||
| 226 | Scope identifier is hardcoded to the name of the hardware interface associated | ||
| 227 | with the link | ||
| 228 | .Po | ||
| 229 | such as | ||
| 230 | .Li ne0 | ||
| 231 | .Pc . | ||
| 232 | An example is | ||
| 233 | .Dq Li fe80::1%ne0 , | ||
| 234 | which means | ||
| 235 | .Do | ||
| 236 | .Li fe80::1 | ||
| 237 | on the link associated with the | ||
| 238 | .Li ne0 | ||
| 239 | interface | ||
| 240 | .Dc . | ||
| 241 | .Pp | ||
| 242 | The IPv6 implementation is still very experimental and non-standard. | ||
| 243 | The current implementation assumes a one-to-one relationship between | ||
| 244 | the interface and link, which is not necessarily true from the specification. | ||
| 245 | .Pp | ||
| 215 | All of the information returned by | 246 | All of the information returned by |
| 216 | .Fn getaddrinfo | 247 | .Fn getaddrinfo |
| 217 | is dynamically allocated: the | 248 | is dynamically allocated: the |
| @@ -247,6 +278,105 @@ are | |||
| 247 | .Fn getaddrinfo | 278 | .Fn getaddrinfo |
| 248 | returns | 279 | returns |
| 249 | .Dv EAI_NONAME . | 280 | .Dv EAI_NONAME . |
| 281 | .Sh EXAMPLES | ||
| 282 | The following code tries to connect to | ||
| 283 | .Dq Li www.kame.net | ||
| 284 | service | ||
| 285 | .Dq Li http . | ||
| 286 | via stream socket. | ||
| 287 | It loops through all the addresses available, regardless of address family. | ||
| 288 | If the destination resolves to an IPv4 address, it will use | ||
| 289 | .Dv AF_INET | ||
| 290 | socket. | ||
| 291 | Similarly, if it resolves to IPv6, | ||
| 292 | .Dv AF_INET6 | ||
| 293 | socket is used. | ||
| 294 | Observe that there is no hardcoded reference to a particular address family. | ||
| 295 | The code works even if | ||
| 296 | .Nm getaddrinfo | ||
| 297 | returns addresses that are not IPv4/v6. | ||
| 298 | .Bd -literal -offset indent | ||
| 299 | struct addrinfo hints, *res, *res0; | ||
| 300 | int error; | ||
| 301 | int s; | ||
| 302 | const char *cause = NULL; | ||
| 303 | |||
| 304 | memset(&hints, 0, sizeof(hints)); | ||
| 305 | hints.ai_family = PF_UNSPEC; | ||
| 306 | hints.ai_socktype = SOCK_STREAM; | ||
| 307 | error = getaddrinfo("www.kame.net", "http", &hints, &res0); | ||
| 308 | if (error) { | ||
| 309 | errx(1, "%s", gai_strerror(error)); | ||
| 310 | /*NOTREACHED*/ | ||
| 311 | } | ||
| 312 | s = -1; | ||
| 313 | for (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 | } | ||
| 330 | if (s < 0) { | ||
| 331 | err(1, "%s", cause); | ||
| 332 | /*NOTREACHED*/ | ||
| 333 | } | ||
| 334 | freeaddrinfo(res0); | ||
| 335 | .Ed | ||
| 336 | .Pp | ||
| 337 | The following example tries to open a wildcard listening socket onto service | ||
| 338 | .Dq Li http , | ||
| 339 | for all the address families available. | ||
| 340 | .Bd -literal -offset indent | ||
| 341 | struct addrinfo hints, *res, *res0; | ||
| 342 | int error; | ||
| 343 | int s[MAXSOCK]; | ||
| 344 | int nsock; | ||
| 345 | const char *cause = NULL; | ||
| 346 | |||
| 347 | memset(&hints, 0, sizeof(hints)); | ||
| 348 | hints.ai_family = PF_UNSPEC; | ||
| 349 | hints.ai_socktype = SOCK_STREAM; | ||
| 350 | hints.ai_flags = AI_PASSIVE; | ||
| 351 | error = getaddrinfo(NULL, "http", &hints, &res0); | ||
| 352 | if (error) { | ||
| 353 | errx(1, "%s", gai_strerror(error)); | ||
| 354 | /*NOTREACHED*/ | ||
| 355 | } | ||
| 356 | nsock = 0; | ||
| 357 | for (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 | } | ||
| 374 | if (nsock == 0) { | ||
| 375 | err(1, "%s", cause); | ||
| 376 | /*NOTREACHED*/ | ||
| 377 | } | ||
| 378 | freeaddrinfo(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 , |
