chiark / gitweb /
remove unused includes
[elogind.git] / src / shared / socket-util.c
index c6f64876be4485f6a9c2b4aab41b04b2a094563d..74d90fa2a9716df237aecc02f8098ccb4242f318 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <assert.h>
 #include <string.h>
 #include <unistd.h>
 #include <errno.h>
-#include <stdlib.h>
 #include <arpa/inet.h>
 #include <stdio.h>
 #include <net/if.h>
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <stddef.h>
-#include <sys/ioctl.h>
 #include <netdb.h>
 
 #include "macro.h"
-#include "util.h"
-#include "mkdir.h"
 #include "path-util.h"
+#include "util.h"
 #include "socket-util.h"
 #include "missing.h"
 #include "fileio.h"
@@ -325,9 +320,6 @@ bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
         if (a->type != b->type)
                 return false;
 
-        if (a->size != b->size)
-                return false;
-
         if (socket_address_family(a) != socket_address_family(b))
                 return false;
 
@@ -352,14 +344,20 @@ bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
                 break;
 
         case AF_UNIX:
+                if (a->size <= offsetof(struct sockaddr_un, sun_path) ||
+                    b->size <= offsetof(struct sockaddr_un, sun_path))
+                        return false;
 
                 if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0))
                         return false;
 
                 if (a->sockaddr.un.sun_path[0]) {
-                        if (!strneq(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, sizeof(a->sockaddr.un.sun_path)))
+                        if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path))
                                 return false;
                 } else {
+                        if (a->size != b->size)
+                                return false;
+
                         if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
                                 return false;
                 }
@@ -367,7 +365,6 @@ bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
                 break;
 
         case AF_NETLINK:
-
                 if (a->protocol != b->protocol)
                         return false;
 
@@ -437,52 +434,36 @@ bool socket_ipv6_is_supported(void) {
 }
 
 bool socket_address_matches_fd(const SocketAddress *a, int fd) {
-        union sockaddr_union sa;
-        socklen_t salen = sizeof(sa), solen;
-        int protocol, type;
+        SocketAddress b;
+        socklen_t solen;
 
         assert(a);
         assert(fd >= 0);
 
-        if (getsockname(fd, &sa.sa, &salen) < 0)
+        b.size = sizeof(b.sockaddr);
+        if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0)
                 return false;
 
-        if (sa.sa.sa_family != a->sockaddr.sa.sa_family)
+        if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family)
                 return false;
 
-        solen = sizeof(type);
-        if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &solen) < 0)
+        solen = sizeof(b.type);
+        if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0)
                 return false;
 
-        if (type != a->type)
+        if (b.type != a->type)
                 return false;
 
         if (a->protocol != 0)  {
-                solen = sizeof(protocol);
-                if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &solen) < 0)
+                solen = sizeof(b.protocol);
+                if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0)
                         return false;
 
-                if (protocol != a->protocol)
+                if (b.protocol != a->protocol)
                         return false;
         }
 
-        switch (sa.sa.sa_family) {
-
-        case AF_INET:
-                return sa.in.sin_port == a->sockaddr.in.sin_port &&
-                        sa.in.sin_addr.s_addr == a->sockaddr.in.sin_addr.s_addr;
-
-        case AF_INET6:
-                return sa.in6.sin6_port == a->sockaddr.in6.sin6_port &&
-                        memcmp(&sa.in6.sin6_addr, &a->sockaddr.in6.sin6_addr, sizeof(struct in6_addr)) == 0;
-
-        case AF_UNIX:
-                return salen == a->size &&
-                        memcmp(sa.un.sun_path, a->sockaddr.un.sun_path, salen - offsetof(struct sockaddr_un, sun_path)) == 0;
-
-        }
-
-        return false;
+        return socket_address_equal(a, &b);
 }
 
 int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, char **ret) {