/* * Write at most 'len' characters */ int net_send( void *ctx, const unsigned char *buf, size_t len ) { int ret = write( *((int *) ctx), buf, len ); if( ret < 0 ) { if( net_is_blocking() != 0 ) return( POLARSSL_ERR_NET_WANT_WRITE ); #if defined(_WIN32) || defined(_WIN32_WCE) if( WSAGetLastError() == WSAECONNRESET ) return( POLARSSL_ERR_NET_CONN_RESET ); #else if( errno == EPIPE || errno == ECONNRESET ) return( POLARSSL_ERR_NET_CONN_RESET ); if( errno == EINTR ) return( POLARSSL_ERR_NET_WANT_WRITE ); #endif return( POLARSSL_ERR_NET_SEND_FAILED ); } return( ret ); }
/* * Accept a connection from a remote client */ int net_accept( int bind_fd, int *client_fd, void *client_ip ) { struct sockaddr_in client_addr; #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \ defined(_SOCKLEN_T_DECLARED) socklen_t n = (socklen_t) sizeof( client_addr ); #else int n = (int) sizeof( client_addr ); #endif *client_fd = accept( bind_fd, (struct sockaddr *) &client_addr, &n ); if( *client_fd < 0 ) { if( net_is_blocking() != 0 ) return( POLARSSL_ERR_NET_WANT_READ ); return( POLARSSL_ERR_NET_ACCEPT_FAILED ); } if( client_ip != NULL ) memcpy( client_ip, &client_addr.sin_addr.s_addr, sizeof( client_addr.sin_addr.s_addr ) ); return( 0 ); }
/* * Read at most 'len' characters */ int net_recv( void *ctx, unsigned char *buf, int len ) { int ret = read( *((int *) ctx), buf, len ); if( len > 0 && ret == 0 ) return( POLARSSL_ERR_NET_CONN_RESET ); if( ret < 0 ) { if( net_is_blocking() != 0 ) return( POLARSSL_ERR_NET_TRY_AGAIN ); #if defined(WIN32) || defined(_WIN32_WCE) if( WSAGetLastError() == WSAECONNRESET ) return( POLARSSL_ERR_NET_CONN_RESET ); #else if( errno == EPIPE || errno == ECONNRESET ) return( POLARSSL_ERR_NET_CONN_RESET ); if( errno == EINTR ) return( POLARSSL_ERR_NET_TRY_AGAIN ); #endif return( POLARSSL_ERR_NET_RECV_FAILED ); } return( ret ); }
/* * Write at most 'len' characters */ int net_send( int fd, unsigned char *buf, int *len ) { int ret, n = 0; while( *len > 0 ) { if( ( ret = write( fd, buf + n, *len ) ) < 0 ) { if( net_is_blocking() != 0 ) return( ERR_NET_WOULD_BLOCK ); #if defined(WIN32) || defined(_WIN32_WCE) if( WSAGetLastError() == WSAECONNRESET ) return( ERR_NET_CONN_RESET ); #else if( errno == EINTR ) continue; if( errno == EPIPE ) return( ERR_NET_CONN_RESET ); #endif return( ERR_NET_SEND_FAILED ); } *len -= ret; n += ret; } return( 0 ); }
/* * Read at most 'len' characters */ int net_recv( int fd, unsigned char *buf, int *len ) { int ret, n; n = *len; *len = 0; while( *len < n ) { if( ( ret = read( fd, buf + *len, n - *len ) ) <= 0 ) { if( ret == 0 ) return( ERR_NET_CONN_RESET ); if( net_is_blocking() != 0 ) return( ERR_NET_WOULD_BLOCK ); #if defined(WIN32) || defined(_WIN32_WCE) if( WSAGetLastError() == WSAECONNRESET ) return( ERR_NET_CONN_RESET ); #else if( errno == EINTR ) continue; #endif return( ERR_NET_RECV_FAILED ); } *len += ret; } return( 0 ); }
/* * Accept a connection from a remote client */ int net_accept( int bind_fd, int *client_fd, unsigned char client_ip[4] ) { struct sockaddr_in client_addr; #ifdef __socklen_t_defined socklen_t n = (socklen_t) sizeof( client_addr ); #else int n = (int) sizeof( client_addr ); #endif *client_fd = accept( bind_fd, (struct sockaddr *) &client_addr, &n ); if( *client_fd < 0 ) { if( net_is_blocking() != 0 ) return( ERR_NET_WOULD_BLOCK ); return( ERR_NET_ACCEPT_FAILED ); } if( client_ip != NULL ) memcpy( client_ip, &client_addr.sin_addr.s_addr, 4 ); return( 0 ); }
/* Accept a connection from a remote client */ int net_accept(int bind_fd, int *client_fd, void *client_ip) { struct sockaddr_in client_addr; #if defined(__socklen_t_defined) || 1 socklen_t n = (socklen_t) sizeof(client_addr); #else int n = (int)sizeof(client_addr); #endif *client_fd = accept(bind_fd, (struct sockaddr *) &client_addr, &n); if (*client_fd < 0) { if (net_is_blocking() != 0) { return EST_ERR_NET_TRY_AGAIN; } return EST_ERR_NET_ACCEPT_FAILED; } if (client_ip != NULL) { memcpy(client_ip, &client_addr.sin_addr.s_addr, sizeof(client_addr.sin_addr.s_addr)); } return 0; }
/* Write at most 'len' characters */ int net_send(void *ctx, uchar *buf, int len) { int ret = send(*((int*)ctx), buf, len, 0); if (ret < 0) { if (net_is_blocking() != 0) { return EST_ERR_NET_TRY_AGAIN; } #if defined(WIN32) || defined(_WIN32_WCE) if (WSAGetLastError() == WSAECONNRESET) { return EST_ERR_NET_CONN_RESET; } #else if (errno == EPIPE || errno == ECONNRESET) { return EST_ERR_NET_CONN_RESET; } if (errno == EINTR) { return EST_ERR_NET_TRY_AGAIN; } #endif return EST_ERR_NET_SEND_FAILED; } return ret; }
/* * Write at most 'len' characters */ int net_send(void *ctx, unsigned char *buf, int len) { int ret = write(*((int *)ctx), buf, len); if (ret < 0) { if (net_is_blocking() != 0) return (TROPICSSL_ERR_NET_TRY_AGAIN); #if defined(WIN32) || defined(_WIN32_WCE) if (WSAGetLastError() == WSAECONNRESET) return (TROPICSSL_ERR_NET_CONN_RESET); #else if (errno == EPIPE || errno == ECONNRESET) return (TROPICSSL_ERR_NET_CONN_RESET); if (errno == EINTR) return (TROPICSSL_ERR_NET_TRY_AGAIN); #endif return (TROPICSSL_ERR_NET_SEND_FAILED); } return (ret); }