コード例 #1
0
ファイル: dns.c プロジェクト: InfiniteYuan1/STM32
/*
********************************************************************************
*              MAKE DNS QUERY AND PARSE THE REPLY
*
* Description : This function makes DNS query message and parses the reply from DNS server.
* Arguments   : name - is a pointer to the domain name.
* Returns     : if succeeds : 1, fails : -1
* Note        :
********************************************************************************
*/
uint8 dns_query(uint8 s, uint8 * name)
{
  static uint32 dns_wait_time = 0;
  struct dhdr dhp;
  uint8 ip[4];
  uint16 len, port;
  switch(getSn_SR(s))
  {
    case SOCK_UDP:
      if ((len = getSn_RX_RSR(s)) > 0)
      {
        if (len > MAX_DNS_BUF_SIZE) len = MAX_DNS_BUF_SIZE;
        len = recvfrom(s, BUFPUB, len, ip, &port);
        if(parseMSG(&dhp, BUFPUB))
        {
          close(s);
          return DNS_RET_SUCCESS;
        }
        else 
          dns_wait_time = DNS_RESPONSE_TIMEOUT;
      }
      else
      {
        Delay_ms(1000);
        dns_wait_time++;
        //if(ConfigMsg.debug) printf("dns wait time=%d\r\n", dns_wait_time);
      }
      if(dns_wait_time >= DNS_RESPONSE_TIMEOUT)   // 3ÃÊ
      {
        close(s);
        return DNS_RET_FAIL;
      }
      break;
    case SOCK_CLOSED:
      dns_wait_time = 0;
      socket(s, Sn_MR_UDP, 3000, 0);
      //if(ConfigMsg.debug) printf("dns socket init over\r\n");
      len = dns_makequery(0, name, BUFPUB, MAX_DNS_BUF_SIZE);
      //if(ConfigMsg.debug) printf("dns make query\r\n");
      sendto(s, BUFPUB, len, EXTERN_DNS_SERVERIP, IPPORT_DOMAIN);
      break;         
  }
  return DNS_RET_PROGRESS;
}
コード例 #2
0
ファイル: dns.cpp プロジェクト: 0x1abin/ebox_stm32
/*
********************************************************************************
*              MAKE DNS QUERY AND PARSE THE REPLY
*
* Description : This function makes DNS query message and parses the reply from DNS server.
* Arguments   : name - is a pointer to the domain name.
* Returns     : if succeeds : 1, fails : -1
* Note        :
********************************************************************************
*/
int DNS::query(char *name)
{
    int state;
    static uint32_t dns_wait_time = 0;
    struct dhdr dhp;
    uint8_t ip[4];
    uint16_t len, port;
    uint8_t dns_server_ip[4];
    uint8_t BUFPUB[100];

    get_local_dns(dns_server_ip);
    len = makequery(0, (uint8_t *)name, BUFPUB, MAX_DNS_BUF_SIZE);
    DNS_DBG("ÓòÃû:[%s]\r\n", name);
    DNS_DBG("ÓòÃû½âÎöÖÐ...\r\n");

    while(1)
    {
        state = socket_status(s);
        if(state == SOCK_CLOSED)
        {
            DNS_DBG("dns's socket closed!,socket: %d\r\n", s);
            DNS_DBG("openning dns's socket... !\r\n");
            if(_socket(s, Sn_MR_UDP, port, 0) == 1)
                DNS_DBG("open dns's socket seccusse!\r\n");
        }

        if(state == SOCK_UDP)
        {
            DNS_DBG("sending query cmd...\r\n");
            if(_sendto(s, BUFPUB, len, dns_server_ip, IPPORT_DOMAIN) > 0)
                DNS_DBG("send dns cmd ok!\r\n");

            if ((len = recv_available(s)) > 0)
            {
                if (len > MAX_DNS_BUF_SIZE)
                {
                    len = MAX_DNS_BUF_SIZE;
                }
                len = _recvfrom(s, BUFPUB, len, ip, &port);
                DNS_DBG("receiv dns's date ok!datelen:%d\r\n", len);
                if(parseMSG(&dhp, BUFPUB))
                {
                    _close(s);
                    DNS_DBG("query is ok!\r\n");
                    return 1;
                }
                else
                {
                    DNS_DBG("dns's date is bad!\r\n");
                    return -1;
                }
            }
            else
            {
                DNS_DBG("wait dns's date...!\r\n");
                delay_ms(1000);
                dns_wait_time++;
            }
            if(dns_wait_time >= 3)
            {
                _close(s);
                return -2;
            }
        }
    };
}