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