Author: Isomer <isomer@coders.net>
[ircu2.10.12-pk.git] / ircd / m_ping.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_ping.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
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)
12  * any later version.
13  *
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.
18  *
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.
22  *
23  * $Id$
24  */
25
26 /*
27  * Stupid ircd Tricks
28  * Excerpt from conversation on #coder-com:
29  *  > the number horks it
30  *  <Gte-> yea
31  *  > thinks it's a numeric?
32  *  > no
33  *  <Gte-> hm
34  *  > why would the number hork it
35  *  <Gte-> why is it even looking up servers in a client ping :)
36  *  > it's a server ping
37  *  <Gte-> true
38  *  > oh
39  *  > I know why :)
40  *  >   origin = parv[1];
41  *  >   destination = parv[2];
42  *  >   acptr = FindClient(origin);
43  *  >   if (acptr && acptr != sptr)
44  *  >     origin = cptr->name;
45  *  > heh, that's a bug :)
46  *  <Gte-> yea, client/server handling in the same function sucks :P
47  *  > blindly pass a bogus origin around, and where do you send the reply to?
48  *  <Gte-> I tried /quote ping 12345 uworld.blah.net locally, and uworld
49  *  +recieved -> ":Gte- PING N :Uworld.blah.net"
50  *  <Gte-> oh no, sorry, I did ping N :)
51  *  > right, it's broken
52  *  <Gte-> good thing it doesn't have a fit replying to it
53  *  > hmm
54  *  *** plano.tx.us.undernet.org: PONG received from plano.tx.us.undernet.org
55  *  > you can send a number for the first ping .. but if you ping a remote server
56  *  +it goes to the remote end where it's received as a unsolicited pong by the
57  *  +server closest to the target
58  *  > hahaha
59  *  <Gte-> cool
60  *  <Gte-> Parsing: :Gte PING 12345 :widnes.uk.eu.blah.net
61  *  <Gte-> Sending [:widnes.uk.eu.blah.net PONG widnes.uk.eu.blah.net
62  *  +:12345] to alphatest.blah.net
63  *  <Gte-> Parsing: :alphatest.blah.net 402 widnes.uk.eu.blah.net
64  *  +12345 :No such server
65  *  > oh even better, the pongee sends no such server :)
66  *  > bwhahahah
67  *  <Gte-> for a second, I thought you could trigger a loop, which would be
68  *  +hideously nasty, but it doesn't look like
69  *  > it goes a ------ > b ------- > c ------- > d then c <------- d then c -----
70  *  +> d :)
71  *  <Gte-> weeee
72  *  <Gte-> [04:15] -> Server: ping Gte- Dallas-R.Tx.US.Undernet.org
73  *  <Gte-> *** PONG from Dallas-R.Tx.US.Undernet.org: Gte-
74  *  <Gte-> there we go
75  *  <Gte-> I can get a ping reply from a server I'm not on :)
76  */
77 /*
78  * m_functions execute protocol messages on this server:
79  *
80  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
81  *            structure (with an open socket connected!). This
82  *            identifies the physical socket where the message
83  *            originated (or which caused the m_function to be
84  *            executed--some m_functions may call others...).
85  *
86  *    sptr    is the source of the message, defined by the
87  *            prefix part of the message if present. If not
88  *            or prefix not found, then sptr==cptr.
89  *
90  *            (!IsServer(cptr)) => (cptr == sptr), because
91  *            prefixes are taken *only* from servers...
92  *
93  *            (IsServer(cptr))
94  *                    (sptr == cptr) => the message didn't
95  *                    have the prefix.
96  *
97  *                    (sptr != cptr && IsServer(sptr) means
98  *                    the prefix specified servername. (?)
99  *
100  *                    (sptr != cptr && !IsServer(sptr) means
101  *                    that message originated from a remote
102  *                    user (not local).
103  *
104  *            combining
105  *
106  *            (!IsServer(sptr)) means that, sptr can safely
107  *            taken as defining the target structure of the
108  *            message in this server.
109  *
110  *    *Always* true (if 'parse' and others are working correct):
111  *
112  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
113  *
114  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
115  *            *cannot* be a local connection, unless it's
116  *            actually cptr!). [MyConnect(x) should probably
117  *            be defined as (x == x->from) --msa ]
118  *
119  *    parc    number of variable parameter strings (if zero,
120  *            parv is allowed to be NULL)
121  *
122  *    parv    a NULL terminated list of parameter pointers,
123  *
124  *                    parv[0], sender (prefix string), if not present
125  *                            this points to an empty string.
126  *                    parv[1]...parv[parc-1]
127  *                            pointers to additional parameters
128  *                    parv[parc] == NULL, *always*
129  *
130  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
131  *                    non-NULL pointers.
132  */
133 #if 0
134 /*
135  * No need to include handlers.h here the signatures must match
136  * and we don't need to force a rebuild of all the handlers everytime
137  * we add a new one to the list. --Bleep
138  */
139 #include "handlers.h"
140 #endif /* 0 */
141 #include "client.h"
142 #include "hash.h"
143 #include "ircd_reply.h"
144 #include "ircd_string.h"
145 #include "ircd.h"
146 #include "msg.h"
147 #include "numeric.h"
148 #include "numnicks.h"
149 #include "s_debug.h"
150 #include "send.h"
151
152 #include <assert.h>
153 #include <string.h>
154
155 /*
156  * m_ping - generic message handler
157  *
158  * parv[0] = sender prefix
159  * parv[1] = origin
160  * parv[2] = destination
161  */
162 int m_ping(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
163 {
164   struct Client* acptr;
165   char*          destination;
166   assert(0 != cptr);
167   assert(cptr == sptr);
168
169   if (parc < 2 || EmptyString(parv[1]))
170     return send_reply(sptr, ERR_NOORIGIN);
171
172   destination = parv[2];        /* Will get NULL or pointer (parc >= 2!!) */
173
174   if (!EmptyString(destination) && 0 != ircd_strcmp(destination, me.name)) {
175     if ((acptr = FindServer(destination)))
176       sendcmdto_one(sptr, CMD_PING, acptr, "%C :%s", sptr, destination);
177     else
178       send_reply(sptr, ERR_NOSUCHSERVER, destination);
179   }
180   else {
181     /*
182      * NOTE: clients rely on this to return the origin string.
183      * it's pointless to send more than 64 bytes back tho'
184      */
185     char* origin = parv[1];
186     
187     /* Is this supposed to be here? */
188     acptr = FindClient(origin);
189     if (acptr && acptr != sptr)
190       origin = cptr->name;
191     
192     if (strlen(origin) > 64)
193       origin[64] = '\0';
194     sendcmdto_one(&me, CMD_PONG, sptr, "%C :%s", &me, origin);
195   }
196   return 0;
197 }
198
199 /*
200  * Extension notes
201  *
202  * <Vek> bleep:  here's the change you make to PING:
203  * <Vek> F TOK_PING F G 7777777777.7777777
204  * <Vek> then optional parameter for further enhancement
205  * <Vek> G TOK_PONG G F 7777777777.7777777 0.1734637
206  */
207
208 /*
209  * ms_ping - server message handler template
210  */
211 int ms_ping(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
212 {
213   struct Client* acptr;
214   char*          origin;
215   char*          destination;
216
217   assert(0 != cptr);
218   assert(0 != sptr);
219   assert(IsServer(cptr));
220
221   if (parc < 2 || EmptyString(parv[1])) {
222     /*
223      * don't bother sending the error back
224      */
225 #if 0
226     sendto_one(sptr, err_str(ERR_NOORIGIN), me.name, parv[0]); /* XXX DEAD */
227 #endif
228     return 0;
229   }
230   origin      = parv[1];
231   destination = parv[2];        /* Will get NULL or pointer (parc >= 2!!) */
232
233   if (!EmptyString(destination) && 0 != ircd_strcmp(destination, me.name)) {
234     if ((acptr = FindServer(destination))) {
235       /*
236        * Servers can just forward the origin
237        */
238       sendcmdto_one(sptr, CMD_PING, acptr, "%s :%s", origin, destination);
239     }
240     else {
241       /*
242        * this can happen if server split before the ping got here
243        */
244       send_reply(sptr, ERR_NOSUCHSERVER, destination);
245     }
246   }
247   else {
248     /*
249      * send pong back
250      * NOTE:  sptr is never local so if pong handles numerics everywhere we
251      * could send a numeric here.
252      */
253     sendcmdto_one(&me, CMD_PONG, sptr, "%C :%s", &me, origin);
254   }
255   return 0;
256 }
257
258
259 #if 0
260 /*
261  * m_ping
262  *
263  * parv[0] = sender prefix
264  * parv[1] = origin
265  * parv[2] = destination
266  */
267 int m_ping(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
268 {
269   struct Client *acptr;
270   char *origin, *destination;
271
272   if (parc < 2 || *parv[1] == '\0')
273   {
274     sendto_one(sptr, err_str(ERR_NOORIGIN), me.name, parv[0]); /* XXX DEAD */
275     return 0;
276   }
277   origin = parv[1];
278   destination = parv[2];        /* Will get NULL or pointer (parc >= 2!!) */
279
280   acptr = FindClient(origin);
281   if (acptr && acptr != sptr)
282     origin = cptr->name;
283
284   if (!EmptyString(destination) && 0 != ircd_strcmp(destination, me.name) != 0)
285   {
286     if ((acptr = FindServer(destination)))
287       sendto_one(acptr, ":%s PING %s :%s", parv[0], origin, destination); /* XXX DEAD */
288     else
289     {
290       sendto_one(sptr, err_str(ERR_NOSUCHSERVER), /* XXX DEAD */
291           me.name, parv[0], destination);
292       return 0;
293     }
294   }
295   else
296     sendto_one(sptr, ":%s PONG %s :%s", me.name, me.name, origin); /* XXX DEAD */
297   return 0;
298 }
299 #endif
300