Live & Learn
分类: LINUX
2017-04-21 17:04:46
域名解析功能,所以把LWIP的DNS添加上去了,走了写弯路,写出来,给大家借鉴下:
1. 先把宏定义添加上去
#ifndef LWIP_DNS
#define LWIP_DNS 1
#endif
2. 在系统初始化调用
dns_init();
3. 在while(1)之前或者在你认为需要调用的地方调用dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found, void *found)
其中,hostname为你需要解析的域名,addr为解析返回的IP地址指针,found回调函数。
4.特别注意:配置正确的DNS服务器地址。(尽量通过DHCP获取DNS服务器地址,不要手动填写)
/** DNS server IP address */
#ifndef DNS_SERVER_ADDRESS
#define DNS_SERVER_ADDRESS(ipaddr) (ip4_addr_set_u32(ipaddr, ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */
#endif
5. 如果没有使用操作系统,需要自己添加定时处理函数,例如在LwIP_Periodic_Handle函数中添加:
#if LWIP_DNS
if (localtime - DNScoarseTimer >= DNS_TMR_INTERVAL)
{
DNScoarseTimer = localtime;
dns_tmr();
}
#endif
//
Raw API applications can use the dns_gethostbyname() function to request a lookup, and specify a callback function to notify the application when the lookup is complete. As you can see from its header bellow, this function will return immediately. If the requested address is already known, it will be returned via the passed argument pointer. If a request to a DNS server needs to be made, your callback function will be called when it has finshed.
* err_t * dns_gethostbyname(const char *hostname, struct ip_addr *addr, dns_found_callback found, * void *callback_arg) * * Resolve a hostname (string) into an IP address. * NON-BLOCKING callback version for use with raw API!!! * * Returns immediately with one of err_t return codes: * - ERR_OK if hostname is a valid IP address string or the host * name is already in the local names table. * - ERR_INPROGRESS enqueue a request to be sent to the DNS server * for resolution if no errors are present. * * @param hostname the hostname that is to be queried * @param addr pointer to a struct ip_addr where to store the address if it is already * cached in the dns_table (only valid if ERR_OK is returned!) * @param found a callback function to be called on success, failure or timeout (only if * ERR_INPROGRESS is returned!) * @param callback_arg argument to pass to the callback function * callback function and argument defined as follows: * 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 a struct ip_addr containing the IP address of the hostname, * or NULL if the name could not be found (or on any other error). * @param callback_arg a user-specified callback argument passed to dns_gethostbyname: * * typedef void (*dns_found_callback)(const char *name, struct ip_addr *ipaddr, void *callback_arg);
A sample call:
struct ip_addr resolved; switch(dns_gethostbyname("", &resolved, smtp_serverFound, NULL)){ case ERR_OK: // numeric or cached, returned in resolved smtp.serverIP.addr = resolved->addr; smtp.state = SMTP_NAME_RESOLVED; break; case ERR_INPROGRESS: // need to ask, will return data via callback smtp.state = SMTP_NAME_RESOLVING; break; default: // bad arguments in function call break; }
A sample DNS callback function:
void smtp_serverFound(const char *name, struct ip_addr *ipaddr, void *arg) { if ((ipaddr) && (ipaddr->addr)) { smtp.serverIP.addr = ipaddr->addr; smtp.state = SMTP_NAME_RESOLVED; if (smtp_connect() == ERR_OK) return; smtp.lastError = SMTP_CONNECT_FAILED; } else smtp.lastError = SMTP_UNKNOWN_HOST; smtp.state = SMTP_IDLE; }
err_t netconn_gethostbyname(const char *name, ip_addr_t *addr)
See also .