您的位置:首页 > 运维架构 > Linux

Linux Socket Programming by Example-第九章 IP与域名

2014-04-01 22:55 323 查看
关于IP地址与Domain的转换主要由DNS协议来实现。

关于DNS,有2点很重要:

1) 它是一颗树,根节点 为.

2) 分布式系统

关于DNS相关的RFC为:

1034   域名 - 概念和工具

1035   域名 - 实现和规范

1123   Internet 主机 - 应用和支持的要求

1886   支持 IP 版本 6 的 DNS 扩展名

1995   DNS 中的增量区域传输

1996   提示通知区域更改的机制 (DNS NOTIFY)

2136   域名系统中的动态更新 (DNS UPDATE)

2181   对 DNS 规范的说明

2308  DNS 查询的负缓存 (DNS NCACHE)

2535  域名系统安全扩展 (DNSSEC)

2671  DNS 的扩展机制 (EDNS0)

2782  指定服务位置的 DNS RR (DNS SRV)

2930  DNS 的密钥建立 (TKEY RR)

3645  DNS (GSS-TSIG) 密钥事务身分验证的通用安全服务算法

3646  IPv6 (DHCPv6) 动态主机配置协议的 DNS 配置选项

对于开发者来说,最重要的是下面2个库:

bind

dnsmasq (Android中使用)

感觉到C的强大了吗? 最重要的库基本由C语言统治。(^_^)

域名查询相关的配置文件主要有下面3个:

/etc/resolv.conf   (域名 IP地址)

/etc/hosts 

/etc/nsswitch.conf     (name service switch configuration,名字服务切换配置)

关于域名查询,这一章提供了以下函数:

函数1: uname

#include <sys/utsname.h>

int uname(struct utsname *buf);

struct utsname {

               char sysname[];    /* Operating system name (e.g., "Linux") */

               char nodename[];   /* Name within "some implementation-defined

                                     network" */

               char release[];    /* OS release (e.g., "2.6.28") */

               char version[];    /* OS version */

               char machine[];    /* Hardware identifier */

           #ifdef _GNU_SOURCE

               char domainname[]; /* NIS or YP domain name */

           #endif

           };

函数2:gethostname / getdomainname

#include <unistd.h>

int gethostname(char* name, size_t len);

int getdomainname(char* name, size_t len);

函数3: gethostbyname/ gethostbyaddr  域名与IP的相互转换

 #include <netdb.h>

extern int h_errno;

 struct hostent {

               char  *h_name;            /* official name of host */

               char **h_aliases;         /* alias list */

               int    h_addrtype;        /* host address type */

               int    h_length;          /* length of address */

               char **h_addr_list;       /* list of addresses */

           };

struct hostent *gethostbyname(const char *name);

#include <sys/socket.h>       /* for AF_INET */

struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type);

void sethostent(int stayopen); 采用TCP设置为TRUE,UDP设置为FALSE

 void endhostent(void); 结束查询

 void herror(const char *s); 错误信息

 const char *hstrerror(int err);

/* System V/POSIX extension */

struct hostent *gethostent(void);

/* GNU extensions */  GNU 扩展接口

struct hostent *gethostbyname2(const char *name, int af);

int gethostent_r(

       struct hostent *ret, char *buf, size_t buflen,

     struct hostent **result, int *h_errnop);

 int gethostbyaddr_r(const void *addr, socklen_t len, int type,

               struct hostent *ret, char *buf, size_t buflen,

               struct hostent **result, int *h_errnop);

int gethostbyname_r(const char *name,

              struct hostent *ret, char *buf, size_t buflen,

              struct hostent **result, int *h_errnop);

int gethostbyname2_r(const char *name, int af,

               struct hostent *ret, char *buf, size_t buflen,

               struct hostent **result, int *h_errnop);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dns