68421b3b4cb0f16c410ec0fadfcc93859637d37d
[jra/samba/.git] / source3 / rpc_client / rpc_transport_sock.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC client transport over a socket
4  *  Copyright (C) Volker Lendecke 2009
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_RPC_CLI
24
25 struct rpc_transport_sock_state {
26         int fd;
27 };
28
29 static int rpc_transport_sock_state_destructor(struct rpc_transport_sock_state *s)
30 {
31         if (s->fd != -1) {
32                 close(s->fd);
33                 s->fd = -1;
34         }
35         return 0;
36 }
37
38 struct rpc_sock_read_state {
39         ssize_t received;
40 };
41
42 static void rpc_sock_read_done(struct tevent_req *subreq);
43
44 static struct async_req *rpc_sock_read_send(TALLOC_CTX *mem_ctx,
45                                             struct event_context *ev,
46                                             uint8_t *data, size_t size,
47                                             void *priv)
48 {
49         struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
50                 priv, struct rpc_transport_sock_state);
51         struct async_req *result;
52         struct tevent_req *subreq;
53         struct rpc_sock_read_state *state;
54
55         if (!async_req_setup(mem_ctx, &result, &state,
56                              struct rpc_sock_read_state)) {
57                 return NULL;
58         }
59
60         subreq = async_recv_send(state, ev, sock_transp->fd, data, size, 0);
61         if (subreq == NULL) {
62                 goto fail;
63         }
64         subreq->async.fn = rpc_sock_read_done;
65         subreq->async.private_data = result;
66         return result;
67  fail:
68         TALLOC_FREE(result);
69         return NULL;
70 }
71
72 static void rpc_sock_read_done(struct tevent_req *subreq)
73 {
74         struct async_req *req = talloc_get_type_abort(
75                 subreq->async.private_data, struct async_req);
76         struct rpc_sock_read_state *state = talloc_get_type_abort(
77                 req->private_data, struct rpc_sock_read_state);
78         int err;
79
80         state->received = async_recv_recv(subreq, &err);
81         if (state->received == -1) {
82                 async_req_nterror(req, map_nt_error_from_unix(err));
83                 return;
84         }
85         async_req_done(req);
86 }
87
88 static NTSTATUS rpc_sock_read_recv(struct async_req *req, ssize_t *preceived)
89 {
90         struct rpc_sock_read_state *state = talloc_get_type_abort(
91                 req->private_data, struct rpc_sock_read_state);
92         NTSTATUS status;
93
94         if (async_req_is_nterror(req, &status)) {
95                 return status;
96         }
97         *preceived = state->received;
98         return NT_STATUS_OK;
99 }
100
101 static struct async_req *rpc_sock_write_send(TALLOC_CTX *mem_ctx,
102                                              struct event_context *ev,
103                                              const uint8_t *data, size_t size,
104                                              void *priv)
105 {
106         struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
107                 priv, struct rpc_transport_sock_state);
108         return async_send(mem_ctx, ev, sock_transp->fd, data, size, 0);
109 }
110
111 static NTSTATUS rpc_sock_write_recv(struct async_req *req, ssize_t *psent)
112 {
113         ssize_t sent;
114         int sys_errno;
115
116         sent = async_syscall_result_ssize_t(req, &sys_errno);
117         if (sent == -1) {
118                 return map_nt_error_from_unix(sys_errno);
119         }
120         *psent = sent;
121         return NT_STATUS_OK;
122 }
123
124 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
125                                  struct rpc_cli_transport **presult)
126 {
127         struct rpc_cli_transport *result;
128         struct rpc_transport_sock_state *state;
129
130         result = talloc(mem_ctx, struct rpc_cli_transport);
131         if (result == NULL) {
132                 return NT_STATUS_NO_MEMORY;
133         }
134         state = talloc(result, struct rpc_transport_sock_state);
135         if (state == NULL) {
136                 TALLOC_FREE(result);
137                 return NT_STATUS_NO_MEMORY;
138         }
139         result->priv = state;
140
141         state->fd = fd;
142         talloc_set_destructor(state, rpc_transport_sock_state_destructor);
143
144         result->trans_send = NULL;
145         result->trans_recv = NULL;
146         result->write_send = rpc_sock_write_send;
147         result->write_recv = rpc_sock_write_recv;
148         result->read_send = rpc_sock_read_send;
149         result->read_recv = rpc_sock_read_recv;
150
151         *presult = result;
152         return NT_STATUS_OK;
153 }