r3304: changed the API to lib/socket/ a little.
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket / socket_unix.c
index 7e169c47a7ef5e96540cca47f228dae90d822273..3a3ce5fe8afbd839c25143b8e5d97f8c4760cddd 100644 (file)
 
 #include "includes.h"
 
+
+
+/*
+  approximate errno mapping
+*/
+static NTSTATUS unixdom_error(int ernum)
+{
+       return map_nt_error_from_unix(ernum);
+}
+
 static NTSTATUS unixdom_init(struct socket_context *sock)
 {
        sock->fd = socket(PF_UNIX, SOCK_STREAM, 0);
@@ -37,11 +47,6 @@ static NTSTATUS unixdom_init(struct socket_context *sock)
 static void unixdom_close(struct socket_context *sock)
 {
        close(sock->fd);
-       /* if we were listening, then don't leave the socket lying
-          around in the filesystem */
-       if (sock->private_data) {
-               unlink((const char *)sock->private_data);
-       }
 }
 
 static NTSTATUS unixdom_connect(struct socket_context *sock,
@@ -69,7 +74,7 @@ static NTSTATUS unixdom_connect(struct socket_context *sock,
 
        ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
        if (ret == -1) {
-               return NT_STATUS_CONNECTION_REFUSED;
+               return unixdom_error(errno);
        }
 
        sock->state = SOCKET_STATE_CLIENT_CONNECTED;
@@ -97,18 +102,18 @@ static NTSTATUS unixdom_listen(struct socket_context *sock,
 
        ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
        if (ret == -1) {
-               return NT_STATUS_UNSUCCESSFUL;
+               return unixdom_error(errno);
        }
 
        ret = listen(sock->fd, queue_size);
        if (ret == -1) {
-               return NT_STATUS_INSUFFICIENT_RESOURCES;
+               return unixdom_error(errno);
        }
 
        if (!(flags & SOCKET_FLAG_BLOCK)) {
                ret = set_blocking(sock->fd, False);
                if (ret == -1) {
-                       return NT_STATUS_INVALID_PARAMETER;
+                       return unixdom_error(errno);
                }
        }
 
@@ -128,7 +133,7 @@ static NTSTATUS unixdom_accept(struct socket_context *sock,
 
        new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
        if (new_fd == -1) {
-               return NT_STATUS_INSUFFICIENT_RESOURCES;
+               return unixdom_error(errno);
        }
 
        (*new_sock) = talloc_p(NULL, struct socket_context);
@@ -150,107 +155,46 @@ static NTSTATUS unixdom_accept(struct socket_context *sock,
        return NT_STATUS_OK;
 }
 
-static NTSTATUS unixdom_recv(struct socket_context *sock, TALLOC_CTX *mem_ctx,
-                            DATA_BLOB *blob, size_t wantlen, uint32_t flags)
+static NTSTATUS unixdom_recv(struct socket_context *sock, void *buf, 
+                            size_t wantlen, size_t *nread, uint32_t flags)
 {
        ssize_t gotlen;
-       void *buf;
        int flgs = 0;
 
-       buf = talloc(mem_ctx, wantlen);
-       if (!buf) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
        /* TODO: we need to map all flags here */
        if (flags & SOCKET_FLAG_PEEK) {
                flgs |= MSG_PEEK;
        }
 
-       if (!(flags & SOCKET_FLAG_BLOCK)) {
-               flgs |= MSG_DONTWAIT;
-       }
-
        if (flags & SOCKET_FLAG_BLOCK) {
                flgs |= MSG_WAITALL;
        }
 
+       *nread = 0;
+
        gotlen = recv(sock->fd, buf, wantlen, flgs);
        if (gotlen == 0) {
-               talloc_free(buf);
                return NT_STATUS_END_OF_FILE;
        } else if (gotlen == -1) {
-               NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
-               switch (errno) {
-                       case EBADF:
-                       case ENOTCONN:
-                       case ENOTSOCK:
-                       case EFAULT:
-                       case EINVAL:
-                               status = NT_STATUS_INVALID_PARAMETER;
-                               break;
-                       case EAGAIN:
-                       case EINTR:
-                               status = STATUS_MORE_ENTRIES;
-                               break;
-                       case ECONNREFUSED:
-                               status = NT_STATUS_CONNECTION_REFUSED;
-                               break;
-               }
-               talloc_free(buf);
-               return status;
+               return unixdom_error(errno);
        }
 
-       blob->length = gotlen;
-       blob->data = talloc_realloc(mem_ctx, buf, gotlen);
-       if (!blob->data) {
-               return NT_STATUS_NO_MEMORY;
-       }
+       *nread = gotlen;
 
        return NT_STATUS_OK;
 }
 
-static NTSTATUS unixdom_send(struct socket_context *sock, TALLOC_CTX *mem_ctx,
+static NTSTATUS unixdom_send(struct socket_context *sock,
                             const DATA_BLOB *blob, size_t *sendlen, uint32_t flags)
 {
        ssize_t len;
        int flgs = 0;
 
-       /* TODO: we need to map all flags here */
-       if (!(flags & SOCKET_FLAG_BLOCK)) {
-               flgs |= MSG_DONTWAIT;
-       }
+       *sendlen = 0;
 
        len = send(sock->fd, blob->data, blob->length, flgs);
        if (len == -1) {
-               NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
-               switch (errno) {
-                       case EBADF:
-                       case ENOTSOCK:
-                       case EFAULT:
-                       case EINVAL:
-                               status = NT_STATUS_INVALID_PARAMETER;
-                               break;
-                       case EMSGSIZE:
-                               status = NT_STATUS_INVALID_BUFFER_SIZE;
-                               break;
-                       case EAGAIN:
-                       /*case EWOULDBLOCK: this is an alis of EAGAIN --metze */
-                       case EINTR:
-                               *sendlen = 0;
-                               status = STATUS_MORE_ENTRIES;
-                               break;
-                       case ENOBUFS:
-                               status = NT_STATUS_FOOBAR;
-                               break;
-                       case ENOMEM:
-                               status = NT_STATUS_NO_MEMORY;
-                               break;
-                       case EPIPE:
-                               status = NT_STATUS_CONNECTION_DISCONNECTED;
-                               break;
-               }
-               return status;
+               return unixdom_error(errno);
        }       
 
        *sendlen = len;
@@ -261,7 +205,6 @@ static NTSTATUS unixdom_send(struct socket_context *sock, TALLOC_CTX *mem_ctx,
 static NTSTATUS unixdom_set_option(struct socket_context *sock, 
                                   const char *option, const char *val)
 {
-       set_socket_options(sock->fd, option);
        return NT_STATUS_OK;
 }