static int app_dhcp_main(void) { uint32_t timeleft = 15000; struct dhcpc_state state; void *dhcp_handle; int ret; dhcp_handle = dhcpc_open(NET_DEVNAME); ret = dhcpc_request(dhcp_handle, &state); while (ret != OK) { usleep(10); //sys_msleep(10); timeleft -= 10; if (timeleft <= 0) break; } if (timeleft <= 0) { dhcpc_close(dhcp_handle); return -1; } netlib_set_ipv4addr(NET_DEVNAME, &state.ipaddr); netlib_set_ipv4netmask(NET_DEVNAME, &state.netmask); netlib_set_dripv4addr(NET_DEVNAME, &state.default_router); printf("IP address : %s ----\n", inet_ntoa(state.ipaddr)); return 1; }
static void net_configure(void) { struct in_addr addr; #if defined(CONFIG_EXAMPLES_POLL_NOMAC) uint8_t mac[IFHWADDRLEN]; #endif /* Configure uIP */ /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLES_POLL_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; netlib_setmacaddr("eth0", mac); #endif /* Set up our host address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_POLL_IPADDR); netlib_set_ipv4addr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_POLL_DRIPADDR); netlib_set_dripv4addr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_POLL_NETMASK); netlib_set_ipv4netmask("eth0", &addr); }
int udp_main(int argc, char *argv[]) #endif { #ifdef CONFIG_EXAMPLES_UDP_IPv6 #ifdef CONFIG_NET_ICMPv6_AUTOCONF /* Perform ICMPv6 auto-configuration */ netlib_icmpv6_autoconfiguration("eth0"); #else /* CONFIG_NET_ICMPv6_AUTOCONF */ /* Set up our fixed host address */ netlib_set_ipv6addr("eth0", (FAR const struct in6_addr *)g_ipv6_hostaddr); /* Set up the default router address */ netlib_set_dripv6addr("eth0", (FAR const struct in6_addr *)g_ipv6_draddr); /* Setup the subnet mask */ netlib_set_ipv6netmask("eth0", (FAR const struct in6_addr *)g_ipv6_netmask); #endif /* CONFIG_NET_ICMPv6_AUTOCONF */ #else /* CONFIG_EXAMPLES_UDP_IPv6 */ struct in_addr addr; /* Set up our host address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_UDP_IPADDR); netlib_set_ipv4addr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_UDP_DRIPADDR); netlib_set_dripv4addr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_UDP_NETMASK); netlib_set_ipv4netmask("eth0", &addr); #endif /* CONFIG_EXAMPLES_UDP_IPv6 */ #ifdef CONFIG_EXAMPLES_UDP_SERVER recv_server(); #else send_client(); #endif return 0; }
int wget_main(int argc, char *argv[]) #endif { struct in_addr addr; #if defined(CONFIG_EXAMPLES_WGET_NOMAC) uint8_t mac[IFHWADDRLEN]; #endif /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLES_WGET_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; netlib_setmacaddr("eth0", mac); #endif /* Set up our host address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_WGET_IPADDR); netlib_set_ipv4addr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_WGET_DRIPADDR); netlib_set_dripv4addr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_WGET_NETMASK); netlib_set_ipv4netmask("eth0", &addr); /* Then start the server */ wget(CONFIG_EXAMPLES_WGET_URL, g_iobuffer, 512, callback, NULL); return 0; }
static void telnetd_netinit(void) { #if 0 struct in_addr addr; #endif #ifdef CONFIG_EXAMPLES_TELNETD_NOMAC uint8_t mac[IFHWADDRLEN]; #endif /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLES_TELNETD_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; netlib_setmacaddr("eth0", mac); #endif #if 0 // Block this code. Network conf is setup by DHCPC /* Set up our host address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_TELNETD_IPADDR); netlib_set_ipv4addr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_TELNETD_DRIPADDR); netlib_set_dripv4addr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_TELNETD_NETMASK); netlib_set_ipv4netmask("eth0", &addr); #endif }
static int tcpecho_netsetup() { /* If this task is excecutated as an NSH built-in function, then the * network has already been configured by NSH's start-up logic. */ #ifndef CONFIG_NSH_BUILTIN_APPS struct in_addr addr; #if defined(CONFIG_EXAMPLES_TCPECHO_DHCPC) || defined(CONFIG_EXAMPLES_TCPECHO_NOMAC) uint8_t mac[IFHWADDRLEN]; #endif #ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC struct dhcpc_state ds; void *handle; #endif /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLES_TCPECHO_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; netlib_setmacaddr("eth0", mac); #endif /* Set up our host address */ #ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC addr.s_addr = 0; #else addr.s_addr = HTONL(CONFIG_EXAMPLES_TCPECHO_IPADDR); #endif netlib_set_ipv4addr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_TCPECHO_DRIPADDR); netlib_set_dripv4addr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_TCPECHO_NETMASK); netlib_set_ipv4netmask("eth0", &addr); #ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC /* Get the MAC address of the NIC */ netlib_getmacaddr("eth0", mac); /* Set up the DHCPC modules */ handle = dhcpc_open(&mac, IFHWADDRLEN); /* Get an IP address. Note: there is no logic here for renewing the address in this * example. The address should be renewed in ds.lease_time/2 seconds. */ if (!handle) { return ERROR; } if (dhcpc_request(handle, &ds) != OK) { return ERROR; } netlib_set_ipv4addr("eth0", &ds.ipaddr); if (ds.netmask.s_addr != 0) { netlib_set_ipv4netmask("eth0", &ds.netmask); } if (ds.default_router.s_addr != 0) { netlib_set_dripv4addr("eth0", &ds.default_router); } if (ds.dnsaddr.s_addr != 0) { netlib_set_ipv4dnsaddr(&ds.dnsaddr); } dhcpc_close(handle); printf("IP: %s\n", inet_ntoa(ds.ipaddr)); #endif /* CONFIG_EXAMPLES_TCPECHO_DHCPC */ #endif /* CONFIG_NSH_BUILTIN_APPS */ return OK; }
void ipcp_rx(struct ppp_context_s *ctx, u8_t *buffer, u16_t count) { u8_t *bptr = buffer; //IPCPPKT *pkt=(IPCPPKT *)buffer; u16_t len; DEBUG1(("IPCP len %d\n",count)); switch (*bptr++) { case CONF_REQ: /* Parse request and see if we can ACK it */ ++bptr; len = (*bptr++ << 8); len |= *bptr++; /* len-=2; */ DEBUG1(("check lcplist\n")); if (scan_packet(ctx, IPCP, ipcplist, buffer, bptr, (u16_t)(len - 4))) { DEBUG1(("option was bad\n")); } else { DEBUG1(("IPCP options are good\n")); /* Parse out the results */ /* lets try to implement what peer wants */ /* Reject any protocol not */ /* Error? if we we need to send a config Reject ++++ this is good for a subroutine*/ /* All we should get is the peer IP address */ if (IPCP_IPADDRESS == *bptr++) { /* Dump length */ ++bptr; #ifdef IPCP_GET_PEER_IP ((u8_t*)&ctx->peer_ip)[0] = *bptr++; ((u8_t*)&ctx->peer_ip)[1] = *bptr++; ((u8_t*)&ctx->peer_ip)[2] = *bptr++; ((u8_t*)&ctx->peer_ip)[3] = *bptr++; DEBUG1(("Peer IP ")); /* printip(peer_ip_addr); */ DEBUG1(("\n")); netlib_set_dripv4addr((char*)ctx->ifname, &ctx->peer_ip); #else bptr += 4; #endif } else { DEBUG1(("HMMMM this shouldn't happen IPCP1\n")); } #if 0 if (error) { /* Write the config NAK packet we've built above, take on the header */ bptr = buffer; *bptr++ = CONF_NAK; /* Write Conf_rej */ *bptr++; /*tptr++;*/ /* skip over ID */ /* Write new length */ *bptr++ = 0; *bptr = tptr - buffer; /* Write the reject frame */ DEBUG1(("Writing NAK frame \n")); ahdlc_tx(IPCP, buffer, (u16_t)(tptr - buffer)); DEBUG1(("- End NAK Write frame\n")); } else { } #endif /* If we get here then we are OK, lets send an ACK and tell the rest * of our modules our negotiated config. */ ctx->ipcp_state |= IPCP_RX_UP; DEBUG1(("Send IPCP ACK!\n")); bptr = buffer; *bptr++ = CONF_ACK; /* Write Conf_ACK */ bptr++; /* Skip ID (send same one) */ /* Set stuff */ /* ppp_flags |= tflag; */ DEBUG1(("SET- stuff -- are we up? c=%d dif=%d \n", count, (u16_t)(bptr - buffer))); /* Write the ACK frame */ DEBUG1(("Writing ACK frame \n")); /* Send packet ahdlc_txz(procol,header,data,headerlen,datalen); */ ahdlc_tx(ctx, IPCP, 0, buffer, 0, count /*bptr-buffer*/); DEBUG1(("- End ACK Write frame\n")); } break; case CONF_ACK: /* config Ack */ DEBUG1(("CONF ACK\n")); /* Parse out the results * * Dump the ID and get the length. */ /* Dump the ID */ bptr++; /* Get the length */ len = (*bptr++ << 8); len |= *bptr++; #if 0 /* Parse ACK and set data */ while (bptr < buffer + len) { switch (*bptr++) { case IPCP_IPADDRESS: /* Dump length */ bptr++; ((u8_t*)ipaddr)[0] = *bptr++; ((u8_t*)ipaddr)[1] = *bptr++; ((u8_t*)ipaddr)[2] = *bptr++; ((u8_t*)ipaddr)[3] = *bptr++; break; case IPCP_PRIMARY_DNS: bptr++; ((u8_t*)pri_dns_addr)[0] = *bptr++; ((u8_t*)pri_dns_addr)[1] = *bptr++; ((u8_t*)pri_dns_addr)[2] = *bptr++; ((u8_t*)pri_dns_addr)[3] = *bptr++; break; case IPCP_SECONDARY_DNS: bptr++; ((u8_t*)sec_dns_addr)[0] = *bptr++; ((u8_t*)sec_dns_addr)[1] = *bptr++; ((u8_t*)sec_dns_addr)[2] = *bptr++; ((u8_t*)sec_dns_addr)[3] = *bptr++; break; default: DEBUG1(("IPCP CONFIG_ACK problem1\n")); } } #endif ctx->ipcp_state |= IPCP_TX_UP; /*ppp_ipcp_state &= ~IPCP_RX_UP;*/ DEBUG1(("were up! \n")); //printip(pppif.ipaddr); #ifdef IPCP_GET_PRI_DNS printip(pri_dns_addr); #endif #ifdef IPCP_GET_SEC_DNS printip(sec_dns_addr); #endif DEBUG1(("\n")); break; case CONF_NAK: /* Config Nack */ DEBUG1(("CONF NAK\n")); /* Dump the ID */ bptr++; /* Get the length */ len = (*bptr++ << 8); len |= *bptr++; /* Parse ACK and set data */ while (bptr < buffer + len) { switch (*bptr++) { case IPCP_IPADDRESS: /* dump length */ bptr++; ((u8_t*)&ctx->local_ip)[0] = (char)*bptr++; ((u8_t*)&ctx->local_ip)[1] = (char)*bptr++; ((u8_t*)&ctx->local_ip)[2] = (char)*bptr++; ((u8_t*)&ctx->local_ip)[3] = (char)*bptr++; netlib_ifup((char*)ctx->ifname); netlib_set_ipv4addr((char*)ctx->ifname, &ctx->local_ip); //DEBUG1(("My PPP-ipno: (%d.%d.%d.%d)\n", ((u8_t*)pppif.ipaddr)[0], ((u8_t*)pppif.ipaddr)[1], ((u8_t*)pppif.ipaddr)[2], ((u8_t*)pppif.ipaddr)[3])); break; #ifdef IPCP_GET_PRI_DNS case IPCP_PRIMARY_DNS: bptr++; ((u8_t*)&ctx->pri_dns_addr)[0] = *bptr++; ((u8_t*)&ctx->pri_dns_addr)[1] = *bptr++; ((u8_t*)&ctx->pri_dns_addr)[2] = *bptr++; ((u8_t*)&ctx->pri_dns_addr)[3] = *bptr++; break; #endif #ifdef IPCP_GET_SEC_DNS case IPCP_SECONDARY_DNS: bptr++; ((u8_t*)&ctx->sec_dns_addr)[0] = *bptr++; ((u8_t*)&ctx->sec_dns_addr)[1] = *bptr++; ((u8_t*)&ctx->sec_dns_addr)[2] = *bptr++; ((u8_t*)&ctx->sec_dns_addr)[3] = *bptr++; break; #endif default: DEBUG1(("IPCP CONFIG_ACK problem 2\n")); } } ctx->ppp_id++; printip(pppif.ipaddr); #ifdef IPCP_GET_PRI_DNS printip(pri_dns_addr); #endif #ifdef IPCP_GET_PRI_DNS printip(sec_dns_addr); #endif DEBUG1(("\n")); break; case CONF_REJ: /* Config Reject */ DEBUG1(("CONF REJ\n")); /* Remove the offending options*/ ctx->ppp_id++; /* Dump the ID */ bptr++; /* Get the length */ len = (*bptr++ << 8); len |= *bptr++; /* Parse ACK and set data */ while (bptr < buffer + len) { switch (*bptr++) { case IPCP_IPADDRESS: ctx->ipcp_state |= IPCP_IP_BIT; bptr += 5; break; #ifdef IPCP_GET_PRI_DNS case IPCP_PRIMARY_DNS: ctx->ipcp_state |= IPCP_PRI_DNS_BIT; bptr += 5; break; #endif #ifdef IPCP_GET_PRI_DNS case IPCP_SECONDARY_DNS: ctx->ipcp_state |= IPCP_SEC_DNS_BIT; bptr += 5; break; #endif default: DEBUG1(("IPCP this shoudln't happen 3\n")); } } break; default: DEBUG1(("-Unknown 4\n")); } }
static void netest_initialize(void) { #ifndef CONFIG_EXAMPLES_NETTEST_IPv6 struct in_addr addr; #endif #ifdef CONFIG_EXAMPLES_NETTEST_NOMAC uint8_t mac[IFHWADDRLEN]; #endif /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLES_NETTEST_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; netlib_setmacaddr("eth0", mac); #endif #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 #ifdef CONFIG_NET_ICMPv6_AUTOCONF /* Perform ICMPv6 auto-configuration */ netlib_icmpv6_autoconfiguration("eth0"); #else /* CONFIG_NET_ICMPv6_AUTOCONF */ /* Set up our fixed host address */ netlib_set_ipv6addr("eth0", (FAR const struct in6_addr *)g_ipv6_hostaddr); /* Set up the default router address */ netlib_set_dripv6addr("eth0", (FAR const struct in6_addr *)g_ipv6_draddr); /* Setup the subnet mask */ netlib_set_ipv6netmask("eth0", (FAR const struct in6_addr *)g_ipv6_netmask); #endif /* CONFIG_NET_ICMPv6_AUTOCONF */ #else /* CONFIG_EXAMPLES_NETTEST_IPv6 */ /* Set up our host address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_IPADDR); netlib_set_ipv4addr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_DRIPADDR); netlib_set_dripv4addr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_NETMASK); netlib_set_ipv4netmask("eth0", &addr); #endif /* CONFIG_EXAMPLES_NETTEST_IPv6 */ }
int webserver_main(int argc, char *argv[]) #endif { struct in_addr addr; #if defined(CONFIG_EXAMPLES_WEBSERVER_DHCPC) || defined(CONFIG_EXAMPLES_WEBSERVER_NOMAC) uint8_t mac[IFHWADDRLEN]; #endif #ifdef CONFIG_EXAMPLES_WEBSERVER_DHCPC void *handle; #endif /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLES_WEBSERVER_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; netlib_setmacaddr("eth0", mac); #endif /* Set up our host address */ #ifdef CONFIG_EXAMPLES_WEBSERVER_DHCPC addr.s_addr = 0; #else addr.s_addr = HTONL(CONFIG_EXAMPLES_WEBSERVER_IPADDR); #endif netlib_set_ipv4addr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_WEBSERVER_DRIPADDR); netlib_set_dripv4addr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_WEBSERVER_NETMASK); netlib_set_ipv4netmask("eth0", &addr); #ifdef CONFIG_EXAMPLES_WEBSERVER_DHCPC /* Get the MAC address of the NIC */ netlib_getmacaddr("eth0", mac); /* Set up the DHCPC modules */ handle = dhcpc_open(&mac, IFHWADDRLEN); /* Get an IP address. Note: there is no logic here for renewing the address in this * example. The address should be renewed in ds.lease_time/2 seconds. */ printf("Getting IP address\n"); if (handle) { struct dhcpc_state ds; (void)dhcpc_request(handle, &ds); netlib_set_ipv4addr("eth0", &ds.ipaddr); if (ds.netmask.s_addr != 0) { netlib_set_ipv4netmask("eth0", &ds.netmask); } if (ds.default_router.s_addr != 0) { netlib_set_dripv4addr("eth0", &ds.default_router); } if (ds.dnsaddr.s_addr != 0) { netlib_set_ipv4dnsaddr(&ds.dnsaddr); } dhcpc_close(handle); printf("IP: %s\n", inet_ntoa(ds.ipaddr)); } #endif #ifdef CONFIG_NET_TCP printf("Starting webserver\n"); httpd_init(); cgi_register(); httpd_listen(); #endif while (1) { sleep(3); printf("webserver_main: Still running\n"); #if CONFIG_NFILE_DESCRIPTORS > 0 fflush(stdout); #endif } return 0; }
int discover_main(int argc, char *argv[]) #endif { /* If this task is excecutated as an NSH built-in function, then the * network has already been configured by NSH's start-up logic. */ #ifndef CONFIG_NSH_BUILTIN_APPS struct in_addr addr; #if defined(CONFIG_EXAMPLES_DISCOVER_DHCPC) || defined(CONFIG_EXAMPLES_DISCOVER_NOMAC) uint8_t mac[IFHWADDRLEN]; #endif #ifdef CONFIG_EXAMPLES_DISCOVER_DHCPC void *handle; #endif /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLES_DISCOVER_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; netlib_setmacaddr("eth0", mac); #endif /* Set up our host address */ #ifdef CONFIG_EXAMPLES_DISCOVER_DHCPC addr.s_addr = 0; #else addr.s_addr = HTONL(CONFIG_EXAMPLES_DISCOVER_IPADDR); #endif netlib_set_ipv4addr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_DISCOVER_DRIPADDR); netlib_set_dripv4addr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_DISCOVER_NETMASK); netlib_set_ipv4netmask("eth0", &addr); #ifdef CONFIG_EXAMPLES_DISCOVER_DHCPC /* Set up the resolver */ dns_bind(); /* Get the MAC address of the NIC */ netlib_getmacaddr("eth0", mac); /* Set up the DHCPC modules */ handle = dhcpc_open(&mac, IFHWADDRLEN); /* Get an IP address. Note: there is no logic here for renewing the address in this * example. The address should be renewed in ds.lease_time/2 seconds. */ printf("Getting IP address\n"); if (handle) { struct dhcpc_state ds; (void)dhcpc_request(handle, &ds); netlib_set_ipv4addr("eth0", &ds.ipaddr); if (ds.netmask.s_addr != 0) { netlib_set_ipv4netmask("eth0", &ds.netmask); } if (ds.default_router.s_addr != 0) { netlib_set_dripv4addr("eth0", &ds.default_router); } if (ds.dnsaddr.s_addr != 0) { dns_setserver(&ds.dnsaddr); } dhcpc_close(handle); printf("IP: %s\n", inet_ntoa(ds.ipaddr)); } #endif /* CONFIG_EXAMPLES_DISCOVER_DHCPC */ #endif /* CONFIG_NSH_BUILTIN_APPS */ if (discover_start(NULL) < 0) { ndbg("Could not start discover daemon.\n"); return ERROR; } return OK; }