static void fapp_stat_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv ) { struct fnet_netif_statistics statistics; fnet_netif_desc_t netif = fnet_netif_get_default(); FNET_COMP_UNUSED_ARG(argc); FNET_COMP_UNUSED_ARG(argv); /* Print Packet statistics. */ if(fnet_netif_get_statistics(netif, &statistics) == FNET_OK) { fnet_shell_println(desc, "\nPackets:"); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "TX Packets", statistics.tx_packet); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "RX Packets", statistics.rx_packet); } #if FNET_CFG_IP6 { fnet_index_t i; fnet_netif_ip6_prefix_t ip6_prefix; fnet_netif_ip6_neighbor_cache_t ip6_neighbor_cache; fnet_char_t numaddr[FNET_IP6_ADDR_STR_SIZE]; fnet_char_t mac_str[FNET_MAC_ADDR_STR_SIZE]; /* Print content of IPv6 Prefix List. */ fnet_shell_println(desc, "\nIPv6 Prefix List:"); for(i=0U; fnet_netif_get_ip6_prefix(netif, i, &ip6_prefix) == FNET_TRUE; i++) { fnet_shell_println(desc," [%d] %s/%d\n", i, fnet_inet_ntop(AF_INET6, &ip6_prefix.prefix, numaddr, sizeof(numaddr)), ip6_prefix.prefix_length); } /* Print content of IPv6 Neighbor Cache. */ for(i=0U; fnet_netif_get_ip6_neighbor_cache(netif, i, &ip6_neighbor_cache) == FNET_TRUE; i++) { if(i == 0U) { fnet_shell_println(desc, "\nIPv6 Neighbor Cache:"); } fnet_shell_println(desc," [%d] %s = %s (%s)\n", i, fnet_inet_ntop(AF_INET6, &ip6_neighbor_cache.ip_addr, numaddr, sizeof(numaddr)), fnet_mac_to_str(ip6_neighbor_cache.ll_addr, mac_str), (ip6_neighbor_cache.is_router == FNET_TRUE) ? "router" : "host"); } } #endif }
void fapp_debug_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv ) { FNET_COMP_UNUSED_ARG(desc); FNET_COMP_UNUSED_ARG(argc); FNET_COMP_UNUSED_ARG(argv); /* Put here you debugging information.*/ #if 0 fnet_fec_debug_mii_print_regs(fnet_netif_get_default()); #endif #if 0 { fnet_index_t i = 0; fnet_ip6_addr_t addr_dns; fnet_uint8_t ip_str[FNET_IP_ADDR_STR_SIZE]={0}; while(fnet_netif_get_ip6_dns(fnet_netif_get_default(), i, &addr_dns ) == FNET_TRUE) { fnet_inet_ntop(AF_INET6, (fnet_uint8_t*)(&addr_dns), ip_str, sizeof(ip_str)); fnet_shell_println(desc, "DNS[%d]=%s", i, ip_str); i++; } } #endif #if 0 /* Test ctacrypt library.*/ extern void ctaocrypt_test(void); ctaocrypt_test(); #endif }
/************************************************************************ * NAME: fapp_netif_addr_print * * DESCRIPTION: Print Interface IP addresses. ************************************************************************/ void fapp_netif_addr_print(fnet_shell_desc_t desc, fnet_address_family_t family, fnet_netif_desc_t netif, fnet_bool_t print_type) { fnet_char_t ip_str[FNET_IP_ADDR_STR_SIZE]={0}; #if FNET_CFG_IP4 if((family & AF_INET)==AF_INET) { fnet_ip4_addr_t local_ip; local_ip = fnet_netif_get_ip4_addr(netif); fnet_shell_printf(desc, FAPP_SHELL_INFO_FORMAT_S, "IPv4 Address", fnet_inet_ntoa(*(struct in_addr *)(&local_ip), ip_str) ); if(print_type) { fnet_shell_println(desc," <%s>", fnet_netif_get_ip4_addr_automatic(netif) ? "automatic" : "manual"); } else { fnet_shell_println(desc, ""); } } #endif /* FNET_CFG_IP4 */ #if FNET_CFG_IP6 if((family & AF_INET6)==AF_INET6) { fnet_bool_t result; fnet_index_t n; fnet_netif_ip6_addr_info_t addr_info; /* Print all assigned IPv6 addreses.*/ n=0u; for(;;) { result = fnet_netif_get_ip6_addr (netif, n, &addr_info); if(result == FNET_TRUE) { fnet_inet_ntop(AF_INET6, (fnet_uint8_t*)(&addr_info.address), ip_str, sizeof(ip_str)); fnet_shell_printf(desc, FAPP_SHELL_INFO_FORMAT_S, "IPv6 Address", ip_str); if(print_type) { fnet_shell_println(desc," <%s> ScopeID:%d", (addr_info.type == FNET_NETIF_IP6_ADDR_TYPE_AUTOCONFIGURABLE) ? "autoconfigurable" : "manual", fnet_netif_get_scope_id(netif)); } else { fnet_shell_println(desc,""); } } else { break; } n++; } } #endif /* FNET_CFG_IP6 */ }
/************************************************************************ * NAME: fapp_ping_handler * * DESCRIPTION: ************************************************************************/ static void fapp_ping_handler (fnet_error_t result, fnet_size_t packet_count, struct sockaddr *target_addr, fnet_uint32_t cookie) { fnet_char_t ip_str[FNET_IP_ADDR_STR_SIZE]; fnet_shell_desc_t desc = (fnet_shell_desc_t)cookie; if(result == FNET_ERR_OK) { fnet_shell_println(desc, FAPP_PING_STR_REPLY, fnet_inet_ntop(target_addr->sa_family, target_addr->sa_data, ip_str, sizeof(ip_str))); } else if(result == FNET_ERR_TIMEDOUT) { fnet_shell_println(desc, FAPP_PING_STR_TIMEOUT); } else { fnet_shell_println(desc, "Error = %d", result); } if(packet_count == 0U) { fnet_shell_unblock(desc); /* Unblock the shell. */ } }
/************************************************************************ * NAME: fapp_bench_udp_tx * * DESCRIPTION: Start TX TCP Benchmark. ************************************************************************/ static void fapp_bench_udp_tx (struct fapp_bench_tx_params *params) { int send_result; char ip_str[FNET_IP_ADDR_STR_SIZE]; int i; int received; const struct linger linger_option ={1, /*l_onoff*/ 4 /*l_linger*/}; const unsigned long bufsize_option = FAPP_BENCH_SOCKET_BUF_SIZE; struct sockaddr foreign_addr; int exit_flag = 0; int sock_err; unsigned int option_len; fnet_shell_desc_t desc = params->desc; int packet_size = params->packet_size; int cur_packet_number; int iterations = params->iteration_number; fnet_address_family_t family = params->foreign_addr.sa_family; if(packet_size > FAPP_BENCH_BUFFER_SIZE) /* Check max size.*/ packet_size = FAPP_BENCH_BUFFER_SIZE; fapp_bench.socket_listen = SOCKET_INVALID; /* ------ Start test.----------- */ fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, " UDP TX Test" ); fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "Remote IP addr", fnet_inet_ntop(family, params->foreign_addr.sa_data, ip_str, sizeof(ip_str))); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Remote Port", fnet_ntohs(params->foreign_addr.sa_port)); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Message Size", params->packet_size); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Num. of messages", params->packet_number); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Num. of iterations", params->iteration_number); fnet_shell_println(desc, FAPP_TOCANCEL_STR); fnet_shell_println(desc, FAPP_DELIMITER_STR); while(iterations--) { /* Create socket */ if((fapp_bench.socket_foreign = socket(family, SOCK_DGRAM, 0)) == SOCKET_INVALID) { FNET_DEBUG("BENCH: Socket creation error.\n"); iterations = 0; goto ERROR_1; } /* Set Socket options. */ if( /* Setup linger option. */ (setsockopt (fapp_bench.socket_foreign, SOL_SOCKET, SO_LINGER, (char *)&linger_option, sizeof(linger_option)) == SOCKET_ERROR) || /* Set socket buffer size. */ (setsockopt(fapp_bench.socket_foreign, SOL_SOCKET, SO_RCVBUF, (char *) &bufsize_option, sizeof(bufsize_option))== SOCKET_ERROR) || (setsockopt(fapp_bench.socket_foreign, SOL_SOCKET, SO_SNDBUF, (char *) &bufsize_option, sizeof(bufsize_option))== SOCKET_ERROR) ) { FNET_DEBUG("BENCH: Socket setsockopt error.\n"); iterations = 0; goto ERROR_2; } /* Bind to the server.*/ fnet_shell_println(desc,"Connecting."); foreign_addr = params->foreign_addr; if(connect(fapp_bench.socket_foreign, &foreign_addr, sizeof(foreign_addr))== FNET_ERR) { fnet_shell_println(desc, "Connection failed."); iterations = 0; goto ERROR_2; } /* Sending.*/ fnet_shell_println(desc,"Sending."); fapp_bench.bytes = 0; fapp_bench.remote_bytes = 0; cur_packet_number = 0; fapp_bench.first_time = fnet_timer_ticks(); while(1) { send_result = send( fapp_bench.socket_foreign, (char*)(&fapp_bench.buffer[0]), packet_size, 0); fapp_bench.last_time = fnet_timer_ticks(); if ( send_result == SOCKET_ERROR ) { option_len = sizeof(sock_err); getsockopt(fapp_bench.socket_foreign, SOL_SOCKET, SO_ERROR, (char*)&sock_err, &option_len); fnet_shell_println(desc, "socket_error = %d", sock_err); iterations = 0; goto ERROR_2; } else { fapp_bench.bytes += send_result; cur_packet_number ++; exit_flag = fnet_shell_ctrlc (desc); /* Check [Ctrl+c]*/ if((cur_packet_number >= params->packet_number)|| exit_flag) { if(exit_flag) { fnet_shell_println(desc, FAPP_SHELL_CANCELED_CTRL_C); iterations = 0; } break;/* => TX END. */ } } } /* Send End mark.*/ for(i=0; i < FAPP_BENCH_TX_UDP_END_ITERATIONS; i++) { unsigned long ack_bytes; /* Send END mark.*/ send( fapp_bench.socket_foreign, (char*)(&fapp_bench.buffer[0]), 1, 0); fnet_timer_delay(1); /* Check ACK. It should contain recieved amount of data.*/ received = recv(fapp_bench.socket_foreign, (char*)(&ack_bytes), sizeof(ack_bytes), 0); if(received == sizeof(ack_bytes)) /* ACK received.*/ { fapp_bench.remote_bytes = fnet_ntohl(ack_bytes); break; } else if(received == SOCKET_ERROR) { break; } else {} } /* Print benchmark results.*/ fapp_bench_print_results (desc); ERROR_2: closesocket(fapp_bench.socket_foreign); } ERROR_1: fnet_shell_println(desc, FAPP_BENCH_COMPLETED_STR); }
/************************************************************************ * NAME: fapp_bench_tcp_tx * * DESCRIPTION: Start TX TCP Benchmark. ************************************************************************/ static void fapp_bench_tcp_tx (struct fapp_bench_tx_params *params) { int send_result; char ip_str[FNET_IP_ADDR_STR_SIZE]; const struct linger linger_option ={1, /*l_onoff*/ 4 /*l_linger*/}; const unsigned long bufsize_option = FAPP_BENCH_SOCKET_BUF_SIZE; const int keepalive_option = 1; const int keepcnt_option = FAPP_BENCH_TCP_KEEPCNT; const int keepintvl_option = FAPP_BENCH_TCP_KEEPINTVL; const int keepidle_option = FAPP_BENCH_TCP_KEEPIDLE; struct sockaddr foreign_addr; int exit_flag = 0; int sock_err ; unsigned int option_len; fnet_shell_desc_t desc = params->desc; fnet_socket_state_t connection_state; int packet_size = params->packet_size; int cur_packet_number; int buffer_offset; int iterations = params->iteration_number; fnet_address_family_t family = params->foreign_addr.sa_family; if(packet_size > FAPP_BENCH_BUFFER_SIZE) /* Check max size.*/ packet_size = FAPP_BENCH_BUFFER_SIZE; fapp_bench.socket_listen = SOCKET_INVALID; /* ------ Start test.----------- */ fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, " TCP TX Test" ); fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "Remote IP Addr", fnet_inet_ntop(family, params->foreign_addr.sa_data, ip_str, sizeof(ip_str))); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Remote Port", fnet_ntohs(params->foreign_addr.sa_port)); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Message Size", params->packet_size); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Num. of messages", params->packet_number); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Num. of iterations", params->iteration_number); fnet_shell_println(desc, FAPP_TOCANCEL_STR); fnet_shell_println(desc, FAPP_DELIMITER_STR); while(iterations--) { /* Create socket */ if((fapp_bench.socket_foreign = socket(family, SOCK_STREAM, 0)) == SOCKET_INVALID) { FNET_DEBUG("BENCH: Socket creation error.\n"); iterations = 0; goto ERROR_1; } /* Set Socket options. */ if( /* Setup linger option. */ (setsockopt (fapp_bench.socket_foreign, SOL_SOCKET, SO_LINGER, (char *)&linger_option, sizeof(linger_option)) == SOCKET_ERROR) || /* Set socket buffer size. */ (setsockopt(fapp_bench.socket_foreign, SOL_SOCKET, SO_RCVBUF, (char *) &bufsize_option, sizeof(bufsize_option))== SOCKET_ERROR) || (setsockopt(fapp_bench.socket_foreign, SOL_SOCKET, SO_SNDBUF, (char *) &bufsize_option, sizeof(bufsize_option))== SOCKET_ERROR) || /* Enable keepalive_option option. */ (setsockopt (fapp_bench.socket_foreign, SOL_SOCKET, SO_KEEPALIVE, (char *)&keepalive_option, sizeof(keepalive_option)) == SOCKET_ERROR) || /* Keepalive probe retransmit limit. */ (setsockopt (fapp_bench.socket_foreign, IPPROTO_TCP, TCP_KEEPCNT, (char *)&keepcnt_option, sizeof(keepcnt_option)) == SOCKET_ERROR) || /* Keepalive retransmit interval.*/ (setsockopt (fapp_bench.socket_foreign, IPPROTO_TCP, TCP_KEEPINTVL, (char *)&keepintvl_option, sizeof(keepintvl_option)) == SOCKET_ERROR) || /* Time between keepalive probes.*/ (setsockopt (fapp_bench.socket_foreign, IPPROTO_TCP, TCP_KEEPIDLE, (char *)&keepidle_option, sizeof(keepidle_option)) == SOCKET_ERROR) ) { FNET_DEBUG("BENCH: Socket setsockopt error.\n"); iterations = 0; goto ERROR_2; } /* Connect to the server.*/ fnet_shell_println(desc,"Connecting."); foreign_addr = params->foreign_addr; connect(fapp_bench.socket_foreign, (struct sockaddr *)(&foreign_addr), sizeof(foreign_addr)); do { option_len = sizeof(connection_state); getsockopt(fapp_bench.socket_foreign, SOL_SOCKET, SO_STATE, (char*)&connection_state, &option_len); } while (connection_state == SS_CONNECTING); if(connection_state != SS_CONNECTED) { fnet_shell_println(desc, "Connection failed."); iterations = 0; goto ERROR_2; } /* Sending.*/ fnet_shell_println(desc,"Sending."); fapp_bench.bytes = 0; fapp_bench.remote_bytes = 0; cur_packet_number = 0; buffer_offset = 0; fapp_bench.first_time = fnet_timer_ticks(); while(1) { send_result = send( fapp_bench.socket_foreign, (char*)(&fapp_bench.buffer[buffer_offset]), (packet_size-buffer_offset), 0); fapp_bench.last_time = fnet_timer_ticks(); if ( send_result == SOCKET_ERROR ) { option_len = sizeof(sock_err); getsockopt(fapp_bench.socket_foreign, SOL_SOCKET, SO_ERROR, (char*)&sock_err, &option_len); fnet_shell_println(desc, "Socket error = %d", sock_err); iterations = 0; #if 0 /* Print benchmark results.*/ fapp_bench_print_results (desc); #endif break; } else if(send_result) { fapp_bench.bytes += send_result; buffer_offset += send_result; if(buffer_offset == packet_size) { cur_packet_number ++; buffer_offset = 0; } exit_flag = fnet_shell_ctrlc(desc); /* Check [Ctrl+c]*/ if((cur_packet_number >= params->packet_number)|| exit_flag) { if(exit_flag) { fnet_shell_println(desc, FAPP_SHELL_CANCELED_CTRL_C); iterations = 0; } /* Print benchmark results.*/ fapp_bench_print_results (desc); break; } } else {} } ERROR_2: closesocket(fapp_bench.socket_foreign); } ERROR_1: fnet_shell_println(desc, FAPP_BENCH_COMPLETED_STR); }
/************************************************************************ * NAME: fapp_bench_udp_rx * * DESCRIPTION: Start Benchmark UDP server. ************************************************************************/ static void fapp_bench_udp_rx (fnet_shell_desc_t desc, fnet_address_family_t family, struct sockaddr *multicast_address /* optional, set to 0*/) { struct sockaddr local_addr; const unsigned long bufsize_option = FAPP_BENCH_SOCKET_BUF_SIZE; int received; char ip_str[FNET_IP_ADDR_STR_SIZE]; struct sockaddr addr; unsigned int addr_len; int is_first = 1; int exit_flag = 0; /* Create listen socket */ if((fapp_bench.socket_listen = socket(family, SOCK_DGRAM, 0)) == SOCKET_INVALID) { FNET_DEBUG("BENCH: Socket creation error.\n"); goto ERROR_1; } /*Bind.*/ fnet_memset_zero(&local_addr, sizeof(local_addr)); local_addr.sa_port = FAPP_BENCH_PORT; local_addr.sa_family = family; if(bind(fapp_bench.socket_listen, &local_addr, sizeof(local_addr)) == SOCKET_ERROR) { FNET_DEBUG("BENCH: Socket bind error.\n"); goto ERROR_2; } /* Set socket options. */ if( /* Set socket buffer size. */ (setsockopt(fapp_bench.socket_listen, SOL_SOCKET, SO_RCVBUF, (char *) &bufsize_option, sizeof(bufsize_option))== SOCKET_ERROR) || (setsockopt(fapp_bench.socket_listen, SOL_SOCKET, SO_SNDBUF, (char *) &bufsize_option, sizeof(bufsize_option))== SOCKET_ERROR) ) { FNET_DEBUG("BENCH: Socket setsockopt error.\n"); goto ERROR_2; } /* Join multicast group, if set. */ if(multicast_address) { #if FNET_CFG_IP4 if(multicast_address->sa_family == AF_INET) { struct ip_mreq mreq; /* Multicast group information.*/ mreq.imr_multiaddr.s_addr = ((struct sockaddr_in*)multicast_address)->sin_addr.s_addr; mreq.imr_interface.s_addr = FNET_HTONL(INADDR_ANY); /* Default Interface.*/ /* Join multicast group. */ if(setsockopt(fapp_bench.socket_listen, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) == SOCKET_ERROR) { FNET_DEBUG("BENCH: Joining to multicast group is failed.\n"); goto ERROR_2; } } #endif #if FNET_CFG_IP6 if(multicast_address->sa_family == AF_INET6) { struct ipv6_mreq mreq6; /* Multicast group information.*/ FNET_IP6_ADDR_COPY(&((struct sockaddr_in6*)multicast_address)->sin6_addr.s6_addr, &mreq6.ipv6imr_multiaddr.s6_addr); mreq6.ipv6imr_interface = ((struct sockaddr_in6*)multicast_address)->sin6_scope_id; /* Join multicast group. */ if(setsockopt(fapp_bench.socket_listen, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&mreq6, sizeof(mreq6)) == SOCKET_ERROR) { FNET_DEBUG("BENCH: Joining to multicast group is failed.\n"); goto ERROR_2; } } #endif } /* ------ Start test.----------- */ fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, " UDP RX Test" ); fnet_shell_println(desc, FAPP_DELIMITER_STR); fapp_netif_addr_print(desc, family, fapp_default_netif, FNET_FALSE); if(multicast_address) { fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "Multicast Group", fnet_inet_ntop(multicast_address->sa_family, (char*)(multicast_address->sa_data), ip_str, sizeof(ip_str)) ); } fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Local Port", FNET_NTOHS(FAPP_BENCH_PORT)); fnet_shell_println(desc, FAPP_TOCANCEL_STR); fnet_shell_println(desc, FAPP_DELIMITER_STR); while(exit_flag == 0) /* Main loop */ { fnet_shell_println(desc, "Waiting."); fapp_bench.bytes = 0; fapp_bench.remote_bytes = 0; addr_len = sizeof(addr); is_first = 1; while(exit_flag == 0) /* Test loop. */ { /* Receive data */ received = recvfrom (fapp_bench.socket_listen, (char*)(&fapp_bench.buffer[0]), FAPP_BENCH_BUFFER_SIZE, 0, &addr, &addr_len ); if(received >= FAPP_BENCH_UDP_END_BUFFER_LENGTH) { /* Reset timeout. */ fapp_bench.last_time = fnet_timer_ticks(); if(is_first) { if( received > FAPP_BENCH_UDP_END_BUFFER_LENGTH ) { fnet_shell_println(desc,"Receiving from %s:%d", fnet_inet_ntop(addr.sa_family, (char*)(addr.sa_data), ip_str, sizeof(ip_str)), fnet_ntohs(addr.sa_port)); fapp_bench.first_time = fnet_timer_ticks(); is_first = 0; } } else { if(received == FAPP_BENCH_UDP_END_BUFFER_LENGTH ) /* End of transfer. */ { /* Send ACK containing amount of received data.*/ unsigned long ack_bytes = fnet_htonl(fapp_bench.bytes); /* Send several times, just to be sure that it is received/not lost.*/ sendto(fapp_bench.socket_listen, (char*)(&ack_bytes), sizeof(ack_bytes), 0, (struct sockaddr*)&addr, sizeof(addr)); sendto(fapp_bench.socket_listen, (char*)(&ack_bytes), sizeof(ack_bytes), 0, (struct sockaddr*)&addr, sizeof(addr)); sendto(fapp_bench.socket_listen, (char*)(&ack_bytes), sizeof(ack_bytes), 0, (struct sockaddr*)&addr, sizeof(addr)); /* Print benchmark results.*/ fapp_bench_print_results (desc); break; } else fapp_bench.bytes += received; } } else { /* Check error. Check timeout */ if(received == SOCKET_ERROR) { fnet_shell_println(desc, "BENCH: Error (%d).", fnet_error_get()); break; } /* Check timeout. */ if((is_first == 0) && (fnet_timer_get_interval(fapp_bench.last_time, fnet_timer_ticks()) > (FAPP_UDP_TIMEOUT_MS/FNET_TIMER_PERIOD_MS))) { fnet_shell_println(desc, "BENCH: Exit on timeout."); fapp_bench_print_results (desc); break; } } exit_flag = fnet_shell_ctrlc (desc); } } ERROR_2: closesocket(fapp_bench.socket_listen); ERROR_1: fnet_shell_println(desc, FAPP_BENCH_COMPLETED_STR); }
/************************************************************************ * NAME: fapp_bench_tcp_rx * * DESCRIPTION: Start Benchmark TCP server. ************************************************************************/ static void fapp_bench_tcp_rx (fnet_shell_desc_t desc, fnet_address_family_t family) { struct sockaddr local_addr; int received; char ip_str[FNET_IP_ADDR_STR_SIZE]; struct linger linger_option ={1, /*l_onoff*/ 4 /*l_linger*/}; unsigned long bufsize_option = FAPP_BENCH_SOCKET_BUF_SIZE; int keepalive_option = 1; int keepcnt_option = FAPP_BENCH_TCP_KEEPCNT; int keepintvl_option = FAPP_BENCH_TCP_KEEPINTVL; int keepidle_option = FAPP_BENCH_TCP_KEEPIDLE; struct sockaddr foreign_addr; unsigned int addr_len; int exit_flag = 0; fapp_bench.socket_foreign = SOCKET_INVALID; /* Create listen socket */ if((fapp_bench.socket_listen = socket(family, SOCK_STREAM, 0)) == SOCKET_INVALID) { FNET_DEBUG("BENCH: Socket creation error."); goto ERROR_1; } /* Bind socket.*/ fnet_memset_zero(&local_addr, sizeof(local_addr)); local_addr.sa_port = FAPP_BENCH_PORT; local_addr.sa_family = family; if(bind(fapp_bench.socket_listen, &local_addr, sizeof(local_addr)) == SOCKET_ERROR) { FNET_DEBUG("BENCH: Socket bind error."); goto ERROR_2; } /* Set Socket options. */ if( /* Setup linger option. */ (setsockopt (fapp_bench.socket_listen, SOL_SOCKET, SO_LINGER, (char *)&linger_option, sizeof(linger_option)) == SOCKET_ERROR) || /* Set socket buffer size. */ (setsockopt(fapp_bench.socket_listen, SOL_SOCKET, SO_RCVBUF, (char *) &bufsize_option, sizeof(bufsize_option))== SOCKET_ERROR) || (setsockopt(fapp_bench.socket_listen, SOL_SOCKET, SO_SNDBUF, (char *) &bufsize_option, sizeof(bufsize_option))== SOCKET_ERROR) || /* Enable keepalive_option option. */ (setsockopt (fapp_bench.socket_listen, SOL_SOCKET, SO_KEEPALIVE, (char *)&keepalive_option, sizeof(keepalive_option)) == SOCKET_ERROR) || /* Keepalive probe retransmit limit. */ (setsockopt (fapp_bench.socket_listen, IPPROTO_TCP, TCP_KEEPCNT, (char *)&keepcnt_option, sizeof(keepcnt_option)) == SOCKET_ERROR) || /* Keepalive retransmit interval.*/ (setsockopt (fapp_bench.socket_listen, IPPROTO_TCP, TCP_KEEPINTVL, (char *)&keepintvl_option, sizeof(keepintvl_option)) == SOCKET_ERROR) || /* Time between keepalive probes.*/ (setsockopt (fapp_bench.socket_listen, IPPROTO_TCP, TCP_KEEPIDLE, (char *)&keepidle_option, sizeof(keepidle_option)) == SOCKET_ERROR) ) { FNET_DEBUG("BENCH: Socket setsockopt error.\n"); goto ERROR_2; } /* Listen. */ if(listen(fapp_bench.socket_listen, 1) == SOCKET_ERROR) { FNET_DEBUG("BENCH: Socket listen error.\n"); goto ERROR_2; } /* ------ Start test.----------- */ fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, " TCP RX Test"); fnet_shell_println(desc, FAPP_DELIMITER_STR); fapp_netif_addr_print(desc, family, fapp_default_netif, FNET_FALSE); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Local Port", FNET_NTOHS(FAPP_BENCH_PORT)); fnet_shell_println(desc, FAPP_TOCANCEL_STR); fnet_shell_println(desc, FAPP_DELIMITER_STR); while(exit_flag == 0) { fnet_shell_println(desc, "Waiting."); fapp_bench.bytes = 0; fapp_bench.remote_bytes = 0; if(fapp_bench.socket_foreign != SOCKET_INVALID) { closesocket(fapp_bench.socket_foreign); fapp_bench.socket_foreign = SOCKET_INVALID; } while((fapp_bench.socket_foreign == SOCKET_INVALID) && (exit_flag == 0)) { /*Accept*/ addr_len = sizeof(foreign_addr); fapp_bench.socket_foreign = accept(fapp_bench.socket_listen, &foreign_addr, &addr_len); exit_flag = fnet_shell_ctrlc (desc); if(fapp_bench.socket_foreign != SOCKET_INVALID) { fnet_shell_println(desc,"Receiving from %s:%d", fnet_inet_ntop(foreign_addr.sa_family, (char*)(foreign_addr.sa_data), ip_str, sizeof(ip_str)), fnet_ntohs(foreign_addr.sa_port)); fapp_bench.first_time = fnet_timer_ticks(); while(1) /* Receiving data.*/ { received = recv(fapp_bench.socket_foreign, (char*)(&fapp_bench.buffer[0]), FAPP_BENCH_BUFFER_SIZE, 0); if ((received == SOCKET_ERROR) || exit_flag) { fapp_bench.last_time = fnet_timer_ticks(); /* Print benchmark results.*/ fapp_bench_print_results (desc); break; } else { fapp_bench.bytes += received; } exit_flag = fnet_shell_ctrlc (desc); /* Check [Ctrl+c]*/ } } } } closesocket(fapp_bench.socket_foreign); ERROR_2: closesocket(fapp_bench.socket_listen); ERROR_1: fnet_shell_println(desc, FAPP_BENCH_COMPLETED_STR); }
/************************************************************************ * NAME: fnet_telnet_state_machine * * DESCRIPTION: Telnet server state machine. ************************************************************************/ static void fnet_telnet_state_machine( void *telnet_if_p ) { struct sockaddr foreign_addr; int res; struct fnet_telnet_if *telnet = (struct fnet_telnet_if *)telnet_if_p; char rx_data[1]; int len; int i; struct fnet_telnet_session_if *session; for(i=0; i<FNET_CFG_TELNET_SESSION_MAX; i++) { session = &telnet->session[i]; telnet->session_active = session; do { switch(session->state) { /*---- LISTENING ------------------------------------------------*/ case FNET_TELNET_STATE_LISTENING: len = sizeof(foreign_addr); session->socket_foreign = accept(telnet->socket_listen, (struct sockaddr *) &foreign_addr, &len); if(session->socket_foreign != SOCKET_INVALID) { #if FNET_CFG_DEBUG_TELNET { char ip_str[FNET_IP_ADDR_STR_SIZE]; fnet_inet_ntop(foreign_addr.sa_family, foreign_addr.sa_data, ip_str, sizeof(ip_str)); FNET_DEBUG_TELNET("\nTELNET: New connection: %s; Port: %d.", ip_str, fnet_ntohs(foreign_addr.sa_port)); } #endif /* Init Shell. */ session->shell_descriptor = fnet_shell_init(&session->shell_params); if(session->shell_descriptor == FNET_ERR) { session->shell_descriptor = 0; FNET_DEBUG_TELNET("TELNET: Shell Service registration error."); session->state = FNET_TELNET_STATE_CLOSING; /*=> CLOSING */ } else { listen(telnet->socket_listen, --telnet->backlog); /* Ignor other connections.*/ /* Reset TX timeout. */ session->state = FNET_TELNET_STATE_RECEIVING; /* => WAITING data */ } } break; /*---- NORMAL -----------------------------------------------*/ case FNET_TELNET_STATE_RECEIVING: if(rx_buffer_free_space(session)>0) { res = recv(session->socket_foreign, rx_data, 1, 0); if(res == 1) { if(rx_data[0] == FNET_TELNET_CMD_IAC ) { session->state = FNET_TELNET_STATE_IAC; /*=> Handle IAC */ } else { rx_buffer_write (session, rx_data[0]); } } else if (res == SOCKET_ERROR) { session->state = FNET_TELNET_STATE_CLOSING; /*=> CLOSING */ } } break; /*---- IAC -----------------------------------------------*/ case FNET_TELNET_STATE_IAC: FNET_DEBUG_TELNET("TELNET: STATE_IAC"); if((res = recv(session->socket_foreign, rx_data, 1, 0) )!= SOCKET_ERROR) { if(res) { switch(rx_data[0]) { case FNET_TELNET_CMD_WILL: session->state = FNET_TELNET_STATE_DONT; break; case FNET_TELNET_CMD_DO: session->state = FNET_TELNET_STATE_WONT; break; case FNET_TELNET_CMD_WONT: case FNET_TELNET_CMD_DONT: session->state = FNET_TELNET_STATE_SKIP ; break; case FNET_TELNET_CMD_IAC: /* the IAC need be doubled to be sent as data, and the other 255 codes may be passed transparently. */ rx_buffer_write (session, rx_data[0]); default: session->state = FNET_TELNET_STATE_RECEIVING; /*=> Ignore commands */ } } } else { session->state = FNET_TELNET_STATE_CLOSING; /*=> CLOSING */ } break; /*---- DONT & WONT -----------------------------------------------*/ case FNET_TELNET_STATE_DONT: case FNET_TELNET_STATE_WONT: { char command; if(session->state == FNET_TELNET_STATE_DONT) { FNET_DEBUG_TELNET("TELNET: STATE_DONT"); command = FNET_TELNET_CMD_DONT; } else { FNET_DEBUG_TELNET("TELNET: STATE_WONT"); command = FNET_TELNET_CMD_WONT; } if(tx_buffer_free_space(session) >= 3) { res = recv(session->socket_foreign, rx_data, 1, 0); if(res == 1) { /* Send command. */ fnet_telnet_send_cmd(session, command, rx_data[0]); session->state = FNET_TELNET_STATE_RECEIVING; } else if (res == SOCKET_ERROR) { session->state = FNET_TELNET_STATE_CLOSING; /*=> CLOSING */ } } } break; /*---- SKIP -----------------------------------------------*/ case FNET_TELNET_STATE_SKIP: FNET_DEBUG_TELNET("TELNET: STATE_SKIP"); res = recv(session->socket_foreign, rx_data, 1, 0); if(res == 1) { session->state = FNET_TELNET_STATE_RECEIVING; } else if (res == SOCKET_ERROR) { session->state = FNET_TELNET_STATE_CLOSING; /*=> CLOSING */ } break; /*---- CLOSING --------------------------------------------*/ case FNET_TELNET_STATE_CLOSING: FNET_DEBUG_TELNET("TELNET: STATE_CLOSING"); if(session->shell_descriptor) { fnet_shell_release(session->shell_descriptor); session->shell_descriptor = 0; } session->rx_buffer_head = session->rx_buffer; session->rx_buffer_tail = session->rx_buffer; closesocket(session->socket_foreign); session->socket_foreign = SOCKET_INVALID; listen(telnet->socket_listen, ++telnet->backlog); /* Allow connection.*/ session->state = FNET_TELNET_STATE_LISTENING; /*=> LISTENING */ break; default: break; } } while(session->state == FNET_TELNET_STATE_CLOSING); } }
/************************************************************************ * NAME: fapp_ping_cmd * * DESCRIPTION: Ping command. ************************************************************************/ void fapp_ping_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv ) { struct fnet_ping_params ping_params; fnet_index_t i; fnet_char_t *p; fnet_uint32_t value; fnet_char_t ip_str[FNET_IP_ADDR_STR_SIZE]; fnet_memset_zero(&ping_params, sizeof(ping_params)); ping_params.cookie = (fnet_uint32_t)desc; ping_params.handler = fapp_ping_handler; ping_params.packet_size = FAPP_PING_DEFAULT_SIZE; ping_params.timeout = FAPP_PING_DEFAULT_TIMEOUT; ping_params.pattern = FAPP_PING_DEFAULT_PATTERN; ping_params.ttl = FAPP_PING_DEFAULT_HOP_LIMIT; ping_params.packet_count = FAPP_PING_DEFAULT_NUMBER; /* Last parameter must be ip address.*/ i = (argc-1u); if(fnet_inet_ptos(argv[i], &ping_params.target_addr) == FNET_ERR) { goto ERROR_PARAMETER; } else { /* TBD Optimise parameters parsing.*/ if(argc > 2u) /* There are additional parameters */ { /* [-c <count>][-i <seconds>]\n\t[-p <pattern>][-s <size>][-h <hoplimit/ttl>] */ for(i=1u; i<(fnet_index_t)(argc-1u); i++) { if (!fnet_strcmp(argv[i], "-c")) { i++; value = fnet_strtoul(argv[i], &p, 10u); if ((value == 0U) && (p == argv[i])) { goto ERROR_PARAMETER; } else { ping_params.packet_count = value; } } else if (!fnet_strcmp(argv[i], "-i")) { i++; value = fnet_strtoul(argv[i], &p, 10u); if ((value == 0U) && (p == argv[i])) { goto ERROR_PARAMETER; } else { ping_params.timeout = value*1000U; } } else if (!fnet_strcmp(argv[i], "-p")) { i++; value = fnet_strtoul(argv[i], &p, 10u); if ((value == 0U) && (p == argv[i])) { goto ERROR_PARAMETER; } else { ping_params.pattern = (fnet_uint8_t)value; } } else if (!fnet_strcmp(argv[i], "-s")) { i++; value = fnet_strtoul(argv[i], &p, 10u); if ((value == 0U) && (p == argv[i])) { goto ERROR_PARAMETER; } else { ping_params.packet_size = (fnet_size_t)value; } } else if (!fnet_strcmp(argv[i], "-h")) { i++; value = fnet_strtoul(argv[i], &p, 10u); if ((value == 0U) && (p == argv[i])) { goto ERROR_PARAMETER; } else { ping_params.ttl = (fnet_uint8_t)value; } } else if (!fnet_strcmp(argv[i], "-n")) { /* Just ignore the -n parameter.*/ } else if (!fnet_strcmp(argv[i], "-I")) { i++; /* Just ignore the -I parameter and its value.*/ } else /* Wrong parameter.*/ { goto ERROR_PARAMETER; } } } if(fnet_ping_request(&ping_params) == FNET_OK) { fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, " PING" ); fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "Remote IP addr", fnet_inet_ntop(ping_params.target_addr.sa_family, ping_params.target_addr.sa_data, ip_str, sizeof(ip_str))); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Message Size", ping_params.packet_size>FNET_CFG_PING_PACKET_MAX?FNET_CFG_PING_PACKET_MAX:ping_params.packet_size); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Num. of messages", ping_params.packet_count); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Pattern", ping_params.pattern); fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Hoplimit (TTL)", ping_params.ttl); fnet_shell_println(desc, FAPP_TOCANCEL_STR); fnet_shell_println(desc, FAPP_DELIMITER_STR); fnet_shell_block(desc, fapp_ping_on_ctrlc); /* Block shell. */ } else { fnet_shell_println(desc, FAPP_INIT_ERR, "PING"); } } return; ERROR_PARAMETER: fnet_shell_println(desc, FAPP_PARAM_ERR, argv[i]); return; }
/************************************************************************ * NAME: fnet_http_state_machine * * DESCRIPTION: Http server state machine. ************************************************************************/ static void fnet_http_state_machine( void *http_if_p ) { struct sockaddr foreign_addr; int len; int res; struct fnet_http_if *http = (struct fnet_http_if *)http_if_p; int iteration; char *ch; int i; struct fnet_http_session_if *session; for(i=0; i<FNET_CFG_HTTP_SESSION_MAX; i++) { session = &http->session[i]; http->session_active = session; for(iteration = 0; iteration < FNET_HTTP_ITERATION_NUMBER; iteration++) { switch(session->state) { /*---- LISTENING ------------------------------------------------*/ case FNET_HTTP_STATE_LISTENING: len = sizeof(foreign_addr); if((session->socket_foreign = accept(http->socket_listen, &foreign_addr, &len)) != SOCKET_INVALID) { #if FNET_CFG_DEBUG_HTTP { char ip_str[FNET_IP_ADDR_STR_SIZE]; fnet_inet_ntop(foreign_addr.sa_family, (char*)(foreign_addr.sa_data), ip_str, sizeof(ip_str)); FNET_DEBUG_HTTP(""); FNET_DEBUG_HTTP("HTTP: RX Request From: %s; Port: %d.", ip_str, fnet_ntohs(foreign_addr.sa_port)); } #endif /* Reset response & request parameters.*/ fnet_memset_zero(&session->response, sizeof(struct fnet_http_response)); fnet_memset_zero(&session->request, sizeof(struct fnet_http_request)); #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/ session->response.content_length = -1; /* No content length by default.*/ /* Default HTTP version response.*/ session->response.version.major = FNET_HTTP_VERSION_MAJOR; session->response.version.minor = FNET_HTTP_VERSION_MINOR; session->response.tx_data = fnet_http_tx_status_line; #endif session->state_time = fnet_timer_ticks(); /* Reset timeout. */ session->buffer_actual_size = 0; session->state = FNET_HTTP_STATE_RX_REQUEST; /* => WAITING HTTP REQUEST */ } break; /*---- RX_LINE -----------------------------------------------*/ case FNET_HTTP_STATE_RX_REQUEST: do { /* Read character by character.*/ ch = &session->buffer[session->buffer_actual_size]; if((res = recv(session->socket_foreign, ch, 1, 0) )!= SOCKET_ERROR) { if(res > 0) /* Received a data.*/ { session->state_time = fnet_timer_ticks(); /* Reset timeout.*/ session->buffer_actual_size++; if(*ch == '\r') *ch = '\0'; else if(*ch == '\n') /* Line received.*/ { char * req_buf = &session->buffer[0]; *ch = '\0'; if(session->request.method == 0) /* Parse Request line.*/ { const struct fnet_http_method **method = &fnet_http_method_list[0]; FNET_DEBUG_HTTP("HTTP: RX Request: %s", req_buf); /* Determine the method type. */ while(*method) { if ( !fnet_strcmp_splitter(req_buf, (*method)->token, ' ') ) { req_buf+=fnet_strlen((*method)->token); session->request.method = *method; break; } method++; } /* Check if the method is supported? */ if(session->request.method && session->request.method->handle) { /* Parse URI.*/ req_buf = fnet_http_uri_parse(req_buf, &session->request.uri); FNET_DEBUG_HTTP("HTTP: URI Path = %s; Query = %s", http->request.uri.path, http->request.uri.query); #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/ /* Parse HTTP/x.x version.*/ fnet_http_version_parse(++req_buf, &session->response.version); /* Check the highest supported HTTP version.*/ if(((session->response.version.major<<8)|session->response.version.minor) > ((FNET_HTTP_VERSION_MAJOR<<8)|FNET_HTTP_VERSION_MINOR)) { session->response.version.major = FNET_HTTP_VERSION_MAJOR; session->response.version.minor = FNET_HTTP_VERSION_MINOR; } if(session->response.version.major == 0) /* HTTP/0.x */ { session->state = FNET_HTTP_STATE_CLOSING; /* Client does not support HTTP/1.x*/ break; } #if FNET_CFG_HTTP_AUTHENTICATION_BASIC /* Check Authentification.*/ fnet_http_auth_validate_uri(http); #endif #endif/*FNET_CFG_HTTP_VERSION_MAJOR*/ /* Call method initial handler.*/ res = session->request.method->handle(http, &session->request.uri); #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/ if(fnet_http_status_ok(res) == FNET_OK) #else if((res == FNET_OK)) #endif { #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/ session->buffer_actual_size = 0; /* => Parse Header line.*/ #else /* HTTP/0.9 */ session->response.tx_data = session->request.method->send; /* Reset buffer pointers.*/ session->buffer_actual_size = 0; session->state = FNET_HTTP_STATE_TX; /* Send data.*/ #endif } /* Method error.*/ else /* Error.*/ { #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/ /* Default code = FNET_HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR. */ if(res != FNET_ERR) session->response.status.code = (fnet_http_status_code_t)res; /* Send status line.*/ session->buffer_actual_size = 0; session->state = FNET_HTTP_STATE_TX; /* Send error.*/ #else /* HTTP/0.9 */ session->state = FNET_HTTP_STATE_CLOSING; #endif } } /* Method is not supported.*/ else /* Error.*/ { #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/ session->response.status.code = FNET_HTTP_STATUS_CODE_NOT_IMPLEMENTED; /* Send status line.*/ session->buffer_actual_size = 0; session->state = FNET_HTTP_STATE_TX; /* Send error.*/ #else /* HTTP/0.9 */ session->state = FNET_HTTP_STATE_CLOSING; #endif } } #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/ /* Parse Header line.*/ else { if(session->request.skip_line == 0) { if(*req_buf == 0) /* === Empty line => End of the request header. ===*/ { #if FNET_CFG_HTTP_AUTHENTICATION_BASIC if(session->response.auth_entry) /* Send UNAUTHORIZED error.*/ session->response.status.code = FNET_HTTP_STATUS_CODE_UNAUTHORIZED; else /* Send Data.*/ #endif session->response.status.code = FNET_HTTP_STATUS_CODE_OK; #if FNET_CFG_HTTP_POST if(session->request.content_length > 0) /* RX Entity-Body.*/ { session->buffer_actual_size = 0; session->state = FNET_HTTP_STATE_RX; } else #endif /* TX Full-Responce.*/ { /* Send status line.*/ session->buffer_actual_size = 0; session->state = FNET_HTTP_STATE_TX; } break; } else /* === Parse header fields. ===*/ { FNET_DEBUG_HTTP("HTTP: RX Header: %s", req_buf); #if FNET_CFG_HTTP_AUTHENTICATION_BASIC /* --- Authorization: ---*/ if (session->response.auth_entry && fnet_strncmp(req_buf, FNET_HTTP_HEADER_FIELD_AUTHORIZATION, sizeof(FNET_HTTP_HEADER_FIELD_AUTHORIZATION)-1) == 0) /* Authetication is required.*/ { char *auth_str = &req_buf[sizeof(FNET_HTTP_HEADER_FIELD_AUTHORIZATION)-1]; /* Validate credentials.*/ if(fnet_http_auth_validate_credentials(http, auth_str) == FNET_OK) session->response.auth_entry = 0; /* Authorization is succesful.*/ } #endif #if FNET_CFG_HTTP_POST /* --- Content-Length: ---*/ if (session->request.method->receive && fnet_strncmp(req_buf, FNET_HTTP_HEADER_FIELD_CONTENT_LENGTH, sizeof(FNET_HTTP_HEADER_FIELD_CONTENT_LENGTH)-1) == 0) { char *p; char *length_str = &req_buf[sizeof(FNET_HTTP_HEADER_FIELD_CONTENT_LENGTH)-1]; session->request.content_length = (long)fnet_strtoul(length_str,&p,10); } #endif } } /* Line is skiped.*/ else session->request.skip_line = 0; /* Reset the Skip flag.*/ session->buffer_actual_size = 0; /* => Parse Next Header line.*/ } #endif/* FNET_CFG_HTTP_VERSION_MAJOR */ } /* Not whole line received yet.*/ else if (session->buffer_actual_size == FNET_HTTP_BUF_SIZE) /* Buffer is full.*/ { #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/ if(session->request.method != 0) /* For header, skip the line.*/ { /* Skip line.*/ session->request.skip_line = 1; session->buffer_actual_size = 0; } else /* Error.*/ { /* Default code = FNET_HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR. */ session->buffer_actual_size = 0; session->state = FNET_HTTP_STATE_TX; /* Send error.*/ } #else /* HTTP/0.9 */ session->state = FNET_HTTP_STATE_CLOSING; #endif } } /* No data.*/ else if(fnet_timer_get_interval(session->state_time, fnet_timer_ticks()) /* Time out? */ > (FNET_HTTP_WAIT_RX_MS / FNET_TIMER_PERIOD_MS)) { session->state = FNET_HTTP_STATE_CLOSING; /*=> CLOSING */ } /* else => WAITING REQUEST. */ } /* recv() error.*/ else { session->state = FNET_HTTP_STATE_CLOSING; /*=> CLOSING */ } } while ((res > 0) && (session->state == FNET_HTTP_STATE_RX_REQUEST)); /* Till receiving the request header.*/ break; #if FNET_CFG_HTTP_POST /*---- RX --------------------------------------------------*/ case FNET_HTTP_STATE_RX: /* Receive data (Entity-Body). */ if((res = recv(session->socket_foreign, &session->buffer[session->buffer_actual_size], (int)(FNET_HTTP_BUF_SIZE-session->buffer_actual_size), 0) )!= SOCKET_ERROR) { session->buffer_actual_size += res; session->request.content_length -= res; if(res > 0) /* Some Data.*/ { session->state_time = fnet_timer_ticks(); /* Reset timeout.*/ res = session->request.method->receive(http); if(fnet_http_status_ok(res) != FNET_OK) { if(res != FNET_ERR) session->response.status.code = (fnet_http_status_code_t)res; else session->response.status.code = FNET_HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR; session->request.content_length = 0; } if(session->request.content_length <= 0) /* The last data.*/ { session->state = FNET_HTTP_STATE_TX; /* Send data.*/ } session->buffer_actual_size = 0; } else /* No Data.*/ { if(fnet_timer_get_interval(session->state_time, fnet_timer_ticks()) > (FNET_HTTP_WAIT_RX_MS / FNET_TIMER_PERIOD_MS)) /* Time out.*/ { session->state = FNET_HTTP_STATE_CLOSING; /*=> CLOSING */ } } } else /* Socket error.*/ { session->state = FNET_HTTP_STATE_CLOSING; /*=> CLOSING */ } break; #endif /* FNET_CFG_HTTP_POST.*/ /*---- TX --------------------------------------------------*/ case FNET_HTTP_STATE_TX: /* Send data. */ if(fnet_timer_get_interval(session->state_time, fnet_timer_ticks()) < (FNET_HTTP_WAIT_TX_MS / FNET_TIMER_PERIOD_MS)) /* Check timeout */ { int send_size; if(session->buffer_actual_size == session->response.buffer_sent) { /* Reset counters.*/ session->buffer_actual_size =0; session->response.buffer_sent = 0; if(session->response.send_eof || session->response.tx_data(http) == FNET_ERR) /* get data for sending */ { session->state = FNET_HTTP_STATE_CLOSING; /*=> CLOSING */ break; } } send_size = (int)(session->buffer_actual_size - (int)session->response.buffer_sent); if(send_size > http->send_max) send_size = (int)http->send_max; if((res = send(session->socket_foreign, session->buffer + session->response.buffer_sent, send_size, 0)) != SOCKET_ERROR) { if(res) { FNET_DEBUG_HTTP("HTTP: TX %d bytes.", res); session->state_time = fnet_timer_ticks(); /* reset timeout */ session->response.buffer_sent += res; } break; /* => SENDING */ } } session->state = FNET_HTTP_STATE_CLOSING; /*=> CLOSING */ break; /*---- CLOSING --------------------------------------------------*/ case FNET_HTTP_STATE_CLOSING: if(session->request.method && session->request.method->close) session->request.method->close(http); closesocket(session->socket_foreign); session->socket_foreign = SOCKET_INVALID; session->state = FNET_HTTP_STATE_LISTENING; /*=> LISTENING */ break; default: break; } } } /*for(sessions)*/ }
static void fapp_get_cmd_tftp(fnet_shell_desc_t desc) { char ip_str[FNET_IP_ADDR_STR_SIZE]; fnet_shell_println(desc, FAPP_GET_SOPT_FORMAT, fnet_inet_ntop(fapp_params_tftp_config.server_addr.sa_family, fapp_params_tftp_config.server_addr.sa_data, ip_str, sizeof(ip_str)) ); }