]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - net.c
06_Unix-Domain-Sockets.diff
[paraslash.git] / net.c
diff --git a/net.c b/net.c
index f1c55b9180f5a90011cf7c637bd68b42951a886e..2add48449f26fd46345a5644df00700df3ab5826 100644 (file)
--- a/net.c
+++ b/net.c
@@ -457,27 +457,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 +491,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 +508,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 +543,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)
 {