Spdk/module/sock/sock_kernel.h
paul luse a6dbe3721e update Intel copyright notices
per Intel policy to include file commit date using git cmd
below.  The policy does not apply to non-Intel (C) notices.

git log --follow -C90% --format=%ad --date default <file> | tail -1

and then pull just the 4 digit year from the result.

Intel copyrights were not added to files where Intel either had
no contribution ot the contribution lacked substance (ie license
header updates, formatting changes, etc).  Contribution date used
"--follow -C95%" to get the most accurate date.

Note that several files in this patch didn't end the license/(c)
block with a blank comment line so these were added as the vast
majority of files do have this last blank line.  Simply there for
consistency.

Signed-off-by: paul luse <paul.e.luse@intel.com>
Change-Id: Id5b7ce4f658fe87132f14139ead58d6e285c04d4
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/15192
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Community-CI: Mellanox Build Bot
2022-11-10 08:28:53 +00:00

82 lines
1.7 KiB
C

/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (C) 2021 Intel Corporation. All rights reserved.
*/
static int
get_addr_str(struct sockaddr *sa, char *host, size_t hlen)
{
const char *result = NULL;
if (sa == NULL || host == NULL) {
return -1;
}
switch (sa->sa_family) {
case AF_INET:
result = inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
host, hlen);
break;
case AF_INET6:
result = inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
host, hlen);
break;
default:
break;
}
if (result != NULL) {
return 0;
} else {
return -1;
}
}
static bool
sock_is_loopback(int fd)
{
struct ifaddrs *addrs, *tmp;
struct sockaddr_storage sa = {};
socklen_t salen;
struct ifreq ifr = {};
char ip_addr[256], ip_addr_tmp[256];
int rc;
bool is_loopback = false;
salen = sizeof(sa);
rc = getsockname(fd, (struct sockaddr *)&sa, &salen);
if (rc != 0) {
return is_loopback;
}
memset(ip_addr, 0, sizeof(ip_addr));
rc = get_addr_str((struct sockaddr *)&sa, ip_addr, sizeof(ip_addr));
if (rc != 0) {
return is_loopback;
}
getifaddrs(&addrs);
for (tmp = addrs; tmp != NULL; tmp = tmp->ifa_next) {
if (tmp->ifa_addr && (tmp->ifa_flags & IFF_UP) &&
(tmp->ifa_addr->sa_family == sa.ss_family)) {
memset(ip_addr_tmp, 0, sizeof(ip_addr_tmp));
rc = get_addr_str(tmp->ifa_addr, ip_addr_tmp, sizeof(ip_addr_tmp));
if (rc != 0) {
continue;
}
if (strncmp(ip_addr, ip_addr_tmp, sizeof(ip_addr)) == 0) {
memcpy(ifr.ifr_name, tmp->ifa_name, sizeof(ifr.ifr_name));
ioctl(fd, SIOCGIFFLAGS, &ifr);
if (ifr.ifr_flags & IFF_LOOPBACK) {
is_loopback = true;
}
goto end;
}
}
}
end:
freeifaddrs(addrs);
return is_loopback;
}