/* Try the sockaddrs until a connection succeeds */
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
for ( cur = addr_list; cur != NULL; cur = cur->ai_next ) {
- ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
- cur->ai_protocol );
- if ( ctx->fd < 0 ) {
+ int fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
+
+ if ( fd < 0 ) {
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}
- if ( connect( ctx->fd, cur->ai_addr, cur->ai_addrlen ) == 0 ) {
+ if ( connect( fd, cur->ai_addr, cur->ai_addrlen ) == 0 ) {
+ ctx->fd = fd; // connected!
ret = 0;
break;
}
- close( ctx->fd );
+ close( fd );
ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
}
/* Try the sockaddrs until a binding succeeds */
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
for ( cur = addr_list; cur != NULL; cur = cur->ai_next ) {
- ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
- cur->ai_protocol );
- if ( ctx->fd < 0 ) {
+ int fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
+ if ( fd < 0 ) {
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}
/*SO_REUSEADDR option dafault is disable in source code(lwip)*/
#if SO_REUSE
int n = 1;
- if ( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
+ if ( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &n, sizeof( n ) ) != 0 ) {
- close( ctx->fd );
+ close( fd );
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}
struct sockaddr_in *serv_addr = NULL;
serv_addr = (struct sockaddr_in *)cur->ai_addr;
serv_addr->sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
- if ( bind( ctx->fd, (struct sockaddr *)serv_addr, cur->ai_addrlen ) != 0 ) {
- close( ctx->fd );
+ if ( bind( fd, (struct sockaddr *)serv_addr, cur->ai_addrlen ) != 0 ) {
+ close( fd );
ret = MBEDTLS_ERR_NET_BIND_FAILED;
continue;
}
/* Listen only makes sense for TCP */
if ( proto == MBEDTLS_NET_PROTO_TCP ) {
- if ( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 ) {
- close( ctx->fd );
+ if ( listen( fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 ) {
+ close( fd );
ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
continue;
}
}
/* I we ever get there, it's a success */
+ ctx->fd = fd;
ret = 0;
break;
}