r11821: got rid of two more unnecessary variables and made the variable names
[bbaumbach/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 "librpc/gen_ndr/nbt.h"
29 #include "libcli/composite/composite.h"
30
31
32 struct connect_state {
33         struct socket_context *sock;
34         const char *my_address;
35         int my_port;
36         const char *server_address;
37         int server_port;
38         uint32_t flags;
39 };
40
41 static void socket_connect_handler(struct event_context *ev,
42                                    struct fd_event *fde, 
43                                    uint16_t flags, void *private);
44 static void continue_resolve_name(struct composite_context *ctx);
45 static void continue_socket_connect(struct composite_context *creq);
46
47 /*
48   call the real socket_connect() call, and setup event handler
49 */
50 static void socket_send_connect(struct composite_context *result)
51 {
52         struct composite_context *creq;
53         struct fd_event *fde;
54         struct connect_state *state = talloc_get_type(result->private_data, 
55                                                       struct connect_state);
56
57         creq = talloc_zero(state, struct composite_context);
58         if (composite_nomem(creq, result)) return;
59         creq->state = COMPOSITE_STATE_IN_PROGRESS;
60         creq->event_ctx = result->event_ctx;
61         creq->async.fn = continue_socket_connect;
62         creq->async.private_data = result;
63
64         result->status = socket_connect(state->sock,
65                                         state->my_address,
66                                         state->my_port, 
67                                         state->server_address,
68                                         state->server_port,
69                                         state->flags);
70         if (NT_STATUS_IS_ERR(result->status) && 
71             !NT_STATUS_EQUAL(result->status,
72                              NT_STATUS_MORE_PROCESSING_REQUIRED)) {
73                 composite_error(result, result->status);
74                 return;
75         }
76
77         fde = event_add_fd(result->event_ctx, result,
78                            socket_get_fd(state->sock),
79                            EVENT_FD_WRITE, 
80                            socket_connect_handler, result);
81         composite_nomem(fde, result);
82 }
83
84
85 /*
86   send a socket connect, potentially doing some name resolution first
87 */
88 struct composite_context *socket_connect_send(struct socket_context *sock,
89                                               const char *my_address,
90                                               int my_port,
91                                               const char *server_address,
92                                               int server_port,
93                                               uint32_t flags,
94                                               struct event_context *event_ctx)
95 {
96         struct composite_context *result;
97         struct connect_state *state;
98
99         result = talloc_zero(sock, struct composite_context);
100         if (result == NULL) return NULL;
101         result->state = COMPOSITE_STATE_IN_PROGRESS;
102         result->event_ctx = event_ctx;
103
104         state = talloc_zero(result, struct connect_state);
105         if (composite_nomem(state, result)) goto failed;
106         result->private_data = state;
107
108         state->sock = talloc_reference(state, sock);
109         if (composite_nomem(state->sock, result)) goto failed;
110
111         if (my_address) {
112                 state->my_address = talloc_strdup(state, my_address);
113                 if (composite_nomem(state->my_address, result)) goto failed;
114         }
115         state->my_port = my_port;
116
117         state->server_address = talloc_strdup(state, server_address);
118         if (composite_nomem(state->server_address, result)) goto failed;
119
120         state->server_port = server_port;
121         state->flags = flags;
122
123         set_blocking(socket_get_fd(sock), False);
124
125         if (strcmp(sock->backend_name, "ipv4") == 0) {
126                 struct nbt_name name;
127                 struct composite_context *creq;
128                 make_nbt_name_client(&name, server_address);
129                 creq = resolve_name_send(&name, result->event_ctx,
130                                          lp_name_resolve_order());
131                 if (composite_nomem(creq, result)) goto failed;
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 failed:
141         composite_trigger_error(result);
142         return result;
143 }
144
145 /*
146   handle write events on connect completion
147 */
148 static void socket_connect_handler(struct event_context *ev,
149                                    struct fd_event *fde, 
150                                    uint16_t flags, void *private)
151 {
152         struct composite_context *result =
153                 talloc_get_type(private, struct composite_context);
154         struct connect_state *state = talloc_get_type(result->private_data,
155                                                       struct connect_state);
156
157         result->status = socket_connect_complete(state->sock, state->flags);
158         if (!composite_is_ok(result)) return;
159
160         composite_done(result);
161 }
162
163 /*
164   recv name resolution reply then send the connect
165 */
166 static void continue_resolve_name(struct composite_context *creq)
167 {
168         struct composite_context *result = talloc_get_type(creq->async.private_data, 
169                                                            struct composite_context);
170         struct connect_state *state = talloc_get_type(result->private_data, struct connect_state);
171         const char *addr;
172
173         result->status = resolve_name_recv(creq, state, &addr);
174         if (!composite_is_ok(result)) return;
175
176         state->server_address = addr;
177
178         socket_send_connect(result);
179 }
180
181 /*
182   called when a connect has finished. Complete the top level composite context
183 */
184 static void continue_socket_connect(struct composite_context *creq)
185 {
186         struct composite_context *result = talloc_get_type(creq->async.private_data, 
187                                                            struct composite_context);
188         result->status = creq->status;
189         if (!composite_is_ok(result)) return;
190         composite_done(result);
191 }
192
193
194 /*
195   wait for a socket_connect_send() to finish
196 */
197 NTSTATUS socket_connect_recv(struct composite_context *result)
198 {
199         NTSTATUS status = composite_wait(result);
200         talloc_free(result);
201         return status;
202 }
203
204
205 /*
206   like socket_connect() but takes an event context, doing a semi-async connect
207 */
208 NTSTATUS socket_connect_ev(struct socket_context *sock,
209                            const char *my_address, int my_port,
210                            const char *server_address, int server_port,
211                            uint32_t flags, struct event_context *ev)
212 {
213         struct composite_context *ctx;
214         ctx = socket_connect_send(sock, my_address, my_port,
215                                   server_address, server_port, flags, ev);
216         return socket_connect_recv(ctx);
217 }