TMP: add a ctdb snapshot of current ctdb master (git://git.samba.org/ctdb.git) to...
[obnox/samba/samba-obnox.git] / ctdb / tcp / tcp_connect.c
1 /* 
2    ctdb over TCP
3
4    Copyright (C) Andrew Tridgell  2006
5    Copyright (C) Ronnie Sahlberg  2008
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/tevent/tevent.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 /*
30   stop any connecting (established or pending) to a node
31  */
32 void ctdb_tcp_stop_connection(struct ctdb_node *node)
33 {
34         struct ctdb_tcp_node *tnode = talloc_get_type(
35                 node->private_data, struct ctdb_tcp_node);
36         
37         ctdb_queue_set_fd(tnode->out_queue, -1);
38         talloc_free(tnode->connect_te);
39         talloc_free(tnode->connect_fde);
40         tnode->connect_fde = NULL;
41         tnode->connect_te = NULL;
42         if (tnode->fd != -1) {
43                 close(tnode->fd);
44                 tnode->fd = -1;
45         }
46 }
47
48
49 /*
50   called when a complete packet has come in - should not happen on this socket
51   unless the other side closes the connection with RST or FIN
52  */
53 void ctdb_tcp_tnode_cb(uint8_t *data, size_t cnt, void *private_data)
54 {
55         struct ctdb_node *node = talloc_get_type(private_data, struct ctdb_node);
56         struct ctdb_tcp_node *tnode = talloc_get_type(
57                 node->private_data, struct ctdb_tcp_node);
58
59         if (data == NULL) {
60                 node->ctdb->upcalls->node_dead(node);
61         }
62
63         ctdb_tcp_stop_connection(node);
64         tnode->connect_te = event_add_timed(node->ctdb->ev, tnode,
65                                             timeval_current_ofs(3, 0),
66                                             ctdb_tcp_node_connect, node);
67 }
68
69 /*
70   called when socket becomes writeable on connect
71 */
72 static void ctdb_node_connect_write(struct event_context *ev, struct fd_event *fde, 
73                                     uint16_t flags, void *private_data)
74 {
75         struct ctdb_node *node = talloc_get_type(private_data,
76                                                  struct ctdb_node);
77         struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data,
78                                                       struct ctdb_tcp_node);
79         struct ctdb_context *ctdb = node->ctdb;
80         int error = 0;
81         socklen_t len = sizeof(error);
82         int one = 1;
83
84         talloc_free(tnode->connect_te);
85         tnode->connect_te = NULL;
86
87         if (getsockopt(tnode->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0 ||
88             error != 0) {
89                 ctdb_tcp_stop_connection(node);
90                 tnode->connect_te = event_add_timed(ctdb->ev, tnode, 
91                                                     timeval_current_ofs(1, 0),
92                                                     ctdb_tcp_node_connect, node);
93                 return;
94         }
95
96         talloc_free(tnode->connect_fde);
97         tnode->connect_fde = NULL;
98
99         setsockopt(tnode->fd,IPPROTO_TCP,TCP_NODELAY,(char *)&one,sizeof(one));
100         setsockopt(tnode->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one));
101
102         ctdb_queue_set_fd(tnode->out_queue, tnode->fd);
103
104         /* the queue subsystem now owns this fd */
105         tnode->fd = -1;
106 }
107
108
109 static int ctdb_tcp_get_address(struct ctdb_context *ctdb,
110                                 const char *address, ctdb_sock_addr *addr)
111 {
112         if (parse_ip(address, NULL, 0, addr) == 0) {
113                 DEBUG(DEBUG_CRIT, (__location__ " Unparsable address : %s.\n", address));
114                 return -1;
115         }
116         return 0;
117 }
118
119 /*
120   called when we should try and establish a tcp connection to a node
121 */
122 void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te, 
123                            struct timeval t, void *private_data)
124 {
125         struct ctdb_node *node = talloc_get_type(private_data,
126                                                  struct ctdb_node);
127         struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data, 
128                                                       struct ctdb_tcp_node);
129         struct ctdb_context *ctdb = node->ctdb;
130         ctdb_sock_addr sock_in;
131         int sockin_size;
132         int sockout_size;
133         ctdb_sock_addr sock_out;
134
135         ctdb_tcp_stop_connection(node);
136
137         ZERO_STRUCT(sock_out);
138 #ifdef HAVE_SOCK_SIN_LEN
139         sock_out.ip.sin_len = sizeof(sock_out);
140 #endif
141         if (ctdb_tcp_get_address(ctdb, node->address.address, &sock_out) != 0) {
142                 return;
143         }
144         switch (sock_out.sa.sa_family) {
145         case AF_INET:
146                 sock_out.ip.sin_port = htons(node->address.port);
147                 break;
148         case AF_INET6:
149                 sock_out.ip6.sin6_port = htons(node->address.port);
150                 break;
151         default:
152                 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
153                         sock_out.sa.sa_family));
154                 return;
155         }
156
157         tnode->fd = socket(sock_out.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
158         set_nonblocking(tnode->fd);
159         set_close_on_exec(tnode->fd);
160
161         DEBUG(DEBUG_DEBUG, (__location__ " Created TCP SOCKET FD:%d\n", tnode->fd));
162
163         /* Bind our side of the socketpair to the same address we use to listen
164          * on incoming CTDB traffic.
165          * We must specify this address to make sure that the address we expose to
166          * the remote side is actually routable in case CTDB traffic will run on
167          * a dedicated non-routeable network.
168          */
169         ZERO_STRUCT(sock_in);
170         if (ctdb_tcp_get_address(ctdb, ctdb->address.address, &sock_in) != 0) {
171                 DEBUG(DEBUG_ERR, (__location__ " Failed to find our address. Failing bind.\n"));
172                 close(tnode->fd);
173                 return;
174         }
175
176         /* AIX libs check to see if the socket address and length
177            arguments are consistent with each other on calls like
178            connect().   Can not get by with just sizeof(sock_in),
179            need sizeof(sock_in.ip).
180         */
181         switch (sock_in.sa.sa_family) {
182         case AF_INET:
183                 sockin_size = sizeof(sock_in.ip);
184                 sockout_size = sizeof(sock_out.ip);
185                 break;
186         case AF_INET6:
187                 sockin_size = sizeof(sock_in.ip6);
188                 sockout_size = sizeof(sock_out.ip6);
189                 break;
190         default:
191                 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
192                         sock_in.sa.sa_family));
193                 close(tnode->fd);
194                 return;
195         }
196 #ifdef HAVE_SOCK_SIN_LEN
197         sock_in.ip.sin_len = sockin_size;
198         sock_out.ip.sin_len = sockout_size;
199 #endif
200         bind(tnode->fd, (struct sockaddr *)&sock_in, sockin_size);
201
202         if (connect(tnode->fd, (struct sockaddr *)&sock_out, sockout_size) != 0 &&
203             errno != EINPROGRESS) {
204                 ctdb_tcp_stop_connection(node);
205                 tnode->connect_te = event_add_timed(ctdb->ev, tnode, 
206                                                     timeval_current_ofs(1, 0),
207                                                     ctdb_tcp_node_connect, node);
208                 return;
209         }
210
211         /* non-blocking connect - wait for write event */
212         tnode->connect_fde = event_add_fd(node->ctdb->ev, tnode, tnode->fd, 
213                                           EVENT_FD_WRITE|EVENT_FD_READ, 
214                                           ctdb_node_connect_write, node);
215
216         /* don't give it long to connect - retry in one second. This ensures
217            that we find a node is up quickly (tcp normally backs off a syn reply
218            delay by quite a lot) */
219         tnode->connect_te = event_add_timed(ctdb->ev, tnode, timeval_current_ofs(1, 0), 
220                                             ctdb_tcp_node_connect, node);
221 }
222
223 /*
224   called when we get contacted by another node
225   currently makes no attempt to check if the connection is really from a ctdb
226   node in our cluster
227 */
228 static void ctdb_listen_event(struct event_context *ev, struct fd_event *fde, 
229                               uint16_t flags, void *private_data)
230 {
231         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
232         struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data, struct ctdb_tcp);
233         ctdb_sock_addr addr;
234         socklen_t len;
235         int fd, nodeid;
236         struct ctdb_incoming *in;
237         int one = 1;
238         const char *incoming_node;
239
240         memset(&addr, 0, sizeof(addr));
241         len = sizeof(addr);
242         fd = accept(ctcp->listen_fd, (struct sockaddr *)&addr, &len);
243         if (fd == -1) return;
244
245         incoming_node = ctdb_addr_to_str(&addr);
246         nodeid = ctdb_ip_to_nodeid(ctdb, incoming_node);
247
248         if (nodeid == -1) {
249                 DEBUG(DEBUG_ERR, ("Refused connection from unknown node %s\n", incoming_node));
250                 close(fd);
251                 return;
252         }
253
254         in = talloc_zero(ctcp, struct ctdb_incoming);
255         in->fd = fd;
256         in->ctdb = ctdb;
257
258         set_nonblocking(in->fd);
259         set_close_on_exec(in->fd);
260
261         DEBUG(DEBUG_DEBUG, (__location__ " Created SOCKET FD:%d to incoming ctdb connection\n", fd));
262
263         setsockopt(in->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one));
264
265         in->queue = ctdb_queue_setup(ctdb, in, in->fd, CTDB_TCP_ALIGNMENT, 
266                                      ctdb_tcp_read_cb, in, "ctdbd-%s", incoming_node);
267 }
268
269
270 /*
271   automatically find which address to listen on
272 */
273 static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
274 {
275         struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
276                                                 struct ctdb_tcp);
277         ctdb_sock_addr sock;
278         int lock_fd, i;
279         const char *lock_path = "/tmp/.ctdb_socket_lock";
280         struct flock lock;
281         int one = 1;
282         int sock_size;
283         struct tevent_fd *fde;
284
285         /* in order to ensure that we don't get two nodes with the
286            same adddress, we must make the bind() and listen() calls
287            atomic. The SO_REUSEADDR setsockopt only prevents double
288            binds if the first socket is in LISTEN state  */
289         lock_fd = open(lock_path, O_RDWR|O_CREAT, 0666);
290         if (lock_fd == -1) {
291                 DEBUG(DEBUG_CRIT,("Unable to open %s\n", lock_path));
292                 return -1;
293         }
294
295         lock.l_type = F_WRLCK;
296         lock.l_whence = SEEK_SET;
297         lock.l_start = 0;
298         lock.l_len = 1;
299         lock.l_pid = 0;
300
301         if (fcntl(lock_fd, F_SETLKW, &lock) != 0) {
302                 DEBUG(DEBUG_CRIT,("Unable to lock %s\n", lock_path));
303                 close(lock_fd);
304                 return -1;
305         }
306
307         for (i=0; i < ctdb->num_nodes; i++) {
308                 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
309                         continue;
310                 }
311
312                 /* if node_ip is specified we will only try to bind to that
313                    ip.
314                 */
315                 if (ctdb->node_ip != NULL) {
316                         if (strcmp(ctdb->node_ip, ctdb->nodes[i]->address.address)) {
317                                 continue;
318                         }
319                 }
320
321                 ZERO_STRUCT(sock);
322                 if (ctdb_tcp_get_address(ctdb,
323                                 ctdb->nodes[i]->address.address, 
324                                 &sock) != 0) {
325                         continue;
326                 }
327         
328                 switch (sock.sa.sa_family) {
329                 case AF_INET:
330                         sock.ip.sin_port = htons(ctdb->nodes[i]->address.port);
331                         sock_size = sizeof(sock.ip);
332                         break;
333                 case AF_INET6:
334                         sock.ip6.sin6_port = htons(ctdb->nodes[i]->address.port);
335                         sock_size = sizeof(sock.ip6);
336                         break;
337                 default:
338                         DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
339                                 sock.sa.sa_family));
340                         continue;
341                 }
342 #ifdef HAVE_SOCK_SIN_LEN
343                 sock.ip.sin_len = sock_size;
344 #endif
345
346                 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
347                 if (ctcp->listen_fd == -1) {
348                         ctdb_set_error(ctdb, "socket failed\n");
349                         continue;
350                 }
351
352                 set_close_on_exec(ctcp->listen_fd);
353
354                 setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
355
356                 if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) == 0) {
357                         break;
358                 }
359
360                 if (errno == EADDRNOTAVAIL) {
361                         DEBUG(DEBUG_DEBUG,(__location__ " Failed to bind() to socket. %s(%d)\n",
362                                         strerror(errno), errno));
363                 } else {
364                         DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n",
365                                         strerror(errno), errno));
366                 }
367         }
368         
369         if (i == ctdb->num_nodes) {
370                 DEBUG(DEBUG_CRIT,("Unable to bind to any of the node addresses - giving up\n"));
371                 goto failed;
372         }
373         ctdb->address.address = talloc_strdup(ctdb, ctdb->nodes[i]->address.address);
374         ctdb->address.port    = ctdb->nodes[i]->address.port;
375         ctdb->name = talloc_asprintf(ctdb, "%s:%u", 
376                                      ctdb->address.address, 
377                                      ctdb->address.port);
378         ctdb->pnn = ctdb->nodes[i]->pnn;
379         ctdb->nodes[i]->flags &= ~NODE_FLAGS_DISCONNECTED;
380         DEBUG(DEBUG_INFO,("ctdb chose network address %s:%u pnn %u\n", 
381                  ctdb->address.address, 
382                  ctdb->address.port, 
383                  ctdb->pnn));
384         /* do we start out in DISABLED mode? */
385         if (ctdb->start_as_disabled != 0) {
386                 DEBUG(DEBUG_INFO, ("This node is configured to start in DISABLED state\n"));
387                 ctdb->nodes[i]->flags |= NODE_FLAGS_DISABLED;
388         }
389         /* do we start out in STOPPED mode? */
390         if (ctdb->start_as_stopped != 0) {
391                 DEBUG(DEBUG_INFO, ("This node is configured to start in STOPPED state\n"));
392                 ctdb->nodes[i]->flags |= NODE_FLAGS_STOPPED;
393         }
394         
395         if (listen(ctcp->listen_fd, 10) == -1) {
396                 goto failed;
397         }
398
399         fde = event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ,
400                            ctdb_listen_event, ctdb);
401         tevent_fd_set_auto_close(fde);
402
403         close(lock_fd);
404         return 0;
405         
406 failed:
407         close(lock_fd);
408         close(ctcp->listen_fd);
409         ctcp->listen_fd = -1;
410         return -1;
411 }
412
413
414 /*
415   listen on our own address
416 */
417 int ctdb_tcp_listen(struct ctdb_context *ctdb)
418 {
419         struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
420                                                 struct ctdb_tcp);
421         ctdb_sock_addr sock;
422         int sock_size;
423         int one = 1;
424         struct tevent_fd *fde;
425
426         /* we can either auto-bind to the first available address, or we can
427            use a specified address */
428         if (!ctdb->address.address) {
429                 return ctdb_tcp_listen_automatic(ctdb);
430         }
431
432         ZERO_STRUCT(sock);
433         if (ctdb_tcp_get_address(ctdb, ctdb->address.address, 
434                                  &sock) != 0) {
435                 goto failed;
436         }
437         
438         switch (sock.sa.sa_family) {
439         case AF_INET:
440                 sock.ip.sin_port = htons(ctdb->address.port);
441                 sock_size = sizeof(sock.ip);
442                 break;
443         case AF_INET6:
444                 sock.ip6.sin6_port = htons(ctdb->address.port);
445                 sock_size = sizeof(sock.ip6);
446                 break;
447         default:
448                 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
449                         sock.sa.sa_family));
450                 goto failed;
451         }
452 #ifdef HAVE_SOCK_SIN_LEN
453         sock.ip.sin_len = sock_size;
454 #endif
455
456         ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
457         if (ctcp->listen_fd == -1) {
458                 ctdb_set_error(ctdb, "socket failed\n");
459                 return -1;
460         }
461
462         set_close_on_exec(ctcp->listen_fd);
463
464         setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
465
466         if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) != 0) {
467                 DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n", strerror(errno), errno));
468                 goto failed;
469         }
470
471         if (listen(ctcp->listen_fd, 10) == -1) {
472                 goto failed;
473         }
474
475         fde = event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ,
476                      ctdb_listen_event, ctdb);  
477         tevent_fd_set_auto_close(fde);
478
479         return 0;
480
481 failed:
482         if (ctcp->listen_fd != -1) {
483                 close(ctcp->listen_fd);
484         }
485         ctcp->listen_fd = -1;
486         return -1;
487 }
488