2 Unix SMB/CIFS implementation.
3 Samba internal messaging functions
4 Copyright (C) 2007 by Volker Lendecke
5 Copyright (C) 2007 by Andrew Tridgell
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.
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.
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/>.
24 #include "ctdbd_conn.h"
25 #include "system/select.h"
26 #include "lib/util/sys_rw_data.h"
27 #include "lib/util/iov_buf.h"
28 #include "lib/util/select.h"
29 #include "lib/util/debug.h"
30 #include "lib/util/talloc_stack.h"
31 #include "lib/util/genrand.h"
32 #include "lib/util/fault.h"
34 /* paths to these include files come from --with-ctdb= in configure */
36 #include "ctdb_private.h"
38 struct ctdbd_srvid_cb {
40 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
42 const uint8_t *msg, size_t msglen,
47 struct ctdbd_connection {
51 struct ctdbd_srvid_cb *callbacks;
53 struct tevent_fd *fde;
57 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
60 if (conn->reqid == 0) {
66 static int ctdbd_control(struct ctdbd_connection *conn,
67 uint32_t vnn, uint32_t opcode,
68 uint64_t srvid, uint32_t flags,
70 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
74 * exit on fatal communications errors with the ctdbd daemon
76 static void cluster_fatal(const char *why)
78 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
79 /* we don't use smb_panic() as we don't want to delay to write
80 a core file. We need to release this process id immediately
81 so that someone else can take over without getting sharing
89 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
91 if (DEBUGLEVEL < 11) {
94 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
95 (int)hdr->length, (int)hdr->ctdb_magic,
96 (int)hdr->ctdb_version, (int)hdr->generation,
97 (int)hdr->operation, (int)hdr->reqid));
101 * Register a srvid with ctdbd
103 int register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid,
104 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
106 const uint8_t *msg, size_t msglen,
113 size_t num_callbacks;
114 struct ctdbd_srvid_cb *tmp;
116 ret = ctdbd_control_local(conn, CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
117 tdb_null, NULL, NULL, &cstatus);
122 num_callbacks = talloc_array_length(conn->callbacks);
124 tmp = talloc_realloc(conn, conn->callbacks, struct ctdbd_srvid_cb,
129 conn->callbacks = tmp;
131 conn->callbacks[num_callbacks] = (struct ctdbd_srvid_cb) {
132 .srvid = srvid, .cb = cb, .private_data = private_data
138 static int ctdbd_msg_call_back(struct ctdbd_connection *conn,
139 struct ctdb_req_message_old *msg)
142 size_t i, num_callbacks;
144 msg_len = msg->hdr.length;
145 if (msg_len < offsetof(struct ctdb_req_message_old, data)) {
146 DBG_DEBUG("len %"PRIu32" too small\n", msg_len);
149 msg_len -= offsetof(struct ctdb_req_message_old, data);
151 if (msg_len < msg->datalen) {
152 DBG_DEBUG("msg_len=%"PRIu32" < msg->datalen=%"PRIu32"\n",
153 msg_len, msg->datalen);
157 num_callbacks = talloc_array_length(conn->callbacks);
159 for (i=0; i<num_callbacks; i++) {
160 struct ctdbd_srvid_cb *cb = &conn->callbacks[i];
162 if ((cb->srvid == msg->srvid) && (cb->cb != NULL)) {
165 ret = cb->cb(msg->hdr.srcnode, msg->hdr.destnode,
166 msg->srvid, msg->data, msg->datalen,
177 * get our vnn from the cluster
179 static int get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
183 ret = ctdbd_control_local(conn, CTDB_CONTROL_GET_PNN, 0, 0,
184 tdb_null, NULL, NULL, &cstatus);
186 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
189 *vnn = (uint32_t)cstatus;
194 * Are we active (i.e. not banned or stopped?)
196 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
200 struct ctdb_node_map_old *m;
205 ret = ctdbd_control_local(conn, CTDB_CONTROL_GET_NODEMAP, 0, 0,
206 tdb_null, talloc_tos(), &outdata, &cstatus);
208 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
211 if ((cstatus != 0) || (outdata.dptr == NULL)) {
212 DEBUG(2, ("Received invalid ctdb data\n"));
216 m = (struct ctdb_node_map_old *)outdata.dptr;
218 for (i=0; i<m->num; i++) {
219 if (vnn == m->nodes[i].pnn) {
225 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
230 if ((m->nodes[i].flags & NODE_FLAGS_INACTIVE) != 0) {
231 DEBUG(2, ("Node has status %x, not active\n",
232 (int)m->nodes[i].flags));
238 TALLOC_FREE(outdata.dptr);
242 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
244 return conn->our_vnn;
248 * Get us a ctdb connection
251 static int ctdbd_connect(const char *sockname, int *pfd)
253 struct sockaddr_un addr = { 0, };
258 fd = socket(AF_UNIX, SOCK_STREAM, 0);
261 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
265 addr.sun_family = AF_UNIX;
267 namelen = strlcpy(addr.sun_path, sockname, sizeof(addr.sun_path));
268 if (namelen >= sizeof(addr.sun_path)) {
269 DEBUG(3, ("%s: Socket name too long: %s\n", __func__,
275 salen = sizeof(struct sockaddr_un);
277 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
279 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
289 static int ctdb_read_packet(int fd, int timeout, TALLOC_CTX *mem_ctx,
290 struct ctdb_req_header **result)
292 struct ctdb_req_header *req;
297 struct pollfd pfd = { .fd = fd, .events = POLLIN };
300 ret = sys_poll_intr(&pfd, 1, timeout);
312 nread = read_data(fd, &msglen, sizeof(msglen));
320 if (msglen < sizeof(struct ctdb_req_header)) {
324 req = talloc_size(mem_ctx, msglen);
328 talloc_set_name_const(req, "struct ctdb_req_header");
330 req->length = msglen;
332 nread = read_data(fd, ((char *)req) + sizeof(msglen),
333 msglen - sizeof(msglen));
348 * Read a full ctdbd request. If we have a messaging context, defer incoming
349 * messages that might come in between.
352 static int ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
353 TALLOC_CTX *mem_ctx, struct ctdb_req_header **result)
355 struct ctdb_req_header *hdr;
360 ret = ctdb_read_packet(conn->fd, conn->timeout, mem_ctx, &hdr);
362 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
363 cluster_fatal("ctdbd died\n");
366 DEBUG(11, ("Received ctdb packet\n"));
367 ctdb_packet_dump(hdr);
369 if (hdr->operation == CTDB_REQ_MESSAGE) {
370 struct ctdb_req_message_old *msg = (struct ctdb_req_message_old *)hdr;
372 ret = ctdbd_msg_call_back(conn, msg);
382 if ((reqid != 0) && (hdr->reqid != reqid)) {
383 /* we got the wrong reply */
384 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
385 "been %u\n", hdr->reqid, reqid));
390 *result = talloc_move(mem_ctx, &hdr);
395 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
405 * Get us a ctdbd connection
408 static int ctdbd_init_connection_internal(TALLOC_CTX *mem_ctx,
409 const char *sockname, int timeout,
410 struct ctdbd_connection *conn)
414 conn->timeout = timeout;
415 if (conn->timeout == 0) {
419 ret = ctdbd_connect(sockname, &conn->fd);
421 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret)));
424 talloc_set_destructor(conn, ctdbd_connection_destructor);
426 ret = get_cluster_vnn(conn, &conn->our_vnn);
428 DEBUG(10, ("get_cluster_vnn failed: %s\n", strerror(ret)));
432 if (!ctdbd_working(conn, conn->our_vnn)) {
433 DEBUG(2, ("Node is not working, can not connect\n"));
437 generate_random_buffer((unsigned char *)&conn->rand_srvid,
438 sizeof(conn->rand_srvid));
440 ret = register_with_ctdbd(conn, conn->rand_srvid, NULL, NULL);
442 DEBUG(5, ("Could not register random srvid: %s\n",
450 int ctdbd_init_connection(TALLOC_CTX *mem_ctx,
451 const char *sockname, int timeout,
452 struct ctdbd_connection **pconn)
454 struct ctdbd_connection *conn;
457 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
458 DEBUG(0, ("talloc failed\n"));
462 ret = ctdbd_init_connection_internal(mem_ctx,
467 DBG_ERR("ctdbd_init_connection_internal failed (%s)\n",
480 int ctdbd_reinit_connection(TALLOC_CTX *mem_ctx,
481 const char *sockname, int timeout,
482 struct ctdbd_connection *conn)
486 ret = ctdbd_connection_destructor(conn);
488 DBG_ERR("ctdbd_connection_destructor failed\n");
492 ret = ctdbd_init_connection_internal(mem_ctx,
497 DBG_ERR("ctdbd_init_connection_internal failed (%s)\n",
505 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
511 * Packet handler to receive and handle a ctdb message
513 static int ctdb_handle_message(struct ctdbd_connection *conn,
514 struct ctdb_req_header *hdr)
516 struct ctdb_req_message_old *msg;
518 if (hdr->operation != CTDB_REQ_MESSAGE) {
519 DEBUG(0, ("Received async msg of type %u, discarding\n",
524 msg = (struct ctdb_req_message_old *)hdr;
526 ctdbd_msg_call_back(conn, msg);
531 void ctdbd_socket_readable(struct ctdbd_connection *conn)
533 struct ctdb_req_header *hdr = NULL;
536 ret = ctdb_read_packet(conn->fd, conn->timeout, talloc_tos(), &hdr);
538 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
539 cluster_fatal("ctdbd died\n");
542 ret = ctdb_handle_message(conn, hdr);
547 DEBUG(10, ("could not handle incoming message: %s\n",
552 int ctdbd_messaging_send_iov(struct ctdbd_connection *conn,
553 uint32_t dst_vnn, uint64_t dst_srvid,
554 const struct iovec *iov, int iovlen)
556 struct ctdb_req_message_old r;
557 struct iovec iov2[iovlen+1];
558 size_t buflen = iov_buflen(iov, iovlen);
561 r.hdr.length = offsetof(struct ctdb_req_message_old, data) + buflen;
562 r.hdr.ctdb_magic = CTDB_MAGIC;
563 r.hdr.ctdb_version = CTDB_PROTOCOL;
564 r.hdr.generation = 1;
565 r.hdr.operation = CTDB_REQ_MESSAGE;
566 r.hdr.destnode = dst_vnn;
567 r.hdr.srcnode = conn->our_vnn;
572 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
573 ctdb_packet_dump(&r.hdr);
575 iov2[0].iov_base = &r;
576 iov2[0].iov_len = offsetof(struct ctdb_req_message_old, data);
577 memcpy(&iov2[1], iov, iovlen * sizeof(struct iovec));
579 nwritten = write_data_iov(conn->fd, iov2, iovlen+1);
580 if (nwritten == -1) {
581 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
582 cluster_fatal("cluster dispatch daemon msg write error\n");
589 * send/recv a generic ctdb control message
591 static int ctdbd_control(struct ctdbd_connection *conn,
592 uint32_t vnn, uint32_t opcode,
593 uint64_t srvid, uint32_t flags,
595 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
598 struct ctdb_req_control_old req;
599 struct ctdb_req_header *hdr;
600 struct ctdb_reply_control_old *reply = NULL;
606 req.hdr.length = offsetof(struct ctdb_req_control_old, data) + data.dsize;
607 req.hdr.ctdb_magic = CTDB_MAGIC;
608 req.hdr.ctdb_version = CTDB_PROTOCOL;
609 req.hdr.operation = CTDB_REQ_CONTROL;
610 req.hdr.reqid = ctdbd_next_reqid(conn);
611 req.hdr.destnode = vnn;
614 req.datalen = data.dsize;
617 DBG_DEBUG("Sending ctdb packet reqid=%"PRIu32", vnn=%"PRIu32", "
618 "opcode=%"PRIu32", srvid=%"PRIu64"\n", req.hdr.reqid,
619 req.hdr.destnode, req.opcode, req.srvid);
620 ctdb_packet_dump(&req.hdr);
622 iov[0].iov_base = &req;
623 iov[0].iov_len = offsetof(struct ctdb_req_control_old, data);
624 iov[1].iov_base = data.dptr;
625 iov[1].iov_len = data.dsize;
627 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
628 if (nwritten == -1) {
629 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
630 cluster_fatal("cluster dispatch daemon msg write error\n");
633 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
640 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
642 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
646 if (hdr->operation != CTDB_REPLY_CONTROL) {
647 DEBUG(0, ("received invalid reply\n"));
651 reply = (struct ctdb_reply_control_old *)hdr;
654 if (!(outdata->dptr = (uint8_t *)talloc_memdup(
655 mem_ctx, reply->data, reply->datalen))) {
659 outdata->dsize = reply->datalen;
662 (*cstatus) = reply->status;
670 * see if a remote process exists
672 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
677 ret = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
678 (TDB_DATA) { .dptr = (uint8_t *)&pid,
679 .dsize = sizeof(pid) },
680 NULL, NULL, &cstatus);
684 return (cstatus == 0);
690 char *ctdbd_dbpath(struct ctdbd_connection *conn,
691 TALLOC_CTX *mem_ctx, uint32_t db_id)
695 TDB_DATA rdata = {0};
698 data.dptr = (uint8_t*)&db_id;
699 data.dsize = sizeof(db_id);
701 ret = ctdbd_control_local(conn, CTDB_CONTROL_GETDBPATH, 0, 0, data,
702 mem_ctx, &rdata, &cstatus);
703 if ((ret != 0) || cstatus != 0) {
704 DEBUG(0, (__location__ " ctdb_control for getdbpath failed: %s\n",
709 return (char *)rdata.dptr;
713 * attach to a ctdb database
715 int ctdbd_db_attach(struct ctdbd_connection *conn,
716 const char *name, uint32_t *db_id, int tdb_flags)
721 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
723 data = string_term_tdb_data(name);
725 ret = ctdbd_control_local(conn,
727 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
728 : CTDB_CONTROL_DB_ATTACH,
729 tdb_flags, 0, data, NULL, &data, &cstatus);
731 DEBUG(0, (__location__ " ctdb_control for db_attach "
732 "failed: %s\n", strerror(ret)));
736 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
737 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
741 *db_id = *(uint32_t *)data.dptr;
742 talloc_free(data.dptr);
744 if (!(tdb_flags & TDB_SEQNUM)) {
748 data.dptr = (uint8_t *)db_id;
749 data.dsize = sizeof(*db_id);
751 ret = ctdbd_control_local(conn, CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
752 NULL, NULL, &cstatus);
753 if ((ret != 0) || cstatus != 0) {
754 DEBUG(0, (__location__ " ctdb_control for enable seqnum "
755 "failed: %s\n", strerror(ret)));
756 return (ret == 0) ? EIO : ret;
763 * force the migration of a record to this node
765 int ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id, TDB_DATA key)
767 struct ctdb_req_call_old req;
768 struct ctdb_req_header *hdr;
775 req.hdr.length = offsetof(struct ctdb_req_call_old, data) + key.dsize;
776 req.hdr.ctdb_magic = CTDB_MAGIC;
777 req.hdr.ctdb_version = CTDB_PROTOCOL;
778 req.hdr.operation = CTDB_REQ_CALL;
779 req.hdr.reqid = ctdbd_next_reqid(conn);
780 req.flags = CTDB_IMMEDIATE_MIGRATION;
781 req.callid = CTDB_NULL_FUNC;
783 req.keylen = key.dsize;
785 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
786 ctdb_packet_dump(&req.hdr);
788 iov[0].iov_base = &req;
789 iov[0].iov_len = offsetof(struct ctdb_req_call_old, data);
790 iov[1].iov_base = key.dptr;
791 iov[1].iov_len = key.dsize;
793 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
794 if (nwritten == -1) {
795 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
796 cluster_fatal("cluster dispatch daemon msg write error\n");
799 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
801 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
805 if (hdr->operation != CTDB_REPLY_CALL) {
806 if (hdr->operation == CTDB_REPLY_ERROR) {
807 DBG_ERR("received error from ctdb\n");
809 DBG_ERR("received invalid reply\n");
822 * Fetch a record and parse it
824 int ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
825 TDB_DATA key, bool local_copy,
826 void (*parser)(TDB_DATA key, TDB_DATA data,
830 struct ctdb_req_call_old req;
831 struct ctdb_req_header *hdr = NULL;
832 struct ctdb_reply_call_old *reply;
838 flags = local_copy ? CTDB_WANT_READONLY : 0;
842 req.hdr.length = offsetof(struct ctdb_req_call_old, data) + key.dsize;
843 req.hdr.ctdb_magic = CTDB_MAGIC;
844 req.hdr.ctdb_version = CTDB_PROTOCOL;
845 req.hdr.operation = CTDB_REQ_CALL;
846 req.hdr.reqid = ctdbd_next_reqid(conn);
848 req.callid = CTDB_FETCH_FUNC;
850 req.keylen = key.dsize;
852 iov[0].iov_base = &req;
853 iov[0].iov_len = offsetof(struct ctdb_req_call_old, data);
854 iov[1].iov_base = key.dptr;
855 iov[1].iov_len = key.dsize;
857 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
858 if (nwritten == -1) {
859 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
860 cluster_fatal("cluster dispatch daemon msg write error\n");
863 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
865 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
869 if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
870 DEBUG(0, ("received invalid reply\n"));
874 reply = (struct ctdb_reply_call_old *)hdr;
876 if (reply->datalen == 0) {
878 * Treat an empty record as non-existing
884 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
894 Traverse a ctdb database. "conn" must be an otherwise unused
895 ctdb_connection where no other messages but the traverse ones are
899 int ctdbd_traverse(struct ctdbd_connection *conn, uint32_t db_id,
900 void (*fn)(TDB_DATA key, TDB_DATA data,
906 struct ctdb_traverse_start t;
910 t.srvid = conn->rand_srvid;
911 t.reqid = ctdbd_next_reqid(conn);
913 data.dptr = (uint8_t *)&t;
914 data.dsize = sizeof(t);
916 ret = ctdbd_control_local(conn, CTDB_CONTROL_TRAVERSE_START,
918 0, data, NULL, NULL, &cstatus);
920 if ((ret != 0) || (cstatus != 0)) {
921 DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret),
926 * We need a mapping here
934 struct ctdb_req_header *hdr = NULL;
935 struct ctdb_req_message_old *m;
936 struct ctdb_rec_data_old *d;
938 ret = ctdb_read_packet(conn->fd, conn->timeout, conn, &hdr);
940 DEBUG(0, ("ctdb_read_packet failed: %s\n",
942 cluster_fatal("ctdbd died\n");
945 if (hdr->operation != CTDB_REQ_MESSAGE) {
946 DEBUG(0, ("Got operation %u, expected a message\n",
947 (unsigned)hdr->operation));
951 m = (struct ctdb_req_message_old *)hdr;
952 d = (struct ctdb_rec_data_old *)&m->data[0];
953 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
954 DEBUG(0, ("Got invalid traverse data of length %d\n",
959 key.dsize = d->keylen;
960 key.dptr = &d->data[0];
961 data.dsize = d->datalen;
962 data.dptr = &d->data[d->keylen];
964 if (key.dsize == 0 && data.dsize == 0) {
965 /* end of traverse */
969 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
970 DEBUG(0, ("Got invalid ltdb header length %d\n",
974 data.dsize -= sizeof(struct ctdb_ltdb_header);
975 data.dptr += sizeof(struct ctdb_ltdb_header);
978 fn(key, data, private_data);
985 This is used to canonicalize a ctdb_sock_addr structure.
987 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
988 struct sockaddr_storage *out)
990 memcpy(out, in, sizeof (*out));
993 if (in->ss_family == AF_INET6) {
994 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
995 const struct sockaddr_in6 *in6 =
996 (const struct sockaddr_in6 *)in;
997 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
998 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
999 memset(out, 0, sizeof(*out));
1000 #ifdef HAVE_SOCK_SIN_LEN
1001 out4->sin_len = sizeof(*out);
1003 out4->sin_family = AF_INET;
1004 out4->sin_port = in6->sin6_port;
1005 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1012 * Register us as a server for a particular tcp connection
1015 int ctdbd_register_ips(struct ctdbd_connection *conn,
1016 const struct sockaddr_storage *_server,
1017 const struct sockaddr_storage *_client,
1018 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
1020 const uint8_t *msg, size_t msglen,
1021 void *private_data),
1024 struct ctdb_connection p;
1025 TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
1027 struct sockaddr_storage client;
1028 struct sockaddr_storage server;
1031 * Only one connection so far
1034 smbd_ctdb_canonicalize_ip(_client, &client);
1035 smbd_ctdb_canonicalize_ip(_server, &server);
1037 switch (client.ss_family) {
1039 memcpy(&p.dst.ip, &server, sizeof(p.dst.ip));
1040 memcpy(&p.src.ip, &client, sizeof(p.src.ip));
1043 memcpy(&p.dst.ip6, &server, sizeof(p.dst.ip6));
1044 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1051 * We want to be told about IP releases
1054 ret = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP,
1061 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1062 * can send an extra ack to trigger a reset for our client, so it
1063 * immediately reconnects
1065 ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
1066 CTDB_CONTROL_TCP_CLIENT, 0,
1067 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL,
1076 call a control on the local node
1078 int ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1079 uint64_t srvid, uint32_t flags, TDB_DATA data,
1080 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1083 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data,
1084 mem_ctx, outdata, cstatus);
1087 int ctdb_watch_us(struct ctdbd_connection *conn)
1089 struct ctdb_notify_data_old reg_data;
1094 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1096 reg_data.notify_data[0] = 0;
1098 struct_len = offsetof(struct ctdb_notify_data_old,
1099 notify_data) + reg_data.len;
1101 ret = ctdbd_control_local(
1102 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1103 make_tdb_data((uint8_t *)®_data, struct_len),
1104 NULL, NULL, &cstatus);
1106 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1112 int ctdb_unwatch(struct ctdbd_connection *conn)
1114 uint64_t srvid = CTDB_SRVID_SAMBA_NOTIFY;
1118 ret = ctdbd_control_local(
1119 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1120 make_tdb_data((uint8_t *)&srvid, sizeof(srvid)),
1121 NULL, NULL, &cstatus);
1123 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1129 int ctdbd_probe(const char *sockname, int timeout)
1132 * Do a very early check if ctdbd is around to avoid an abort and core
1135 struct ctdbd_connection *conn = NULL;
1138 ret = ctdbd_init_connection(talloc_tos(), sockname, timeout,
1142 * We only care if we can connect.