2 * Unix SMB/CIFS implementation.
3 * RPC client transport over a socket
4 * Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
23 #define DBGC_CLASS DBGC_RPC_CLI
25 struct rpc_transport_sock_state {
30 static int rpc_transport_sock_state_destructor(struct rpc_transport_sock_state *s)
39 struct rpc_sock_read_state {
40 struct rpc_transport_sock_state *transp;
44 static void rpc_sock_read_done(struct tevent_req *subreq);
46 static struct async_req *rpc_sock_read_send(TALLOC_CTX *mem_ctx,
47 struct event_context *ev,
48 uint8_t *data, size_t size,
51 struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
52 priv, struct rpc_transport_sock_state);
53 struct async_req *result;
54 struct tevent_req *subreq;
55 struct rpc_sock_read_state *state;
56 struct timeval endtime;
58 if (!async_req_setup(mem_ctx, &result, &state,
59 struct rpc_sock_read_state)) {
62 if (sock_transp->fd == -1) {
63 if (!async_post_ntstatus(result, ev, NT_STATUS_CONNECTION_INVALID)) {
68 state->transp = sock_transp;
69 endtime = timeval_current_ofs(0, sock_transp->timeout * 1000);
70 subreq = async_recv_send(state, ev, sock_transp->fd, data, size, 0);
75 if (!tevent_req_set_endtime(subreq, ev, endtime)) {
79 tevent_req_set_callback(subreq, rpc_sock_read_done, result);
86 static void rpc_sock_read_done(struct tevent_req *subreq)
88 struct async_req *req =
89 tevent_req_callback_data(subreq, struct async_req);
90 struct rpc_sock_read_state *state = talloc_get_type_abort(
91 req->private_data, struct rpc_sock_read_state);
94 /* We must free subreq in this function as there is
95 a timer event attached to it. */
97 state->received = async_recv_recv(subreq, &err);
99 if (state->received == -1) {
100 if (state->transp->fd != -1) {
101 close(state->transp->fd);
102 state->transp->fd = -1;
105 async_req_nterror(req, map_nt_error_from_unix(err));
112 static NTSTATUS rpc_sock_read_recv(struct async_req *req, ssize_t *preceived)
114 struct rpc_sock_read_state *state = talloc_get_type_abort(
115 req->private_data, struct rpc_sock_read_state);
118 if (async_req_is_nterror(req, &status)) {
121 *preceived = state->received;
125 struct rpc_sock_write_state {
126 struct rpc_transport_sock_state *transp;
130 static void rpc_sock_write_done(struct tevent_req *subreq);
132 static struct async_req *rpc_sock_write_send(TALLOC_CTX *mem_ctx,
133 struct event_context *ev,
134 const uint8_t *data, size_t size,
137 struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
138 priv, struct rpc_transport_sock_state);
139 struct async_req *result;
140 struct tevent_req *subreq;
141 struct rpc_sock_write_state *state;
142 struct timeval endtime;
144 if (!async_req_setup(mem_ctx, &result, &state,
145 struct rpc_sock_write_state)) {
148 if (sock_transp->fd == -1) {
149 if (!async_post_ntstatus(result, ev, NT_STATUS_CONNECTION_INVALID)) {
154 state->transp = sock_transp;
155 endtime = timeval_current_ofs(0, sock_transp->timeout * 1000);
156 subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
157 if (subreq == NULL) {
161 if (!tevent_req_set_endtime(subreq, ev, endtime)) {
165 tevent_req_set_callback(subreq, rpc_sock_write_done, result);
172 static void rpc_sock_write_done(struct tevent_req *subreq)
174 struct async_req *req =
175 tevent_req_callback_data(subreq, struct async_req);
176 struct rpc_sock_write_state *state = talloc_get_type_abort(
177 req->private_data, struct rpc_sock_write_state);
180 /* We must free subreq in this function as there is
181 a timer event attached to it. */
183 state->sent = async_send_recv(subreq, &err);
185 if (state->sent == -1) {
186 if (state->transp->fd != -1) {
187 close(state->transp->fd);
188 state->transp->fd = -1;
191 async_req_nterror(req, map_nt_error_from_unix(err));
198 static NTSTATUS rpc_sock_write_recv(struct async_req *req, ssize_t *psent)
200 struct rpc_sock_write_state *state = talloc_get_type_abort(
201 req->private_data, struct rpc_sock_write_state);
204 if (async_req_is_nterror(req, &status)) {
207 *psent = state->sent;
211 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
212 struct rpc_cli_transport **presult)
214 struct rpc_cli_transport *result;
215 struct rpc_transport_sock_state *state;
217 result = talloc(mem_ctx, struct rpc_cli_transport);
218 if (result == NULL) {
219 return NT_STATUS_NO_MEMORY;
221 state = talloc(result, struct rpc_transport_sock_state);
224 return NT_STATUS_NO_MEMORY;
226 result->priv = state;
229 state->timeout = 10000; /* 10 seconds. */
230 talloc_set_destructor(state, rpc_transport_sock_state_destructor);
232 result->trans_send = NULL;
233 result->trans_recv = NULL;
234 result->write_send = rpc_sock_write_send;
235 result->write_recv = rpc_sock_write_recv;
236 result->read_send = rpc_sock_read_send;
237 result->read_recv = rpc_sock_read_recv;