2 * IRC - Internet Relay Chat, ircd/s_serv.c (formerly ircd/s_msg.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.
24 * @brief Miscellaneous server support functions.
36 #include "ircd_alloc.h"
37 #include "ircd_reply.h"
38 #include "ircd_string.h"
39 #include "ircd_snprintf.h"
40 #include "ircd_crypt.h"
49 #include "querycmds.h"
64 /** Maximum connection count since last restart. */
65 unsigned int max_connection_count = 0;
66 /** Maximum (local) client count since last restart. */
67 unsigned int max_client_count = 0;
69 /** Squit a new (pre-burst) server.
70 * @param cptr Local client that tried to introduce the server.
71 * @param sptr Server to disconnect.
72 * @param host Name of server being disconnected.
73 * @param timestamp Link time of server being disconnected.
74 * @param pattern Format string for squit message.
75 * @return CPTR_KILLED if cptr == sptr, else 0.
77 int exit_new_server(struct Client *cptr, struct Client *sptr, const char *host,
78 time_t timestamp, const char *pattern, ...)
83 vd.vd_format = pattern;
84 va_start(vd.vd_args, pattern);
87 retval = vexit_client_msg(cptr, cptr, &me, pattern, vd.vd_args);
89 sendcmdto_one(&me, CMD_SQUIT, cptr, "%s %Tu :%v", host, timestamp, &vd);
96 /** Indicate whether \a a is between \a b and #me (that is, \a b would
97 * be killed if \a a squits).
98 * @param a A server that may be between us and \a b.
99 * @param b A client that may be on the far side of \a a.
100 * @return Non-zero if \a a is between \a b and #me.
102 int a_kills_b_too(struct Client *a, struct Client *b)
104 for (; b != a && b != &me; b = cli_serv(b)->up);
105 return (a == b ? 1 : 0);
108 /** Handle a connection that has sent a valid PASS and SERVER.
109 * @param cptr New peer server.
110 * @param aconf Connect block for \a cptr.
113 int server_estab(struct Client *cptr, struct ConfItem *aconf)
115 struct Client* acptr = 0;
120 assert(0 != cli_local(cptr));
122 inpath = cli_name(cptr);
124 if (IsUnknown(cptr)) {
125 if (aconf->passwd[0])
126 sendrawto_one(cptr, MSG_PASS " :%s", aconf->passwd);
128 * Pass my info to the new server
130 sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s +%s :%s",
131 cli_name(&me), cli_serv(&me)->timestamp,
132 cli_serv(cptr)->timestamp, MAJOR_PROTOCOL, NumServCap(&me),
133 feature_bool(FEAT_HUB) ? "h" : "",
134 *(cli_info(&me)) ? cli_info(&me) : "IRCers United");
136 * Don't charge this IP# for connecting
137 * XXX - if this comes from a server port, it will not have been added
138 * to the IP check registry, see add_connection in s_bsd.c
140 IPcheck_connect_fail(&cli_ip(cptr));
143 det_confs_butmask(cptr, CONF_SERVER | CONF_UWORLD);
145 if (!IsHandshake(cptr))
148 cli_handler(cptr) = SERVER_HANDLER;
149 Count_unknownbecomesserver(UserStats);
151 release_dns_reply(cptr);
155 /* nextping = CurrentTime; */
158 * NOTE: check for acptr->user == cptr->serv->user is necessary to insure
159 * that we got the same one... bleah
161 if (cli_serv(cptr)->user && *(cli_serv(cptr))->by &&
162 (acptr = findNUser(cli_serv(cptr)->by))) {
163 if (cli_user(acptr) == cli_serv(cptr)->user) {
164 sendcmdto_one(&me, CMD_NOTICE, acptr, "%C :Link with %s established.",
169 * if not the same client, set by to empty string
172 *(cli_serv(cptr))->by = '\0';
176 sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s established.", inpath);
177 cli_serv(cptr)->up = &me;
178 cli_serv(cptr)->updown = add_dlink(&(cli_serv(&me))->down, cptr);
179 sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s", cli_name(&me),
183 * Old sendto_serv_but_one() call removed because we now
184 * need to send different names to different servers
185 * (domain name matching) Send new server to other servers.
187 for (i = 0; i <= HighestFd; i++)
189 if (!(acptr = LocalClientArray[i]) || !IsServer(acptr) ||
190 acptr == cptr || IsMe(acptr))
192 if (!match(cli_name(&me), cli_name(cptr)))
194 sendcmdto_one(&me, CMD_SERVER, acptr,
195 "%s 2 0 %Tu J%02u %s%s +%s%s :%s", cli_name(cptr),
196 cli_serv(cptr)->timestamp, Protocol(cptr), NumServCap(cptr),
197 IsHub(cptr) ? "h" : "", IsService(cptr) ? "s" : "",
201 /* Send these as early as possible so that glined users/juped servers can
202 * be removed from the network while the remote server is still chewing
209 * Pass on my client information to the new server
211 * First, pass only servers (idea is that if the link gets
212 * cancelled beacause the server was already there,
213 * there are no NICK's to be cancelled...). Of course,
214 * if cancellation occurs, all this info is sent anyway,
215 * and I guess the link dies when a read is attempted...? --msa
217 * Note: Link cancellation to occur at this point means
218 * that at least two servers from my fragment are building
219 * up connection this other fragment at the same time, it's
220 * a race condition, not the normal way of operation...
223 for (acptr = &me; acptr; acptr = cli_prev(acptr)) {
224 /* acptr->from == acptr for acptr == cptr */
225 if (cli_from(acptr) == cptr)
227 if (IsServer(acptr)) {
228 const char* protocol_str;
230 if (Protocol(acptr) > 9)
231 protocol_str = IsBurst(acptr) ? "J" : "P";
233 protocol_str = IsBurst(acptr) ? "J0" : "P0";
235 if (0 == match(cli_name(&me), cli_name(acptr)))
237 sendcmdto_one(cli_serv(acptr)->up, CMD_SERVER, cptr,
238 "%s %d 0 %Tu %s%u %s%s +%s%s :%s", cli_name(acptr),
239 cli_hopcount(acptr) + 1, cli_serv(acptr)->timestamp,
240 protocol_str, Protocol(acptr), NumServCap(acptr),
241 IsHub(acptr) ? "h" : "", IsService(acptr) ? "s" : "",
246 for (acptr = &me; acptr; acptr = cli_prev(acptr))
248 /* acptr->from == acptr for acptr == cptr */
249 if (cli_from(acptr) == cptr)
254 char *s = umode_str(acptr);
255 sendcmdto_one(cli_user(acptr)->server, CMD_NICK, cptr,
256 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
257 cli_name(acptr), cli_hopcount(acptr) + 1, cli_lastnick(acptr),
258 cli_user(acptr)->username, cli_user(acptr)->realhost,
259 *s ? "+" : "", s, *s ? " " : "",
260 iptobase64(xxx_buf, &cli_ip(acptr), sizeof(xxx_buf)),
261 NumNick(acptr), cli_info(acptr));
265 * Last, send the BURST.
266 * (Or for 2.9 servers: pass all channels plus statuses)
269 struct Channel *chptr;
270 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
271 send_channel_modes(cptr, chptr);
273 sendcmdto_one(&me, CMD_END_OF_BURST, cptr, "");