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
86 * No need to include handlers.h here the signatures must match
87 * and we don't need to force a rebuild of all the handlers everytime
88 * we add a new one to the list. --Bleep
95 #include "ircd_reply.h"
96 #include "ircd_string.h"
100 #include "numnicks.h"
112 * ms_uping - server message handler
116 * parv[0] = sender prefix
117 * parv[1] = pinged server
119 * parv[3] = hunted server
120 * parv[4] = number of requested pings
122 int ms_uping(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
124 struct ConfItem *aconf;
131 if (!IsAnOper(sptr)) {
132 send_reply(sptr, ERR_NOPRIVILEGES);
137 send_reply(sptr, ERR_NEEDMOREPARAMS, "UPING");
141 if (hunt_server_cmd(sptr, CMD_UPING, cptr, 1, "%s %s %C %s", 3, parc, parv)
145 * Determine port: First user supplied, then default : 7007
147 if (EmptyString(parv[2]) || (port = atoi(parv[2])) <= 0)
148 port = atoi(UDP_PORT);
150 if (EmptyString(parv[4]) || (count = atoi(parv[4])) <= 0)
152 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING : Illegal number of "
153 "packets: %s", sptr, parv[4]);
157 * Check if a CONNECT would be possible at all (adapted from m_connect)
159 if ((aconf = conf_find_server(parv[1])))
160 uping_server(sptr, aconf, port, count);
162 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host %s not listed in "
163 "ircd.conf", sptr, parv[1]);
169 * mo_uping - oper message handler
173 * parv[0] = sender prefix
174 * parv[1] = pinged server
176 * parv[3] = hunted server
177 * parv[4] = number of requested pings
179 int mo_uping(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
181 struct ConfItem *aconf;
187 assert(cptr == sptr);
189 assert(IsAnOper(sptr));
192 send_reply(sptr, ERR_NEEDMOREPARAMS, "UPING");
197 parv[parc++] = UDP_PORT;
198 parv[parc++] = cli_name(&me);
201 else if (parc == 3) {
202 if (IsDigit(*parv[2]))
203 parv[parc++] = cli_name(&me);
205 parv[parc++] = parv[2];
210 else if (parc == 4) {
211 if (IsDigit(*parv[2])) {
212 if (IsDigit(*parv[3])) {
213 parv[parc++] = parv[3];
214 parv[3] = cli_name(&me);
220 parv[parc++] = parv[3];
225 if (hunt_server_cmd(sptr, CMD_UPING, sptr, 1, "%s %s %C %s", 3, parc, parv)
229 * Determine port: First user supplied, then default : 7007
231 if (EmptyString(parv[2]) || (port = atoi(parv[2])) <= 0)
232 port = atoi(UDP_PORT);
234 if (EmptyString(parv[4]) || (count = atoi(parv[4])) <= 0)
236 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Illegal number of "
237 "packets: %s", sptr, parv[4]);
241 * Check if a CONNECT would be possible at all (adapted from m_connect)
243 if ((aconf = conf_find_server(parv[1])))
244 uping_server(sptr, aconf, port, count);
246 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host %s not listed in "
247 "ircd.conf", sptr, parv[1]);
257 * parv[0] = sender prefix
258 * parv[1] = pinged server
260 * parv[3] = hunted server
261 * parv[4] = number of requested pings
263 int m_uping(struct Client* cptr, struct Client *sptr, int parc, char *parv[])
265 struct ConfItem *aconf;
268 struct UPing* pptr = 0;
270 if (!IsPrivileged(sptr))
272 sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]); /* XXX DEAD */
278 sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "UPING"); /* XXX DEAD */
286 parv[parc++] = UDP_PORT;
287 parv[parc++] = me.name;
292 if (IsDigit(*parv[2]))
293 parv[parc++] = me.name;
296 parv[parc++] = parv[2];
303 if (IsDigit(*parv[2]))
305 if (IsDigit(*parv[3]))
307 parv[parc++] = parv[3];
315 parv[parc++] = parv[3];
321 if (hunt_server(1, cptr, sptr, ":%s UPING %s %s %s %s", 3, parc, parv) != HUNTED_ISME) /* XXX DEAD */
324 if (BadPtr(parv[4]) || atoi(parv[4]) <= 0)
326 if (MyUser(sptr) || Protocol(cptr) < 10)
327 sendto_one(sptr, ":%s NOTICE %s :UPING: Illegal number of packets: %s", /* XXX DEAD */
328 me.name, parv[0], parv[4]);
330 sendto_one(sptr, "%s NOTICE %s%s :UPING: Illegal number of packets: %s", /* XXX DEAD */
331 NumServ(&me), NumNick(sptr), parv[4]);
335 /* Check if a CONNECT would be possible at all (adapted from m_connect) */
336 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
338 if (aconf->status == CONF_SERVER &&
339 match(parv[1], aconf->name) == 0)
344 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
346 if (aconf->status == CONF_SERVER &&
347 (match(parv[1], aconf->host) == 0 ||
348 match(parv[1], strchr(aconf->host, '@') + 1) == 0))
354 if (MyUser(sptr) || Protocol(cptr) < 10)
355 sendto_one(sptr, ":%s NOTICE %s :UPING: Host %s not listed in ircd.conf", /* XXX DEAD */
356 me.name, parv[0], parv[1]);
358 sendto_one(sptr, /* XXX DEAD */
359 "%s NOTICE %s%s :UPING: Host %s not listed in ircd.conf",
360 NumServ(&me), NumNick(sptr), parv[1]);
365 cancel_ping(sptr, sptr); /* Cancel previous ping request */
368 * Determine port: First user supplied, then default : 7007
370 if (BadPtr(parv[2]) || (port = atoi(parv[2])) <= 0)
371 port = atoi(UDP_PORT);
373 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
375 sendto_ops("m_uping: socket: %s", (err != EMFILE) /* XXX DEAD */
376 ? ((strerror(err)) ? strerror(err) : "Unknown error") : "No more sockets");
377 if (MyUser(sptr) || Protocol(cptr) < 10)
378 sendto_one(sptr, /* XXX DEAD */
379 ":%s NOTICE %s :UPING: Unable to create udp ping socket",
382 sendto_one(sptr, /* XXX DEAD */
383 "%s NOTICE %s%s :UPING: Unable to create udp ping socket",
384 NumServ(&me), NumNick(sptr));
385 ircd_log(L_ERROR, "UPING: Unable to create UDP socket"); /* XXX DEAD */
389 if (!os_set_nonblocking(fd)) {
390 if (MyUser(sptr) || Protocol(cptr) < 10)
391 sendto_one(sptr, ":%s NOTICE %s :UPING: Can't set fd non-blocking", /* XXX DEAD */
394 sendto_one(sptr, "%s NOTICE %s%s :UPING: Can't set fd non-blocking", /* XXX DEAD */
395 NumServ(&me), NumNick(sptr));
399 pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
401 memset(pptr, 0, sizeof(struct UPing));
404 pptr->sin.sin_port = htons(port);
405 pptr->sin.sin_addr.s_addr = aconf->ipnum.s_addr;
406 pptr->sin.sin_family = AF_INET;
407 pptr->count = IRCD_MIN(20, atoi(parv[4]));
408 strcpy(pptr->name, aconf->host);
412 pptr->next = pingList;