|
1 | 1 | /* |
2 | | - * Copyright (c) 2008-2014 Cisco Systems, Inc. All rights reserved. |
| 2 | + * Copyright (c) 2008-2018 Cisco Systems, Inc. All rights reserved |
3 | 3 | * Copyright (c) 2009 Sandia National Laboratories. All rights reserved. |
4 | 4 | * Copyright (c) 2017 Mellanox Technologies. All rights reserved. |
5 | 5 | * |
|
18 | 18 | #ifdef HAVE_SYS_STAT_H |
19 | 19 | #include <sys/stat.h> |
20 | 20 | #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 |
23 | 27 | #ifdef HAVE_UNISTD_H |
24 | 28 | #include <unistd.h> |
25 | 29 | #endif |
26 | 30 | #include <errno.h> |
27 | 31 | #include <fcntl.h> |
| 32 | +#include <stdlib.h> |
| 33 | +#include <string.h> |
28 | 34 |
|
29 | 35 | #include "opal/util/fd.h" |
30 | 36 | #include "opal/constants.h" |
@@ -126,3 +132,49 @@ bool opal_fd_is_blkdev(int fd) |
126 | 132 | return S_ISBLK(buf.st_mode); |
127 | 133 | } |
128 | 134 |
|
| 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 | +} |
0 commit comments