r20991: use relative includes for ctdb headers. This works with both
[Samba/gbeck.git] / source / cluster / ctdb / tcp / tcp_connect.c
blobe828bb7cbba2bb6a546465311eccce927213ce19
1 /*
2 ctdb over TCP
4 Copyright (C) Andrew Tridgell 2006
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.
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.
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
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"
29 static void set_nonblocking(int fd)
31 unsigned v;
32 v = fcntl(fd, F_GETFL, 0);
33 fcntl(fd, F_SETFL, v | O_NONBLOCK);
38 called when socket becomes writeable on connect
40 static void ctdb_node_connect_write(struct event_context *ev, struct fd_event *fde,
41 uint16_t flags, void *private)
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);
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;
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);
64 /* tell the ctdb layer we are connected */
65 node->ctdb->upcalls->node_connected(node);
67 if (tnode->queue) {
68 EVENT_FD_WRITEABLE(tnode->fde);
73 called when we should try and establish a tcp connection to a node
75 void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te,
76 struct timeval t, void *private)
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;
84 tnode->fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
86 set_nonblocking(tnode->fd);
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;
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;
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);
107 destroy a ctdb_incoming structure
109 static int ctdb_incoming_destructor(struct ctdb_incoming *in)
111 close(in->fd);
112 in->fd = -1;
113 return 0;
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
121 static void ctdb_listen_event(struct event_context *ev, struct fd_event *fde,
122 uint16_t flags, void *private)
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;
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;
138 in = talloc_zero(ctdb, struct ctdb_incoming);
139 in->fd = fd;
140 in->ctdb = ctdb;
142 set_nonblocking(in->fd);
144 event_add_fd(ctdb->ev, in, in->fd, EVENT_FD_READ,
145 ctdb_tcp_incoming_read, in);
147 talloc_set_destructor(in, ctdb_incoming_destructor);
152 listen on our own address
154 int ctdb_tcp_listen(struct ctdb_context *ctdb)
156 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private, struct ctdb_tcp);
157 struct sockaddr_in sock;
158 int one = 1;
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);
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;
170 setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
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;
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;
186 event_add_fd(ctdb->ev, ctdb, ctcp->listen_fd, EVENT_FD_READ,
187 ctdb_listen_event, ctdb);
189 return 0;