CSAPP读书笔记11-01

CSAPP读书笔记11-01

最近找工作的时候发现自己以前看的网络编程方面的知识长时间没有回顾,忘记了不少。找来CSAPP直接看看网络部分相关内容,看的是英文版本,虽然有点慢,但收益颇多。

13.3 IP网络概览

13.3.1 IP地址

  • 使用32位IP地址映射所有主机

  • 使用域名标识IP地址

  • 因特网上所有主机之间可以使用IP地址进行通信

    struct in_addr {
    unsignend int s_addr; /大端模式(big endian)/
    }
    /*头文件netinet/in.h */

    现实中不同主机存在不同的字节序,大端模式和小端模式。为了统一相关的数据表达方式,
    定义网络字节序(大端模式)。

    include <netinet/in.h>

    unsigned long int htonl(unsigned long int hostlong);
    unsigned short int htons(unsigned short int hostshort);
    Returns: value in network byte order
    unsigned long int ntohl(unsigned long int netlong);
    unsigned short int ntohs(unsigned short int netshort);
    Returns: value in host byte order

    htonl将32位整数主机字节序转化成网络字节序。htons将16位整数数转化为16位的网络字节序。相对的,ntohl将32位网络字节序转化为32位的主机字节序,nstoh则表示将16位的网络字节序转化为16位的主机字节序。(ps: h---host 本地主机;to---到;n---net 网络;l 是 unsigned long;s表示short;l 表示long)。为了便于人们建议,一般将IP地址用点画十进制来表示。例如,将0x8002c2f2表示成128.2.194.242。在linux下可以使用下面的命令查看本机的ip地址。

    wolf@wolf:~$ hostname -i
    127.0.0.1
    如果我们需要将点画十进制和32位整数进行相互转化,可以使用下面的函数:

    include <arpa/inet.h>

    int inet_aton(const char *cp, struct in_addr *inp);
    Returns: 1 if OK, 0 on error
    char *inet_ntoa(struct in_addr in);
    Returns: pointer to a dotted-decimal string

13.3.2域名

无意义的整数系统对于人的记忆系统来说还是比较难以记忆的,所有为了便于人类记忆,于是设计了一套域名管理机制,将域名与IP地址进行映射。

/* DNS host entry structure */
struct hostent {
char h_name; / Official domain name of host */
char *h_aliases;/ Null-terminated array of domain names /
int h_addrtype; /
Host address type (AF_INET) /
int h_length; /
Length of an address, in bytes /
char *h_addr_list; / Null-terminated array of in_addr structs /
}; /
头文件 netdb.h
/
hostent数据结构是host entry简写。网络程序通过gethostbyname或者gethostbyaddr向DNS服务器检索任意的域名信息。

include <netdb.h>

struct hostent *gethostbyname(const char *name);
Returns: non-NULL pointer if OK, NULL pointer on error with h_errno set
struct hostent *gethostbyaddr(const char *addr, int len, 0);
Returns: non-NULL pointer if OK, NULL pointer on error with h_errno set
实例:

1/******************************************************************************
2 * FileName: gethostinfo.c
3 * Desc:
4 * Author: Zhang Wenwei
5 * Email: 1026367056@qq.com
6 * HomePage: http://www.cnblogs.com/ti1023/
7 * Version: 0.0.1
8 * LastChange: 2016-09-24 14:03:58
9 * History:
10 ******************************************************************************/
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 #include<arpa/inet.h>
15 #include<netinet/in.h>
16 #include<netdb.h>
17
18 int main(int argc, char **argv)
19 {
20 char **pp;
21 struct in_addr addr;
22 struct hostent *hostp;
23
24 if(argc != 2) {
25 printf("usage: %s \n", argv[1]);
26 return 0;
27 }
28
29 if(inet_aton(argv[1], &addr) != 0)
30 hostp = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
31 else
32 hostp = gethostbyname(argv[1]);
33 printf("Offical hostname:%s\n", hostp->h_name);
34 for(pp = hostp->h_aliases; *pp != NULL; pp++)
35 printf("alias: %s\n", *pp);
36
37 for(pp = hostp->h_addr_list; *pp != NULL; pp++) {
38 addr.s_addr = ((struct in_addr )pp)->s_addr;
39 printf("%s\n", inet_ntoa(addr));
40 }
41 return 0;
42 }
运行结果:

wolf@wolf:~/Workspace/gethost$ ./gethostinfo www.google.com
Offical hostname:www.google.com
93.46.8.89
wolf@wolf:~/Workspace/gethost$

posted @ 2016-09-24 14:41  寒雪安东侯  阅读(184)  评论(0)    收藏  举报