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 {
29 static int rpc_transport_sock_state_destructor(struct rpc_transport_sock_state *s)
38 struct rpc_sock_read_state {
42 static void rpc_sock_read_done(struct tevent_req *subreq);
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,
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;
55 if (!async_req_setup(mem_ctx, &result, &state,
56 struct rpc_sock_read_state)) {
60 subreq = async_recv_send(state, ev, sock_transp->fd, data, size, 0);
64 subreq->async.fn = rpc_sock_read_done;
65 subreq->async.private_data = result;
72 static void rpc_sock_read_done(struct tevent_req *subreq)
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);
80 state->received = async_recv_recv(subreq, &err);
81 if (state->received == -1) {
82 async_req_nterror(req, map_nt_error_from_unix(err));
88 static NTSTATUS rpc_sock_read_recv(struct async_req *req, ssize_t *preceived)
90 struct rpc_sock_read_state *state = talloc_get_type_abort(
91 req->private_data, struct rpc_sock_read_state);
94 if (async_req_is_nterror(req, &status)) {
97 *preceived = state->received;
101 struct rpc_sock_write_state {
105 static void rpc_sock_write_done(struct tevent_req *subreq);
107 static struct async_req *rpc_sock_write_send(TALLOC_CTX *mem_ctx,
108 struct event_context *ev,
109 const uint8_t *data, size_t size,
112 struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
113 priv, struct rpc_transport_sock_state);
114 struct async_req *result;
115 struct tevent_req *subreq;
116 struct rpc_sock_write_state *state;
118 if (!async_req_setup(mem_ctx, &result, &state,
119 struct rpc_sock_write_state)) {
122 subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
123 if (subreq == NULL) {
126 subreq->async.fn = rpc_sock_write_done;
127 subreq->async.private_data = result;
134 static void rpc_sock_write_done(struct tevent_req *subreq)
136 struct async_req *req = talloc_get_type_abort(
137 subreq->async.private_data, struct async_req);
138 struct rpc_sock_write_state *state = talloc_get_type_abort(
139 req->private_data, struct rpc_sock_write_state);
142 state->sent = async_send_recv(subreq, &err);
143 if (state->sent == -1) {
144 async_req_nterror(req, map_nt_error_from_unix(err));
150 static NTSTATUS rpc_sock_write_recv(struct async_req *req, ssize_t *psent)
152 struct rpc_sock_write_state *state = talloc_get_type_abort(
153 req->private_data, struct rpc_sock_write_state);
156 if (async_req_is_nterror(req, &status)) {
159 *psent = state->sent;
163 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
164 struct rpc_cli_transport **presult)
166 struct rpc_cli_transport *result;
167 struct rpc_transport_sock_state *state;
169 result = talloc(mem_ctx, struct rpc_cli_transport);
170 if (result == NULL) {
171 return NT_STATUS_NO_MEMORY;
173 state = talloc(result, struct rpc_transport_sock_state);
176 return NT_STATUS_NO_MEMORY;
178 result->priv = state;
181 talloc_set_destructor(state, rpc_transport_sock_state_destructor);
183 result->trans_send = NULL;
184 result->trans_recv = NULL;
185 result->write_send = rpc_sock_write_send;
186 result->write_recv = rpc_sock_write_recv;
187 result->read_send = rpc_sock_read_send;
188 result->read_recv = rpc_sock_read_recv;