From e0d86b1c72113786efb6fc4474642f53956e71d2 Mon Sep 17 00:00:00 2001 From: Jeff Squyres Date: Wed, 21 Mar 2018 13:48:51 -0700 Subject: [PATCH] opal/util/fd: add opal_fd_get_peer_name(() Returns a string name (either a resolved name or IPv4/IPv6 name in a string if unresolvable. The caller is responsible for freeing the string. Signed-off-by: Jeff Squyres --- opal/util/fd.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++--- opal/util/fd.h | 11 +++++++++- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/opal/util/fd.c b/opal/util/fd.c index a74b92bc2d..398f93276e 100644 --- a/opal/util/fd.c +++ b/opal/util/fd.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008-2014 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2008-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2009 Sandia National Laboratories. All rights reserved. * Copyright (c) 2017 Mellanox Technologies. All rights reserved. * @@ -18,13 +18,19 @@ #ifdef HAVE_SYS_STAT_H #include #endif - - +#ifdef HAVE_SYS_SOCKET_H +#include +#endif +#ifdef HAVE_ARPA_INET_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif #include #include +#include +#include #include "opal/util/fd.h" #include "opal/constants.h" @@ -126,3 +132,49 @@ bool opal_fd_is_blkdev(int fd) return S_ISBLK(buf.st_mode); } +const char *opal_fd_get_peer_name(int fd) +{ + char *str; + const char *ret; + struct sockaddr sa; + socklen_t slt = (socklen_t) sizeof(sa); + + int rc = getpeername(fd, &sa, &slt); + if (0 != rc) { + ret = strdup("Unknown"); + return ret; + } + + size_t len = INET_ADDRSTRLEN; +#if OPAL_ENABLE_IPV6 + len = INET6_ADDRSTRLEN; +#endif + str = malloc(len); + if (NULL == str) { + return NULL; + } + + if (sa.sa_family == AF_INET) { + struct sockaddr_in *si; + si = (struct sockaddr_in*) &sa; + ret = inet_ntop(AF_INET, &(si->sin_addr), str, INET_ADDRSTRLEN); + if (NULL == ret) { + free(str); + } + } +#if OPAL_ENABLE_IPV6 + else if (sa.sa_family == AF_INET6) { + struct sockaddr_in6 *si6; + si6 = (struct sockaddr_in6*) &sa; + ret = inet_ntop(AF_INET6, &(si6->sin6_addr), str, INET6_ADDRSTRLEN); + if (NULL == ret) { + free(str); + } + } +#endif + else { + ret = strdup("Unknown"); + } + + return ret; +} diff --git a/opal/util/fd.h b/opal/util/fd.h index ea8a7a1a0e..168690b5cf 100644 --- a/opal/util/fd.h +++ b/opal/util/fd.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008-2014 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2008-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2009 Sandia National Laboratories. All rights reserved. * Copyright (c) 2017 Mellanox Technologies. All rights reserved. * @@ -94,6 +94,15 @@ OPAL_DECLSPEC bool opal_fd_is_chardev(int fd); */ OPAL_DECLSPEC bool opal_fd_is_blkdev(int fd); +/** + * Convenience function to get a string name of the peer on the other + * end of this internet socket. + * + * @param fd File descriptor of an AF_INET/AF_INET6 socket + * + * @returns resolvable IP name, or "a.b.c.d". This string must be freed by the caller. + */ +OPAL_DECLSPEC const char *opal_fd_get_peer_name(int fd); END_C_DECLS