static int NonBlockSSH_connect(WOLFSSH* ssh) { int ret; int error; SOCKET_T sockfd; int select_ret = 0; ret = wolfSSH_connect(ssh); error = wolfSSH_get_error(ssh); sockfd = (SOCKET_T)wolfSSH_get_fd(ssh); while (ret != WS_SUCCESS && (error == WS_WANT_READ || error == WS_WANT_WRITE)) { if (error == WS_WANT_READ) printf("... client would read block\n"); else if (error == WS_WANT_WRITE) printf("... client would write block\n"); select_ret = tcp_select(sockfd, 1); if (select_ret == WS_SELECT_RECV_READY || select_ret == WS_SELECT_ERROR_READY) { ret = wolfSSH_connect(ssh); } else if (select_ret == WS_SELECT_TIMEOUT) error = WS_WANT_READ; else error = WS_FATAL_ERROR; } return ret; }
int tcp_accept(int localsocket_fd, int *remotesocket_fd, int timeout) { int new_sd; PRVERBOSE1("accepting...for %ds\n", timeout); if (timeout==0) timeout=-1; /* select will wait forever */ if (tcp_select(localsocket_fd, timeout) != 0) { new_sd = accept(localsocket_fd, NULL, NULL); if (new_sd == -1) { PRERROR("tcp_accept: accept error\n"); perror(""); return -1; } *remotesocket_fd = new_sd; close(localsocket_fd); /* connlist[conn].state = CONNSTATE_UP; */ PRVERBOSE("connected to client!\n"); } else { PRERROR("timeout for server exceeded!\n"); return -1; } return 0; }
/* Echo number of bytes specified by -e arg */ int ServerEchoData(SSL* ssl, int clientfd, int echoData, int throughput) { int ret = 0; char* buffer = (char*)malloc(TEST_BUFFER_SIZE); if(buffer) { double start = 0, rx_time = 0, tx_time = 0; int xfer_bytes = 0; while((echoData && throughput == 0) || (!echoData && xfer_bytes < throughput)) { int select_ret = tcp_select(clientfd, 1); /* Timeout=1 second */ if (select_ret == TEST_RECV_READY) { int len = min(TEST_BUFFER_SIZE, throughput - xfer_bytes); int rx_pos = 0; if(throughput) { start = current_time(1); } while(rx_pos < len) { ret = SSL_read(ssl, &buffer[rx_pos], len - rx_pos); if (ret <= 0) { int readErr = SSL_get_error(ssl, 0); if (readErr != SSL_ERROR_WANT_READ) { printf("SSL_read error %d!\n", readErr); err_sys("SSL_read failed"); } } else { rx_pos += ret; } } if(throughput) { rx_time += current_time(0) - start; start = current_time(1); } if (SSL_write(ssl, buffer, len) != len) { err_sys("SSL_write failed"); } if(throughput) { tx_time += current_time(0) - start; } xfer_bytes += len; } } free(buffer); if(throughput) { printf("wolfSSL Server Benchmark %d bytes\n" "\tRX %8.3f ms (%8.3f MBps)\n" "\tTX %8.3f ms (%8.3f MBps)\n", throughput, tx_time * 1000, throughput / tx_time / 1024 / 1024, rx_time * 1000, throughput / rx_time / 1024 / 1024 ); } } else { err_sys("Server buffer malloc failed"); } return EXIT_SUCCESS; }
/* handle SFTP operations * returns 0 on success */ static int sftp_worker(thread_ctx_t* threadCtx) { int ret = WS_SUCCESS; int error = WS_SUCCESS; SOCKET_T sockfd; int select_ret = 0; sockfd = (SOCKET_T)wolfSSH_get_fd(threadCtx->ssh); do { if (threadCtx->nonBlock) { if (error == WS_WANT_READ) printf("... sftp server would read block\n"); else if (error == WS_WANT_WRITE) printf("... sftp server would write block\n"); } select_ret = tcp_select(sockfd, TEST_SFTP_TIMEOUT); if (select_ret == WS_SELECT_RECV_READY || select_ret == WS_SELECT_ERROR_READY || error == WS_WANT_WRITE) { ret = wolfSSH_SFTP_read(threadCtx->ssh); error = wolfSSH_get_error(threadCtx->ssh); } else if (select_ret == WS_SELECT_TIMEOUT) error = WS_WANT_READ; else error = WS_FATAL_ERROR; if (error == WS_WANT_READ || error == WS_WANT_WRITE) ret = WS_WANT_READ; } while (ret != WS_FATAL_ERROR); return ret; }
static void NonBlockingSSL_Accept(SSL* ssl) { #ifndef CYASSL_CALLBACKS int ret = SSL_accept(ssl); #else int ret = CyaSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB, srvTo); #endif int error = SSL_get_error(ssl, 0); SOCKET_T sockfd = (SOCKET_T)CyaSSL_get_fd(ssl); int select_ret; while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)) { if (error == SSL_ERROR_WANT_READ) printf("... server would read block\n"); else printf("... server would write block\n"); if (CyaSSL_dtls(ssl)) select_ret = tcp_select(sockfd, CyaSSL_dtls_get_current_timeout(ssl)); else select_ret = tcp_select(sockfd, 1); if ((select_ret == TEST_RECV_READY) || (select_ret == TEST_ERROR_READY)) { #ifndef CYASSL_CALLBACKS ret = SSL_accept(ssl); #else ret = CyaSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB, srvTo); #endif error = SSL_get_error(ssl, 0); } else if (select_ret == TEST_TIMEOUT && (!CyaSSL_dtls(ssl) || (CyaSSL_dtls_got_timeout(ssl) >= 0))) { error = SSL_ERROR_WANT_READ; } else { error = SSL_FATAL_ERROR; } } if (ret != SSL_SUCCESS) err_sys("SSL_accept failed"); }
static int NonBlockingSSL_Accept(SSL* ssl) { #ifndef CYASSL_CALLBACKS int ret = SSL_accept(ssl); #else int ret = CyaSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB, srvTo); #endif int error = SSL_get_error(ssl, 0); SOCKET_T sockfd = (SOCKET_T)CyaSSL_get_fd(ssl); int select_ret; while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)) { int currTimeout = 1; if (error == SSL_ERROR_WANT_READ) { /* printf("... server would read block\n"); */ } else { /* printf("... server would write block\n"); */ } #ifdef CYASSL_DTLS currTimeout = CyaSSL_dtls_get_current_timeout(ssl); #endif select_ret = tcp_select(sockfd, currTimeout); if ((select_ret == TEST_RECV_READY) || (select_ret == TEST_ERROR_READY)) { #ifndef CYASSL_CALLBACKS ret = SSL_accept(ssl); #else ret = CyaSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB, srvTo); #endif error = SSL_get_error(ssl, 0); } else if (select_ret == TEST_TIMEOUT && !CyaSSL_dtls(ssl)) { error = SSL_ERROR_WANT_READ; } #ifdef CYASSL_DTLS else if (select_ret == TEST_TIMEOUT && CyaSSL_dtls(ssl) && CyaSSL_dtls_got_timeout(ssl) >= 0) { error = SSL_ERROR_WANT_READ; } #endif else { error = SSL_FATAL_ERROR; } } return ret; }
static void NonBlockingSSL_Connect(CYASSL* ssl) { #ifndef CYASSL_CALLBACKS int ret = CyaSSL_connect(ssl); #else int ret = CyaSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeout); #endif int error = CyaSSL_get_error(ssl, 0); SOCKET_T sockfd = (SOCKET_T)CyaSSL_get_fd(ssl); int select_ret; while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)) { int currTimeout = 1; if (error == SSL_ERROR_WANT_READ) printf("... client would read block\n"); else printf("... client would write block\n"); #ifdef CYASSL_DTLS currTimeout = CyaSSL_dtls_get_current_timeout(ssl); #endif select_ret = tcp_select(sockfd, currTimeout); if ((select_ret == TEST_RECV_READY) || (select_ret == TEST_ERROR_READY)) { #ifndef CYASSL_CALLBACKS ret = CyaSSL_connect(ssl); #else ret = CyaSSL_connect_ex(ssl,handShakeCB,timeoutCB,timeout); #endif error = CyaSSL_get_error(ssl, 0); } else if (select_ret == TEST_TIMEOUT && !CyaSSL_dtls(ssl)) { error = SSL_ERROR_WANT_READ; } #ifdef CYASSL_DTLS else if (select_ret == TEST_TIMEOUT && CyaSSL_dtls(ssl) && CyaSSL_dtls_got_timeout(ssl) >= 0) { error = SSL_ERROR_WANT_READ; } #endif else { error = SSL_FATAL_ERROR; } } if (ret != SSL_SUCCESS) err_sys("SSL_connect failed"); }
int main(){ int conn1; char recbuf[PKTSIZE]; int size,j,i; sequence_init(); tcp_init(); conn1=tcp_add_connection("134.130.62.98" ,2500, "134.130.62.94",2500, CONN_SERVER); tcp_add_ip(conn1,"134.130.62.98" ,2501, "134.130.62.94",2501); tcp_add_ip(conn1,"134.130.62.98" ,2502, "134.130.62.94",2502); tcp_add_ip(conn1,"134.130.62.98" ,2503, "134.130.62.94",2503); if (tcp_establish_connection(conn1) < 0) { printf("kann Verbindung nicht aufbauen!\n"); exit(-1); } i = 0; while(1) { printf("testing for data...\n"); while (!tcp_select(conn1, 0)) { printf("."); fflush(stdout); sleep(1); } printf("\n"); printf("message available!\n"); size = tcp_receive_message(recbuf, PKTSIZE, conn1); if (size != i) { fprintf(stderr, "expected %d Bytes, got %d Bytes\n", i, size); fflush(stderr); } fprintf(stderr, "got %d Bytes, first Byte: %d\n", size, (int)((char*)recbuf)[1]); fflush(stderr); if (*((char*)recbuf) == FERTIG) { printf("Dienstschluss!\n"); break; } for ( j = 1; j < size; j++) if (((char*)recbuf)[j] != sequence()) { fprintf(stderr,"error in data: size= %d, pos = %d!\n",size,j);fflush(stderr); exit(-1); } i += 128; } tcp_close_connections(conn1); return 1; }
/* * Pulled in from examples/server/server.c * Function to handle nonblocking. Loops until tcp_select notifies that it's * ready for action. */ int NonBlockingSSL(WOLFSSL* ssl) { int ret; int error; int select_ret; int sockfd = wolfSSL_get_fd(ssl); ret = wolfSSL_accept(ssl); error = wolfSSL_get_error(ssl, 0); while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)) { int currTimeout = 1; /* print out for user notification */ if (error == SSL_ERROR_WANT_READ) printf("... server would read block\n"); else printf("... server would write block\n"); select_ret = tcp_select(sockfd, currTimeout); /* if tcp_select signals ready try to accept otherwise continue loop*/ if ((select_ret == TEST_RECV_READY) || (select_ret == TEST_ERROR_READY)) { ret = wolfSSL_accept(ssl); error = wolfSSL_get_error(ssl, 0); } else if (select_ret == TEST_TIMEOUT) { error = SSL_ERROR_WANT_READ; } else { error = SSL_FATAL_ERROR; } } /* faliure to accept */ if (ret != SSL_SUCCESS) { printf("Fatal error : SSL_accept failed\n"); ret = SSL_FATAL_ERROR; } return ret; }
/* * sets up and uses nonblocking protocols using wolfssl */ static int NonBlockingSSL_Connect(WOLFSSL* ssl) { int ret, error, sockfd, select_ret, currTimeout; ret = wolfSSL_connect(ssl); error = wolfSSL_get_error(ssl, 0); sockfd = (int)wolfSSL_get_fd(ssl); while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)) { currTimeout = 1; if (error == SSL_ERROR_WANT_READ) printf("... client would read block\n"); else printf("... client would write block\n"); select_ret = tcp_select(sockfd, currTimeout); if ((select_ret == TEST_RECV_READY) || (select_ret == TEST_ERROR_READY)) { ret = wolfSSL_connect(ssl); error = wolfSSL_get_error(ssl, 0); } else if (select_ret == TEST_TIMEOUT) { error = SSL_ERROR_WANT_READ; } else { error = SSL_FATAL_ERROR; } } if (ret != SSL_SUCCESS){ printf("SSL_connect failed"); return 1; } return 0; }
int device_select(struct timeval *timeout, struct gn_statemachine *state) { switch (state->device.type) { case GN_CT_DKU2: case GN_CT_Serial: case GN_CT_Infrared: return serial_select(state->device.fd, timeout, state); case GN_CT_Irda: return irda_select(state->device.fd, timeout, state); case GN_CT_Bluetooth: return bluetooth_select(state->device.fd, timeout, state); case GN_CT_Tekram: return tekram_select(state->device.fd, timeout, state); case GN_CT_TCP: return tcp_select(state->device.fd, timeout, state); case GN_CT_DKU2LIBUSB: return fbusdku2usb_select(timeout, state); case GN_CT_SOCKETPHONET: return socketphonet_select(state->device.fd, timeout, state); default: break; } return -1; }
/** * This function initializes the tcp context global instance to 0 except for * file descriptor descriptor for stdin, stdout and stderr. * * @author Mohd Naushad Ahmed * @version 1.0 * @param none * @return none */ int main (int argc, char *argv[]) { tcp_instance_create("192.168.3.1",8989,tcp_process_request_cb); tcp_select(); }
/* handle SSH echo operations * returns 0 on success */ static int ssh_worker(thread_ctx_t* threadCtx) { byte* buf = NULL; byte* tmpBuf; int bufSz, backlogSz = 0, rxSz, txSz, stop = 0, txSum; do { bufSz = EXAMPLE_BUFFER_SZ + backlogSz; tmpBuf = (byte*)realloc(buf, bufSz); if (tmpBuf == NULL) stop = 1; else buf = tmpBuf; if (!stop) { if (threadCtx->nonBlock) { SOCKET_T sockfd; int select_ret = 0; sockfd = (SOCKET_T)wolfSSH_get_fd(threadCtx->ssh); select_ret = tcp_select(sockfd, 1); if (select_ret != WS_SELECT_RECV_READY && select_ret != WS_SELECT_ERROR_READY && select_ret != WS_SELECT_TIMEOUT) { break; } } rxSz = wolfSSH_stream_read(threadCtx->ssh, buf + backlogSz, EXAMPLE_BUFFER_SZ); if (rxSz > 0) { backlogSz += rxSz; txSum = 0; txSz = 0; while (backlogSz != txSum && txSz >= 0 && !stop) { txSz = wolfSSH_stream_send(threadCtx->ssh, buf + txSum, backlogSz - txSum); if (txSz > 0) { byte c; const byte matches[] = { 0x03, 0x05, 0x06, 0x00 }; c = find_char(matches, buf + txSum, txSz); switch (c) { case 0x03: stop = 1; break; case 0x06: if (wolfSSH_TriggerKeyExchange(threadCtx->ssh) != WS_SUCCESS) stop = 1; break; case 0x05: if (dump_stats(threadCtx) <= 0) stop = 1; break; } txSum += txSz; } else if (txSz != WS_REKEYING) { int error = wolfSSH_get_error(threadCtx->ssh); if (error != WS_WANT_WRITE) { stop = 1; } else { txSz = 0; } } } if (txSum < backlogSz) memmove(buf, buf + txSum, backlogSz - txSum); backlogSz -= txSum; } else { int error = wolfSSH_get_error(threadCtx->ssh); if (error != WS_WANT_READ) stop = 1; } } } while (!stop); free(buf); return 0; }
int ClientBenchmarkThroughput(WOLFSSL_CTX* ctx, char* host, word16 port, int doDTLS, int throughput) { double start, conn_time = 0, tx_time = 0, rx_time = 0; SOCKET_T sockfd; WOLFSSL* ssl; int ret; start = current_time(); ssl = wolfSSL_new(ctx); if (ssl == NULL) err_sys("unable to get SSL object"); tcp_connect(&sockfd, host, port, doDTLS, ssl); wolfSSL_set_fd(ssl, sockfd); if (wolfSSL_connect(ssl) == SSL_SUCCESS) { /* Perform throughput test */ char *tx_buffer, *rx_buffer; /* Record connection time */ conn_time = current_time() - start; /* Allocate TX/RX buffers */ tx_buffer = (char*)malloc(TEST_BUFFER_SIZE); rx_buffer = (char*)malloc(TEST_BUFFER_SIZE); if(tx_buffer && rx_buffer) { WC_RNG rng; /* Startup the RNG */ ret = wc_InitRng(&rng); if(ret == 0) { int xfer_bytes; /* Generate random data to send */ ret = wc_RNG_GenerateBlock(&rng, (byte*)tx_buffer, TEST_BUFFER_SIZE); wc_FreeRng(&rng); if(ret != 0) { err_sys("wc_RNG_GenerateBlock failed"); } /* Perform TX and RX of bytes */ xfer_bytes = 0; while(throughput > xfer_bytes) { int len, rx_pos, select_ret; /* Determine packet size */ len = min(TEST_BUFFER_SIZE, throughput - xfer_bytes); /* Perform TX */ start = current_time(); if (wolfSSL_write(ssl, tx_buffer, len) != len) { int writeErr = wolfSSL_get_error(ssl, 0); printf("wolfSSL_write error %d!\n", writeErr); err_sys("wolfSSL_write failed"); } tx_time += current_time() - start; /* Perform RX */ select_ret = tcp_select(sockfd, 1); /* Timeout=1 second */ if (select_ret == TEST_RECV_READY) { start = current_time(); rx_pos = 0; while(rx_pos < len) { ret = wolfSSL_read(ssl, &rx_buffer[rx_pos], len - rx_pos); if(ret <= 0) { int readErr = wolfSSL_get_error(ssl, 0); if (readErr != SSL_ERROR_WANT_READ) { printf("wolfSSL_read error %d!\n", readErr); err_sys("wolfSSL_read failed"); } } else { rx_pos += ret; } } rx_time += current_time() - start; } /* Compare TX and RX buffers */ if(XMEMCMP(tx_buffer, rx_buffer, len) != 0) { err_sys("Compare TX and RX buffers failed"); } /* Update overall position */ xfer_bytes += len; } } else { err_sys("wc_InitRng failed"); } } else { err_sys("Client buffer malloc failed"); } if(tx_buffer) free(tx_buffer); if(rx_buffer) free(rx_buffer); } else { err_sys("wolfSSL_connect failed"); } wolfSSL_shutdown(ssl); wolfSSL_free(ssl); CloseSocket(sockfd); printf("wolfSSL Client Benchmark %d bytes\n" "\tConnect %8.3f ms\n" "\tTX %8.3f ms (%8.3f MBps)\n" "\tRX %8.3f ms (%8.3f MBps)\n", throughput, conn_time * 1000, tx_time * 1000, throughput / tx_time / 1024 / 1024, rx_time * 1000, throughput / rx_time / 1024 / 1024 ); return EXIT_SUCCESS; }
/* メイン処理 */ main(int argc, char **argv) { int i; int ret; int stdbuf_len; time_t tm; char stdbuf[MAX_STDIN_BUF]; signal(SIGPIPE, SigHandler); /* デフォルト値設定 */ com_ReadConfig(CONFIG_FILE); strcpy(config_name,CONFIG_FILE); strcpy(server_name,LOGTRAIN_SERVER); server_port=LOGTRAIN_PORT; strcpy(cache_dir,CACHE_DIR); connect_timeout=CONNECT_TIMEOUT; connect_err_wait=CONNECT_ERR_WAIT; read_timeout=READ_TIMEOUT; write_timeout=WRITE_TIMEOUT; cache_limit=CACHE_LIMIT; /* パラメータ解析 */ for ( i=1; i < argc; i++) { if ( strcmp(argv[i],"-d") == 0 ) { debug_flg=1; continue; } if ( i < argc -1 ) { if ( strcmp(argv[i],"-f") == 0 ) { strcpy(config_name,argv[++i]); com_ReadConfig(config_name); continue; } if ( strcmp(argv[i],"-h") == 0 ) { strcpy(server_name,argv[++i]); continue; } if ( strcmp(argv[i],"-p") == 0 ) { server_port=atoi(argv[++i]); continue; } if ( strcmp(argv[i],"-c") == 0 ) { strcpy(cache_dir,argv[++i]); continue; } if ( strcmp(argv[i],"-ct") == 0 ) { connect_timeout=atoi(argv[++i]); continue; } if ( strcmp(argv[i],"-rt") == 0 ) { read_timeout=atoi(argv[++i]); continue; } if ( strcmp(argv[i],"-wt") == 0 ) { write_timeout=atoi(argv[++i]); continue; } } if ( argv[i][0] == '-' ) { usage(argv[0]); exit(1); } } if ( com_fileCheck(cache_dir) != 1 ) { printf("no directory (%s)\n",cache_dir); exit(1); } com_logPrint("-------parameter----------"); com_logPrint("config = %s",config_name); com_logPrint("server = %s",server_name); com_logPrint("port = %d",server_port); com_logPrint("cache_dir = %s",cache_dir); com_logPrint("connect timeout = %d",connect_timeout); com_logPrint("connect err wait = %d",connect_err_wait); com_logPrint("read timeout = %d",read_timeout); com_logPrint("write timeout = %d",write_timeout); com_logPrint("--------------------------"); /* サーバ接続 */ LogTrainTcpConnect(); ret=tcp_select(1,connect_timeout); if ( ret != 1 ) { com_logPrint("connect error"); exit(1); } /* キャッシュファイル検索とリモート転送処理 */ LogTrainTrans(cache_dir); LogTrainTcpClose(); exit(0); }