///////////////////////////////////////////////////////////////////////////// // Prints current IP settings ///////////////////////////////////////////////////////////////////////////// static s32 UIP_TASK_SendDebugMessage_IP(void) { uip_ipaddr_t ipaddr; uip_gethostaddr(&ipaddr); MIOS32_MIDI_SendDebugMessage("[UIP_TASK] IP address: %d.%d.%d.%d\n", uip_ipaddr1(ipaddr), uip_ipaddr2(ipaddr), uip_ipaddr3(ipaddr), uip_ipaddr4(ipaddr)); uip_ipaddr_t netmask; uip_getnetmask(&netmask); MIOS32_MIDI_SendDebugMessage("[UIP_TASK] Netmask: %d.%d.%d.%d\n", uip_ipaddr1(netmask), uip_ipaddr2(netmask), uip_ipaddr3(netmask), uip_ipaddr4(netmask)); uip_ipaddr_t draddr; uip_getdraddr(&draddr); MIOS32_MIDI_SendDebugMessage("[UIP_TASK] Default Router (Gateway): %d.%d.%d.%d\n", uip_ipaddr1(draddr), uip_ipaddr2(draddr), uip_ipaddr3(draddr), uip_ipaddr4(draddr)); return 0; // no error }
int dhcp_release(uip_ipaddr_t server_ip) { DhcpPacket release; // Set up the UDP connection. struct uip_udp_conn *conn = uip_udp_new(&server_ip, htonw(DhcpServerPort)); if (!conn) { printf("Failed to set up UDP connection.\n"); return 1; } uip_udp_bind(conn, htonw(DhcpClientPort)); // Prepare the DHCP release packet. dhcp_prep_packet(&release, rand()); uip_ipaddr_t my_ip; uip_gethostaddr(&my_ip); release.client_ip = (uip_ipaddr1(&my_ip) << 0) | (uip_ipaddr2(&my_ip) << 8) | (uip_ipaddr3(&my_ip) << 16) | (uip_ipaddr4(&my_ip) << 24); uint8_t *options = release.options; int remaining = sizeof(release.options); uint8_t byte = DhcpRelease; dhcp_add_option(&options, DhcpTagMessageType, &byte, sizeof(byte), &remaining); dhcp_add_option(&options, DhcpTagEndOfList, NULL, 0, &remaining); // Call uip_udp_packet_send directly since we won't get a reply. uip_udp_packet_send(conn, &release, sizeof(release)); dhcp_state = DhcpInit; uip_udp_remove(conn); return 0; }
struct net_context *net_context_get(enum ip_protocol ip_proto, const struct net_addr *remote_addr, uint16_t remote_port, struct net_addr *local_addr, uint16_t local_port) { #ifdef CONFIG_NETWORKING_WITH_IPV6 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; const uip_ds6_addr_t *uip_addr; uip_ipaddr_t ipaddr; #endif int i; struct net_context *context = NULL; /* User must provide storage for the local address. */ if (!local_addr) { return NULL; } #ifdef CONFIG_NETWORKING_WITH_IPV6 if (memcmp(&local_addr->in6_addr, &in6addr_any, sizeof(in6addr_any)) == 0) { uip_addr = uip_ds6_get_global(-1); if (!uip_addr) { uip_addr = uip_ds6_get_link_local(-1); } if (!uip_addr) { return NULL; } memcpy(&local_addr->in6_addr, &uip_addr->ipaddr, sizeof(struct in6_addr)); } #else if (local_addr->in_addr.s_addr == INADDR_ANY) { uip_gethostaddr((uip_ipaddr_t *)&local_addr->in_addr); } #endif nano_sem_take(&contexts_lock, TICKS_UNLIMITED); if (local_port) { if (context_port_used(ip_proto, local_port, local_addr) < 0) { return NULL; } } else { do { local_port = random_rand() | 0x8000; } while (context_port_used(ip_proto, local_port, local_addr) == -EEXIST); } for (i = 0; i < NET_MAX_CONTEXT; i++) { if (!contexts[i].tuple.ip_proto) { contexts[i].tuple.ip_proto = ip_proto; contexts[i].tuple.remote_addr = (struct net_addr *)remote_addr; contexts[i].tuple.remote_port = remote_port; contexts[i].tuple.local_addr = (struct net_addr *)local_addr; contexts[i].tuple.local_port = local_port; context = &contexts[i]; break; } } context_sem_give(&contexts_lock); /* Set our local address */ #ifdef CONFIG_NETWORKING_WITH_IPV6 memcpy(&ipaddr.u8, local_addr->in6_addr.s6_addr, sizeof(ipaddr.u8)); if (uip_is_addr_mcast(&ipaddr)) { uip_ds6_maddr_add(&ipaddr); } else { uip_ds6_addr_add(&ipaddr, 0, ADDR_MANUAL); } #endif return context; }
//This function will soon take an char ** of input vars int indexPage(struct argData *args) { //Lets get our IP address for fun uip_ipaddr_t ipaddr; uip_gethostaddr(ipaddr); char IPAdd[16]; sprintf(IPAdd, "%d.%d.%d.%d", (uint8_t)ipaddr[0], (uint8_t)(ipaddr[0] >> 8), (uint8_t)ipaddr[1], (uint8_t)(ipaddr[1] >> 8)); fib_send ( "<h1>Atmega 644 webserver</h1>" "<b>Stats:</b><br/>" "\tCHIP: Atmel Atmega 644 20Mhz 4Kb RAM.<br/>" #ifdef ENC28J60 "\tNIC: Microchip ENC28J60.<br/>" #elif CP2200 "\tNIC: Silicon Labs CP2200.<br/>" #endif "\tTCP/IP stack: uIP<br/>" "\tIP: " ); fib_send(IPAdd); fib_send ( "<br/><a href=\"?redlight=on\">turn on red light</a><br/>" "<a href=\"?redlight=off\">turn off red light</a><br/>" ); fib_send ( "<a href=\"?greenlight=on\">turn on green light</a><br/>" "<a href=\"?greenlight=off\">turn off green light</a><br/>" ); fib_send ( "<a href=\"?testlight=on\">turn on test light</a><br/>" "<a href=\"?testlight=off\">turn off test light</a><br/>" ); fib_send("\tARGS: <br/>"); int i; for(i = 0; i < 5; i ++) { if(args[i].argName) { if(strcmp(args[i].argName, "greenlight") == 0) { if(strcmp(args[i].argValue, "off") == 0) { PORTC |= 1; } else { PORTC &= ~1; } } if(strcmp(args[i].argName, "redlight") == 0) { if(strcmp(args[i].argValue, "off") == 0) { PORTC |= 2; } else { PORTC &= ~2; } } if(strcmp(args[i].argName, "testlight") == 0) { if(strcmp(args[i].argValue, "on") == 0) { PORTB |= 1; } else { PORTB &= ~1; } } fib_send("\t\t"); fib_send(args[i].argName); fib_send(": "); if(args[i].argValue) fib_send(args[i].argValue); fib_send("<br/>"); } } }
/*---------------------------------------------------------------------------*/ int main(void) { #if UIP_CONF_IPV6 /* A hard coded address overrides the stack default MAC address to allow multiple instances. * uip6.c defines it as {0x00,0x06,0x98,0x00,0x02,0x32} giving an ipv6 address of [fe80::206:98ff:fe00:232] * We make it simpler, {0x02,0x00,0x00 + the last three bytes of the hard coded address (if any are nonzero). * HARD_CODED_ADDRESS can be defined in the contiki-conf.h file, or here to allow quick builds using different addresses. * If HARD_CODED_ADDRESS has a prefix it also applied, unless built as a RPL end node. * E.g. bbbb::12:3456 becomes fe80::ff:fe12:3456 and prefix bbbb::/64 if non-RPL * ::10 becomes fe80::ff:fe00:10 and prefix awaits RA or RPL formation * bbbb:: gives an address of bbbb::206:98ff:fe00:232 if non-RPL */ //#define HARD_CODED_ADDRESS "bbbb::20" #ifdef HARD_CODED_ADDRESS { uip_ipaddr_t ipaddr; uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr); if ((ipaddr.u8[13]!=0) || (ipaddr.u8[14]!=0) || (ipaddr.u8[15]!=0)) { if (sizeof(uip_lladdr)==6) { //Minimal-net uses ethernet MAC uip_lladdr.addr[0]=0x02;uip_lladdr.addr[1]=0;uip_lladdr.addr[2]=0; uip_lladdr.addr[3]=ipaddr.u8[13];; uip_lladdr.addr[4]=ipaddr.u8[14]; uip_lladdr.addr[5]=ipaddr.u8[15]; } } } #endif #endif process_init(); /* procinit_init initializes RPL which sets a ctimer for the first DIS */ /* We must start etimers and ctimers,before calling it */ process_start(&etimer_process, NULL); ctimer_init(); procinit_init(); autostart_start(autostart_processes); #if RPL_BORDER_ROUTER process_start(&border_router_process, NULL); printf("Border Router Process started\n"); #elif UIP_CONF_IPV6_RPL printf("RPL enabled\n"); #endif /* Set default IP addresses if not specified */ #if !UIP_CONF_IPV6 uip_ipaddr_t addr; uip_gethostaddr(&addr); if (addr.u8[0]==0) { uip_ipaddr(&addr, 10,1,1,1); } printf("IP Address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_sethostaddr(&addr); uip_getnetmask(&addr); if (addr.u8[0]==0) { uip_ipaddr(&addr, 255,0,0,0); uip_setnetmask(&addr); } printf("Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_getdraddr(&addr); if (addr.u8[0]==0) { uip_ipaddr(&addr, 10,1,1,100); uip_setdraddr(&addr); } printf("Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); #else /* UIP_CONF_IPV6 */ #if !UIP_CONF_IPV6_RPL #ifdef HARD_CODED_ADDRESS uip_ipaddr_t ipaddr; uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr); if ((ipaddr.u16[0]!=0) || (ipaddr.u16[1]!=0) || (ipaddr.u16[2]!=0) || (ipaddr.u16[3]!=0)) { #if UIP_CONF_ROUTER uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0); #else /* UIP_CONF_ROUTER */ uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0); #endif /* UIP_CONF_ROUTER */ #if !UIP_CONF_IPV6_RPL uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); #endif } #endif /* HARD_CODED_ADDRESS */ #endif #if !RPL_BORDER_ROUTER //Border router process prints addresses later { uint8_t i; for (i=0;i<UIP_DS6_ADDR_NB;i++) { if (uip_ds6_if.addr_list[i].isused) { printf("IPV6 Addresss: ");sprint_ip6(uip_ds6_if.addr_list[i].ipaddr);printf("\n"); } } } #endif #endif /* !UIP_CONF_IPV6 */ /* Make standard output unbuffered. */ setvbuf(stdout, (char *)NULL, _IONBF, 0); while(1) { fd_set fds; int n; struct timeval tv; n = process_run(); /* if(n > 0) { printf("%d processes in queue\n"); }*/ tv.tv_sec = 0; tv.tv_usec = 1; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds); select(1, &fds, NULL, NULL, &tv); if(FD_ISSET(STDIN_FILENO, &fds)) { char c; if(read(STDIN_FILENO, &c, 1) > 0) { serial_line_input_byte(c); } } etimer_request_poll(); } return 0; }
/*---------------------------------------------------------------------------*/ int main(void) { process_init(); procinit_init(); ctimer_init(); autostart_start(autostart_processes); /* Set default IP addresses if not specified */ #if !UIP_CONF_IPV6 uip_ipaddr_t addr; uip_gethostaddr(&addr); if (addr.u8[0]==0) { uip_ipaddr(&addr, 10,1,1,1); } printf("IP Address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_sethostaddr(&addr); uip_getnetmask(&addr); if (addr.u8[0]==0) { uip_ipaddr(&addr, 255,0,0,0); uip_setnetmask(&addr); } printf("Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_getdraddr(&addr); if (addr.u8[0]==0) { uip_ipaddr(&addr, 10,1,1,100); uip_setdraddr(&addr); } printf("Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); #else /* !UIP_CONF_IPV6 */ uip_ipaddr_t ipaddr; uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); uip_netif_addr_autoconf_set(&ipaddr, &uip_lladdr); uip_netif_addr_add(&ipaddr, 16, 0, TENTATIVE); printf("IP6 Address: ");sprint_ip6(ipaddr);printf("\n"); #endif /* !UIP_CONF_IPV6 */ /* Make standard output unbuffered. */ setvbuf(stdout, (char *)NULL, _IONBF, 0); while(1) { fd_set fds; int n; struct timeval tv; n = process_run(); /* if(n > 0) { printf("%d processes in queue\n"); }*/ tv.tv_sec = 0; tv.tv_usec = 1; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds); select(1, &fds, NULL, NULL, &tv); if(FD_ISSET(STDIN_FILENO, &fds)) { char c; if(read(STDIN_FILENO, &c, 1) > 0) { serial_line_input_byte(c); } } etimer_request_poll(); } return 0; }
int dhcpc_request(void *handle, struct dhcpc_state *presult) { struct dhcpc_state_s *pdhcpc = (struct dhcpc_state_s *)handle; struct in_addr oldaddr; struct in_addr newaddr; ssize_t result; uint8_t msgtype; int retries; int state; /* Save the currently assigned IP address (should be INADDR_ANY) */ oldaddr.s_addr = 0; uip_gethostaddr("eth0", &oldaddr); /* Loop until we receive the lease (or an error occurs) */ do { /* Set the IP address to INADDR_ANY. */ newaddr.s_addr = INADDR_ANY; (void)uip_sethostaddr("eth0", &newaddr); /* Loop sending DISCOVER until we receive an OFFER from a DHCP * server. We will lock on to the first OFFER and decline any * subsequent offers (which will happen if there are more than one * DHCP servers on the network. */ state = STATE_INITIAL; do { /* Send the DISCOVER command */ dbg("Broadcast DISCOVER\n"); if (dhcpc_sendmsg(pdhcpc, presult, DHCPDISCOVER) < 0) { return ERROR; } /* Get the DHCPOFFER response */ result = recv(pdhcpc->sockfd, &pdhcpc->packet, sizeof(struct dhcp_msg), 0); if (result >= 0) { msgtype = dhcpc_parsemsg(pdhcpc, result, presult); if (msgtype == DHCPOFFER) { /* Save the servid from the presult so that it is not clobbered * by a new OFFER. */ dbg("Received OFFER from %08x\n", ntohl(presult->serverid.s_addr)); pdhcpc->ipaddr.s_addr = presult->ipaddr.s_addr; pdhcpc->serverid.s_addr = presult->serverid.s_addr; /* Temporarily use the address offered by the server and break * out of the loop. */ (void)uip_sethostaddr("eth0", &presult->ipaddr); state = STATE_HAVE_OFFER; } } /* An error has occurred. If this was a timeout error (meaning that * nothing was received on this socket for a long period of time). * Then loop and send the DISCOVER command again. */ else if (errno != EAGAIN) { /* An error other than a timeout was received -- error out */ return ERROR; } } while (state == STATE_INITIAL); /* Loop sending the REQUEST up to three times (if there is no response) */ retries = 0; do { /* Send the REQUEST message to obtain the lease that was offered to us. */ dbg("Send REQUEST\n"); if (dhcpc_sendmsg(pdhcpc, presult, DHCPREQUEST) < 0) { return ERROR; } retries++; /* Get the ACK/NAK response to the REQUEST (or timeout) */ result = recv(pdhcpc->sockfd, &pdhcpc->packet, sizeof(struct dhcp_msg), 0); if (result >= 0) { /* Parse the response */ msgtype = dhcpc_parsemsg(pdhcpc, result, presult); /* The ACK response means that the server has accepted our request * and we have the lease. */ if (msgtype == DHCPACK) { dbg("Received ACK\n"); state = STATE_HAVE_LEASE; } /* NAK means the server has refused our request. Break out of * this loop with state == STATE_HAVE_OFFER and send DISCOVER again */ else if (msgtype == DHCPNAK) { dbg("Received NAK\n"); break; } /* If we get any OFFERs from other servers, then decline them now * and continue waiting for the ACK from the server that we * requested from. */ else if (msgtype == DHCPOFFER) { dbg("Received another OFFER, send DECLINE\n"); (void)dhcpc_sendmsg(pdhcpc, presult, DHCPDECLINE); } /* Otherwise, it is something that we do not recognize */ else { dbg("Ignoring msgtype=%d\n", msgtype); } } /* An error has occurred. If this was a timeout error (meaning * that nothing was received on this socket for a long period of time). * Then break out and send the DISCOVER command again (at most * 3 times). */ else if (errno != EAGAIN) { /* An error other than a timeout was received */ (void)uip_sethostaddr("eth0", &oldaddr); return ERROR; } } while (state == STATE_HAVE_OFFER && retries < 3); } while (state != STATE_HAVE_LEASE); dbg("Got IP address %d.%d.%d.%d\n", (presult->ipaddr.s_addr >> 24 ) & 0xff, (presult->ipaddr.s_addr >> 16 ) & 0xff, (presult->ipaddr.s_addr >> 8 ) & 0xff, (presult->ipaddr.s_addr ) & 0xff); dbg("Got netmask %d.%d.%d.%d\n", (presult->netmask.s_addr >> 24 ) & 0xff, (presult->netmask.s_addr >> 16 ) & 0xff, (presult->netmask.s_addr >> 8 ) & 0xff, (presult->netmask.s_addr ) & 0xff); dbg("Got DNS server %d.%d.%d.%d\n", (presult->dnsaddr.s_addr >> 24 ) & 0xff, (presult->dnsaddr.s_addr >> 16 ) & 0xff, (presult->dnsaddr.s_addr >> 8 ) & 0xff, (presult->dnsaddr.s_addr ) & 0xff); dbg("Got default router %d.%d.%d.%d\n", (presult->default_router.s_addr >> 24 ) & 0xff, (presult->default_router.s_addr >> 16 ) & 0xff, (presult->default_router.s_addr >> 8 ) & 0xff, (presult->default_router.s_addr ) & 0xff); dbg("Lease expires in %d seconds\n", presult->lease_time); return OK; }
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; }
// get the configured ip address static unsigned short function_get_configured_ip(char* buffer, int bufsize) { u8_t adr[4]; uip_gethostaddr(adr); return snprintf_P(buffer, bufsize, tcp_address_formatter,config.ipaddr[0],config.ipaddr[1],config.ipaddr[2],config.ipaddr[3]); }
/*---------------------------------------------------------------------------*/ int main(void) { clock_init(); #if UIP_CONF_IPV6 /* A hard coded address overrides the stack default MAC address to allow multiple instances. uip6.c defines it as {0x00,0x06,0x98,0x00,0x02,0x32} giving an ipv6 address of [fe80::206:98ff:fe00:232] We make it simpler, {0x02,0x00,0x00 + the last three bytes of the hard coded address (if any are nonzero). HARD_CODED_ADDRESS can be defined in the contiki-conf.h file, or here to allow quick builds using different addresses. If HARD_CODED_ADDRESS has a prefix it also applied, unless built as a RPL end node. E.g. bbbb::12:3456 becomes fe80::ff:fe12:3456 and prefix bbbb::/64 if non-RPL ::10 becomes fe80::ff:fe00:10 and prefix awaits RA or RPL formation bbbb:: gives an address of bbbb::206:98ff:fe00:232 if non-RPL */ #ifdef HARD_CODED_ADDRESS { uip_ipaddr_t ipaddr; uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr); if((ipaddr.u8[13] != 0) || (ipaddr.u8[14] != 0) || (ipaddr.u8[15] != 0)) { if(sizeof(uip_lladdr) == 6) { /* Minimal-net uses ethernet MAC */ uip_lladdr.addr[0] = 0x02; uip_lladdr.addr[1] = 0; uip_lladdr.addr[2] = 0; uip_lladdr.addr[3] = ipaddr.u8[13]; uip_lladdr.addr[4] = ipaddr.u8[14]; uip_lladdr.addr[5] = ipaddr.u8[15]; } } } #endif /* HARD_CODED_ADDRESS */ #endif /* UIP_CONF_IPV6 */ process_init(); /* procinit_init initializes RPL which sets a ctimer for the first DIS */ /* We must start etimers and ctimers,before calling it */ process_start(&etimer_process, NULL); ctimer_init(); #if RPL_BORDER_ROUTER process_start(&border_router_process, NULL); printf("Border Router Process started\n"); #elif UIP_CONF_IPV6_RPL printf("RPL enabled\n"); #endif procinit_init(); autostart_start(autostart_processes); /* Set default IP addresses if not specified */ #if !UIP_CONF_IPV6 { uip_ipaddr_t addr; uip_gethostaddr(&addr); if(addr.u8[0] == 0) { uip_ipaddr(&addr, 172,18,0,2); } printf("IP Address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_sethostaddr(&addr); uip_getnetmask(&addr); if(addr.u8[0] == 0) { uip_ipaddr(&addr, 255,255,0,0); uip_setnetmask(&addr); } printf("Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_getdraddr(&addr); if(addr.u8[0] == 0) { uip_ipaddr(&addr, 172,18,0,1); uip_setdraddr(&addr); } printf("Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); } #else /* UIP_CONF_IPV6 */ #if !UIP_CONF_IPV6_RPL { uip_ipaddr_t ipaddr; #ifdef HARD_CODED_ADDRESS uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr); #else uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); #endif if((ipaddr.u16[0] != 0) || (ipaddr.u16[1] != 0) || (ipaddr.u16[2] != 0) || (ipaddr.u16[3] != 0)) { #if UIP_CONF_ROUTER if(!uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0)) { fprintf(stderr,"uip_ds6_prefix_add() failed.\n"); exit(EXIT_FAILURE); } #else /* UIP_CONF_ROUTER */ if(!uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0)) { fprintf(stderr,"uip_ds6_prefix_add() failed.\n"); exit(EXIT_FAILURE); } #endif /* UIP_CONF_ROUTER */ uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); } } #endif /* !UIP_CONF_IPV6_RPL */ #endif /* !UIP_CONF_IPV6 */ // procinit_init(); // autostart_start(autostart_processes); /* Make standard output unbuffered. */ setvbuf(stdout, (char *)NULL, _IONBF, 0); printf("\n*******%s online*******\n",CONTIKI_VERSION_STRING); #if UIP_CONF_IPV6 && !RPL_BORDER_ROUTER /* Border router process prints addresses later */ { int i = 0; int interface_count = 0; for(i = 0; i < UIP_DS6_ADDR_NB; i++) { if(uip_ds6_if.addr_list[i].isused) { printf("IPV6 Addresss: "); sprint_ip6(uip_ds6_if.addr_list[i].ipaddr); printf("\n"); interface_count++; } } assert(0 < interface_count); } #endif while(1) { fd_set fds; int n; struct timeval tv; clock_time_t next_event; n = process_run(); next_event = etimer_next_expiration_time() - clock_time(); #if DEBUG_SLEEP if(n > 0) printf("sleep: %d events pending\n",n); else printf("sleep: next event @ T-%.03f\n",(double)next_event / (double)CLOCK_SECOND); #endif #ifdef __CYGWIN__ /* wpcap doesn't appear to support select, so * we can't idle the process on windows. */ next_event = 0; #endif if(next_event > (CLOCK_SECOND * 2)) next_event = CLOCK_SECOND * 2; tv.tv_sec = n ? 0 : (next_event / CLOCK_SECOND); tv.tv_usec = n ? 0 : ((next_event % 1000) * 1000); FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds); #ifdef __CYGWIN__ select(1, &fds, NULL, NULL, &tv); #else FD_SET(tapdev_fd(), &fds); if(0 > select(tapdev_fd() + 1, &fds, NULL, NULL, &tv)) { perror("Call to select() failed."); exit(EXIT_FAILURE); } #endif if(FD_ISSET(STDIN_FILENO, &fds)) { char c; if(read(STDIN_FILENO, &c, 1) > 0) { serial_line_input_byte(c); } } #ifdef __CYGWIN__ process_poll(&wpcap_process); #else process_poll(&tapdev_process); #endif etimer_request_poll(); } return 0; }
void httpd_handle_solometer (void) { static int8_t i = 0; static uint8_t cont_send = 0, parsing = 0; uint16_t mss,page_size; static uip_ipaddr_t hostaddr, dnsserver; //char *buf; static uint16_t ppos; uint16_t lpos,wslen; uint8_t p_par,pct,send_packet,buf[64]; uip_gethostaddr(&hostaddr); #ifdef DNS_SUPPORT eeprom_restore(dns_server, &dnsserver, IPADDR_LEN); #endif #define NUM_PAR 16 PARAM p[NUM_PAR] = { {PR_U8,"%u",(uint8_t *)&hostaddr}, {PR_U8,"%u",((uint8_t *)&hostaddr)+1}, {PR_U8,"%u",((uint8_t *)&hostaddr)+2}, {PR_U8,"%u",((uint8_t *)&hostaddr)+3}, {PR_U8,"%u",&WRID[0]}, {PR_STRING,"%s",post_cookie}, {PR_STRING,"%s",post_hostname}, {PR_U8,"%u",(uint8_t *)&post_hostip}, {PR_U8,"%u",((uint8_t *)&post_hostip)+1}, {PR_U8,"%u",((uint8_t *)&post_hostip)+2}, {PR_U8,"%u",((uint8_t *)&post_hostip)+3}, {PR_STRING,"%s",post_scriptname}, {PR_U8,"%u",(uint8_t *)&dnsserver}, {PR_U8,"%u",((uint8_t *)&dnsserver)+1}, {PR_U8,"%u",((uint8_t *)&dnsserver)+2}, {PR_U8,"%u",((uint8_t *)&dnsserver)+3} }; //debug_printf("Handle_solometer called.\n"); mss = uip_mss(); if(mss > 400) mss = 400; wslen = strlen_P(website); if (uip_newdata()) { /* We've received new data (maybe even the first time). We'll receive something like this: GET /solometer[?...] */ /* Make sure it's zero-terminated, so we can safely use strstr */ char *ptr = (char *)uip_appdata; ptr[uip_len] = 0; //debug_printf("Newdata: ---------\n%s\n-----------\n",ptr); if(strncasecmp_P (uip_appdata, PSTR ("GET /solometer"),14) == 0) { //debug_printf("This is the GET request header...\n"); ptr = strstr_P (uip_appdata, PSTR("?")) + 1; if(!ptr || *ptr == 0) { //debug_printf("This is a request only. Send page.\n"); i = 0; } else { //debug_printf("This is a set operation. Parsing...\n"); i = solometer_parse(ptr); } //debug_printf("Setze Parsing auf 1.\n"); parsing = 1; } if (parsing == 1) { // Do not start answering until all packets have arrived //debug_printf("Parsing = 1\n"); ptr = strstr_P (uip_appdata, PSTR("\r\n\r\n")); if (ptr) { //debug_printf("Setze Parsing auf 2.\n"); parsing = 2; } else { //debug_printf("Double NL not found. Waiting...\n"); return; } } if (parsing == 2) { //debug_printf("Parsing = 2. Sende Antwort.\n"); PASTE_RESET (); if(i || mss < 200) { PASTE_P (httpd_header_500_smt); cont_send = 0; } else { page_size = wslen); for(i=0;i<NUM_PAR;i++) if(p[i].typ == PR_STRING) page_size += sprintf(buf,p[i].s2,p[i].s3); else page_size += sprintf(buf,p[i].s2,*(uint8_t *)p[i].s3); PASTE_P (p1); sprintf(uip_appdata+uip_len,"%u\n",page_size); PASTE_P (p2); cont_send = 1; ppos = 0; } //debug_printf("%d: %s\n",cont_send,uip_appdata); PASTE_SEND (); parsing = 0; return; } }