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"
38 #include "ircd_reply.h"
39 #include "ircd_string.h"
40 #include "ircd_snprintf.h"
41 #include "ircd_crypt.h"
50 #include "querycmds.h"
61 /* #include <assert.h> -- Now using assert in ircd_log.h */
65 /** Maximum connection count since last restart. */
66 unsigned int max_connection_count = 0;
67 /** Maximum (local) client count since last restart. */
68 unsigned int max_client_count = 0;
70 /** Squit a new (pre-burst) server.
71 * @param cptr Local client that tried to introduce the server.
72 * @param sptr Server to disconnect.
73 * @param host Name of server being disconnected.
74 * @param timestamp Link time of server being disconnected.
75 * @param pattern Format string for squit message.
76 * @return CPTR_KILLED if cptr == sptr, else 0.
78 int exit_new_server(struct Client *cptr, struct Client *sptr, const char *host,
79 time_t timestamp, const char *pattern, ...)
84 vd.vd_format = pattern;
85 va_start(vd.vd_args, pattern);
88 retval = vexit_client_msg(cptr, cptr, &me, pattern, vd.vd_args);
90 sendcmdto_one(&me, CMD_SQUIT, cptr, "%s %Tu :%v", host, timestamp, &vd);
97 /** Indicate whether \a a is between \a b and #me (that is, \a b would
98 * be killed if \a a squits).
99 * @param a A server that may be between us and \a b.
100 * @param b A client that may be on the far side of \a a.
101 * @return Non-zero if \a a is between \a b and #me.
103 int a_kills_b_too(struct Client *a, struct Client *b)
105 for (; b != a && b != &me; b = cli_serv(b)->up);
106 return (a == b ? 1 : 0);
109 /** Handle a connection that has sent a valid PASS and SERVER.
110 * @param cptr New peer server.
111 * @param aconf Connect block for \a cptr.
114 int server_estab(struct Client *cptr, struct ConfItem *aconf)
116 struct Client* acptr = 0;
121 assert(0 != cli_local(cptr));
123 inpath = cli_name(cptr);
125 if (IsUnknown(cptr)) {
126 if (aconf->passwd[0])
127 sendrawto_one(cptr, MSG_PASS " :%s", aconf->passwd);
129 * Pass my info to the new server
131 sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s +%s6 :%s",
132 cli_name(&me), cli_serv(&me)->timestamp,
133 cli_serv(cptr)->timestamp, MAJOR_PROTOCOL, NumServCap(&me),
134 feature_bool(FEAT_HUB) ? "h" : "",
135 *(cli_info(&me)) ? cli_info(&me) : "IRCers United");
138 det_confs_butmask(cptr, CONF_SERVER | CONF_UWORLD);
140 if (!IsHandshake(cptr))
143 cli_handler(cptr) = SERVER_HANDLER;
144 Count_unknownbecomesserver(UserStats);
147 /* nextping = CurrentTime; */
150 * NOTE: check for acptr->user == cptr->serv->user is necessary to insure
151 * that we got the same one... bleah
153 if (cli_serv(cptr)->user && *(cli_serv(cptr))->by &&
154 (acptr = findNUser(cli_serv(cptr)->by))) {
155 if (cli_user(acptr) == cli_serv(cptr)->user) {
156 sendcmdto_one(&me, CMD_NOTICE, acptr, "%C :Link with %s established.",
161 * if not the same client, set by to empty string
164 *(cli_serv(cptr))->by = '\0';
168 sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s established.", inpath);
169 cli_serv(cptr)->up = &me;
170 cli_serv(cptr)->updown = add_dlink(&(cli_serv(&me))->down, cptr);
171 sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s", cli_name(&me),
175 * Old sendto_serv_but_one() call removed because we now
176 * need to send different names to different servers
177 * (domain name matching) Send new server to other servers.
179 for (i = 0; i <= HighestFd; i++)
181 if (!(acptr = LocalClientArray[i]) || !IsServer(acptr) ||
182 acptr == cptr || IsMe(acptr))
184 if (!match(cli_name(&me), cli_name(cptr)))
186 sendcmdto_one(&me, CMD_SERVER, acptr,
187 "%s 2 0 %Tu J%02u %s%s +%s%s%s :%s", cli_name(cptr),
188 cli_serv(cptr)->timestamp, Protocol(cptr), NumServCap(cptr),
189 IsHub(cptr) ? "h" : "", IsService(cptr) ? "s" : "",
190 IsIPv6(cptr) ? "6" : "", cli_info(cptr));
193 /* Send these as early as possible so that glined users/juped servers can
194 * be removed from the network while the remote server is still chewing
201 * Pass on my client information to the new server
203 * First, pass only servers (idea is that if the link gets
204 * canceled because the server was already there,
205 * there are no NICK's to be canceled...). Of course,
206 * if cancellation occurs, all this info is sent anyway,
207 * and I guess the link dies when a read is attempted...? --msa
209 * Note: Link cancellation to occur at this point means
210 * that at least two servers from my fragment are building
211 * up connection this other fragment at the same time, it's
212 * a race condition, not the normal way of operation...
215 for (acptr = &me; acptr; acptr = cli_prev(acptr)) {
216 /* acptr->from == acptr for acptr == cptr */
217 if (cli_from(acptr) == cptr)
219 if (IsServer(acptr)) {
220 const char* protocol_str;
222 if (Protocol(acptr) > 9)
223 protocol_str = IsBurst(acptr) ? "J" : "P";
225 protocol_str = IsBurst(acptr) ? "J0" : "P0";
227 if (0 == match(cli_name(&me), cli_name(acptr)))
229 sendcmdto_one(cli_serv(acptr)->up, CMD_SERVER, cptr,
230 "%s %d 0 %Tu %s%u %s%s +%s%s%s :%s", cli_name(acptr),
231 cli_hopcount(acptr) + 1, cli_serv(acptr)->timestamp,
232 protocol_str, Protocol(acptr), NumServCap(acptr),
233 IsHub(acptr) ? "h" : "", IsService(acptr) ? "s" : "",
234 IsIPv6(acptr) ? "6" : "", cli_info(acptr));
238 for (acptr = &me; acptr; acptr = cli_prev(acptr))
240 /* acptr->from == acptr for acptr == cptr */
241 if (cli_from(acptr) == cptr)
246 char *s = umode_str(acptr);
247 sendcmdto_one(cli_user(acptr)->server, CMD_NICK, cptr,
248 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
249 cli_name(acptr), cli_hopcount(acptr) + 1, cli_lastnick(acptr),
250 cli_user(acptr)->username, cli_user(acptr)->realhost,
251 *s ? "+" : "", s, *s ? " " : "",
252 iptobase64(xxx_buf, &cli_ip(acptr), sizeof(xxx_buf), IsIPv6(cptr)),
253 NumNick(acptr), cli_info(acptr));
257 * Last, send the BURST.
258 * (Or for 2.9 servers: pass all channels plus statuses)
261 struct Channel *chptr;
262 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
263 send_channel_modes(cptr, chptr);
265 sendcmdto_one(&me, CMD_END_OF_BURST, cptr, "");