Example #1
0
static int _socket_getaddrinfo2(const mp_obj_t host, const mp_obj_t portx, struct addrinfo **resp) {
    const struct addrinfo hints = {
        .ai_family = AF_INET,
        .ai_socktype = SOCK_STREAM,
    };

    mp_obj_t port = portx;
    if (MP_OBJ_IS_SMALL_INT(port)) {
        // This is perverse, because lwip_getaddrinfo promptly converts it back to an int, but
        // that's the API we have to work with ...
        port = mp_obj_str_binary_op(MP_BINARY_OP_MODULO, mp_obj_new_str_via_qstr("%s", 2), port);
    }

    const char *host_str = mp_obj_str_get_str(host);
    const char *port_str = mp_obj_str_get_str(port);

    if (host_str[0] == '\0') {
        // a host of "" is equivalent to the default/all-local IP address
        host_str = "0.0.0.0";
    }

    MP_THREAD_GIL_EXIT();
    int res = lwip_getaddrinfo(host_str, port_str, &hints, resp);
    MP_THREAD_GIL_ENTER();

    return res;
}
// returns IPAddress(0, 0, 0, 0) on fail
IPAddress MTD_FLASHMEM NSLookup::lookup(char const *hostname) {
  APtr<char> memHostName(f_strdup(hostname));
  addrinfo *addrinfo;
  if (lwip_getaddrinfo(memHostName.get(), NULL, NULL, &addrinfo) == 0 && addrinfo) {
    // according to lwip documentation uses only first item of "addrinfo"
    sockaddr_in *sa = (sockaddr_in *)(addrinfo->ai_addr);
    in_addr_t addr = sa->sin_addr.s_addr;
    lwip_freeaddrinfo(addrinfo);
    return IPAddress(addr);
  }
  return IPAddress(0, 0, 0, 0); // fail!
}
Example #3
0
int
curl_dogetaddrinfo(const char *hostname,
                   const char *service,
                   const struct addrinfo *hints,
                   struct addrinfo **result,
                   int line, const char *source)
{
#ifdef USE_LWIPSOCK
  int res=lwip_getaddrinfo(hostname, service, hints, result);
#else
  int res=(getaddrinfo)(hostname, service, hints, result);
#endif
  if(0 == res)
    /* success */
    curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
                source, line, (void *)*result);
  else
    curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
                source, line);
  return res;
}
int LWIP_SOCKETS_Driver::GetAddrInfo(const char* nodename, char* servname, const SOCK_addrinfo* hints, SOCK_addrinfo** res)
{ 
#if LWIP_DNS
    NATIVE_PROFILE_PAL_NETWORK();

    SOCK_addrinfo *ai;
    SOCK_sockaddr_in *sa = NULL;
    int total_size = sizeof(SOCK_addrinfo) + sizeof(SOCK_sockaddr_in);
    struct addrinfo *lwipAddrinfo = NULL;

    if(res == NULL) return -1;

    *res = NULL;

    // if the nodename == "" then return the IP address of this device
    if(nodename[0] == 0 && servname == NULL)
    {
        struct netif *pNetIf = netif_find_interface(g_LWIP_SOCKETS_Driver.m_interfaces[0].m_interfaceNumber);

        if(pNetIf == NULL) return -1;

        ai = (SOCK_addrinfo*)mem_malloc(total_size);
        if (ai == NULL) 
        {
            return -1;
        }
        memset(ai, 0, total_size);
        sa = (SOCK_sockaddr_in*)((u8_t*)ai + sizeof(SOCK_addrinfo));
        /* set up sockaddr */
        sa->sin_addr.S_un.S_addr = pNetIf->ip_addr.addr;
        sa->sin_family = AF_INET;
        sa->sin_port = 0;
        
        /* set up addrinfo */
        ai->ai_family = AF_INET;
        if (hints != NULL) 
        {
            /* copy socktype & protocol from hints if specified */
            ai->ai_socktype = hints->ai_socktype;
            ai->ai_protocol = hints->ai_protocol;
        }

        ai->ai_addrlen = sizeof(SOCK_sockaddr_in);
        ai->ai_addr = (SOCK_sockaddr*)sa;

        *res = ai;

        return 0;
    }

    int err = lwip_getaddrinfo(nodename, servname, (addrinfo*)hints, &lwipAddrinfo);

    if(err == 0)
    {
        ///
        /// Marshal addrinfo data
        ///
        struct sockaddr_in* lwip_sockaddr_in;
        
        ai = (SOCK_addrinfo*)mem_malloc(total_size);
        if (ai == NULL) 
        {
            lwip_freeaddrinfo(lwipAddrinfo);
            return -1;
        }
        memset(ai, 0, total_size);

        lwip_sockaddr_in = ((struct sockaddr_in*)lwipAddrinfo->ai_addr);

        sa = (SOCK_sockaddr_in*)((u8_t*)ai + sizeof(SOCK_addrinfo));
        /* set up sockaddr */
        sa->sin_addr.S_un.S_addr = lwip_sockaddr_in->sin_addr.s_addr;
        sa->sin_family = lwip_sockaddr_in->sin_family;
        sa->sin_port = lwip_sockaddr_in->sin_port;
        
        /* set up addrinfo */
        ai->ai_family = lwipAddrinfo->ai_family;
        if (hints != NULL) 
        {
            /* copy socktype & protocol from hints if specified */
            ai->ai_socktype = hints->ai_socktype;
            ai->ai_protocol = hints->ai_protocol;
        }
        
        ai->ai_addrlen = sizeof(SOCK_sockaddr_in);
        ai->ai_addr = (SOCK_sockaddr*)sa;
        
        *res = ai;

        // free marshalled addrinfo
        lwip_freeaddrinfo(lwipAddrinfo);
        
    }
    else
    {
        err = -1;
    }
 
    return err;
#else
    return -1;
#endif
}