s3: re-run make samba3-idl.
[Samba/cd1.git] / source3 / rpc_client / rpc_transport_sock.c
blob4ab17dbd8daf0f86e206f6203fef7a67592f6394
1 /*
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/>.
20 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_RPC_CLI
25 struct rpc_transport_sock_state {
26 int fd;
27 int timeout;
30 static int rpc_transport_sock_state_destructor(struct rpc_transport_sock_state *s)
32 if (s->fd != -1) {
33 close(s->fd);
34 s->fd = -1;
36 return 0;
39 struct rpc_sock_read_state {
40 struct rpc_transport_sock_state *transp;
41 ssize_t received;
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,
49 void *priv)
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);
58 if (req == NULL) {
59 return NULL;
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);
68 if (subreq == NULL) {
69 goto fail;
72 if (!tevent_req_set_endtime(subreq, ev, endtime)) {
73 goto fail;
76 tevent_req_set_callback(subreq, rpc_sock_read_done, req);
77 return req;
78 fail:
79 TALLOC_FREE(req);
80 return NULL;
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);
89 int err;
91 /* We must free subreq in this function as there is
92 a timer event attached to it. */
94 state->received = async_recv_recv(subreq, &err);
96 if (state->received == -1) {
97 if (state->transp->fd != -1) {
98 close(state->transp->fd);
99 state->transp->fd = -1;
101 TALLOC_FREE(subreq);
102 tevent_req_nterror(req, map_nt_error_from_unix(err));
103 return;
105 TALLOC_FREE(subreq);
106 tevent_req_done(req);
109 static NTSTATUS rpc_sock_read_recv(struct tevent_req *req, ssize_t *preceived)
111 struct rpc_sock_read_state *state = tevent_req_data(
112 req, struct rpc_sock_read_state);
113 NTSTATUS status;
115 if (tevent_req_is_nterror(req, &status)) {
116 return status;
118 *preceived = state->received;
119 return NT_STATUS_OK;
122 struct rpc_sock_write_state {
123 struct rpc_transport_sock_state *transp;
124 ssize_t sent;
127 static void rpc_sock_write_done(struct tevent_req *subreq);
129 static struct tevent_req *rpc_sock_write_send(TALLOC_CTX *mem_ctx,
130 struct event_context *ev,
131 const uint8_t *data, size_t size,
132 void *priv)
134 struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
135 priv, struct rpc_transport_sock_state);
136 struct tevent_req *req, *subreq;
137 struct rpc_sock_write_state *state;
138 struct timeval endtime;
140 req = tevent_req_create(mem_ctx, &state, struct rpc_sock_write_state);
141 if (req == NULL) {
142 return NULL;
144 if (sock_transp->fd == -1) {
145 tevent_req_nterror(req, NT_STATUS_CONNECTION_INVALID);
146 return tevent_req_post(req, ev);
148 state->transp = sock_transp;
149 endtime = timeval_current_ofs(0, sock_transp->timeout * 1000);
150 subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
151 if (subreq == NULL) {
152 goto fail;
155 if (!tevent_req_set_endtime(subreq, ev, endtime)) {
156 goto fail;
159 tevent_req_set_callback(subreq, rpc_sock_write_done, req);
160 return req;
161 fail:
162 TALLOC_FREE(req);
163 return NULL;
166 static void rpc_sock_write_done(struct tevent_req *subreq)
168 struct tevent_req *req = tevent_req_callback_data(
169 subreq, struct tevent_req);
170 struct rpc_sock_write_state *state = tevent_req_data(
171 req, struct rpc_sock_write_state);
172 int err;
174 /* We must free subreq in this function as there is
175 a timer event attached to it. */
177 state->sent = async_send_recv(subreq, &err);
179 if (state->sent == -1) {
180 if (state->transp->fd != -1) {
181 close(state->transp->fd);
182 state->transp->fd = -1;
184 TALLOC_FREE(subreq);
185 tevent_req_nterror(req, map_nt_error_from_unix(err));
186 return;
188 TALLOC_FREE(subreq);
189 tevent_req_done(req);
192 static NTSTATUS rpc_sock_write_recv(struct tevent_req *req, ssize_t *psent)
194 struct rpc_sock_write_state *state = tevent_req_data(
195 req, struct rpc_sock_write_state);
196 NTSTATUS status;
198 if (tevent_req_is_nterror(req, &status)) {
199 return status;
201 *psent = state->sent;
202 return NT_STATUS_OK;
205 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
206 struct rpc_cli_transport **presult)
208 struct rpc_cli_transport *result;
209 struct rpc_transport_sock_state *state;
211 result = talloc(mem_ctx, struct rpc_cli_transport);
212 if (result == NULL) {
213 return NT_STATUS_NO_MEMORY;
215 state = talloc(result, struct rpc_transport_sock_state);
216 if (state == NULL) {
217 TALLOC_FREE(result);
218 return NT_STATUS_NO_MEMORY;
220 result->priv = state;
222 state->fd = fd;
223 state->timeout = 10000; /* 10 seconds. */
224 talloc_set_destructor(state, rpc_transport_sock_state_destructor);
226 result->trans_send = NULL;
227 result->trans_recv = NULL;
228 result->write_send = rpc_sock_write_send;
229 result->write_recv = rpc_sock_write_recv;
230 result->read_send = rpc_sock_read_send;
231 result->read_recv = rpc_sock_read_recv;
233 *presult = result;
234 return NT_STATUS_OK;
237 int rpccli_set_sock_timeout(struct rpc_pipe_client *cli, int timeout)
239 struct rpc_transport_sock_state *state = talloc_get_type(cli->transport->priv,
240 struct rpc_transport_sock_state);
241 int orig_timeout;
242 if (!state) {
243 return 0;
245 orig_timeout = state->timeout;
246 state->timeout = timeout;
247 return orig_timeout;
250 void rpccli_close_sock_fd(struct rpc_pipe_client *cli)
252 struct rpc_transport_sock_state *state = talloc_get_type(cli->transport->priv,
253 struct rpc_transport_sock_state);
254 if (state) {
255 if (state->fd != -1) {
256 close(state->fd);
257 state->fd = -1;
260 return;
263 bool rpc_pipe_tcp_connection_ok(struct rpc_pipe_client *cli)
265 struct rpc_transport_sock_state *state = talloc_get_type(cli->transport->priv,
266 struct rpc_transport_sock_state);
267 if (state && state->fd != -1) {
268 return true;
271 return false;