zoukankan      html  css  js  c++  java
  • LwIP Application Developers Manual5---高层协议之DNS

    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)
  • 相关阅读:
    MAC之基本命令(持续更新)
    Mac下android_sdk配置环境变量
    Eclipse最常用10大快捷键
    Android之使用Jsoup抓取网络数据
    MAC之curl命令
    MAC之cat命令
    Android之FileOutputStream与openFileOutput()的区别
    C# 数字语音wav 提示。。。。。。。。。。。
    HttpWebRequest 获取验证码的图片 并针对有验证码的网页进行Winform登陆。
    经常开车,坐车的朋友请进(看后对你绝对有好处)
  • 原文地址:https://www.cnblogs.com/smartjourneys/p/8207040.html
Copyright © 2011-2022 走看看