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 <jsquyres@cisco.com>
Этот коммит содержится в:
родитель
c23dff24bc
Коммит
e0d86b1c72
@ -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 <sys/stat.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef HAVE_SYS_SOCKET_H
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
#ifdef HAVE_ARPA_INET_H
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
Загрузка…
Ссылка в новой задаче
Block a user