1 /*
2  servers-reconnect.c : irssi
3 
4     Copyright (C) 1999-2000 Timo Sirainen
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program 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
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 
21 #include "module.h"
22 #include "commands.h"
23 #include "network.h"
24 #include "signals.h"
25 
26 #include "chat-protocols.h"
27 #include "servers.h"
28 #include "servers-setup.h"
29 #include "servers-reconnect.h"
30 
31 #include "settings.h"
32 
33 GSList *reconnects;
34 static int last_reconnect_tag;
35 static int reconnect_timeout_tag;
36 static int reconnect_time;
37 static int connect_timeout;
38 
reconnect_save_status(SERVER_CONNECT_REC * conn,SERVER_REC * server)39 void reconnect_save_status(SERVER_CONNECT_REC *conn, SERVER_REC *server)
40 {
41         g_free_not_null(conn->tag);
42 	conn->tag = g_strdup(server->tag);
43 
44 	g_free_not_null(conn->away_reason);
45 	conn->away_reason = !server->usermode_away ? NULL :
46 		g_strdup(server->away_reason);
47 
48 	if (!server->connected) {
49 		/* default to channels/usermode from connect record
50 		   since server isn't fully connected yet */
51 		/* XXX when is reconnect_save_status() called with
52 		 * server->connected==FALSE? */
53 		g_free_not_null(conn->channels);
54 		conn->channels = server->connrec->no_autojoin_channels ? NULL :
55 			g_strdup(server->connrec->channels);
56 	}
57 
58 	signal_emit("server reconnect save status", 2, conn, server);
59 }
60 
server_reconnect_add(SERVER_CONNECT_REC * conn,time_t next_connect)61 static void server_reconnect_add(SERVER_CONNECT_REC *conn,
62 				 time_t next_connect)
63 {
64 	RECONNECT_REC *rec;
65 
66 	g_return_if_fail(IS_SERVER_CONNECT(conn));
67 
68 	rec = g_new(RECONNECT_REC, 1);
69 	rec->tag = ++last_reconnect_tag;
70 	rec->next_connect = next_connect;
71 
72 	rec->conn = conn;
73 	conn->reconnecting = TRUE;
74 	server_connect_ref(conn);
75 
76 	reconnects = g_slist_append(reconnects, rec);
77 }
78 
server_reconnect_destroy(RECONNECT_REC * rec)79 void server_reconnect_destroy(RECONNECT_REC *rec)
80 {
81 	g_return_if_fail(rec != NULL);
82 
83 	reconnects = g_slist_remove(reconnects, rec);
84 
85 	signal_emit("server reconnect remove", 1, rec);
86 	server_connect_unref(rec->conn);
87 	g_free(rec);
88 
89 	if (reconnects == NULL)
90 	    last_reconnect_tag = 0;
91 }
92 
server_reconnect_timeout(void)93 static int server_reconnect_timeout(void)
94 {
95 	SERVER_CONNECT_REC *conn;
96 	GSList *list, *tmp, *next;
97 	time_t now;
98 
99 	now = time(NULL);
100 
101 	/* timeout any connections that haven't gotten to connected-stage */
102 	for (tmp = servers; tmp != NULL; tmp = next) {
103 		SERVER_REC *server = tmp->data;
104 
105 		next = tmp->next;
106 		if (!server->connected &&
107 		    server->connect_time + connect_timeout < now &&
108 		    connect_timeout > 0) {
109 			server->connection_lost = TRUE;
110 			server_disconnect(server);
111 		}
112 	}
113 
114 	for (tmp = lookup_servers; tmp != NULL; tmp = next) {
115 		SERVER_REC *server = tmp->data;
116 
117 		next = tmp->next;
118 		if (server->connect_time + connect_timeout < now &&
119 		    connect_timeout > 0) {
120 			if (server->connect_tag != -1) {
121 				g_source_remove(server->connect_tag);
122 				server->connect_tag = -1;
123 			}
124 			server->connection_lost = TRUE;
125 			server_connect_failed(server, "Timeout");
126 		}
127 	}
128 
129 	/* If server_connect() removes the next reconnection in queue,
130 	   we're screwed. I don't think this should happen anymore, but just
131 	   to be sure we don't crash, do this safely. */
132 	list = g_slist_copy(reconnects);
133 	for (tmp = list; tmp != NULL; tmp = tmp->next) {
134 		RECONNECT_REC *rec = tmp->data;
135 
136 		if (g_slist_find(reconnects, rec) == NULL)
137 			continue;
138 
139 		if (rec->next_connect <= now) {
140 			conn = rec->conn;
141 			server_connect_ref(conn);
142 			server_reconnect_destroy(rec);
143 			server_connect(conn);
144 			server_connect_unref(conn);
145 		}
146 	}
147 
148 	g_slist_free(list);
149 	return 1;
150 }
151 
sserver_connect(SERVER_SETUP_REC * rec,SERVER_CONNECT_REC * conn)152 static void sserver_connect(SERVER_SETUP_REC *rec, SERVER_CONNECT_REC *conn)
153 {
154 	server_setup_fill_reconn(conn, rec);
155 	server_reconnect_add(conn, rec->last_connect+reconnect_time);
156 	server_connect_unref(conn);
157 }
158 
159 static SERVER_CONNECT_REC *
server_connect_copy_skeleton(SERVER_CONNECT_REC * src,int connect_info)160 server_connect_copy_skeleton(SERVER_CONNECT_REC *src, int connect_info)
161 {
162 	SERVER_CONNECT_REC *dest;
163 
164         dest = NULL;
165 	signal_emit("server connect copy", 2, &dest, src);
166 	g_return_val_if_fail(dest != NULL, NULL);
167 
168         server_connect_ref(dest);
169 	dest->type = module_get_uniq_id("SERVER CONNECT", 0);
170 	dest->reconnection = src->reconnection;
171 	dest->proxy = g_strdup(src->proxy);
172         dest->proxy_port = src->proxy_port;
173 	dest->proxy_string = g_strdup(src->proxy_string);
174 	dest->proxy_string_after = g_strdup(src->proxy_string_after);
175 	dest->proxy_password = g_strdup(src->proxy_password);
176 
177 	dest->tag = g_strdup(src->tag);
178 
179 	if (connect_info) {
180 		dest->family = src->family;
181 		dest->address = g_strdup(src->address);
182 		dest->port = src->port;
183 		dest->password = g_strdup(src->password);
184 
185 		dest->use_tls = src->use_tls;
186 		dest->tls_cert = g_strdup(src->tls_cert);
187 		dest->tls_pkey = g_strdup(src->tls_pkey);
188 		dest->tls_verify = src->tls_verify;
189 		dest->tls_cafile = g_strdup(src->tls_cafile);
190 		dest->tls_capath = g_strdup(src->tls_capath);
191 		dest->tls_ciphers = g_strdup(src->tls_ciphers);
192 		dest->tls_pinned_cert = g_strdup(src->tls_pinned_cert);
193 		dest->tls_pinned_pubkey = g_strdup(src->tls_pinned_pubkey);
194 	}
195 
196 	dest->chatnet = g_strdup(src->chatnet);
197 	dest->nick = g_strdup(src->nick);
198 	dest->username = g_strdup(src->username);
199 	dest->realname = g_strdup(src->realname);
200 
201 	if (src->own_ip4 != NULL) {
202 		dest->own_ip4 = g_new(IPADDR, 1);
203 		memcpy(dest->own_ip4, src->own_ip4, sizeof(IPADDR));
204 	}
205 	if (src->own_ip6 != NULL) {
206 		dest->own_ip6 = g_new(IPADDR, 1);
207 		memcpy(dest->own_ip6, src->own_ip6, sizeof(IPADDR));
208 	}
209 
210 	dest->channels = g_strdup(src->channels);
211 	dest->away_reason = g_strdup(src->away_reason);
212 	dest->no_autojoin_channels = src->no_autojoin_channels;
213 	dest->no_autosendcmd = src->no_autosendcmd;
214 	dest->unix_socket = src->unix_socket;
215 
216 	return dest;
217 }
218 
219 #define server_should_reconnect(server) \
220 	((server)->connection_lost && !(server)->no_reconnect && \
221 	((server)->connrec->chatnet != NULL || \
222 		!(server)->banned))
223 
224 #define sserver_connect_ok(rec, net) \
225 	(!(rec)->banned && (rec)->chatnet != NULL && \
226 	g_ascii_strcasecmp((rec)->chatnet, (net)) == 0)
227 
sig_reconnect(SERVER_REC * server)228 static void sig_reconnect(SERVER_REC *server)
229 {
230 	SERVER_CONNECT_REC *conn;
231 	SERVER_SETUP_REC *sserver;
232 	GSList *tmp;
233 	int use_next, through;
234 	time_t now;
235 
236 	g_return_if_fail(IS_SERVER(server));
237 
238 	if (reconnect_time == -1 || !server_should_reconnect(server))
239 		return;
240 
241 	conn = server_connect_copy_skeleton(server->connrec, FALSE);
242         g_return_if_fail(conn != NULL);
243 
244 	/* save the server status */
245 	if (server->connected) {
246 		conn->reconnection = TRUE;
247 
248                 reconnect_save_status(conn, server);
249 	}
250 
251 	sserver = server_setup_find(server->connrec->address,
252 				    server->connrec->port,
253 				    server->connrec->chatnet);
254 
255 	if (sserver != NULL) {
256 		/* save the last connection time/status */
257 		sserver->last_connect = server->connect_time == 0 ?
258 			time(NULL) : server->connect_time;
259 		sserver->last_failed = !server->connected;
260 		sserver->banned = server->banned;
261                 sserver->dns_error = server->dns_error;
262 	}
263 
264 	if (sserver == NULL || conn->chatnet == NULL) {
265 		/* not in any chatnet, just reconnect back to same server */
266                 conn->family = server->connrec->family;
267 		conn->address = g_strdup(server->connrec->address);
268 		conn->port = server->connrec->port;
269 		conn->password = g_strdup(server->connrec->password);
270 
271 		if (strchr(conn->address, '/') != NULL)
272 			conn->unix_socket = TRUE;
273 
274 		server_reconnect_add(conn, (server->connect_time == 0 ? time(NULL) :
275 					    server->connect_time) + reconnect_time);
276 		server_connect_unref(conn);
277 		return;
278 	}
279 
280 	/* always try to first connect to the first on the list where we
281 	   haven't got unsuccessful connection attempts for the past half
282 	   an hour. */
283 
284 	now = time(NULL);
285 	for (tmp = setupservers; tmp != NULL; tmp = tmp->next) {
286 		SERVER_SETUP_REC *rec = tmp->data;
287 
288 		if (sserver_connect_ok(rec, conn->chatnet) &&
289 		    (!rec->last_connect || !rec->last_failed ||
290 		     rec->last_connect < now-FAILED_RECONNECT_WAIT)) {
291 			if (rec == sserver)
292 				conn->port = server->connrec->port;
293 			sserver_connect(rec, conn);
294 			return;
295 		}
296 	}
297 
298 	/* just try the next server in list */
299 	use_next = through = FALSE;
300 	for (tmp = setupservers; tmp != NULL; ) {
301 		SERVER_SETUP_REC *rec = tmp->data;
302 
303 		if (!use_next && server->connrec->port == rec->port &&
304 		    g_ascii_strcasecmp(rec->address, server->connrec->address) == 0)
305 			use_next = TRUE;
306 		else if (use_next && sserver_connect_ok(rec, conn->chatnet)) {
307 			if (rec == sserver)
308                                 conn->port = server->connrec->port;
309 			sserver_connect(rec, conn);
310 			break;
311 		}
312 
313 		if (tmp->next != NULL) {
314 			tmp = tmp->next;
315 			continue;
316 		}
317 
318 		if (through) {
319 			/* shouldn't happen unless there's no servers in
320 			   this chatnet in setup.. */
321 			server_connect_unref(conn);
322 			break;
323 		}
324 
325 		tmp = setupservers;
326 		use_next = through = TRUE;
327 	}
328 }
329 
sig_connected(SERVER_REC * server)330 static void sig_connected(SERVER_REC *server)
331 {
332 	g_return_if_fail(IS_SERVER(server));
333 	if (!server->connrec->reconnection)
334 		return;
335 
336 	if (server->connrec->channels != NULL)
337 		server->channels_join(server, server->connrec->channels, TRUE);
338 }
339 
340 /* Remove all servers from reconnect list */
341 /* SYNTAX: RMRECONNS */
cmd_rmreconns(void)342 static void cmd_rmreconns(void)
343 {
344 	while (reconnects != NULL)
345 		server_reconnect_destroy(reconnects->data);
346 }
347 
reconnect_find_tag(int tag)348 static RECONNECT_REC *reconnect_find_tag(int tag)
349 {
350 	GSList *tmp;
351 
352 	for (tmp = reconnects; tmp != NULL; tmp = tmp->next) {
353 		RECONNECT_REC *rec = tmp->data;
354 
355 		if (rec->tag == tag)
356 			return rec;
357 	}
358 
359 	return NULL;
360 }
361 
reconnect_all(void)362 static void reconnect_all(void)
363 {
364 	GSList *list;
365 	SERVER_CONNECT_REC *conn;
366 	RECONNECT_REC *rec;
367 
368 	/* first move reconnects to another list so if server_connect()
369 	   fails and goes to reconnection list again, we won't get stuck
370 	   here forever */
371 	list = NULL;
372 	while (reconnects != NULL) {
373 		rec = reconnects->data;
374 
375 		list = g_slist_append(list, rec->conn);
376                 server_connect_ref(rec->conn);
377 		server_reconnect_destroy(rec);
378 	}
379 
380 
381 	while (list != NULL) {
382 		conn = list->data;
383 
384 		server_connect(conn);
385                 server_connect_unref(conn);
386                 list = g_slist_remove(list, conn);
387 	}
388 }
389 
390 /* SYNTAX: RECONNECT <tag> [<quit message>] */
cmd_reconnect(const char * data,SERVER_REC * server)391 static void cmd_reconnect(const char *data, SERVER_REC *server)
392 {
393 	SERVER_CONNECT_REC *conn;
394 	RECONNECT_REC *rec;
395 	char *tag, *msg;
396 	void *free_arg;
397 	int tagnum;
398 
399 	if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &tag, &msg))
400 		return;
401 
402 	if (*tag != '\0' && g_strcmp0(tag, "*") != 0)
403 		server = server_find_tag(tag);
404 
405 	if (server != NULL) {
406 		/* reconnect connected server */
407 		conn = server_connect_copy_skeleton(server->connrec, TRUE);
408 
409 		if (server->connected)
410 			reconnect_save_status(conn, server);
411 
412 		msg = g_strconcat("* ", *msg == '\0' ?
413 				  "Reconnecting" : msg, NULL);
414 		signal_emit("command disconnect", 2, msg, server);
415 		g_free(msg);
416 
417 		conn->reconnection = TRUE;
418 		server_connect(conn);
419 		server_connect_unref(conn);
420 		cmd_params_free(free_arg);
421                 return;
422 	}
423 
424 	if (g_ascii_strcasecmp(tag, "all") == 0) {
425 		/* reconnect all servers in reconnect queue */
426                 reconnect_all();
427 		cmd_params_free(free_arg);
428                 return;
429 	}
430 
431 	if (*data == '\0') {
432 		/* reconnect to first server in reconnection list */
433 		if (reconnects == NULL)
434 			cmd_param_error(CMDERR_NOT_CONNECTED);
435                 rec = reconnects->data;
436 	} else {
437 		if (g_ascii_strncasecmp(tag, "RECON-", 6) == 0)
438 			tag += 6;
439 
440 		tagnum = atoi(tag);
441 		rec = tagnum <= 0 ? NULL : reconnect_find_tag(tagnum);
442 	}
443 
444 	if (rec == NULL) {
445 		signal_emit("server reconnect not found", 1, data);
446 	} else {
447 		conn = rec->conn;
448 		server_connect_ref(conn);
449 		server_reconnect_destroy(rec);
450 		server_connect(conn);
451 		server_connect_unref(conn);
452 	}
453 
454 	cmd_params_free(free_arg);
455 }
456 
cmd_disconnect(const char * data,SERVER_REC * server)457 static void cmd_disconnect(const char *data, SERVER_REC *server)
458 {
459 	RECONNECT_REC *rec;
460 
461 	if (g_ascii_strncasecmp(data, "RECON-", 6) != 0)
462 		return; /* handle only reconnection removing */
463 
464 	rec = reconnect_find_tag(atoi(data+6));
465 
466 	if (rec == NULL)
467 		signal_emit("server reconnect not found", 1, data);
468 	else
469 		server_reconnect_destroy(rec);
470 	signal_stop();
471 }
472 
sig_chat_protocol_deinit(CHAT_PROTOCOL_REC * proto)473 static void sig_chat_protocol_deinit(CHAT_PROTOCOL_REC *proto)
474 {
475 	GSList *tmp, *next;
476 
477 	for (tmp = reconnects; tmp != NULL; tmp = next) {
478 		RECONNECT_REC *rec = tmp->data;
479 
480                 next = tmp->next;
481                 if (rec->conn->chat_type == proto->id)
482 			server_reconnect_destroy(rec);
483 	}
484 }
485 
read_settings(void)486 static void read_settings(void)
487 {
488 	reconnect_time = settings_get_time("server_reconnect_time")/1000;
489         connect_timeout = settings_get_time("server_connect_timeout")/1000;
490 }
491 
servers_reconnect_init(void)492 void servers_reconnect_init(void)
493 {
494 	settings_add_time("server", "server_reconnect_time", "5min");
495 	settings_add_time("server", "server_connect_timeout", "5min");
496 
497 	reconnects = NULL;
498 	last_reconnect_tag = 0;
499 
500 	reconnect_timeout_tag = g_timeout_add(1000, (GSourceFunc) server_reconnect_timeout, NULL);
501 	read_settings();
502 
503 	signal_add("server connect failed", (SIGNAL_FUNC) sig_reconnect);
504 	signal_add("server disconnected", (SIGNAL_FUNC) sig_reconnect);
505 	signal_add("event connected", (SIGNAL_FUNC) sig_connected);
506 	signal_add("chat protocol deinit", (SIGNAL_FUNC) sig_chat_protocol_deinit);
507 	signal_add("setup changed", (SIGNAL_FUNC) read_settings);
508 
509 	command_bind("rmreconns", NULL, (SIGNAL_FUNC) cmd_rmreconns);
510 	command_bind("reconnect", NULL, (SIGNAL_FUNC) cmd_reconnect);
511 	command_bind_first("disconnect", NULL, (SIGNAL_FUNC) cmd_disconnect);
512 }
513 
servers_reconnect_deinit(void)514 void servers_reconnect_deinit(void)
515 {
516 	g_source_remove(reconnect_timeout_tag);
517 
518 	signal_remove("server connect failed", (SIGNAL_FUNC) sig_reconnect);
519 	signal_remove("server disconnected", (SIGNAL_FUNC) sig_reconnect);
520 	signal_remove("event connected", (SIGNAL_FUNC) sig_connected);
521 	signal_remove("chat protocol deinit", (SIGNAL_FUNC) sig_chat_protocol_deinit);
522 	signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
523 
524 	command_unbind("rmreconns", (SIGNAL_FUNC) cmd_rmreconns);
525 	command_unbind("reconnect", (SIGNAL_FUNC) cmd_reconnect);
526 	command_unbind("disconnect", (SIGNAL_FUNC) cmd_disconnect);
527 }
528