static void dhcp_t1_timeout(struct dhcp_state *state) { DEBUGF(DHCP_DEBUG, ("dhcp_t1_timeout()\n")); if ((state->state == DHCP_REQUESTING) || (state->state == DHCP_BOUND) || (state->state == DHCP_RENEWING)) { DEBUGF(DHCP_DEBUG, ("dhcp_t1_timeout(): must renew\n")); dhcp_renew(state); } }
// something has timed out, handle this static void dhcp_timeout(struct dhcp_state *state) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout()\n")); if ((state->state == DHCP_BACKING_OFF) || (state->state == DHCP_SELECTING)) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): restarting discovery\n")); dhcp_discover(state); } else if (state->state == DHCP_REQUESTING) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): REQUESTING, DHCP request timed out\n")); if (state->tries <= 5) { dhcp_select(state); } else { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): REQUESTING, releasing, restarting\n")); dhcp_release(state); dhcp_discover(state); } } else if (state->state == DHCP_CHECKING) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): CHECKING, ARP request timed out\n")); if (state->tries <= 1) { dhcp_check(state); } // no ARP replies on the offered address, // looks like the IP address is indeed free else { dhcp_bind(state); } } else if (state->state == DHCP_RENEWING) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): RENEWING, DHCP request timed out\n")); dhcp_renew(state); } else if (state->state == DHCP_REBINDING) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): REBINDING, DHCP request timed out\n")); if (state->tries <= 8) { dhcp_rebind(state); } else { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): REBINDING, release, restart\n")); dhcp_release(state); dhcp_discover(state); } } }
//Renew the DHCP configuration on a specific layer3 interface. BOOL lwipRenewDHCP(LPVOID pL3Interface) { struct netif* pif = (struct netif*)pL3Interface; if (NULL == pif) { BUG(); } dhcp_renew(pif); return TRUE; }
void link_callback(struct netif *netif) { if (netif_is_link_up(netif)) { printf("link_callback==UP\n"); #if USE_DHCP if (netif->dhcp != NULL) { dhcp_renew(netif); } #endif /* USE_DHCP */ } else { printf("link_callback==DOWN\n"); } }
mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_obj_t *args) { if (n_args == 0) { // Get IP addresses const ip_addr_t *dns = dns_getserver(0); mp_obj_t tuple[4] = { netutils_format_ipv4_addr((uint8_t*)&netif->ip_addr, NETUTILS_BIG), netutils_format_ipv4_addr((uint8_t*)&netif->netmask, NETUTILS_BIG), netutils_format_ipv4_addr((uint8_t*)&netif->gw, NETUTILS_BIG), netutils_format_ipv4_addr((uint8_t*)dns, NETUTILS_BIG), }; return mp_obj_new_tuple(4, tuple); } else if (args[0] == MP_OBJ_NEW_QSTR(MP_QSTR_dhcp)) { // Start the DHCP client if (dhcp_supplied_address(netif)) { dhcp_renew(netif); } else { dhcp_stop(netif); dhcp_start(netif); } // Wait for DHCP to get IP address uint32_t start = mp_hal_ticks_ms(); while (!dhcp_supplied_address(netif)) { if (mp_hal_ticks_ms() - start > 10000) { mp_raise_msg(&mp_type_OSError, "timeout waiting for DHCP to get IP address"); } mp_hal_delay_ms(100); } return mp_const_none; } else { // Release and stop any existing DHCP dhcp_release(netif); dhcp_stop(netif); // Set static IP addresses mp_obj_t *items; mp_obj_get_array_fixed_n(args[0], 4, &items); netutils_parse_ipv4_addr(items[0], (uint8_t*)&netif->ip_addr, NETUTILS_BIG); netutils_parse_ipv4_addr(items[1], (uint8_t*)&netif->netmask, NETUTILS_BIG); netutils_parse_ipv4_addr(items[2], (uint8_t*)&netif->gw, NETUTILS_BIG); ip_addr_t dns; netutils_parse_ipv4_addr(items[3], (uint8_t*)&dns, NETUTILS_BIG); dns_setserver(0, &dns); return mp_const_none; } }
/** * Set ESP8266 station DHCP hostname * @param aHostname max length:24 * @return ok */ bool ESP8266WiFiSTAClass::hostname(const char* aHostname) { /* vvvv RFC952 vvvv ASSUMPTIONS 1. A "name" (Net, Host, Gateway, or Domain name) is a text string up to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus sign (-), and period (.). Note that periods are only allowed when they serve to delimit components of "domain style names". (See RFC-921, "Domain Name System Implementation Schedule", for background). No blank or space characters are permitted as part of a name. No distinction is made between upper and lower case. The first character must be an alpha character. The last character must not be a minus sign or period. A host which serves as a GATEWAY should have "-GATEWAY" or "-GW" as part of its name. Hosts which do not serve as Internet gateways should not use "-GATEWAY" and "-GW" as part of their names. A host which is a TAC should have "-TAC" as the last part of its host name, if it is a DoD host. Single character names or nicknames are not allowed. ^^^^ RFC952 ^^^^ - 24 chars max - only a..z A..Z 0..9 '-' - no '-' as last char */ size_t len = strlen(aHostname); if (len == 0 || len > 32) { // nonos-sdk limit is 32 // (dhcp hostname option minimum size is ~60) DEBUG_WIFI_GENERIC("WiFi.(set)hostname(): empty or large(>32) name\n"); return false; } // check RFC compliance bool compliant = (len <= 24); for (size_t i = 0; compliant && i < len; i++) if (!isalnum(aHostname[i]) && aHostname[i] != '-') compliant = false; if (aHostname[len - 1] == '-') compliant = false; if (!compliant) { DEBUG_WIFI_GENERIC("hostname '%s' is not compliant with RFC952\n", aHostname); } bool ret = wifi_station_set_hostname(aHostname); if (!ret) { DEBUG_WIFI_GENERIC("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname); return false; } // now we should inform dhcp server for this change, using lwip_renew() // looping through all existing interface // harmless for AP, also compatible with ethernet adapters (to come) for (netif* intf = netif_list; intf; intf = intf->next) { // unconditionally update all known interfaces #if LWIP_VERSION_MAJOR == 1 intf->hostname = (char*)wifi_station_get_hostname(); #else intf->hostname = wifi_station_get_hostname(); #endif if (netif_dhcp_data(intf) != nullptr) { // renew already started DHCP leases err_t lwipret = dhcp_renew(intf); if (lwipret != ERR_OK) { DEBUG_WIFI_GENERIC("WiFi.hostname(%s): lwIP error %d on interface %c%c (index %d)\n", intf->hostname, (int)lwipret, intf->name[0], intf->name[1], intf->num); ret = false; } } } return ret && compliant; }
//***************************************************************************** // //! Initializes the lwIP TCP/IP stack. //! //! \param pucMAC is a pointer to a six byte array containing the MAC //! address to be used for the interface. //! \param ulIPAddr is the IP address to be used (static). //! \param ulNetMask is the network mask to be used (static). //! \param ulGWAddr is the Gateway address to be used (static). //! \param ulIPMode is the IP Address Mode. \b IPADDR_USE_STATIC will force //! static IP addressing to be used, \b IPADDR_USE_DHCP will force DHCP with //! fallback to Link Local (Auto IP), while \b IPADDR_USE_AUTOIP will force //! Link Local only. //! //! This function performs initialization of the lwIP TCP/IP stack for the //! Stellaris Ethernet MAC, including DHCP and/or AutoIP, as configured. //! //! \return None. // //***************************************************************************** void LWIPServiceTaskInit(void *pvParameters) { struct ip_addr *ip_addr; struct ip_addr *net_mask; struct ip_addr *gw_addr; //struct ip_addr ip_addr_local; //struct ip_addr net_mask_local; char IPState = 0; char* configLoad; IP_CONFIG * ipCfg; printf("Initialisiere IP "); ipCfg = pvPortMalloc(sizeof(IP_CONFIG)); configLoad = loadFromConfig(IP_CONFIG_FILE, "USE_DHCP"); #ifdef ENABLE_GRAPHIC vShowBootText("load ipconfig ..."); #endif printf("LWIPSTACK: LOAD CONFIG: (%s)\n", configLoad); if (configLoad == 0) { IPState = IPADDR_USE_AUTOIP; } else if (strcmp(configLoad, "true") == 0) { IPState = IPADDR_USE_DHCP; } else { IPState = IPADDR_USE_STATIC; } vPortFree(configLoad); // Start the TCP/IP thread & init stuff tcpip_init(NULL, NULL); vTaskDelay(100 / portTICK_RATE_MS); // Setup the network address values. if (IPState == IPADDR_USE_STATIC) { ip_addr = getAddresFromConfig("IP_ADDRESS"); net_mask = getAddresFromConfig("IP_SUBNETMASK"); gw_addr = getAddresFromConfig("IP_GATEWAY"); } #if LWIP_DHCP || LWIP_AUTOIP else { ip_addr = pvPortMalloc(sizeof(struct ip_addr)); net_mask = pvPortMalloc(sizeof(struct ip_addr)); gw_addr = pvPortMalloc(sizeof(struct ip_addr)); } #endif // Create, configure and add the Ethernet controller interface with // default settings. // WARNING: This must only be run after the OS has been started. // Typically this is the case, however, if not, you must place this // in a post-OS initialization // @SEE http://lwip.wikia.com/wiki/Initialization_using_tcpip.c printf("Starting NETIF ... \n"); #ifdef ENABLE_GRAPHIC vShowBootText("starting Network ..."); #endif netif_add(&lwip_netif, ip_addr, net_mask, gw_addr, NULL, ethernetif_init, tcpip_input); netif_set_default(&lwip_netif); printf("NETIF UP\n"); // Start DHCP, if enabled. #if LWIP_DHCP if (IPState == IPADDR_USE_DHCP) { #ifdef ENABLE_GRAPHIC vShowBootText("waiting for DHCP ..."); #endif printf("Starte DHCP Client ... "); if (dhcp_start(&lwip_netif) == ERR_OK) { printf("[ok]\n"); } else { printf("[fail]\n"); } } #endif // Start AutoIP, if enabled and DHCP is not. #if LWIP_AUTOIP if (IPState == IPADDR_USE_AUTOIP) { printf ("Setzte Auto IP (NICHT DHCP) ...\n"); autoip_start(&lwip_netif); } #endif if (IPState == IPADDR_USE_STATIC) { // Bring the interface up. netif_set_up(&lwip_netif); } vTaskDelay(1000 / portTICK_RATE_MS); while (0 == netif_is_up(&lwip_netif)) { vTaskDelay(5000 / portTICK_RATE_MS); if (0 == netif_is_up(&lwip_netif)) { dhcp_renew(&lwip_netif); } } printnetif(&lwip_netif); configLoad = loadFromConfig(IP_CONFIG_FILE, "IS_SERVER"); if (strcmp(configLoad, "true") == 0) { /* Initialize HTTP, DNS, SNTP */ printf("HTTPD Starten ...\n"); httpd_init(); } vPortFree(configLoad); #if ENABLE_SNTP printf("SNTP Starten ...\n"); sntp_init(); #endif #if ENABLE_DNS printf("DNS Starten ...\n"); dns_init(); #endif #if ENABLE_NET_BIOS printf("NETBIOS Starten ...\n"); netbios_init(); #endif printf("Dienste gestartet ...\n"); #ifdef ENABLE_GRAPHIC configLoad = loadFromConfig(IP_CONFIG_FILE, "IS_CLIENT"); if (strcmp(configLoad, "true") == 0) { vShowBootText("loading menu ..."); vLoadMenu(); } else { vShowBootText("ready for requests ..."); } vPortFree(configLoad); #endif // Nothing else to do. No point hanging around. while (1) { vTaskDelay(500 / portTICK_RATE_MS); if (EthernetPHYRead(ETH_BASE, PHY_MR1) & ETH_PHY_LINK_UP) { if (!(netif_is_up(&lwip_netif))) { // set link up flag netif_set_up(&lwip_netif); #ifdef ENABLE_GRAPHIC vShowBootText("activate networkinterface ..."); configLoad = loadFromConfig(IP_CONFIG_FILE, "IS_CLIENT"); if (strcmp(configLoad, "true") == 0) { vShowBootText("loading menu ..."); vLoadMenu(); } else { vShowBootText("ready for requests ..."); } vPortFree(configLoad); #endif if (IPState == IPADDR_USE_DHCP) { printf("DHCP Adresse anfordern ... "); if (dhcp_renew(&lwip_netif) == ERR_OK) { printf("[ok]\n"); printnetif(&lwip_netif); } else { printf("[fail]\n"); } } } } else { if (netif_is_up(&lwip_netif)) { #ifdef ENABLE_GRAPHIC vShowBootText("no networkconnection!!"); #endif printf("Deaktiviere Netzwerkinterface ... "); netif_set_down(&lwip_netif); } } } }
HRESULT LWIP_SOCKETS_Driver::UpdateAdapterConfiguration( UINT32 interfaceIndex, UINT32 updateFlags, SOCK_NetworkConfiguration* config ) { NATIVE_PROFILE_PAL_NETWORK(); if(interfaceIndex >= NETWORK_INTERFACE_COUNT) { return CLR_E_INVALID_PARAMETER; } BOOL fEnableDhcp = (0 != (config->flags & SOCK_NETWORKCONFIGURATION_FLAGS_DHCP)); BOOL fDynamicDns = (0 != (config->flags & SOCK_NETWORKCONFIGURATION_FLAGS_DYNAMIC_DNS)); BOOL fDhcpStarted; struct netif *pNetIf = netif_find_interface(g_LWIP_SOCKETS_Driver.m_interfaces[interfaceIndex].m_interfaceNumber); if (NULL == pNetIf) { return CLR_E_FAIL; } fDhcpStarted = (0 != (pNetIf->flags & NETIF_FLAG_DHCP)); #if LWIP_DNS // when using DHCP do not use the static settings if(0 != (updateFlags & SOCK_NETWORKCONFIGURATION_UPDATE_DNS)) { if(!fDynamicDns && (config->dnsServer1 != 0 || config->dnsServer2 != 0)) { // user defined DNS addresses if(config->dnsServer1 != 0) { u8_t idx = 0; dns_setserver(idx, (struct ip_addr *)&config->dnsServer1); } if(config->dnsServer2 != 0) { u8_t idx = 1; dns_setserver(idx, (struct ip_addr *)&config->dnsServer2); } } } #endif #if LWIP_DHCP if(0 != (updateFlags & SOCK_NETWORKCONFIGURATION_UPDATE_DHCP)) { if(fEnableDhcp) { if(!fDhcpStarted) { if(ERR_OK != dhcp_start(pNetIf)) { return CLR_E_FAIL; } } } else { if(fDhcpStarted) { dhcp_stop(pNetIf); } netif_set_addr(pNetIf, (struct ip_addr *) &config->ipaddr, (struct ip_addr *)&config->subnetmask, (struct ip_addr *)&config->gateway); Network_PostEvent( NETWORK_EVENT_TYPE_ADDRESS_CHANGED, 0 ); } } if(fEnableDhcp && fDhcpStarted) { // Try Renew before release since renewing after release will fail if(0 != (updateFlags & SOCK_NETWORKCONFIGURATION_UPDATE_DHCP_RENEW)) { //netifapi_netif_common(pNetIf, NULL, dhcp_renew); dhcp_renew(pNetIf); } else if(0 != (updateFlags & SOCK_NETWORKCONFIGURATION_UPDATE_DHCP_RELEASE)) { //netifapi_netif_common(pNetIf, NULL, dhcp_release); dhcp_release(pNetIf); } } #endif if(0 != (updateFlags & SOCK_NETWORKCONFIGURATION_UPDATE_MAC)) { int len = __min(config->macAddressLen, sizeof(pNetIf->hwaddr)); memcpy(pNetIf->hwaddr, config->macAddressBuffer, len); pNetIf->hwaddr_len = len; // mac address requires stack re-init Network_Interface_Close(interfaceIndex); g_LWIP_SOCKETS_Driver.m_interfaces[interfaceIndex].m_interfaceNumber = Network_Interface_Open(interfaceIndex); } return S_OK; }
/** * @brief DHCP Process * @param argument: network interface * @retval None */ void dhcp_process(void const * argument) { struct netif *netif = (struct netif *) argument; struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; uint32_t IPaddress = 0; int mscnt =0; netif->ip_addr.addr = 0; netif->netmask.addr = 0; netif->gw.addr = 0; dhcp_start(netif); DHCP_state = DHCP_WAIT_ADDRESS; // int DHCP_state = DHCP_START; while( 1 ) { osDelay(DHCP_FINE_TIMER_MSECS); dhcp_fine_tmr(); mscnt += DHCP_FINE_TIMER_MSECS; if (mscnt >= DHCP_COARSE_TIMER_SECS*1000) { dhcp_coarse_tmr(); mscnt = 0; } switch (DHCP_state) { case DHCP_WAIT_ADDRESS: { /* Read the new IP address */ IPaddress = netif->ip_addr.addr; if (IPaddress!=0) { dhcp_renew(netif); DHCP_state = DHCP_ADDRESS_ASSIGNED; #if 1 uint8_t iptab[4]; char iptxt[80]; iptab[0] = (uint8_t)(IPaddress >> 24); iptab[1] = (uint8_t)(IPaddress >> 16); iptab[2] = (uint8_t)(IPaddress >> 8); iptab[3] = (uint8_t)(IPaddress); sprintf(iptxt, "IP address assigned by a DHCP server: %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]); WriteConsole ("address IP affecté\n"); #endif return; } else { /* DHCP timeout */ if (netif->dhcp->tries > MAX_DHCP_TRIES) { DHCP_state = DHCP_TIMEOUT; /* Stop DHCP */ dhcp_stop(netif); /* Static address used */ IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 ); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); netif_set_addr(netif, &ipaddr , &netmask, &gw); return; #if CONSOLE char iptxt[80]; sprintf((char*)iptxt, "%Static IP address : d.%d.%d.%d", IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); WriteConsole("DHCP timeout !!\n"); WriteConsole(iptxt); #endif } else netif->dhcp->tries++; } } break; default: break; }
//***************************************************************************** // //! Initializes the lwIP TCP/IP stack. //! Call it from your main to initialize the lwip //! //! \param pucMAC is a pointer to a six byte array containing the MAC //! address to be used for the interface. //! \param ulIPAddr is the IP address to be used (static). //! \param ulNetMask is the network mask to be used (static). //! \param ulGWAddr is the Gateway address to be used (static). //! \param ulIPMode is the IP Address Mode. \b IPADDR_USE_STATIC will force //! static IP addressing to be used, \b IPADDR_USE_DHCP will force DHCP with //! fallback to Link Local (Auto IP), while \b IPADDR_USE_AUTOIP will force //! Link Local only. //! //! This function performs initialization of the lwIP TCP/IP stack for the //! Stellaris Ethernet MAC, including DHCP and/or AutoIP, as configured. //! //! \return None. // //***************************************************************************** void LWIPServiceTaskInit(IP_CONFIG *ipCfg) { struct ip_addr ip_addr; struct ip_addr net_mask; struct ip_addr gw_addr; LWIP_ASSERT("ipCfg != NULL", (ipCfg != NULL)); // Check the parameters. #if LWIP_DHCP && LWIP_AUTOIP ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) || (ipCfg->IPMode == IPADDR_USE_DHCP) || (ipCfg->IPMode == IPADDR_USE_AUTOIP)) #elif LWIP_DHCP ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) || (ipCfg->IPMode == IPADDR_USE_DHCP)) #elif LWIP_AUTOIP ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) || (ipCfg->IPMode == IPADDR_USE_AUTOIP)) #else ASSERT(ipCfg->IPMode == IPADDR_USE_STATIC) #endif LWIP_DEBUGF(DHCP_DEBUG, ("----- LWIP_DEBUGF calling tcpip_init -----\n")); // Start the TCP/IP thread & init stuff tcpip_init(NULL, NULL); vTaskDelay(100 / portTICK_RATE_MS); // Setup the network address values. if (ipCfg->IPMode == IPADDR_USE_STATIC) { ip_addr.addr = htonl(ipCfg->IPAddr); net_mask.addr = htonl(ipCfg->NetMask); gw_addr.addr = htonl(ipCfg->GWAddr); } else { ip_addr.addr = 0; net_mask.addr = 0; gw_addr.addr = 0; } // Create, configure and add the Ethernet controller interface with // default settings. // WARNING: This must only be run after the OS has been started. // Typically this is the case, however, if not, you must place this // in a post-OS initialization // @SEE http://lwip.wikia.com/wiki/Initialization_using_tcpip.c netif_add(&lwip_netif, &ip_addr, &net_mask, &gw_addr, NULL, ethernetif_init, tcpip_input); netif_set_default(&lwip_netif); // Start DHCP, if enabled. #if LWIP_DHCP if (ipCfg->IPMode == IPADDR_USE_DHCP) { LWIP_DEBUGF(DHCP_DEBUG, ("----- Starting DHCP client -----\n")); dhcp_start(&lwip_netif); } #endif // Start AutoIP, if enabled and DHCP is not. #if LWIP_AUTOIP if (ipCfg->IPMode == IPADDR_USE_AUTOIP) { autoip_start(&lwip_netif); } #endif if (ipCfg->IPMode == IPADDR_USE_STATIC) { // Bring the interface up. netif_set_up(&lwip_netif); } vTaskDelay(1000/portTICK_RATE_MS); while (0 == netif_is_up(&lwip_netif)) { lstr("<delay 5s>"); vTaskDelay(5000/portTICK_RATE_MS); #if LWIP_DHCP if (ipCfg->IPMode == IPADDR_USE_DHCP) { if (0 == netif_is_up(&lwip_netif)) { lstr("<dhcp_renew>"); dhcp_renew(&lwip_netif); } } #endif } /* Initialize HTTP */ #ifdef INCLUDE_HTTPD_SSI init_ssi_cgi_handlers(); #else #ifdef INCLUDE_HTTPD_CGI init_ssi_cgi_handlers(); #endif #endif httpd_init(); }