1.前言

lwIP提供一个基本的DNS客户端(1.3.0后引进),通过使用DNS(Domain Name System)协议来允许应用程序解决主机名到地址的转换。

在文件lwipopts.h里面定义LWIP_DNS的值为非零值可以使能DNS。

如果DHCP与lwIP DNS客户端一起工作,那么DNS将会自动被配置使用提供的DNS服务器(如果DHCP提供一个)。

2.Application DNS requests with Raw/Native API

Raw API应用可以使用dns_gethostbyname()函数来请求一次查找,并指定一个回调函数当查找结束时来通知应用程序。

你可以通过如下来阅读函数头部,该函数会马上返回。如果请求的地址已经知道,那么该函数会马上通过指针参数返回。

当一个DNS服务器的请求结束时,你的回调函数将会被调用

* 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("www.lwIP.com", &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;
}

3.Application DNS requests with Netconn API

err_t netconn_gethostbyname(const char *name, ip_addr_t *addr)

See also netconn_gethostbyname().

4.Application DNS requests with Sockets API

For socket based apps, a gethostbyname() wrapper function is provided that blocks during the lookup if necessary. Use the following functions:

  • gethostbyname() - standard implementation, blocks if necessary until lookup complete or fails
  • gethostbyname_r() - thread-safe version of gethostbyname (separate buffer for each invokation)

5.External References

DNS-related RFCs

6.Revisions

  • Local static host lookup table support (added in 1.3.1)
  • Return multiple IP addresses (future)
  • Return other RR (record) types e.g. MX, SRV... (future)
  • implement a "Dynamic DNS update" message (future)

LwIP Application Developers Manual5---高层协议之DNS的更多相关文章

  1. LwIP Application Developers Manual5---高层协议之DHCP,AUTOIP,SNMP,PPP

    1.前言 本文主要讲述高层协议,包括DHCP 2.DHCP 2.1 从应用的角度看DHCP 你必须确保在编译和链接时使能DHCP,可通过在文件lwipopts.h里面定义LWIP_DHCP选项,该选项 ...

  2. LwIP Application Developers Manual2---Protocols概览

    1.前言 本文是对LwIP Application Developers Manual的翻译 lwIP是模块化的并支持广泛的协议,这些大部分协议可以被裁减从而减小代码的尺寸 2.协议概览 链路层和网络 ...

  3. LwIP Application Developers Manual1---介绍

    1.前言 本文主要是对LwIP Application Developers Manual的翻译 2.读者(应用开发手册的读者) 谁适合读这份手册 网络应用的开发者 想了解lwIP的网络应用开发者 阅 ...

  4. LwIP Application Developers Manual3---链路层和网络层协议之ARP,IPV4

    1.前言 本文主要讲述链路层和网络层的几种协议:ARP,ipv4 2. ARP 2.1 ARP的主要应用 ARP的主要应用是在与互联网相连的以太网网络层,该层需要一些机制将MAC地址(该地址主要由制造 ...

  5. LwIP Application Developers Manual6---Application API layers

    1.前言 lwIP提供3种应用编程接口来跟TCP/IP内核通信,如下所示: 低水平的内核/回调或raw API 2个高水平序列API: 1) netconn API 2) socket API(为了兼 ...

  6. LwIP Application Developers Manual3---链路层和网络层协议之IPV6,ICMP,IGMP

    1.前言 本文主要讲述链路层和网络层的协议IPV6,ICMP 2.IPV6 2.1 IPV6特性 IPv6是IPv4的更新.其最显著的差别在于地址空间由32位转换成128位 2.2 从应用的角度看IP ...

  7. LwIP Application Developers Manual9---LwIP and multithreading

    1.前言 lwIP的内核并不是线程安全的.如果我们必须在多线程环境里使用lwIP,那么我们必须使用“upper”API层的函数(netconn或sockets).当使用raw API时,你需要自己保护 ...

  8. LwIP Application Developers Manual7---lwIP with or without an operating system

    1.前言 最近有一些讨论关于lwIP如何在单机的环境(比如,没有一个多线程的操作系统)使用. 本文的目的就是描述lwIP如何在无多线程操作系统或有多线程操作系统环境中运行 2.lwIP单线程内核 2. ...

  9. LwIP Application Developers Manual4---传输层之UDP、TCP

    1.前言 本文主要讲解传输层协议UDP TCP 2.UDP 2.1 UDP from an application perspective 2.2 UDP support history in lwI ...

随机推荐

  1. 2017-12-14python全栈9期第一天第六节之用户交互

    9,用户交互.input 1,等待输入, 2,将你输入的内容赋值给了前面变量. 3,input出来的数据类型全部是str 10,基础数据类型初始.数字:int 12,3,45 + - * / ** % ...

  2. MySQL权限授权认证详解

    MySQL权限授权认证详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL权限系统介绍1>.权限系统的作用是授予来自某个主机的某个用户可以查询.插入.修改.删除 ...

  3. Pycharm搭建Django开发环境

    Pycharm搭建Django开发环境 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们大家都知道Django是python都一个web框架,因此大家需要自行安装python环境 ...

  4. HDFS集群优化篇

    HDFS集群优化篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.操作系统级别优化 1>.优化文件系统(推荐使用EXT4和XFS文件系统,相比较而言,更推荐后者,因为XF ...

  5. Python中集合的操作

    Python集合的基本详情 集合是无序的 集合是可变数据类型 集合属于不可哈希范围 集合自动去重 集合的操作 set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} ...

  6. JAVA核心技术I---JAVA基础知识(文件系统及java文件基本操作)

    一:文件概述 文件系统是由OS(操作系统)管理的 文件系统和Java进程是平行的,是两套系统 文件系统是由文件夹和文件递归组合而成 文件目录分隔符 –Linux/Unix 用/隔开 –Windows用 ...

  7. SQL记录-ORACLE 12C初体验

    1.部署 2.使用

  8. EJB到底是什么

    EJB到底是什么?   1. 我们不禁要问,什么是"服务集群"?什么是"企业级开发"? 既然说了EJB 是为了"服务集群"和"企业 ...

  9. springBoot打包发布项目------jar包

    这两年微服务很流行,这里简单介绍一下如何将自己使用idea写的微服务打包成一个可执行的jar包,并发布到linux服务器的步骤.因为spring boot有内置的tomcat所以一般使用内置的tomc ...

  10. 计算机网络--HTTP协议

    TCP/IP协议 互联网构建的初衷是信息的共享.在信息的传递过程中,计算机不可避免的需要产生交流.就像我们与别人交谈需要懂得对方的语言才能明白对方表达的意思一样,计算机的交流也需要一个约束了,称之为协 ...