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
{
39 struct rpc_transport_sock_state
*transp
;
43 static void rpc_sock_read_done(struct tevent_req
*subreq
);
45 static struct tevent_req
*rpc_sock_read_send(TALLOC_CTX
*mem_ctx
,
46 struct event_context
*ev
,
47 uint8_t *data
, size_t size
,
50 struct rpc_transport_sock_state
*sock_transp
= talloc_get_type_abort(
51 priv
, struct rpc_transport_sock_state
);
52 struct tevent_req
*req
, *subreq
;
53 struct rpc_sock_read_state
*state
;
55 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_sock_read_state
);
59 if (sock_transp
->fd
== -1) {
60 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
61 return tevent_req_post(req
, ev
);
63 state
->transp
= sock_transp
;
64 subreq
= async_recv_send(state
, ev
, sock_transp
->fd
, data
, size
, 0);
68 tevent_req_set_callback(subreq
, rpc_sock_read_done
, req
);
75 static void rpc_sock_read_done(struct tevent_req
*subreq
)
77 struct tevent_req
*req
= tevent_req_callback_data(
78 subreq
, struct tevent_req
);
79 struct rpc_sock_read_state
*state
= tevent_req_data(
80 req
, struct rpc_sock_read_state
);
83 state
->received
= async_recv_recv(subreq
, &err
);
84 if (state
->received
== -1) {
85 if (state
->transp
->fd
!= -1) {
86 close(state
->transp
->fd
);
87 state
->transp
->fd
= -1;
89 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
95 static NTSTATUS
rpc_sock_read_recv(struct tevent_req
*req
, ssize_t
*preceived
)
97 struct rpc_sock_read_state
*state
= tevent_req_data(
98 req
, struct rpc_sock_read_state
);
101 if (tevent_req_is_nterror(req
, &status
)) {
104 *preceived
= state
->received
;
108 struct rpc_sock_write_state
{
109 struct rpc_transport_sock_state
*transp
;
113 static void rpc_sock_write_done(struct tevent_req
*subreq
);
115 static struct tevent_req
*rpc_sock_write_send(TALLOC_CTX
*mem_ctx
,
116 struct event_context
*ev
,
117 const uint8_t *data
, size_t size
,
120 struct rpc_transport_sock_state
*sock_transp
= talloc_get_type_abort(
121 priv
, struct rpc_transport_sock_state
);
122 struct tevent_req
*req
, *subreq
;
123 struct rpc_sock_write_state
*state
;
125 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_sock_write_state
);
129 if (sock_transp
->fd
== -1) {
130 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
131 return tevent_req_post(req
, ev
);
133 state
->transp
= sock_transp
;
134 subreq
= async_send_send(state
, ev
, sock_transp
->fd
, data
, size
, 0);
135 if (subreq
== NULL
) {
138 tevent_req_set_callback(subreq
, rpc_sock_write_done
, req
);
145 static void rpc_sock_write_done(struct tevent_req
*subreq
)
147 struct tevent_req
*req
= tevent_req_callback_data(
148 subreq
, struct tevent_req
);
149 struct rpc_sock_write_state
*state
= tevent_req_data(
150 req
, struct rpc_sock_write_state
);
153 state
->sent
= async_send_recv(subreq
, &err
);
154 if (state
->sent
== -1) {
155 if (state
->transp
->fd
!= -1) {
156 close(state
->transp
->fd
);
157 state
->transp
->fd
= -1;
159 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
162 tevent_req_done(req
);
165 static NTSTATUS
rpc_sock_write_recv(struct tevent_req
*req
, ssize_t
*psent
)
167 struct rpc_sock_write_state
*state
= tevent_req_data(
168 req
, struct rpc_sock_write_state
);
171 if (tevent_req_is_nterror(req
, &status
)) {
174 *psent
= state
->sent
;
178 NTSTATUS
rpc_transport_sock_init(TALLOC_CTX
*mem_ctx
, int fd
,
179 struct rpc_cli_transport
**presult
)
181 struct rpc_cli_transport
*result
;
182 struct rpc_transport_sock_state
*state
;
184 result
= talloc(mem_ctx
, struct rpc_cli_transport
);
185 if (result
== NULL
) {
186 return NT_STATUS_NO_MEMORY
;
188 state
= talloc(result
, struct rpc_transport_sock_state
);
191 return NT_STATUS_NO_MEMORY
;
193 result
->priv
= state
;
196 talloc_set_destructor(state
, rpc_transport_sock_state_destructor
);
198 result
->trans_send
= NULL
;
199 result
->trans_recv
= NULL
;
200 result
->write_send
= rpc_sock_write_send
;
201 result
->write_recv
= rpc_sock_write_recv
;
202 result
->read_send
= rpc_sock_read_send
;
203 result
->read_recv
= rpc_sock_read_recv
;