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