diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/pulsecore/socket-util.c | 39 | ||||
| -rw-r--r-- | src/pulsecore/socket-util.h | 6 | 
2 files changed, 44 insertions, 1 deletions
| 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 <sys/types.h> +#include <sys/socket.h> + +#include <pulsecore/macro.h>  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 | 
