Elementary Name and Address Domain name system Conversions gethostbyname Function RES_USE_INET6 resolver option gethostbyname2 Function and IPv6 support gethostbyaddr Function uname and gethostname Functions getservbyname andgetservbyport Functions Other networking information 1 Some materials in these slides are taken from Prof. Ying-Dar Lin with his permission of usage
Domain Name System Entries in DNS: resource records (RRs) for a host A record: maps a hostname to a 32-bit IPv4 addr AAAA (quad A) record: maps to a 128-bit IPv6 addr PTR record: maps IP addr to hostname (p. 11) MX record: specifies a mail exchanger of the host CNAME record: assigns canonical name for common e.g. services 正 式 名 稱 solaris IN A 206.62.226.33 IN AAAA 5f1b:df00:ce3e:e200:0020:0800:2078:e3e3 IN MX 5 solaris.kohala.com IN MX 10 mailhost.kohala.com 5, 10 是 偏 好 值 www IN CNAME bsdi.kohala.com 2
DNS: Application, Resolver, Name Servers application application 以 domain name 為 階 層 架 構 code UDP call return request resolver local other code name name UDP server servers reply resolver configuration files resolver functions: gethostbyname, gethostbyaddr name server: BIND (Berkeley Internet Name Domain) static hosts files (DNS alternatives): /etc/hosts resolver configuration file (specifies name server IPs): /etc/resolv.conf 3
gethostbyname Function performs a DNS query for an A record or a AAAA record #include <netdb.h> struct hostent *gethostbyname (const char *hostname); returns: nonnull pointer if OK, NULL on error with h_errno set struct hostent { char *h_name; /* official (canonical) name of host */ char **h_aliases; /* pointer to array of pointers to alias names */ int h_addrtype; /* host addr type: AF_INET or AF_INET6 */ int h_length; /* length of address: 4 or 16 */ char **h_addr_list; /* ptr to array of ptrs with IPv4/IPv6 addrs */ }; #define h_addr h_addr_list[0] /* first address in list */ 4
hostent Structure Returned by gethostbyname Socket address structure (Ch.3) hostent { } h_name canonical hostname \0 h_aliases h_addrtype AF_INET/6 h_length 4/16 h_addr_list NULL 字 串 alias #1 \0 alias #2 \0 NULL in/6_addr { } IP addr #1 in/6_addr { } IP addr #2 in/6_addr { } IP addr #3 5
Callgethostbyname and Print Returned Info #include "unp.h" names/hostent.c int main(int argc, char **argv) { char *ptr, **pptr, str[inet6_addrstrlen]; struct hostent *hptr; 下 個 命 令 列 參 數 while (--argc > 0) { ptr = *++argv; if ( (hptr = gethostbyname(ptr)) == NULL) { err_msg("gethostbyname error for host: %s: %s", ptr, hstrerror(h_errno)); continue; 印 出 正 式 名 稱 } 印 出 所 有 別 名 printf("official hostname: %s\n", hptr->h_name); for (pptr = hptr->h_aliases; *pptr!= NULL; pptr++) printf( \talias: %s\n", *pptr); 6
#ifdef #endif } switch (hptr->h_addrtype) { case AF_INET: AF_INET6 case AF_INET6: } exit(0); pptr = hptr->h_addr_list; for ( ; *pptr!= NULL; pptr++) printf( \taddress: %s\n, default: err_ret("unknown address type"); break; } names/hostent.c 呼 叫 Inet_ntop 將 所 有 位 址 全 部 列 出 來 Inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str))); break; Address type: AF_INET or AF_INET6 7
Program Output lhyen@lhyen-desktop:~/np/unpv13e/names$./hostent www.yahoo.com.tw official hostname: tw1-w2.rd.tw.g1.b.yahoo.com alias: www.yahoo.com.tw alias: rc.tpe.yahoo.com alias: tw.rc.yahoo.com alias: w2.rd.tw.g1.b.yahoo.com address: 119.160.246.23 lhyen@lhyen-desktop:~/np/unpv13e/names$ 8
Reversed Name Resolution 原 始 DNS 架 構 下 由 IP address 查 domain name 時 不 容 易 找 到 正 確 的 DNS server, 因 DNS 是 以 domain name 而 不 是 以 IP address 為 階 層 架 構 例 如, 某 固 網 公 司 DNS 伺 服 器 存 有 所 有 *.idv.tw 主 機 的 domain name 與 IP 位 址 的 對 應, 但 這 些 主 機 的 IP 位 址 並 沒 有 規 律 性 ( 來 自 各 處 ) 如 果 給 定 了 某 個 IP 位 址, 怎 能 知 道 要 去 此 DNS 伺 服 器 查 它 的 domain name? 9
Reversed Name Resolution (cont.) 解 決 方 案 : 建 立 一 個 特 殊 的 domain (inaddr.arpa for IPv4), 專 門 用 來 由 IP address 查 domain name 要 查 206.62.226.33 對 應 的 domain name 到 負 責 33.226.62.206.in-addr.arpa 的 DNS 伺 服 器 去 查 所 謂 的 PTR record 處 理 起 來 就 如 同 由 domain name 查 IP 位 址 33.226.62.206.in-addr.arpa. IN PTR admin1.uohio.edu. 10
gethostbyaddr Function binary IP address tohostent structure 由 IP Address ( 置 於 位 址 結 構 中 ) 查 hostname ( 在 struct hostent 中 ) #include <netdb.h> struct hostent *gethostbyaddr (const char *addr, size_t len, int family); returns: non-null pointer if OK, NULL on error with h_errno set addr argument: a pointer to an in_addr or in6_addr structure h_name inhostent: canonical hostname gethostbyaddr: queries a DNS name server for a PTR record in the in-addr.arpa domain for IPv4 or a PTR record in the ip6.int domain for IPv6. 11
getservbyname / getservbyport Function Service name ( 如 ftp, domain, tftp) 與 port number ( 如 21,53,69) 間 的 互 查 通 常 定 義 在 /etc/services 檔 中 同 時 支 援 TCP 與 UDP 的 服 務 通 常 在 這 兩 個 protocol 內 使 用 相 同 的 port, 但 也 有 例 外 : shell 514/tcp syslog 514/udp 12
getservbyname / getservbyport Function ftp, domain #include <netdb.h> struct servent *getservbyname(const char *servname, const char *protoname); udp, tcp or NULL returns: non-null pointer if OK, NULL on error struct servent *getservbyport (int port, const char *protoname); returns: non-null pointer if OK, NULL on error struct servent { char *s_name; /* official service name */ char **s_aliases; /* service alias list */ int s_port; /* port number, network-byte order */ char *s_proto; /* protocol, TCP or UDP, to use */ }; 13
daytime Client using gethostbyname and getservbyname (see Figure 11.4) Call gethostbyname and getservbyname Try each server address Call connect Check for failure Read server s reply 14
getaddrinfo Function Handle both name-to-address and service-to-port translation #include <netdb.h> int getaddrinfo (const char *hostname, const char *service, const structure addrinfo *hints, struct addrinfo **result); returns: 0 if OK, nonzero on error hostname: either a host name or an address string service: either a service name or a decimal port num string hints: types of information the caller wants returned result: a pointer to a linked list ofaddrinfo structure 15
addrinfo members of hints that can be set by the caller #include <netdb.h> struct addrinfo { int ai_flags; /* AI_PASSIVE, AI_CANNONNAME */ int ai_family; /* AF_xxx */ int ai_socktype; /* SOCK_xxx */ int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPV6 */ socklen_t ai_addrlen; char *ai_canonmame; struct sockaddr *ai_addr; struct addrinfo *ai_next; }; 16
Other Networking Info Four types of info: hosts (gethostbyname, gethostbyaddr) through DNS or /etc/hosts, hostent structure networks (getnetbyname, getnetbyaddr) through DNS or /etc/networks, netent structure protocols (getprotobyname, getprotobynumber) through /etc/protocols, protoent structure services (getservbyname, getservbyport) through /etc/services, servent structure 17