// 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); } } }
void lwip_bringdown(void) { // Disconnect from the network dhcp_release(&lwip_netif); dhcp_stop(&lwip_netif); lwip_ip_addr[0] = '\0'; }
void net_shutdown(void) { printf("Releasing DHCP lease...\n"); dhcp_release(ð); dhcp_stop(ð); printf("Shutting down network...\n"); netif_remove(ð); gelicif_shutdown(ð); }
//Release the DHCP configuration on a specific layer3 interface. BOOL lwipReleaseDHCP(LPVOID pL3Interface) { struct netif* pif = (struct netif*)pL3Interface; if (NULL == pif) { BUG(); } dhcp_release(pif); return TRUE; }
static void remove_network(gpointer user_data) { struct connman_dhcp *dhcp = user_data; DBG("dhcp %p", dhcp); dhcp_invalidate(dhcp, FALSE); dhcp_release(dhcp); g_free(dhcp); }
int EthernetInterface::disconnect() { if (use_dhcp) { dhcp_release(&lpcNetif); dhcp_stop(&lpcNetif); } else { netif_set_down(&lpcNetif); } NVIC_DisableIRQ(ENET_IRQn); return 0; }
int EthernetInterface::disconnect() { if (use_dhcp) { dhcp_release(&netif); dhcp_stop(&netif); } else { netif_set_down(&netif); } eth_arch_disable_interrupts(); return 0; }
nsapi_error_t mbed_lwip_bringdown_2(bool ppp) { // Check if we've connected if (lwip_connected == NSAPI_STATUS_DISCONNECTED) { return NSAPI_ERROR_PARAMETER; } #if LWIP_DHCP // Disconnect from the network if (lwip_dhcp) { dhcp_release(&lwip_netif); dhcp_stop(&lwip_netif); lwip_dhcp = false; lwip_dhcp_has_to_be_set = false; } #endif if (ppp) { /* this is a blocking call, returns when PPP is properly closed */ err_t err = ppp_lwip_disconnect(); if (err) { return mbed_lwip_err_remap(err); } MBED_ASSERT(!netif_is_link_up(&lwip_netif)); /*if (netif_is_link_up(&lwip_netif)) { if (sys_arch_sem_wait(&lwip_netif_unlinked, 15000) == SYS_ARCH_TIMEOUT) { return NSAPI_ERROR_DEVICE_ERROR; } }*/ } else { netif_set_down(&lwip_netif); } #if LWIP_IPV6 mbed_lwip_clear_ipv6_addresses(&lwip_netif); #endif sys_sem_free(&lwip_netif_has_any_addr); sys_sem_new(&lwip_netif_has_any_addr, 0); #if PREF_ADDR_TIMEOUT sys_sem_free(&lwip_netif_has_pref_addr); sys_sem_new(&lwip_netif_has_pref_addr, 0); #endif #if BOTH_ADDR_TIMEOUT sys_sem_free(&lwip_netif_has_both_addr); sys_sem_new(&lwip_netif_has_both_addr, 0); #endif lwip_has_addr_state = 0; lwip_connected = NSAPI_STATUS_DISCONNECTED; return 0; }
int lwip_bringdown(void) { // Check if we've connected if (!lwip_get_ip_address()) { return NSAPI_ERROR_PARAMETER; } // Disconnect from the network dhcp_release(&lwip_netif); dhcp_stop(&lwip_netif); lwip_ip_addr[0] = '\0'; return 0; }
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; } }
static void wl_cm_disconn_cb(void* ctx) { struct ctx_server* hs = ctx; LINK_LED_OFF(); INFO_INIT("Disconnection cb...\n"); if (hs->net_cfg.dhcp_running) { printk("link down, release dhcp\n"); dhcp_release(hs->net_cfg.netif); dhcp_stop(hs->net_cfg.netif); hs->net_cfg.dhcp_running = 0; } else { printk("link down\n"); netif_set_down(hs->net_cfg.netif); } set_result_cmd(WL_FAILURE); }
/** * @brief Stop lwip service in a certain operation mode. * @param[in] uint8_t opmode: the current operation mode. * @retval None */ void lwip_net_stop(uint8_t opmode) { struct netif *sta_if; struct netif *ap_if; sta_if = netif_find_by_type(NETIF_TYPE_STA); ap_if = netif_find_by_type(NETIF_TYPE_AP); switch (opmode) { case WIFI_MODE_AP_ONLY: dhcpd_stop(); netif_set_link_down(ap_if); break; case WIFI_MODE_STA_ONLY: case WIFI_MODE_REPEATER: netif_set_status_callback(sta_if, NULL); dhcp_release(sta_if); dhcp_stop(sta_if); netif_set_link_down(sta_if); break; } }
nsapi_error_t mbed_lwip_bringdown(void) { // Check if we've connected if (!lwip_connected) { return NSAPI_ERROR_PARAMETER; } #if LWIP_IPV4 // Disconnect from the network if (lwip_dhcp) { dhcp_release(&lwip_netif); dhcp_stop(&lwip_netif); lwip_dhcp = false; } else { netif_set_down(&lwip_netif); } #endif lwip_connected = false; // TO DO - actually remove addresses from stack, and shut down properly return 0; }
static void LwipInitTask(void* pvArguments) { err_t err; struct netif fsl_netif0; ip_addr_t fsl_netif0_ipaddr, fsl_netif0_netmask, fsl_netif0_gw; char msg[] = "This is my message"; (void)pvArguments; // Init lwip stack tcpip_init(NULL,NULL); printf("%s: lwip init called ..\n", __FUNCTION__); // Setup IP Config for DHCP ... IP4_ADDR(&fsl_netif0_ipaddr, 0,0,0,0); IP4_ADDR(&fsl_netif0_netmask, 0,0,0,0); IP4_ADDR(&fsl_netif0_gw, 0,0,0,0); /* Add a network interface to the list of lwIP netifs. */ netif_add(&fsl_netif0, &fsl_netif0_ipaddr, &fsl_netif0_netmask, &fsl_netif0_gw, NULL, ethernetif_init, ethernet_input); /* Set the network interface as the default network interface. */ netif_set_default(&fsl_netif0); /* obtain the IP address, default gateway and subnet mask by using DHCP*/ err = dhcp_start(&fsl_netif0); printf("%s : Started DCHP request (%s)\n", __FUNCTION__, lwip_strerr(err)); for(int i=0; i < DHCP_TIMEOUT && fsl_netif0.dhcp->state != DHCP_BOUND; i++) { printf("%s : Current DHCP State : %d\n", __FUNCTION__, fsl_netif0.dhcp->state); // Wait a second vTaskDelay(1000/portTICK_PERIOD_MS); } // Make it active ... netif_set_up(&fsl_netif0); printf("%s : Interface is up : %d\n", __FUNCTION__, fsl_netif0.dhcp->state); printf("%s : IP %s\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.ip_addr)); printf("%s : NM %s\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.netmask)); printf("%s : GW %s\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.gw)); if (fsl_netif0.dhcp->state == DHCP_BOUND) { // Send out some UDP data struct netconn* pConnection; // Create UDP connection pConnection = netconn_new(NETCONN_UDP); // Connect to local port err = netconn_bind(pConnection, IP_ADDR_ANY, 12345); printf("%s : Bound to IP_ADDR_ANY port 12345 (%s)\n", __FUNCTION__, lwip_strerr(err)); err = netconn_connect(pConnection, IP_ADDR_BROADCAST, 12346 ); printf("%s : Connected to IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err)); for(int i = 0; i < 10; i++ ){ struct netbuf* buf = netbuf_new(); void* data = netbuf_alloc(buf, sizeof(msg)); memcpy (data, msg, sizeof (msg)); err = netconn_send(pConnection, buf); printf("%s : Sending to IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err)); netbuf_delete(buf); // De-allocate packet buffer // Wait a second vTaskDelay(1000/portTICK_PERIOD_MS); } err = netconn_disconnect(pConnection); printf("%s : Disconnected from IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err)); err = netconn_delete(pConnection); printf("%s : Deleted connection (%s)\n", __FUNCTION__, lwip_strerr(err)); } // Wait a second vTaskDelay(1000/portTICK_PERIOD_MS); /* finish the lease of the IP address */ err = dhcp_release(&fsl_netif0); printf("%s : DHCP Release (%s)\n", __FUNCTION__, lwip_strerr(err)); for(;;) {}; }
void dhcp_close(void) { dhcp_release(&Dhcp); }
nsapi_error_t LWIP::Interface::bringdown() { // Check if we've connected if (connected == NSAPI_STATUS_DISCONNECTED) { return NSAPI_ERROR_NO_CONNECTION; } #if LWIP_DHCP // Disconnect from the network if (dhcp_started) { dhcp_release(&netif); dhcp_stop(&netif); dhcp_started = false; dhcp_has_to_be_set = false; } #endif if (ppp) { /* this is a blocking call, returns when PPP is properly closed */ err_t err = ppp_lwip_disconnect(hw); if (err) { return err_remap(err); } MBED_ASSERT(!netif_is_link_up(&netif)); /*if (netif_is_link_up(&netif)) { if (sys_arch_sem_wait(&unlinked, 15000) == SYS_ARCH_TIMEOUT) { return NSAPI_ERROR_DEVICE_ERROR; } }*/ } else { netif_set_down(&netif); } #if LWIP_IPV6 mbed_lwip_clear_ipv6_addresses(&netif); #endif #if LWIP_IPV4 ip_addr_set_zero(&(netif.ip_addr)); ip_addr_set_zero(&(netif.netmask)); ip_addr_set_zero(&(netif.gw)); #endif osSemaphoreDelete(has_any_addr); osSemaphoreAttr_t attr; attr.name = NULL; attr.attr_bits = 0; attr.cb_mem = &has_any_addr_sem; attr.cb_size = sizeof has_any_addr_sem; has_any_addr = osSemaphoreNew(UINT16_MAX, 0, &attr); #if PREF_ADDR_TIMEOUT osSemaphoreDelete(has_pref_addr); attr.cb_mem = &has_pref_addr_sem; attr.cb_size = sizeof has_pref_addr_sem; has_pref_addr = osSemaphoreNew(UINT16_MAX, 0, &attr); #endif #if BOTH_ADDR_TIMEOUT osSemaphoreDelete(has_both_addr); attr.cb_mem = &has_both_addr_sem; attr.cb_size = sizeof has_both_addr_sem; has_both_addr = osSemaphoreNew(UINT16_MAX, 0, &attr); #endif has_addr_state = 0; connected = NSAPI_STATUS_DISCONNECTED; if (client_callback) { client_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, connected); } return 0; }
int main(void) { NetbootParam *param; // Initialize some consoles. serial_console_init(); cbmem_console_init(); video_console_init(); input_init(); printf("\n\nStarting netboot on " CONFIG_BOARD "...\n"); timestamp_init(); if (run_init_funcs()) halt(); // Make sure graphics are available if they aren't already. enable_graphics(); dc_usb_initialize(); srand(timer_raw_value()); printf("Looking for network device... "); while (!net_get_device()) usb_poll(); printf("done.\n"); printf("Waiting for link... "); int ready = 0; while (!ready) { if (net_ready(&ready)) halt(); } mdelay(200); // some dongles need more time than they think printf("done.\n"); // Start up the network stack. uip_init(); // Plug in the MAC address. const uip_eth_addr *mac_addr = net_get_mac(); if (!mac_addr) halt(); printf("MAC: "); print_mac_addr(mac_addr); printf("\n"); uip_setethaddr(*mac_addr); // Find out who we are. uip_ipaddr_t my_ip, next_ip, server_ip; const char *dhcp_bootfile; while (dhcp_request(&next_ip, &server_ip, &dhcp_bootfile)) printf("Dhcp failed, retrying.\n"); printf("My ip is "); uip_gethostaddr(&my_ip); print_ip_addr(&my_ip); printf("\nThe DHCP server ip is "); print_ip_addr(&server_ip); printf("\n"); // Retrieve settings from the shared data area. FmapArea shared_data; if (fmap_find_area("SHARED_DATA", &shared_data)) { printf("Couldn't find the shared data area.\n"); halt(); } void *data = flash_read(shared_data.offset, shared_data.size); netboot_params_init(data, shared_data.size); // Get TFTP server IP and file name from params with DHCP as fallback uip_ipaddr_t *tftp_ip = NULL; param = netboot_params_val(NetbootParamIdTftpServerIp); if (param->data && param->size >= sizeof(uip_ipaddr_t)) { tftp_ip = (uip_ipaddr_t *)param->data; printf("TFTP server IP set from firmware parameters: "); } else { tftp_ip = &next_ip; printf("TFTP server IP supplied by DHCP server: "); } print_ip_addr(tftp_ip); printf("\n"); const char *bootfile = NULL; param = netboot_params_val(NetbootParamIdBootfile); if (param->data && param->size > 0 && strnlen((const char *)param->data, param->size) < param->size) { bootfile = (const char *)param->data; printf("Bootfile set from firmware parameters: %s\n", bootfile); } else { bootfile = dhcp_bootfile; printf("Bootfile supplied by DHCP server: %s\n", bootfile); } // Download the bootfile. uint32_t size; if (tftp_read(payload, tftp_ip, bootfile, &size, MaxPayloadSize)) { printf("Tftp failed.\n"); if (dhcp_release(server_ip)) printf("Dhcp release failed.\n"); halt(); } printf("The bootfile was %d bytes long.\n", size); // Use command line from params when present (added to the default). param = netboot_params_val(NetbootParamIdKernelArgs); if (param->data && param->size > 0 && *(char *)param->data != '\0') { cmd_line[sizeof(def_cmd_line) - 1] = ' '; strncpy(&cmd_line[sizeof(def_cmd_line)], param->data, sizeof(cmd_line) - sizeof(def_cmd_line)); printf("Command line set from firmware parameters.\n"); // Otherwise, try to fetch it dynamically as a TFTP file. } else if (!(tftp_read(cmd_line, tftp_ip, "cmdline." CONFIG_BOARD, &size, sizeof(cmd_line) - 1))) { while (cmd_line[size - 1] <= ' ') // strip trailing whitespace if (!--size) break; // and control chars (\n, \r) cmd_line[size] = '\0'; while (size--) // replace inline control if (cmd_line[size] < ' ') // chars with spaces cmd_line[size] = ' '; printf("Command line loaded dynamically from TFTP server.\n"); // If the file doesn't exist, finally fall back to built-in default. } else { printf("No command line from TFTP, falling back to default.\n"); } cmd_line[sizeof(cmd_line) - 1] = '\0'; // We're done on the network, so release our IP. if (dhcp_release(server_ip)) { printf("Dhcp release failed.\n"); halt(); } // Add tftp server IP into command line. static const char def_tftp_cmdline[] = " tftpserverip=xxx.xxx.xxx.xxx"; const int tftp_cmdline_def_size = sizeof(def_tftp_cmdline) - 1; int cmd_line_size = strlen(cmd_line); if (cmd_line_size + tftp_cmdline_def_size >= sizeof(cmd_line)) { printf("Out of space adding TFTP server IP to the command line.\n"); return 1; } sprintf(&cmd_line[cmd_line_size], " tftpserverip=%d.%d.%d.%d", uip_ipaddr1(tftp_ip), uip_ipaddr2(tftp_ip), uip_ipaddr3(tftp_ip), uip_ipaddr4(tftp_ip)); printf("The command line is: %s\n", cmd_line); // Boot. boot(payload, cmd_line, NULL, NULL); // We should never get here. printf("Got to the end!\n"); halt(); return 0; }
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; }