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 tevent_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 tevent_req *req, *subreq;
52 struct rpc_sock_read_state *state;
54 req = tevent_req_create(mem_ctx, &state, struct rpc_sock_read_state);
59 subreq = async_recv_send(state, ev, sock_transp->fd, data, size, 0);
63 tevent_req_set_callback(subreq, rpc_sock_read_done, req);
70 static void rpc_sock_read_done(struct tevent_req *subreq)
72 struct tevent_req *req = tevent_req_callback_data(
73 subreq, struct tevent_req);
74 struct rpc_sock_read_state *state = tevent_req_data(
75 req, struct rpc_sock_read_state);
78 state->received = async_recv_recv(subreq, &err);
79 if (state->received == -1) {
80 tevent_req_nterror(req, map_nt_error_from_unix(err));
86 static NTSTATUS rpc_sock_read_recv(struct tevent_req *req, ssize_t *preceived)
88 struct rpc_sock_read_state *state = tevent_req_data(
89 req, struct rpc_sock_read_state);
92 if (tevent_req_is_nterror(req, &status)) {
95 *preceived = state->received;
99 struct rpc_sock_write_state {
103 static void rpc_sock_write_done(struct tevent_req *subreq);
105 static struct async_req *rpc_sock_write_send(TALLOC_CTX *mem_ctx,
106 struct event_context *ev,
107 const uint8_t *data, size_t size,
110 struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
111 priv, struct rpc_transport_sock_state);
112 struct async_req *result;
113 struct tevent_req *subreq;
114 struct rpc_sock_write_state *state;
116 if (!async_req_setup(mem_ctx, &result, &state,
117 struct rpc_sock_write_state)) {
120 subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
121 if (subreq == NULL) {
124 tevent_req_set_callback(subreq, rpc_sock_write_done, result);
131 static void rpc_sock_write_done(struct tevent_req *subreq)
133 struct async_req *req =
134 tevent_req_callback_data(subreq, struct async_req);
135 struct rpc_sock_write_state *state = talloc_get_type_abort(
136 req->private_data, struct rpc_sock_write_state);
139 state->sent = async_send_recv(subreq, &err);
140 if (state->sent == -1) {
141 async_req_nterror(req, map_nt_error_from_unix(err));
147 static NTSTATUS rpc_sock_write_recv(struct async_req *req, ssize_t *psent)
149 struct rpc_sock_write_state *state = talloc_get_type_abort(
150 req->private_data, struct rpc_sock_write_state);
153 if (async_req_is_nterror(req, &status)) {
156 *psent = state->sent;
160 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
161 struct rpc_cli_transport **presult)
163 struct rpc_cli_transport *result;
164 struct rpc_transport_sock_state *state;
166 result = talloc(mem_ctx, struct rpc_cli_transport);
167 if (result == NULL) {
168 return NT_STATUS_NO_MEMORY;
170 state = talloc(result, struct rpc_transport_sock_state);
173 return NT_STATUS_NO_MEMORY;
175 result->priv = state;
178 talloc_set_destructor(state, rpc_transport_sock_state_destructor);
180 result->trans_send = NULL;
181 result->trans_recv = NULL;
182 result->write_send = rpc_sock_write_send;
183 result->write_recv = rpc_sock_write_recv;
184 result->read_send = rpc_sock_read_send;
185 result->read_recv = rpc_sock_read_recv;