2 * IRC - Internet Relay Chat, ircd/m_uping.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
84 * No need to include handlers.h here the signatures must match
85 * and we don't need to force a rebuild of all the handlers everytime
86 * we add a new one to the list. --Bleep
93 #include "ircd_reply.h"
94 #include "ircd_string.h"
110 * ms_uping - server message handler
114 * parv[0] = sender prefix
115 * parv[1] = pinged server
117 * parv[3] = hunted server
118 * parv[4] = number of requested pings
120 int ms_uping(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
122 struct ConfItem *aconf;
129 if (!IsAnOper(sptr)) {
130 sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]);
135 sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "UPING");
139 if (hunt_server(1, cptr, sptr, "%s%s " TOK_UPING " %s %s %s %s", 3, parc, parv) != HUNTED_ISME)
142 * Determine port: First user supplied, then default : 7007
144 if (EmptyString(parv[2]) || (port = atoi(parv[2])) <= 0)
145 port = atoi(UDP_PORT);
147 if (EmptyString(parv[4]) || (count = atoi(parv[4])) <= 0)
150 sendto_one(sptr, ":%s NOTICE %s :UPING: Illegal number of packets: %s",
151 me.name, parv[0], parv[4]);
153 sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Illegal number of packets: %s",
154 NumServ(&me), NumNick(sptr), parv[4]);
158 * Check if a CONNECT would be possible at all (adapted from m_connect)
160 if ((aconf = conf_find_server(parv[1])))
161 uping_server(sptr, aconf, port, count);
164 sendto_one(sptr, ":%s NOTICE %s :UPING: Host %s not listed in ircd.conf",
165 me.name, parv[0], parv[1]);
167 sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Host %s not listed in ircd.conf",
168 NumServ(&me), NumNick(sptr), parv[1]);
174 * mo_uping - oper message handler
178 * parv[0] = sender prefix
179 * parv[1] = pinged server
181 * parv[3] = hunted server
182 * parv[4] = number of requested pings
184 int mo_uping(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
186 struct ConfItem *aconf;
192 assert(cptr == sptr);
194 assert(IsAnOper(sptr));
197 sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "UPING");
202 parv[parc++] = UDP_PORT;
203 parv[parc++] = me.name;
206 else if (parc == 3) {
207 if (IsDigit(*parv[2]))
208 parv[parc++] = me.name;
210 parv[parc++] = parv[2];
215 else if (parc == 4) {
216 if (IsDigit(*parv[2])) {
217 if (IsDigit(*parv[3])) {
218 parv[parc++] = parv[3];
225 parv[parc++] = parv[3];
230 if (hunt_server(1, cptr, sptr, "%s%s " TOK_UPING " %s %s %s %s", 3, parc, parv) != HUNTED_ISME)
233 * Determine port: First user supplied, then default : 7007
235 if (EmptyString(parv[2]) || (port = atoi(parv[2])) <= 0)
236 port = atoi(UDP_PORT);
238 if (EmptyString(parv[4]) || (count = atoi(parv[4])) <= 0)
241 sendto_one(sptr, ":%s NOTICE %s :UPING: Illegal number of packets: %s",
242 me.name, parv[0], parv[4]);
244 sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Illegal number of packets: %s",
245 NumServ(&me), NumNick(sptr), parv[4]);
249 * Check if a CONNECT would be possible at all (adapted from m_connect)
251 if ((aconf = conf_find_server(parv[1])))
252 uping_server(sptr, aconf, port, count);
255 sendto_one(sptr, ":%s NOTICE %s :UPING: Host %s not listed in ircd.conf",
256 me.name, parv[0], parv[1]);
258 sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Host %s not listed in ircd.conf",
259 NumServ(&me), NumNick(sptr), parv[1]);
269 * parv[0] = sender prefix
270 * parv[1] = pinged server
272 * parv[3] = hunted server
273 * parv[4] = number of requested pings
275 int m_uping(struct Client* cptr, struct Client *sptr, int parc, char *parv[])
277 struct ConfItem *aconf;
280 struct UPing* pptr = 0;
282 if (!IsPrivileged(sptr))
284 sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]);
290 sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "UPING");
298 parv[parc++] = UDP_PORT;
299 parv[parc++] = me.name;
304 if (IsDigit(*parv[2]))
305 parv[parc++] = me.name;
308 parv[parc++] = parv[2];
315 if (IsDigit(*parv[2]))
317 if (IsDigit(*parv[3]))
319 parv[parc++] = parv[3];
327 parv[parc++] = parv[3];
333 if (hunt_server(1, cptr, sptr, ":%s UPING %s %s %s %s", 3, parc, parv) != HUNTED_ISME)
336 if (BadPtr(parv[4]) || atoi(parv[4]) <= 0)
338 if (MyUser(sptr) || Protocol(cptr) < 10)
339 sendto_one(sptr, ":%s NOTICE %s :UPING: Illegal number of packets: %s",
340 me.name, parv[0], parv[4]);
342 sendto_one(sptr, "%s NOTICE %s%s :UPING: Illegal number of packets: %s",
343 NumServ(&me), NumNick(sptr), parv[4]);
347 /* Check if a CONNECT would be possible at all (adapted from m_connect) */
348 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
350 if (aconf->status == CONF_SERVER &&
351 match(parv[1], aconf->name) == 0)
356 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
358 if (aconf->status == CONF_SERVER &&
359 (match(parv[1], aconf->host) == 0 ||
360 match(parv[1], strchr(aconf->host, '@') + 1) == 0))
366 if (MyUser(sptr) || Protocol(cptr) < 10)
367 sendto_one(sptr, ":%s NOTICE %s :UPING: Host %s not listed in ircd.conf",
368 me.name, parv[0], parv[1]);
371 "%s NOTICE %s%s :UPING: Host %s not listed in ircd.conf",
372 NumServ(&me), NumNick(sptr), parv[1]);
377 cancel_ping(sptr, sptr); /* Cancel previous ping request */
380 * Determine port: First user supplied, then default : 7007
382 if (BadPtr(parv[2]) || (port = atoi(parv[2])) <= 0)
383 port = atoi(UDP_PORT);
385 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
387 sendto_ops("m_uping: socket: %s", (err != EMFILE)
388 ? ((strerror(err)) ? strerror(err) : "Unknown error") : "No more sockets");
389 if (MyUser(sptr) || Protocol(cptr) < 10)
391 ":%s NOTICE %s :UPING: Unable to create udp ping socket",
395 "%s NOTICE %s%s :UPING: Unable to create udp ping socket",
396 NumServ(&me), NumNick(sptr));
397 ircd_log(L_ERROR, "UPING: Unable to create UDP socket");
401 if (!os_set_nonblocking(fd)) {
402 if (MyUser(sptr) || Protocol(cptr) < 10)
403 sendto_one(sptr, ":%s NOTICE %s :UPING: Can't set fd non-blocking",
406 sendto_one(sptr, "%s NOTICE %s%s :UPING: Can't set fd non-blocking",
407 NumServ(&me), NumNick(sptr));
411 pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
413 memset(pptr, 0, sizeof(struct UPing));
416 pptr->sin.sin_port = htons(port);
417 pptr->sin.sin_addr.s_addr = aconf->ipnum.s_addr;
418 pptr->sin.sin_family = AF_INET;
419 pptr->count = IRCD_MIN(20, atoi(parv[4]));
420 strcpy(pptr->name, aconf->host);
424 pptr->next = pingList;