r26335: Specify name_resolve_order to socket code.
[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                                               const char **name_resolve_order,
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 (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(&name, result->event_ctx,
131                                          lp_name_resolve_order(global_loadparm));
132                 if (composite_nomem(creq, result)) return result;
133                 composite_continue(result, creq, continue_resolve_name, result);
134                 return result;
135         }
136
137         socket_send_connect(result);
138
139         return result;
140 }
141
142 /*
143   handle write events on connect completion
144 */
145 static void socket_connect_handler(struct event_context *ev,
146                                    struct fd_event *fde, 
147                                    uint16_t flags, void *private)
148 {
149         struct composite_context *result =
150                 talloc_get_type(private, struct composite_context);
151         struct connect_state *state = talloc_get_type(result->private_data,
152                                                       struct connect_state);
153
154         result->status = socket_connect_complete(state->sock, state->flags);
155         if (!composite_is_ok(result)) return;
156
157         composite_done(result);
158 }
159
160 /*
161   recv name resolution reply then send the connect
162 */
163 static void continue_resolve_name(struct composite_context *creq)
164 {
165         struct composite_context *result = talloc_get_type(creq->async.private_data, 
166                                                            struct composite_context);
167         struct connect_state *state = talloc_get_type(result->private_data, struct connect_state);
168         const char *addr;
169
170         result->status = resolve_name_recv(creq, state, &addr);
171         if (!composite_is_ok(result)) return;
172
173         state->server_address = socket_address_from_strings(state, state->sock->backend_name,
174                                                             addr, state->server_address->port);
175         if (composite_nomem(state->server_address, result)) return;
176
177         socket_send_connect(result);
178 }
179
180 /*
181   called when a connect has finished. Complete the top level composite context
182 */
183 static void continue_socket_connect(struct composite_context *creq)
184 {
185         struct composite_context *result = talloc_get_type(creq->async.private_data, 
186                                                            struct composite_context);
187         result->status = creq->status;
188         if (!composite_is_ok(result)) return;
189         composite_done(result);
190 }
191
192
193 /*
194   wait for a socket_connect_send() to finish
195 */
196 NTSTATUS socket_connect_recv(struct composite_context *result)
197 {
198         NTSTATUS status = composite_wait(result);
199         talloc_free(result);
200         return status;
201 }
202
203
204 /*
205   like socket_connect() but takes an event context, doing a semi-async connect
206 */
207 NTSTATUS socket_connect_ev(struct socket_context *sock,
208                            struct socket_address *my_address,
209                            struct socket_address *server_address, 
210                            uint32_t flags, const char **name_resolve_order,
211                            struct event_context *ev)
212 {
213         struct composite_context *ctx;
214         ctx = socket_connect_send(sock, my_address, 
215                                   server_address, flags, name_resolve_order, ev);
216         return socket_connect_recv(ctx);
217 }