diff options
Diffstat (limited to 'sdk/net/sockname.c')
-rw-r--r-- | sdk/net/sockname.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/sdk/net/sockname.c b/sdk/net/sockname.c new file mode 100644 index 0000000..a8e9aad --- /dev/null +++ b/sdk/net/sockname.c @@ -0,0 +1,62 @@ +/* + * Portable socket and peer name services. + * $Id$ + * Copyright (c) 1997 by Tycho Softworks. + * For conditions of distribution and reuse see product license. + */ + +#include <net/socket.h> + +char *homename(SOCKET so) +{ + struct hostent *hp; + struct sockaddr_in sin; + int len = sizeof(sin); + + if(getsockname(so, (struct sockaddr *)&sin, &len) < 0) + return NULL; + + hp = gethostbyaddr((char *)&sin.sin_addr, sizeof(struct in_addr), AF_INET); + if(hp) + return (char *)hp->h_name; + else + return inet_ntoa(sin.sin_addr); +} + +char *homeaddr(SOCKET so) +{ + struct sockaddr_in sin; + int len = sizeof(sin); + + if(getsockname(so, (struct sockaddr *)&sin, &len) < 0) + return NULL; + + return inet_ntoa(sin.sin_addr); +} + +char *peername(SOCKET so) +{ + struct hostent *hp; + struct sockaddr_in sin; + int len = sizeof(sin); + + if(getpeername(so, (struct sockaddr *)&sin, &len) < 0) + return NULL; + + hp = gethostbyaddr((char *)&sin.sin_addr, sizeof(struct in_addr), AF_INET); + if(hp) + return (char *)hp->h_name; + else + return inet_ntoa(sin.sin_addr); +} + +char *peeraddr(SOCKET so) +{ + struct sockaddr_in sin; + int len = sizeof(sin); + + if(getpeername(so, (struct sockaddr *)&sin, &len) < 0) + return NULL; + + return inet_ntoa(sin.sin_addr); +} |