2 * IRC - Internet Relay Chat, ircd/m_server.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * m_functions execute protocol messages on this server:
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
88 #include "ircd_features.h"
89 #include "ircd_reply.h"
90 #include "ircd_string.h"
97 #include "querycmds.h"
104 #include "userload.h"
111 * mr_server - registration message handler
113 * parv[0] = sender prefix
114 * parv[1] = servername
116 * parv[3] = start timestamp
117 * parv[4] = link timestamp
118 * parv[5] = major protocol version: P09/P10
119 * parv[parc-1] = serverinfo
121 * parv[6] = "YMM", where 'Y' is the server numeric and "MM" is the
122 * numeric nick mask of this server.
123 * parv[7] = +hs (h == hub, s == service)
125 int mr_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
129 char info[REALLEN + 1];
131 struct Client* acptr;
132 struct Client* bcptr;
133 struct Client* LHcptr = 0;
134 struct ConfItem* aconf = 0;
135 struct ConfItem* lhconf = 0;
136 struct Jupe* ajupe = 0;
139 int active_lh_line = 0;
141 time_t start_timestamp;
142 time_t timestamp = 0;
146 if (IsUserPort(cptr))
147 return exit_client_msg(cptr, cptr, &me,
148 "Cannot connect a server to a user port");
150 recv_time = TStime();
155 need_more_params(sptr, "SERVER");
156 return exit_client(cptr, cptr, &me, "Need more parameters");
160 if ((ajupe = jupe_find(host)) && JupeIsActive(ajupe))
161 return exit_client_msg(cptr, sptr, &me, "Juped: %s", JupeReason(ajupe));
163 log_write(LS_NETWORK, L_NOTICE, LOG_NOSNOTICE, "SERVER: %s %s[%s]", parv[1],
164 cli_sockhost(cptr), cli_sock_ip(cptr));
169 if (strlen(parv[5]) != 3 || (parv[5][0] != 'P' && parv[5][0] != 'J'))
170 return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
174 if (*parv[7] == '+') {
175 for (ch = parv[7] + 1; *ch; ch++)
186 prot = atoi(parv[5] + 1);
187 if (prot > atoi(MAJOR_PROTOCOL))
188 prot = atoi(MAJOR_PROTOCOL);
191 start_timestamp = atoi(parv[3]);
192 timestamp = atoi(parv[4]);
193 Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age %Tu (%Tu)",
194 host, parv[4], start_timestamp, cli_serv(&me)->timestamp));
196 if ((timestamp < OLDEST_TS || (hop == 1 && start_timestamp < OLDEST_TS)))
198 return exit_client_msg(cptr, sptr, &me,
199 "Bogus timestamps (%s %s)", parv[3], parv[4]);
201 ircd_strncpy(info, parv[parc - 1], REALLEN);
202 info[REALLEN] = '\0';
203 if (prot < atoi(MINOR_PROTOCOL)) {
204 sendto_opmask_butone(0, SNO_OLDSNO, "Got incompatible protocol version "
205 "(%s) from %s", parv[5], cli_name(cptr));
206 return exit_new_server(cptr, sptr, host, timestamp,
207 "Incompatible protocol: %s", parv[5]);
210 * Check for "FRENCH " infection ;-) (actually this should
211 * be replaced with routine to check the hostname syntax in
212 * general). [ This check is still needed, even after the parse
213 * is fixed, because someone can send "SERVER :foo bar " ].
214 * Also, changed to check other "difficult" characters, now
215 * that parse lets all through... --msa
217 if (strlen(host) > HOSTLEN)
218 host[HOSTLEN] = '\0';
220 for (ch = host; *ch; ch++) {
221 if (*ch <= ' ' || *ch > '~')
224 if (*ch || !strchr(host, '.')) {
225 sendto_opmask_butone(0, SNO_OLDSNO, "Bogus server name (%s) from %s",
226 host, cli_name(cptr));
227 return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
230 if (IsUnknown(cptr) || IsHandshake(cptr))
235 * A local link that is still in undefined state wants
236 * to be a SERVER. Check if this is allowed and change
237 * status accordingly...
240 * If there is more then one server on the same machine
241 * that we try to connect to, it could be that the /CONNECT
242 * <mask> caused this connect to be put at the wrong place
243 * in the hashtable. --Run
244 * Same thing for Unknown connections that first send NICK.
246 * Better check if the two strings are (caseless) identical
247 * and not mess with hash internals.
250 if (!EmptyString(cli_name(cptr)) &&
251 (IsUnknown(cptr) || IsHandshake(cptr)) &&
252 0 != ircd_strcmp(cli_name(cptr), host))
253 hChangeClient(cptr, host);
254 ircd_strncpy(cli_name(cptr), host, HOSTLEN);
255 ircd_strncpy(cli_info(cptr), info[0] ? info : cli_name(&me), REALLEN);
256 cli_hopcount(cptr) = hop;
258 /* check connection rules */
259 if (0 != conf_eval_crule(host, CRULE_ALL)) {
260 ServerStats->is_ref++;
261 sendto_opmask_butone(0, SNO_OLDSNO, "Refused connection from %s.", cli_name(cptr));
262 return exit_client(cptr, cptr, &me, "Disallowed by connection rule");
265 if (conf_check_server(cptr)) {
266 ++ServerStats->is_ref;
267 sendto_opmask_butone(0, SNO_OLDSNO, "Received unauthorized connection "
268 "from %s.", cli_name(cptr));
269 return exit_client(cptr, cptr, &me, "No C:line");
272 host = cli_name(cptr);
276 if (!(aconf = find_conf_byname(cli_confs(cptr), host, CONF_SERVER))) {
277 ++ServerStats->is_ref;
278 sendto_opmask_butone(0, SNO_OLDSNO, "Access denied. No conf line for "
279 "server %s", cli_name(cptr));
280 return exit_client_msg(cptr, cptr, &me,
281 "Access denied. No conf line for server %s", cli_name(cptr));
283 encr = cli_passwd(cptr);
285 if (*aconf->passwd && !!strcmp(aconf->passwd, encr)) {
286 ++ServerStats->is_ref;
287 sendto_opmask_butone(0, SNO_OLDSNO, "Access denied (passwd mismatch) %s",
289 return exit_client_msg(cptr, cptr, &me,
290 "No Access (passwd mismatch) %s", cli_name(cptr));
293 memset(cli_passwd(cptr), 0, sizeof(cli_passwd(cptr)));
295 if (!feature_bool(FEAT_HUB)) {
296 for (i = 0; i <= HighestFd; i++)
297 if (LocalClientArray[i] && IsServer(LocalClientArray[i])) {
306 * We want to find IsConnecting() and IsHandshake() too,
308 * The second finds collisions with numeric representation of existing
309 * servers - these shouldn't happen anymore when all upgraded to 2.10.
312 while ((acptr = FindClient(host)) ||
313 (parc > 7 && (acptr = FindNServer(parv[6]))))
316 * This link is trying feed me a server that I already have
317 * access through another path
319 * Do not allow Uworld to do this.
320 * Do not allow servers that are juped.
321 * Do not allow servers that have older link timestamps
323 * Do not allow servers that use the same numeric as an existing
324 * server, but have a different name.
326 * If my ircd.conf sucks, I can try to connect to myself:
329 return exit_client_msg(cptr, cptr, &me, "nick collision with me (%s), check server number in M:?", host);
331 * Detect wrong numeric.
333 if (0 != ircd_strcmp(cli_name(acptr), host)) {
334 sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
335 ":SERVER Numeric Collision: %s != %s",
336 cli_name(acptr), host);
337 return exit_client_msg(cptr, cptr, &me,
338 "NUMERIC collision between %s and %s."
339 " Is your server numeric correct ?", host, cli_name(acptr));
342 * Kill our try, if we had one.
344 if (IsConnecting(acptr))
346 if (!active_lh_line && exit_client(cptr, acptr, &me,
347 "Just connected via another link") == CPTR_KILLED)
350 * We can have only ONE 'IsConnecting', 'IsHandshake' or
351 * 'IsServer', because new 'IsConnecting's are refused to
352 * the same server if we already had it.
357 * Avoid other nick collisions...
358 * This is a doubtfull test though, what else would it be
359 * when it has a server.name ?
361 else if (!IsServer(acptr) && !IsHandshake(acptr))
362 return exit_client_msg(cptr, cptr, &me,
363 "Nickname %s already exists!", host);
365 * Our new server might be a juped server,
366 * or someone trying abuse a second Uworld:
368 else if (IsServer(acptr) && (0 == ircd_strncmp(cli_info(acptr), "JUPE", 4) ||
369 find_conf_byhost(cli_confs(cptr), cli_name(acptr), CONF_UWORLD)))
372 return exit_client(cptr, sptr, &me, cli_info(acptr));
373 sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
374 ":Received :%s SERVER %s from %s !?!", parv[0],
375 parv[1], cli_name(cptr));
376 return exit_new_server(cptr, sptr, host, timestamp, "%s", cli_info(acptr));
379 * Of course we find the handshake this link was before :)
381 else if (IsHandshake(acptr) && acptr == cptr)
384 * Here we have a server nick collision...
385 * We don't want to kill the link that was last /connected,
386 * but we neither want to kill a good (old) link.
387 * Therefor we kill the second youngest link.
391 struct Client* c2ptr = 0;
392 struct Client* c3ptr = acptr;
393 struct Client* ac2ptr;
394 struct Client* ac3ptr;
396 /* Search youngest link: */
397 for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
398 if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
402 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
403 if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
406 if (timestamp > cli_serv(c3ptr)->timestamp)
409 c2ptr = acptr; /* Make sure they differ */
411 /* Search second youngest link: */
412 for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
413 if (ac2ptr != c3ptr &&
414 cli_serv(ac2ptr)->timestamp >
415 (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
419 for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
420 if (ac2ptr != c3ptr &&
421 cli_serv(ac2ptr)->timestamp >
422 (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
425 if (c3ptr && timestamp > (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
427 /* If timestamps are equal, decide which link to break
430 if ((c2ptr ? cli_serv(c2ptr)->timestamp : timestamp) ==
431 (c3ptr ? cli_serv(c3ptr)->timestamp : timestamp))
439 n2 = cli_name(c2ptr);
440 n2up = MyConnect(c2ptr) ? cli_name(&me) : cli_name(cli_serv(c2ptr)->up);
445 n2up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
449 n3 = cli_name(c3ptr);
450 n3up = MyConnect(c3ptr) ? cli_name(&me) : cli_name(cli_serv(c3ptr)->up);
455 n3up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
457 if (strcmp(n2, n2up) > 0)
459 if (strcmp(n3, n3up) > 0)
461 if (strcmp(n3, n2) > 0)
468 /* Now squit the second youngest link: */
470 return exit_new_server(cptr, sptr, host, timestamp,
471 "server %s already exists and is %ld seconds younger.",
472 host, (long)cli_serv(acptr)->timestamp - (long)timestamp);
473 else if (cli_from(c2ptr) == cptr || IsServer(sptr))
475 struct Client *killedptrfrom = cli_from(c2ptr);
479 * If the L: or H: line also gets rid of this link,
480 * we sent just one squit.
482 if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
485 * If breaking the loop here solves the L: or H:
486 * line problem, we don't squit that.
488 if (cli_from(c2ptr) == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
493 * If we still have a L: or H: line problem,
494 * we prefer to squit the new server, solving
495 * loop and L:/H: line problem with only one squit.
502 * If the new server was introduced by a server that caused a
503 * Ghost less then 20 seconds ago, this is probably also
504 * a Ghost... (20 seconds is more then enough because all
505 * SERVER messages are at the beginning of a net.burst). --Run
507 if (CurrentTime - cli_serv(cptr)->ghost < 20)
509 killedptrfrom = cli_from(acptr);
510 if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
513 else if (exit_client_msg(cptr, c2ptr, &me,
514 "Loop <-- %s (new link is %ld seconds younger)", host,
515 (c3ptr ? (long)cli_serv(c3ptr)->timestamp : timestamp) -
516 (long)cli_serv(c2ptr)->timestamp) == CPTR_KILLED)
519 * Did we kill the incoming server off already ?
521 if (killedptrfrom == cptr)
528 if (LHcptr && a_kills_b_too(LHcptr, acptr))
530 if (cli_from(acptr) == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
539 * We can't believe it is a lagged server message
540 * when it directly connects to us...
541 * kill the older link at the ghost, rather then
542 * at the second youngest link, assuming it isn't
545 ghost = CurrentTime; /* Mark that it caused a ghost */
546 if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
556 return exit_new_server(cptr, sptr, host, timestamp,
557 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s), check H:" :
558 "Leaf-only link %s <- %s(%s), check L:",
559 cli_name(cptr), host,
560 lhconf ? (lhconf->name ? lhconf->name : "*") : "!");
564 int killed = a_kills_b_too(LHcptr, sptr);
565 if (active_lh_line < 3)
567 if (exit_client_msg(cptr, LHcptr, &me,
568 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s), check H:" :
569 "Leaf-only link %s <- %s(%s), check L:",
570 cli_name(cptr), host,
571 lhconf ? (lhconf->name ? lhconf->name : "*") : "!") == CPTR_KILLED)
576 ServerStats->is_ref++;
577 if (exit_client(cptr, LHcptr, &me, "I'm a leaf, define HUB") == CPTR_KILLED)
581 * Did we kill the incoming server off already ?
588 if (IsUnknown(cptr) || IsHandshake(cptr))
591 cli_serv(cptr)->timestamp = timestamp;
592 cli_serv(cptr)->prot = prot;
593 cli_serv(cptr)->ghost = ghost;
594 SetServerYXX(cptr, cptr, parv[6]);
595 if (start_timestamp > OLDEST_TS)
597 Debug((DEBUG_DEBUG, "My start time: %Tu; other's start time: %Tu",
598 cli_serv(&me)->timestamp, start_timestamp));
599 Debug((DEBUG_DEBUG, "Receive time: %Tu; received timestamp: %Tu; "
600 "difference %ld", recv_time, timestamp, timestamp - recv_time));
601 if (feature_bool(FEAT_RELIABLE_CLOCK)) {
602 if (start_timestamp < cli_serv(&me)->timestamp)
603 cli_serv(&me)->timestamp = start_timestamp;
605 cli_serv(cptr)->timestamp = TStime();
607 if (start_timestamp < cli_serv(&me)->timestamp) {
608 sendto_opmask_butone(0, SNO_OLDSNO, "got earlier start time: "
609 "%Tu < %Tu", start_timestamp,
610 cli_serv(&me)->timestamp);
611 cli_serv(&me)->timestamp = start_timestamp;
612 TSoffset += timestamp - recv_time;
613 sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
614 (int)(timestamp - recv_time));
615 } else if ((start_timestamp > cli_serv(&me)->timestamp) &&
617 cli_serv(cptr)->timestamp = TStime();
619 else if (timestamp != recv_time) {
621 * Equal start times, we have a collision. Let the connected-to
622 * server decide. This assumes leafs issue more than half of the
623 * connection attempts.
626 cli_serv(cptr)->timestamp = TStime();
627 else if (IsHandshake(cptr)) {
628 sendto_opmask_butone(0, SNO_OLDSNO, "clock adjusted by adding %d",
629 (int)(timestamp - recv_time));
630 TSoffset += timestamp - recv_time;
636 ret = server_estab(cptr, aconf);
641 if (feature_bool(FEAT_RELIABLE_CLOCK) &&
642 abs(cli_serv(cptr)->timestamp - recv_time) > 30) {
643 sendto_opmask_butone(0, SNO_OLDSNO, "Connected to a net with a "
644 "timestamp-clock difference of %Td seconds! "
645 "Used SETTIME to correct this.",
646 timestamp - recv_time);
647 sendcmdto_prio_one(&me, CMD_SETTIME, cptr, "%Tu :%s", TStime(),
655 * ms_server - server message handler
657 * parv[0] = sender prefix
658 * parv[1] = servername
660 * parv[3] = start timestamp
661 * parv[4] = link timestamp
662 * parv[5] = major protocol version: P09/P10
663 * parv[parc-1] = serverinfo
665 * parv[6] = "YMM", where 'Y' is the server numeric and "MM" is the
666 * numeric nick mask of this server.
667 * parv[7] = +hs (h == hub, s == service)
669 int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
673 char info[REALLEN + 1];
675 struct Client* acptr;
676 struct Client* bcptr;
677 struct Client* LHcptr = 0;
678 struct ConfItem* aconf = 0;
679 struct ConfItem* lhconf = 0;
682 int active_lh_line = 0;
684 time_t start_timestamp;
685 time_t timestamp = 0;
689 recv_time = TStime();
693 return need_more_params(sptr, "SERVER");
694 return exit_client(cptr, cptr, &me, "Need more parameters");
701 if (strlen(parv[5]) != 3 || (parv[5][0] != 'P' && parv[5][0] != 'J'))
702 return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
706 if (*parv[7] == '+') {
707 for (ch = parv[7] + 1; *ch; ch++)
718 prot = atoi(parv[5] + 1);
719 if (prot > atoi(MAJOR_PROTOCOL))
720 prot = atoi(MAJOR_PROTOCOL);
722 start_timestamp = atoi(parv[3]);
723 timestamp = atoi(parv[4]);
725 Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age %Tu (%Tu)",
726 host, parv[4], start_timestamp, cli_serv(&me)->timestamp));
728 if ((timestamp < OLDEST_TS || (hop == 1 && start_timestamp < OLDEST_TS)))
730 return exit_client_msg(cptr, sptr, &me,
731 "Bogus timestamps (%s %s)", parv[3], parv[4]);
734 ircd_strncpy(info, parv[parc - 1], REALLEN);
735 info[REALLEN] = '\0';
737 if (prot < atoi(MINOR_PROTOCOL)) {
738 sendto_opmask_butone(0, SNO_OLDSNO, "Got incompatible protocol version "
739 "(%s) from %s", parv[5], cli_name(cptr));
740 return exit_new_server(cptr, sptr, host, timestamp,
741 "Incompatible protocol: %s", parv[5]);
745 * Check for "FRENCH " infection ;-) (actually this should
746 * be replaced with routine to check the hostname syntax in
747 * general). [ This check is still needed, even after the parse
748 * is fixed, because someone can send "SERVER :foo bar " ].
749 * Also, changed to check other "difficult" characters, now
750 * that parse lets all through... --msa
752 if (strlen(host) > HOSTLEN)
753 host[HOSTLEN] = '\0';
754 for (ch = host; *ch; ch++)
755 if (*ch <= ' ' || *ch > '~')
757 if (*ch || !strchr(host, '.')) {
758 sendto_opmask_butone(0, SNO_OLDSNO, "Bogus server name (%s) from %s",
759 host, cli_name(cptr));
760 return exit_client_msg(cptr, cptr, &me, "Bogus server name (%s)", host);
764 * We want to find IsConnecting() and IsHandshake() too,
766 * The second finds collisions with numeric representation of existing
767 * servers - these shouldn't happen anymore when all upgraded to 2.10.
770 while ((acptr = FindClient(host)) ||
771 (parc > 7 && (acptr = FindNServer(parv[6]))))
774 * This link is trying feed me a server that I already have
775 * access through another path
777 * Do not allow Uworld to do this.
778 * Do not allow servers that are juped.
779 * Do not allow servers that have older link timestamps
781 * Do not allow servers that use the same numeric as an existing
782 * server, but have a different name.
784 * If my ircd.conf sucks, I can try to connect to myself:
787 return exit_client_msg(cptr, cptr, &me,
788 "nick collision with me, check server number in M:? (%s)", host);
790 * Detect wrong numeric.
792 if (0 != ircd_strcmp(cli_name(acptr), host))
794 sendcmdto_serv_butone(&me, CMD_WALLOPS, cptr,
795 ":SERVER Numeric Collision: %s != %s", cli_name(acptr),
797 return exit_client_msg(cptr, cptr, &me,
798 "NUMERIC collision between %s and %s."
799 " Is your server numeric correct ?", host, cli_name(acptr));
803 * Kill our try, if we had one.
805 if (IsConnecting(acptr))
807 if (!active_lh_line && exit_client(cptr, acptr, &me,
808 "Just connected via another link") == CPTR_KILLED)
811 * We can have only ONE 'IsConnecting', 'IsHandshake' or
812 * 'IsServer', because new 'IsConnecting's are refused to
813 * the same server if we already had it.
818 * Avoid other nick collisions...
819 * This is a doubtfull test though, what else would it be
820 * when it has a server.name ?
822 else if (!IsServer(acptr) && !IsHandshake(acptr))
823 return exit_client_msg(cptr, cptr, &me,
824 "Nickname %s already exists!", host);
826 * Our new server might be a juped server,
827 * or someone trying abuse a second Uworld:
829 else if (IsServer(acptr) && (0 == ircd_strncmp(cli_info(acptr), "JUPE", 4) ||
830 find_conf_byhost(cli_confs(cptr), cli_name(acptr), CONF_UWORLD)))
833 return exit_client(cptr, sptr, &me, cli_info(acptr));
834 sendcmdto_one(&me, CMD_WALLOPS, cptr, ":Received :%s SERVER %s "
835 "from %s !?!", parv[0], parv[1], cli_name(cptr));
836 return exit_new_server(cptr, sptr, host, timestamp, "%s", cli_info(acptr));
839 * Of course we find the handshake this link was before :)
841 else if (IsHandshake(acptr) && acptr == cptr)
844 * Here we have a server nick collision...
845 * We don't want to kill the link that was last /connected,
846 * but we neither want to kill a good (old) link.
847 * Therefor we kill the second youngest link.
851 struct Client* c2ptr = 0;
852 struct Client* c3ptr = acptr;
853 struct Client* ac2ptr;
854 struct Client* ac3ptr;
856 /* Search youngest link: */
857 for (ac3ptr = acptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
858 if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
863 for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
864 if (cli_serv(ac3ptr)->timestamp > cli_serv(c3ptr)->timestamp)
868 if (timestamp > cli_serv(c3ptr)->timestamp)
871 c2ptr = acptr; /* Make sure they differ */
874 /* Search second youngest link: */
875 for (ac2ptr = acptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
876 if (ac2ptr != c3ptr &&
877 cli_serv(ac2ptr)->timestamp >
878 (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
883 for (ac2ptr = sptr; ac2ptr != &me; ac2ptr = cli_serv(ac2ptr)->up)
884 if (ac2ptr != c3ptr &&
885 cli_serv(ac2ptr)->timestamp >
886 (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
890 if (c3ptr && timestamp > (c2ptr ? cli_serv(c2ptr)->timestamp : timestamp))
893 /* If timestamps are equal, decide which link to break
896 if ((c2ptr ? cli_serv(c2ptr)->timestamp : timestamp) ==
897 (c3ptr ? cli_serv(c3ptr)->timestamp : timestamp))
906 n2 = cli_name(c2ptr);
907 n2up = MyConnect(c2ptr) ? cli_name(&me) : cli_name(cli_serv(c2ptr)->up);
912 n2up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
917 n3 = cli_name(c3ptr);
918 n3up = MyConnect(c3ptr) ? cli_name(&me) : cli_name(cli_serv(c3ptr)->up);
923 n3up = IsServer(sptr) ? cli_name(sptr) : cli_name(&me);
926 if (strcmp(n2, n2up) > 0)
928 if (strcmp(n3, n3up) > 0)
931 if (strcmp(n3, n2) > 0)
938 /* Now squit the second youngest link: */
940 return exit_new_server(cptr, sptr, host, timestamp,
941 "server %s already exists and is %ld seconds younger.",
942 host, (long)cli_serv(acptr)->timestamp - (long)timestamp);
943 else if (cli_from(c2ptr) == cptr || IsServer(sptr))
945 struct Client *killedptrfrom = cli_from(c2ptr);
949 * If the L: or H: line also gets rid of this link,
950 * we sent just one squit.
952 if (LHcptr && a_kills_b_too(LHcptr, c2ptr))
955 * If breaking the loop here solves the L: or H:
956 * line problem, we don't squit that.
958 if (cli_from(c2ptr) == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
963 * If we still have a L: or H: line problem,
964 * we prefer to squit the new server, solving
965 * loop and L:/H: line problem with only one squit.
972 * If the new server was introduced by a server that caused a
973 * Ghost less then 20 seconds ago, this is probably also
974 * a Ghost... (20 seconds is more then enough because all
975 * SERVER messages are at the beginning of a net.burst). --Run
977 if (CurrentTime - cli_serv(cptr)->ghost < 20)
979 killedptrfrom = cli_from(acptr);
980 if (exit_client(cptr, acptr, &me, "Ghost loop") == CPTR_KILLED)
983 else if (exit_client_msg(cptr, c2ptr, &me,
984 "Loop <-- %s (new link is %ld seconds younger)", host,
985 (c3ptr ? (long)cli_serv(c3ptr)->timestamp : timestamp) -
986 (long)cli_serv(c2ptr)->timestamp) == CPTR_KILLED)
989 * Did we kill the incoming server off already ?
991 if (killedptrfrom == cptr)
998 if (LHcptr && a_kills_b_too(LHcptr, acptr))
1000 if (cli_from(acptr) == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
1009 * We can't believe it is a lagged server message
1010 * when it directly connects to us...
1011 * kill the older link at the ghost, rather then
1012 * at the second youngest link, assuming it isn't
1015 ghost = CurrentTime; /* Mark that it caused a ghost */
1016 if (exit_client(cptr, acptr, &me, "Ghost") == CPTR_KILLED)
1026 return exit_new_server(cptr, sptr, host, timestamp,
1027 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1028 cli_name(cptr), host,
1029 lhconf ? (lhconf->name ? lhconf->name : "*") : "!");
1033 int killed = a_kills_b_too(LHcptr, sptr);
1034 if (active_lh_line < 3)
1036 if (exit_client_msg(cptr, LHcptr, &me,
1037 (active_lh_line == 2) ? "Non-Hub link %s <- %s(%s)" : "Leaf-only link %s <- %s(%s)",
1038 cli_name(cptr), host,
1039 lhconf ? (lhconf->name ? lhconf->name : "*") : "!") == CPTR_KILLED)
1044 ServerStats->is_ref++;
1045 if (exit_client(cptr, LHcptr, &me, "I'm a leaf") == CPTR_KILLED)
1049 * Did we kill the incoming server off already ?
1057 * Server is informing about a new server behind
1058 * this link. Create REMOTE server structure,
1059 * add it to list and propagate word to my other
1063 acptr = make_client(cptr, STAT_SERVER);
1065 cli_serv(acptr)->prot = prot;
1066 cli_serv(acptr)->timestamp = timestamp;
1067 cli_hopcount(acptr) = hop;
1068 ircd_strncpy(cli_name(acptr), host, HOSTLEN);
1069 ircd_strncpy(cli_info(acptr), info, REALLEN);
1070 cli_serv(acptr)->up = sptr;
1071 cli_serv(acptr)->updown = add_dlink(&(cli_serv(sptr))->down, acptr);
1072 /* Use cptr, because we do protocol 9 -> 10 translation
1073 for numeric nicks ! */
1074 SetServerYXX(cptr, acptr, parv[6]);
1076 Count_newremoteserver(UserStats);
1077 add_client_to_list(acptr);
1080 if (*parv[5] == 'J')
1083 sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s",
1084 cli_name(sptr), cli_name(acptr));
1089 * Old sendto_serv_but_one() call removed because we now need to send
1090 * different names to different servers (domain name matching).
1092 for (i = 0; i <= HighestFd; i++)
1094 if (!(bcptr = LocalClientArray[i]) || !IsServer(bcptr) ||
1095 bcptr == cptr || IsMe(bcptr))
1097 if (0 == match(cli_name(&me), cli_name(acptr)))
1099 sendcmdto_one(sptr, CMD_SERVER, bcptr, "%s %d 0 %s %s %s%s +%s%s :%s",
1100 cli_name(acptr), hop + 1, parv[4], parv[5],
1101 NumServCap(acptr), IsHub(acptr) ? "h" : "",
1102 IsService(acptr) ? "s" : "", cli_info(acptr));