1.\" $NetBSD: resolver.3,v 1.26 2013/05/08 18:18:32 wiz Exp $ 2.\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3.\" 4.\" Permission to use, copy, modify, and distribute this software for any 5.\" purpose with or without fee is hereby granted, provided that the above 6.\" copyright notice and this permission notice appear in all copies. 7.\" 8.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 9.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 11.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 14.\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15.\" 16.\" Copyright (c) 1985, 1995 The Regents of the University of California. 17.\" All rights reserved. 18.\" 19.\" Redistribution and use in source and binary forms are permitted provided 20.\" that: (1) source distributions retain this entire copyright notice and 21.\" comment, and (2) distributions including binaries display the following 22.\" acknowledgement: ``This product includes software developed by the 23.\" University of California, Berkeley and its contributors'' in the 24.\" documentation or other materials provided with the distribution and in 25.\" all advertising materials mentioning features or use of this software. 26.\" Neither the name of the University nor the names of its contributors may 27.\" be used to endorse or promote products derived from this software without 28.\" specific prior written permission. 29.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 30.\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 31.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 32.\" 33.\" @(#)resolver.3 6.5 (Berkeley) 6/23/90 34.\" Id: resolver.man3,v 1.2 2009/01/21 00:12:34 each Exp 35.\" 36.Dd May 8, 2013 37.Dt RESOLVER 3 38.Os 39.Sh NAME 40.Nm res_ninit , 41.Nm res_ourserver_p , 42.Nm fp_resstat , 43.Nm res_hostalias , 44.Nm res_pquery , 45.Nm res_nquery , 46.Nm res_nsearch , 47.Nm res_nquerydomain , 48.Nm res_nmkquery , 49.Nm res_nsend , 50.Nm res_nupdate , 51.Nm res_nmkupdate , 52.Nm res_nclose , 53.Nm res_nsendsigned , 54.Nm res_findzonecut , 55.Nm res_getservers , 56.Nm res_setservers , 57.Nm res_ndestroy , 58.Nm dn_comp , 59.Nm dn_expand , 60.\" .Nm hstrerror , 61.Nm res_init , 62.Nm res_isourserver , 63.Nm fp_nquery , 64.Nm p_query , 65.Nm hostalias , 66.Nm res_query , 67.Nm res_search , 68.Nm res_querydomain , 69.Nm res_mkquery , 70.Nm res_send , 71.Nm res_update , 72.Nm res_close , 73.\" .Nm herror 74.Nd resolver routines 75.Sh LIBRARY 76.Lb libc 77.Lb libresolv 78.Sh SYNOPSIS 79.In resolv.h 80.In res_update.h 81.Vt typedef struct __res_state *res_state ; 82.Pp 83.Ft int 84.Fn res_ninit "res_state statp" 85.Ft int 86.Fn res_ourserver_p "const res_state statp" "const struct sockaddr_in *addr" 87.Ft void 88.Fn fp_resstat "const res_state statp" "FILE *fp" 89.Ft "const char *" 90.Fn res_hostalias "const res_state statp" "const char *name" "char *buf" "size_t buflen" 91.Ft int 92.Fn res_pquery "const res_state statp" "const u_char *msg" "int msglen" "FILE *fp" 93.Ft int 94.Fn res_nquery "res_state statp" "const char *dname" "int class" "int type" "u_char *answer" "int anslen" 95.Ft int 96.Fn res_nsearch "res_state statp" "const char *dname" "int class" "int type" "u_char * answer" "int anslen" 97.Ft int 98.Fn res_nquerydomain "res_state statp" "const char *name" "const char *domain" "int class" "int type" "u_char *answer" "int anslen" 99.Ft int 100.Fo res_nmkquery 101.Fa "res_state statp" 102.Fa "int op" 103.Fa "const char *dname" 104.Fa "int class" 105.Fa "int type" 106.Fa "const u_char *data" 107.Fa "int datalen" 108.Fa "const u_char *newrr" 109.Fa "u_char *buf" 110.Fa "int buflen" 111.Fc 112.Ft int 113.Fn res_nsend "res_state statp" "const u_char *msg" "int msglen" "u_char *answer" "int anslen" 114.Ft int 115.Fn res_nupdate "res_state statp" "ns_updrec *rrecp_in" 116.Ft int 117.Fn res_nmkupdate "res_state statp" "ns_updrec *rrecp_in" "u_char *buf" "int buflen" 118.Ft void 119.Fn res_nclose "res_state statp" 120.Ft int 121.Fn res_nsendsigned "res_state statp" "const u_char *msg" "int msglen" "ns_tsig_key *key" "u_char *answer" "int anslen" 122.Ft int 123.Fn res_findzonecut "res_state statp" "const char *dname" "ns_class class" "int options" "char *zname" "size_t zsize" "struct in_addr *addrs" "int naddrs" 124.Ft int 125.Fn res_getservers "res_state statp" "union res_sockaddr_union *set" "int cnt" 126.Ft void 127.Fn res_setservers "res_state statp" "const union res_sockaddr_union *set" "int cnt" 128.Ft void 129.Fn res_ndestroy "res_state statp" 130.Ft int 131.Fn dn_comp "const char *exp_dn" "u_char *comp_dn" "int length" "u_char **dnptrs" "u_char **lastdnptr" 132.Ft int 133.Fn dn_expand "const u_char *msg" "const u_char *eomorig" "const u_char *comp_dn" "char *exp_dn" "int length" 134.\" .Ft "const char *" 135.\" .Fn hstrerror "int err" 136.Ss DEPRECATED 137.In sys/types.h 138.In netinet/in.h 139.In arpa/nameser.h 140.In resolv.h 141.In res_update.h 142.Ft int 143.Fn res_init "void" 144.Ft int 145.Fn res_isourserver "const struct sockaddr_in *addr" 146.Ft int 147.Fn fp_nquery "const u_char *msg" "int msglen" "FILE *fp" 148.Ft void 149.Fn p_query "const u_char *msg" "FILE *fp" 150.Ft "const char *" 151.Fn hostalias "const char *name" 152.Ft int 153.Fn res_query "const char *dname" "int class" "int type" "u_char *answer" "int anslen" 154.Ft int 155.Fn res_search "const char *dname" "int class" "int type" "u_char *answer" "int anslen" 156.Ft int 157.Fn res_querydomain "const char *name" "const char *domain" "int class" "int type" "u_char *answer" "int anslen" 158.Ft int 159.Fo res_mkquery 160.Fa "int op" 161.Fa "const char *dname" 162.Fa "int class" 163.Fa "int type" 164.Fa "const char *data" 165.Fa "int datalen" 166.Fa "struct rrec *newrr" 167.Fa "u_char *buf" 168.Fa "int buflen" 169.Fc 170.Ft int 171.Fn res_send "const u_char *msg" "int msglen" "u_char *answer" "int anslen" 172.Ft int 173.Fn res_update "ns_updrec *rrecp_in" 174.Ft void 175.Fn res_close "void" 176.\" .Ft void 177.\" .Fn herror "const char *s" 178.Sh DESCRIPTION 179These routines are used for making, sending and interpreting 180query and reply messages with Internet domain name servers. 181.Pp 182State information is kept in 183.Fa statp 184and is used to control the behavior of these functions. 185.Fa statp 186should be set to all zeros prior to the first call to any of these functions. 187.Pp 188The functions 189.Fn res_init , 190.Fn res_isourserver , 191.Fn fp_nquery , 192.Fn p_query , 193.Fn hostalias , 194.Fn res_query , 195.Fn res_search , 196.Fn res_querydomain , 197.Fn res_mkquery , 198.Fn res_send , 199.Fn res_update , 200.Fn res_close 201.\" and 202.\" .Fn herror 203are deprecated and are supplied for compatability with old source 204code. 205They use global configuration and state information that is 206kept in the structure 207.Ft _res 208rather than that referenced through 209.Ft statp . 210.Pp 211Most of the values in 212.Ft statp 213and 214.Ft _res 215are initialized on the first call to 216.Fn res_ninit 217/ 218.Fn res_init 219to reasonable defaults and can be ignored. 220Options 221stored in 222.Ft statp->options 223/ 224.Ft _res.options 225are defined in 226.Pa resolv.h 227and are as follows. 228Options are stored as a simple bit mask containing the bitwise 229.Dq OR 230of the options enabled. 231.Bl -tag -width "RES_USE_INET6" 232.It Dv RES_INIT 233True if the initial name server address and default domain name are 234initialized (i.e., 235.Fn res_ninit 236/ 237.Fn res_init 238has been called). 239.It Dv RES_DEBUG 240Print debugging messages. 241.It Dv RES_AAONLY 242Accept authoritative answers only. 243Should continue until it finds an authoritative answer or finds an error. 244Currently this is not implemented. 245.It Dv RES_USEVC 246Use TCP connections for queries instead of UDP datagrams. 247.It Dv RES_STAYOPEN 248Used with 249.Dv RES_USEVC 250to keep the TCP connection open between queries. 251This is useful only in programs that regularly do many queries. 252UDP should be the normal mode used. 253.It Dv RES_IGNTC 254Ignore truncation errors, i.e., don't retry with TCP. 255.It Dv RES_RECURSE 256Set the recursion-desired bit in queries. 257This is the default. 258(\c 259.Fn res_nsend 260/ 261.Fn res_send 262does not do iterative queries and expects the name server 263to handle recursion.) 264.It Dv RES_DEFNAMES 265If set, 266.Fn res_nsearch 267/ 268.Fn res_search 269will append the default domain name to single-component names 270(those that do not contain a dot). 271This option is enabled by default. 272.It Dv RES_DNSRCH 273If this option is set, 274.Fn res_nsearch 275/ 276.Fn res_search 277will search for host names in the current domain and in parent domains; see 278.Xr hostname 7 . 279This is used by the standard host lookup routine 280.Xr gethostbyname 3 . 281This option is enabled by default. 282.It Dv RES_USE_INET6 283Enables support for IPv6-only applications. 284This causes IPv4 addresses to be returned as an IPv4 mapped address. 285For example, 10.1.1.1 will be returned as ::ffff:10.1.1.1. 286The option is meaningful with certain kernel configuration only. 287.It Dv RES_USE_EDNS0 288Enables support for OPT pseudo-RR for EDNS0 extension. 289With the option, resolver code will attach OPT pseudo-RR into DNS queries, 290to inform of our receive buffer size. 291The option will allow DNS servers to take advantage of non-default receive 292buffer size, and to send larger replies. 293DNS query packets with EDNS0 extension is not compatible with 294non-EDNS0 DNS servers. 295.It Dv RES_NOALIASES 296This option turns off the user level aliasing feature controlled by 297the 298.Ev HOSTALIASES 299environment variable. 300Network daemons should set this option. 301.It Dv RES_ROTATE 302This options causes the 303.Fn res_nsend 304/ 305.Fn res_send 306to rotate the list of nameservers in 307.Fa statp->nsaddr_list 308/ 309.Fa _res.nsaddr_list . 310.It Dv RES_KEEPTSIG 311This option causes 312.Fn res_nsendsigned 313to leave the message unchanged after TSIG verification; otherwise the TSIG 314record would be removed and the header updated. 315.It Dv RES_NOTLDQUERY 316This option causes 317.Fn res_nsearch 318to not attempt to resolve an unqualified name as if it were a top level 319domain (TLD). 320This option can cause problems if the site has "localhost" as a TLD rather 321than having localhost on one or more elements of the search list. 322This option has no effect if neither 323.Dv RES_DEFNAMES 324or 325.Dv RES_DNSRCH 326is set. 327.El 328.Pp 329The 330.Fn res_ninit 331/ 332.Fn res_init 333routine 334reads the configuration file (if any; see 335.Xr resolv.conf 5 ) 336to get the default domain name, search list and 337the Internet address of the local name server(s). 338If no server is configured, the host running the resolver is tried. 339The current domain name is defined by the hostname 340if not specified in the configuration file; 341it can be overridden by the environment variable 342.Ev LOCALDOMAIN . 343This environment variable may contain several blank-separated 344tokens if you wish to override the 345.Fa search list 346on a per-process basis. 347This is similar to the 348.Fa search 349command in the configuration file. 350Another environment variable 351.Ev RES_OPTIONS 352can be set to override certain internal resolver options which are otherwise 353set by changing fields in the 354.Ft statp 355/ 356.Ft _res 357structure or are inherited from the configuration file's 358.Fa options 359command. 360The syntax of the 361.Ev RES_OPTIONS 362environment variable is explained in 363.Xr resolv.conf 5 . 364Initialization normally occurs on the first call 365to one of the other resolver routines. 366.Pp 367The memory referred to by 368.Ft statp 369must be set to all zeros prior to the first call to 370.Fn res_ninit . 371.Fn res_ndestroy 372should be call to free memory allocated by 373.Fn res_ninit 374after last use. 375.Pp 376The 377.Fn res_nquery 378/ 379.Fn res_query 380functions provides interfaces to the server query mechanism. 381They constructs a query, sends it to the local server, 382awaits a response, and makes preliminary checks on the reply. 383The query requests information of the specified 384.Fa type 385and 386.Fa class 387for the specified fully-qualified domain name 388.Fa dname . 389The reply message is left in the 390.Fa answer 391buffer with length 392.Fa anslen 393supplied by the caller. 394.Fn res_nquery 395/ 396.Fn res_query 397return \-1 on error or the length of the answer. 398.Pp 399The 400.Fn res_nsearch 401/ 402.Fn res_search 403routines make a query and awaits a response like 404.Fn res_nquery 405/ 406.Fn res_query , 407but in addition, it implements the default and search rules 408controlled by the 409.Dv RES_DEFNAMES 410and 411.Dv RES_DNSRCH 412options. 413It returns the length of the first successful reply which is stored in 414.Ft answer 415or \-1 on error. 416.Pp 417The remaining routines are lower-level routines used by 418.Fn res_nquery 419/ 420.Fn res_query . 421The 422.Fn res_nmkquery 423/ 424.Fn res_mkquery 425functions 426constructs a standard query message and places it in 427.Fa buf . 428It returns the size of the query, or \-1 if the query is 429larger than 430.Fa buflen . 431The query type 432.Fa op 433is usually 434.Dv QUERY , 435but can be any of the query types defined in 436.Aq Pa arpa/nameser.h . 437The domain name for the query is given by 438.Fa dname . 439.Fa newrr 440is currently unused but is intended for making update messages. 441.Pp 442The 443.Fn res_nsend 444/ 445.Fn res_send 446/ 447.Fn res_nsendsigned 448routines 449sends a pre-formatted query and returns an answer. 450It will call 451.Fn res_ninit 452/ 453.Fn res_init 454if 455.Dv RES_INIT 456is not set, send the query to the local name server, and 457handle timeouts and retries. 458Additionally, 459.Fn res_nsendsigned 460will use TSIG signatures to add authentication to the query and verify the 461response. 462In this case, only one nameserver will be contacted. 463The length of the reply message is returned, or \-1 if there were errors. 464.Pp 465.Fn res_nquery 466/ 467.Fn res_query , 468.Fn res_nsearch 469/ 470.Fn res_search 471and 472.Fn res_nsend 473/ 474.Fn res_send 475return a length that may be bigger than 476.Fa anslen . 477In that case the query should be retried with a bigger buffer. 478NOTE the answer to the second query may be larger still so supplying 479a buffer that bigger that the answer returned by the previous 480query is recommended. 481.Pp 482.Fa answer 483MUST be big enough to receive a maximum UDP response from the server or 484parts of the answer will be silently discarded. 485The default maximum UDP response size is 512 bytes. 486.Pp 487The function 488.Fn res_ourserver_p 489returns true when 490.Fa inp 491is one of the servers in 492.Fa statp->nsaddr_list 493/ 494.Fa _res.nsaddr_list . 495.Pp 496The functions 497.Fn fp_nquery 498/ 499.Fn p_query 500print out the query and any answer in 501.Fa msg 502on 503.Fa fp . 504.Fn p_query 505is equivalent to 506.Fn fp_nquery 507with 508.Fa msglen 509set to 512. 510.Pp 511The function 512.Fn fp_resstat 513prints out the active flag bits in 514.Fa statp->options 515preceeded by the text ";; res options:" on 516.Fa file . 517.Pp 518The functions 519.Fn res_hostalias 520/ 521.Fn hostalias 522lookup up name in the file referred to by the 523.Ev HOSTALIASES 524files return a fully qualified hostname if found or NULL if 525not found or an error occurred. 526.Fn res_hostalias 527uses 528.Fa buf 529to store the result in, 530.Fn hostalias 531uses a static buffer. 532.Pp 533The functions 534.Fn res_getservers 535and 536.Fn res_setservers 537are used to get and set the list of server to be queried. 538.Pp 539The functions 540.Fn res_nupdate 541/ 542.Fn res_update 543take a list of ns_updrec 544.Fa rrecp_in . 545Identifies the containing zone for each record and groups the records 546according to containing zone maintaining in zone order then sends and update 547request to the servers for these zones. 548The number of zones updated is returned or \-1 on error. 549Note that 550.Fn res_nupdate 551will perform TSIG authenticated dynamic update operations if the key is not 552NULL. 553.Pp 554The function 555.Fn res_findzonecut 556discovers the closest enclosing zone cut for a specified domain name, 557and finds the IP addresses of the zone's master servers. 558.Pp 559The functions 560.Fn res_nmkupdate 561/ 562.Fn res_mkupdate 563take a linked list of ns_updrec 564.Fa rrecp_in 565and construct a UPDATE message in 566.Fa buf . 567.Fn res_nmkupdate 568/ 569.Fn res_mkupdate 570return the length of the constructed message on no error or one of the 571following error values. 572.Bl -inset -width "-5" 573.It \-1 574An error occurred parsing 575.Fa rrecp_in . 576.It \-2 577The buffer 578.Fa buf 579was too small. 580.It \-3 581The first record was not a zone section or there was a section order problem. 582The section order is S_ZONE, S_PREREQ and S_UPDATE. 583.It \-4 584A number overflow occurred. 585.It \-5 586Unknown operation or no records. 587.El 588.Pp 589The functions 590.Fn res_nclose 591/ 592.Fn res_close 593close any open files referenced through 594.Fa statp 595/ 596.Fa _res . 597.Pp 598The function 599.Fn res_ndestroy 600calls 601.Fn res_nclose 602then frees any memory allocated by 603.Fn res_ninit . 604.Pp 605The 606.Fn dn_comp 607function 608compresses the domain name 609.Fa exp_dn 610and stores it in 611.Fa comp_dn . 612The size of the compressed name is returned or \-1 if there were errors. 613The size of the array pointed to by 614.Fa comp_dn 615is given by 616.Fa length . 617The compression uses 618an array of pointers 619.Fa dnptrs 620to previously-compressed names in the current message. 621The first pointer points to 622the beginning of the message and the list ends with 623.Dv NULL . 624The limit to the array is specified by 625.Fa lastdnptr . 626A side effect of 627.Fn dn_comp 628is to update the list of pointers for labels inserted into the message 629as the name is compressed. 630If 631.Fa dnptr 632is 633.Dv NULL , 634names are not compressed. 635If 636.Fa lastdnptr 637is 638.Dv NULL , 639the list of labels is not updated. 640.Pp 641The 642.Fn dn_expand 643entry expands the compressed domain name 644.Fa comp_dn 645to a full domain name. 646The compressed name is contained in a query or reply message; 647.Fa msg 648is a pointer to the beginning of the message. 649.Fa eomorig 650is a pointer to the first location after the message. 651The uncompressed name is placed in the buffer indicated by 652.Fa exp_dn 653which is of size 654.Fa length . 655The size of compressed name is returned or \-1 if there was an error. 656.Pp 657The variables 658.Ft statp->res_h_errno 659/ 660.Ft _res.res_h_errno 661and external variable 662.Ft h_errno 663is set whenever an error occurs during resolver operation. 664The following 665definitions are given in 666.Aq Pa netdb.h : 667.Bd -literal 668#define NETDB_INTERNAL -1 669/* see errno */ 670#define NETDB_SUCCESS 0 671/* no problem */ 672#define HOST_NOT_FOUND 1 673/* Authoritative Answer Host not found */ 674#define TRY_AGAIN 2 675/* Non-Authoritative not found, or SERVFAIL */ 676#define NO_RECOVERY 3 677/* Non-Recoverable: FORMERR, REFUSED, NOTIMP */ 678#define NO_DATA 4 679/* Valid name, no data for requested type */ 680.Ed 681.\" .Pp 682.\" The 683.\" .Fn herror 684.\" function writes a message to the diagnostic output consisting of the string 685.\" parameter 686.\" .Fa s , 687.\" the constant string ": ", and a message corresponding to the value of 688.\" .Ft h_errno . 689.\" .Pp 690.\" The 691.\" .Fn hstrerror 692.\" function returns a string which is the message text corresponding to the 693.\" value of the 694.\" .Fa err 695.\" parameter. 696.Pp 697The following functions are only in 698.Dv libresolv : 699.Fn res_findzonecut , 700.Fn res_nmkupdate , 701.Fn res_nsendsigned , 702and 703.Fn res_nupdate . 704All the rest are in both 705.Dv libc 706and 707.Dv libresolv . 708.Sh FILES 709.Bl -tag -width "/etc/resolv.conf " 710.It Pa /etc/resolv.conf 711The configuration file, see 712.Xr resolv.conf 5 . 713.El 714.Sh SEE ALSO 715.Xr getaddrinfo 3 , 716.Xr getnameinfo 3 , 717.Xr gethostbyaddr 3 , 718.Xr gethostbyname 3 , 719.Xr hostname 7 , 720.Xr resolv.conf 5 , 721.Xr named 8 722.Pp 723.%T RFC 974 , 724.%T RFC 1032 , 725.%T RFC 1033 , 726.%T RFC 1034 , 727.%T RFC 1035 , 728.%T RFC 1535 729.Rs 730.%T "Name Server Operations Guide for BIND" 731.Re 732.Sh HISTORY 733The 734.Nm 735function appeared in 736.Bx 4.3 . 737