diff options
author | Stefan Metzmacher <metze@samba.org> | 2004-09-20 12:31:07 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 12:58:54 -0500 |
commit | 7d06a06584e5163b69f712e38dc46afc2668389c (patch) | |
tree | 7b15e5c67ad99a2a576fac13ecf04287fdacbc61 /source4 | |
parent | e8d244251732cd744c2c69e3e7bd7a875e704a60 (diff) | |
download | samba-7d06a06584e5163b69f712e38dc46afc2668389c.tar.gz samba-7d06a06584e5163b69f712e38dc46afc2668389c.tar.bz2 samba-7d06a06584e5163b69f712e38dc46afc2668389c.zip |
r2447: let the server code use the new lib/socket/ stuff
metze
(This used to be commit 2fd577d2417e117a7e8c1a56feb147eae805df34)
Diffstat (limited to 'source4')
-rw-r--r-- | source4/ldap_server/ldap_server.c | 55 | ||||
-rw-r--r-- | source4/ntvfs/cifs/vfs_cifs.c | 4 | ||||
-rw-r--r-- | source4/rpc_server/dcerpc_server.c | 12 | ||||
-rw-r--r-- | source4/rpc_server/dcerpc_tcp.c | 75 | ||||
-rw-r--r-- | source4/smb_server/request.c | 10 | ||||
-rw-r--r-- | source4/smb_server/service.c | 2 | ||||
-rw-r--r-- | source4/smb_server/smb_server.c | 40 | ||||
-rw-r--r-- | source4/smbd/process_single.c | 15 | ||||
-rw-r--r-- | source4/smbd/process_standard.c | 21 | ||||
-rw-r--r-- | source4/smbd/process_thread.c | 29 | ||||
-rw-r--r-- | source4/smbd/service.c | 126 |
11 files changed, 215 insertions, 174 deletions
diff --git a/source4/ldap_server/ldap_server.c b/source4/ldap_server/ldap_server.c index 09df833722..59065e142d 100644 --- a/source4/ldap_server/ldap_server.c +++ b/source4/ldap_server/ldap_server.c @@ -33,13 +33,15 @@ static void ldapsrv_terminate_connection(struct ldapsrv_connection *ldap_conn, c add a socket address to the list of events, one event per port */ static void add_socket(struct server_service *service, - const struct model_ops *model_ops, - struct socket_context *socket_ctx, + const struct model_ops *model_ops, struct in_addr *ifip) { uint16_t port = 389; + char *ip_str = talloc_strdup(service->mem_ctx, inet_ntoa(*ifip)); - service_setup_socket(service, model_ops, socket_ctx, ifip, &port); + service_setup_socket(service, model_ops, ip_str, &port); + + talloc_free(ip_str); } /**************************************************************************** @@ -67,7 +69,7 @@ static void ldapsrv_init(struct server_service *service, continue; } - add_socket(service, model_ops, NULL, ifip); + add_socket(service, model_ops, ifip); } } else { struct in_addr *ifip; @@ -79,7 +81,7 @@ static void ldapsrv_init(struct server_service *service, /* Just bind to lp_socket_address() (usually 0.0.0.0) */ ifip = interpret_addr2(mem_ctx, lp_socket_address()); - add_socket(service, model_ops, NULL, ifip); + add_socket(service, model_ops, ifip); talloc_destroy(mem_ctx); } @@ -103,25 +105,42 @@ static BOOL append_to_buf(struct rw_buffer *buf, uint8_t *data, size_t length) return True; } -static BOOL read_into_buf(int fd, struct rw_buffer *buf) +static BOOL read_into_buf(struct socket_context *sock, struct rw_buffer *buf) { - char tmp_buf[1024]; - int len; + NTSTATUS status; + DATA_BLOB tmp_blob; + BOOL ret; - len = read(fd, tmp_buf, sizeof(tmp_buf)); - if (len == 0) + status = socket_recv(sock, sock, &tmp_blob, 1024, 0); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("socket_recv: %s\n",nt_errstr(status))); return False; + } + + ret = append_to_buf(buf, tmp_blob.data, tmp_blob.length); - return append_to_buf(buf, tmp_buf, len); + talloc_free(tmp_blob.data); + + return ret; } -static BOOL write_from_buf(int fd, struct rw_buffer *buf) +static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf) { - int len; + NTSTATUS status; + DATA_BLOB tmp_blob; + size_t sendlen; + + tmp_blob.data = buf->data; + tmp_blob.length = buf->length; - len = write(fd, buf->data, buf->length); - if (len != buf->length) + status = socket_send(sock, sock, &tmp_blob, &sendlen, 0); + if (!NT_STATUS_IS_OK(status)) { return False; + } + + if (buf->length != sendlen) { + return False; + } return True; } @@ -495,7 +514,7 @@ static void ldapsrv_recv(struct server_connection *conn, time_t t, DEBUG(10,("ldapsrv_recv\n")); - if (!read_into_buf(conn->event.fde->fd, &ldap_conn->in_buffer)) { + if (!read_into_buf(conn->socket, &ldap_conn->in_buffer)) { ldapsrv_terminate_connection(ldap_conn, "read_into_buf() failed"); return; } @@ -570,7 +589,7 @@ static void ldapsrv_send(struct server_connection *conn, time_t t, DEBUG(10,("ldapsrv_send\n")); - if (!write_from_buf(conn->event.fde->fd, &ldap_conn->out_buffer)) { + if (!write_from_buf(conn->socket, &ldap_conn->out_buffer)) { ldapsrv_terminate_connection(ldap_conn, "write_from_buf() failed"); return; } @@ -617,7 +636,7 @@ static void ldapsrv_accept(struct server_connection *conn) ldap_conn->connection = conn; conn->private_data = ldap_conn; - + return; } diff --git a/source4/ntvfs/cifs/vfs_cifs.c b/source4/ntvfs/cifs/vfs_cifs.c index ecc2c7ebcb..f0fa5dc85b 100644 --- a/source4/ntvfs/cifs/vfs_cifs.c +++ b/source4/ntvfs/cifs/vfs_cifs.c @@ -51,7 +51,9 @@ struct async_info { static void idle_func(struct smbcli_transport *transport, void *p_private) { struct cvfs_private *private = p_private; - if (socket_pending(private->tcon->smb_conn->connection->event.fde->fd)) { + int fd = socket_get_fd(private->tcon->smb_conn->connection->socket); + + if (socket_pending(fd)) { smbd_process_async(private->tcon->smb_conn); } } diff --git a/source4/rpc_server/dcerpc_server.c b/source4/rpc_server/dcerpc_server.c index e619053f6a..14b7c14d2a 100644 --- a/source4/rpc_server/dcerpc_server.c +++ b/source4/rpc_server/dcerpc_server.c @@ -929,7 +929,7 @@ NTSTATUS dcesrv_input(struct dcesrv_connection *dce_conn, const DATA_BLOB *data) */ NTSTATUS dcesrv_output(struct dcesrv_connection *dce_conn, void *private, - ssize_t (*write_fn)(void *, const void *, size_t)) + ssize_t (*write_fn)(void *, DATA_BLOB *)) { struct dcesrv_call_state *call; struct dcesrv_call_reply *rep; @@ -942,7 +942,7 @@ NTSTATUS dcesrv_output(struct dcesrv_connection *dce_conn, } rep = call->replies; - nwritten = write_fn(private, rep->data.data, rep->data.length); + nwritten = write_fn(private, &rep->data); if (nwritten == -1) { /* TODO: hmm, how do we cope with this? destroy the connection perhaps? */ @@ -972,13 +972,13 @@ NTSTATUS dcesrv_output(struct dcesrv_connection *dce_conn, /* write_fn() for dcesrv_output_blob() */ -static ssize_t dcesrv_output_blob_write_fn(void *private, const void *buf, size_t count) +static ssize_t dcesrv_output_blob_write_fn(void *private, DATA_BLOB *out) { DATA_BLOB *blob = private; - if (count < blob->length) { - blob->length = count; + if (out->length < blob->length) { + blob->length = out->length; } - memcpy(blob->data, buf, blob->length); + memcpy(blob->data, out->data, blob->length); return blob->length; } diff --git a/source4/rpc_server/dcerpc_tcp.c b/source4/rpc_server/dcerpc_tcp.c index 26ced8c087..c5f88d467f 100644 --- a/source4/rpc_server/dcerpc_tcp.c +++ b/source4/rpc_server/dcerpc_tcp.c @@ -31,15 +31,18 @@ struct dcesrv_socket_context { /* write_fn callback for dcesrv_output() */ -static ssize_t dcerpc_write_fn(void *private, const void *buf, size_t count) +static ssize_t dcerpc_write_fn(void *private, DATA_BLOB *out) { - struct fd_event *fde = private; - ssize_t ret; - ret = write(fde->fd, buf, count); - if (ret == -1 && errno == EINTR) { - return 0; + NTSTATUS status; + struct socket_context *sock = private; + size_t sendlen; + + status = socket_send(sock, sock, out, &sendlen, 0); + if (!NT_STATUS_IS_OK(status)) { + return -1; } - return ret; + + return sendlen; } void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason) @@ -52,24 +55,24 @@ void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char */ static void add_socket_rpc(struct server_service *service, const struct model_ops *model_ops, - struct socket_context *socket_ctx, struct dcesrv_context *dce_ctx, struct in_addr *ifip) { struct dcesrv_endpoint *e; + char *ip_str = talloc_strdup(service->mem_ctx, inet_ntoa(*ifip)); for (e=dce_ctx->endpoint_list;e;e=e->next) { if (e->ep_description.type == ENDPOINT_TCP) { struct server_socket *sock; struct dcesrv_socket_context *dcesrv_sock; - sock = service_setup_socket(service,model_ops,socket_ctx,ifip, &e->ep_description.info.tcp_port); + sock = service_setup_socket(service,model_ops, ip_str, &e->ep_description.info.tcp_port); if (!sock) { DEBUG(0,("service_setup_socket(port=%u) failed\n",e->ep_description.info.tcp_port)); continue; } - dcesrv_sock = talloc_p(sock->mem_ctx, struct dcesrv_socket_context); + dcesrv_sock = talloc_p(sock, struct dcesrv_socket_context); if (!dcesrv_sock) { DEBUG(0,("talloc_p(sock->mem_ctx, struct dcesrv_socket_context) failed\n")); continue; @@ -78,10 +81,12 @@ static void add_socket_rpc(struct server_service *service, /* remeber the enpoint of this socket */ dcesrv_sock->endpoint = e; dcesrv_sock->dcesrv_ctx = dce_ctx; - + sock->private_data = dcesrv_sock; } } + + talloc_free(ip_str); } /**************************************************************************** @@ -99,7 +104,7 @@ void dcesrv_tcp_init(struct server_service *service, const struct model_ops *mod if (ifip == NULL) { continue; } - add_socket_rpc(service, model_ops, NULL, dce_ctx, ifip); + add_socket_rpc(service, model_ops, dce_ctx, ifip); } } else { struct in_addr *ifip; @@ -109,7 +114,7 @@ void dcesrv_tcp_init(struct server_service *service, const struct model_ops *mod } ifip = interpret_addr2(mem_ctx, lp_socket_address()); - add_socket_rpc(service, model_ops, NULL, dce_ctx, ifip); + add_socket_rpc(service, model_ops, dce_ctx, ifip); talloc_destroy(mem_ctx); } @@ -135,45 +140,32 @@ void dcesrv_tcp_accept(struct server_connection *conn) conn->private_data = dcesrv_conn; - /* TODO: this should to the generic code - * but the smb server can't handle it yet - * --metze - */ - set_blocking(conn->event.fde->fd, False); - return; } void dcesrv_tcp_recv(struct server_connection *conn, time_t t, uint16_t flags) { + NTSTATUS status; struct dcesrv_connection *dce_conn = conn->private_data; - DATA_BLOB blob; - ssize_t ret; + DATA_BLOB tmp_blob; DEBUG(10,("dcesrv_tcp_recv\n")); - blob = data_blob(NULL, 0x4000); - if (!blob.data) { - dcesrv_terminate_connection(dce_conn, "eof on socket"); + status = socket_recv(conn->socket, conn->socket, &tmp_blob, 0x4000, 0); + if (!NT_STATUS_IS_OK(status)) { + if (NT_STATUS_IS_ERR(status)) { + dcesrv_terminate_connection(dce_conn, "eof on socket"); + return; + } return; } - ret = read(conn->event.fde->fd, blob.data, blob.length); - if (ret == 0 || (ret == -1 && errno != EINTR)) { - data_blob_free(&blob); + status = dcesrv_input(dce_conn, &tmp_blob); + talloc_free(tmp_blob.data); + if (!NT_STATUS_IS_OK(status)) { dcesrv_terminate_connection(dce_conn, "eof on socket"); return; } - if (ret == -1) { - data_blob_free(&blob); - return; - } - - blob.length = ret; - - dcesrv_input(dce_conn, &blob); - - data_blob_free(&blob); if (dce_conn->call_list && dce_conn->call_list->replies) { conn->event.fde->flags |= EVENT_FD_WRITE; @@ -189,16 +181,17 @@ void dcesrv_tcp_send(struct server_connection *conn, time_t t, uint16_t flags) DEBUG(10,("dcesrv_tcp_send\n")); - status = dcesrv_output(dce_conn, conn->event.fde, dcerpc_write_fn); - if (NT_STATUS_IS_ERR(status)) { - /* TODO: destroy fd_event? */ + status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn); + if (!NT_STATUS_IS_OK(status)) { + dcesrv_terminate_connection(dce_conn, "eof on socket"); + return; } if (!dce_conn->call_list || !dce_conn->call_list->replies) { conn->event.fde->flags &= ~EVENT_FD_WRITE; } - return; + return; } void dcesrv_tcp_idle(struct server_connection *conn, time_t t) diff --git a/source4/smb_server/request.c b/source4/smb_server/request.c index a5ca789a39..80663b9ccc 100644 --- a/source4/smb_server/request.c +++ b/source4/smb_server/request.c @@ -247,11 +247,19 @@ void req_grow_data(struct smbsrv_request *req, uint_t new_size) */ void req_send_reply_nosign(struct smbsrv_request *req) { + NTSTATUS status; + DATA_BLOB tmp_blob; + size_t sendlen; + if (req->out.size > NBT_HDR_SIZE) { _smb_setlen(req->out.buffer, req->out.size - NBT_HDR_SIZE); } - if (write_data(req->smb_conn->connection->event.fde->fd, req->out.buffer, req->out.size) != req->out.size) { + tmp_blob.data = req->out.buffer; + tmp_blob.length = req->out.size; + + status = socket_send(req->smb_conn->connection->socket, req, &tmp_blob, &sendlen, SOCKET_FLAG_BLOCK); + if (!NT_STATUS_IS_OK(status) || (req->out.size != sendlen)) { smbsrv_terminate_connection(req->smb_conn, "failed to send reply\n"); } diff --git a/source4/smb_server/service.c b/source4/smb_server/service.c index 54e96476fe..843b0a3872 100644 --- a/source4/smb_server/service.c +++ b/source4/smb_server/service.c @@ -249,7 +249,7 @@ close a cnum void close_cnum(struct smbsrv_tcon *tcon) { DEBUG(3,("%s closed connection to service %s\n", - get_socket_addr(tcon, tcon->smb_conn->connection->event.fde->fd), + socket_get_peer_addr(tcon->smb_conn->connection->socket, tcon), lp_servicename(SNUM(tcon)))); /* tell the ntvfs backend that we are disconnecting */ diff --git a/source4/smb_server/smb_server.c b/source4/smb_server/smb_server.c index 228ff4f4a3..516babf4b7 100644 --- a/source4/smb_server/smb_server.c +++ b/source4/smb_server/smb_server.c @@ -60,35 +60,40 @@ receive a SMB request from the wire, forming a request_context from the result ****************************************************************************/ static struct smbsrv_request *receive_smb_request(struct smbsrv_connection *smb_conn) { + NTSTATUS status; ssize_t len, len2; - char header[4]; + DATA_BLOB tmp_blob; struct smbsrv_request *req; - len = read_data(smb_conn->connection->event.fde->fd, header, 4); - if (len != 4) { + status = socket_recv(smb_conn->connection->socket, smb_conn, &tmp_blob, 4, SOCKET_FLAG_BLOCK|SOCKET_FLAG_PEEK); + if (!NT_STATUS_IS_OK(status)) { + return NULL; + } + if (tmp_blob.length != 4) { return NULL; } - len = smb_len(header); + len = smb_len(tmp_blob.data); + talloc_free(tmp_blob.data); req = init_smb_request(smb_conn); GetTimeOfDay(&req->request_time); req->chained_fnum = -1; - - /* allocate the incoming buffer at the right size */ - req->in.buffer = talloc(req, len + NBT_HDR_SIZE); - /* fill in the already received header */ - memcpy(req->in.buffer, header, 4); + len2 = len + NBT_HDR_SIZE; - len2 = read_data(smb_conn->connection->event.fde->fd, req->in.buffer + NBT_HDR_SIZE, len); - if (len2 != len) { + status = socket_recv(smb_conn->connection->socket, req, &tmp_blob, len2, SOCKET_FLAG_BLOCK); + if (!NT_STATUS_IS_OK(status)) { + return NULL; + } + if (tmp_blob.length != len2) { return NULL; } /* fill in the rest of the req->in structure */ - req->in.size = len + NBT_HDR_SIZE; + req->in.buffer = tmp_blob.data; + req->in.size = len2; req->in.allocated = req->in.size; req->in.hdr = req->in.buffer + NBT_HDR_SIZE; req->in.vwv = req->in.hdr + HDR_VWV; @@ -683,12 +688,15 @@ static void add_socket(struct server_service *service, { const char **ports = lp_smb_ports(); int i; + char *ip_str = talloc_strdup(service->mem_ctx, inet_ntoa(*ifip)); for (i=0;ports[i];i++) { uint16_t port = atoi(ports[i]); if (port == 0) continue; - service_setup_socket(service, model_ops, socket_ctx, ifip, &port); + service_setup_socket(service, model_ops, ip_str, &port); } + + talloc_free(ip_str); } /**************************************************************************** @@ -831,8 +839,10 @@ void smbsrv_accept(struct server_connection *conn) /* set an initial client name based on its IP address. This will be replaced with the netbios name later if it gives us one */ - socket_addr = get_socket_addr(smb_conn, conn->event.fde->fd); - sub_set_remote_machine(socket_addr); + socket_addr = socket_get_peer_addr(conn->socket, smb_conn); + if (socket_addr) { + sub_set_remote_machine(socket_addr); + } /* now initialise a few default values associated with this smb socket */ smb_conn->negotiate.max_send = 0xFFFF; diff --git a/source4/smbd/process_single.c b/source4/smbd/process_single.c index 8bd00c53b9..f3b69eae28 100644 --- a/source4/smbd/process_single.c +++ b/source4/smbd/process_single.c @@ -36,23 +36,22 @@ static void single_start_server(void) */ static void single_accept_connection(struct event_context *ev, struct fd_event *srv_fde, time_t t, uint16_t flags) { - int accepted_fd; - struct sockaddr addr; - socklen_t in_addrlen = sizeof(addr); + NTSTATUS status; + struct socket_context *sock; struct server_socket *server_socket = srv_fde->private; struct server_connection *conn; /* accept an incoming connection. */ - accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen); - if (accepted_fd == -1) { + status = socket_accept(server_socket->socket, &sock, 0); + if (!NT_STATUS_IS_OK(status)) { DEBUG(0,("accept_connection_single: accept: %s\n", - strerror(errno))); + nt_errstr(status))); return; } - conn = server_setup_connection(ev, server_socket, accepted_fd, t); + conn = server_setup_connection(ev, server_socket, sock, t); if (!conn) { - DEBUG(0,("server_setup_connection(ev, server_socket, accepted_fd) failed\n")); + DEBUG(0,("server_setup_connection(ev, server_socket, sock, t) failed\n")); return; } diff --git a/source4/smbd/process_standard.c b/source4/smbd/process_standard.c index c12a1f4305..9dfda41db3 100644 --- a/source4/smbd/process_standard.c +++ b/source4/smbd/process_standard.c @@ -36,18 +36,17 @@ static void standard_model_startup(void) */ static void standard_accept_connection(struct event_context *ev, struct fd_event *srv_fde, time_t t, uint16_t flags) { - int accepted_fd; - struct sockaddr addr; - socklen_t in_addrlen = sizeof(addr); - pid_t pid; + NTSTATUS status; + struct socket_context *sock; struct server_socket *server_socket = srv_fde->private; struct server_connection *conn; + pid_t pid; /* accept an incoming connection. */ - accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen); - if (accepted_fd == -1) { - DEBUG(0,("standard_accept_connection: accept: %s\n", - strerror(errno))); + status = socket_accept(server_socket->socket, &sock, 0); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("accept_connection_single: accept: %s\n", + nt_errstr(status))); return; } @@ -56,7 +55,7 @@ static void standard_accept_connection(struct event_context *ev, struct fd_event if (pid != 0) { /* parent or error code ... */ - close(accepted_fd); + socket_destroy(sock); /* go back to the event loop */ return; } @@ -75,9 +74,9 @@ static void standard_accept_connection(struct event_context *ev, struct fd_event set_need_random_reseed(); - conn = server_setup_connection(ev, server_socket, accepted_fd, t); + conn = server_setup_connection(ev, server_socket, sock, t); if (!conn) { - DEBUG(0,("server_setup_connection(ev, server_socket, accepted_fd) failed\n")); + DEBUG(0,("server_setup_connection(ev, server_socket, sock, t) failed\n")); return; } diff --git a/source4/smbd/process_thread.c b/source4/smbd/process_thread.c index ced07d5d76..ef4d53e188 100644 --- a/source4/smbd/process_thread.c +++ b/source4/smbd/process_thread.c @@ -49,19 +49,19 @@ static int thread_get_id(struct smbsrv_request *req) static void thread_accept_connection(struct event_context *ev, struct fd_event *srv_fde, time_t t, uint16_t flags) { - int accepted_fd, rc; - struct sockaddr addr; - socklen_t in_addrlen = sizeof(addr); + NTSTATUS status; + struct socket_context *sock; + int rc; pthread_t thread_id; pthread_attr_t thread_attr; struct server_socket *server_socket = srv_fde->private; struct server_connection *conn; /* accept an incoming connection. */ - accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen); - if (accepted_fd == -1) { - DEBUG(0,("standard_accept_connection: accept: %s\n", - strerror(errno))); + status = socket_accept(server_socket->socket, &sock, 0); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("accept_connection_single: accept: %s\n", + nt_errstr(status))); return; } @@ -72,16 +72,18 @@ static void thread_accept_connection(struct event_context *ev, struct fd_event * main event_context is continued. */ - ev = event_context_init(); if (!ev) { DEBUG(0,("thread_accept_connection: failed to create event_context!\n")); + socket_destroy(sock); return; } - conn = server_setup_connection(ev, server_socket, accepted_fd, t); + conn = server_setup_connection(ev, server_socket, sock, t); if (!conn) { - DEBUG(0,("server_setup_connection(ev, server_socket, accepted_fd) failed\n")); + DEBUG(0,("server_setup_connection(ev, server_socket, sock, t) failed\n")); + event_context_destroy(ev); + socket_destroy(sock); return; } @@ -98,9 +100,12 @@ static void thread_accept_connection(struct event_context *ev, struct fd_event * pthread_attr_destroy(&thread_attr); if (rc == 0) { DEBUG(4,("accept_connection_thread: created thread_id=%lu for fd=%d\n", - (unsigned long int)thread_id, accepted_fd)); + (unsigned long int)thread_id, socket_get_fd(sock))); } else { - DEBUG(0,("accept_connection_thread: thread create failed for fd=%d, rc=%d\n", accepted_fd, rc)); + DEBUG(0,("accept_connection_thread: thread create failed for fd=%d, rc=%d\n", socket_get_fd(sock), rc)); + event_context_destroy(ev); + socket_destroy(sock); + return; } } diff --git a/source4/smbd/service.c b/source4/smbd/service.c index bcdb953e56..22d8f23765 100644 --- a/source4/smbd/service.c +++ b/source4/smbd/service.c @@ -97,103 +97,111 @@ struct server_context *server_service_startup(const char *model) } /* - setup a single listener of any type - if you pass *port == 0, then a port < 1024 is used + setup a listen stream socket + if you pass *port == 0, then a port > 1024 is used */ struct server_socket *service_setup_socket(struct server_service *service, - const struct model_ops *model_ops, - struct socket_context *socket_ctx, - struct in_addr *ifip, uint16_t *port) + const struct model_ops *model_ops, + const char *sock_addr, + uint16_t *port) { - TALLOC_CTX *mem_ctx; - struct server_socket *sock; + NTSTATUS status; + struct server_socket *srv_sock; + struct socket_context *socket_ctx; struct fd_event fde; int i; - mem_ctx = talloc_init("struct server_socket"); - - sock = talloc_p(mem_ctx, struct server_socket); - if (!sock) { - DEBUG(0,("talloc_p(mem_ctx, struct server_socket) failed\n")); - return NULL; - } - if (*port == 0) { - fde.fd = -1; for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) { - fde.fd = open_socket_in(SOCK_STREAM, i, 0, ifip->s_addr, True); - if (fde.fd != -1) break; - } - if (fde.fd != -1) { - *port = i; + status = socket_create("ipv4", SOCKET_TYPE_STREAM, &socket_ctx, 0); + if (NT_STATUS_IS_OK(status)) { + *port = i; + break; + } } } else { - fde.fd = open_socket_in(SOCK_STREAM, *port, 0, ifip->s_addr, True); + status = socket_create("ipv4", SOCKET_TYPE_STREAM, &socket_ctx, 0); } - if (fde.fd == -1) { + if (!NT_STATUS_IS_OK(status)) { DEBUG(0,("Failed to open socket on %s:%u - %s\n", - inet_ntoa(*ifip), *port, strerror(errno))); + sock_addr, *port, nt_errstr(status))); return NULL; } /* ready to listen */ - set_socket_options(fde.fd, "SO_KEEPALIVE"); - set_socket_options(fde.fd, lp_socket_options()); - - if (listen(fde.fd, SERVER_LISTEN_BACKLOG) == -1) { + status = socket_set_option(socket_ctx, "SO_KEEPALIVE", NULL); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("socket_set_option(socket_ctx, SO_KEEPALIVE, NULL): %s\n", + nt_errstr(status))); + socket_destroy(socket_ctx); + return NULL; + } + status = socket_set_option(socket_ctx, lp_socket_options(), NULL); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("socket_set_option(socket_ctx, lp_socket_options(), NULL): %s\n", + nt_errstr(status))); + socket_destroy(socket_ctx); + return NULL; + } + + /* TODO: set socket ACL's here when they're implemented */ + + status = socket_listen(socket_ctx, sock_addr, *port, SERVER_LISTEN_BACKLOG, 0); + if (!NT_STATUS_IS_OK(status)) { DEBUG(0,("Failed to listen on %s:%u - %s\n", - inet_ntoa(*ifip), *port, strerror(errno))); - close(fde.fd); + sock_addr, *port, nt_errstr(status))); + socket_destroy(socket_ctx); + return NULL; + } + + srv_sock = talloc_p(NULL, struct server_socket); + if (!srv_sock) { + DEBUG(0,("talloc_p(mem_ctx, struct server_socket) failed\n")); + socket_destroy(socket_ctx); return NULL; } /* we are only interested in read events on the listen socket */ - fde.flags = EVENT_FD_READ; - fde.private = sock; - fde.handler = model_ops->accept_connection; - - ZERO_STRUCTP(sock); - sock->mem_ctx = mem_ctx; - sock->service = service; - sock->socket = socket_ctx; - sock->event.ctx = service->srv_ctx->events; - sock->event.fde = event_add_fd(sock->event.ctx, &fde); - if (!sock->event.fde) { - DEBUG(0,("event_add_fd(sock->event.ctx, &fde) failed\n")); + fde.fd = socket_get_fd(socket_ctx); + fde.flags = EVENT_FD_READ; + fde.private = srv_sock; + fde.handler = model_ops->accept_connection; + + ZERO_STRUCTP(srv_sock); + srv_sock->mem_ctx = srv_sock; + srv_sock->service = service; + srv_sock->socket = socket_ctx; + srv_sock->event.ctx = service->srv_ctx->events; + srv_sock->event.fde = event_add_fd(srv_sock->event.ctx, &fde); + if (!srv_sock->event.fde) { + DEBUG(0,("event_add_fd(srv_sock->event.ctx, &fde) failed\n")); + socket_destroy(socket_ctx); return NULL; } - DLIST_ADD(service->socket_list, sock); + DLIST_ADD(service->socket_list, srv_sock); - return sock; + return srv_sock; } -struct server_connection *server_setup_connection(struct event_context *ev, struct server_socket *server_socket, int accepted_fd, time_t t) +struct server_connection *server_setup_connection(struct event_context *ev, struct server_socket *server_socket, struct socket_context *sock, time_t t) { struct fd_event fde; struct timed_event idle; struct server_connection *srv_conn; - TALLOC_CTX *mem_ctx; - mem_ctx = talloc_init("server_service_connection"); - if (!mem_ctx) { - DEBUG(0,("talloc_init(server_service_connection) failed\n")); - return NULL; - } - - srv_conn = talloc_p(mem_ctx, struct server_connection); + srv_conn = talloc_p(NULL, struct server_connection); if (!srv_conn) { DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n")); - talloc_destroy(mem_ctx); return NULL; } ZERO_STRUCTP(srv_conn); - srv_conn->mem_ctx = mem_ctx; + srv_conn->mem_ctx = srv_conn; fde.private = srv_conn; - fde.fd = accepted_fd; + fde.fd = socket_get_fd(sock); fde.flags = EVENT_FD_READ; fde.handler = server_io_handler; @@ -208,9 +216,7 @@ struct server_connection *server_setup_connection(struct event_context *ev, stru srv_conn->server_socket = server_socket; srv_conn->service = server_socket->service; - - /* TODO: we need a generic socket subsystem */ - srv_conn->socket = NULL; + srv_conn->socket = sock; /* create a smb server context and add it to out event handling */ @@ -234,7 +240,7 @@ void server_terminate_connection(struct server_connection *srv_conn, const char void server_destroy_connection(struct server_connection *srv_conn) { - close(srv_conn->event.fde->fd); + socket_destroy(srv_conn->socket); event_remove_fd(srv_conn->event.ctx, srv_conn->event.fde); srv_conn->event.fde = NULL; |