abe474720cbac4bf8a4797a1044a46a4fe5d086f
[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 events
5    system
6
7    Copyright (C) Andrew Tridgell 2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "lib/socket/socket.h"
26 #include "lib/events/events.h"
27
28
29 /*
30   handle write events on connect completion
31 */
32 static void socket_connect_handler(struct event_context *ev, struct fd_event *fde, 
33                                    uint16_t flags, void *private)
34 {
35         NTSTATUS *status = (NTSTATUS *)private;
36         *status = NT_STATUS_OK;
37 }
38
39
40 /*
41   just like socket_connect() but other events can happen while the
42   connect is ongoing. This isn't as good as making the calling code
43   fully async during its connect phase, but at least it means that any
44   calling code that uses this won't interfere with code that is
45   properly async
46  */
47 NTSTATUS socket_connect_ev(struct socket_context *sock,
48                            const char *my_address, int my_port,
49                            const char *server_address, int server_port,
50                            uint32_t flags, struct event_context *ev)
51 {
52         TALLOC_CTX *tmp_ctx = talloc_new(sock);
53         NTSTATUS status;
54         
55         set_blocking(socket_get_fd(sock), False);
56
57         status = socket_connect(sock, my_address, my_port, 
58                                 server_address, server_port, flags);
59         if (NT_STATUS_IS_ERR(status) && 
60             !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
61                 return status;
62         }
63
64         event_add_fd(ev, tmp_ctx, socket_get_fd(sock), EVENT_FD_WRITE, 
65                      socket_connect_handler, &status);
66
67         while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
68                 if (event_loop_once(ev) != 0) {
69                         talloc_free(tmp_ctx);
70                         return NT_STATUS_INTERNAL_ERROR;
71                 }
72         }
73
74         status = socket_connect_complete(sock, flags);
75
76         talloc_free(tmp_ctx);
77         return status;
78 }