From 39f6fc00f845e6f98ad51f88d30bd7d5b42ce19f Mon Sep 17 00:00:00 2001 From: Asanka Herath Date: Mon, 14 Sep 2009 15:05:13 -0400 Subject: [PATCH] krb5_net_read(), krb5_net_write() nad krb5_storage_from_fd() should accept both sockets and fds When a socket and a file descriptor aren't interchangeable, these functions should be able to determine whether it has received one or the other and act accordingly. This assumes that a fd can be cast into a SOCKET. --- lib/krb5/net_read.c | 14 ++++++++++++++ lib/krb5/net_write.c | 14 ++++++++++++++ lib/krb5/store_fd.c | 18 ++++++++++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/krb5/net_read.c b/lib/krb5/net_read.c index 5a8045988..512a3ccef 100644 --- a/lib/krb5/net_read.c +++ b/lib/krb5/net_read.c @@ -41,5 +41,19 @@ krb5_net_read (krb5_context context, { SOCKET fd = *((SOCKET *)p_fd); +#ifdef SOCKET_IS_NOT_AN_FD +#ifdef _MSC_VER + { + HANDLE h = _get_osfhandle(fd); + + if (h != INVALID_HANDLE_VALUE) { + return net_read (fd, buf, len); + } + } +#else +#error Don't know how to handle socket that may be an fd +#endif +#endif + return net_read_s (fd, buf, len); } diff --git a/lib/krb5/net_write.c b/lib/krb5/net_write.c index 6377957ac..1561a4440 100644 --- a/lib/krb5/net_write.c +++ b/lib/krb5/net_write.c @@ -41,6 +41,20 @@ krb5_net_write (krb5_context context, { SOCKET fd = *((SOCKET *)p_fd); +#ifdef SOCKET_IS_NOT_AN_FD +#ifdef _MSC_VER + { + HANDLE h = _get_osfhandle(fd); + + if (h != INVALID_HANDLE_VALUE) { + return net_write (fd, buf, len); + } + } +#else +#error Don't know how to handle SOCKET that may be an fd +#endif +#endif + return net_write_s (fd, buf, len); } diff --git a/lib/krb5/store_fd.c b/lib/krb5/store_fd.c index 2a3fdaf60..a8974b649 100644 --- a/lib/krb5/store_fd.c +++ b/lib/krb5/store_fd.c @@ -86,11 +86,25 @@ fd_free(krb5_storage * sp) */ KRB5_LIB_FUNCTION krb5_storage * KRB5_LIB_CALL -krb5_storage_from_fd(int fd) +krb5_storage_from_fd(SOCKET fd_in) { krb5_storage *sp; + int fd; + +#ifdef SOCKET_IS_NOT_AN_FD +#ifdef _MSC_VER + if (_get_osfhandle(fd_in) != -1) { + fd = dup(fd_in); + } else { + fd = _open_osfhandle(fd_in, 0); + } +#else +#error Don't know how to deal with fd that may or may not be a socket. +#endif +#else /* SOCKET_IS_NOT_AN_FD */ + fd = dup(fd_in); +#endif - fd = dup(fd); if (fd < 0) return NULL;