summaryrefslogtreecommitdiff
path: root/minecctl/misc-commands.c
blob: 28d9e3a6c962a9661f59f1a2e8aad30bb1c2c0f2 (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
#include "shared/utils.h"
#include "minecctl.h"
#include "server.h"
#include "misc-commands.h"
#include "rcon-commands.h"
#include "mc-commands.h"

bool do_list(struct cfg *cfg)
{
	struct server *server;

	/* server->scfg.filename check excludes servers created from cmdline */
	list_for_each_entry(server, &cfg->servers, list)
		if (server->scfg.filename)
			info("• %s", server->name);

	return true;
}

static bool saddr_match(struct list_head *la, struct list_head *lb)
{
	struct saddr *a, *b;

	list_for_each_entry(a, la, list) {
		list_for_each_entry(b, lb, list) {
			if (a->st.ss_family != b->st.ss_family)
				continue;

			switch (a->st.ss_family) {
			case AF_INET:
				if (memcmp(&a->in4.sin_addr,
					   &b->in4.sin_addr,
					   sizeof(a->in4.sin_addr)))
					continue;
				if (a->in4.sin_port != b->in4.sin_port)
					continue;
				return true;

			case AF_INET6:
				if (memcmp(&a->in6.sin6_addr,
					   &b->in6.sin6_addr,
					   sizeof(a->in6.sin6_addr)))
					continue;
				if (a->in6.sin6_port != b->in6.sin6_port)
					continue;
				return true;

			default:
				continue;
			}
		}
	}

	return false;
}

bool do_lint(struct cfg *cfg)
{
	struct server *server;
	unsigned lineno;
	const char *error;
	bool rv = true;
	struct server *a, *b;
	unsigned ia, ib;

	/* server->scfg.filename check excludes servers created from cmdline */
	list_for_each_entry(server, &cfg->servers, list) {
		if (!server->scfg.filename)
			continue;

		if (!server_read_config(cfg, server, &lineno, &error)) {
			if (lineno != 0)
				info("• %s: %sfail%s - line %u: %s",
				     server->name, ansi_red, ansi_normal,
				     lineno, error);
			else
				info("• %s: %sfail%s - %s",
				     server->name, ansi_red, ansi_normal,
				     error);
			rv = false;
		} else if (!scfg_validate(&server->scfg, &error)) {
			info("• %s: %sfail%s - %s",
			     server->name, ansi_red, ansi_normal,
			     error);
			rv = false;
		} else {
			info("• %s: %sok%s", server->name, ansi_green,
			     ansi_normal);
		}
	}

	info("");

	ia = 0;
	list_for_each_entry(a, &cfg->servers, list) {
		ib = 0;
		list_for_each_entry(b, &cfg->servers, list) {
			if (ib <= ia) {
				ib++;
				continue;
			}

			if (a->scfg.announce_port != 0 &&
			    b->scfg.announce_port != 0 &&
			    a->scfg.announce_port == b->scfg.announce_port)
				info("%sNote:%s %s and %s appear to have the "
				     "same announce port", ansi_red,
				     ansi_normal, a->name, b->name);

			if (saddr_match(&a->scfg.locals, &b->scfg.locals))
				info("%sNote:%s %s and %s appear to share at "
				     "least one local address/port pair",
				     ansi_red, ansi_normal, a->name, b->name);

			if (saddr_match(&a->scfg.remotes, &b->scfg.remotes))
				info("%sNote:%s %s and %s appear to share at "
				     "least one remote address/port pair",
				     ansi_red, ansi_normal, a->name, b->name);

			if (saddr_match(&a->scfg.rcons, &b->scfg.rcons))
				info("%sNote:%s %s and %s appear to share at "
				     "least one rcon address/port pair",
				     ansi_red, ansi_normal, a->name, b->name);

			ib++;
		}
		ia++;
	}

	return rv;
}

bool do_pcount(struct cfg *cfg)
{
	unsigned x, y;

	if (do_rcon_pcount(cfg, &y, &x))
		error("Rcon says %u/%u", y, x);

	if (do_mc_pcount(cfg, &y, &x))
		error("MC says %u/%u", y, x);

	return true;
}