ipv4_addr ngethostbyname(unsigned char *host) { int tries = 20; ipv4_addr result; //ipv4_addr next_servers[MAX_DNS_SERVERS]; ipv4_addr * sptr = servers; int sleft = MAX_DNS_SERVERS; while(tries--) { ipv4_addr server = *sptr++; if(sleft-- <= 0 || server == 0) { SHOW_ERROR0( 1, "No more places to look in, give up"); return 0; } SHOW_FLOW( 2, "look in %s", inet_ntoa(* (struct in_addr*)&server) ); errno_t res = dns_request(host, server, &result ); if( res == 0 || result != 0 ) { SHOW_FLOW( 2, "answer is %s", inet_ntoa(* (struct in_addr*)&result) ); return result; } } return 0; }
static void connect_to_cloud(void) { int opt = 0; struct sockaddr_t addr; if(cloud_ip_addr == 0 && dns_pending == 0){ //DNS function cloud_ip_addr = dns_request((char *)device_info->conf.server_domain); if(cloud_ip_addr == -1){ cloud_ip_addr = 0; //dns failed, retry again goto cloud_error; } else if (cloud_ip_addr == 0){ //DNS pending, waiting for callback dns_pending = 1; return; } } if( device_info->cloud_fd == -1 && (u32)cloud_ip_addr>0){ device_info->cloud_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); setsockopt(device_info->cloud_fd,0,SO_BLOCKMODE,&opt,4); addr.s_ip = cloud_ip_addr; addr.s_port = device_info->conf.server_port; if (connect(device_info->cloud_fd, &addr, sizeof(addr))!=0) goto cloud_error; } return; cloud_error: close_cloud(CLOUD_RETRY); return; }
void friend_add(char_t *name, STRING_IDX length, char_t *msg, STRING_IDX msg_length) { if(!length) { addfriend_status = ADDF_NONAME; return; } uint8_t id[TOX_FRIEND_ADDRESS_SIZE]; if(length == TOX_FRIEND_ADDRESS_SIZE * 2 && string_to_id(id, name)) { friend_addid(id, msg, msg_length); } else { /* not a regular id, try DNS discovery */ addfriend_status = ADDF_DISCOVER; dns_request(name, length); } }
void friend_add(char_t *name, STRING_IDX length, char_t *msg, STRING_IDX msg_length) { if(!length) { addfriend_status = ADDF_NONAME; return; } #ifdef EMOJI_IDS uint8_t emo_id[TOX_FRIEND_ADDRESS_SIZE]; if (emoji_string_to_bytes(emo_id, TOX_FRIEND_ADDRESS_SIZE, name, length) == TOX_FRIEND_ADDRESS_SIZE) { friend_addid(emo_id, msg, msg_length); } #endif uint8_t name_cleaned[length]; uint16_t length_cleaned = 0; unsigned int i; for (i = 0; i < length; ++i) { if (name[i] != ' ') { name_cleaned[length_cleaned] = name[i]; ++length_cleaned; } } if(!length_cleaned) { addfriend_status = ADDF_NONAME; return; } uint8_t id[TOX_FRIEND_ADDRESS_SIZE]; if(length_cleaned == TOX_FRIEND_ADDRESS_SIZE * 2 && string_to_id(id, name_cleaned)) { friend_addid(id, msg, msg_length); } else { /* not a regular id, try DNS discovery */ addfriend_status = ADDF_DISCOVER; dns_request(name_cleaned, length_cleaned); } }
int epoll_process_events(int timer) { int events; int err; int flag; int i; events = epoll_wait(ep, event_list, (int) nevents, timer); err = (events == -1) ? errno : 0; if (err) { if (err != EINTR) { printf("epoll_wait() failed:%d\n", errno); return -1; } return 0; } if (events != 0) { for (i = 0; i < events; i++) { flag = ((struct my_epoll_data *)event_list[i].data.ptr)->flag; if (flag == SERVER_SOCKET_TCP) { dns_accept_request((struct my_epoll_data *)event_list[i].data.ptr); } else if (flag == SERVER_SOCKET_UDP) { dns_request((struct my_epoll_data *)event_list[i].data.ptr); } else if (flag == DNS_SERVER_SOCKET) { dns_response((struct my_epoll_data *)event_list[i].data.ptr); } } } return 0; }
static void rdns_handler(ngx_resolver_ctx_t * rctx) { ngx_http_request_t * r = rctx->data; ngx_http_rdns_ctx_t * ctx = ngx_http_get_module_ctx(r, ngx_http_rdns_module); ngx_http_rdns_loc_conf_t * loc_cf; ngx_http_rdns_common_conf_t * cconf; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "rdns: reverse dns request handler"); if (ctx == NULL) { ngx_log_debug0(NGX_LOG_ERR, r->connection->log, 0, "rdns: reverse dns request handler: failed to get request context"); ngx_resolve_addr_done(rctx); ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); return; } loc_cf = ngx_http_get_module_loc_conf(r, ngx_http_rdns_module); if (loc_cf == NULL) { ngx_log_debug0(NGX_LOG_ERR, r->connection->log, 0, "rdns: reverse dns request handler: failed to get rdns location config"); ngx_resolve_addr_done(rctx); ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); return; } if (rctx->state) { ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "rdns: reverse dns request handler: failed with error '%s'", ngx_resolver_strerror(rctx->state)); ngx_resolve_addr_done(rctx); var_set(r, loc_cf->rdns_result_index, var_rdns_result_not_found); resolver_handler_finalize(r, ctx); } else { ngx_str_t hostname; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "rdns: reverse dns request handler: result='%V'", &rctx->name); hostname.data = ngx_pcalloc(r->pool, rctx->name.len * sizeof(u_char)); ngx_memcpy(hostname.data, rctx->name.data, rctx->name.len); hostname.len = rctx->name.len; ngx_resolve_addr_done(rctx); cconf = rdns_get_common_conf(ctx, loc_cf); if (cconf == NULL) { ngx_log_debug0(NGX_LOG_ERR, r->connection->log, 0, "rdns: reverse dns request handler: failed to get common config"); ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); return; } if (cconf->double_mode) { ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "rdns: reverse dns request handler: double mode"); dns_request(r, hostname); } else { ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "rdns: reverse dns request handler: resolved to '%V'", &hostname); var_set(r, loc_cf->rdns_result_index, hostname); ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "(DONE) rdns: reverse dns request handler"); resolver_handler_finalize(r, ctx); } } }
int main(void) { int i, j, fd_listen = -1, fd_udp = -1, fd_client = -1; char *buf, ip_address[16],ipstr[32]; int len; int con = -1; int opt = 0; int clientfd[8]; fd_set readfds, exceptfds; struct timeval_t t; struct sockaddr_t addr; socklen_t addrLen; struct timeval_t timeout; #ifdef DynamicMemAlloc int bufferSize; libConfig.tcp_buf_dynamic = mxEnable; libConfig.tcp_max_connection_num = 12; libConfig.tcp_rx_size = 2048; libConfig.tcp_tx_size = 2048; libConfig.hw_watchdog = 0; libConfig.wifi_channel = WIFI_CHANNEL_1_13; lib_config(&libConfig); #endif for(i=0;i<8;i++) clientfd[i] = -1; buf = (char*)malloc(3*1024); mxchipInit(); UART_Init(); printf("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version()); #ifdef LowPowerMode ps_enable(); #endif memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st)); wNetConfig.wifi_mode = Station; strcpy((char*)wNetConfig.wifi_ssid, AP_NAME); strcpy((char*)wNetConfig.wifi_key, AP_PASSWORD); wNetConfig.dhcpMode = DHCP_Client; StartNetwork(&wNetConfig); printf("Connect to %s.....\r\n", wNetConfig.wifi_ssid); t.tv_sec = 0; t.tv_usec = 100; set_tcp_keepalive(3, 60); while(1) { mxchipTick(); /*If wifi is established, connect to www.baidu.com, and send a http request*/ #ifdef BlockMode if(wifi_up&&webserverTest){ webserverTest = 0; if(gethostbyname(WEB_SERVER, (u8 *)ipstr, 32)!=0){ printf("DNS test: %s failed \r\n", WEB_SERVER); webserverTest = 1; continue; } printf("DNS test: %s address is %s \r\n", WEB_SERVER, ipstr); fd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); addr.s_ip = inet_addr(ipstr); addr.s_port = 80; timeout.tv_sec = 3; timeout.tv_usec = 0; setsockopt(fd_client,0,SO_CONTIMEO,&timeout,sizeof(struct timeval_t)); if (connect(fd_client, &addr, sizeof(addr))!=0) { close(fd_client); fd_client = -1; printf("Connect to web server failed! \r\n"); webserverTest = 1; } else{ printf("Connect to web server success! Reading web pages...\r\n"); send(fd_client, httpRequest, sizeof(httpRequest), 0); } } #else if(cloud_ip_addr == -1 && dns_pending == 0 && wifi_up){ //DNS function cloud_ip_addr = dns_request(WEB_SERVER); if(cloud_ip_addr == -1) printf("DNS test: %s failed. \r\n", WEB_SERVER); else if (cloud_ip_addr == 0) //DNS pending, waiting for callback dns_pending = 1; } if( fd_client == -1 && cloud_ip_addr!=-1 && !dns_pending){ fd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); setsockopt(fd_client,0,SO_BLOCKMODE,&opt,4); addr.s_ip = cloud_ip_addr; addr.s_port = 80; printf("Connecting to %s..., at port %d\r\n", inet_ntoa(ipstr, cloud_ip_addr), addr.s_port); if (connect(fd_client, &addr, sizeof(addr))!=0) { printf("Connect to %s failed.\r\n", WEB_SERVER); printf("Free: %d, max length: %d.\r\n", total_free, max_len); } } if(cloud_connected == 1){ send(fd_client, httpRequest, sizeof(httpRequest), 0); cloud_connected = 0; } #endif /*Establish a TCP server that accept the tcp clients connections*/ if (fd_listen==-1) { fd_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); #ifdef DynamicMemAlloc bufferSize = 5*1024; setsockopt(fd_listen,0,SO_RDBUFLEN,&bufferSize,4); bufferSize = 5*1024; setsockopt(fd_listen,0,SO_WRBUFLEN,&bufferSize,4); #endif addr.s_port = 8080; bind(fd_listen, &addr, sizeof(addr)); listen(fd_listen, 0); printf("TCP server established at port: %d \r\n", addr.s_port); } /*Establish a UDP port to receive any data sent to this port*/ if (fd_udp==-1) { fd_udp = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP); addr.s_port = 8090; bind(fd_udp, &addr, sizeof(addr)); printf("Open UDP port %d\r\n", addr.s_port); } /*Check status on erery sockets */ FD_ZERO(&readfds); FD_SET(fd_listen, &readfds); FD_SET(fd_udp, &readfds); if(fd_client!=-1) FD_SET(fd_client, &readfds); for(i=0;i<8;i++) { if (clientfd[i] != -1) FD_SET(clientfd[i], &readfds); } select(1, &readfds, NULL, &exceptfds, &t); /*Check tcp connection requests */ if(FD_ISSET(fd_listen, &readfds)) { j = accept(fd_listen, &addr, &len); if (j > 0) { inet_ntoa(ip_address, addr.s_ip ); printf("Client %s:%d connected\r\n", ip_address, addr.s_port); for(i=0;i<8;i++) { if (clientfd[i] == -1) { clientfd[i] = j; break; } } } } /*Read html data from www.baidu.com */ if(fd_client != -1){ if(FD_ISSET(fd_client, &readfds)) { con = recv(fd_client, buf, 2*1024, 0); if(con > 0) printf("Get %s data successful! data length: %d bytes\r\n", WEB_SERVER, con); else{ close(fd_client); fd_client = -1; cloud_ip_addr = -1; webserverTest = 1; printf("Web connection closed.\r\n"); } } } /*Read data from tcp clients and send data back */ for(i=0;i<8;i++) { if (clientfd[i] != -1) { if (FD_ISSET(clientfd[i], &readfds)) { con = recv(clientfd[i], buf, 1*1024, 0); if (con > 0) send(clientfd[i], buf, con, 0); else { close(clientfd[i]); clientfd[i] = -1; } } else if (FD_ISSET(clientfd[i], &exceptfds)) clientfd[i] = -1; } } /*Read data from udp and send data back */ if (FD_ISSET(fd_udp, &readfds)) { con = recvfrom(fd_udp, buf, 3*1024, 0, &addr, &addrLen); sendto(fd_udp, buf, con, 0, &addr, sizeof(struct sockaddr_t)); } } }
errno_t name2ip( in_addr_t *out, const char *name, int flags ) { int ia, ib, ic, id; if( 4 == sscanf( name, "%d.%d.%d.%d", &ia, &ib, &ic, &id ) ) { // No resolver required, ip4 addr given ipv4_addr iaddr = IPV4_DOTADDR_TO_ADDR( ia, ib, ic, id); *out = htonl( iaddr ); SHOW_FLOW( 2, "parsed %s to %s", name, inet_ntoa(* (struct in_addr*)out) ); return 0; } if(!inited) return ENXIO; int tries = 20; if(flags & RESOLVER_FLAG_NORETRY) tries = 1; ipv4_addr result; //ipv4_addr next_servers[MAX_DNS_SERVERS]; SHOW_FLOW( 1, "request '%s'", name ); ipv4_addr * sptr = servers; int sleft = MAX_DNS_SERVERS; if( !(flags & RESOLVER_FLAG_NORCACHE) ) if( lookup_cache( out, name ) == 0 ) { SHOW_FLOW0( 1, "got from cache"); return 0; } // On OS stop don't produce network traffic if( (flags & RESOLVER_FLAG_NOWAIT) || phantom_stop_level ) return ESRCH; while(tries--) { ipv4_addr server = *sptr++; if(sleft-- <= 0 || server == 0) { SHOW_ERROR0( 1, "No more places to look in, give up\n"); return ENOENT; } SHOW_FLOW( 2, "look in %s", inet_ntoa(* (struct in_addr*)&server) ); errno_t res = dns_request( (const unsigned char *)name, server, &result ); if( res == 0 )//|| result != 0 ) { SHOW_FLOW( 1, "answer is %s", inet_ntoa(* (struct in_addr*)&result) ); *out = result; if( !(flags & RESOLVER_FLAG_NOWCACHE) ) store_to_cache( result, name ); return 0; } } return ENOENT; }
/** * \ingroup tcpcmdcommon * \b DNS-Query Befehl * * \b Syntax: DNSQ \<hostname\> */ int16_t cmd_DNSQ(char *outbuffer) { dns_request(argv,&dnsQueryIP); return 0; }
int main(void) { int fd_client = -1; char *buf; int con = -1; int opt = 0; fd_set readfds, exceptfds; struct timeval_t t; struct sockaddr_t addr; lib_config_t libConfig; volatile int setval; volatile int readval; buf = (char*)malloc(3*1024); libConfig.tcp_buf_dynamic = mxEnable; libConfig.tcp_max_connection_num = 12; libConfig.tcp_rx_size = 2048; libConfig.tcp_tx_size = 2048; libConfig.hw_watchdog = 0; libConfig.wifi_channel = WIFI_CHANNEL_1_13; lib_config(&libConfig); mxchipInit(); UART_Init(); printf("\r\n%s\r\n mxchipWNet library version: %s\r\n", APP_INFO, system_lib_version()); printf(menu); printf ("\nMXCHIP> "); //init http set_tcp_keepalive(3, 60); setSslMaxlen(6*1024); t.tv_sec = 0; t.tv_usec = 100; // void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); while(1) { mxchipTick(); if(menu_enable) Main_Menu(); if(configSuccess) { wNetConfig.wifi_mode = Station; wNetConfig.dhcpMode = DHCP_Client; StartNetwork(&wNetConfig); printf("connect to %s.....\r\n", wNetConfig.wifi_ssid); configSuccess = 0; menu_enable = 1; sysTick_configuration(); config_gpio_pc6(); config_gpio_pb8(); while(1) { readval= GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_6); mxchipTick(); //If wifi is established, connect to WEBSERVER, and send a http request if(https_server_addr == 0 && dns_pending == 0) { //DNS function https_server_addr = dns_request(WEB_SERVER); if(https_server_addr == -1) printf("DNS test: %s failed. \r\n", WEB_SERVER); else if (https_server_addr == 0) //DNS pending, waiting for callback { dns_pending = 1; printf("2DNS test: %s success. \r\n", WEB_SERVER); printf("21http server addr=%x\n",https_server_addr); } } if( fd_client == -1 && (u32)https_server_addr>0) { fd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); setsockopt(fd_client,0,SO_BLOCKMODE,&opt,4); addr.s_ip = https_server_addr; addr.s_port = 443; if (connect(fd_client, &addr, sizeof(addr))!=0) printf("Connect to %s failed.\r\n", WEB_SERVER); else printf("Connect to %s success.\r\n", WEB_SERVER); } if(serverConnectted&&sslConnectted==0) { printf("Connect to web server success! Setup SSL ecryption...\r\n"); if (setSSLmode(1, fd_client)!= MXCHIP_SUCCESS) { printf("SSL connect fail\r\n"); close(fd_client); fd_client = -1; serverConnectted = 0; } else { printf("SSL connect\r\n"); sslConnectted = 1; } } if(readval==1&&sslConnectted) { printf("read value is 1\n"); send(fd_client, sendhttpRequest, strlen(sendhttpRequest), 0); // return 0; } if(check_responce&&sslConnectted) { printf("readval=%d\n",readval); check_responce=0; send(fd_client, responcehttpRequest, strlen(responcehttpRequest), 0); //Check status on erery sockets FD_ZERO(&readfds); if(sslConnectted) FD_SET(fd_client, &readfds); select(1, &readfds, NULL, &exceptfds, &t); //Read html data from WEBSERVER if(sslConnectted) { if(FD_ISSET(fd_client, &readfds)) { con = recv(fd_client, buf, 2*1024, 0); if(con > 0) { printf("Get %s data successful! data length: %d bytes data\r\n", WEB_SERVER, con); setval=r_http(buf); printf("read from http is %d\n",setval); if(setval==1) GPIO_SetBits(GPIOB,GPIO_Pin_8); else GPIO_ResetBits(GPIOB,GPIO_Pin_8); } else { close(fd_client); serverConnectted = 0; sslConnectted = 0; fd_client = -1; printf("Web connection closed.\r\n"); } } } } } } if(easylink) { OpenEasylink2(60); easylink = 0; } } }