Commit e8671c89 authored by Jim Harris's avatar Jim Harris
Browse files

util: add spdk_net_get_interface_name



This function provides the name of the network interface for the
given IP address.

Signed-off-by: default avatarJim Harris <jim.harris@samsung.com>
Change-Id: Ib6d2c08c98d4b4b21c52b0b42a9703125c834f99
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/24177


Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Community-CI: Mellanox Build Bot
Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
parent 7798a257
Loading
Loading
Loading
Loading

include/spdk/net.h

0 → 100644
+36 −0
Original line number Diff line number Diff line
/*   SPDX-License-Identifier: BSD-3-Clause
 *   Copyright (C) 2024 Samsung Electonrics Co., Ltd.
 *   All rights reserved.
 */

/** \file
 * Network related helper functions
 */

#ifndef SPDK_NET_H
#define SPDK_NET_H

#include "spdk/stdinc.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Gets the name of the network interface for the given IP address.
 *
 * \param ip IP address to find the interface name for
 * \param ifc string output parameter for the interface name
 * \param len length of the ifc parameter in bytes
 *
 * \return 0 if successful, the interface name will be copied to the ifc parameter
 *         -ENODEV if an interface name could not be identified
 *         -ENOMEM the provided ifc string was too small
 */
int spdk_net_get_interface_name(const char *ip, char *ifc, size_t len);

#ifdef __cplusplus
}
#endif

#endif /* SPDK_NET_H */
+2 −2
Original line number Diff line number Diff line
@@ -10,8 +10,8 @@ SO_VER := 10
SO_MINOR := 0

C_SRCS = base64.c bit_array.c cpuset.c crc16.c crc32.c crc32c.c crc32_ieee.c crc64.c \
	 dif.c fd.c file.c hexlify.c iov.c math.c pipe.c strerror_tls.c string.c uuid.c \
	 fd_group.c xor.c zipf.c
	 dif.c fd.c fd_group.c file.c hexlify.c iov.c math.c net.c \
	 pipe.c strerror_tls.c string.c uuid.c xor.c zipf.c
LIBNAME = util

ifneq ($(OS),FreeBSD)

lib/util/net.c

0 → 100644
+39 −0
Original line number Diff line number Diff line
/*   SPDX-License-Identifier: BSD-3-Clause
 *   Copyright (C) 2021 Intel Corporation. All rights reserved.
 *   Copyright (C) 2024 Samsung Electronics Co., Ltd.
 *   All rights reserved.
 */

#include "spdk/stdinc.h"
#include "spdk/net.h"

int
spdk_net_get_interface_name(const char *ip, char *ifc, size_t len)
{
	struct ifaddrs *addrs, *iap;
	struct sockaddr_in *sa;
	char buf[32];
	int rc = -ENODEV;

	getifaddrs(&addrs);
	for (iap = addrs; iap != NULL; iap = iap->ifa_next) {
		if (!(iap->ifa_addr && (iap->ifa_flags & IFF_UP) && iap->ifa_addr->sa_family == AF_INET)) {
			continue;
		}
		sa = (struct sockaddr_in *)(iap->ifa_addr);
		inet_ntop(iap->ifa_addr->sa_family, &sa->sin_addr, buf, sizeof(buf));
		if (strcmp(ip, buf) != 0) {
			continue;
		}
		if (strnlen(iap->ifa_name, len) == len) {
			rc = -ENOMEM;
			goto ret;
		}
		snprintf(ifc, len, "%s", iap->ifa_name);
		rc = 0;
		break;
	}
ret:
	freeifaddrs(addrs);
	return rc;
}
+3 −0
Original line number Diff line number Diff line
@@ -104,6 +104,9 @@
	spdk_hexlify;
	spdk_unhexlify;

	# public functions in net.h
	spdk_net_get_interface_name;

	# public functions in pipe.h
	spdk_pipe_create;
	spdk_pipe_destroy;
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

DIRS-y = base64.c bit_array.c cpuset.c crc16.c crc32_ieee.c crc32c.c crc64.c dif.c \
	 file.c iov.c math.c pipe.c string.c xor.c
	 file.c iov.c math.c net.c pipe.c string.c xor.c

.PHONY: all clean $(DIRS-y)

Loading