xref: /spdk/include/spdk/net.h (revision b6875e1ce57743f3b1416016b9c624d79a862af9)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2024 Samsung Electonrics Co., Ltd.
3  *   All rights reserved.
4  */
5 
6 /** \file
7  * Network related helper functions
8  */
9 
10 #ifndef SPDK_NET_H
11 #define SPDK_NET_H
12 
13 #include "spdk/stdinc.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /**
20  * Gets the name of the network interface for the given IP address.
21  *
22  * \param ip IP address to find the interface name for
23  * \param ifc string output parameter for the interface name
24  * \param len length of the ifc parameter in bytes
25  *
26  * \return 0 if successful, the interface name will be copied to the ifc parameter
27  *         -ENODEV if an interface name could not be identified
28  *         -ENOMEM the provided ifc string was too small
29  */
30 int spdk_net_get_interface_name(const char *ip, char *ifc, size_t len);
31 
32 /**
33  * Gets the address string for a given struct sockaddr
34  *
35  * \param sa sockaddr to get the address string for
36  * \param addr string to put the address
37  * \param len length of the the addr parameter
38  *
39  * \return 0 if successful, negative -errno otherwise
40  */
41 int spdk_net_get_address_string(struct sockaddr *sa, char *addr, size_t len);
42 
43 /**
44  * Checks if the given fd is a loopback interface or not.
45  *
46  * \param fd file descriptor to check
47  *
48  * \return true if the fd is loopback, false if not
49  */
50 bool spdk_net_is_loopback(int fd);
51 
52 /*
53  * Get local and peer addresses of the given fd.
54  *
55  * \param fd file descriptor to get address.
56  * \param laddr A pointer (may be NULL) to the buffer to hold the local address.
57  * \param llen Length of the buffer 'laddr'.
58  * \param lport A pointer (may be NULL) to the buffer to hold the local port info.
59  * \param paddr A pointer (may be NULL) to the buffer to hold the peer address.
60  * \param plen Length of the buffer 'paddr'.
61  * \param pport A pointer (may be NULL) to the buffer to hold the peer port info.
62  *
63  * \return 0 on success, -1 on failure.
64  */
65 int spdk_net_getaddr(int fd, char *laddr, int llen, uint16_t *lport,
66 		     char *paddr, int plen, uint16_t *pport);
67 
68 #ifdef __cplusplus
69 }
70 #endif
71 
72 #endif /* SPDK_NET_H */
73