s4-smb: Migrate named_pipe_server to tsocket.
[ira/wip.git] / source4 / smbd / service_stream.c
index a5642c258ff159d1d86f1ab970b0451a0dd3bf55..14387e9558abd7474b47456c183a990726a5cc8f 100644 (file)
 */
 
 #include "includes.h"
+#include <tevent.h>
 #include "process_model.h"
-#include "lib/events/events.h"
-#include "lib/socket/socket.h"
-#include "smbd/service.h"
-#include "smbd/service_stream.h"
 #include "lib/messaging/irpc.h"
 #include "cluster/cluster.h"
 #include "param/param.h"
+#include "../lib/tsocket/tsocket.h"
 
 /* the range of ports to try for dcerpc over tcp endpoints */
 #define SERVER_TCP_LOW_PORT  1024
@@ -47,7 +45,7 @@ struct stream_socket {
        struct tevent_context *event_ctx;
        const struct model_ops *model_ops;
        struct socket_context *sock;
-       void *private;
+       void *private_data;
 };
 
 
@@ -72,7 +70,7 @@ void stream_terminate_connection(struct stream_connection *srv_conn, const char
                 *
                 * and we don't want to read or write to the connection...
                 */
-               event_set_fd_flags(srv_conn->event.fde, 0);
+               tevent_fd_set_flags(srv_conn->event.fde, 0);
                return;
        }
 
@@ -88,9 +86,9 @@ void stream_terminate_connection(struct stream_connection *srv_conn, const char
 static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
 {
        conn->processing++;
-       if (flags & EVENT_FD_WRITE) {
+       if (flags & TEVENT_FD_WRITE) {
                conn->ops->send_handler(conn, flags);
-       } else if (flags & EVENT_FD_READ) {
+       } else if (flags & TEVENT_FD_READ) {
                conn->ops->recv_handler(conn, flags);
        }
        conn->processing--;
@@ -100,17 +98,17 @@ static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
        }
 }
 
-static void stream_io_handler_fde(struct tevent_context *ev, struct tevent_fd *fde, 
-                                 uint16_t flags, void *private)
+void stream_io_handler_fde(struct tevent_context *ev, struct tevent_fd *fde,
+                                 uint16_t flags, void *private_data)
 {
-       struct stream_connection *conn = talloc_get_type(private
+       struct stream_connection *conn = talloc_get_type(private_data,
                                                         struct stream_connection);
        stream_io_handler(conn, flags);
 }
 
-void stream_io_handler_callback(void *private, uint16_t flags) 
+void stream_io_handler_callback(void *private_data, uint16_t flags)
 {
-       struct stream_connection *conn = talloc_get_type(private
+       struct stream_connection *conn = talloc_get_type(private_data,
                                                         struct stream_connection);
        stream_io_handler(conn, flags);
 }
@@ -136,7 +134,7 @@ NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
 
        talloc_steal(srv_conn, sock);
 
-       srv_conn->private       = private_data;
+       srv_conn->private_data  = private_data;
        srv_conn->model_ops     = model_ops;
        srv_conn->socket        = sock;
        srv_conn->server_id     = cluster_id(0, 0);
@@ -144,9 +142,14 @@ NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
        srv_conn->msg_ctx       = msg_ctx;
        srv_conn->event.ctx     = ev;
        srv_conn->lp_ctx        = lp_ctx;
-       srv_conn->event.fde     = event_add_fd(ev, srv_conn, socket_get_fd(sock),
-                                              EVENT_FD_READ, 
-                                              stream_io_handler_fde, srv_conn);
+       srv_conn->event.fde     = tevent_add_fd(ev, srv_conn, socket_get_fd(sock),
+                                               TEVENT_FD_READ,
+                                               stream_io_handler_fde, srv_conn);
+       if (!srv_conn->event.fde) {
+               talloc_free(srv_conn);
+               return NT_STATUS_NO_MEMORY;
+       }
+
        *_srv_conn = srv_conn;
        return NT_STATUS_OK;
 }
@@ -158,11 +161,10 @@ NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
 static void stream_new_connection(struct tevent_context *ev,
                                  struct loadparm_context *lp_ctx,
                                  struct socket_context *sock, 
-                                 struct server_id server_id, void *private)
+                                 struct server_id server_id, void *private_data)
 {
-       struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
+       struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
        struct stream_connection *srv_conn;
-       struct socket_address *c, *s;
 
        srv_conn = talloc_zero(ev, struct stream_connection);
        if (!srv_conn) {
@@ -172,21 +174,26 @@ static void stream_new_connection(struct tevent_context *ev,
 
        talloc_steal(srv_conn, sock);
 
-       srv_conn->private       = stream_socket->private;
+       srv_conn->private_data  = stream_socket->private_data;
        srv_conn->model_ops     = stream_socket->model_ops;
        srv_conn->socket        = sock;
        srv_conn->server_id     = server_id;
        srv_conn->ops           = stream_socket->ops;
        srv_conn->event.ctx     = ev;
        srv_conn->lp_ctx        = lp_ctx;
-       srv_conn->event.fde     = event_add_fd(ev, srv_conn, socket_get_fd(sock),
-                                              0, stream_io_handler_fde, srv_conn);
 
        if (!socket_check_access(sock, "smbd", lp_hostsallow(NULL, lp_default_service(lp_ctx)), lp_hostsdeny(NULL, lp_default_service(lp_ctx)))) {
                stream_terminate_connection(srv_conn, "denied by access rules");
                return;
        }
 
+       srv_conn->event.fde     = tevent_add_fd(ev, srv_conn, socket_get_fd(sock),
+                                               0, stream_io_handler_fde, srv_conn);
+       if (!srv_conn->event.fde) {
+               stream_terminate_connection(srv_conn, "tevent_add_fd() failed");
+               return;
+       }
+
        /* setup to receive internal messages on this connection */
        srv_conn->msg_ctx = messaging_init(srv_conn, 
                                           lp_messaging_path(srv_conn, lp_ctx),
@@ -198,23 +205,37 @@ static void stream_new_connection(struct tevent_context *ev,
                return;
        }
 
-       c = socket_get_peer_addr(sock, ev);
-       s = socket_get_my_addr(sock, ev);
-       if (s && c) {
+       srv_conn->remote_address = socket_get_remote_addr(srv_conn->socket, srv_conn);
+       if (!srv_conn->remote_address) {
+               stream_terminate_connection(srv_conn, "socket_get_remote_addr() failed");
+               return;
+       }
+
+       srv_conn->local_address = socket_get_local_addr(srv_conn->socket, srv_conn);
+       if (!srv_conn->local_address) {
+               stream_terminate_connection(srv_conn, "socket_get_local_addr() failed");
+               return;
+       }
+
+       {
+               TALLOC_CTX *tmp_ctx;
                const char *title;
-               title = talloc_asprintf(s, "conn[%s] c[%s:%u] s[%s:%u] server_id[%s]",
+
+               tmp_ctx = talloc_new(srv_conn);
+
+               title = talloc_asprintf(tmp_ctx, "conn[%s] c[%s] s[%s] server_id[%s]",
                                        stream_socket->ops->name, 
-                                       c->addr, c->port, s->addr, s->port,
-                                       cluster_id_string(s, server_id));
+                                       tsocket_address_string(srv_conn->remote_address, tmp_ctx),
+                                       tsocket_address_string(srv_conn->local_address, tmp_ctx),
+                                       cluster_id_string(tmp_ctx, server_id));
                if (title) {
                        stream_connection_set_title(srv_conn, title);
                }
+               talloc_free(tmp_ctx);
        }
-       talloc_free(c);
-       talloc_free(s);
 
        /* we're now ready to start receiving events on this stream */
-       EVENT_FD_READABLE(srv_conn->event.fde);
+       TEVENT_FD_READABLE(srv_conn->event.fde);
 
        /* call the server specific accept code */
        stream_socket->ops->accept_connection(srv_conn);
@@ -225,9 +246,9 @@ static void stream_new_connection(struct tevent_context *ev,
   called when someone opens a connection to one of our listening ports
 */
 static void stream_accept_handler(struct tevent_context *ev, struct tevent_fd *fde, 
-                                 uint16_t flags, void *private)
+                                 uint16_t flags, void *private_data)
 {
-       struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
+       struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
 
        /* ask the process model to create us a process for this new
           connection.  When done, it calls stream_new_connection()
@@ -253,7 +274,7 @@ NTSTATUS stream_setup_socket(struct tevent_context *event_context,
                             const char *sock_addr,
                             uint16_t *port,
                             const char *socket_options,
-                            void *private)
+                            void *private_data)
 {
        NTSTATUS status;
        struct stream_socket *stream_socket;
@@ -318,7 +339,8 @@ NTSTATUS stream_setup_socket(struct tevent_context *event_context,
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Failed to listen on %s:%u - %s\n",
-                       sock_addr, *port, nt_errstr(status)));
+                        sock_addr, port ? (unsigned int)(*port) : 0,
+                        nt_errstr(status)));
                talloc_free(stream_socket);
                return status;
        }
@@ -342,7 +364,7 @@ NTSTATUS stream_setup_socket(struct tevent_context *event_context,
        tevent_fd_set_close_fn(fde, socket_tevent_fd_close_fn);
        socket_set_flags(stream_socket->sock, SOCKET_FLAG_NOCLOSE);
 
-       stream_socket->private          = talloc_reference(stream_socket, private);
+       stream_socket->private_data     = talloc_reference(stream_socket, private_data);
        stream_socket->ops              = stream_ops;
        stream_socket->event_ctx        = event_context;
        stream_socket->model_ops        = model_ops;