summaryrefslogtreecommitdiff
path: root/ctdb/protocol/protocol_util.c
blob: c67aa2e056f60dc67ffb7e3b67612704c85c5a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
   CTDB protocol marshalling

   Copyright (C) Amitay Isaacs  2015

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/network.h"

#include <talloc.h>

#include "common/line.h"

#include "protocol.h"
#include "protocol_util.h"
#include "lib/util/util.h"

static struct {
	enum ctdb_runstate runstate;
	const char * label;
} runstate_map[] = {
	{ CTDB_RUNSTATE_UNKNOWN, "UNKNOWN" },
	{ CTDB_RUNSTATE_INIT, "INIT" },
	{ CTDB_RUNSTATE_SETUP, "SETUP" },
	{ CTDB_RUNSTATE_FIRST_RECOVERY, "FIRST_RECOVERY" },
	{ CTDB_RUNSTATE_STARTUP, "STARTUP" },
	{ CTDB_RUNSTATE_RUNNING, "RUNNING" },
	{ CTDB_RUNSTATE_SHUTDOWN, "SHUTDOWN" },
	{ -1, NULL },
};

const char *ctdb_runstate_to_string(enum ctdb_runstate runstate)
{
	int i;

	for (i=0; runstate_map[i].label != NULL; i++) {
		if (runstate_map[i].runstate == runstate) {
			return runstate_map[i].label;
		}
	}

	return runstate_map[0].label;
}

enum ctdb_runstate ctdb_runstate_from_string(const char *runstate_str)
{
	int i;

	for (i=0; runstate_map[i].label != NULL; i++) {
		if (strcasecmp(runstate_map[i].label,
			       runstate_str) == 0) {
			return runstate_map[i].runstate;
		}
	}

	return CTDB_RUNSTATE_UNKNOWN;
}

static struct {
	enum ctdb_event event;
	const char *label;
} event_map[] = {
	{ CTDB_EVENT_INIT, "init" },
	{ CTDB_EVENT_SETUP, "setup" },
	{ CTDB_EVENT_STARTUP, "startup" },
	{ CTDB_EVENT_START_RECOVERY, "startrecovery" },
	{ CTDB_EVENT_RECOVERED, "recovered" },
	{ CTDB_EVENT_TAKE_IP, "takeip" },
	{ CTDB_EVENT_RELEASE_IP, "releaseip" },
	{ CTDB_EVENT_MONITOR, "monitor" },
	{ CTDB_EVENT_SHUTDOWN, "shutdown" },
	{ CTDB_EVENT_UPDATE_IP, "updateip" },
	{ CTDB_EVENT_IPREALLOCATED, "ipreallocated" },
	{ CTDB_EVENT_MAX, "all" },
	{ -1, NULL },
};

const char *ctdb_event_to_string(enum ctdb_event event)
{
	int i;

	for (i=0; event_map[i].label != NULL; i++) {
		if (event_map[i].event == event) {
			return event_map[i].label;
		}
	}

	return "unknown";
}

enum ctdb_event ctdb_event_from_string(const char *event_str)
{
	int i;

	for (i=0; event_map[i].label != NULL; i++) {
		if (strcmp(event_map[i].label, event_str) == 0) {
			return event_map[i].event;
		}
	}

	return CTDB_EVENT_MAX;
}

int ctdb_sock_addr_to_buf(char *buf, socklen_t buflen,
			  ctdb_sock_addr *addr, bool with_port)
{
	const char *t;

	switch (addr->sa.sa_family) {
	case AF_INET:
		t = inet_ntop(addr->ip.sin_family, &addr->ip.sin_addr,
			      buf, buflen);
		if (t == NULL) {
			return errno;
		}
		break;

	case AF_INET6:
		t = inet_ntop(addr->ip6.sin6_family, &addr->ip6.sin6_addr,
			      buf, buflen);
		if (t == NULL) {
			return errno;
		}
		break;

	default:
		return EAFNOSUPPORT;
		break;
	}

	if (with_port) {
		size_t len = strlen(buf);
		int ret;

		ret = snprintf(buf+len, buflen-len,
			       ":%u", ctdb_sock_addr_port(addr));
		if (ret < 0 || (size_t)ret >= buflen-len) {
			return ENOSPC;
		}
	}

	return 0;
}

char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx,
			       ctdb_sock_addr *addr,
			       bool with_port)
{
	size_t len = 64;
	char *cip;
	int ret;

	cip = talloc_size(mem_ctx, len);

	if (cip == NULL) {
		return NULL;
	}

	ret = ctdb_sock_addr_to_buf(cip, len, addr, with_port);
	if (ret != 0) {
		talloc_free(cip);
		return NULL;
	}

	return cip;
}

static int ipv4_from_string(const char *str, struct sockaddr_in *ip)
{
	int ret;

	*ip = (struct sockaddr_in) {
		.sin_family = AF_INET,
	};

	ret = inet_pton(AF_INET, str, &ip->sin_addr);
	if (ret != 1) {
		return EINVAL;
	}

#ifdef HAVE_SOCK_SIN_LEN
	ip->sin_len = sizeof(*ip);
#endif
	return 0;
}

static int ipv6_from_string(const char *str, struct sockaddr_in6 *ip6)
{
	int ret;

	*ip6 = (struct sockaddr_in6) {
		.sin6_family   = AF_INET6,
	};

	ret = inet_pton(AF_INET6, str, &ip6->sin6_addr);
	if (ret != 1) {
		return EINVAL;
	}

#ifdef HAVE_SOCK_SIN6_LEN
	ip6->sin6_len = sizeof(*ip6);
#endif
	return 0;
}

static int ip_from_string(const char *str, ctdb_sock_addr *addr)
{
	char *p;
	int ret;

	if (addr == NULL) {
		return EINVAL;
	}

	ZERO_STRUCTP(addr); /* valgrind :-) */

	/* IPv4 or IPv6 address?
	 *
	 * Use rindex() because we need the right-most ':' below for
	 * IPv4-mapped IPv6 addresses anyway...
	 */
	p = rindex(str, ':');
	if (p == NULL) {
		ret = ipv4_from_string(str, &addr->ip);
	} else {
		uint8_t ipv4_mapped_prefix[12] = {
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
		};

		ret = ipv6_from_string(str, &addr->ip6);
		if (ret != 0) {
			return ret;
		}

		/*
		 * Check for IPv4-mapped IPv6 address
		 * (e.g. ::ffff:192.0.2.128) - reparse as IPv4 if
		 * necessary
		 */
		if (memcmp(&addr->ip6.sin6_addr.s6_addr[0],
			   ipv4_mapped_prefix,
			   sizeof(ipv4_mapped_prefix)) == 0) {
                        /* Initialize addr struct to zero before reparsing as IPV4 */
                        ZERO_STRUCTP(addr);

			/* Reparse as IPv4 */
			ret = ipv4_from_string(p+1, &addr->ip);
		}
	}

	return ret;
}

int ctdb_sock_addr_from_string(const char *str,
			       ctdb_sock_addr *addr, bool with_port)
{
	char *p;
	char s[64]; /* Much longer than INET6_ADDRSTRLEN */
	unsigned port;
	size_t len;
	int ret;

	if (! with_port) {
		ret = ip_from_string(str, addr);
		return ret;
	}

	/* Parse out port number and then IP address */

	len = strlcpy(s, str, sizeof(s));
	if (len >= sizeof(s)) {
		return EINVAL;
	}

	p = rindex(s, ':');
	if (p == NULL) {
		return EINVAL;
	}

	port = smb_strtoul(p+1, NULL, 10, &ret, SMB_STR_FULL_STR_CONV);
	if (ret != 0) {
		/* Empty string or trailing garbage */
		return EINVAL;
	}

	*p = '\0';
	ret = ip_from_string(s, addr);

	ctdb_sock_addr_set_port(addr, port);

	return ret;
}

int ctdb_sock_addr_mask_from_string(const char *str,
				    ctdb_sock_addr *addr,
				    unsigned int *mask)
{
	char *p;
	char s[64]; /* Much longer than INET6_ADDRSTRLEN */
	unsigned int m;
	size_t len;
	int ret = 0;

	if (addr == NULL || mask == NULL) {
		return EINVAL;
	}

	len = strlcpy(s, str, sizeof(s));
	if (len >= sizeof(s)) {
		return EINVAL;
	}

	p = rindex(s, '/');
	if (p == NULL) {
		return EINVAL;
	}

	m = smb_strtoul(p+1, NULL, 10, &ret, SMB_STR_FULL_STR_CONV);
	if (ret != 0) {
		/* Empty string or trailing garbage */
		return EINVAL;
	}

	*p = '\0';
	ret = ip_from_string(s, addr);

	if (ret == 0) {
		*mask = m;
	}

	return ret;
}

unsigned int ctdb_sock_addr_port(ctdb_sock_addr *addr)
{
	switch (addr->sa.sa_family) {
	case AF_INET:
		return ntohs(addr->ip.sin_port);
		break;
	case AF_INET6:
		return ntohs(addr->ip6.sin6_port);
		break;
	default:
		return 0;
	}
}

void ctdb_sock_addr_set_port(ctdb_sock_addr *addr, unsigned int port)
{
	switch (addr->sa.sa_family) {
	case AF_INET:
		addr->ip.sin_port = htons(port);
		break;
	case AF_INET6:
		addr->ip6.sin6_port = htons(port);
		break;
	default:
		break;
	}
}

static int ctdb_sock_addr_cmp_family(const ctdb_sock_addr *addr1,
				     const ctdb_sock_addr *addr2)
{
	/* This is somewhat arbitrary.  However, when used for sorting
	 * it just needs to be consistent.
	 */
	if (addr1->sa.sa_family < addr2->sa.sa_family) {
		return -1;
	}
	if (addr1->sa.sa_family > addr2->sa.sa_family) {
		return 1;
	}

	return 0;
}

int ctdb_sock_addr_cmp_ip(const ctdb_sock_addr *addr1,
			  const ctdb_sock_addr *addr2)
{
	int ret;

	ret = ctdb_sock_addr_cmp_family(addr1, addr2);
	if (ret != 0) {
		return ret;
	}

	switch (addr1->sa.sa_family) {
	case AF_INET:
		ret = memcmp(&addr1->ip.sin_addr.s_addr,
			     &addr2->ip.sin_addr.s_addr, 4);
		break;

	case AF_INET6:
		ret = memcmp(addr1->ip6.sin6_addr.s6_addr,
			     addr2->ip6.sin6_addr.s6_addr, 16);
		break;

	default:
		ret = -1;
	}

	return ret;
}

int ctdb_sock_addr_cmp(const ctdb_sock_addr *addr1,
		       const ctdb_sock_addr *addr2)
{
	int ret = 0;

	ret = ctdb_sock_addr_cmp_ip(addr1, addr2);
	if (ret != 0) {
		return ret;
	}

	switch (addr1->sa.sa_family) {
	case AF_INET:
		if (addr1->ip.sin_port < addr2->ip.sin_port) {
			ret = -1;
		} else if (addr1->ip.sin_port > addr2->ip.sin_port) {
			ret = 1;
		}
		break;

	case AF_INET6:
		if (addr1->ip6.sin6_port < addr2->ip6.sin6_port) {
			ret = -1;
		} else if (addr1->ip6.sin6_port > addr2->ip6.sin6_port) {
			ret = 1;
		}
		break;

	default:
		ret = -1;
	}

	return ret;
}

bool ctdb_sock_addr_same_ip(const ctdb_sock_addr *addr1,
			    const ctdb_sock_addr *addr2)
{
	return (ctdb_sock_addr_cmp_ip(addr1, addr2) == 0);
}

bool ctdb_sock_addr_same(const ctdb_sock_addr *addr1,
			 const ctdb_sock_addr *addr2)
{
	return (ctdb_sock_addr_cmp(addr1, addr2) == 0);
}

int ctdb_connection_to_buf(char *buf, size_t buflen,
			   struct ctdb_connection *conn, bool client_first)
{
	char server[64], client[64];
	int ret;

	ret = ctdb_sock_addr_to_buf(server, sizeof(server),
				    &conn->server, true);
	if (ret != 0) {
		return ret;
	}

	ret = ctdb_sock_addr_to_buf(client, sizeof(client),
				    &conn->client, true);
	if (ret != 0) {
		return ret;
	}

	if (! client_first) {
		ret = snprintf(buf, buflen, "%s %s", server, client);
	} else {
		ret = snprintf(buf, buflen, "%s %s", client, server);
	}
	if (ret < 0 || (size_t)ret >= buflen) {
		return ENOSPC;
	}

	return 0;
}

char *ctdb_connection_to_string(TALLOC_CTX *mem_ctx,
				struct ctdb_connection *conn,
				bool client_first)
{
	const size_t len = 128;
	char *out;
	int ret;

	out = talloc_size(mem_ctx, len);
	if (out == NULL) {
		return NULL;
	}

	ret = ctdb_connection_to_buf(out, len, conn, client_first);
	if (ret != 0) {
		talloc_free(out);
		return NULL;
	}

	return out;
}

int ctdb_connection_from_string(const char *str, bool client_first,
				struct ctdb_connection *conn)
{
	char s[128];
	char *t1 = NULL, *t2 = NULL;
	size_t len;
	ctdb_sock_addr *first = (client_first ? &conn->client : &conn->server);
	ctdb_sock_addr *second = (client_first ? &conn->server : &conn->client);
	int ret;

	len = strlcpy(s, str, sizeof(s));
	if (len >= sizeof(s)) {
		return EINVAL;
	}

	t1 = strtok(s, " \t\n");
	if (t1 == NULL) {
		return EINVAL;
	}

	t2 = strtok(NULL, " \t\n\0");
	if (t2 == NULL) {
		return EINVAL;
	}

	ret = ctdb_sock_addr_from_string(t1, first, true);
	if (ret != 0) {
		return ret;
	}

	ret = ctdb_sock_addr_from_string(t2, second, true);
	if (ret != 0) {
		return ret;
	}

	ret = ctdb_sock_addr_cmp_family(first, second);
	if (ret != 0) {
		return EINVAL;
	}

	return 0;
}

int ctdb_connection_list_add(struct ctdb_connection_list *conn_list,
			     struct ctdb_connection *conn)
{
	uint32_t len;

	if (conn_list == NULL) {
		return EINVAL;
	}

	/* Ensure array is big enough */
	len = talloc_array_length(conn_list->conn);
	if (conn_list->num == len) {
		conn_list->conn = talloc_realloc(conn_list, conn_list->conn,
						 struct ctdb_connection,
						 len+128);
		if (conn_list->conn == NULL) {
			return ENOMEM;
		}
	}

	conn_list->conn[conn_list->num] = *conn;
	conn_list->num++;

	return 0;
}

static int connection_cmp(const void *a, const void *b)
{
	const struct ctdb_connection *conn_a = a;
	const struct ctdb_connection *conn_b = b;
	int ret;

	ret = ctdb_sock_addr_cmp(&conn_a->server, &conn_b->server);
	if (ret == 0) {
		ret = ctdb_sock_addr_cmp(&conn_a->client, &conn_b->client);
	}

	return ret;
}

int ctdb_connection_list_sort(struct ctdb_connection_list *conn_list)
{
	if (conn_list == NULL) {
		return EINVAL;
	}

	if (conn_list->num > 0) {
		qsort(conn_list->conn, conn_list->num,
		      sizeof(struct ctdb_connection), connection_cmp);
	}

	return 0;
}

char *ctdb_connection_list_to_string(
	TALLOC_CTX *mem_ctx,
	struct ctdb_connection_list *conn_list, bool client_first)
{
	uint32_t i;
	char *out;

	out = talloc_strdup(mem_ctx, "");
	if (out == NULL) {
		return NULL;
	}

	if (conn_list == NULL || conn_list->num == 0) {
		return out;
	}

	for (i = 0; i < conn_list->num; i++) {
		char buf[128];
		int ret;

		ret = ctdb_connection_to_buf(buf, sizeof(buf),
					     &conn_list->conn[i], client_first);
		if (ret != 0) {
			talloc_free(out);
			return NULL;
		}

		out = talloc_asprintf_append(out, "%s\n", buf);
		if (out == NULL) {
			return NULL;
		}
	}

	return out;
}

struct ctdb_connection_list_read_state {
	struct ctdb_connection_list *list;
	bool client_first;
};

static int ctdb_connection_list_read_line(char *line, void *private_data)
{
	struct ctdb_connection_list_read_state *state =
		(struct ctdb_connection_list_read_state *)private_data;
	struct ctdb_connection conn;
	int ret;

	/* Skip empty lines */
	if (line[0] == '\0') {
		return 0;
	}

	/* Comment */
	if (line[0] == '#') {
		return 0;
	}

	ret = ctdb_connection_from_string(line, state->client_first, &conn);
	if (ret != 0) {
		return ret;
	}

	ret = ctdb_connection_list_add(state->list, &conn);
	if (ret != 0) {
		return ret;
	}

	return 0;
}

int ctdb_connection_list_read(TALLOC_CTX *mem_ctx,
			      int fd,
			      bool client_first,
			      struct ctdb_connection_list **conn_list)
{
	struct ctdb_connection_list_read_state state;
	int ret;

	if (conn_list == NULL) {
		return EINVAL;
	}

	state.list = talloc_zero(mem_ctx, struct ctdb_connection_list);
	if (state.list == NULL) {
		return ENOMEM;
	}

	state.client_first = client_first;

	ret = line_read(fd,
			128,
			mem_ctx,
			ctdb_connection_list_read_line,
			&state,
			NULL);

	*conn_list = state.list;

	return ret;
}