ctdb-daemon: Store node addresses as ctdb_sock_addr rather than strings
[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 "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         if (setsockopt(tnode->fd,IPPROTO_TCP,TCP_NODELAY,(char *)&one,sizeof(one)) == -1) {
99                 DEBUG(DEBUG_WARNING, ("Failed to set TCP_NODELAY on fd - %s\n",
100                                       strerror(errno)));
101         }
102         if (setsockopt(tnode->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one)) == -1) {
103                 DEBUG(DEBUG_WARNING, ("Failed to set KEEPALIVE on fd - %s\n",
104                                       strerror(errno)));
105         }
106
107         ctdb_queue_set_fd(tnode->out_queue, tnode->fd);
108
109         /* the queue subsystem now owns this fd */
110         tnode->fd = -1;
111 }
112
113
114 /*
115   called when we should try and establish a tcp connection to a node
116 */
117 void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te, 
118                            struct timeval t, void *private_data)
119 {
120         struct ctdb_node *node = talloc_get_type(private_data,
121                                                  struct ctdb_node);
122         struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data, 
123                                                       struct ctdb_tcp_node);
124         struct ctdb_context *ctdb = node->ctdb;
125         ctdb_sock_addr sock_in;
126         int sockin_size;
127         int sockout_size;
128         ctdb_sock_addr sock_out;
129
130         ctdb_tcp_stop_connection(node);
131
132         sock_out = node->address;
133
134         tnode->fd = socket(sock_out.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
135         if (tnode->fd == -1) {
136                 DEBUG(DEBUG_ERR, (__location__ "Failed to create socket\n"));
137                 return;
138         }
139         set_nonblocking(tnode->fd);
140         set_close_on_exec(tnode->fd);
141
142         DEBUG(DEBUG_DEBUG, (__location__ " Created TCP SOCKET FD:%d\n", tnode->fd));
143
144         /* Bind our side of the socketpair to the same address we use to listen
145          * on incoming CTDB traffic.
146          * We must specify this address to make sure that the address we expose to
147          * the remote side is actually routable in case CTDB traffic will run on
148          * a dedicated non-routeable network.
149          */
150         sock_in = *ctdb->address;
151
152         /* AIX libs check to see if the socket address and length
153            arguments are consistent with each other on calls like
154            connect().   Can not get by with just sizeof(sock_in),
155            need sizeof(sock_in.ip).
156         */
157         switch (sock_in.sa.sa_family) {
158         case AF_INET:
159                 sock_in.ip.sin_port = 0 /* Any port */;
160                 sockin_size = sizeof(sock_in.ip);
161                 sockout_size = sizeof(sock_out.ip);
162                 break;
163         case AF_INET6:
164                 sock_in.ip6.sin6_port = 0 /* Any port */;
165                 sockin_size = sizeof(sock_in.ip6);
166                 sockout_size = sizeof(sock_out.ip6);
167                 break;
168         default:
169                 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
170                         sock_in.sa.sa_family));
171                 close(tnode->fd);
172                 return;
173         }
174
175         if (bind(tnode->fd, (struct sockaddr *)&sock_in, sockin_size) == -1) {
176                 DEBUG(DEBUG_ERR, (__location__ "Failed to bind socket %s(%d)\n",
177                                   strerror(errno), errno));
178                 close(tnode->fd);
179                 return;
180         }
181
182         if (connect(tnode->fd, (struct sockaddr *)&sock_out, sockout_size) != 0 &&
183             errno != EINPROGRESS) {
184                 ctdb_tcp_stop_connection(node);
185                 tnode->connect_te = event_add_timed(ctdb->ev, tnode, 
186                                                     timeval_current_ofs(1, 0),
187                                                     ctdb_tcp_node_connect, node);
188                 return;
189         }
190
191         /* non-blocking connect - wait for write event */
192         tnode->connect_fde = event_add_fd(node->ctdb->ev, tnode, tnode->fd, 
193                                           EVENT_FD_WRITE|EVENT_FD_READ, 
194                                           ctdb_node_connect_write, node);
195
196         /* don't give it long to connect - retry in one second. This ensures
197            that we find a node is up quickly (tcp normally backs off a syn reply
198            delay by quite a lot) */
199         tnode->connect_te = event_add_timed(ctdb->ev, tnode, timeval_current_ofs(1, 0), 
200                                             ctdb_tcp_node_connect, node);
201 }
202
203 /*
204   called when we get contacted by another node
205   currently makes no attempt to check if the connection is really from a ctdb
206   node in our cluster
207 */
208 static void ctdb_listen_event(struct event_context *ev, struct fd_event *fde, 
209                               uint16_t flags, void *private_data)
210 {
211         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
212         struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data, struct ctdb_tcp);
213         ctdb_sock_addr addr;
214         socklen_t len;
215         int fd, nodeid;
216         struct ctdb_incoming *in;
217         int one = 1;
218
219         memset(&addr, 0, sizeof(addr));
220         len = sizeof(addr);
221         fd = accept(ctcp->listen_fd, (struct sockaddr *)&addr, &len);
222         if (fd == -1) return;
223
224         nodeid = ctdb_ip_to_nodeid(ctdb, &addr);
225
226         if (nodeid == -1) {
227                 DEBUG(DEBUG_ERR, ("Refused connection from unknown node %s\n", ctdb_addr_to_str(&addr)));
228                 close(fd);
229                 return;
230         }
231
232         in = talloc_zero(ctcp, struct ctdb_incoming);
233         in->fd = fd;
234         in->ctdb = ctdb;
235
236         set_nonblocking(in->fd);
237         set_close_on_exec(in->fd);
238
239         DEBUG(DEBUG_DEBUG, (__location__ " Created SOCKET FD:%d to incoming ctdb connection\n", fd));
240
241         if (setsockopt(in->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one)) == -1) {
242                 DEBUG(DEBUG_WARNING, ("Failed to set KEEPALIVE on fd - %s\n",
243                                       strerror(errno)));
244         }
245
246         in->queue = ctdb_queue_setup(ctdb, in, in->fd, CTDB_TCP_ALIGNMENT,
247                                      ctdb_tcp_read_cb, in, "ctdbd-%s", ctdb_addr_to_str(&addr));
248 }
249
250
251 /*
252   automatically find which address to listen on
253 */
254 static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
255 {
256         struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
257                                                 struct ctdb_tcp);
258         ctdb_sock_addr sock;
259         int lock_fd, i;
260         const char *lock_path = CTDB_RUNDIR "/.socket_lock";
261         struct flock lock;
262         int one = 1;
263         int sock_size;
264         struct tevent_fd *fde;
265
266         /* If there are no nodes, then it won't be possible to find
267          * the first one.  Log a failure and short circuit the whole
268          * process.
269          */
270         if (ctdb->num_nodes == 0) {
271                 DEBUG(DEBUG_CRIT,("No nodes available to attempt bind to - is the nodes file empty?\n"));
272                 return -1;
273         }
274
275         /* in order to ensure that we don't get two nodes with the
276            same adddress, we must make the bind() and listen() calls
277            atomic. The SO_REUSEADDR setsockopt only prevents double
278            binds if the first socket is in LISTEN state  */
279         lock_fd = open(lock_path, O_RDWR|O_CREAT, 0666);
280         if (lock_fd == -1) {
281                 DEBUG(DEBUG_CRIT,("Unable to open %s\n", lock_path));
282                 return -1;
283         }
284
285         lock.l_type = F_WRLCK;
286         lock.l_whence = SEEK_SET;
287         lock.l_start = 0;
288         lock.l_len = 1;
289         lock.l_pid = 0;
290
291         if (fcntl(lock_fd, F_SETLKW, &lock) != 0) {
292                 DEBUG(DEBUG_CRIT,("Unable to lock %s\n", lock_path));
293                 close(lock_fd);
294                 return -1;
295         }
296
297         for (i=0; i < ctdb->num_nodes; i++) {
298                 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
299                         continue;
300                 }
301                 sock = ctdb->nodes[i]->address;
302
303                 switch (sock.sa.sa_family) {
304                 case AF_INET:
305                         sock_size = sizeof(sock.ip);
306                         break;
307                 case AF_INET6:
308                         sock_size = sizeof(sock.ip6);
309                         break;
310                 default:
311                         DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
312                                 sock.sa.sa_family));
313                         continue;
314                 }
315
316                 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
317                 if (ctcp->listen_fd == -1) {
318                         ctdb_set_error(ctdb, "socket failed\n");
319                         continue;
320                 }
321
322                 set_close_on_exec(ctcp->listen_fd);
323
324                 if (setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,
325                                (char *)&one,sizeof(one)) == -1) {
326                         DEBUG(DEBUG_WARNING, ("Failed to set REUSEADDR on fd - %s\n",
327                                               strerror(errno)));
328                 }
329
330                 if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) == 0) {
331                         break;
332                 }
333
334                 if (errno == EADDRNOTAVAIL) {
335                         DEBUG(DEBUG_DEBUG,(__location__ " Failed to bind() to socket. %s(%d)\n",
336                                         strerror(errno), errno));
337                 } else {
338                         DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n",
339                                         strerror(errno), errno));
340                 }
341         }
342
343         if (i == ctdb->num_nodes) {
344                 DEBUG(DEBUG_CRIT,("Unable to bind to any of the node addresses - giving up\n"));
345                 goto failed;
346         }
347         ctdb->address = talloc_memdup(ctdb,
348                                       &ctdb->nodes[i]->address,
349                                       sizeof(ctdb_sock_addr));
350         CTDB_NO_MEMORY(ctdb, ctdb->address);
351         ctdb->name = talloc_asprintf(ctdb, "%s:%u",
352                                      ctdb_addr_to_str(ctdb->address),
353                                      ctdb_addr_to_port(ctdb->address));
354         DEBUG(DEBUG_INFO,("ctdb chose network address %s\n", ctdb->name));
355
356         if (listen(ctcp->listen_fd, 10) == -1) {
357                 goto failed;
358         }
359
360         fde = event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ,
361                            ctdb_listen_event, ctdb);
362         tevent_fd_set_auto_close(fde);
363
364         close(lock_fd);
365
366         return 0;
367
368 failed:
369         close(lock_fd);
370         if (ctcp->listen_fd != -1) {
371                 close(ctcp->listen_fd);
372                 ctcp->listen_fd = -1;
373         }
374         return -1;
375 }
376
377
378 /*
379   listen on our own address
380 */
381 int ctdb_tcp_listen(struct ctdb_context *ctdb)
382 {
383         struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
384                                                 struct ctdb_tcp);
385         ctdb_sock_addr sock;
386         int sock_size;
387         int one = 1;
388         struct tevent_fd *fde;
389
390         /* we can either auto-bind to the first available address, or we can
391            use a specified address */
392         if (!ctdb->address) {
393                 return ctdb_tcp_listen_automatic(ctdb);
394         }
395
396         sock = *ctdb->address;
397
398         switch (sock.sa.sa_family) {
399         case AF_INET:
400                 sock_size = sizeof(sock.ip);
401                 break;
402         case AF_INET6:
403                 sock_size = sizeof(sock.ip6);
404                 break;
405         default:
406                 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
407                         sock.sa.sa_family));
408                 goto failed;
409         }
410
411         ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
412         if (ctcp->listen_fd == -1) {
413                 ctdb_set_error(ctdb, "socket failed\n");
414                 return -1;
415         }
416
417         set_close_on_exec(ctcp->listen_fd);
418
419         if (setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one)) == -1) {
420                 DEBUG(DEBUG_WARNING, ("Failed to set REUSEADDR on fd - %s\n",
421                                       strerror(errno)));
422         }
423
424         if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) != 0) {
425                 DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n", strerror(errno), errno));
426                 goto failed;
427         }
428
429         if (listen(ctcp->listen_fd, 10) == -1) {
430                 goto failed;
431         }
432
433         fde = event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ,
434                      ctdb_listen_event, ctdb);  
435         tevent_fd_set_auto_close(fde);
436
437         return 0;
438
439 failed:
440         if (ctcp->listen_fd != -1) {
441                 close(ctcp->listen_fd);
442         }
443         ctcp->listen_fd = -1;
444         return -1;
445 }
446