// 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 dhcp_ack() { if (next_state != ACK) { fprintf(err, "State is not ACK!\n"); return; } struct dhcp_packet *packet = malloc(sizeof(struct dhcp_packet)); memset(packet, 0, sizeof(struct dhcp_packet)); int valid = 0; while (!valid) { int len = recv_packet((char*)packet, sizeof(struct dhcp_packet)); if (len < 0) {/* timeout */ free_socket(); if (timeout_count--) { next_state = REQUEST; dhcp_request(); return; } else { if (renew) { fprintf(err, "Failed to renew, try to re-allocate\n"); timeout_count = TIMEOUT_RETRY_TIMES; next_state = DISCOVER; dhcp_discover(); return; } else { //fprintf(err, "give up...\n"); //exit(0); fprintf(err, "Error in dhcp_ack, sleep 60s...\n"); sleep(60); timeout_count = TIMEOUT_RETRY_TIMES; next_state = DISCOVER; dhcp_discover(); return; } } } valid = check_packet(packet); } process_lease(&ack_lease, packet); free(packet); free_socket(); configure_interface(&ack_lease); }
/** Connect to specified network. * * @param[in] dev_sess Device session. * @param[in] ssid_start Network SSID prefix. * @param[in] password Network password (pass empty string if not needed). * * @return EOK If the operation was successfully completed, * negative error code otherwise. * */ int ieee80211_connect(async_sess_t *dev_sess, char *ssid_start, char *password) { assert(ssid_start); sysarg_t rc_orig; async_exch_t *exch = async_exchange_begin(dev_sess); aid_t aid = async_send_1(exch, DEV_IFACE_ID(IEEE80211_DEV_IFACE), IEEE80211_CONNECT, NULL); sysarg_t rc = async_data_write_start(exch, ssid_start, str_size(ssid_start) + 1); if (rc != EOK) { async_exchange_end(exch); async_wait_for(aid, &rc_orig); if (rc_orig == EOK) return (int) rc; return (int) rc_orig; } // FIXME: Typecasting string literal if (password == NULL) password = (char *) ""; rc = async_data_write_start(exch, password, str_size(password) + 1); if (rc != EOK) { async_exchange_end(exch); async_wait_for(aid, &rc_orig); if (rc_orig == EOK) return (int) rc; return (int) rc_orig; } async_exchange_end(exch); async_wait_for(aid, &rc); if (rc != EOK) return rc; /* Send DHCP discover. */ nic_address_t wifi_mac; rc = nic_get_address(dev_sess, &wifi_mac); if (rc != EOK) return rc; sysarg_t link_id = get_link_id(wifi_mac.address); if (link_id == ((sysarg_t) -1)) return EINVAL; rc = dhcp_discover(link_id); return (int) rc; }
static int dhcp_thread(void *arg) { for (;;) { if (configured) break; thread_sleep(500); if (configured) break; dhcp_discover(0xaabbccdd); } return 0; }
void dhcp_offer() { if (next_state != OFFER) { fprintf(err, "State is not OFFER!\n"); return; } struct dhcp_packet *packet = malloc(sizeof(struct dhcp_packet)); memset(packet, 0, sizeof(struct dhcp_packet)); int valid = 0; while (!valid) { int len = recv_packet((char*)packet, sizeof(struct dhcp_packet)); if (len < 0) {/* timeout */ free_socket(); if (timeout_count--) { next_state = DISCOVER; dhcp_discover(); return; } else { //fprintf(err, "give up...\n"); //exit(0); fprintf(err, "Error in dhcp_offer, sleep 60s...\n"); sleep(60); timeout_count = TIMEOUT_RETRY_TIMES; next_state = DISCOVER; dhcp_discover(); return; } } valid = check_packet(packet); } process_lease(&offer_lease, packet); free(packet); free_socket(); timeout_count = TIMEOUT_RETRY_TIMES; next_state = REQUEST; dhcp_request(); }
ER get_dhcp_addr(DHCP *dhcp) { dhcp_discover(dhcp); if(dhcp_offer(dhcp) < 0){ syslog(LOG_NOTICE, "DHCP Offer error"); return E_SYS; } dhcp_request(dhcp); if(dhcp_ack(dhcp) < 0){ syslog(LOG_NOTICE, "DHCP Ack error"); return E_SYS; } return E_OK; }
void init_dhcp() { timeout_count = TIMEOUT_RETRY_TIMES; if (load_lease(&offer_lease)) { renew = 1; generate_xid(); next_state = REQUEST; dhcp_request(); } else { next_state = DISCOVER; dhcp_discover(); } }
void dhcp_serch( int dhcp_count ) { while(1) { if( dhcp_count == 0) break; if( mib_DHCP_p->IPAddr == 0x0 ) { //ZOTIPS dhcp_discover(WLanface); dhcp_discover(Lanface); //ZOTIPS ppause(2000); dhcp_count--; } else break; } }
int main() { rflpc_uart_init(RFLPC_UART0); /* Init random number generator using RTC value */ srand(LPC_RTC->CTIME0); simple_net_ethernet_init(); simple_net_set_rx_callback(packet_in); dhcp_discover(); /* dhcp_send_request(rand(), make_ip(192,168,100,110), make_ip(192,168,100,1),0);*/ while (1) { } return 0; }
void ipv4ll_handle_failure(void *arg) { struct interface *ifp = arg; struct dhcp_state *state = D_STATE(ifp); time_t up; if (state->fail.s_addr == state->addr.s_addr) { /* RFC 3927 Section 2.5 */ up = uptime(); if (state->defend + DEFEND_INTERVAL > up) { syslog(LOG_WARNING, "%s: IPv4LL %d second defence failed", ifp->name, DEFEND_INTERVAL); dhcp_drop(ifp, "EXPIRE"); state->conflicts = -1; } else { syslog(LOG_DEBUG, "%s: defended IPv4LL address", ifp->name); state->defend = up; return; } } free(state->offer); state->offer = NULL; eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); if (++state->conflicts > MAX_CONFLICTS) { syslog(LOG_ERR, "%s: failed to acquire an IPv4LL address", ifp->name); if (ifp->options->options & DHCPCD_DHCP) { state->interval = RATE_LIMIT_INTERVAL / 2; dhcp_discover(ifp); } else eloop_timeout_add_sec(ifp->ctx->eloop, RATE_LIMIT_INTERVAL, ipv4ll_start, ifp); } else { eloop_timeout_add_sec(ifp->ctx->eloop, PROBE_WAIT, ipv4ll_start, ifp); } }
// ------------------------------------------------ // Function: dhcp_get_ip() // ------------------------------------------------ // Input: - // Output: TRUE if succesful // ------------------------------------------------ // Description: Obtain a local IP address // using the DHCP protocol // ------------------------------------------------ BOOL dhcp_get_ip(void) { IPV4 ip; if(ip_local[INTERFACE_ETH].d) return TRUE; // already has an IP ip.d = 0xffffffff; udp_close(SOCKET_DHCP); if(!udp_open(SOCKET_DHCP, UDP_DHCP_CLI, ip, UDP_DHCP_SERV, INTERFACE_ETH)) return FALSE; ip_local[INTERFACE_ETH].d = 0; ip_tmp.d = 0; ip_dhcp.d = 0xffffffff; xid.b[0] = random(); xid.b[1] = random(); xid.b[2] = random(); xid.b[3] = random(); if(!dhcp_discover()) goto fail; // find a DHCP server udp_close(SOCKET_DHCP); if(!udp_open(SOCKET_DHCP, UDP_DHCP_CLI, ip, UDP_DHCP_SERV, INTERFACE_ETH)) return FALSE; if(!dhcp_req()) goto fail; // request an IP address ip_local[INTERFACE_ETH].d = ip_tmp.d; udp_close(SOCKET_DHCP); return TRUE; fail: ip_local[INTERFACE_ETH].d = 0; udp_close(SOCKET_DHCP); return FALSE; }
/** * Start DHCP negotiation for a network interface. * * If no DHCP client instance was attached to this interface, * a new client is created first. */ struct dhcp_state *dhcp_start(struct netif *netif) { struct dhcp_state *state = NULL; struct dhcp_state *list_state = client_list; err_t result = ERR_OK; DEBUGF(DHCP_DEBUG, ("dhcp_start()\n")); // find the DHCP client attached to the given interface state = dhcp_find_client(netif); DEBUGF(DHCP_DEBUG, ("dhcp_start(): finished parsing through list\n")); // a DHCP client already attached to this interface if (state != NULL) { DEBUGF(DHCP_DEBUG, ("dhcp_start(): already active on interface\n")); // just restart the DHCP negotiation result = dhcp_discover(state); if (result == ERR_OK) { return state; } else { dhcp_stop(state); return NULL; } } DEBUGF(DHCP_DEBUG, ("dhcp_start(): starting new DHCP client\n")); state = mem_malloc(sizeof(struct dhcp_state)); if (state == NULL) { DEBUGF(DHCP_DEBUG, ("dhcp_start(): could not allocate dhcp_state\n")); return NULL; } memset(state, 0, sizeof(struct dhcp_state)); DEBUGF(DHCP_DEBUG, ("dhcp_start(): allocated dhcp_state\n")); state->pcb = udp_new(); if (state->pcb == NULL) { DEBUGF(DHCP_DEBUG, ("dhcp_start(): could not obtain pcb\n")); mem_free((void *)state); state = NULL; return NULL; } DEBUGF(DHCP_DEBUG, ("dhcp_start(): created new udp pcb\n")); state->netif = netif; // enqueue in list of clients // we are last in list state->next = NULL; // empty list? if (client_list == NULL) { // single item at head of list client_list = state; } else { // proceed to the last DHCP client state while (list_state->next != NULL) list_state = list_state->next; // { list_state->next == NULL } list_state->next = state; } dhcp_discover(state); return state; }