📄 net-ns-dns-api.html
字号:
<!-- Copyright (C) 2009 Free Software Foundation, Inc. -->
<!-- This material may be distributed only subject to the terms -->
<!-- and conditions set forth in the Open Publication License, v1.0 -->
<!-- or later (the latest version is presently available at -->
<!-- http://www.opencontent.org/openpub/). -->
<!-- Distribution of the work or derivative of the work in any -->
<!-- standard (paper) book form is prohibited unless prior -->
<!-- permission is obtained from the copyright holder. -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>DNS</TITLE
><meta name="MSSmartTagsPreventParsing" content="TRUE">
<META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="eCos Reference Manual"
HREF="ecos-ref.html"><LINK
REL="UP"
TITLE="DNS for eCos and RedBoot"
HREF="net-ns-dns.html"><LINK
REL="PREVIOUS"
TITLE="DNS for eCos and RedBoot"
HREF="net-ns-dns.html"><LINK
REL="NEXT"
TITLE="DNS Client Testing"
HREF="net-ns-dns-testing.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>eCos Reference Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="net-ns-dns.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="net-ns-dns-testing.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="NET-NS-DNS-API"
></A
>Chapter 49. DNS</H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="net-ns-dns-api.html#NET-NS-DNS-API1"
>DNS API</A
></DT
><DT
><A
HREF="net-ns-dns-testing.html"
>DNS Client Testing</A
></DT
></DL
></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="NET-NS-DNS-API1"
>DNS API</A
></H1
><P
>The DNS client uses the normal BSD API for performing lookups:
<CODE
CLASS="FUNCTION"
>gethostbyname(),</CODE
>
<CODE
CLASS="FUNCTION"
>gethostbyaddr()</CODE
>,
<CODE
CLASS="FUNCTION"
>getaddrinfo()</CODE
>,
<CODE
CLASS="FUNCTION"
>getnameinfo()</CODE
>. </P
><P
>There are a few restrictions:</P
><P
></P
><UL
><LI
><P
>If the DNS server returns multiple authoritive records
for a host name to <CODE
CLASS="FUNCTION"
>gethostbyname</CODE
>, the hostent will only contain a record for the
first entry. If multiple records are desired, use
<CODE
CLASS="FUNCTION"
>getaddrinfo</CODE
>, which will return multiple results.</P
></LI
><LI
><P
>The code has been made thread safe. ie multiple threads
may call
<CODE
CLASS="FUNCTION"
>gethostbyname()</CODE
>
without causing problems to the hostent structure returned. What
is not safe is one thread using both
<CODE
CLASS="FUNCTION"
>gethostbyname()</CODE
>
and
<CODE
CLASS="FUNCTION"
>gethostbyaddr()</CODE
>.
A call to one will destroy the results from the previous call
to the other function. <CODE
CLASS="FUNCTION"
>getaddrinfo()</CODE
> and
<CODE
CLASS="FUNCTION"
>getnameinfo()</CODE
> are thread
safe and so these are the preferred interfaces. They are also address
family independent so making it easier to port code to IPv6.</P
></LI
><LI
><P
>The DNS client will only return IPv4 addresses to
RedBoot. At the moment this is not really a limitation,
since RedBoot only supports IPv4 and not IPv6.</P
></LI
></UL
><P
>To initialise the DNS client the following function must be
called:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#include <network.h>
int cyg_dns_res_start(char * dns_server)</PRE
></TD
></TR
></TABLE
><P
>Where dns_server is the address of the DNS server. The address
must be in numeric form and can be either an IPv4 or an IPv6 address.</P
><P
>There also exists a deprecated function to start the DNS
client:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>int cyg_dns_res_init(struct in_addr *dns_server)</PRE
></TD
></TR
></TABLE
><P
>where dns_server is the address of the DNS server
the client should query. The address should be in network order and
can only be an IPv4 address.</P
><P
> On error both this function returns -1, otherwise
0 for success. If lookups are attemped before this function has
been called, they will fail and return NULL, unless numeric host addresses
are passed. In this cause, the address will be converted and returned
without the need for a lookup.</P
><P
>A default, hard coded, server may be specified in the CDL option
<TT
CLASS="LITERAL"
>CYGDAT_NS_DNS_DEFAULT_SERVER</TT
>. The use of this is
controlled by <TT
CLASS="LITERAL"
>CYGPKG_NS_DNS_DEFAULT</TT
>. If this is
enabled, <TT
CLASS="LITERAL"
>init_all_network_interfaces()</TT
> will
initialize the resolver with the hard coded address. The DHCP client
or user code my override this address by calling
<TT
CLASS="LITERAL"
>cyg_dns_res_init</TT
> again. </P
><P
>The DNS client understands the concepts of the target being
in a domain. By default no domain will be used. Host name lookups
should be for fully qualified names. The domain name can be set
and retrieved using the functions:
<DIV
CLASS="FUNCSYNOPSIS"
><P
></P
><A
NAME="AEN13155"
></A
><P
><CODE
><CODE
CLASS="FUNCDEF"
>int getdomainname</CODE
>(char *name, size_t len);</CODE
></P
><P
><CODE
><CODE
CLASS="FUNCDEF"
>int setdomainname</CODE
>(const char *name, size_t len);</CODE
></P
><P
></P
></DIV
></P
><P
>Alternatively, a hard coded domain name can be set using CDL.
The boolean <TT
CLASS="LITERAL"
>CYGPKG_NS_DNS_DOMAINNAME</TT
> enables this
and the domain name is taken from
<TT
CLASS="LITERAL"
>CYGPKG_NS_DNS_DOMAINNAME_NAME</TT
>.</P
><P
>Once set, the DNS client will use some simple heuristics when
deciding how to use the domainname. If the name given to the
client ends with a "." it is assumed to be a FQDN and the domain
name will not be used. If the name contains a "." somewhere
within it, first a lookup will be performed without the
domainname. If that fails the domainname will be appended and
looked up. If the name does not contain a ".", the domainname is
appended and used for the first query. If that fails, the
unadorned name is lookup.</P
><P
>The <CODE
CLASS="FUNCTION"
>getaddrinfo</CODE
> will return both IPv4 and
IPv6 addresses for a given host name, when IPv6 is enabled in
the eCos configuration. The CDL option
<TT
CLASS="LITERAL"
>CYGOPT_NS_DNS_FIRST_FAMILY</TT
> controls the order
IPv6 and IPv4 addresses are returned in the linked list of
<TT
CLASS="LITERAL"
>addrinfo</TT
> structures. If the value
<TT
CLASS="LITERAL"
>AF_INET</TT
> is used, the IPv4 addresses will be
first. If the value <TT
CLASS="LITERAL"
>AF_INET6</TT
>, which is the
default, is used, IPv6 address will be first. This ordering will
control how clients attempt to connect to servers, ie using IPv6
or IPv4 first.
</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="net-ns-dns.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ecos-ref.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="net-ns-dns-testing.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>DNS for eCos and RedBoot</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="net-ns-dns.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>DNS Client Testing</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -