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 send_reply(sptr, ERR_NOPRIVILEGES);
135 send_reply(sptr, ERR_NEEDMOREPARAMS, "UPING");
139 if (hunt_server_cmd(sptr, CMD_UPING, cptr, 1, "%s %s %C %s", 3, parc, parv)
143 * Determine port: First user supplied, then default : 7007
145 if (EmptyString(parv[2]) || (port = atoi(parv[2])) <= 0)
146 port = atoi(UDP_PORT);
148 if (EmptyString(parv[4]) || (count = atoi(parv[4])) <= 0)
150 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING : Illegal number of "
151 "packets: %s", sptr, parv[4]);
155 * Check if a CONNECT would be possible at all (adapted from m_connect)
157 if ((aconf = conf_find_server(parv[1])))
158 uping_server(sptr, aconf, port, count);
160 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host %s not listed in "
161 "ircd.conf", sptr, parv[1]);
167 * mo_uping - oper message handler
171 * parv[0] = sender prefix
172 * parv[1] = pinged server
174 * parv[3] = hunted server
175 * parv[4] = number of requested pings
177 int mo_uping(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
179 struct ConfItem *aconf;
185 assert(cptr == sptr);
187 assert(IsAnOper(sptr));
190 send_reply(sptr, ERR_NEEDMOREPARAMS, "UPING");
195 parv[parc++] = UDP_PORT;
196 parv[parc++] = cli_name(&me);
199 else if (parc == 3) {
200 if (IsDigit(*parv[2]))
201 parv[parc++] = cli_name(&me);
203 parv[parc++] = parv[2];
208 else if (parc == 4) {
209 if (IsDigit(*parv[2])) {
210 if (IsDigit(*parv[3])) {
211 parv[parc++] = parv[3];
212 parv[3] = cli_name(&me);
218 parv[parc++] = parv[3];
223 if (hunt_server_cmd(sptr, CMD_UPING, sptr, 1, "%s %s %C %s", 3, parc, parv)
227 * Determine port: First user supplied, then default : 7007
229 if (EmptyString(parv[2]) || (port = atoi(parv[2])) <= 0)
230 port = atoi(UDP_PORT);
232 if (EmptyString(parv[4]) || (count = atoi(parv[4])) <= 0)
234 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Illegal number of "
235 "packets: %s", sptr, parv[4]);
239 * Check if a CONNECT would be possible at all (adapted from m_connect)
241 if ((aconf = conf_find_server(parv[1])))
242 uping_server(sptr, aconf, port, count);
244 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host %s not listed in "
245 "ircd.conf", sptr, parv[1]);
255 * parv[0] = sender prefix
256 * parv[1] = pinged server
258 * parv[3] = hunted server
259 * parv[4] = number of requested pings
261 int m_uping(struct Client* cptr, struct Client *sptr, int parc, char *parv[])
263 struct ConfItem *aconf;
266 struct UPing* pptr = 0;
268 if (!IsPrivileged(sptr))
270 sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]); /* XXX DEAD */
276 sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "UPING"); /* XXX DEAD */
284 parv[parc++] = UDP_PORT;
285 parv[parc++] = me.name;
290 if (IsDigit(*parv[2]))
291 parv[parc++] = me.name;
294 parv[parc++] = parv[2];
301 if (IsDigit(*parv[2]))
303 if (IsDigit(*parv[3]))
305 parv[parc++] = parv[3];
313 parv[parc++] = parv[3];
319 if (hunt_server(1, cptr, sptr, ":%s UPING %s %s %s %s", 3, parc, parv) != HUNTED_ISME) /* XXX DEAD */
322 if (BadPtr(parv[4]) || atoi(parv[4]) <= 0)
324 if (MyUser(sptr) || Protocol(cptr) < 10)
325 sendto_one(sptr, ":%s NOTICE %s :UPING: Illegal number of packets: %s", /* XXX DEAD */
326 me.name, parv[0], parv[4]);
328 sendto_one(sptr, "%s NOTICE %s%s :UPING: Illegal number of packets: %s", /* XXX DEAD */
329 NumServ(&me), NumNick(sptr), parv[4]);
333 /* Check if a CONNECT would be possible at all (adapted from m_connect) */
334 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
336 if (aconf->status == CONF_SERVER &&
337 match(parv[1], aconf->name) == 0)
342 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
344 if (aconf->status == CONF_SERVER &&
345 (match(parv[1], aconf->host) == 0 ||
346 match(parv[1], strchr(aconf->host, '@') + 1) == 0))
352 if (MyUser(sptr) || Protocol(cptr) < 10)
353 sendto_one(sptr, ":%s NOTICE %s :UPING: Host %s not listed in ircd.conf", /* XXX DEAD */
354 me.name, parv[0], parv[1]);
356 sendto_one(sptr, /* XXX DEAD */
357 "%s NOTICE %s%s :UPING: Host %s not listed in ircd.conf",
358 NumServ(&me), NumNick(sptr), parv[1]);
363 cancel_ping(sptr, sptr); /* Cancel previous ping request */
366 * Determine port: First user supplied, then default : 7007
368 if (BadPtr(parv[2]) || (port = atoi(parv[2])) <= 0)
369 port = atoi(UDP_PORT);
371 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
373 sendto_ops("m_uping: socket: %s", (err != EMFILE) /* XXX DEAD */
374 ? ((strerror(err)) ? strerror(err) : "Unknown error") : "No more sockets");
375 if (MyUser(sptr) || Protocol(cptr) < 10)
376 sendto_one(sptr, /* XXX DEAD */
377 ":%s NOTICE %s :UPING: Unable to create udp ping socket",
380 sendto_one(sptr, /* XXX DEAD */
381 "%s NOTICE %s%s :UPING: Unable to create udp ping socket",
382 NumServ(&me), NumNick(sptr));
383 ircd_log(L_ERROR, "UPING: Unable to create UDP socket"); /* XXX DEAD */
387 if (!os_set_nonblocking(fd)) {
388 if (MyUser(sptr) || Protocol(cptr) < 10)
389 sendto_one(sptr, ":%s NOTICE %s :UPING: Can't set fd non-blocking", /* XXX DEAD */
392 sendto_one(sptr, "%s NOTICE %s%s :UPING: Can't set fd non-blocking", /* XXX DEAD */
393 NumServ(&me), NumNick(sptr));
397 pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
399 memset(pptr, 0, sizeof(struct UPing));
402 pptr->sin.sin_port = htons(port);
403 pptr->sin.sin_addr.s_addr = aconf->ipnum.s_addr;
404 pptr->sin.sin_family = AF_INET;
405 pptr->count = IRCD_MIN(20, atoi(parv[4]));
406 strcpy(pptr->name, aconf->host);
410 pptr->next = pingList;