(一)交代了下DNS.C文件的代码,那么在(二)中我再补充下 DNS.h的代码,并做简要说明。
之后是netdb.h的代码和netdb.c的代码!
代码已经注释完。
代码中可能加了注释后,可能会出现一些<p></p>类似这种代码,是因为我编辑时,出现的插入,在csdn提供的编写环境里,会自动添加一些语句。
大家可以根据经验,手动剔除掉,如果大家想直接复制粘贴代码的话。。。
#ifndef __LWIP_DNS_H__
#define __LWIP_DNS_H__
#include "lwip/opt.h"
#if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
#ifdef __cplusplus
extern "C" {
#endif
/** DNS timer period */
#define DNS_TMR_INTERVAL 1000
/** DNS field TYPE used for "Resource Records" */
#define DNS_RRTYPE_A 1 /* a host address ipv4的地址*/
#define DNS_RRTYPE_NS 2 /* an authoritative name server 授权的名字服务*/
#define DNS_RRTYPE_MD 3 /* a mail destination (Obsolete - use MX) 已作废*/
#define DNS_RRTYPE_MF 4 /* a mail forwarder (Obsolete - use MX) 已作废*/
#define DNS_RRTYPE_CNAME 5 /* the canonical name for an alias 别名,可以理解为外号*/
#define DNS_RRTYPE_SOA 6 /* marks the start of a zone of authority 初始授权区域*/
#define DNS_RRTYPE_MB 7 /* a mailbox domain name (EXPERIMENTAL) 实验性质的邮箱域名*/
#define DNS_RRTYPE_MG 8 /* a mail group member (EXPERIMENTAL) 实验性质的*/
#define DNS_RRTYPE_MR 9 /* a mail rename domain name (EXPERIMENTAL) 实验性质的*/
#define DNS_RRTYPE_NULL 10 /* a null RR (EXPERIMENTAL) 实验性质的*/
#define DNS_RRTYPE_WKS 11 /* a well known service description 众所周知的服务*/
#define DNS_RRTYPE_PTR 12 /* a domain name pointer 域名指针,用于指向域名,有时候域名会出现多次,那么重复的就用指针偏移来取代*/
#define DNS_RRTYPE_HINFO 13 /* host information 主机信息 */
#define DNS_RRTYPE_MINFO 14 /* mailbox or mail list information 邮箱列表信息*/
#define DNS_RRTYPE_MX 15 /* mail exchange 邮箱交换*/
#define DNS_RRTYPE_TXT 16 /* text strings 文本字符串*/
/*#define DNS_RRTYPE_AAAA 28 /* ipv6 addr*/ 如果要实现ipv6的DNS,这个必须实现。在请求和响应报文中都要有此类型 */
/** DNS field CLASS used for "Resource Records" 类型 */
#define DNS_RRCLASS_IN 1 /* the Internet 我们最常用的也最常见的,也就是这个*/
#define DNS_RRCLASS_CS 2 /* the CSNET class (Obsolete - used only for examples in some obsolete RFCs) */
#define DNS_RRCLASS_CH 3 /* the CHAOS class */
#define DNS_RRCLASS_HS 4 /* Hesiod [Dyer 87] */
#define DNS_RRCLASS_FLUSH 0x800 /* Flush bit */
/* The size used for the next line is rather a hack, but it prevents including socket.h in all files
that include memp.h, and that would possibly break portability (since socket.h defines some types
and constants possibly already define by the OS).
Calculation rule:
sizeof(struct addrinfo) + sizeof(struct sockaddr_in) + DNS_MAX_NAME_LENGTH + 1 byte zero-termination */
#define NETDB_ELEM_SIZE (32 + 16 + DNS_MAX_NAME_LENGTH + 1)
#if DNS_LOCAL_HOSTLIST
/** struct used for local host-list */
struct local_hostlist_entry {
/** static hostname 域名字符串*/
const char *name;
/** static host address in network byteorder 网络字节序的主机地址*/
ip_addr_t addr;
struct local_hostlist_entry *next;
};
#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
#ifndef DNS_LOCAL_HOSTLIST_MAX_NAMELEN
#define DNS_LOCAL_HOSTLIST_MAX_NAMELEN DNS_MAX_NAME_LENGTH
#endif
#define LOCALHOSTLIST_ELEM_SIZE ((sizeof(struct local_hostlist_entry) + DNS_LOCAL_HOSTLIST_MAX_NAMELEN + 1))
#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
#endif /* DNS_LOCAL_HOSTLIST */
/** Callback which is invoked when a hostname is found. 如果找到了,就回调,callback是回调函数的指针
* A function of this type must be implemented by the application using the DNS resolver.
* @param name pointer to the name that was looked up.
* @param ipaddr pointer to an ip