*/
#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"
struct stream_socket {
const struct stream_server_ops *ops;
struct loadparm_context *lp_ctx;
- struct event_context *event_ctx;
+ struct tevent_context *event_ctx;
const struct model_ops *model_ops;
struct socket_context *sock;
- void *private;
+ void *private_data;
};
*/
void stream_terminate_connection(struct stream_connection *srv_conn, const char *reason)
{
- struct event_context *event_ctx = srv_conn->event.ctx;
+ struct tevent_context *event_ctx = srv_conn->event.ctx;
const struct model_ops *model_ops = srv_conn->model_ops;
if (!reason) reason = "unknown reason";
+ DEBUG(3,("Terminating connection - '%s'\n", reason));
+
srv_conn->terminate = reason;
if (srv_conn->processing) {
*
* 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;
}
talloc_free(srv_conn->event.fde);
srv_conn->event.fde = NULL;
+ model_ops->terminate(event_ctx, srv_conn->lp_ctx, reason);
talloc_free(srv_conn);
- model_ops->terminate(event_ctx, reason);
}
/**
*/
static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
{
- conn->processing = true;
- if (flags & EVENT_FD_WRITE) {
+ conn->processing++;
+ 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 = false;
+ conn->processing--;
if (conn->terminate) {
stream_terminate_connection(conn, conn->terminate);
}
}
-static void stream_io_handler_fde(struct event_context *ev, struct fd_event *fde,
- uint16_t flags, void *private)
+static 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);
}
used for protocols, where a client connection needs to switched into
a server connection
*/
-NTSTATUS stream_new_connection_merge(struct event_context *ev,
+NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
+ struct loadparm_context *lp_ctx,
const struct model_ops *model_ops,
struct socket_context *sock,
const struct stream_server_ops *stream_ops,
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);
srv_conn->ops = stream_ops;
srv_conn->msg_ctx = msg_ctx;
srv_conn->event.ctx = ev;
- 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->lp_ctx = lp_ctx;
+ 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;
}
called when a new socket connection has been established. This is called in the process
context of the new process (if appropriate)
*/
-static void stream_new_connection(struct event_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;
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),
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);
/*
called when someone opens a connection to one of our listening ports
*/
-static void stream_accept_handler(struct event_context *ev, struct fd_event *fde,
- uint16_t flags, void *private)
+static void stream_accept_handler(struct tevent_context *ev, struct tevent_fd *fde,
+ 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()
a string for the port. Should leave allocating a port nr
to the socket implementation - JRV20070903
*/
-NTSTATUS stream_setup_socket(struct event_context *event_context,
+NTSTATUS stream_setup_socket(struct tevent_context *event_context,
struct loadparm_context *lp_ctx,
const struct model_ops *model_ops,
const struct stream_server_ops *stream_ops,
const char *sock_addr,
uint16_t *port,
const char *socket_options,
- void *private)
+ void *private_data)
{
NTSTATUS status;
struct stream_socket *stream_socket;
struct socket_address *socket_address;
+ struct tevent_fd *fde;
int i;
stream_socket = talloc_zero(event_context, struct stream_socket);
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;
}
- /* By specifying EVENT_FD_AUTOCLOSE below, we indicate that we
- * will close the socket using the events system. This avoids
- * nasty interactions with waiting for talloc to close the socket. */
-
- socket_set_flags(stream_socket->sock, SOCKET_FLAG_NOCLOSE);
-
/* Add the FD from the newly created socket into the event
* subsystem. it will call the accept handler whenever we get
* new connections */
- event_add_fd(event_context, stream_socket->sock,
- socket_get_fd(stream_socket->sock),
- EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
- stream_accept_handler, stream_socket);
+ fde = tevent_add_fd(event_context, stream_socket->sock,
+ socket_get_fd(stream_socket->sock),
+ TEVENT_FD_READ,
+ stream_accept_handler, stream_socket);
+ if (!fde) {
+ DEBUG(0,("Failed to setup fd event\n"));
+ talloc_free(stream_socket);
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ /* we let events system to the close on the socket. This avoids
+ * nasty interactions with waiting for talloc to close the socket. */
+ 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;