dbwrap: Use dbwrap_parse_record in dbwrap_fetch_uint32_bystring
[Samba/gbeck.git] / source3 / rpc_server / rpc_sock_helper.c
blob198df903ad422aae34949f3c27900cd6c1d9b5a8
1 /*
2 * Unix SMB/CIFS implementation.
4 * RPC Socket Helper
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "ntdomain.h"
25 #include "../lib/tsocket/tsocket.h"
26 #include "librpc/rpc/dcerpc_ep.h"
27 #include "rpc_server/rpc_server.h"
28 #include "rpc_server/rpc_sock_helper.h"
30 NTSTATUS rpc_create_tcpip_sockets(const struct ndr_interface_table *iface,
31 struct dcerpc_binding_vector *bvec,
32 uint16_t port,
33 int *listen_fd,
34 int *listen_fd_size)
36 uint32_t num_ifs = iface_count();
37 uint32_t i;
38 uint16_t p = port;
39 TALLOC_CTX *tmp_ctx;
40 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
41 int rc;
43 tmp_ctx = talloc_stackframe();
44 if (tmp_ctx == NULL) {
45 return NT_STATUS_NO_MEMORY;
48 if (lp_interfaces() && lp_bind_interfaces_only()) {
50 * We have been given an interfaces line, and been told to only
51 * bind to those interfaces. Create a socket per interface and
52 * bind to only these.
55 /* Now open a listen socket for each of the interfaces. */
56 for (i = 0; i < num_ifs; i++) {
57 const struct sockaddr_storage *ifss =
58 iface_n_sockaddr_storage(i);
59 struct tsocket_address *bind_addr;
60 const char *addr;
61 int fd;
63 fd = create_tcpip_socket(ifss, &p);
64 if (fd < 0 || p == 0) {
65 status = NT_STATUS_UNSUCCESSFUL;
66 goto done;
68 listen_fd[*listen_fd_size] = fd;
69 (*listen_fd_size)++;
71 if (bvec != NULL) {
72 rc = tsocket_address_bsd_from_sockaddr(tmp_ctx,
73 (struct sockaddr *)ifss,
74 sizeof(struct sockaddr_storage),
75 &bind_addr);
76 if (rc < 0) {
77 close(fd);
78 status = NT_STATUS_NO_MEMORY;
79 goto done;
82 addr = tsocket_address_inet_addr_string(bind_addr,
83 tmp_ctx);
84 if (addr == NULL) {
85 close(fd);
86 status = NT_STATUS_NO_MEMORY;
87 goto done;
90 status = dcerpc_binding_vector_add_port(iface,
91 bvec,
92 addr,
93 p);
94 if (!NT_STATUS_IS_OK(status)) {
95 close(fd);
96 goto done;
100 } else {
101 const char *sock_addr;
102 const char *sock_ptr;
103 char *sock_tok;
105 #if HAVE_IPV6
106 sock_addr = "::,0.0.0.0";
107 #else
108 sock_addr = "0.0.0.0";
109 #endif
111 for (sock_ptr = sock_addr;
112 next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,");
114 struct sockaddr_storage ss;
115 int fd;
117 /* open an incoming socket */
118 if (!interpret_string_addr(&ss,
119 sock_tok,
120 AI_NUMERICHOST|AI_PASSIVE)) {
121 continue;
124 fd = create_tcpip_socket(&ss, &p);
125 if (fd < 0 || p == 0) {
126 status = NT_STATUS_UNSUCCESSFUL;
127 goto done;
129 listen_fd[*listen_fd_size] = fd;
130 (*listen_fd_size)++;
132 if (bvec != NULL) {
133 status = dcerpc_binding_vector_add_port(iface,
134 bvec,
135 sock_ptr,
137 if (!NT_STATUS_IS_OK(status)) {
138 close(fd);
139 return status;
145 status = NT_STATUS_OK;
146 done:
147 talloc_free(tmp_ctx);
148 return status;
151 NTSTATUS rpc_setup_tcpip_sockets(struct tevent_context *ev_ctx,
152 struct messaging_context *msg_ctx,
153 const struct ndr_interface_table *iface,
154 struct dcerpc_binding_vector *bvec,
155 uint16_t port)
157 uint32_t num_ifs = iface_count();
158 uint32_t i;
159 uint16_t p = 0;
160 TALLOC_CTX *tmp_ctx;
161 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
162 int rc;
164 tmp_ctx = talloc_stackframe();
165 if (tmp_ctx == NULL) {
166 return NT_STATUS_NO_MEMORY;
169 if (lp_interfaces() && lp_bind_interfaces_only()) {
171 * We have been given an interfaces line, and been told to only
172 * bind to those interfaces. Create a socket per interface and
173 * bind to only these.
176 /* Now open a listen socket for each of the interfaces. */
177 for (i = 0; i < num_ifs; i++) {
178 const struct sockaddr_storage *ifss =
179 iface_n_sockaddr_storage(i);
180 struct tsocket_address *bind_addr;
181 const char *addr;
183 p = setup_dcerpc_ncacn_tcpip_socket(ev_ctx,
184 msg_ctx,
185 ifss,
186 port);
187 if (p == 0) {
188 status = NT_STATUS_UNSUCCESSFUL;
189 goto done;
192 if (bvec != NULL) {
193 rc = tsocket_address_bsd_from_sockaddr(tmp_ctx,
194 (struct sockaddr*)ifss,
195 sizeof(struct sockaddr_storage),
196 &bind_addr);
197 if (rc < 0) {
198 return NT_STATUS_NO_MEMORY;
201 addr = tsocket_address_inet_addr_string(bind_addr,
202 tmp_ctx);
203 if (addr == NULL) {
204 return NT_STATUS_NO_MEMORY;
207 status = dcerpc_binding_vector_add_port(iface,
208 bvec,
209 addr,
211 if (!NT_STATUS_IS_OK(status)) {
212 return status;
216 } else {
217 const char *sock_addr;
218 const char *sock_ptr;
219 char *sock_tok;
221 #if HAVE_IPV6
222 sock_addr = "::,0.0.0.0";
223 #else
224 sock_addr = "0.0.0.0";
225 #endif
227 for (sock_ptr = sock_addr;
228 next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,");
230 struct sockaddr_storage ss;
232 /* open an incoming socket */
233 if (!interpret_string_addr(&ss,
234 sock_tok,
235 AI_NUMERICHOST|AI_PASSIVE)) {
236 continue;
239 p = setup_dcerpc_ncacn_tcpip_socket(ev_ctx,
240 msg_ctx,
241 &ss,
242 port);
243 if (p == 0) {
244 return NT_STATUS_UNSUCCESSFUL;
247 if (bvec != NULL) {
248 status = dcerpc_binding_vector_add_port(iface,
249 bvec,
250 sock_tok,
252 if (!NT_STATUS_IS_OK(status)) {
253 return status;
259 status = NT_STATUS_OK;
260 done:
261 talloc_free(tmp_ctx);
262 return status;
265 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */