tsocket/bsd: fix do_bind logic for AF_INET
[Samba/cd1.git] / source3 / rpc_client / rpc_transport_sock.c
blobdf060e61e99e7538a7e472eada94fc04f1255f3a
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 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));
98 return;
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);
107 NTSTATUS status;
109 if (tevent_req_is_nterror(req, &status)) {
110 return status;
112 *preceived = state->received;
113 return NT_STATUS_OK;
116 struct rpc_sock_write_state {
117 struct rpc_transport_sock_state *transp;
118 ssize_t sent;
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,
126 void *priv)
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);
135 if (req == NULL) {
136 return NULL;
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) {
146 goto fail;
149 if (!tevent_req_set_endtime(subreq, ev, endtime)) {
150 goto fail;
153 tevent_req_set_callback(subreq, rpc_sock_write_done, req);
154 return req;
155 fail:
156 TALLOC_FREE(req);
157 return NULL;
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);
166 int err;
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));
175 return;
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);
184 NTSTATUS status;
186 if (tevent_req_is_nterror(req, &status)) {
187 return status;
189 *psent = state->sent;
190 return NT_STATUS_OK;
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);
204 if (state == NULL) {
205 TALLOC_FREE(result);
206 return NT_STATUS_NO_MEMORY;
208 result->priv = state;
210 state->fd = fd;
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;
221 *presult = result;
222 return NT_STATUS_OK;
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);
229 int orig_timeout;
230 if (!state) {
231 return 0;
233 orig_timeout = state->timeout;
234 state->timeout = timeout;
235 return orig_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);
242 if (state) {
243 if (state->fd != -1) {
244 close(state->fd);
245 state->fd = -1;
248 return;
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) {
256 return true;
259 return false;