2 Unix SMB/CIFS implementation.
4 dcerpc over standard sockets transport
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Jelmer Vernooij 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "dlinklist.h"
26 #include "lib/events/events.h"
27 #include "librpc/gen_ndr/ndr_epmapper.h"
28 #include "lib/socket/socket.h"
30 #define MIN_HDR_SIZE 16
33 struct sock_blob *next, *prev;
37 /* transport private information used by general socket pipe transports */
39 struct event_context *event_ctx;
41 struct socket_context *sock;
45 struct sock_blob *pending_send;
58 static void sock_dead(struct dcerpc_connection *p, NTSTATUS status)
60 struct sock_private *sock = p->transport.private;
62 if (sock && sock->sock != NULL) {
63 talloc_free(sock->sock);
67 /* wipe any pending sends */
68 while (sock->pending_send) {
69 struct sock_blob *blob = sock->pending_send;
70 DLIST_REMOVE(sock->pending_send, blob);
74 if (!NT_STATUS_IS_OK(status)) {
75 p->transport.recv_data(p, NULL, status);
78 talloc_free(sock->fde);
84 static void sock_process_send(struct dcerpc_connection *p)
86 struct sock_private *sock = p->transport.private;
88 while (sock->pending_send) {
89 struct sock_blob *blob = sock->pending_send;
92 status = socket_send(sock->sock, &blob->data, &sent, 0);
93 if (NT_STATUS_IS_ERR(status)) {
94 sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
101 blob->data.data += sent;
102 blob->data.length -= sent;
104 if (blob->data.length != 0) {
108 DLIST_REMOVE(sock->pending_send, blob);
112 if (sock->pending_send == NULL) {
113 EVENT_FD_NOT_WRITEABLE(sock->fde);
119 process recv requests
121 static void sock_process_recv(struct dcerpc_connection *p)
123 struct sock_private *sock = p->transport.private;
127 if (sock->recv.data.data == NULL) {
128 sock->recv.data = data_blob_talloc(sock, NULL, MIN_HDR_SIZE);
131 /* read in the base header to get the fragment length */
132 if (sock->recv.received < MIN_HDR_SIZE) {
133 uint32_t frag_length;
135 status = socket_recv(sock->sock,
136 sock->recv.data.data + sock->recv.received,
137 MIN_HDR_SIZE - sock->recv.received,
139 if (NT_STATUS_IS_ERR(status)) {
140 sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
147 sock->recv.received += nread;
149 if (sock->recv.received != MIN_HDR_SIZE) {
152 frag_length = dcerpc_get_frag_length(&sock->recv.data);
154 sock->recv.data.data = talloc_realloc(sock, sock->recv.data.data,
155 uint8_t, frag_length);
156 if (sock->recv.data.data == NULL) {
157 sock_dead(p, NT_STATUS_NO_MEMORY);
160 sock->recv.data.length = frag_length;
163 /* read in the rest of the packet */
164 status = socket_recv(sock->sock,
165 sock->recv.data.data + sock->recv.received,
166 sock->recv.data.length - sock->recv.received,
168 if (NT_STATUS_IS_ERR(status)) {
169 sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
175 sock->recv.received += nread;
177 if (sock->recv.received != sock->recv.data.length) {
181 /* we have a full packet */
182 p->transport.recv_data(p, &sock->recv.data, NT_STATUS_OK);
183 talloc_free(sock->recv.data.data);
184 sock->recv.data = data_blob(NULL, 0);
185 sock->recv.received = 0;
186 sock->recv.pending_count--;
187 if (sock->recv.pending_count == 0) {
188 EVENT_FD_NOT_READABLE(sock->fde);
193 called when a IO is triggered by the events system
195 static void sock_io_handler(struct event_context *ev, struct fd_event *fde,
196 uint16_t flags, void *private)
198 struct dcerpc_connection *p = talloc_get_type(private, struct dcerpc_connection);
199 struct sock_private *sock = p->transport.private;
201 if (flags & EVENT_FD_WRITE) {
202 sock_process_send(p);
206 if (sock->sock == NULL) {
210 if (flags & EVENT_FD_READ) {
211 sock_process_recv(p);
216 initiate a read request
218 static NTSTATUS sock_send_read(struct dcerpc_connection *p)
220 struct sock_private *sock = p->transport.private;
222 sock->recv.pending_count++;
223 if (sock->recv.pending_count == 1) {
224 EVENT_FD_READABLE(sock->fde);
230 send an initial pdu in a multi-pdu sequence
232 static NTSTATUS sock_send_request(struct dcerpc_connection *p, DATA_BLOB *data, BOOL trigger_read)
234 struct sock_private *sock = p->transport.private;
235 struct sock_blob *blob;
237 if (sock->sock == NULL) {
238 return NT_STATUS_CONNECTION_DISCONNECTED;
241 blob = talloc(sock, struct sock_blob);
243 return NT_STATUS_NO_MEMORY;
246 blob->data = data_blob_talloc(blob, data->data, data->length);
247 if (blob->data.data == NULL) {
249 return NT_STATUS_NO_MEMORY;
252 DLIST_ADD_END(sock->pending_send, blob, struct sock_blob *);
254 EVENT_FD_WRITEABLE(sock->fde);
264 return the event context so the caller can process asynchronously
266 static struct event_context *sock_event_context(struct dcerpc_connection *p)
268 struct sock_private *sock = p->transport.private;
270 return sock->event_ctx;
274 shutdown sock pipe connection
276 static NTSTATUS sock_shutdown_pipe(struct dcerpc_connection *p)
278 sock_dead(p, NT_STATUS_OK);
284 return sock server name
286 static const char *sock_peer_name(struct dcerpc_connection *p)
288 struct sock_private *sock = p->transport.private;
289 return sock->server_name;
293 open a rpc connection using the generic socket library
295 static NTSTATUS dcerpc_pipe_open_socket(struct dcerpc_connection *c,
299 enum dcerpc_transport_t transport)
301 struct sock_private *sock;
302 struct socket_context *socket_ctx;
305 sock = talloc(c, struct sock_private);
307 return NT_STATUS_NO_MEMORY;
310 status = socket_create(type, SOCKET_TYPE_STREAM, &socket_ctx, 0);
311 if (!NT_STATUS_IS_OK(status)) {
315 talloc_steal(sock, socket_ctx);
317 status = socket_connect(socket_ctx, NULL, 0, server, port, 0);
318 if (!NT_STATUS_IS_OK(status)) {
324 fill in the transport methods
326 c->transport.transport = transport;
327 c->transport.private = NULL;
329 c->transport.send_request = sock_send_request;
330 c->transport.send_read = sock_send_read;
331 c->transport.event_context = sock_event_context;
332 c->transport.recv_data = NULL;
334 c->transport.shutdown_pipe = sock_shutdown_pipe;
335 c->transport.peer_name = sock_peer_name;
337 sock->sock = socket_ctx;
338 sock->server_name = strupper_talloc(sock, server);
339 sock->event_ctx = event_context_init(sock);
340 sock->pending_send = NULL;
341 sock->recv.received = 0;
342 sock->recv.data = data_blob(NULL, 0);
343 sock->recv.pending_count = 0;
345 sock->fde = event_add_fd(sock->event_ctx, sock, socket_get_fd(sock->sock),
346 0, sock_io_handler, c);
348 c->transport.private = sock;
350 /* ensure we don't get SIGPIPE */
351 BlockSignals(True,SIGPIPE);
357 open a rpc connection using tcp
359 NTSTATUS dcerpc_pipe_open_tcp(struct dcerpc_connection *c, const char *server, uint32_t port)
364 status = dcerpc_pipe_open_socket(c, server, port, "ipv6", NCACN_IP_TCP);
365 if (NT_STATUS_IS_OK(status)) {
369 return dcerpc_pipe_open_socket(c, server, port, "ipv4", NCACN_IP_TCP);
373 open a rpc connection to a unix socket
375 NTSTATUS dcerpc_pipe_open_unix_stream(struct dcerpc_connection *c, const char *path)
377 return dcerpc_pipe_open_socket(c, path, 0, "unix", NCACN_UNIX_STREAM);
381 open a rpc connection to a named pipe
383 NTSTATUS dcerpc_pipe_open_pipe(struct dcerpc_connection *c, const char *identifier)
386 char *canon, *full_path;
388 canon = talloc_strdup(NULL, identifier);
390 string_replace(canon, '/', '\\');
391 full_path = talloc_asprintf(canon, "%s/%s", lp_ncalrpc_dir(), canon);
393 status = dcerpc_pipe_open_socket(c, full_path, 0, "unix", NCALRPC);