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 tevent_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 tevent_req
*req
, *subreq
;
54 struct rpc_sock_read_state
*state
;
55 struct timeval endtime
;
57 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_sock_read_state
);
61 if (sock_transp
->fd
== -1) {
62 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
63 return tevent_req_post(req
, ev
);
65 state
->transp
= sock_transp
;
66 endtime
= timeval_current_ofs(0, sock_transp
->timeout
* 1000);
67 subreq
= async_recv_send(state
, ev
, sock_transp
->fd
, data
, size
, 0);
72 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
76 tevent_req_set_callback(subreq
, rpc_sock_read_done
, req
);
83 static void rpc_sock_read_done(struct tevent_req
*subreq
)
85 struct tevent_req
*req
= tevent_req_callback_data(
86 subreq
, struct tevent_req
);
87 struct rpc_sock_read_state
*state
= tevent_req_data(
88 req
, struct rpc_sock_read_state
);
91 state
->received
= async_recv_recv(subreq
, &err
);
92 if (state
->received
== -1) {
93 if (state
->transp
->fd
!= -1) {
94 close(state
->transp
->fd
);
95 state
->transp
->fd
= -1;
97 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
100 tevent_req_done(req
);
103 static NTSTATUS
rpc_sock_read_recv(struct tevent_req
*req
, ssize_t
*preceived
)
105 struct rpc_sock_read_state
*state
= tevent_req_data(
106 req
, struct rpc_sock_read_state
);
109 if (tevent_req_is_nterror(req
, &status
)) {
112 *preceived
= state
->received
;
116 struct rpc_sock_write_state
{
117 struct rpc_transport_sock_state
*transp
;
121 static void rpc_sock_write_done(struct tevent_req
*subreq
);
123 static struct tevent_req
*rpc_sock_write_send(TALLOC_CTX
*mem_ctx
,
124 struct event_context
*ev
,
125 const uint8_t *data
, size_t size
,
128 struct rpc_transport_sock_state
*sock_transp
= talloc_get_type_abort(
129 priv
, struct rpc_transport_sock_state
);
130 struct tevent_req
*req
, *subreq
;
131 struct rpc_sock_write_state
*state
;
132 struct timeval endtime
;
134 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_sock_write_state
);
138 if (sock_transp
->fd
== -1) {
139 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
140 return tevent_req_post(req
, ev
);
142 state
->transp
= sock_transp
;
143 endtime
= timeval_current_ofs(0, sock_transp
->timeout
* 1000);
144 subreq
= async_send_send(state
, ev
, sock_transp
->fd
, data
, size
, 0);
145 if (subreq
== NULL
) {
149 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
153 tevent_req_set_callback(subreq
, rpc_sock_write_done
, req
);
160 static void rpc_sock_write_done(struct tevent_req
*subreq
)
162 struct tevent_req
*req
= tevent_req_callback_data(
163 subreq
, struct tevent_req
);
164 struct rpc_sock_write_state
*state
= tevent_req_data(
165 req
, struct rpc_sock_write_state
);
168 state
->sent
= async_send_recv(subreq
, &err
);
169 if (state
->sent
== -1) {
170 if (state
->transp
->fd
!= -1) {
171 close(state
->transp
->fd
);
172 state
->transp
->fd
= -1;
174 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
177 tevent_req_done(req
);
180 static NTSTATUS
rpc_sock_write_recv(struct tevent_req
*req
, ssize_t
*psent
)
182 struct rpc_sock_write_state
*state
= tevent_req_data(
183 req
, struct rpc_sock_write_state
);
186 if (tevent_req_is_nterror(req
, &status
)) {
189 *psent
= state
->sent
;
193 NTSTATUS
rpc_transport_sock_init(TALLOC_CTX
*mem_ctx
, int fd
,
194 struct rpc_cli_transport
**presult
)
196 struct rpc_cli_transport
*result
;
197 struct rpc_transport_sock_state
*state
;
199 result
= talloc(mem_ctx
, struct rpc_cli_transport
);
200 if (result
== NULL
) {
201 return NT_STATUS_NO_MEMORY
;
203 state
= talloc(result
, struct rpc_transport_sock_state
);
206 return NT_STATUS_NO_MEMORY
;
208 result
->priv
= state
;
211 state
->timeout
= 10000; /* 10 seconds. */
212 talloc_set_destructor(state
, rpc_transport_sock_state_destructor
);
214 result
->trans_send
= NULL
;
215 result
->trans_recv
= NULL
;
216 result
->write_send
= rpc_sock_write_send
;
217 result
->write_recv
= rpc_sock_write_recv
;
218 result
->read_send
= rpc_sock_read_send
;
219 result
->read_recv
= rpc_sock_read_recv
;
225 int rpccli_set_sock_timeout(struct rpc_pipe_client
*cli
, int timeout
)
227 struct rpc_transport_sock_state
*state
= talloc_get_type(cli
->transport
->priv
,
228 struct rpc_transport_sock_state
);
233 orig_timeout
= state
->timeout
;
234 state
->timeout
= timeout
;
238 void rpccli_close_sock_fd(struct rpc_pipe_client
*cli
)
240 struct rpc_transport_sock_state
*state
= talloc_get_type(cli
->transport
->priv
,
241 struct rpc_transport_sock_state
);
243 if (state
->fd
!= -1) {
251 bool rpc_pipe_tcp_connection_ok(struct rpc_pipe_client
*cli
)
253 struct rpc_transport_sock_state
*state
= talloc_get_type(cli
->transport
->priv
,
254 struct rpc_transport_sock_state
);
255 if (state
&& state
->fd
!= -1) {