- connect.c的struct address_list *al = lookup_host(host, 0)
- 调用的
- 在lookup_host()过程中
- /*
- 263 address_list_from_ipv4_addresses (char **vec)
- 设置ip地址,ip域,等*/
- /*
- 717 al = cache_query (host);
- 从cashe中寻找要访问的主机名称*/
- 65 struct addrinfo /*存储地址信息的结构体*/
- 66 {
- 67 int ai_flags; /* Input flags. */
- 68 int ai_family; /* Protocol family for socket. */
- 69 int ai_socktype; /* Socket type. */
- 70 int ai_protocol; /* Protocol for socket. */
- 71 socklen_t ai_addrlen; /* Length of socket address. */
- 72 struct sockaddr *ai_addr; /* Socket address for socket. */
- 73 char *ai_canonname; /* Canonical name for service location. */
- 74 struct addrinfo *ai_next; /* Pointer to next in list. */
- 75 };
- 372 struct gaiwt_context {
- 373 const char *node;
- 374 const char *service;
- 375 const struct addrinfo *hints;
- 376 struct addrinfo **res;
- 377 int exit_code;
- 378 };
复制代码- /*是否运行超时
- err = getaddrinfo_with_timeout (host, NULL, &hints, &res, timeout);/timeout是规定的计时器*/
-
- 2023 if (timeout == 0)
- 2024 {
- 2025 fun (arg);
- 2026 return false;
- 2027 }
- 2028
- 2029 signal (SIGALRM, abort_run_with_timeout);
- 1950 static void
- 1951 alarm_set (double timeout)
- 1952 {
- /*从新设置定时器,并且为addrinfo 类型的hints变量赋值,并且将端口也转化成host转化成inet格式*/
复制代码- 从创建的addrinfo中创建address_list链表 al
- al = address_list_from_addrinfo (res); 完成后,al中存储着所有的ip地址,其中ipv6和ipv4类型在函数中分开
- */
- 打印一些信息到logfp文件*/
复制代码- 846 if (use_cache)
- 847 cache_store (host, al); /*根据host名存储到hash表,其中以host名字作为hash方程的hash元素,al存储到cell->value*/
复制代码结束了*/
返回到
- connect_to_host()函数
- address_list_get_bounds (al, &start, &end); 获得边界*/
- 398 for (i = start; i < end; i++)
- 399 {
- 400 const ip_address *ip = address_list_address_at (al, i);
- 401 sock = connect_to_ip (ip, port, host); /*这个就开始连接网络*/
复制代码- connect_to_ip()函数
- 00 sockaddr_set_data (sa, ip, port); /*struct sockaddr 类型的======>1、________------->*/
- 301
- 302
- 303 sock = socket (sa->sa_family, SOCK_STREAM, 0); /*显然是面向TCP协议的,创建sock文件*/
- 304 if (sock < 0)
- 305 goto err;
复制代码- ========================>
- struct sockaddr {
- unsigned short sa_family; /* address family, AF_xxx */
- char sa_data[14]; /* 14 bytes of protocol address */
- };
- sa_family是地址家族,一般都是“AF_xxx”的形式。好像通常大多用的是都是AF_INET。
- sa_data是14字节协议地址。
- 但一般编程中并不直接针对此数据结构操作,而是使用另一个与sockaddr等价的数据结构
- sockaddr_in(在netinet/in.h中定义):
- struct sockaddr_in {
- short int sin_family; /* Address family */
- unsigned short int sin_port; /* Port number */
- struct in_addr sin_addr; /* Internet address */
- unsigned char sin_zero[8]; /* Same size as struct sockaddr */
- };
复制代码- 1、______________------------->
- 82 static void
- 83 sockaddr_set_data (struct sockaddr *sa, const ip_address *ip, int port)
- 84 {
- 85 switch (ip->family)
- 86 {
- 87 case AF_INET:
- 88 {
- 89 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
- 90 xzero (*sin);
- 91 sin->sin_family = AF_INET;
- 92 sin->sin_port = htons (port);
- 93 sin->sin_addr = ip->data.d4;
- 94 break;
- 95 }
- 96 #ifdef ENABLE_IPV6
- 97 case AF_INET6:
- 98 {
- 99 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
- 100 xzero (*sin6);
- 101 sin6->sin6_family = AF_INET6;
- 102 sin6->sin6_port = htons (port);
- 103 sin6->sin6_addr = ip->data.d6;
- 104 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
- 105 sin6->sin6_scope_id = ip->ipv6_scope;
- 106 #endif
- 107 break;
- 108 }
- 108 }
- 109 #endif /* ENABLE_IPV6 */
- 110 default:
- 111 abort ();
- 112 }
- 113 }
- 114
复制代码[code]
接着就是
322 if (opt.limit_rate && opt.limit_rate < 8192)
323 {
324 int bufsize = opt.limit_rate;
设置buffer大小,至少为512字节*/
343 if (bind (sock, bind_sa, sockaddr_size (bind_sa)) < 0)
/*这是给上面的sock文件编号,使之可以对应进行访问此sock文件*/
connect_with_timeout_________---------->230 connect_with_timeout_callback (void *arg)
231 {
232 struct cwt_context *ctx = (struct cwt_context *)arg;
233 ctx->result = connect (ctx->fd, ctx->addr, ctx->addrlen);
234 }
/*这就是开始连接了,伴随计时开始了*/
阅读(1763) | 评论(0) | 转发(0) |