/** * 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; }
bool EspDrv::getNetmask(IPAddress& mask) { LOGDEBUG(F("> getNetmask")); char buf[20]; if (sendCmdGet(F("AT+CIPSTA?"), F("+CIPSTA:netmask:\""), F("\""), buf, sizeof(buf))) { mask.fromString (buf); return true; } return false; }
bool EspDrv::getGateway(IPAddress& gw) { LOGDEBUG(F("> getGateway")); char buf[20]; if (sendCmdGet(F("AT+CIPSTA?"), F("+CIPSTA:gateway:\""), F("\""), buf, sizeof(buf))) { gw.fromString (buf); return true; } return false; }
IPAddress ESPSerialWiFiManager::_prompt_ip(String prompt, IPAddress def){ while(true){ String p(prompt); if(def != 0){ p = p + " (Default: " + def.toString() + ")"; } String ip_str = _prompt(p, ' ', 0); if(ip_str.length() == 0){ return def; } IPAddress ip; if(ip.fromString(ip_str)) return ip; else{ OFL("Invalid IP entered! Please try again.\n"); } } }
int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) { // check if aHostname is already an ipaddress if (aResult.fromString(aHostname)) { // if fromString returns true we have an IP address ready return 1; } else { // Network led ON (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 0); // Send DNS request: _resolve = 0; if (gethostbyname((uint8 *)aHostname) < 0) { // Network led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); return 0; } // Wait for connection or timeout: unsigned long start = millis(); while (_resolve == 0 && millis() - start < 20000) { m2m_wifi_handle_events(NULL); } // Network led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); if (_resolve == 0) { return 0; } aResult = _resolve; return 1; } }
bool EPDStreamer::connect(const char *host, uint16_t port) { if (connState != STATE_CONN_DISCONNECTED) { return false; } callbacksEnabled = true; IPAddress ip; connState = STATE_CONN_CONNECTING; Serial.print("Attempting connection to: "); Serial.print(host); Serial.print(":"); Serial.println(port); bool connectionSuccess; if (ip.fromString(host)) { Serial.println("Skipping name resolution"); connectionSuccess = client.connect(ip, port); } else { connectionSuccess = client.connect(host, port); } if (connectionSuccess) { Serial.println("Connected"); connState = STATE_CONN_CONNECTED; return true; } else { Serial.println("Connection failed"); connState = STATE_CONN_DISCONNECTED; return false; } }