Skip to content

Commit e0d86b1

Browse files
committed
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 <[email protected]>
1 parent c23dff2 commit e0d86b1

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

opal/util/fd.c

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 Cisco Systems, Inc. All rights reserved.
2+
* Copyright (c) 2008-2018 Cisco Systems, Inc. All rights reserved
33
* Copyright (c) 2009 Sandia National Laboratories. All rights reserved.
44
* Copyright (c) 2017 Mellanox Technologies. All rights reserved.
55
*
@@ -18,13 +18,19 @@
1818
#ifdef HAVE_SYS_STAT_H
1919
#include <sys/stat.h>
2020
#endif
21-
22-
21+
#ifdef HAVE_SYS_SOCKET_H
22+
#include <sys/socket.h>
23+
#endif
24+
#ifdef HAVE_ARPA_INET_H
25+
#include <arpa/inet.h>
26+
#endif
2327
#ifdef HAVE_UNISTD_H
2428
#include <unistd.h>
2529
#endif
2630
#include <errno.h>
2731
#include <fcntl.h>
32+
#include <stdlib.h>
33+
#include <string.h>
2834

2935
#include "opal/util/fd.h"
3036
#include "opal/constants.h"
@@ -126,3 +132,49 @@ bool opal_fd_is_blkdev(int fd)
126132
return S_ISBLK(buf.st_mode);
127133
}
128134

135+
const char *opal_fd_get_peer_name(int fd)
136+
{
137+
char *str;
138+
const char *ret;
139+
struct sockaddr sa;
140+
socklen_t slt = (socklen_t) sizeof(sa);
141+
142+
int rc = getpeername(fd, &sa, &slt);
143+
if (0 != rc) {
144+
ret = strdup("Unknown");
145+
return ret;
146+
}
147+
148+
size_t len = INET_ADDRSTRLEN;
149+
#if OPAL_ENABLE_IPV6
150+
len = INET6_ADDRSTRLEN;
151+
#endif
152+
str = malloc(len);
153+
if (NULL == str) {
154+
return NULL;
155+
}
156+
157+
if (sa.sa_family == AF_INET) {
158+
struct sockaddr_in *si;
159+
si = (struct sockaddr_in*) &sa;
160+
ret = inet_ntop(AF_INET, &(si->sin_addr), str, INET_ADDRSTRLEN);
161+
if (NULL == ret) {
162+
free(str);
163+
}
164+
}
165+
#if OPAL_ENABLE_IPV6
166+
else if (sa.sa_family == AF_INET6) {
167+
struct sockaddr_in6 *si6;
168+
si6 = (struct sockaddr_in6*) &sa;
169+
ret = inet_ntop(AF_INET6, &(si6->sin6_addr), str, INET6_ADDRSTRLEN);
170+
if (NULL == ret) {
171+
free(str);
172+
}
173+
}
174+
#endif
175+
else {
176+
ret = strdup("Unknown");
177+
}
178+
179+
return ret;
180+
}

opal/util/fd.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 Cisco Systems, Inc. All rights reserved.
2+
* Copyright (c) 2008-2018 Cisco Systems, Inc. All rights reserved
33
* Copyright (c) 2009 Sandia National Laboratories. All rights reserved.
44
* Copyright (c) 2017 Mellanox Technologies. All rights reserved.
55
*
@@ -94,6 +94,15 @@ OPAL_DECLSPEC bool opal_fd_is_chardev(int fd);
9494
*/
9595
OPAL_DECLSPEC bool opal_fd_is_blkdev(int fd);
9696

97+
/**
98+
* Convenience function to get a string name of the peer on the other
99+
* end of this internet socket.
100+
*
101+
* @param fd File descriptor of an AF_INET/AF_INET6 socket
102+
*
103+
* @returns resolvable IP name, or "a.b.c.d". This string must be freed by the caller.
104+
*/
105+
OPAL_DECLSPEC const char *opal_fd_get_peer_name(int fd);
97106

98107
END_C_DECLS
99108

0 commit comments

Comments
 (0)