diff --git a/lib/isc/include/isc/result.h b/lib/isc/include/isc/result.h index c7262d9081..b6ab4b832a 100644 --- a/lib/isc/include/isc/result.h +++ b/lib/isc/include/isc/result.h @@ -42,7 +42,7 @@ #define ISC_R_EXISTS 18 #define ISC_R_NOSPACE 19 /* ran out of space */ #define ISC_R_CANCELED 20 -/* AVAILABLE CODE 21 */ +#define ISC_R_NOTBOUND 21 /* socket is not bound */ #define ISC_R_SHUTTINGDOWN 22 /* shutting down */ #define ISC_R_NOTFOUND 23 #define ISC_R_UNEXPECTEDEND 24 /* unexpected end of input */ @@ -61,8 +61,9 @@ #define ISC_R_MASKNONCONTIG 37 #define ISC_R_FILENOTFOUND 38 #define ISC_R_FILEEXISTS 39 +#define ISC_R_NOTCONNECTED 40 /* socket is not connected */ -#define ISC_R_NRESULTS 40 /* Number of results */ +#define ISC_R_NRESULTS 41 /* Number of results */ ISC_LANG_BEGINDECLS diff --git a/lib/isc/result.c b/lib/isc/result.c index 9f3c3ebee5..6b1930fae3 100644 --- a/lib/isc/result.c +++ b/lib/isc/result.c @@ -57,7 +57,7 @@ static char *text[ISC_R_NRESULTS] = { "already exists", /* 18 */ "ran out of space", /* 19 */ "operation canceled", /* 20 */ - "", /* 21 */ + "socket is not bound", /* 21 */ "shutting down", /* 22 */ "not found", /* 23 */ "unexpected end of input", /* 24 */ @@ -75,7 +75,8 @@ static char *text[ISC_R_NRESULTS] = { "ignore", /* 36 */ "address mask not contiguous", /* 37 */ "file not found", /* 38 */ - "file already exists" /* 39 */ + "file already exists", /* 39 */ + "socket is not connected" /* 40 */ }; #define ISC_RESULT_RESULTSET 2 diff --git a/lib/isc/unix/socket.c b/lib/isc/unix/socket.c index 81768e3f6e..e2f2d8e24a 100644 --- a/lib/isc/unix/socket.c +++ b/lib/isc/unix/socket.c @@ -2887,38 +2887,55 @@ internal_connect(isc_task_t *me, isc_event_t *ev) isc_result_t isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp) { + isc_result_t ret; + REQUIRE(VALID_SOCKET(sock)); REQUIRE(addressp != NULL); LOCK(&sock->lock); - *addressp = sock->address; + if (sock->connected) { + *addressp = sock->address; + ret = ISC_R_SUCCESS; + } else { + ret = ISC_R_NOTCONNECTED; + } UNLOCK(&sock->lock); - return (ISC_R_SUCCESS); + return (ret); } isc_result_t isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp) { ISC_SOCKADDR_LEN_T len; + isc_result_t ret; REQUIRE(VALID_SOCKET(sock)); REQUIRE(addressp != NULL); LOCK(&sock->lock); + if (!sock->bound) { + ret = ISC_R_NOTBOUND; + goto out; + } + + ret = ISC_R_SUCCESS; + len = sizeof addressp->type; if (getsockname(sock->fd, &addressp->type.sa, (void *)&len) < 0) { UNEXPECTED_ERROR(__FILE__, __LINE__, "getsockname: %s", strerror(errno)); - UNLOCK(&sock->lock); - return (ISC_R_UNEXPECTED); + ret = ISC_R_UNEXPECTED; + goto out; } addressp->length = (unsigned int)len; + out: UNLOCK(&sock->lock); + return (ISC_R_SUCCESS); }