r17586: merge lib/netif into lib/socket and use -lnsl -lsocket on the
[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 2 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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "lib/socket/socket.h"
27 #include "lib/events/events.h"
28 #include "libcli/composite/composite.h"
29 #include "libcli/resolve/resolve.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 event_context *event_ctx)
89 {
90         struct composite_context *result;
91         struct connect_state *state;
92
93         result = talloc_zero(sock, struct composite_context);
94         if (result == NULL) return NULL;
95         result->state = COMPOSITE_STATE_IN_PROGRESS;
96         result->event_ctx = event_ctx;
97
98         state = talloc_zero(result, struct connect_state);
99         if (composite_nomem(state, result)) return result;
100         result->private_data = state;
101
102         state->sock = talloc_reference(state, sock);
103         if (composite_nomem(state->sock, result)) return result;
104
105         if (my_address) {
106                 void *ref = talloc_reference(state, my_address);
107                 if (composite_nomem(ref, result)) {
108                         return result;
109                 }
110                 state->my_address = my_address;
111         }
112
113         {
114                 void *ref = talloc_reference(state, server_address);
115                 if (composite_nomem(ref, result)) {
116                         return result;
117                 }
118                 state->server_address = server_address;
119         }
120
121         state->flags = flags;
122
123         set_blocking(socket_get_fd(sock), False);
124
125         if (server_address->addr && strcmp(sock->backend_name, "ipv4") == 0) {
126                 struct nbt_name name;
127                 struct composite_context *creq;
128                 make_nbt_name_client(&name, server_address->addr);
129                 creq = resolve_name_send(&name, result->event_ctx,
130                                          lp_name_resolve_order());
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 event_context *ev)
210 {
211         struct composite_context *ctx;
212         ctx = socket_connect_send(sock, my_address, 
213                                   server_address, flags, ev);
214         return socket_connect_recv(ctx);
215 }