From: Theodore Tso Date: Fri, 4 Jul 1997 04:13:56 +0000 (+0000) Subject: Added Winsock kludge for finding your local IP address. May not work X-Git-Tag: krb5-1.1-beta1~1110 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8ba8a8717ea2d5bb9a70f6dec7f719ad3fb7a8f9;p=krb5.git Added Winsock kludge for finding your local IP address. May not work for all stacks, so we use it as a fallback. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@10114 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/lib/crypto/os/ChangeLog b/src/lib/crypto/os/ChangeLog index 3690df66f..d38c4f8a7 100644 --- a/src/lib/crypto/os/ChangeLog +++ b/src/lib/crypto/os/ChangeLog @@ -1,3 +1,9 @@ +Fri Jul 4 00:13:02 1997 Theodore Y. Ts'o + + * c_localaddr.c (local_addr_fallback_kludge): Added Winsock + kludge for finding your local IP address. May not work + for all stacks, so we use it as a fallback. + Sat Feb 22 18:54:53 1997 Richard Basch * Makefile.in: Use some of the new library list build rules in diff --git a/src/lib/crypto/os/c_localaddr.c b/src/lib/crypto/os/c_localaddr.c index 407631555..b85bfc0b2 100644 --- a/src/lib/crypto/os/c_localaddr.c +++ b/src/lib/crypto/os/c_localaddr.c @@ -241,10 +241,55 @@ n_found = 0; #else /* Windows/Mac version */ +/* + * Hold on to your lunch! Backup kludge method of obtaining your + * local IP address, courtesy of Windows Socket Network Programming, + * by Robert Quinn + */ +#if defined(_MSDOS) || !defined(_WIN32) +static struct hostent *local_addr_fallback_kludge() +{ + static struct hostent host; + static SOCKADDR_IN addr; + static char * ip_ptrs[2]; + SOCKET sock; + int size = sizeof(SOCKADDR); + int err; + + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock == INVALID_SOCKET) + return NULL; + + /* connect to arbitrary port and address (NOT loopback) */ + addr.sin_family = AF_INET; + addr.sin_port = htons(IPPORT_ECHO); + addr.sin_addr.s_addr = inet_addr("204.137.220.51"); + + err = connect(sock, (LPSOCKADDR) &addr, sizeof(SOCKADDR)); + if (err == SOCKET_ERROR) + return NULL; + + err = getsockname(sock, (LPSOCKADDR) &addr, (int FAR *) size); + if (err == SOCKET_ERROR) + return NULL; + + closesocket(sock); + + host.h_name = 0; + host.h_aliases = 0; + host.h_addrtype = AF_INET; + host.h_length = 4; + host.h_addr_list = ip_ptrs; + ip_ptrs[0] = (char *) &addr.sin_addr.s_addr; + ip_ptrs[1] = NULL; + + return &host; +} +#endif + /* No ioctls in winsock so we just assume there is only one networking * card per machine, so gethostent is good enough. */ - krb5_error_code krb5_crypto_os_localaddr (krb5_address ***addr) { char host[64]; /* Name of local machine */ @@ -258,15 +303,23 @@ krb5_crypto_os_localaddr (krb5_address ***addr) { #ifdef HAVE_MACSOCK_H hostrec = getmyipaddr(); #else /* HAVE_MACSOCK_H */ + err = 0; + if (gethostname (host, sizeof(host))) { err = WSAGetLastError(); - return err; } - hostrec = gethostbyname (host); - if (hostrec == NULL) { - err = WSAGetLastError(); - return err; + if (!err) { + hostrec = gethostbyname (host); + if (hostrec == NULL) { + err = WSAGetLastError(); + } + } + + if (err) { + hostrec = local_addr_fallback_kludge(); + if (!hostrec) + return err; } #endif /* HAVE_MACSOCK_H */