/*-----------------------------------------------------------------------------------*/ static void apply_tcpipconfig(void) { uip_ipaddr_t addr; nullterminate(ipaddr); if(uiplib_ipaddrconv(ipaddr, &addr)) { uip_sethostaddr(&addr); } nullterminate(netmask); if(uiplib_ipaddrconv(netmask, &addr)) { uip_setnetmask(&addr); } nullterminate(gateway); if(uiplib_ipaddrconv(gateway, &addr)) { uip_setdraddr(&addr); } #if UIP_UDP nullterminate(dnsserver); if(uiplib_ipaddrconv(dnsserver, &addr)) { resolv_conf(&addr); } #endif /* UIP_UDP */ }
/*-----------------------------------------------------------------------------------*/ static void apply_tcpipconfig(void) { int file = cfs_open("contiki.cfg", CFS_READ); int size = cfs_read(file, uip_buf, 100); cfs_close(file); nullterminate(ipaddr); uiplib_ipaddrconv(ipaddr, (uip_ipaddr_t *)&uip_buf[0]); nullterminate(netmask); uiplib_ipaddrconv(netmask, (uip_ipaddr_t *)&uip_buf[4]); nullterminate(gateway); uiplib_ipaddrconv(gateway, (uip_ipaddr_t *)&uip_buf[8]); #if WITH_DNS nullterminate(dnsserver); uiplib_ipaddrconv(dnsserver, (uip_ipaddr_t *)&uip_buf[12]); #endif /* WITH_DNS */ file = cfs_open("contiki.cfg", CFS_WRITE); cfs_write(file, uip_buf, size); cfs_close(file); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(shell_udpsend_process, ev, data) { const char *next, *nextptr; struct shell_input *input; uint16_t port, local_port; PROCESS_BEGIN(); next = strchr(data, ' '); if(next == NULL) { shell_output_str(&udpsend_command, "udpsend <server> <port> [localport]: server as address", ""); PROCESS_EXIT(); } ++next; strncpy(server, data, sizeof(server)); port = shell_strtolong(next, &nextptr); uiplib_ipaddrconv(server, (u8_t *)&serveraddr); udpconn = udp_new(&serveraddr, htons(port), NULL); if(next != nextptr) { local_port = shell_strtolong(nextptr, &nextptr); udp_bind(udpconn, htons(local_port)); } running = 1; while(running) { PROCESS_WAIT_EVENT(); if(ev == shell_event_input) { input = data; if(input->len1 + input->len2 == 0) { PROCESS_EXIT(); } if(input->len1 > 0) { send_line(input->data1, input->len1); } } else if(ev == tcpip_event) { if(uip_newdata()) { newdata(uip_appdata, uip_datalen()); } #if 0 } else if(ev == resolv_event_found) { /* Either found a hostname, or not. */ if((char *)data != NULL && resolv_lookup((char *)data) != NULL) { uip_ipaddr_copy(serveraddr, ipaddr); telnet_connect(&s, server, serveraddr, nick); } else { shell_output_str(&udpsend_command, "Host not found.", ""); } #endif /* 0 */ } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(sensortag_process, ev, data) { static struct etimer et; uip_ipaddr_t ipaddr; etimer_set(&et, CLOCK_CONF_SECOND*20); PROCESS_BEGIN(); PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER); if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) { printf("NO IP \n\r"); } else{ printf("Good \n\r"); } print_local_addresses(); if(uiplib_ipaddrconv("2001:0db8:0002:0000:0000:0000:0000:0002", &ipaddr)){ printf("Get server IP! \n\r"); } /* new connection with remote host */ client_conn = udp_new(&ipaddr, UIP_HTONS(10001), NULL); udp_bind(client_conn, UIP_HTONS(10002)); printf("Connect! \n\r"); init_hdc_reading(NULL); while(1) { PROCESS_YIELD(); if(ev == sensors_event && data == &hdc_1000_sensor) { get_hdc_reading(); } } PROCESS_END(); }
void send_udp(char* data) { uip_ipaddr_t ipaddr; char* ipEnd; char* portEnd; uint16_t port; int i; char found; // Extract IP address ipEnd = strchr((const char*) data, ' '); ipEnd[0]=0; uiplib_ipaddrconv(data, &ipaddr); portEnd = strchr((const char*) ipEnd + 1, ' '); portEnd[0]=0; port = atoi(ipEnd + 1); //printf("Port target : %i\n",port); //uip_ip6addr(&ipaddr,0xFE80,0000,0000,0000,0x3E97,0x0EFF,0xFE00,0x0001 ); int res = udp_socket_sendto(&client_sock, portEnd+1, strlen(portEnd+1), &ipaddr, port); }
/*-----------------------------------------------------------------------------------*/ unsigned char webclient_get(const char *host, u16_t port, const char *file) { uip_ipaddr_t addr; struct uip_conn *conn; uip_ipaddr_t *ipaddr; /* First check if the host is an IP address. */ ipaddr = &addr; if(uiplib_ipaddrconv(host, &addr) == 0) { #if UIP_UDP ipaddr = resolv_lookup(host); if(ipaddr == NULL) { return 0; } #else /* UIP_UDP */ return 0; #endif /* UIP_UDP */ } conn = tcp_connect(ipaddr, uip_htons(port), NULL); if(conn == NULL) { return 0; } s.port = port; strncpy(s.file, file, sizeof(s.file)); strncpy(s.host, host, sizeof(s.host)); init_connection(); return 1; }
/*-----------------------------------------------------------------------------------*/ unsigned char webclient_get(char *host, u16_t port, char *file) { struct uip_conn *conn; uip_ipaddr_t *ipaddr; static uip_ipaddr_t addr; /* First check if the host is an IP address. */ ipaddr = &addr; if(uiplib_ipaddrconv(host, (unsigned char *)addr) == 0) { ipaddr = (uip_ipaddr_t *)resolv_lookup(host); if(ipaddr == NULL) { return 0; } } conn = uip_connect(ipaddr, htons(port)); if(conn == NULL) { return 0; } s.port = port; strncpy(s.file, file, sizeof(s.file)); strncpy(s.host, host, sizeof(s.host)); init_connection(); return 1; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(border_router_process, ev, data) { static struct etimer et; rpl_dag_t *dag; PROCESS_BEGIN(); prefix_set = 0; PROCESS_PAUSE(); PRINTF("RPL-Border router started\n"); slip_config_handle_arguments(contiki_argc, contiki_argv); /* tun init is also responsible for setting up the SLIP connection */ tun_init(); while(!mac_set) { etimer_set(&et, CLOCK_SECOND); request_mac(); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); } if(slip_config_ipaddr != NULL) { uip_ipaddr_t prefix; if(uiplib_ipaddrconv((const char *)slip_config_ipaddr, &prefix)) { PRINTF("Setting prefix "); PRINT6ADDR(&prefix); PRINTF("\n"); set_prefix_64(&prefix); } else { PRINTF("Parse error: %s\n", slip_config_ipaddr); exit(0); } } dag = rpl_set_root(RPL_DEFAULT_INSTANCE,(uip_ip6addr_t *)dag_id); if(dag != NULL) { rpl_set_prefix(dag, &prefix, 64); PRINTF("created a new RPL dag\n"); } #if DEBUG print_local_addresses(); #endif /* The border router runs with a 100% duty cycle in order to ensure high packet reception rates. */ NETSTACK_MAC.off(1); while(1) { etimer_set(&et, CLOCK_SECOND * 2); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); /* do anything here??? */ } PROCESS_END(); }
/*-----------------------------------------------------------------------------------*/ static void connect(void) { uip_ipaddr_t addr, *addrptr; uint16_t port; char *cptr; struct uip_conn *conn; /* Find the first space character in host and put a zero there to end the string. */ for(cptr = telnethost; *cptr != ' ' && *cptr != 0; ++cptr); *cptr = 0; addrptr = &addr; #if UIP_UDP if(uiplib_ipaddrconv(telnethost, &addr) == 0) { addrptr = resolv_lookup(telnethost); if(addrptr == NULL) { resolv_query(telnethost); show("Resolving host..."); return; } } #else /* UIP_UDP */ uiplib_ipaddrconv(telnethost, &addr); #endif /* UIP_UDP */ port = 0; for(cptr = telnetport; *cptr != ' ' && *cptr != 0; ++cptr) { if(*cptr < '0' || *cptr > '9') { show("Port number error"); return; } port = 10 * port + *cptr - '0'; } conn = tcp_connect(addrptr, uip_htons(port), &ts_appstate); if(conn == NULL) { show("Out of memory error"); return; } show("Connecting..."); }
PROCESS_THREAD(border_router_process, ev, data) { PROCESS_BEGIN(); PROCESS_PAUSE(); { rpl_dag_t *dag; char buf[sizeof(dag_id)]; memcpy(buf,dag_id,sizeof(dag_id)); dag = rpl_set_root((uip_ip6addr_t *)buf); /* Assign separate addresses to the uip stack and the host network interface, but with the same prefix */ /* E.g. bbbb::ff:fe00:200 to the stack and bbbb::1 to the host *fallback* network interface */ /* Otherwise the host will trap packets intended for the stack, just as the stack will trap packets intended for the host */ /* $ifconfig usb0 -arp on Ubuntu to skip the neighbor solicitations. Add explicit neighbors on other OSs */ if(dag != NULL) { printf("Created a new RPL dag\n"); #if UIP_CONF_ROUTER_RECEIVE_RA //Contiki stack will shut down until assigned an address from the interface RA //Currently this requires changes in the core rpl-icmp6.c to pass the link-local RA broadcast #else void sprint_ip6(uip_ip6addr_t addr); int i; uip_ip6addr_t ipaddr; #ifdef HARD_CODED_ADDRESS uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr); #else uip_ip6addr(&ipaddr, 0xbbbb, 0, 0, 0, 0, 0, 0, 0x1); #endif uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); rpl_set_prefix(dag, &ipaddr, 64); for (i=0;i<UIP_DS6_ADDR_NB;i++) { if (uip_ds6_if.addr_list[i].isused) { printf("IPV6 Address: ");sprint_ip6(uip_ds6_if.addr_list[i].ipaddr);printf("\n"); } } #endif } } /* The border router runs with a 100% duty cycle in order to ensure high packet reception rates. */ // NETSTACK_MAC.off(1); while(1) { PROCESS_YIELD(); /* Local and global dag repair can be done from ? */ // rpl_set_prefix(rpl_get_dag(RPL_ANY_INSTANCE), &ipaddr, 64); // rpl_repair_dag(rpl_get_dag(RPL_ANY_INSTANCE)); } PROCESS_END(); }
static char * parse_url(const char *url, uip_ipaddr_t *addr, char *path, uint16_t path_max_len) { char *ptr_b; char *ptr_e; char ip[IPSTR_SIZE]; uint16_t len; int retv; ptr_b = strstr_p(url, "<coap://["); if(!ptr_b) { return NULL; } // getting IP ptr_e = strstr(ptr_b, "]"); if(!ptr_e) { return NULL; } len = ptr_e - ptr_b; if(len > IPSTR_SIZE - 1) { return NULL; } memcpy(ip, ptr_b, len); ip[len] = '\0'; PRINTF("IP: %s\n", ip); retv = uiplib_ipaddrconv(ip, addr); if(retv == 0) { return NULL; } PRINT6ADDR(addr); PRINTF("\n"); // getting path ptr_b = ptr_e + 1; if(*ptr_b == '>') { // no path return ptr_b + 1; } if(*ptr_b != '/') { // path does not start with '/' return NULL; } ptr_b++; ptr_e = strstr(ptr_b, ">"); if(!ptr_e) { return NULL; } len = ptr_e - ptr_b; if(len > path_max_len - 1) { //len = path_max_len - 1; return NULL; } memcpy(path, ptr_b, len); path[len] = '\0'; PRINTF("Path: %s\n", path); return ptr_e + 1; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(border_router_process, ev, data) { #ifdef PREFIX_DISC static struct etimer et; #endif rpl_dag_t *dag; PROCESS_BEGIN(); prefix_set = 0; PROCESS_PAUSE(); SENSORS_ACTIVATE(button_sensor); PRINTF("RPL-Border router started\n"); /* The border router runs with a 100% duty cycle in order to ensure high packet reception rates. Note if the MAC RDC is not turned off now, aggressive power management of the cpu will interfere with establishing the SLIP connection */ NETSTACK_MAC.off(1); #ifndef PREFIX_DISC if (!uiplib_ipaddrconv("aaaa::", &prefix)) goto err; #else /* Request prefix until it has been received */ while(!prefix_set) { etimer_set(&et, CLOCK_SECOND); request_prefix(); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); } #endif dag = rpl_set_root(RPL_DEFAULT_INSTANCE,(uip_ip6addr_t *)dag_id); if(dag != NULL) { rpl_set_prefix(dag, &prefix, 64); PRINTF("created a new RPL dag\n"); } #if DEBUG || 1 print_local_addresses(); #endif while(1) { PROCESS_YIELD(); if (ev == sensors_event && data == &button_sensor) { PRINTF("Initiating global repair\n"); rpl_repair_root(RPL_DEFAULT_INSTANCE); } } err: PRINTF("Shutting down\n"); PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static resolv_status_t set_connection_address(uip_ipaddr_t *ipaddr, int *port) { #ifndef UDP_CONNECTION_ADDR #if RESOLV_CONF_SUPPORTS_MDNS && RESOLV_CONF_SUPPORTS_DNS_SD #define UDP_CONNECTION_ADDR _server._udp.local #elif RESOLV_CONF_SUPPORTS_MDNS #define UDP_CONNECTION_ADDR cus.local #elif UIP_CONF_ROUTER #define UDP_CONNECTION_ADDR fd00:0:0:0:0212:7404:0004:0404 #else #define UDP_CONNECTION_ADDR fe80:0:0:0:6466:6666:6666:6666 #endif #endif /* !UDP_CONNECTION_ADDR */ #define _QUOTEME(x) #x #define QUOTEME(x) _QUOTEME(x) resolv_status_t status = RESOLV_STATUS_ERROR; if(uiplib_ipaddrconv(QUOTEME(UDP_CONNECTION_ADDR), ipaddr) == 0) { /* * We are looking for a hostname and not an IP address. */ uip_ipaddr_t *resolved_addr = NULL; #if RESOLV_CONF_SUPPORTS_MDNS && RESOLV_CONF_SUPPORTS_DNS_SD status = resolv_service_lookup(QUOTEME(UDP_CONNECTION_ADDR),&resolved_addr,port); #else status = resolv_lookup(QUOTEME(UDP_CONNECTION_ADDR),&resolved_addr); #endif /* RESOLV_CONF_SUPPORTS_MDNS && RESOLV_CONF_SUPPORTS_DNS_SD */ if(status == RESOLV_STATUS_UNCACHED || status == RESOLV_STATUS_EXPIRED) { PRINTF("Attempting to look up %s\n",QUOTEME(UDP_CONNECTION_ADDR)); #if RESOLV_CONF_SUPPORTS_MDNS && RESOLV_CONF_SUPPORTS_DNS_SD resolv_query_service(QUOTEME(UDP_CONNECTION_ADDR)); #else resolv_query(QUOTEME(UDP_CONNECTION_ADDR)); #endif /* RESOLV_CONF_SUPPORTS_MDNS && RESOLV_CONF_SUPPORTS_DNS_SD */ status = RESOLV_STATUS_RESOLVING; } else if(status == RESOLV_STATUS_CACHED && resolved_addr != NULL #if RESOLV_CONF_SUPPORTS_MDNS && RESOLV_CONF_SUPPORTS_DNS_SD && port != NULL #endif /* RESOLV_CONF_SUPPORTS_MDNS && RESOLV_CONF_SUPPORTS_DNS_SD */ ) { PRINTF("Lookup of \"%s\" succeded!\n",QUOTEME(UDP_CONNECTION_ADDR)); } else if(status == RESOLV_STATUS_RESOLVING) { PRINTF("Still looking up \"%s\"...\n",QUOTEME(UDP_CONNECTION_ADDR)); } else { PRINTF("Lookup of \"%s\" failed. status = %d\n",QUOTEME(UDP_CONNECTION_ADDR),status); } if(resolved_addr) uip_ipaddr_copy(ipaddr, resolved_addr); } else { status = RESOLV_STATUS_CACHED; } return status; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(ipv6_process, ev, data) { PROCESS_BEGIN(); // Set the local address uip_ip6addr(&my_addr, 0, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&my_addr, &uip_lladdr); uip_ds6_addr_add(&my_addr, 0, ADDR_MANUAL); // Setup the destination address uiplib_ipaddrconv(RECEIVER_ADDR, &dest_addr); // Add a "neighbor" for our custom route // Setup the default broadcast route uiplib_ipaddrconv(ADDR_ALL_ROUTERS, &bcast_ipaddr); uip_ds6_nbr_add(&bcast_ipaddr, &bcast_lladdr, 0, NBR_REACHABLE); uip_ds6_route_add(&dest_addr, 128, &bcast_ipaddr); // Setup a udp "connection" client_conn = udp_new(&dest_addr, UIP_HTONS(RECEIVER_PORT), NULL); if (client_conn == NULL) { // Too many udp connections // not sure how to exit...stupid contiki } udp_bind(client_conn, UIP_HTONS(3001)); etimer_set(&periodic_timer, 10*CLOCK_SECOND); while(1) { PROCESS_YIELD(); if (etimer_expired(&periodic_timer)) { send_handler(ev, data); etimer_restart(&periodic_timer); } else if (ev == tcpip_event) { recv_handler(); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(shell_tcpsend_process, ev, data) { char *next; const char *dummy; struct shell_input *input; uint16_t port; PROCESS_BEGIN(); next = strchr(data, ' '); if(next == NULL) { shell_output_str(&tcpsend_command, "tcpsend <server> <port>: server as address", ""); PROCESS_EXIT(); } *next = 0; ++next; strncpy(server, data, sizeof(server)); port = shell_strtolong(next, &dummy); running = 1; uiplib_ipaddrconv(server, &serveraddr); telnet_connect(&s, &serveraddr, port); while(running) { PROCESS_WAIT_EVENT(); if(ev == shell_event_input) { input = data; if(input->len1 + input->len2 == 0) { PROCESS_EXIT(); } if(input->len1 > 0) { send_line(&s, input->data1, input->len1); } } else if(ev == tcpip_event) { telnet_app(data); #if 0 } else if(ev == resolv_event_found) { /* Either found a hostname, or not. */ if((char *)data != NULL && resolv_lookup((char *)data) != NULL) { uip_ipaddr_copy(serveraddr, ipaddr); telnet_connect(&s, server, serveraddr, nick); } else { shell_output_str(&tcpsend_command, "Host not found.", ""); } #endif /* 0 */ } } PROCESS_END(); }
static int native_config_network_route_handler(config_level_t level, void* user, const char* section, const char* name, const char* value) { static uip_ipaddr_t ipaddr; static uint8_t length = 0; static uip_ipaddr_t next_hop; if(level != CONFIG_LEVEL_NETWORK) { return 1; } if(!name) { if(!uip_is_addr_unspecified(&ipaddr) && length > 0 && !uip_is_addr_unspecified(&next_hop)) { uip_ds6_route_add_static(&ipaddr, length, &next_hop); } else { LOG6LBR_ERROR("Missing parameters for route creation\n"); return 0; } //Reset parameters uip_create_unspecified(&ipaddr); uip_create_unspecified(&next_hop); length = 0; return 1; } if(strcmp(name, "dest") == 0) { if(uiplib_ipaddrconv(value, &ipaddr) == 0) { LOG6LBR_ERROR("Invalid ip address : %s\n", value); return 0; } length = 128; } else if (strcmp(name, "via") == 0) { if(uiplib_ipaddrconv(value, &next_hop) == 0) { LOG6LBR_ERROR("Invalid ip address : %s\n", value); return 0; } } else { LOG6LBR_ERROR("Invalid parameter : %s\n", name); return 0; } return 1; }
/*---------------------------------------------------------------------------*/ static void setup_lwm2m_servers(void) { #ifdef LWM2M_SERVER_ADDRESS uip_ipaddr_t addr; if(uiplib_ipaddrconv(LWM2M_SERVER_ADDRESS, &addr)) { lwm2m_engine_register_with_bootstrap_server(&addr, 0); lwm2m_engine_register_with_server(&addr, 0); } #endif /* LWM2M_SERVER_ADDRESS */ lwm2m_engine_use_bootstrap_server(REGISTER_WITH_LWM2M_BOOTSTRAP_SERVER); lwm2m_engine_use_registration_server(REGISTER_WITH_LWM2M_SERVER); }
/*---------------------------------------------------------------------------*/ static void set_connection_address(uip_ipaddr_t *ipaddr) { #define _QUOTEME(x) #x #define QUOTEME(x) _QUOTEME(x) #ifdef UDP_CONNECTION_ADDR if(uiplib_ipaddrconv(QUOTEME(UDP_CONNECTION_ADDR), ipaddr) == 0) { PRINTF("UDP client failed to parse address '%s'\n", QUOTEME(UDP_CONNECTION_ADDR)); } #else uip_ip6addr(ipaddr, 0xfe80, 0x0000, 0x0000, 0x0000, 0x6500, 0x0012, 0x91c3, 0x2501); #endif /* UDP_CONNECTION_ADDR */ }
/*---------------------------------------------------------------------------*/ int uip_util_text2addr(const char *src, uip_ipaddr_t *dest, int *port) { int p = 0; if (!uiplib_ipaddrconv(src, dest)) goto error; if (!(src = rindex(src, ']'))) goto success; if (*(++src) == '\0') goto success; if (*(src++) != ':') goto error; if (!(p = atoi(src))) goto error; success: if (port) *port = p; return 1; error: return 0; }
/* * this function process the request which fall into 2 cases: request, and reply * an example of receiving request: { 'status': 'start', //start/stop 'update': 1, //in second 'addr': 'aaaa::1', 'port': 20000 } */ char collectd_processing(u8_t* const input, const u16_t input_len, collectd_conf_t *collectd_conf) { static jsmn_parser p; static jsmntok_t tokens[MAX_TOKEN]; static char value[TOKEN_LEN]; //values to save temp from input json (before make sure json msg is valid) u8_t commandtype; u16_t update; u16_t srcport; uip_ipaddr_t mnaddr; int r; input[input_len] = 0; PRINTF("%s\n", input); jsmn_init(&p); r = jsmn_parse(&p, (char*)input, tokens, MAX_TOKEN); check(r == JSMN_SUCCESS); //get status check(js_get(input, tokens, MAX_TOKEN, "status", value, MAX_TOKEN) == JSMN_TOKEN_SUCCESS); check( (strcmp(value, "start")==0) || (strcmp(value, "stop")==0) ); commandtype = (strcmp(value, "start") == 0)? COMMAND_START : COMMAND_STOP; //PRINTF("start = %d\n", commandtype); check(js_get(input, tokens, MAX_TOKEN, "update", value, MAX_TOKEN) == JSMN_TOKEN_SUCCESS); errno = 0; update = (u16_t)strtol(value, NULL, 10); //convert to base 10 check(!(errno == ERANGE || (errno != 0 && update == 0))); //PRINTF("update = %d\n", update); check(js_get(input, tokens, MAX_TOKEN, "addr", value, MAX_TOKEN) == JSMN_TOKEN_SUCCESS); check(uiplib_ipaddrconv(value, &mnaddr) == 1); //PRINT6ADDR(&mnaddr); //PRINTF("\n"); check(js_get(input, tokens, MAX_TOKEN, "port", value, MAX_TOKEN) == JSMN_TOKEN_SUCCESS); errno = 0; srcport = (u16_t)strtol(value, NULL, 10); //convert to base 10 check(!(errno == ERANGE || (errno != 0 && update == 0))); //PRINTF("port = %d\n", srcport); /*save the request to conf*/ collectd_conf->send_active = commandtype; collectd_conf->update_freq_in_sec = update; collectd_conf->mnrport = srcport; uip_ipaddr_copy(&collectd_conf->mnaddr, &mnaddr); return COLLECTD_ERROR_NO_ERROR; }
//process PROCESS_THREAD(ntp_client_Process, ev, data) { static struct etimer ntp_timer; PROCESS_BEGIN(); //printf("ntp_client_Process begin\n"); process_start(&ntp_connectPprocess, NULL); ntp.client_sock = -1; ntp.exit_process = 0; ntp.retry_cnt = 0; ntp.client_sock = udpcreate(NTP_LOCAL_PORT, &ntp_connectPprocess); if(ntp.client_sock == -1) { printf("create udp socket fail"); ntp.exit_process = 1; } else { //printf("create socket:%d\n", ntp.client_sock); //us.pool.ntp.org(108.61.56.35) if( uiplib_ipaddrconv("108.61.56.35", &ntp.server_ip) == 0) { printf("erro ip format\n"); ntp.exit_process = 1; } } memset( &packet, 0, sizeof( ntp_packet ) ); *( ( char * ) &packet + 0 ) = 0x1b; // Represents 27 in base 10 or 00011011 in base 2. while(!ntp.exit_process) { if(ntp.retry_cnt > MAX_RETRYCNT) { printf("ntp client can not rece ntp server data, please check network\n"); break; } etimer_set(&ntp_timer, 1 * CLOCK_SECOND); PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER); if(udpsendto(ntp.client_sock, (char *)&packet, sizeof(packet), &ntp.server_ip, NTP_SERVER_PORT) == -1) { printf("udpsendto fail\n"); } ntp.retry_cnt++; } if(udpclose(ntp.client_sock) == -1) { printf("udpclose fail\n"); } process_post_synch(&ntp_connectPprocess, PROCESS_EVENT_EXIT, NULL); //printf("ntp_client_Process end\n"); PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void set_connection_address(uip_ipaddr_t *ipaddr) { #define _QUOTEME(x) #x #define QUOTEME(x) _QUOTEME(x) #ifdef UDP_CONNECTION_ADDR if(uiplib_ipaddrconv(QUOTEME(UDP_CONNECTION_ADDR), ipaddr) == 0) { PRINTF("UDP client failed to parse address '%s'\n", QUOTEME(UDP_CONNECTION_ADDR)); } #elif UIP_CONF_ROUTER uip_ip6addr(ipaddr,0xaaaa,0,0,0,0x0212,0x7404,0x0004,0x0404); #else uip_ip6addr(ipaddr,0xfe80,0,0,0,0x6466,0x6666,0x6666,0x6666); #endif /* UDP_CONNECTION_ADDR */ }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(shell_irc_process, ev, data) { char *next; struct shell_input *input; PROCESS_BEGIN(); next = strchr(data, ' '); if(next == NULL) { shell_output_str(&irc_command, "irc <server> <nick>: server as address", ""); PROCESS_EXIT(); } *next = 0; ++next; strncpy(server, data, sizeof(server)); strncpy(nick, next, sizeof(nick)); running = 1; uiplib_ipaddrconv(server, (u8_t *)&serveraddr); ircc_connect(&s, server, &serveraddr, nick); while(running) { PROCESS_WAIT_EVENT(); if(ev == shell_event_input) { input = data; if(input->len1 > 0) { parse_line(input->data1); } } else if(ev == tcpip_event) { ircc_appcall(data); #if 0 } else if(ev == resolv_event_found) { /* Either found a hostname, or not. */ if((char *)data != NULL && resolv_lookup((char *)data) != NULL) { uip_ipaddr_copy(serveraddr, ipaddr); ircc_connect(&s, server, serveraddr, nick); } else { shell_output_str(&irc_command, "Host not found.", ""); } #endif /* 0 */ } } PROCESS_END(); }
static httpd_cgi_call_t * webserver_network_nbr_rm(struct httpd_state *s) { uip_ds6_nbr_t *neighbor; uip_ipaddr_t ipaddr; webserver_result_title = "Network"; webserver_result_text = "Delete neighbor: Neighbor not found"; if(s->query && uiplib_ipaddrconv(s->query, &ipaddr) != 0) { neighbor = uip_ds6_nbr_lookup(&ipaddr); if (neighbor) { uip_ds6_nbr_rm(neighbor); webserver_result_text = "Neighbor deleted"; } } return &webserver_result_page; }
static httpd_cgi_call_t * webserver_network_route_rm(struct httpd_state *s) { uip_ds6_route_t *route; uip_ipaddr_t ipaddr; webserver_result_title = "Network"; webserver_result_text = "Delete route: Route not found"; if(s->query && uiplib_ipaddrconv(s->query, &ipaddr) != 0) { route = uip_ds6_route_lookup(&ipaddr); if(route) { uip_ds6_route_rm(route); webserver_result_text = "Route deleted"; } } return &webserver_result_page; }
smcp_t smcp_plat_init(smcp_t self) { SMCP_EMBEDDED_SELF_HOOK; #if UIP_CONF_IPV6 { uip_ipaddr_t all_coap_nodes_addr; if(uiplib_ipaddrconv( COAP_MULTICAST_IP6_LL_ALLDEVICES, &all_coap_nodes_addr )) { uip_ds6_maddr_add(&all_coap_nodes_addr); } } #endif return self; }
static httpd_cgi_call_t * webserver_sensor_delete_node(struct httpd_state *s) { static uip_ipaddr_t ipaddr; static node_info_t * node_info = NULL; webserver_result_title = "Sensor"; if(s->query && strncmp(s->query, "ip=", 3) == 0 && uiplib_ipaddrconv(s->query + 3, &ipaddr) != 0) { node_info = node_info_lookup(&ipaddr); if(node_info) { node_info_rm(node_info); webserver_result_text = "Sensor deleted"; } else { webserver_result_text = "Sensor address unknown"; } } else { webserver_result_text = "Sensor address missing"; } return &webserver_result_page; }
/*---------------------------------------------------------------------------*/ static resolv_status_t set_connection_address(uip_ipaddr_t *ipaddr) { #ifndef UDP_CONNECTION_ADDR //#if RESOLV_CONF_SUPPORTS_MDNS #if 0 #define UDP_CONNECTION_ADDR contiki-udp-server.local #elif UIP_CONF_ROUTER #define UDP_CONNECTION_ADDR aaaa:0:0:0:0201:2dcf:4629:04b4 #else #define UDP_CONNECTION_ADDR fe80:0:0:0:6466:6666:6666:6666 #endif #endif /* !UDP_CONNECTION_ADDR */ #define _QUOTEME(x) #x #define QUOTEME(x) _QUOTEME(x) resolv_status_t status = RESOLV_STATUS_ERROR; if(uiplib_ipaddrconv(QUOTEME(UDP_CONNECTION_ADDR), ipaddr) == 0) { uip_ipaddr_t *resolved_addr = NULL; status = resolv_lookup(QUOTEME(UDP_CONNECTION_ADDR),&resolved_addr); if(status == RESOLV_STATUS_UNCACHED || status == RESOLV_STATUS_EXPIRED) { PRINTF("Attempting to look up %s\n",QUOTEME(UDP_CONNECTION_ADDR)); resolv_query(QUOTEME(UDP_CONNECTION_ADDR)); status = RESOLV_STATUS_RESOLVING; } else if(status == RESOLV_STATUS_CACHED && resolved_addr != NULL) { PRINTF("Lookup of \"%s\" succeded!\n",QUOTEME(UDP_CONNECTION_ADDR)); } else if(status == RESOLV_STATUS_RESOLVING) { PRINTF("Still looking up \"%s\"...\n",QUOTEME(UDP_CONNECTION_ADDR)); } else { PRINTF("Lookup of \"%s\" failed. status = %d\n",QUOTEME(UDP_CONNECTION_ADDR),status); } if(resolved_addr) uip_ipaddr_copy(ipaddr, resolved_addr); } else { status = RESOLV_STATUS_CACHED; } return status; }
/*---------------------------------------------------------------------------*/ static void set_connection_address(uip_ipaddr_t *ipaddr) { #define _QUOTEME(x) #x #define QUOTEME(x) _QUOTEME(x) #ifdef UDP_CONNECTION_ADDR if(uiplib_ipaddrconv(QUOTEME(UDP_CONNECTION_ADDR), ipaddr) == 0) { PRINTF("UDP client failed to parse address '%s'\n", QUOTEME(UDP_CONNECTION_ADDR)); } #elif UIP_CONF_ROUTER // uip_ip6addr(ipaddr,0xaaaa,0,0,0,0x0212,0x7404,0x0004,0x0404); // IP_CLIENT // define IP_SERVER fe80::2a01:22ff:fe33:4455 uip_ip6addr(ipaddr,0xaaaa,0,0,0,0x0212,0x7404,0x0004,0x0404); #else // uip_ip6addr(ipaddr,0xfe80,0,0,0,0x6466,0x6666,0x6666,0x6666); uip_ip6addr(ipaddr,0xfe80,0,0,0,0x2a01,0x22ff,0xfe33,0x4455); #endif /* UDP_CONNECTION_ADDR */ }
void mqtt_tcp_connect(char *addr, int port) { uip_ip4addr_t remote_ip_addr; if(uiplib_ipaddrconv(addr, &remote_ip_addr) == 0) { resolv_query(addr); return; } else { g_mqtt_socket_para.connect_status = SOCKET_CREATE; g_mqtt_socket_para.fd = tcpconnect(&remote_ip_addr, port, &mqtt_socket_process); printf("Tcp socket(%d) connectting to [%d.%d.%d.%d:%d]\n", g_mqtt_socket_para.fd, remote_ip_addr.u8[0], remote_ip_addr.u8[1], remote_ip_addr.u8[2], remote_ip_addr.u8[3], port); } }