409e19e7f5cd0553b3bfeedd232c902217372785
[samba.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
30
31 struct connect_state {
32         struct socket_context *sock;
33         const struct socket_address *my_address;
34         const struct socket_address *server_address;
35         uint32_t flags;
36 };
37
38 static void socket_connect_handler(struct event_context *ev,
39                                    struct fd_event *fde, 
40                                    uint16_t flags, void *private);
41 static void continue_resolve_name(struct composite_context *ctx);
42
43 /*
44   call the real socket_connect() call, and setup event handler
45 */
46 static void socket_send_connect(struct composite_context *result)
47 {
48         struct fd_event *fde;
49         struct connect_state *state = talloc_get_type(result->private_data, 
50                                                       struct connect_state);
51
52         result->status = socket_connect(state->sock,
53                                         state->my_address,
54                                         state->server_address,
55                                         state->flags);
56         if (NT_STATUS_IS_ERR(result->status) && 
57             !NT_STATUS_EQUAL(result->status,
58                              NT_STATUS_MORE_PROCESSING_REQUIRED)) {
59                 composite_error(result, result->status);
60                 return;
61         }
62
63         fde = event_add_fd(result->event_ctx, result,
64                            socket_get_fd(state->sock),
65                            EVENT_FD_READ|EVENT_FD_WRITE, 
66                            socket_connect_handler, result);
67         composite_nomem(fde, result);
68 }
69
70
71 /*
72   send a socket connect, potentially doing some name resolution first
73 */
74 struct composite_context *socket_connect_send(struct socket_context *sock,
75                                               struct socket_address *my_address,
76                                               struct socket_address *server_address, 
77                                               uint32_t flags,
78                                               struct resolve_context *resolve_ctx,
79                                               struct event_context *event_ctx)
80 {
81         struct composite_context *result;
82         struct connect_state *state;
83
84         result = talloc_zero(sock, struct composite_context);
85         if (result == NULL) return NULL;
86         result->state = COMPOSITE_STATE_IN_PROGRESS;
87         result->event_ctx = event_ctx;
88
89         state = talloc_zero(result, struct connect_state);
90         if (composite_nomem(state, result)) return result;
91         result->private_data = state;
92
93         state->sock = talloc_reference(state, sock);
94         if (composite_nomem(state->sock, result)) return result;
95
96         if (my_address) {
97                 void *ref = talloc_reference(state, my_address);
98                 if (composite_nomem(ref, result)) {
99                         return result;
100                 }
101                 state->my_address = my_address;
102         }
103
104         {
105                 void *ref = talloc_reference(state, server_address);
106                 if (composite_nomem(ref, result)) {
107                         return result;
108                 }
109                 state->server_address = server_address;
110         }
111
112         state->flags = flags;
113
114         set_blocking(socket_get_fd(sock), false);
115
116         if (resolve_ctx != NULL && server_address->addr && strcmp(sock->backend_name, "ipv4") == 0) {
117                 struct nbt_name name;
118                 struct composite_context *creq;
119                 make_nbt_name_server(&name, server_address->addr);
120                 creq = resolve_name_send(resolve_ctx, &name, result->event_ctx);
121                 if (composite_nomem(creq, result)) return result;
122                 composite_continue(result, creq, continue_resolve_name, result);
123                 return result;
124         }
125
126         socket_send_connect(result);
127
128         return result;
129 }
130
131 /*
132   handle write events on connect completion
133 */
134 static void socket_connect_handler(struct event_context *ev,
135                                    struct fd_event *fde, 
136                                    uint16_t flags, void *private)
137 {
138         struct composite_context *result =
139                 talloc_get_type(private, struct composite_context);
140         struct connect_state *state = talloc_get_type(result->private_data,
141                                                       struct connect_state);
142
143         result->status = socket_connect_complete(state->sock, state->flags);
144         if (!composite_is_ok(result)) return;
145
146         composite_done(result);
147 }
148
149 /*
150   recv name resolution reply then send the connect
151 */
152 static void continue_resolve_name(struct composite_context *creq)
153 {
154         struct composite_context *result = talloc_get_type(creq->async.private_data, 
155                                                            struct composite_context);
156         struct connect_state *state = talloc_get_type(result->private_data, struct connect_state);
157         const char *addr;
158
159         result->status = resolve_name_recv(creq, state, &addr);
160         if (!composite_is_ok(result)) return;
161
162         state->server_address = socket_address_from_strings(state, state->sock->backend_name,
163                                                             addr, state->server_address->port);
164         if (composite_nomem(state->server_address, result)) return;
165
166         socket_send_connect(result);
167 }
168
169 /*
170   wait for a socket_connect_send() to finish
171 */
172 NTSTATUS socket_connect_recv(struct composite_context *result)
173 {
174         NTSTATUS status = composite_wait(result);
175         talloc_free(result);
176         return status;
177 }
178
179
180 /*
181   like socket_connect() but takes an event context, doing a semi-async connect
182 */
183 NTSTATUS socket_connect_ev(struct socket_context *sock,
184                            struct socket_address *my_address,
185                            struct socket_address *server_address, 
186                            uint32_t flags, struct resolve_context *resolve_ctx,
187                            struct event_context *ev)
188 {
189         struct composite_context *ctx;
190         ctx = socket_connect_send(sock, my_address, 
191                                   server_address, flags, resolve_ctx, ev);
192         return socket_connect_recv(ctx);
193 }