UNP学习第九章 基本名字与地址转换

之前都用数值地址来表示主机(206.6.226.33),用数值端口号来标识服务器。

然而,我们应该使用名字而不是数值:名字比较容易记,数值地址可以改变但名字保持不变。

随着往IPv6上转移,数值地址变得更长,手工输入地址更易出错。

一、域名系统

域名系统DNS(Domain Name System)主要用于主机名与IP地址间的映射。

主机名可以是简单的名字,如solaris或bsdi,也可以是全限定域名FQDN,如solaris.kohala.com

严格来说,FQDN也称为绝对名字,因此必须以一个点号结尾,但用户经常省略最后的点号。

资源记录

  DNS中的条目称为资源记录RR(resource record)。

A    A记录将主机名映射为32位IPv4地址。

AAAA   AAAA记录将主机名映射为128位的IPv6地址。

PTR    PTR记录将IP地址映射为主机名。对于IPv4地址,32位地址的四个字节顺序反转,每个字节都转换成十进制ASCII值(0~255),然后附上in-addr.arpa,结果串用于PTR查询。

对于IPv6地址,128位地址中的32个4位组顺序反转,每组被转换成相应的十六禁止ASCII值,并附上ip6.int

例如:33.226.62.206.in-addr.arpa和3.e.3.e.8.7.0.2.0.0.8.0.0.2.0.0.0.0.2.e.e.3.e.c.0.0.f.d.b.l.f.5.ip6.int

MX    MX记录指定一主机作为某主机的“右键交换器”。

CNAME  CNAME代表canonical name规范名字,其常见的用法是为常用服务如ftp和www指派一个CNAME记录。

 

解析器代码读其依赖于系统的配置文件来确定组织的名字服务器们的所在位置(我们用复数“名字服务器们”是因为大多数组织运行多个名字服务器,虽然我们在图中只画了一个本地服务器)。

文件/etc/resolv.conf一般包含本地名字服务器的IP地址。

解析器用UDP给本地名字服务器发查询,如果本地 名字服务器不知道答案,它也用UDP在整个英特网上给其他名字服务器发查询。

二、gethostbyname函数

#include <netdb.h>

struct hostent *gethostbyname(const char *hostname);
返回:成功非空指针,出错空指针,同时设置h_errno
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 address 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 or IPv6 addrs */
};
#define h_addr h_addr_list[0]        /* first address in list */

 gethostbyname执行一个对A记录的查询或对AAAA记录的查询。

三、gethostnyname2函数与IPv6支持

#include <netdb.h>

struct hostent *gethostbyname2(const char *hostname, int family);
返回:成功非空指针,出错空指针,同时设置h_errno
family:AF_INET还是AF_INET6

四、gethostbyaddr函数

#include <netdb.h>

struct hostent *gethostbyaddr(const char *addr, size_t len, int family);
返回:成功非空指针,出错空指针,同时设置h_errno

五、uname函数

#include <sys/utsname.h>

int uname(struct utsname *name);
返回:成功非负值,出错-1
#define UTS_NAMESIZE 16
#define UTS_NODESIZE 256
struct utsname { char sysname[_UTS_NAMESIZE]; /* name of this operating system */ char nodename[_UTS_NODESIZE]; /* name of this node */ char release[_UTS_NAMESIZE]; /* O.S release level */ char version[_UTS_NAMESIZE]; /* O.S version level */ char machine[_UTS_NAMESIZE]; /* hardware type */ };

六、gethostname函数

#include <unistd.h>

int gethostname(char *name, size_t namelen);
返回:0成功,-1出错

七、getservbyname和getservbyport函数

#include <netdb.h>

struct servent *getservbyname(const char *servname, const char *protoname);
返回:成功非空指针,出错空指针
struct servent {
    char *s_name;               /* official service name */
    char **s_allases;           /* alias list */
    int s_port;                 /* port number, network-byte order */
    char *s_proto;              /* protocol to use */
};

给定端口号和可选协议后查找相应的服务

#include <netdb.h>

struct servent *getservbyport(int port, const char *protname);
返回:成功非空指针,出错空指针
无欲速,无见小利。欲速,则不达;见小利,则大事不成。
原文地址:https://www.cnblogs.com/ch122633/p/8478879.html