Merge Samba3 and Samba4 together
[sfrench/samba-autobuild/.git] / source4 / lib / socket / connect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    implements a non-blocking connect operation that is aware of the samba4
5    events system
6
7    Copyright (C) Andrew Tridgell 2005
8    Copyright (C) Volker Lendecke 2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "lib/socket/socket.h"
26 #include "lib/events/events.h"
27 #include "libcli/composite/composite.h"
28 #include "libcli/resolve/resolve.h"
29 #include "param/param.h"
30
31
32 struct connect_state {
33         struct socket_context *sock;
34         const struct socket_address *my_address;
35         const struct socket_address *server_address;
36         uint32_t flags;
37 };
38
39 static void socket_connect_handler(struct event_context *ev,
40                                    struct fd_event *fde, 
41                                    uint16_t flags, void *private);
42 static void continue_resolve_name(struct composite_context *ctx);
43 static void continue_socket_connect(struct composite_context *creq);
44
45 /*
46   call the real socket_connect() call, and setup event handler
47 */
48 static void socket_send_connect(struct composite_context *result)
49 {
50         struct composite_context *creq;
51         struct fd_event *fde;
52         struct connect_state *state = talloc_get_type(result->private_data, 
53                                                       struct connect_state);
54
55         creq = talloc_zero(state, struct composite_context);
56         if (composite_nomem(creq, result)) return;
57         creq->state = COMPOSITE_STATE_IN_PROGRESS;
58         creq->event_ctx = result->event_ctx;
59         creq->async.fn = continue_socket_connect;
60         creq->async.private_data = result;
61
62         result->status = socket_connect(state->sock,
63                                         state->my_address,
64                                         state->server_address,
65                                         state->flags);
66         if (NT_STATUS_IS_ERR(result->status) && 
67             !NT_STATUS_EQUAL(result->status,
68                              NT_STATUS_MORE_PROCESSING_REQUIRED)) {
69                 composite_error(result, result->status);
70                 return;
71         }
72
73         fde = event_add_fd(result->event_ctx, result,
74                            socket_get_fd(state->sock),
75                            EVENT_FD_READ|EVENT_FD_WRITE, 
76                            socket_connect_handler, result);
77         composite_nomem(fde, result);
78 }
79
80
81 /*
82   send a socket connect, potentially doing some name resolution first
83 */
84 struct composite_context *socket_connect_send(struct socket_context *sock,
85                                               struct socket_address *my_address,
86                                               struct socket_address *server_address, 
87                                               uint32_t flags,
88                                               struct resolve_context *resolve_ctx,
89                                               struct event_context *event_ctx)
90 {
91         struct composite_context *result;
92         struct connect_state *state;
93
94         result = talloc_zero(sock, struct composite_context);
95         if (result == NULL) return NULL;
96         result->state = COMPOSITE_STATE_IN_PROGRESS;
97         result->event_ctx = event_ctx;
98
99         state = talloc_zero(result, struct connect_state);
100         if (composite_nomem(state, result)) return result;
101         result->private_data = state;
102
103         state->sock = talloc_reference(state, sock);
104         if (composite_nomem(state->sock, result)) return result;
105
106         if (my_address) {
107                 void *ref = talloc_reference(state, my_address);
108                 if (composite_nomem(ref, result)) {
109                         return result;
110                 }
111                 state->my_address = my_address;
112         }
113
114         {
115                 void *ref = talloc_reference(state, server_address);
116                 if (composite_nomem(ref, result)) {
117                         return result;
118                 }
119                 state->server_address = server_address;
120         }
121
122         state->flags = flags;
123
124         set_blocking(socket_get_fd(sock), false);
125
126         if (resolve_ctx != NULL && server_address->addr && strcmp(sock->backend_name, "ipv4") == 0) {
127                 struct nbt_name name;
128                 struct composite_context *creq;
129                 make_nbt_name_client(&name, server_address->addr);
130                 creq = resolve_name_send(resolve_ctx, &name, result->event_ctx);
131                 if (composite_nomem(creq, result)) return result;
132                 composite_continue(result, creq, continue_resolve_name, result);
133                 return result;
134         }
135
136         socket_send_connect(result);
137
138         return result;
139 }
140
141 /*
142   handle write events on connect completion
143 */
144 static void socket_connect_handler(struct event_context *ev,
145                                    struct fd_event *fde, 
146                                    uint16_t flags, void *private)
147 {
148         struct composite_context *result =
149                 talloc_get_type(private, struct composite_context);
150         struct connect_state *state = talloc_get_type(result->private_data,
151                                                       struct connect_state);
152
153         result->status = socket_connect_complete(state->sock, state->flags);
154         if (!composite_is_ok(result)) return;
155
156         composite_done(result);
157 }
158
159 /*
160   recv name resolution reply then send the connect
161 */
162 static void continue_resolve_name(struct composite_context *creq)
163 {
164         struct composite_context *result = talloc_get_type(creq->async.private_data, 
165                                                            struct composite_context);
166         struct connect_state *state = talloc_get_type(result->private_data, struct connect_state);
167         const char *addr;
168
169         result->status = resolve_name_recv(creq, state, &addr);
170         if (!composite_is_ok(result)) return;
171
172         state->server_address = socket_address_from_strings(state, state->sock->backend_name,
173                                                             addr, state->server_address->port);
174         if (composite_nomem(state->server_address, result)) return;
175
176         socket_send_connect(result);
177 }
178
179 /*
180   called when a connect has finished. Complete the top level composite context
181 */
182 static void continue_socket_connect(struct composite_context *creq)
183 {
184         struct composite_context *result = talloc_get_type(creq->async.private_data, 
185                                                            struct composite_context);
186         result->status = creq->status;
187         if (!composite_is_ok(result)) return;
188         composite_done(result);
189 }
190
191
192 /*
193   wait for a socket_connect_send() to finish
194 */
195 NTSTATUS socket_connect_recv(struct composite_context *result)
196 {
197         NTSTATUS status = composite_wait(result);
198         talloc_free(result);
199         return status;
200 }
201
202
203 /*
204   like socket_connect() but takes an event context, doing a semi-async connect
205 */
206 NTSTATUS socket_connect_ev(struct socket_context *sock,
207                            struct socket_address *my_address,
208                            struct socket_address *server_address, 
209                            uint32_t flags, struct resolve_context *resolve_ctx,
210                            struct event_context *ev)
211 {
212         struct composite_context *ctx;
213         ctx = socket_connect_send(sock, my_address, 
214                                   server_address, flags, resolve_ctx, ev);
215         return socket_connect_recv(ctx);
216 }