From 30a88002105f562a1420520b8c85cc8cffd2f4fa Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 16 Jun 2008 18:45:17 +0000 Subject: add new functions pa_socket_is_local() and pa_socket_address_is_local() git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@2532 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/pulsecore/socket-util.c | 39 ++++++++++++++++++++++++++++++++++++++- src/pulsecore/socket-util.h | 6 ++++++ 2 files changed, 44 insertions(+), 1 deletion(-) (limited to 'src/pulsecore') diff --git a/src/pulsecore/socket-util.c b/src/pulsecore/socket-util.c index 456accb8..a92d50a5 100644 --- a/src/pulsecore/socket-util.c +++ b/src/pulsecore/socket-util.c @@ -129,8 +129,8 @@ void pa_socket_peer_to_string(int fd, char *c, size_t l) { return; #endif } - } + #ifndef OS_IS_WIN32 pa_snprintf(c, l, "Unknown network client"); return; @@ -284,3 +284,40 @@ int pa_unix_socket_remove_stale(const char *fn) { } #endif /* HAVE_SYS_UN_H */ + + +pa_bool_t pa_socket_address_is_local(const struct sockaddr *sa) { + pa_assert(sa); + + switch (sa->sa_family) { + case AF_UNIX: + return TRUE; + + case AF_INET: + return ((const struct sockaddr_in*) sa)->sin_addr.s_addr == INADDR_LOOPBACK; + + case AF_INET6: + return memcmp(&((const struct sockaddr_in6*) sa)->sin6_addr, &in6addr_loopback, sizeof(struct in6_addr)) == 0; + + default: + return FALSE; + } +} + +pa_bool_t pa_socket_is_local(int fd) { + + union { + struct sockaddr sa; + struct sockaddr_in in; + struct sockaddr_in6 in6; +#ifdef HAVE_SYS_UN_H + struct sockaddr_un un; +#endif + } sa; + socklen_t sa_len = sizeof(sa); + + if (getpeername(fd, &sa.sa, &sa_len) < 0) + return FALSE; + + return pa_socket_address_is_local(&sa.sa); +} diff --git a/src/pulsecore/socket-util.h b/src/pulsecore/socket-util.h index a0344c68..eeddf989 100644 --- a/src/pulsecore/socket-util.h +++ b/src/pulsecore/socket-util.h @@ -26,6 +26,9 @@ ***/ #include +#include + +#include void pa_socket_peer_to_string(int fd, char *c, size_t l); @@ -39,4 +42,7 @@ int pa_socket_set_rcvbuf(int fd, size_t l); int pa_unix_socket_is_stale(const char *fn); int pa_unix_socket_remove_stale(const char *fn); +pa_bool_t pa_socket_address_is_local(const struct sockaddr *sa); +pa_bool_t pa_socket_is_local(int fd); + #endif -- cgit