s4:torture - fix up "ldap_basic" test
[Samba/fernandojvsilva.git] / source3 / rpc_client / rpc_transport_sock.c
blob4ab6500900349544b367177791468e084919f6d7
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;
29 static int rpc_transport_sock_state_destructor(struct rpc_transport_sock_state *s)
31 if (s->fd != -1) {
32 close(s->fd);
33 s->fd = -1;
35 return 0;
38 struct rpc_sock_read_state {
39 struct rpc_transport_sock_state *transp;
40 ssize_t received;
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,
48 void *priv)
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);
56 if (req == NULL) {
57 return NULL;
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);
65 if (subreq == NULL) {
66 goto fail;
68 tevent_req_set_callback(subreq, rpc_sock_read_done, req);
69 return req;
70 fail:
71 TALLOC_FREE(req);
72 return NULL;
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);
81 int err;
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));
90 return;
92 tevent_req_done(req);
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);
99 NTSTATUS status;
101 if (tevent_req_is_nterror(req, &status)) {
102 return status;
104 *preceived = state->received;
105 return NT_STATUS_OK;
108 struct rpc_sock_write_state {
109 struct rpc_transport_sock_state *transp;
110 ssize_t sent;
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,
118 void *priv)
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);
126 if (req == NULL) {
127 return NULL;
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) {
136 goto fail;
138 tevent_req_set_callback(subreq, rpc_sock_write_done, req);
139 return req;
140 fail:
141 TALLOC_FREE(req);
142 return NULL;
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);
151 int err;
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));
160 return;
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);
169 NTSTATUS status;
171 if (tevent_req_is_nterror(req, &status)) {
172 return status;
174 *psent = state->sent;
175 return NT_STATUS_OK;
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);
189 if (state == NULL) {
190 TALLOC_FREE(result);
191 return NT_STATUS_NO_MEMORY;
193 result->priv = state;
195 state->fd = fd;
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;
205 *presult = result;
206 return NT_STATUS_OK;