//------------------------------------------------------------------------------- // TCP client start //------------------------------------------------------------------------------- err_t ICACHE_FLASH_ATTR tc_go(void) { err_t err = ERR_USE; if((tc_init_flg & TC_RUNNING) || iot_data_processing == NULL) return err; // выход, если процесс запущен или нечего запускать #if DEBUGSOO > 4 os_printf("Run: %x, %u\n", iot_data_processing, iot_data_processing->min_interval); #endif err = tc_init(); // инициализация TCP if(err == ERR_OK) { tc_init_flg |= TC_RUNNING; // процесс запущен run_error_timer(TCP_REQUEST_TIMEOUT); // обработать ошибки и продолжение #if DEBUGSOO > 4 int i; for (i = 0; i < DNS_TABLE_SIZE; ++i) { os_printf("TDNS%d: %d, %s, " IPSTR " (%d)\n", i, dns_table[i].state, dns_table[i].name, IP2STR(&dns_table[i].ipaddr), dns_table[i].ttl); } #endif err = dns_gethostbyname(iot_server_name, &tc_remote_ip, (dns_found_callback)tc_dns_found_callback, NULL); #if DEBUGSOO > 4 os_printf("dns_gethostbyname(%s)=%d ", iot_server_name, err); #endif if(err == ERR_OK) { // Адрес разрешен из кэша или локальной таблицы err = tcpsrv_client_start(tc_servcfg, tc_remote_ip.addr, DEFAULT_TC_HOST_PORT); } else if(err == ERR_INPROGRESS) { // Запущен процесс разрешения имени с внешнего DNS err = ERR_OK; } if (err != ERR_OK) { tc_init_flg &= ~TC_RUNNING; // процесс не запущен // tc_close(); } } return err; }
// Lua: net.dns.resolve( domain, function(sk, ip) ) static int net_dns_static( lua_State* L ) { size_t dl; const char* domain = luaL_checklstring(L, 1, &dl); if (!domain && dl > 128) { return luaL_error(L, "wrong domain"); } luaL_checkanyfunction(L, 2); lua_pushvalue(L, 2); // copy argument (func) to the top of stack int cbref = luaL_ref(L, LUA_REGISTRYINDEX); if (cbref == LUA_NOREF) { return luaL_error(L, "wrong callback"); } int *cbref_ptr = c_zalloc(sizeof(int)); cbref_ptr[0] = cbref; ip_addr_t addr; err_t err = dns_gethostbyname(domain, &addr, net_dns_static_cb, cbref_ptr); if (err == ERR_OK) { net_dns_static_cb(domain, &addr, cbref_ptr); return 0; } else if (err == ERR_INPROGRESS) { return 0; } else { int e = lwip_lua_checkerr(L, err); c_free(cbref_ptr); return e; } return 0; }
int GAgent_GetHostByName(char *domain, char *IPAddress) { unsigned char Err; ip_addr_t ip_addr; char str[32]; memset(str, 0x0, sizeof(str)); switch (dns_gethostbyname(domain, &ip_addr, Gagent_ServerFound, NULL)) { case ERR_OK: memcpy((unsigned char*)IPAddress, (unsigned char*)inet_ntoa(ip_addr.addr), strlen(inet_ntoa(ip_addr.addr)) + 1); GAgent_Printf(GAGENT_LOG, "ok Server name %s, first address: %s", domain, inet_ntoa(domain_ipaddr.addr)); return 0; case ERR_INPROGRESS: GAgent_Printf(GAGENT_LOG, "dns_gethostbyname called back success.\n\r"); /* Waiting for connect success */ OSSemPend(pdomainSem, 1000, &Err); if (domain_flag == 1) { memcpy((unsigned char*)IPAddress, (unsigned char*)inet_ntoa(domain_ipaddr.addr), strlen(inet_ntoa(domain_ipaddr.addr)) + 1); GAgent_Printf(GAGENT_LOG, "call back Server name %s, first address: %s", domain, inet_ntoa(domain_ipaddr.addr)); return 0; } else { return -1; } break; default: GAgent_Printf(GAGENT_LOG, "dns_gethostbyname called back error.\n\r"); return -1; } }
/** * Send out an sntp request via raw API. * * @param arg is unused (only necessary to conform to sys_timeout) */ static void sntp_request(void *arg) { ip_addr_t sntp_server_address; err_t err; LWIP_UNUSED_ARG(arg); /* Initialize SNTP server address */ #if SNTP_SERVER_DNS err = dns_gethostbyname(sntp_server_addresses[sntp_current_server], &sntp_server_address, sntp_dns_found, NULL); if (err == ERR_INPROGRESS) { /* DNS request sent, wait for sntp_dns_found being called */ LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_request: Waiting for server address to be resolved.\n")); return; } #else /* SNTP_SERVER_DNS */ err = ipaddr_aton(sntp_server_addresses[sntp_current_server], &sntp_server_address) ? ERR_OK : ERR_ARG; #endif /* SNTP_SERVER_DNS */ if (err == ERR_OK) { sntp_send_request(&sntp_server_address); } else { /* address conversion failed, try another server */ LWIP_DEBUGF(SNTP_DEBUG_WARN_STATE, ("sntp_request: Invalid server address, trying next server.\n")); sys_timeout((u32_t)SNTP_RETRY_TIMEOUT, sntp_try_next_server, NULL); } }
static void handle_req_state(void) { switch (req_state) { case REQ_DNS_INIT: CYG_TEST_INFO("Trying to resolve host name"); if (dns_gethostbyname(CYGDAT_NET_LWIP_PPP_TEST_HOST, &host_addr, dns_found_cb, NULL) == ERR_OK) { // Cached req_state = REQ_DNS_SUCCESS; break; } req_state = REQ_DNS_WAIT; break; case REQ_DNS_WAIT: break; case REQ_DNS_FAILED: CYG_TEST_INFO("Failed to resolve host name"); test_state = TEST_PPP_CLOSE; break; case REQ_DNS_SUCCESS: CYG_TEST_INFO("Successfully resolved host name"); success++; test_state = TEST_PPP_CLOSE; break; } }
void NtpClient::requestTime() { if (!WifiStation.isConnected()) { connectionTimer.initializeMs(1000, TimerDelegate(&NtpClient::requestTime, this)).startOnce(); return; } struct ip_addr resolvedIp; int result = dns_gethostbyname(this->server.c_str(), &resolvedIp, staticDnsResponse, (void*) this); switch (result) { case ERR_OK: // Documentation says this will be the result if the string is already // an ip address in dotted decimal form or if the host is found in dns cache. // however I'm not sure if the dns cache is working since this never seems to // be called for a host lookup other than an ip address. // Doesn't really matter since the loockup will be fast anyways, the host // is most likely found in the dns cache of the next node the query is sent to. internalRequestTime(resolvedIp); break; case ERR_INPROGRESS: // currently finding ip, internalRequestTime() will be called when its found. //debugf("DNS IP lookup in progress."); break; default: debugf("DNS lookup error occurred."); break; } }
/****************************************************************************** * FunctionName : on_timer * Description : 1s time callback to check dns found * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ static void ICACHE_FLASH_ATTR on_timer(void *arg) { int err = -1; struct user_dns_param *param = (struct user_dns_param *)arg; switch(param->ip.addr) { case 0xFFFFFFFF: case 0x00000000: if(0 < param->retry--) { DEBUG_WARNING("retry\n"); err = dns_gethostbyname(param->hostname, ¶m->ip, on_found, param); os_timer_arm(¶m->tmr, 1000, 0); return; } else { err = ETIMEDOUT; } break; default: DEBUG_INFO("callback\n"); err = 0; break; } param->h(err, ¶m->ip, param); }
bool TcpConnection::connect(String server, int port) { if (tcp == NULL) initialize(tcp_new()); ip_addr_t addr; debugf("connect to: %s", server.c_str()); canSend = false; // Wait for connection DnsLookup *look = new DnsLookup { this, port }; err_t dnslook = dns_gethostbyname(server.c_str(), &addr, staticDnsResponse, look); if (dnslook != ERR_OK) { if (dnslook == ERR_INPROGRESS) return true; else { delete look; return false; } } delete look; return internalTcpConnect(addr, port); }
/** * Resolve the given hostname to an IP address. * @param aHostname Name to be resolved * @param aResult IPAddress structure to store the returned IP address * @return 1 if aIPAddrString was successfully converted to an IP address, * else error code */ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) { ip_addr_t addr; aResult = static_cast<uint32_t>(0); if(aResult.fromString(aHostname)) { // Host name is a IP address use it! DEBUG_WIFI_GENERIC("[hostByName] Host: %s is a IP!\n", aHostname); return 1; } DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname); err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); if(err == ERR_OK) { aResult = addr.addr; } else if(err == ERR_INPROGRESS) { esp_yield(); // will return here when dns_found_callback fires if(aResult != 0) { err = ERR_OK; } } if(err != 0) { DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, err); } else { DEBUG_WIFI_GENERIC("[hostByName] Host: %s IP: %s\n", aHostname, aResult.toString().c_str()); } return (err == ERR_OK) ? 1 : 0; }
//Execute request & return OK if found, NOTFOUND or ERROR on error, or PROCESSING if the request has not completed yet void LwipNetDnsRequest::poll() { err_t err; switch(m_state) { case LWIPNETDNS_START: //First req, let's call dns_gethostbyname ip_addr_t ipStruct; err = dns_gethostbyname(m_hostname, &ipStruct, LwipNetDnsRequest::sFoundCb, (void*) this ); if( err == ERR_OK ) { m_ip = IpAddr(&ipStruct); m_state = LWIPNETDNS_OK; DBG("DNS: Ip found in cache.\n"); } else if( err == ERR_INPROGRESS) { DBG("DNS: Processing.\n"); m_state = LWIPNETDNS_PROCESSING; } else //Likely ERR_VAL { DBG("DNS: Error on init.\n"); m_state = LWIPNETDNS_ERROR; } break; case LWIPNETDNS_PROCESSING: break; //Nothing to do, DNS is polled on interrupt case LWIPNETDNS_OK: if(!m_cbFired) { DBG("DNS: Ip found.\n"); m_cbFired = true; onReply(NETDNS_FOUND); //Raise callback } break; case LWIPNETDNS_NOTFOUND: if(!m_cbFired) { DBG("DNS: could not be resolved.\n"); m_cbFired = true; onReply(NETDNS_NOTFOUND); //Raise callback } break; case LWIPNETDNS_ERROR: default: if(!m_cbFired) { DBG("DNS: Error.\n"); m_cbFired = true; onReply(NETDNS_ERROR); //Raise callback } break; } if(m_closing && (m_state!=LWIPNETDNS_PROCESSING)) //Check wether the closure has been reqd { DBG("LwipNetDnsRequest: Closing in poll()\n"); NetDnsRequest::close(); } }
void dns_suite(void) { evdns_init(); dns_gethostbyname(); dns_gethostbyaddr(); evdns_shutdown(0); }
/** * SNTP thread */ static void sntp_thread(void *arg) { LWIP_UNUSED_ARG(arg); while (1) { dns_gethostbyname(SNTP_SERVER_NAME, &SNTPaddr, sntp_server_found, NULL); vTaskSuspend(NULL); sntp_request(); sys_msleep(SNTP_UPDATE_DELAY); } }
static int tls_socket_connect( lua_State *L ) { tls_socket_ud *ud = (tls_socket_ud *)luaL_checkudata(L, 1, "tls.socket"); luaL_argcheck(L, ud, 1, "TLS socket expected"); if(ud==NULL){ NODE_DBG("userdata is nil.\n"); return 0; } if (ud->pesp_conn) { return luaL_error(L, "already connected"); } u16 port = luaL_checkinteger( L, 2 ); size_t il; const char *domain = "127.0.0.1"; if( lua_isstring(L, 3) ) domain = luaL_checklstring( L, 3, &il ); if (port == 0) return luaL_error(L, "invalid port"); if (domain == NULL) return luaL_error(L, "invalid domain"); ud->pesp_conn = (struct espconn*)c_zalloc(sizeof(struct espconn)); if(!ud->pesp_conn) return luaL_error(L, "not enough memory"); ud->pesp_conn->proto.udp = NULL; ud->pesp_conn->proto.tcp = (esp_tcp *)c_zalloc(sizeof(esp_tcp)); if(!ud->pesp_conn->proto.tcp){ c_free(ud->pesp_conn); ud->pesp_conn = NULL; return luaL_error(L, "not enough memory"); } ud->pesp_conn->type = ESPCONN_TCP; ud->pesp_conn->state = ESPCONN_NONE; ud->pesp_conn->reverse = ud; ud->pesp_conn->proto.tcp->remote_port = port; espconn_regist_connectcb(ud->pesp_conn, (espconn_connect_callback)tls_socket_onconnect); espconn_regist_disconcb(ud->pesp_conn, (espconn_connect_callback)tls_socket_ondisconnect); espconn_regist_reconcb(ud->pesp_conn, (espconn_reconnect_callback)tls_socket_onreconnect); espconn_regist_recvcb(ud->pesp_conn, (espconn_recv_callback)tls_socket_onrecv); espconn_regist_sentcb(ud->pesp_conn, (espconn_sent_callback)tls_socket_onsent); if (ud->self_ref == LUA_NOREF) { lua_pushvalue(L, 1); // copy to the top of stack ud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); } ip_addr_t addr; err_t err = dns_gethostbyname(domain, &addr, (dns_found_callback)tls_socket_dns_cb, ud); if (err == ERR_OK) { tls_socket_dns_cb(domain, &addr, ud); } else if (err != ERR_INPROGRESS) { tls_socket_dns_cb(domain, NULL, ud); } return 0; }
void set_clock_sntp(const char * ntp_server, int i) { strcpy(dns_hostname, ntp_server); //To get the ball rolling, all we have to do is send a request to get the NTP server's IP address. dns_gethostbyname(dns_hostname, &ntp_server_ip, send_ntp_req, NULL); //...oh, and save the system GMT offset for later. gmt_offset = i; }
void dns_dorequest(void *arg) { char* dnsname = "3com.com"; ip_addr_t dnsresp; LWIP_UNUSED_ARG(arg); if (dns_gethostbyname(dnsname, &dnsresp, dns_found, 0) == ERR_OK) { dns_found(dnsname, &dnsresp, 0); } }
void ninit(void) { sntp_init(); ip_addr_t ip; int r = dns_gethostbyname("pool.ntp.org", &ip, ntp_dns_found, NULL); if (r == ESPCONN_OK) { sntp_setserver(0, &ip); } }
/******************************************************************************* ** Name: WebDNS ** Input:void ** Return: void ** Owner:zhuzhe ** Date: 2014.6.25 ** Time: 15:33:52 *******************************************************************************/ _WEB_TASK_WEBTASK_READ_ COMMON API void WebDNS(void) { #if LWIP_DNS { char hostname[] = "www.baidu.com"; ip_addr_t addr; dns_gethostbyname(hostname, &addr, dns_found, NULL); } #endif }
/** * Execute a DNS query * Called from netconn_gethostbyname * * @param arg the dns_api_msg pointing to the query */ void do_gethostbyname(void *arg) { struct dns_api_msg *msg = (struct dns_api_msg*)arg; *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg); if (*msg->err != ERR_INPROGRESS) { /* on error or immediate success, wake up the application * task waiting in netconn_gethostbyname */ sys_sem_signal(msg->sem); } }
/** * Execute a DNS query * Called from netconn_gethostbyname * * @param arg the dns_api_msg pointing to the query */ void lwip_netconn_do_gethostbyname(void *arg) { struct dns_api_msg *msg = (struct dns_api_msg*)arg; API_EXPR_DEREF(msg->err) = dns_gethostbyname(msg->name, API_EXPR_REF(msg->addr), lwip_netconn_do_dns_found, msg); if (API_EXPR_DEREF(msg->err) != ERR_INPROGRESS) { /* on error or immediate success, wake up the application * task waiting in netconn_gethostbyname */ sys_sem_signal(API_EXPR_REF(msg->sem)); } }
bool AsyncClient::connect(const char* host, uint16_t port){ ip_addr_t addr; err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_s_dns_found, this); if(err == ERR_OK) { return connect(IPAddress(addr.u_addr.ip4.addr), port); } else if(err == ERR_INPROGRESS) { _connect_port = port; return true; } log_e("error: %d", err); return false; }
void dns_suite(void) { dns_server(); /* Do this before we call evdns_init. */ evdns_init(); dns_gethostbyname(); dns_gethostbyname6(); dns_gethostbyaddr(); evdns_shutdown(0); }
static socket_error_t lwipv4_socket_resolve(struct socket *sock, const char *address) { struct ip_addr ia; // attempt to resolve with DNS or convert to ip addr err_t err = dns_gethostbyname(address, &ia, dnscb, sock); if (err == ERR_OK) { dnscb(address, &ia, sock); } if (err == ERR_INPROGRESS) err = ERR_OK; return lwipv4_socket_error_remap(err); }
// Lua: client:connect(port, addr) int net_connect( lua_State *L ) { lnet_userdata *ud = net_get_udata(L); if (!ud || ud->type != TYPE_TCP_CLIENT) return luaL_error(L, "invalid user data"); if (ud->pcb) return luaL_error(L, "already connected"); uint16_t port = luaL_checkinteger(L, 2); if (port == 0) return luaL_error(L, "specify port"); const char *domain = "127.0.0.1"; if (lua_isstring(L, 3)) { size_t dl = 0; domain = luaL_checklstring(L, 3, &dl); } if (lua_gettop(L) > 3) { luaL_argcheck(L, lua_isfunction(L, 4) || lua_islightfunction(L, 4), 4, "not a function"); lua_pushvalue(L, 4); luaL_unref(L, LUA_REGISTRYINDEX, ud->client.cb_connect_ref); ud->client.cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX); } ud->tcp_pcb = tcp_new(); if (!ud->tcp_pcb) return luaL_error(L, "cannot allocate PCB"); tcp_arg(ud->tcp_pcb, ud); tcp_err(ud->tcp_pcb, net_err_cb); tcp_recv(ud->tcp_pcb, net_tcp_recv_cb); tcp_sent(ud->tcp_pcb, net_sent_cb); ud->tcp_pcb->remote_port = port; ip_addr_t addr; ud->client.wait_dns ++; int unref = 0; if (ud->self_ref == LUA_NOREF) { unref = 1; lua_pushvalue(L, 1); ud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); } err_t err = dns_gethostbyname(domain, &addr, net_dns_cb, ud); if (err == ERR_OK) { net_dns_cb(domain, &addr, ud); } else if (err != ERR_INPROGRESS) { ud->client.wait_dns --; if (unref) { luaL_unref(L, LUA_REGISTRYINDEX, ud->self_ref); ud->self_ref = LUA_NOREF; } tcp_abort(ud->tcp_pcb); ud->tcp_pcb = NULL; return lwip_lua_checkerr(L, err); } return 0; }
int EthernetClient::connect(const char* host, uint16_t port, unsigned long timeout) { ip_addr_t ip; ip.addr = 0; dns_gethostbyname(host, &ip, do_dns, &ip); while (!ip.addr) { delay(10); } if (ip.addr == IPADDR_NONE) return false; return (connect(IPAddress(ip.addr), port, timeout)); }
int EthernetUDP::beginPacket(const char *host, uint16_t port) { ip_addr_t ip; ip.addr = 0; dns_gethostbyname(host, &ip, do_dns, &ip); while(!ip.addr) { delay(10); } if(ip.addr == IPADDR_NONE) return false; return(beginPacket(IPAddress(ip.addr), port)); }
int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) { ip_addr_t addr; aResult = static_cast<uint32_t>(0); err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); if (err == ERR_OK) { aResult = addr.addr; } else if (err == ERR_INPROGRESS) { esp_yield(); // will return here when dns_found_callback fires } return (aResult != 0) ? 1 : 0; }
/*********************************************************************** * dns_fqdn (INTERNAL) */ static BOOL dns_fqdn ( char *name, int *size ) { if ( gethostname ( name, *size + 1 ) ) { switch( errno ) { case ENAMETOOLONG: SetLastError ( ERROR_MORE_DATA ); default: SetLastError ( ERROR_INVALID_PARAMETER ); } return FALSE; } if ( !dns_gethostbyname ( name, size ) ) *size = strlen ( name ); return TRUE; }
//***************************************************************************** // // Handler function when the DNS server gets a response or times out. // // \param pcName is DNS server name. // // This function is called when the DNS server resolves an IP or times out. // If the DNS server returns an IP structure that is not NULL, add the IP to // to the g_sEnet.sServerIP IP structure. // // \return None. // //***************************************************************************** int32_t EthClientDNSResolve(const char *pcName) { err_t iRet; if(HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_DNS_EN)) { return(ERR_INPROGRESS); } // // Set DNS config timer to true. // HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_DNS_EN) = 1; // // Initialize the host name IP address found flag to false. // HWREGBITW(&g_sEnet.ui32Flags, FLAG_DNS_ADDRFOUND) = 0; // // Resolve host name. // iRet = dns_gethostbyname(pcName, &g_sEnet.sServerIP, DNSServerFound, 0); // // If ERR_OK is returned, the local DNS table resolved the host name. If // ERR_INPROGRESS is returned, the DNS request has been queued and will be // sent to the DNS server. // if(iRet == ERR_OK) { // // Stop calling the DNS timer function. // HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_DNS_EN) = 0; } // // Return host name not found. // return(iRet); }
/** * Resolve the given hostname to an IP address. * @param aHostname Name to be resolved * @param aResult IPAddress structure to store the returned IP address * @return 1 if aIPAddrString was successfully converted to an IP address, * else error code */ int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) { ip_addr_t addr; aResult = static_cast<uint32_t>(0); waitStatusBits(WIFI_DNS_IDLE_BIT, 5000); clearStatusBits(WIFI_DNS_IDLE_BIT); err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); if(err == ERR_OK && addr.u_addr.ip4.addr) { aResult = addr.u_addr.ip4.addr; } else if(err == ERR_INPROGRESS) { waitStatusBits(WIFI_DNS_DONE_BIT, 4000); clearStatusBits(WIFI_DNS_DONE_BIT); } setStatusBits(WIFI_DNS_IDLE_BIT); if((uint32_t)aResult == 0){ log_e("DNS Failed for %s", aHostname); } return (uint32_t)aResult != 0; }
/** Start the http request after converting 'server_name' to ip address (DNS or address string) */ static err_t httpc_get_internal_dns(httpc_state_t* req, const char* server_name) { err_t err; LWIP_ASSERT("req != NULL", req != NULL); #if LWIP_DNS err = dns_gethostbyname(server_name, &req->remote_addr, httpc_dns_found, req); #else err = ipaddr_aton(server_name, &req->remote_addr) ? ERR_OK : ERR_ARG; #endif if (err == ERR_OK) { /* cached or IP-string */ err = httpc_get_internal_addr(req, &req->remote_addr); } else if (err == ERR_INPROGRESS) { return ERR_OK; } return err; }