Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / uping.c
1 /*
2  * IRC - Internet Relay Chat, ircd/uping.c
3  * Copyright (C) 1994 Carlo Wood ( Run @ undernet.org )
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * $Id$
20  */
21 #include "uping.h"
22 #include "client.h"
23 #include "ircd.h"
24 #include "ircd_alloc.h"
25 #include "ircd_log.h"
26 #include "ircd_osdep.h"
27 #include "ircd_string.h"
28 #include "match.h"
29 #include "msg.h"
30 #include "numeric.h"
31 #include "numnicks.h"
32 #include "s_bsd.h"    /* vserv */
33 #include "s_conf.h"
34 #include "s_debug.h"
35 #include "s_misc.h"
36 #include "s_user.h"
37 #include "send.h"
38 #include "sys.h"
39
40 #include <arpa/inet.h>
41 #include <assert.h>
42 #include <errno.h>
43 #include <netdb.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/socket.h>
48 #include <sys/time.h>
49 #include <unistd.h>
50
51 #define UPINGTIMEOUT 60   /* Timeout waiting for ping responses */
52
53 #ifndef INADDR_NONE
54 #define INADDR_NONE 0xffffffff
55 #endif
56
57 static struct UPing* pingList = 0;
58 int UPingFileDescriptor       = -1; /* UDP listener socket for upings */
59
60 /*
61  * pings_begin - iterator function for ping list 
62  */
63 struct UPing* uping_begin(void)
64 {
65   return pingList;
66 }
67
68 /*
69  * pings_erase - removes ping struct from ping list
70  */
71 static void uping_erase(struct UPing* p)
72 {
73   struct UPing* it;
74   struct UPing* last = 0;
75
76   assert(0 != p);
77   
78   for (it = pingList; it; last = it, it = it->next) {
79     if (p == it) {
80       if (last)
81         last->next = p->next;
82       else
83         pingList = p->next;
84       break;
85     }
86   }
87 }
88
89 /*
90  * Setup a UDP socket and listen for incoming packets
91  */
92 int uping_init(void)
93 {
94   struct sockaddr_in from = { 0 };
95   int fd;
96
97   memset(&from, 0, sizeof(from));
98   from.sin_addr = VirtualHost.sin_addr;
99   from.sin_port = htons(atoi(UDP_PORT));
100   from.sin_family = AF_INET;
101
102   if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
103     Debug((DEBUG_ERROR, "UPING: UDP listener socket call failed: %s", 
104            (strerror(errno)) ? strerror(errno) : "Unknown error"));
105     return -1;
106   }
107   if (!os_set_reuseaddr(fd)) {
108     ircd_log(L_ERROR, "UPING: setsockopt UDP listener: fd %d", fd);
109     Debug((DEBUG_ERROR, "UPING: set reuseaddr on UDP listener failed: %s",
110            (strerror(errno)) ? strerror(errno) : "Unknown error"));
111     close(fd);
112     return -1;
113   }
114   if (bind(fd, (struct sockaddr*) &from, sizeof(from)) == -1) {
115     ircd_log(L_ERROR, "UPING: bind UDP listener %d fd %d", htons(from.sin_port), fd);
116     Debug((DEBUG_ERROR, "UPING: bind on UDP listener failed : %s",
117            (strerror(errno)) ? strerror(errno) : "Unknown error"));
118     close(fd);
119     return -1;
120   }
121   if (!os_set_nonblocking(fd)) {
122     Debug((DEBUG_ERROR, "UPING: set non-blocking: %s",
123            (strerror(errno)) ? strerror(errno) : "Unknown error"));
124     close(fd);
125     return -1;
126   }
127   UPingFileDescriptor = fd;
128   return fd;
129 }
130
131
132 /*
133  * max # of pings set to 15/sec.
134  */
135 void uping_echo()
136 {
137   struct sockaddr_in from = { 0 };
138   unsigned int       len = 0;
139   static time_t      last = 0;
140   static int         counter = 0;
141   char               buf[BUFSIZE + 1];
142
143   Debug((DEBUG_DEBUG, "UPING: uping_echo"));
144
145   if (IO_SUCCESS != os_recvfrom_nonb(UPingFileDescriptor, buf, BUFSIZE, &len, &from))
146     return;
147   /*
148    * count em even if we're getting flooded so we can tell we're getting
149    * flooded.
150    */
151   ++ServerStats->uping_recv;
152   if (CurrentTime == last) {
153     if (++counter > 10)
154       return;
155   }
156   else {
157     counter = 0;
158     last    = CurrentTime;
159   }
160   if (len < 19)
161     return;
162   sendto(UPingFileDescriptor, buf, len, 0, (struct sockaddr*) &from, sizeof(from));
163 }
164
165
166 /*
167  * start_ping
168  */
169 static void uping_start(struct UPing* pptr)
170 {
171   assert(0 != pptr);
172
173   if (MyUser(pptr->client)) {
174     sendto_one(pptr->client,
175                ":%s NOTICE %s :Sending %d ping%s to %s",
176                me.name, pptr->client->name, pptr->count,
177                (pptr->count == 1) ? "" : "s", pptr->name);
178   }
179   else {
180     sendto_one(pptr->client,
181                "%s NOTICE %s%s :Sending %d ping%s to %s",
182                NumServ(&me), NumNick(pptr->client), pptr->count,
183                (pptr->count == 1) ? "" : "s", pptr->name);
184   }
185   pptr->timeout = CurrentTime + UPINGTIMEOUT;
186   pptr->active = 1;
187 }
188
189 /*
190  * uping_send
191  *
192  */
193 void uping_send(struct UPing* pptr)
194 {
195   struct timeval tv;
196   char buf[BUFSIZE + 1];
197
198   assert(0 != pptr);
199   if (pptr->sent == pptr->count)
200     return;
201   memset(buf, 0, sizeof(buf));
202
203   gettimeofday(&tv, NULL);
204   sprintf(buf, " %10lu%c%6lu", tv.tv_sec, '\0', tv.tv_usec);
205
206   Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d",
207           buf, &buf[12],
208           ircd_ntoa((const char*) &pptr->sin.sin_addr), ntohs(pptr->sin.sin_port),
209           pptr->fd));
210
211   if (sendto(pptr->fd, buf, BUFSIZE, 0, (struct sockaddr*) &pptr->sin,
212              sizeof(struct sockaddr_in)) != BUFSIZE)
213   {
214     const char* msg = strerror(errno);
215     if (!msg)
216       msg = "Unknown error";
217     if (pptr->client) {
218       if (MyUser(pptr->client))
219         sendto_one(pptr->client, ":%s NOTICE %s :UPING: send failed: %s",
220                    me.name, pptr->client->name, msg);
221       else
222         sendto_one(pptr->client, "%s NOTICE %s%s :UPING: sendto() failed: %s",
223                    NumServ(&me), NumNick(pptr->client), msg);
224     }
225     Debug((DEBUG_DEBUG, "UPING: send_ping: sendto failed on %d: %s", pptr->fd, msg));
226     uping_end(pptr);
227     return;
228   }
229   ++pptr->sent;
230 }
231
232 /*
233  * read_ping
234  */
235 void uping_read(struct UPing* pptr)
236 {
237   struct sockaddr_in sin;
238   struct timeval     tv;
239   unsigned int       len;
240   unsigned int       pingtime;
241   char*              s;
242   char               buf[BUFSIZE + 1];
243   IOResult           ior;
244
245   assert(0 != pptr);
246
247   gettimeofday(&tv, NULL);
248
249   ior = os_recvfrom_nonb(pptr->fd, buf, BUFSIZE, &len, &sin);
250   if (IO_BLOCKED == ior)
251     return;
252   else if (IO_FAILURE == ior) {
253     const char* msg = strerror(errno);
254     if (!msg)
255       msg = "Unknown error";
256     if (MyUser(pptr->client))
257       sendto_one(pptr->client, ":%s NOTICE %s :UPING: receive error: %s",
258                  me.name, pptr->client->name, msg);
259     else
260       sendto_one(pptr->client, "%s NOTICE %s%s :UPING: receive error: %s",
261                  NumServ(&me), NumNick(pptr->client), msg);
262     uping_end(pptr);
263     return;
264   }    
265
266   if (len < 19)
267     return;                     /* Broken packet */
268    
269   ++pptr->received;
270
271   buf[len] = 0;
272   pingtime = (tv.tv_sec - atol(&buf[1])) * 1000
273              + (tv.tv_usec - atol(buf + strlen(buf) + 1)) / 1000;
274
275   pptr->ms_ave += pingtime;
276   if (!pptr->ms_min || pptr->ms_min > pingtime)
277     pptr->ms_min = pingtime;
278   if (pingtime > pptr->ms_max)
279     pptr->ms_max = pingtime;
280   
281   pptr->timeout = CurrentTime + UPINGTIMEOUT;
282
283   Debug((DEBUG_SEND, "read_ping: %d bytes, ti %lu: [%s %s] %lu ms",
284          len, pptr->timeout, buf, (buf + strlen(buf) + 1), pingtime));
285
286   s = pptr->buf + strlen(pptr->buf);
287   sprintf(s, " %u", pingtime);
288
289   if (pptr->received == pptr->count)
290     uping_end(pptr);
291   return;
292 }
293
294 int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int count)
295 {
296   int fd;
297   struct UPing* pptr;
298
299   assert(0 != sptr);
300   assert(0 != aconf);
301
302   if (INADDR_NONE == aconf->ipnum.s_addr) {
303     if (MyUser(sptr))
304       sendto_one(sptr, ":%s NOTICE %s :UPING: Host lookup failed for %s",
305                  me.name, sptr->name, aconf->name);
306     else
307       sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Host lookup failed for %s",
308                  NumServ(&me), NumNick(sptr), aconf->name);
309   }
310
311   if (IsUPing(sptr))
312     uping_cancel(sptr, sptr);  /* Cancel previous ping request */
313
314   if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
315     if (MyUser(sptr))
316       sendto_one(sptr, ":%s NOTICE %s :UPING: Unable to create udp ping socket",
317                  me.name, sptr->name);
318     else
319       sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Unable to create udp ping socket",
320                  NumServ(&me), NumNick(sptr));
321     return 0;
322   }
323
324   if (!os_set_nonblocking(fd)) {
325     if (MyUser(sptr))
326       sendto_one(sptr, ":%s NOTICE %s :UPING: Can't set fd non-blocking",
327                  me.name, sptr->name);
328     else
329       sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Can't set fd non-blocking",
330                  NumServ(&me), NumNick(sptr));
331     close(fd);
332     return 0;
333   }
334   pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
335   assert(0 != pptr);
336   memset(pptr, 0, sizeof(struct UPing));
337
338   pptr->fd                  = fd;
339   pptr->sin.sin_port        = htons(port);
340   pptr->sin.sin_addr.s_addr = aconf->ipnum.s_addr;
341   pptr->sin.sin_family      = AF_INET;
342   pptr->count               = IRCD_MIN(20, count);
343   pptr->client              = sptr;
344   pptr->index               = -1;
345   strcpy(pptr->name, aconf->name);
346
347   pptr->next = pingList;
348   pingList   = pptr;
349
350   SetUPing(sptr);
351   uping_start(pptr);
352   return 0;
353 }
354
355
356 void uping_end(struct UPing* pptr)
357 {
358   Debug((DEBUG_DEBUG, "uping_end: %p", pptr));
359
360   if (pptr->client) {
361     if (MyUser(pptr->client)) {
362       if (pptr->lastsent) { 
363         if (0 < pptr->received) {
364           sendto_one(pptr->client, ":%s NOTICE %s :UPING %s%s",
365                      me.name, pptr->client->name, pptr->name, pptr->buf);
366           /*
367            * XXX - warning long unsigned int format, unsigned int arg (7, 8, 9)
368            */
369           sendto_one(pptr->client,
370                      ":%s NOTICE %s :UPING Stats: sent %d recvd %d ; "
371                      "min/avg/max = %1lu/%1lu/%1lu ms",
372                      me.name, pptr->client->name, pptr->sent,
373                      pptr->received, pptr->ms_min,
374                      (2 * pptr->ms_ave) / (2 * pptr->received), 
375                      pptr->ms_max);
376         }
377         else
378           sendto_one(pptr->client,
379                      ":%s NOTICE %s :UPING: no response from %s within %d seconds",
380                      me.name, pptr->client->name, pptr->name,
381                      UPINGTIMEOUT);
382       }
383       else
384         sendto_one(pptr->client,
385                    ":%s NOTICE %s :UPING: Could not start ping to %s",
386                    me.name, pptr->client->name, pptr->name);
387     }
388     else {
389       if (pptr->lastsent) {
390         if (0 < pptr->received) {
391           sendto_one(pptr->client, "%s NOTICE %s%s :UPING %s%s",
392                      NumServ(&me), NumNick(pptr->client), pptr->name, pptr->buf);
393           /* XXX - warning: long unsigned int format, unsigned int arg(9, 10, 11) */
394           sendto_one(pptr->client,
395                      "%s " TOK_NOTICE " %s%s :UPING Stats: sent %d recvd %d ; "
396                      "min/avg/max = %1lu/%1lu/%1lu ms",
397                      NumServ(&me), NumNick(pptr->client), pptr->sent,
398                      pptr->received, pptr->ms_min,
399                      (2 * pptr->ms_ave) / (2 * pptr->received), 
400                      pptr->ms_max);
401         }
402         else
403           sendto_one(pptr->client,
404                      "%s " TOK_NOTICE " %s%s :UPING: no response from %s within %d seconds",
405                      NumServ(&me), NumNick(pptr->client), pptr->name, UPINGTIMEOUT);
406       }
407       else
408         sendto_one(pptr->client,
409                    "%s " TOK_NOTICE " %s%s :UPING: Could not start ping to %s",
410                    NumServ(&me), NumNick(pptr->client), pptr->name);
411     }
412   }
413   close(pptr->fd);
414   pptr->fd = -1;
415   uping_erase(pptr);
416   if (pptr->client)
417     ClearUPing(pptr->client);
418   MyFree(pptr);
419 }
420
421 void uping_cancel(struct Client *sptr, struct Client* acptr)
422 {
423   struct UPing* ping;
424   struct UPing* ping_next;
425
426   Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", sptr->name));
427   for (ping = pingList; ping; ping = ping_next) {
428     ping_next = ping->next;
429     if (sptr == ping->client) {
430       ping->client = acptr;
431       uping_end(ping);
432     }
433   }
434   ClearUPing(sptr);
435 }
436
437