improve socket error reporting

always use getsockopt() to query the meaning of POLLERR, rather than
reporting "Unidentified socket error".
this is unlikely to have any effect when using select(), as that one
pretty much never signals exceptional conditions.
This commit is contained in:
Oswald Buddenhagen 2013-03-29 18:22:40 +01:00
parent 2ef6dc8a90
commit 7ba7be111e

View File

@ -297,7 +297,7 @@ static void start_tls_p3( conn_t *conn, int ok )
static void socket_fd_cb( int, void * ); static void socket_fd_cb( int, void * );
static void socket_connect_failed( conn_t * ); static void socket_connect_failed( conn_t * );
static void socket_connected2( conn_t * ); static void socket_connected( conn_t * );
static void socket_connect_bail( conn_t * ); static void socket_connect_bail( conn_t * );
static void static void
@ -388,7 +388,7 @@ socket_connect( conn_t *sock, void (*cb)( int ok, void *aux ) )
} }
info( "\vok\n" ); info( "\vok\n" );
socket_connected2( sock ); socket_connected( sock );
return; return;
} }
@ -402,24 +402,6 @@ socket_connect_failed( conn_t *conn )
static void static void
socket_connected( conn_t *conn ) socket_connected( conn_t *conn )
{
int soerr;
socklen_t selen = sizeof(soerr);
if (getsockopt( conn->fd, SOL_SOCKET, SO_ERROR, &soerr, &selen )) {
perror( "getsockopt" );
exit( 1 );
}
if (soerr) {
errno = soerr;
socket_connect_failed( conn );
return;
}
socket_connected2( conn );
}
static void
socket_connected2( conn_t *conn )
{ {
conf_fd( conn->fd, 0, POLLIN ); conf_fd( conn->fd, 0, POLLIN );
conn->state = SCK_READY; conn->state = SCK_READY;
@ -637,13 +619,22 @@ socket_fd_cb( int events, void *aux )
{ {
conn_t *conn = (conn_t *)aux; conn_t *conn = (conn_t *)aux;
if (conn->state == SCK_CONNECTING) { if ((events & POLLERR) || conn->state == SCK_CONNECTING) {
socket_connected( conn ); int soerr;
return; socklen_t selen = sizeof(soerr);
} if (getsockopt( conn->fd, SOL_SOCKET, SO_ERROR, &soerr, &selen )) {
perror( "getsockopt" );
if (events & POLLERR) { exit( 1 );
error( "Unidentified socket error from %s.\n", conn->name ); }
errno = soerr;
if (conn->state == SCK_CONNECTING) {
if (errno)
socket_connect_failed( conn );
else
socket_connected( conn );
return;
}
sys_error( "Socket error from %s", conn->name );
socket_fail( conn ); socket_fail( conn );
return; return;
} }