mirror of
https://github.com/owntone/owntone-server.git
synced 2024-12-28 08:05:56 -05:00
[misc] Add utility function to get interface name from IP address
This commit is contained in:
parent
680c27eb66
commit
3754871c73
40
src/misc.c
40
src/misc.c
@ -50,6 +50,7 @@
|
|||||||
|
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <ifaddrs.h> // getifaddrs
|
||||||
|
|
||||||
#include <unistr.h>
|
#include <unistr.h>
|
||||||
#include <uniconv.h>
|
#include <uniconv.h>
|
||||||
@ -160,13 +161,13 @@ net_peer_address_is_trusted(const char *addr)
|
|||||||
int
|
int
|
||||||
net_address_get(char *addr, size_t addr_len, union net_sockaddr *naddr)
|
net_address_get(char *addr, size_t addr_len, union net_sockaddr *naddr)
|
||||||
{
|
{
|
||||||
const char *s;
|
const char *s = NULL;
|
||||||
|
|
||||||
memset(addr, 0, addr_len); // Just in case caller doesn't check for errors
|
memset(addr, 0, addr_len); // Just in case caller doesn't check for errors
|
||||||
|
|
||||||
if (naddr->sa.sa_family == AF_INET6)
|
if (naddr->sa.sa_family == AF_INET6)
|
||||||
s = inet_ntop(AF_INET6, &naddr->sin6.sin6_addr, addr, addr_len);
|
s = inet_ntop(AF_INET6, &naddr->sin6.sin6_addr, addr, addr_len);
|
||||||
else
|
else if (naddr->sa.sa_family == AF_INET)
|
||||||
s = inet_ntop(AF_INET, &naddr->sin.sin_addr, addr, addr_len);
|
s = inet_ntop(AF_INET, &naddr->sin.sin_addr, addr, addr_len);
|
||||||
|
|
||||||
if (!s)
|
if (!s)
|
||||||
@ -180,12 +181,45 @@ net_port_get(short unsigned *port, union net_sockaddr *naddr)
|
|||||||
{
|
{
|
||||||
if (naddr->sa.sa_family == AF_INET6)
|
if (naddr->sa.sa_family == AF_INET6)
|
||||||
*port = ntohs(naddr->sin6.sin6_port);
|
*port = ntohs(naddr->sin6.sin6_port);
|
||||||
else
|
else if (naddr->sa.sa_family == AF_INET)
|
||||||
*port = ntohs(naddr->sin.sin_port);
|
*port = ntohs(naddr->sin.sin_port);
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
net_if_get(char *ifname, size_t ifname_len, const char *addr)
|
||||||
|
{
|
||||||
|
struct ifaddrs *ifaddrs;
|
||||||
|
struct ifaddrs *iap;
|
||||||
|
char s[64];
|
||||||
|
|
||||||
|
memset(ifname, 0, ifname_len);
|
||||||
|
|
||||||
|
getifaddrs(&ifaddrs);
|
||||||
|
|
||||||
|
for (iap = ifaddrs; iap; iap = iap->ifa_next)
|
||||||
|
{
|
||||||
|
if (!iap->ifa_addr)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (net_address_get(s, sizeof(s), (union net_sockaddr *)iap->ifa_addr) < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (strcmp(s, addr) != 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
snprintf(ifname, ifname_len, "%s", iap->ifa_name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
freeifaddrs(ifaddrs);
|
||||||
|
|
||||||
|
return (ifname[0] != 0) ? 0 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
net_connect(const char *addr, unsigned short port, int type, const char *log_service_name)
|
net_connect(const char *addr, unsigned short port, int type, const char *log_service_name)
|
||||||
{
|
{
|
||||||
|
@ -43,6 +43,9 @@ net_address_get(char *addr, size_t addr_len, union net_sockaddr *naddr);
|
|||||||
int
|
int
|
||||||
net_port_get(short unsigned *port, union net_sockaddr *naddr);
|
net_port_get(short unsigned *port, union net_sockaddr *naddr);
|
||||||
|
|
||||||
|
int
|
||||||
|
net_if_get(char *ifname, size_t ifname_len, const char *addr);
|
||||||
|
|
||||||
// Returns the socket fd from socket(), -1 on error
|
// Returns the socket fd from socket(), -1 on error
|
||||||
int
|
int
|
||||||
net_connect(const char *addr, unsigned short port, int type, const char *log_service_name);
|
net_connect(const char *addr, unsigned short port, int type, const char *log_service_name);
|
||||||
|
Loading…
Reference in New Issue
Block a user