void list_tcps(void) { UInt32 num = 0; struct tcp_pcb *pcb; char local_ip_str[16]; char remote_ip_str[16]; extern struct tcp_pcb *tcp_active_pcbs; extern union tcp_listen_pcbs_t tcp_listen_pcbs; extern struct tcp_pcb *tcp_tw_pcbs; extern const char *tcp_state_str[]; rt_enter_critical(); printf("Active PCB states:\n"); for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip))); strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip))); printf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ", num++, local_ip_str, pcb->local_port, remote_ip_str, pcb->remote_port, pcb->snd_nxt, pcb->rcv_nxt); printf("state: %s\n", tcp_state_str[pcb->state]); } printf("Listen PCB states:\n"); num = 0; for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) { printf("#%d local port %d ", num++, pcb->local_port); printf("state: %s\n", tcp_state_str[pcb->state]); } printf("TIME-WAIT PCB states:\n"); num = 0; for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip))); strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip))); printf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ", num++, local_ip_str, pcb->local_port, remote_ip_str, pcb->remote_port, pcb->snd_nxt, pcb->rcv_nxt); printf("state: %s\n", tcp_state_str[pcb->state]); } rt_exit_critical(); }
void list_if(void) { rt_ubase_t index; struct netif * netif; rt_enter_critical(); netif = netif_list; while( netif != NULL ) { printf("network interface: %c%c%s\n", netif->name[0], netif->name[1], (netif == netif_default)?" (Default)":""); printf("MTU: %d\n", netif->mtu); printf("MAC: "); for (index = 0; index < netif->hwaddr_len; index ++) printf("%02x ", netif->hwaddr[index]); printf("\nFLAGS:"); if (netif->flags & NETIF_FLAG_UP) printf(" UP"); else printf(" DOWN"); if (netif->flags & NETIF_FLAG_LINK_UP) printf(" LINK_UP"); else printf(" LINK_DOWN"); if (netif->flags & NETIF_FLAG_DHCP) printf(" DHCP"); if (netif->flags & NETIF_FLAG_POINTTOPOINT) printf(" PPP"); if (netif->flags & NETIF_FLAG_ETHARP) printf(" ETHARP"); if (netif->flags & NETIF_FLAG_IGMP) printf(" IGMP"); printf("\n"); printf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr))); printf("gw address: %s\n", ipaddr_ntoa(&(netif->gw))); printf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask))); printf("\r\n"); netif = netif->next; } #if LWIP_DNS { struct ip_addr ip_addr; for(index=0; index<DNS_MAX_SERVERS; index++) { ip_addr = dns_getserver(index); printf("dns server #%d: %s\n", index, ipaddr_ntoa(&(ip_addr))); } } #endif /**< #if LWIP_DNS */ rt_exit_critical(); }
void socks_tcp_connect(struct socks_data *data) { struct tcp_pcb *pcb; err_t ret; bufferevent_disable(data->bev, EV_READ); LWIP_DEBUGF(SOCKS_DEBUG, ("%s: %s:%d\n", __func__, ipaddr_ntoa(&data->ipaddr), data->port)); pcb = tcp_new(); if (!pcb) data->connect_failed(data); pcb->flags |= TF_NODELAY; if (data->keep_alive) { pcb->so_options |= SOF_KEEPALIVE; pcb->keep_intvl = data->keep_alive; pcb->keep_idle = data->keep_alive; } ret = tcp_connect(pcb, &data->ipaddr, data->port, socks_tcp_connect_ok); if (ret < 0) { tcp_abort(pcb); data->connect_failed(data); } else { data->pcb = pcb; tcp_arg(pcb, data); tcp_err(pcb, socks_tcp_connect_err); } }
static err_t mg_lwip_accept_cb(void *arg, struct tcp_pcb *newtpcb, err_t err) { struct mg_connection *lc = (struct mg_connection *) arg; (void) err; DBG(("%p conn %p from %s:%u", lc, newtpcb, ipaddr_ntoa(&newtpcb->remote_ip), newtpcb->remote_port)); struct mg_connection *nc = mg_if_accept_new_conn(lc); if (nc == NULL) { tcp_abort(newtpcb); return ERR_ABRT; } struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock; cs->pcb.tcp = newtpcb; tcp_arg(newtpcb, nc); tcp_err(newtpcb, mg_lwip_tcp_error_cb); tcp_sent(newtpcb, mg_lwip_tcp_sent_cb); tcp_recv(newtpcb, mg_lwip_tcp_recv_cb); #ifdef SSL_KRYPTON if (lc->ssl_ctx != NULL) { nc->ssl = SSL_new(lc->ssl_ctx); if (nc->ssl == NULL || SSL_set_fd(nc->ssl, (intptr_t) nc) != 1) { LOG(LL_ERROR, ("SSL error")); tcp_close(newtpcb); } } else #endif { mg_lwip_accept_conn(nc, newtpcb); } return ERR_OK; }
static void ping_recv(int s, const ip_addr_t *addr) { char buf[200]; socklen_t fromlen; int len; struct sockaddr_storage from; ip_addr_t ip_from; LWIP_UNUSED_ARG(addr); len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, &fromlen); #if LWIP_IPV4 if(!IP_IS_V6(addr)) { struct sockaddr_in *from4 = (struct sockaddr_in*)&from; inet_addr_to_ipaddr(ip_2_ip4(&ip_from), &from4->sin_addr); } #endif /* LWIP_IPV4 */ #if LWIP_IPV6 if(IP_IS_V6(addr)) { struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from; inet6_addr_to_ip6addr(ip_2_ip6(&ip_from), &from6->sin6_addr); } #endif /* LWIP_IPV6 */ printf("Received %d bytes from %s\n", len, ipaddr_ntoa(&ip_from)); }
int http_wifi_api_get_status(http_connection *c) { CGI_WIFI_DBG("wifi get_status\n"); //wait for whole body if(c->state <HTTPD_STATE_BODY_END) { return HTTPD_CGI_MORE; } api_cgi_status * status = c->cgi.data; //first call, send headers if(status==NULL) { status = (api_cgi_status*)os_malloc(sizeof(api_cgi_status)); status->state=1; c->cgi.data=status; http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE); http_response_OK(c); return HTTPD_CGI_MORE; } else if(status->state==1) { wifi_station_get_config(&wifi_status.station_config); uint8_t c_status = wifi_station_get_connect_status(); //return JSON cJSON *root = cJSON_CreateObject(); cJSON_AddBoolToObject(root,"scanning",wifi_status.scanning); cJSON_AddStringToObject(root,"ssid",(const char *)wifi_status.station_config.ssid); cJSON_AddNumberToObject(root,"mode",wifi_get_opmode()); cJSON_AddNumberToObject(root,"station_status",c_status); //got ip if(c_status==5) { struct ip_info ip; wifi_get_ip_info(0x0,&ip); char *ip_str = (char*)ipaddr_ntoa(&ip.ip); cJSON_AddStringToObject(root,"ip",ip_str); } else { cJSON_AddStringToObject(root,"ip",""); } http_write_json(c,root); cJSON_Delete(root); status->state=99; return HTTPD_CGI_MORE; } else { os_free(c->cgi.data); return HTTPD_CGI_DONE; } }
/** * Returns an entry containing addresses of address family AF_INET * for the host with name name. * Due to dns_gethostbyname limitations, only one address is returned. * * @param name the hostname to resolve * @return an entry containing addresses of address family AF_INET * for the host with name name */ struct hostent* lwip_gethostbyname(const char *name) { err_t err; ip_addr_t addr; /* buffer variables for lwip_gethostbyname() */ HOSTENT_STORAGE struct hostent s_hostent; HOSTENT_STORAGE char *s_aliases; HOSTENT_STORAGE ip_addr_t s_hostent_addr; HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2]; HOSTENT_STORAGE char s_hostname[DNS_MAX_NAME_LENGTH + 1]; /* query host IP address */ err = netconn_gethostbyname(name, &addr); if (err != ERR_OK) { LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err)); h_errno = HOST_NOT_FOUND; return NULL; } /* fill hostent */ s_hostent_addr = addr; s_phostent_addr[0] = &s_hostent_addr; s_phostent_addr[1] = NULL; strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH); s_hostname[DNS_MAX_NAME_LENGTH] = 0; s_hostent.h_name = s_hostname; s_aliases = NULL; s_hostent.h_aliases = &s_aliases; s_hostent.h_addrtype = AF_INET; s_hostent.h_length = sizeof(ip_addr_t); s_hostent.h_addr_list = (char**)&s_phostent_addr; #if DNS_DEBUG /* dump hostent */ LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name == %s\n", s_hostent.h_name)); LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases == %p\n", (void*)s_hostent.h_aliases)); /* h_aliases are always empty */ LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype == %d\n", s_hostent.h_addrtype)); LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length == %d\n", s_hostent.h_length)); LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list == %p\n", (void*)s_hostent.h_addr_list)); if (s_hostent.h_addr_list != NULL) { u8_t idx; for (idx=0; s_hostent.h_addr_list[idx]; idx++) { LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i] == %p\n", idx, s_hostent.h_addr_list[idx])); LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa((ip_addr_t*)s_hostent.h_addr_list[idx]))); } } #endif /* DNS_DEBUG */ #if LWIP_DNS_API_HOSTENT_STORAGE /* this function should return the "per-thread" hostent after copy from s_hostent */ return sys_thread_hostent(&s_hostent); #else return &s_hostent; #endif /* LWIP_DNS_API_HOSTENT_STORAGE */ }
void mqtt_dnsCallback(const char *name, ip_addr_t *ipaddr, void *callback_arg) { mqtt_client_t *client = (mqtt_client_t *)callback_arg; client->serverAddr.sin_addr.s_addr = ipaddr; printf("IP ADDRESS FOUND: %s\n", ipaddr_ntoa(ipaddr)); dnsDone = 1; }
static void lwiperf_report(void *arg, enum lwiperf_report_type report_type, const ip_addr_t* local_addr, u16_t local_port, const ip_addr_t* remote_addr, u16_t remote_port, u32_t bytes_transferred, u32_t ms_duration, u32_t bandwidth_kbitpsec) { LWIP_UNUSED_ARG(arg); LWIP_UNUSED_ARG(local_addr); LWIP_UNUSED_ARG(local_port); printf("IPERF report: type=%d, remote: %s:%d, total bytes: %lu, duration in ms: %lu, kbits/s: %lu\n", (int)report_type, ipaddr_ntoa(remote_addr), (int)remote_port, bytes_transferred, ms_duration, bandwidth_kbitpsec); }
/** * Initialize the connection struct (from IP address) */ static err_t httpc_init_connection_addr(httpc_state_t **connection, const httpc_connection_t *settings, const ip_addr_t* server_addr, u16_t server_port, const char* uri, altcp_recv_fn recv_fn, void* callback_arg) { char *server_addr_str = ipaddr_ntoa(server_addr); if (server_addr_str == NULL) { return ERR_VAL; } return httpc_init_connection_common(connection, settings, server_addr_str, server_port, uri, recv_fn, callback_arg, 1); }
static err_t mg_lwip_tcp_conn_cb(void *arg, struct tcp_pcb *tpcb, err_t err) { struct mg_connection *nc = (struct mg_connection *) arg; DBG(("%p connect to %s:%u = %d", nc, ipaddr_ntoa(&tpcb->remote_ip), tpcb->remote_port, err)); if (nc == NULL) { tcp_abort(tpcb); return ERR_ARG; } nc->err = err; system_os_post(MG_TASK_PRIORITY, MG_SIG_CONNECT_RESULT, (uint32_t) nc); return ERR_OK; }
void eth_onUDPDataReceived(void* arg, struct udp_pcb* upcb, struct pbuf* p, struct ip_addr* addr, u16_t port) { char *srcAddr = ipaddr_ntoa(addr); myprintf("src addr: %s %d\r\n", srcAddr, p->tot_len); uint16_t packetId; uint8_t d[p->tot_len]; pbuf_copy_partial(p, d, p->tot_len, 0); provProcess(d, p->tot_len); pbuf_free(p); }
int mg_if_listen_udp(struct mg_connection *nc, union socket_address *sa) { struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock; struct udp_pcb *upcb = udp_new(); ip_addr_t *ip = (ip_addr_t *) &sa->sin.sin_addr.s_addr; u16_t port = ntohs(sa->sin.sin_port); cs->err = udp_bind(upcb, ip, port); DBG(("%p udb_bind(%s:%u) = %d", nc, ipaddr_ntoa(ip), port, cs->err)); if (cs->err != ERR_OK) { udp_remove(upcb); return -1; } udp_recv(upcb, mg_lwip_udp_recv_cb, nc); cs->pcb.udp = upcb; return 0; }
int mg_if_listen_tcp(struct mg_connection *nc, union socket_address *sa) { struct tcp_pcb *tpcb = tcp_new(); ip_addr_t *ip = (ip_addr_t *) &sa->sin.sin_addr.s_addr; u16_t port = ntohs(sa->sin.sin_port); nc->err = tcp_bind(tpcb, ip, port); DBG(("%p tcp_bind(%s:%u) = %d", nc, ipaddr_ntoa(ip), port, nc->err)); if (nc->err != ERR_OK) { tcp_close(tpcb); return -1; } nc->sock = (int) tpcb; tcp_arg(tpcb, nc); tpcb = tcp_listen(tpcb); tcp_accept(tpcb, mg_lwip_accept_cb); return 0; }
void eth_onUDPDataReceived(void* arg, struct udp_pcb* upcb, struct pbuf* p, struct ip_addr* addr, u16_t port) { TByteBuffer buffer; buffer.p = p; buffer.pos = 0; char *srcAddr = ipaddr_ntoa(addr); myprintf("src addr: %s\r\n", srcAddr); uint16_t packetId; if (BYTEBUFFER_FETCH(&buffer, packetId)) return; provProcess(&buffer); pbuf_free(p); }
/** Prepare HELO/EHLO message */ static enum smtp_session_state smtp_prepare_helo(struct smtp_session *s, u16_t *tx_buf_len, struct altcp_pcb *pcb) { size_t ipa_len; const char *ipa = ipaddr_ntoa(altcp_get_ip(pcb, 1)); LWIP_ASSERT("ipaddr_ntoa returned NULL", ipa != NULL); ipa_len = strlen(ipa); LWIP_ASSERT("string too long", ipa_len <= (SMTP_TX_BUF_LEN-SMTP_CMD_EHLO_1_LEN-SMTP_CMD_EHLO_2_LEN)); *tx_buf_len = (u16_t)(SMTP_CMD_EHLO_1_LEN + (u16_t)ipa_len + SMTP_CMD_EHLO_2_LEN); LWIP_ASSERT("tx_buf overflow detected", *tx_buf_len <= SMTP_TX_BUF_LEN); SMEMCPY(s->tx_buf, SMTP_CMD_EHLO_1, SMTP_CMD_EHLO_1_LEN); MEMCPY(&s->tx_buf[SMTP_CMD_EHLO_1_LEN], ipa, ipa_len); SMEMCPY(&s->tx_buf[SMTP_CMD_EHLO_1_LEN + ipa_len], SMTP_CMD_EHLO_2, SMTP_CMD_EHLO_2_LEN); return SMTP_HELO; }
static void mg_lwip_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port) { struct mg_connection *nc = (struct mg_connection *) arg; size_t len = p->len; char *data = (char *) malloc(len); union socket_address sa; DBG(("%p %s:%u %u", nc, ipaddr_ntoa(addr), port, p->len)); if (data == NULL) { DBG(("OOM")); pbuf_free(p); return; } sa.sin.sin_addr.s_addr = addr->addr; sa.sin.sin_port = htons(port); pbuf_copy_partial(p, data, len, 0); pbuf_free(p); mg_if_recv_udp_cb(nc, data, len, &sa, sizeof(sa.sin)); }
static err_t mg_lwip_accept_cb(void *arg, struct tcp_pcb *newtpcb, err_t err) { struct mg_connection *lc = (struct mg_connection *) arg, *nc; union socket_address sa; DBG(("%p conn from %s:%u\n", lc, ipaddr_ntoa(&newtpcb->remote_ip), newtpcb->remote_port)); sa.sin.sin_addr.s_addr = newtpcb->remote_ip.addr; sa.sin.sin_port = htons(newtpcb->remote_port); nc = mg_if_accept_tcp_cb(lc, &sa, sizeof(sa.sin)); if (nc == NULL) { tcp_abort(newtpcb); return ERR_ABRT; } nc->sock = (int) newtpcb; tcp_arg(newtpcb, nc); tcp_err(newtpcb, mg_lwip_tcp_error_cb); tcp_sent(newtpcb, mg_lwip_tcp_sent_cb); tcp_recv(newtpcb, mg_lwip_tcp_recv_cb); return ERR_OK; }
static err_t mg_lwip_tcp_conn_cb(void *arg, struct tcp_pcb *tpcb, err_t err) { struct mg_connection *nc = (struct mg_connection *) arg; DBG(("%p connect to %s:%u = %d", nc, ipaddr_ntoa(&tpcb->remote_ip), tpcb->remote_port, err)); if (nc == NULL) { tcp_abort(tpcb); return ERR_ARG; } struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock; cs->err = err; #ifdef SSL_KRYPTON if (err == 0 && nc->ssl != NULL) { SSL_set_fd(nc->ssl, (intptr_t) nc); mg_lwip_ssl_do_hs(nc); } else #endif { system_os_post(MG_TASK_PRIORITY, MG_SIG_CONNECT_RESULT, (uint32_t) nc); } return ERR_OK; }
/** * Send out an sntp request. * * @param arg is unused (only necessary to conform to sys_timeout) */ static void sntp_request(void *arg) { ip_addr_t sntp_server_address; err_t err; LWIP_UNUSED_ARG(arg); /* initialize SNTP server address */ #if SNTP_SERVER_DNS if (sntp_servers[sntp_current_server].name) { /* always resolve the name and rely on dns-internal caching & timeout */ ip_addr_set_zero(&sntp_servers[sntp_current_server].addr); err = dns_gethostbyname(sntp_servers[sntp_current_server].name, &sntp_server_address, sntp_dns_found, NULL); if (err == ERR_INPROGRESS) { /* DNS request sent, wait for sntp_dns_found being called */ LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_request: Waiting for server address to be resolved.\n")); return; } else if (err == ERR_OK) { sntp_servers[sntp_current_server].addr = sntp_server_address; } } else #endif /* SNTP_SERVER_DNS */ { sntp_server_address = sntp_servers[sntp_current_server].addr; err = (ip_addr_isany_val(sntp_server_address)) ? ERR_ARG : ERR_OK; } if (err == ERR_OK) { LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_request: current server address is %s\n", ipaddr_ntoa(&sntp_server_address))); sntp_send_request(&sntp_server_address); } else { /* address conversion failed, try another server */ LWIP_DEBUGF(SNTP_DEBUG_WARN_STATE, ("sntp_request: Invalid server address, trying next server.\n")); sys_timeout((u32_t)SNTP_RETRY_TIMEOUT, sntp_try_next_server, NULL); } }
static err_t mg_lwip_tcp_conn_cb(void *arg, struct tcp_pcb *tpcb, err_t err) { struct mg_connection *nc = (struct mg_connection *) arg; DBG(("%p connect to %s:%u = %d", nc, ipaddr_ntoa(&tpcb->remote_ip), tpcb->remote_port, err)); if (nc == NULL) { tcp_abort(tpcb); return ERR_ARG; } struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock; cs->err = err; if (err == 0) mg_lwip_set_keepalive_params(nc, 60, 10, 6); #ifdef SSL_KRYPTON if (err == 0 && nc->ssl != NULL) { SSL_set_fd(nc->ssl, (intptr_t) nc); mg_lwip_ssl_do_hs(nc); } else #endif { mg_lwip_post_signal(MG_SIG_CONNECT_RESULT, nc); } return ERR_OK; }
/****************************************************************************** * FunctionName : on_found * Description : dns found callback * Parameters : name -- pointer to the name that was looked up. * ipaddr -- pointer to an ip_addr_t containing the IP address of * the hostname, or NULL if the name could not be found (or on any * other error). * callback_arg -- a user-specified callback argument passed to * dns_gethostbyname * Returns : none *******************************************************************************/ static void ICACHE_FLASH_ATTR on_found(const char *name, ip_addr_t *ipaddr, void *arg) { struct user_dns_param *param = (struct user_dns_param *)arg; DEBUG_PRINTF("ipaddr=%p\n", ipaddr); DEBUG_PRINTF("param->ip=%p\n", ¶m->ip); if(!ipaddr) { DEBUG_INFO("ipaddr=NULL\n"); return; } DEBUG_NOTICE("%s\n", ipaddr_ntoa(ipaddr)); if(ipaddr->addr) { param->ip.addr = ipaddr->addr; os_timer_disarm(¶m->tmr); os_timer_arm(¶m->tmr, 1, 0); } }
int ICACHE_FLASH_ATTR user_dns_request(struct user_dns_param *param) { int err = EFAULT; if(!param) { DEBUG_WARNING("EINVAL\n"); return EINVAL; } DEBUG_INFO("%s\n", ipaddr_ntoa(¶m->ip)); err = dns_gethostbyname(param->hostname, ¶m->ip, on_found, param); DEBUG_INFO("dns_gethostbyname=%d | %s\n", err, lwip_strerr(err)); switch(err) { case ERR_INPROGRESS: DEBUG_NOTICE("wait for callback\n"); os_timer_setfn(¶m->tmr, (os_timer_func_t *)on_timer, param); os_timer_arm(¶m->tmr, 1000, 0); err = 0; break; case ERR_OK: DEBUG_NOTICE("is ipaddr\n"); os_timer_setfn(¶m->tmr, (os_timer_func_t *)on_timer, param); os_timer_arm(¶m->tmr, 1, 0); err = 0; break; default: DEBUG_WARNING("bad hostname\n"); break; } return err; }
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(;;) {}; }
int http_wifi_api_get_info(http_connection *c) { CGI_WIFI_DBG("http_wifi_api_get_info\n"); //wait for whole body if(c->state <HTTPD_STATE_BODY_END) { return HTTPD_CGI_MORE; } api_cgi_status * status = c->cgi.data; if(status==NULL) { //first call, send headers status = (api_cgi_status*)os_malloc(sizeof(api_cgi_status)); status->state=1; c->cgi.data=status; http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE); http_response_OK(c); return HTTPD_CGI_MORE; } else if(status->state==1) { //json data char mac[20]; wifi_station_get_config(&wifi_status.station_config); char *wifistatus = "unknown"; uint8_t c_status = wifi_station_get_connect_status(); if (c_status >= 0 && c_status < sizeof(connStatuses)) wifistatus = connStatuses[c_status]; int p = wifi_get_phy_mode(); char *phy = wifiPhy[p&3]; sint8 rssi = wifi_station_get_rssi(); if (rssi > 0) rssi = 0; uint8_t op = wifi_get_opmode() & 0x3; char *warn = wifiWarn[op]; uint8 mac_addr[6]; wifi_get_macaddr(0, mac_addr); uint8_t chan = wifi_get_channel(); char *hostname = wifi_station_get_hostname(); os_sprintf(mac,"%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); cJSON *root = cJSON_CreateObject(); cJSON_AddNumberToObject(root,"mode",wifi_get_opmode()); cJSON_AddStringToObject(root,"modechange", MODECHANGE); cJSON_AddStringToObject(root,"ssid", (const char *)wifi_status.station_config.ssid); cJSON_AddStringToObject(root,"status", (const char *)wifistatus); cJSON_AddStringToObject(root,"phy", (const char *)phy); cJSON_AddNumberToObject(root,"rssi", rssi); cJSON_AddStringToObject(root,"warn", (const char *)warn); cJSON_AddStringToObject(root,"mac", (const char *)mac); cJSON_AddNumberToObject(root,"chan", chan); cJSON_AddStringToObject(root,"hostname", (const char *)hostname); cJSON_AddStringToObject(root,"domain", (const char *)INTERFACE_DOMAIN); // got ip if(c_status==5) { struct ip_info ip; wifi_get_ip_info(0x0,&ip); char *ip_str = (char*)ipaddr_ntoa(&ip.ip); cJSON_AddStringToObject(root,"ip",ip_str); } else { cJSON_AddStringToObject(root,"ip",""); } http_write_json(c,root); cJSON_Delete(root); status->state=99; return HTTPD_CGI_MORE; } else { os_free(c->cgi.data); return HTTPD_CGI_DONE; } }
int main(int argc, char **argv) { struct netif netif; sigset_t mask, oldmask, empty; int ch; char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0}; /* startup defaults (may be overridden by one or more opts) */ IP4_ADDR(&gw, 192,168,0,1); IP4_ADDR(&ipaddr, 192,168,0,2); IP4_ADDR(&netmask, 255,255,255,0); trap_flag = 0; /* use debug flags defined by debug.h */ debug_flags = LWIP_DBG_OFF; while ((ch = getopt_long(argc, argv, "dhg:i:m:t:", longopts, NULL)) != -1) { switch (ch) { case 'd': debug_flags |= (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT); break; case 'h': usage(); exit(0); break; case 'g': ipaddr_aton(optarg, &gw); break; case 'i': ipaddr_aton(optarg, &ipaddr); break; case 'm': ipaddr_aton(optarg, &netmask); break; case 't': trap_flag = !0; /* @todo: remove this authentraps tweak when we have proper SET & non-volatile mem */ snmpauthentraps_set = 1; ipaddr_aton(optarg, &trap_addr); strncpy(ip_str, ipaddr_ntoa(&trap_addr),sizeof(ip_str)); printf("SNMP trap destination %s\n", ip_str); break; default: usage(); break; } } argc -= optind; argv += optind; strncpy(ip_str, ipaddr_ntoa(&ipaddr), sizeof(ip_str)); strncpy(nm_str, ipaddr_ntoa(&netmask), sizeof(nm_str)); strncpy(gw_str, ipaddr_ntoa(&gw), sizeof(gw_str)); printf("Host at %s mask %s gateway %s\n", ip_str, nm_str, gw_str); #ifdef PERF perf_init("/tmp/minimal.perf"); #endif /* PERF */ lwip_init(); printf("TCP/IP initialized.\n"); netif_add(&netif, &ipaddr, &netmask, &gw, NULL, mintapif_init, ethernet_input); netif_set_default(&netif); netif_set_up(&netif); #if SNMP_PRIVATE_MIB != 0 /* initialize our private example MIB */ lwip_privmib_init(); #endif snmp_trap_dst_ip_set(0,&trap_addr); snmp_trap_dst_enable(0,trap_flag); snmp_set_syscontact(syscontact_str,&syscontact_len); snmp_set_syslocation(syslocation_str,&syslocation_len); snmp_set_snmpenableauthentraps(&snmpauthentraps_set); snmp_init(); echo_init(); timer_init(); timer_set_interval(TIMER_EVT_ETHARPTMR, ARP_TMR_INTERVAL / 10); timer_set_interval(TIMER_EVT_TCPTMR, TCP_TMR_INTERVAL / 10); #if IP_REASSEMBLY timer_set_interval(TIMER_EVT_IPREASSTMR, IP_TMR_INTERVAL / 10); #endif printf("Applications started.\n"); while (1) { /* poll for input packet and ensure select() or read() arn't interrupted */ sigemptyset(&mask); sigaddset(&mask, SIGALRM); sigprocmask(SIG_BLOCK, &mask, &oldmask); /* start of critical section, poll netif, pass packet to lwIP */ if (mintapif_select(&netif) > 0) { /* work, immediatly end critical section hoping lwIP ended quickly ... */ sigprocmask(SIG_SETMASK, &oldmask, NULL); } else { /* no work, wait a little (10 msec) for SIGALRM */ sigemptyset(&empty); sigsuspend(&empty); /* ... end critical section */ sigprocmask(SIG_SETMASK, &oldmask, NULL); } if(timer_testclr_evt(TIMER_EVT_TCPTMR)) { tcp_tmr(); } #if IP_REASSEMBLY if(timer_testclr_evt(TIMER_EVT_IPREASSTMR)) { ip_reass_tmr(); } #endif if(timer_testclr_evt(TIMER_EVT_ETHARPTMR)) { etharp_tmr(); } } return 0; }
/*-----------------------------------------------------------------------------------*/ int main(int argc, char **argv) { int ch; char ip_str[16] = {0}; /* startup defaults (may be overridden by one or more opts) */ #if LWIP_IPV4 IP_ADDR4(&gw, 192,168, 0,1); IP_ADDR4(&netmask, 255,255,255,0); IP_ADDR4(&ipaddr, 192,168, 0,2); #if LWIP_HAVE_SLIPIF IP_ADDR4(&gw_slip, 192,168, 2, 1); IP_ADDR4(&netmask_slip, 255,255,255,255); IP_ADDR4(&ipaddr_slip, 192,168, 2, 2); #endif #endif /* LWIP_IPV4 */ ping_flag = 0; /* use debug flags defined by debug.h */ debug_flags = LWIP_DBG_OFF; while ((ch = getopt_long(argc, argv, "dhg:i:m:p:", longopts, NULL)) != -1) { switch (ch) { case 'd': debug_flags |= (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT); break; case 'h': usage(); exit(0); break; #if LWIP_IPV4 case 'g': ipaddr_aton(optarg, &gw); break; case 'i': ipaddr_aton(optarg, &ipaddr); break; case 'm': ipaddr_aton(optarg, &netmask); break; #endif /* LWIP_IPV4 */ case 'p': ping_flag = !0; ipaddr_aton(optarg, &ping_addr); strncpy(ip_str,ipaddr_ntoa(&ping_addr),sizeof(ip_str)); ip_str[sizeof(ip_str)-1] = 0; /* ensure \0 termination */ printf("Using %s to ping\n", ip_str); break; default: usage(); break; } } argc -= optind; argv += optind; #ifdef PERF perf_init("/tmp/simhost.perf"); #endif /* PERF */ printf("System initialized.\n"); sys_thread_new("main_thread", main_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); pause(); return 0; }
/** * @brief Displays the IP address on the Console * * @param ipAddr The desired IP address to display. */ static inline void IpAddrDisplay(const ip_addr_t* const ipAddr) { printk("\n\r\n\rIP Address Assigned: %s\r\n", ipaddr_ntoa(ipAddr)); }
int ICACHE_FLASH_ATTR NetworkConnect(Network* n, char* addr, int port) { struct sockaddr_in sAddr; int retVal = -1; bzero(&sAddr, sizeof(struct sockaddr_in)); if (!getIpForHost(addr, &sAddr)) { printf("get ip by hostname fail\n"); return -1; } sAddr.sin_port = FreeRTOS_htons(port); if ((n->my_socket = FreeRTOS_socket(PF_INET, FREERTOS_SOCK_STREAM, 0)) < 0) goto exit; // printf("Connecting to server %s...\n", ipaddr_ntoa((const ip_addr_t*)&sAddr.sin_addr.s_addr)); if ((retVal = FreeRTOS_connect(n->my_socket, (struct sockaddr * )(&sAddr), sizeof(struct sockaddr))) < 0) { FreeRTOS_closesocket(n->my_socket); goto exit; } exit: // printf("------------>connect init: %d, sock:%d \n", retVal, n->my_socket); return retVal; #if 0 struct freertos_sockaddr sAddr; int retVal = -1; uint32_t ipAddress; struct sockaddr_in *name_in; struct ip_addr *ad; struct hostent *pHost; if ((pHost = FreeRTOS_gethostbyname(addr)) == 0) goto exit; ad = (struct ip_addr *)&pHost->h_addr_list[0]; ipAddress = ad->addr; sAddr.sa_family = AF_INET; name_in = (struct sockaddr_in *)(void*)(&sAddr); name_in->sin_port = FreeRTOS_htons(port); name_in->sin_addr.s_addr = ipAddress; printf("----->get ip: %08x, %08x, %08x\n", ipAddress, port, FreeRTOS_htons(port)); if ((n->my_socket = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP)) < 0) goto exit; printf("++++++++++>get ip: %s\n", addr); if ((retVal = FreeRTOS_connect(n->my_socket, &sAddr, sizeof(sAddr))) < 0) { FreeRTOS_closesocket(n->my_socket); goto exit; } exit: printf("------------>connect init fail \n"); return retVal; #endif #if 0 int type = SOCK_STREAM; struct sockaddr_in address; int retVal = -1; sa_family_t family = AF_INET; struct addrinfo *result = NULL; struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL}; if ((retVal = getaddrinfo(addr, NULL, &hints, &result)) == 0) { printf("------------------->1\n"); struct addrinfo* res = result; /* prefer ip4 addresses */ while (res) { if (res->ai_family == AF_INET) { result = res; break; } res = res->ai_next; } if (result->ai_family == AF_INET) { address.sin_port = htons(port); address.sin_family = family = AF_INET; address.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr; printf("------------------->2, %08x, %08x\n", address.sin_port, address.sin_addr); } else retVal = -1; freeaddrinfo(result); } if (retVal == 0) { printf("------------------->3\n"); n->my_socket = socket(family, type, 0); if (n->my_socket != -1) { printf("Connecting to server %s...\n", ipaddr_ntoa((const ip_addr_t*)&address.sin_addr.s_addr)); printf("------------------->4, socket:%d\n", n->my_socket); while (1) { retVal = FreeRTOS_connect(n->my_socket, (struct sockaddr*)&address, sizeof(struct sockaddr)); if (retVal == 0) break; } printf("------------------->5, retrun:%d\n", retVal); } } exit: return retVal; #endif }
int http_wifi_api_connect_ap(http_connection *c) { CGI_WIFI_DBG("http_wifi_api_connect_ap\n"); //wait for whole body if(c->state <HTTPD_STATE_BODY_END) { return HTTPD_CGI_MORE; } api_cgi_connect_status * status = c->cgi.data; if(status==NULL) { // CGI_WIFI_DBG("http_wifi_api_connect_ap status NULL\n"); status = (api_cgi_connect_status*)os_malloc(sizeof(api_cgi_connect_status)); status->state=1; c->cgi.data=status; //parse json and validate cJSON * root = cJSON_Parse(c->body.data); if(root==NULL) goto badrequest; cJSON * ssid = cJSON_GetObjectItem(root,"ssid"); if(ssid==NULL) goto badrequest; else if(ssid->type != cJSON_String) goto badrequest; cJSON * pwd = cJSON_GetObjectItem(root,"pwd"); if(pwd==NULL) goto badrequest; else if(pwd->type!=cJSON_String) goto badrequest; //parse ok strncpy(status->ssid,ssid->valuestring,32); strncpy(status->pwd,pwd->valuestring,64); //set timer to connect os_timer_disarm(&status->timer); os_timer_setfn(&status->timer, (os_timer_func_t *)http_execute_cgi, c); os_timer_arm(&status->timer, 10, 0); return HTTPD_CGI_MORE; } else if (status->state==1) { CGI_WIFI_DBG("connect_ap status %d\n",status->state); //try connect if(strlen(status->ssid)>32 || strlen(status->pwd)>64) goto badrequest; CGI_WIFI_DBG("ap connect ssid: %s, pwd: %s\n",status->ssid,status->pwd); strcpy(wifi_status.station_config.ssid,status->ssid); strcpy(wifi_status.station_config.password,status->pwd); wifi_status.station_config.bssid_set=0; wifi_station_disconnect(); wifi_station_set_config(&wifi_status.station_config); wifi_station_connect(); //set timer to check status os_timer_disarm(&status->timer); os_timer_setfn(&status->timer, (os_timer_func_t *)http_execute_cgi, c); os_timer_arm(&status->timer, 500, 0); status->state=2; return HTTPD_CGI_MORE; } else if (status->state==2) { CGI_WIFI_DBG("connect_ap status %d\n",status->state); uint8_t c_status = wifi_station_get_connect_status(); CGI_WIFI_DBG("wifi sta status %d\n",c_status); if (c_status>=2 && c_status <= 4 ) { wifi_station_disconnect(); strcpy(wifi_status.station_config.ssid,""); strcpy(wifi_status.station_config.password,""); wifi_station_set_config(&wifi_status.station_config); } if (c_status==1) { //set timer to check status os_timer_disarm(&status->timer); os_timer_setfn(&status->timer, (os_timer_func_t *)http_execute_cgi, c); os_timer_arm(&status->timer, 500, 0); return HTTPD_CGI_MORE; } else { //write headers http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE); http_response_OK(c); //create json cJSON *root = cJSON_CreateObject(); cJSON_AddNumberToObject(root,"status",c_status); //got ip if(c_status==5) { struct ip_info ip; wifi_get_ip_info(0x0,&ip); char *ip_str = (char*)ipaddr_ntoa(&ip.ip); cJSON_AddStringToObject(root,"ip",ip_str); } else { cJSON_AddStringToObject(root,"ip",""); } http_write_json(c,root); //delete json struct cJSON_Delete(root); status->state=99; return HTTPD_CGI_MORE; } } else //status=99 { CGI_WIFI_DBG("connect_ap status %d\n",status->state); //clean os_free(c->cgi.data); return HTTPD_CGI_DONE; } badrequest: http_response_BAD_REQUEST(c); status->state=99; return HTTPD_CGI_MORE; //shut up compiler return HTTPD_CGI_DONE; }