merge fixes from samba4
[vlendec/samba-autobuild/.git] / ctdb / tcp / tcp_connect.c
1 /* 
2    ctdb over TCP
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "lib/tdb/include/tdb.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "../include/ctdb_private.h"
27 #include "ctdb_tcp.h"
28
29 static void set_nonblocking(int fd)
30 {
31         unsigned v;
32         v = fcntl(fd, F_GETFL, 0);
33         fcntl(fd, F_SETFL, v | O_NONBLOCK);
34 }
35
36
37 /*
38   called when socket becomes writeable on connect
39 */
40 static void ctdb_node_connect_write(struct event_context *ev, struct fd_event *fde, 
41                                     uint16_t flags, void *private)
42 {
43         struct ctdb_node *node = talloc_get_type(private, struct ctdb_node);
44         struct ctdb_tcp_node *tnode = talloc_get_type(node->private, 
45                                                       struct ctdb_tcp_node);
46         struct ctdb_context *ctdb = node->ctdb;
47         int error = 0;
48         socklen_t len = sizeof(error);
49
50         if (getsockopt(tnode->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0 ||
51             error != 0) {
52                 talloc_free(fde);
53                 close(tnode->fd);
54                 tnode->fd = -1;
55                 event_add_timed(ctdb->ev, node, timeval_current_ofs(1, 0), 
56                                 ctdb_tcp_node_connect, node);
57                 return;
58         }
59
60         talloc_free(fde);
61         tnode->fde = event_add_fd(node->ctdb->ev, node, tnode->fd, EVENT_FD_READ, 
62                                   ctdb_tcp_node_write, node);
63
64         /* tell the ctdb layer we are connected */
65         node->ctdb->upcalls->node_connected(node);
66
67         if (tnode->queue) {
68                 EVENT_FD_WRITEABLE(tnode->fde);         
69         }
70 }
71
72 /*
73   called when we should try and establish a tcp connection to a node
74 */
75 void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te, 
76                            struct timeval t, void *private)
77 {
78         struct ctdb_node *node = talloc_get_type(private, struct ctdb_node);
79         struct ctdb_tcp_node *tnode = talloc_get_type(node->private, 
80                                                       struct ctdb_tcp_node);
81         struct ctdb_context *ctdb = node->ctdb;
82         struct sockaddr_in sock_out;
83
84         tnode->fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
85
86         set_nonblocking(tnode->fd);
87
88         inet_pton(AF_INET, node->address.address, &sock_out.sin_addr);
89         sock_out.sin_port = htons(node->address.port);
90         sock_out.sin_family = PF_INET;
91         
92         if (connect(tnode->fd, (struct sockaddr *)&sock_out, sizeof(sock_out)) != 0 &&
93             errno != EINPROGRESS) {
94                 /* try again once a second */
95                 close(tnode->fd);
96                 event_add_timed(ctdb->ev, node, timeval_current_ofs(1, 0), 
97                                 ctdb_tcp_node_connect, node);
98                 return;
99         }
100
101         /* non-blocking connect - wait for write event */
102         event_add_fd(node->ctdb->ev, node, tnode->fd, EVENT_FD_WRITE|EVENT_FD_READ, 
103                      ctdb_node_connect_write, node);
104 }
105
106 /*
107   destroy a ctdb_incoming structure 
108 */
109 static int ctdb_incoming_destructor(struct ctdb_incoming *in)
110 {
111         close(in->fd);
112         in->fd = -1;
113         return 0;
114 }
115
116 /*
117   called when we get contacted by another node
118   currently makes no attempt to check if the connection is really from a ctdb
119   node in our cluster
120 */
121 static void ctdb_listen_event(struct event_context *ev, struct fd_event *fde, 
122                               uint16_t flags, void *private)
123 {
124         struct ctdb_context *ctdb;
125         struct ctdb_tcp *ctcp;
126         struct sockaddr_in addr;
127         socklen_t len;
128         int fd;
129         struct ctdb_incoming *in;
130
131         ctdb = talloc_get_type(private, struct ctdb_context);
132         ctcp = talloc_get_type(ctdb->private, struct ctdb_tcp);
133         memset(&addr, 0, sizeof(addr));
134         len = sizeof(addr);
135         fd = accept(ctcp->listen_fd, (struct sockaddr *)&addr, &len);
136         if (fd == -1) return;
137
138         in = talloc_zero(ctdb, struct ctdb_incoming);
139         in->fd = fd;
140         in->ctdb = ctdb;
141
142         set_nonblocking(in->fd);
143
144         event_add_fd(ctdb->ev, in, in->fd, EVENT_FD_READ, 
145                      ctdb_tcp_incoming_read, in);       
146
147         talloc_set_destructor(in, ctdb_incoming_destructor);
148 }
149
150
151 /*
152   listen on our own address
153 */
154 int ctdb_tcp_listen(struct ctdb_context *ctdb)
155 {
156         struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private, struct ctdb_tcp);
157         struct sockaddr_in sock;
158         int one = 1;
159
160         sock.sin_port = htons(ctdb->address.port);
161         sock.sin_family = PF_INET;
162         inet_pton(AF_INET, ctdb->address.address, &sock.sin_addr);
163
164         ctcp->listen_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
165         if (ctcp->listen_fd == -1) {
166                 ctdb_set_error(ctdb, "socket failed\n");
167                 return -1;
168         }
169
170         setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
171
172         if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sizeof(sock)) != 0) {
173                 ctdb_set_error(ctdb, "bind failed\n");
174                 close(ctcp->listen_fd);
175                 ctcp->listen_fd = -1;
176                 return -1;
177         }
178
179         if (listen(ctcp->listen_fd, 10) == -1) {
180                 ctdb_set_error(ctdb, "listen failed\n");
181                 close(ctcp->listen_fd);
182                 ctcp->listen_fd = -1;
183                 return -1;
184         }
185
186         event_add_fd(ctdb->ev, ctdb, ctcp->listen_fd, EVENT_FD_READ, 
187                      ctdb_listen_event, ctdb);  
188
189         return 0;
190 }
191