From: Volker Lendecke Date: Thu, 31 Dec 2020 08:21:25 +0000 (+0100) Subject: rpc_server: Factor out dcesrv_open_ncacn_ip_tcp_sockets() X-Git-Tag: samba-4.14.0rc1~130 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=276b9bc20aaa7e37c2a2faef8a50adc1bd5f3cf2;p=thirdparty%2Fsamba.git rpc_server: Factor out dcesrv_open_ncacn_ip_tcp_sockets() The main change is to return an allocated array of file descriptors in dcesrv_open_ncacn_ip_tcp_sockets() instead of filling a preallocated array of pf_listen_fd structures. Signed-off-by: Volker Lendecke --- diff --git a/source3/rpc_server/rpc_sock_helper.c b/source3/rpc_server/rpc_sock_helper.c index 28f319868fa..d0c9bbe2eb5 100644 --- a/source3/rpc_server/rpc_sock_helper.c +++ b/source3/rpc_server/rpc_sock_helper.c @@ -32,100 +32,153 @@ #undef DBGC_CLASS #define DBGC_CLASS DBGC_RPC_SRV -NTSTATUS dcesrv_create_ncacn_ip_tcp_sockets(struct dcesrv_endpoint *e, - struct pf_listen_fd *listen_fd, - int *listen_fd_size) +static NTSTATUS dcesrv_open_ncacn_ip_tcp_sockets( + struct dcesrv_endpoint *e, + TALLOC_CTX *mem_ctx, + size_t *pnum_fds, + int **pfds) { - TALLOC_CTX *tmp_ctx; - NTSTATUS status = NT_STATUS_UNSUCCESSFUL; uint16_t port = 0; char port_str[6]; const char *endpoint = NULL; + size_t i = 0, num_fds; + int *fds = NULL; + struct samba_sockaddr *addrs = NULL; + NTSTATUS status = NT_STATUS_INVALID_PARAMETER; + bool ok; - tmp_ctx = talloc_stackframe(); - if (tmp_ctx == NULL) { - return NT_STATUS_NO_MEMORY; - } - - endpoint = dcerpc_binding_get_string_option(e->ep_description, - "endpoint"); + endpoint = dcerpc_binding_get_string_option( + e->ep_description, "endpoint"); if (endpoint != NULL) { port = atoi(endpoint); } if (lp_interfaces() && lp_bind_interfaces_only()) { - uint32_t num_ifs = iface_count(); - uint32_t i; + num_fds = iface_count(); + } else { + num_fds = 1; +#ifdef HAVE_IPV6 + num_fds += 1; +#endif + } - /* - * We have been given an interfaces line, and been told to only - * bind to those interfaces. Create a socket per interface and - * bind to only these. - */ + addrs = talloc_array(mem_ctx, struct samba_sockaddr, num_fds); + if (addrs == NULL) { + status = NT_STATUS_NO_MEMORY; + goto fail; + } + fds = talloc_array(mem_ctx, int, num_fds); + if (fds == NULL) { + status = NT_STATUS_NO_MEMORY; + goto fail; + } - /* Now open a listen socket for each of the interfaces. */ - for (i = 0; i < num_ifs; i++) { + /* + * Fill "addrs" + */ + + if (lp_interfaces() && lp_bind_interfaces_only()) { + for (i=0; iep_description, - "endpoint", port_str); + status = dcerpc_binding_set_string_option( + e->ep_description, "endpoint", port_str); if (!NT_STATUS_IS_OK(status)) { DBG_ERR("Failed to set binding endpoint '%s': %s\n", port_str, nt_errstr(status)); + goto fail; + } + + TALLOC_FREE(addrs); + + *pfds = fds; + *pnum_fds = num_fds; + + return NT_STATUS_OK; + +fail: + while (i > 0) { + close(fds[i-1]); + i -= 1; + } + TALLOC_FREE(fds); + TALLOC_FREE(addrs); + return status; +} + +NTSTATUS dcesrv_create_ncacn_ip_tcp_sockets(struct dcesrv_endpoint *e, + struct pf_listen_fd *listen_fd, + int *listen_fd_size) +{ + TALLOC_CTX *tmp_ctx; + NTSTATUS status = NT_STATUS_UNSUCCESSFUL; + int *fds = NULL; + size_t i, num_fds = 0; + + tmp_ctx = talloc_stackframe(); + if (tmp_ctx == NULL) { + return NT_STATUS_NO_MEMORY; + } + + status = dcesrv_open_ncacn_ip_tcp_sockets(e, tmp_ctx, &num_fds, &fds); + if (!NT_STATUS_IS_OK(status)) { goto done; } + for (i=0; i