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 tevent_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 tevent_req *req, *subreq;
113 struct rpc_sock_write_state *state;
115 req = tevent_req_create(mem_ctx, &state, struct rpc_sock_write_state);
119 subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
120 if (subreq == NULL) {
123 tevent_req_set_callback(subreq, rpc_sock_write_done, req);
130 static void rpc_sock_write_done(struct tevent_req *subreq)
132 struct tevent_req *req = tevent_req_callback_data(
133 subreq, struct tevent_req);
134 struct rpc_sock_write_state *state = tevent_req_data(
135 req, struct rpc_sock_write_state);
138 state->sent = async_send_recv(subreq, &err);
139 if (state->sent == -1) {
140 tevent_req_nterror(req, map_nt_error_from_unix(err));
143 tevent_req_done(req);
146 static NTSTATUS rpc_sock_write_recv(struct tevent_req *req, ssize_t *psent)
148 struct rpc_sock_write_state *state = tevent_req_data(
149 req, struct rpc_sock_write_state);
152 if (tevent_req_is_nterror(req, &status)) {
155 *psent = state->sent;
159 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
160 struct rpc_cli_transport **presult)
162 struct rpc_cli_transport *result;
163 struct rpc_transport_sock_state *state;
165 result = talloc(mem_ctx, struct rpc_cli_transport);
166 if (result == NULL) {
167 return NT_STATUS_NO_MEMORY;
169 state = talloc(result, struct rpc_transport_sock_state);
172 return NT_STATUS_NO_MEMORY;
174 result->priv = state;
177 talloc_set_destructor(state, rpc_transport_sock_state_destructor);
179 result->trans_send = NULL;
180 result->trans_recv = NULL;
181 result->write_send = rpc_sock_write_send;
182 result->write_recv = rpc_sock_write_recv;
183 result->read_send = rpc_sock_read_send;
184 result->read_recv = rpc_sock_read_recv;