]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - net.c
Trivial whitespace cleanups.
[paraslash.git] / net.c
diff --git a/net.c b/net.c
index f1c55b9180f5a90011cf7c637bd68b42951a886e..9c03ca082e63a9405b8f158c506c8ee9cfaa0c10 100644 (file)
--- a/net.c
+++ b/net.c
@@ -8,6 +8,18 @@
 
 #include <netdb.h>
 
+/* At least NetBSD needs these. */
+#ifndef AI_V4MAPPED
+#define AI_V4MAPPED 0
+#endif
+#ifndef AI_ALL
+#define AI_ALL 0
+#endif
+#ifndef AI_ADDRCONFIG
+#define AI_ADDRCONFIG 0
+#endif
+
+
 #include "para.h"
 #include "error.h"
 #include "net.h"
@@ -126,7 +138,7 @@ static const char *layer4_name(const unsigned l4type)
  *  \sa ipv6(7), getaddrinfo(3), bind(2), connect(2)
  */
 int makesock(unsigned l3type, unsigned l4type, int passive,
-            const char *host, unsigned short port_number)
+               const char *host, unsigned short port_number)
 {
        struct addrinfo *local = NULL, *src,
                        *remote = NULL, *dst, hints;
@@ -168,7 +180,7 @@ int makesock(unsigned l3type, unsigned l4type, int passive,
         */
        for (src = local, dst = remote; src != NULL || dst != NULL; /* no op */ ) {
                if (src && dst && src->ai_family == AF_INET
-                              && dst->ai_family == AF_INET6)   /* v4 -> v6 is not possible */
+                               && dst->ai_family == AF_INET6)  /* v4 -> v6 is not possible */
                        goto get_next_dst;
 
                sockfd = socket(src ? src->ai_family : dst->ai_family, socktype, l4type);
@@ -236,7 +248,7 @@ int para_listen(unsigned l3type, unsigned l4type, unsigned short port)
                        return -ERRNO_TO_PARA_ERROR(errno);
                }
                PARA_INFO_LOG("listening on %s port %u, fd %d\n",
-                             layer4_name(l4type), port, fd);
+                       layer4_name(l4type), port, fd);
        }
        return fd;
 }
@@ -256,7 +268,7 @@ char *host_and_port(struct sockaddr *sa, socklen_t len)
        int             ret;
 
        ret = getnameinfo(sa, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
-                         NI_NUMERICHOST | NI_NUMERICSERV);
+               NI_NUMERICHOST | NI_NUMERICSERV);
        if (ret)        {
                PARA_WARNING_LOG("hostname lookup error (%s).\n", gai_strerror(ret));
                sprintf(output, "(unknown)");
@@ -457,27 +469,6 @@ int recv_buffer(int fd, char *buf, size_t size)
        return n;
 }
 
-/**
- * A wrapper around socket(2).
- *
- * \param domain The communication domain that selects the protocol family.
- *
- * Create an IPv4 socket for sequenced, reliable, two-way, connection-based
- * byte streams.
- *
- * \return The socket fd on success, negative on errors.
- *
- * \sa socket(2).
- */
-int get_stream_socket(int domain)
-{
-       int fd = socket(domain, SOCK_STREAM, 0);
-
-       if (fd < 0)
-               return -ERRNO_TO_PARA_ERROR(errno);
-       return fd;
-}
-
 /**
  * Wrapper around the accept system call.
  *
@@ -512,7 +503,7 @@ int para_accept(int fd, void *addr, socklen_t size)
  * \return Positive on success, \p -E_NAME_TOO_LONG if \a name is longer
  * than \p UNIX_PATH_MAX.
  */
-int init_unix_addr(struct sockaddr_un *u, const char *name)
+static int init_unix_addr(struct sockaddr_un *u, const char *name)
 {
        if (strlen(name) >= UNIX_PATH_MAX)
                return -E_NAME_TOO_LONG;
@@ -529,7 +520,7 @@ int init_unix_addr(struct sockaddr_un *u, const char *name)
  * \param unix_addr Pointer to the \p AF_UNIX socket structure.
  * \param mode The desired mode of the socket.
  *
- * This functions creates a local socket for sequenced, reliable,
+ * This function creates a local socket for sequenced, reliable,
  * two-way, connection-based byte streams.
  *
  * \return The file descriptor, on success, negative on errors.
@@ -564,6 +555,39 @@ err:
        return ret;
 }
 
+/**
+ * Prepare, create, and connect to a Unix domain socket for local communication.
+ *
+ * \param name The socket pathname.
+ *
+ * This function creates a local socket for sequenced, reliable, two-way,
+ * connection-based byte streams.
+ *
+ * \return The file descriptor, on success, negative on errors.
+ *
+ * \sa create_local_socket(), unix(7), connect(2)
+ */
+int create_remote_socket(const char *name)
+{
+       struct sockaddr_un unix_addr;
+       int fd, ret;
+
+       ret = init_unix_addr(&unix_addr, name);
+       if (ret < 0)
+               return ret;
+       fd = socket(PF_UNIX, SOCK_STREAM, 0);
+       if (fd < 0)
+               return -ERRNO_TO_PARA_ERROR(errno);
+       if (connect(fd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) == -1) {
+               ret = -ERRNO_TO_PARA_ERROR(errno);
+               goto err;
+       }
+       return fd;
+err:
+       close(fd);
+       return ret;
+}
+
 #ifndef HAVE_UCRED
 ssize_t send_cred_buffer(int sock, char *buf)
 {