IPv6 support (hopefully with fewer future transition pains)
[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 "config.h"
22
23 #include "uping.h"
24 #include "client.h"
25 #include "ircd.h"
26 #include "ircd_alloc.h"
27 #include "ircd_events.h"
28 #include "ircd_log.h"
29 #include "ircd_osdep.h"
30 #include "ircd_string.h"
31 #include "match.h"
32 #include "msg.h"
33 #include "numeric.h"
34 #include "numnicks.h"
35 #include "s_bsd.h"    /* VirtualHost */
36 #include "s_conf.h"
37 #include "s_debug.h"
38 #include "s_misc.h"
39 #include "s_user.h"
40 #include "send.h"
41 #include "sys.h"
42
43 #include <arpa/inet.h>
44 #include <assert.h>
45 #include <errno.h>
46 #include <netdb.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <sys/socket.h>
51 #include <sys/time.h>
52 #include <unistd.h>
53
54 #define UPINGTIMEOUT 60   /* Timeout waiting for ping responses */
55
56 static struct UPing* pingList = 0;
57 int UPingFileDescriptor       = -1; /* UDP listener socket for upings */
58
59 static struct Socket upingSock;
60
61 /*
62  * pings_begin - iterator function for ping list 
63  */
64 struct UPing* uping_begin(void)
65 {
66   return pingList;
67 }
68
69 /*
70  * pings_erase - removes ping struct from ping list
71  */
72 static void uping_erase(struct UPing* p)
73 {
74   struct UPing* it;
75   struct UPing* last = 0;
76
77   assert(0 != p);
78   
79   for (it = pingList; it; last = it, it = it->next) {
80     if (p == it) {
81       if (last)
82         last->next = p->next;
83       else
84         pingList = p->next;
85       break;
86     }
87   }
88 }
89
90 /* Called when the event engine detects activity on the UPing socket */
91 static void uping_echo_callback(struct Event* ev)
92 {
93   assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR);
94
95   uping_echo();
96 }
97
98 /*
99  * Setup a UDP socket and listen for incoming packets
100  */
101 int uping_init(void)
102 {
103   struct irc_sockaddr from;
104   int fd;
105
106   memcpy(&from, &VirtualHost, sizeof(from));
107   from.port = atoi(UDP_PORT);
108
109   fd = os_socket(&from, SOCK_DGRAM, "UDP listener socket");
110   if (fd < 0)
111     return -1;
112   if (!socket_add(&upingSock, uping_echo_callback, 0, SS_DATAGRAM,
113                   SOCK_EVENT_READABLE, fd)) {
114     Debug((DEBUG_ERROR, "UPING: Unable to queue fd to event system"));
115     close(fd);
116     return -1;
117   }
118   UPingFileDescriptor = fd;
119   return fd;
120 }
121
122
123 /*
124  * max # of pings set to 15/sec.
125  */
126 void uping_echo()
127 {
128   struct irc_sockaddr from;
129   unsigned int       len = 0;
130   static time_t      last = 0;
131   static int         counter = 0;
132   char               buf[BUFSIZE + 1];
133
134   Debug((DEBUG_DEBUG, "UPING: uping_echo"));
135
136   if (IO_SUCCESS != os_recvfrom_nonb(UPingFileDescriptor, buf, BUFSIZE, &len, &from))
137     return;
138   /*
139    * count em even if we're getting flooded so we can tell we're getting
140    * flooded.
141    */
142   ++ServerStats->uping_recv;
143   if (CurrentTime == last) {
144     if (++counter > 10)
145       return;
146   }
147   else {
148     counter = 0;
149     last    = CurrentTime;
150   }
151   if (len < 19)
152     return;
153   os_sendto_nonb(UPingFileDescriptor, buf, len, NULL, 0, &from);
154 }
155
156
157 /* Callback when socket has data to read */
158 static void uping_read_callback(struct Event* ev)
159 {
160   struct UPing *pptr;
161
162   assert(0 != ev_socket(ev));
163   assert(0 != s_data(ev_socket(ev)));
164
165   pptr = (struct UPing*) s_data(ev_socket(ev));
166
167   Debug((DEBUG_SEND, "uping_read_callback called, %p (%d)", pptr,
168          ev_type(ev)));
169
170   if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
171     pptr->freeable &= ~UPING_PENDING_SOCKET;
172
173     if (!pptr->freeable)
174       MyFree(pptr); /* done with it, finally */
175   } else {
176     assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR);
177
178     uping_read(pptr); /* read uping response */
179   }
180 }
181
182 /* Callback to send another ping */
183 static void uping_sender_callback(struct Event* ev)
184 {
185   struct UPing *pptr;
186
187   assert(0 != ev_timer(ev));
188   assert(0 != t_data(ev_timer(ev)));
189
190   pptr = (struct UPing*) t_data(ev_timer(ev));
191
192   Debug((DEBUG_SEND, "uping_sender_callback called, %p (%d)", pptr,
193          ev_type(ev)));
194
195   if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
196     pptr->freeable &= ~UPING_PENDING_SENDER;
197
198     if (!pptr->freeable)
199       MyFree(pptr); /* done with it, finally */
200   } else {
201     assert(ev_type(ev) == ET_EXPIRE);
202
203     pptr->lastsent = CurrentTime; /* store last ping time */
204     uping_send(pptr); /* send a ping */
205
206     if (pptr->sent == pptr->count) /* done sending pings, don't send more */
207       timer_del(ev_timer(ev));
208   }
209 }
210
211 /* Callback to kill a ping */
212 static void uping_killer_callback(struct Event* ev)
213 {
214   struct UPing *pptr;
215
216   assert(0 != ev_timer(ev));
217   assert(0 != t_data(ev_timer(ev)));
218
219   pptr = (struct UPing*) t_data(ev_timer(ev));
220
221   Debug((DEBUG_SEND, "uping_killer_callback called, %p (%d)", pptr,
222          ev_type(ev)));
223
224   if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
225     pptr->freeable &= ~UPING_PENDING_KILLER;
226
227     if (!pptr->freeable)
228       MyFree(pptr); /* done with it, finally */
229   } else {
230     assert(ev_type(ev) == ET_EXPIRE);
231
232     uping_end(pptr); /* <FUDD>kill the uping, kill the uping!</FUDD> */
233   }
234 }
235
236 /*
237  * start_ping
238  */
239 static void uping_start(struct UPing* pptr)
240 {
241   assert(0 != pptr);
242
243   timer_add(timer_init(&pptr->sender), uping_sender_callback, (void*) pptr,
244             TT_PERIODIC, 1);
245   timer_add(timer_init(&pptr->killer), uping_killer_callback, (void*) pptr,
246             TT_RELATIVE, UPINGTIMEOUT);
247   pptr->freeable |= UPING_PENDING_SENDER | UPING_PENDING_KILLER;
248
249   sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :Sending %d ping%s to %s",
250                 pptr->client, pptr->count, (pptr->count == 1) ? "" : "s",
251                 pptr->name);
252   pptr->active = 1;
253 }
254
255 /*
256  * uping_send
257  *
258  */
259 void uping_send(struct UPing* pptr)
260 {
261   struct timeval tv;
262   char buf[BUFSIZE + 1];
263
264   assert(0 != pptr);
265   if (pptr->sent == pptr->count)
266     return;
267   memset(buf, 0, sizeof(buf));
268
269   gettimeofday(&tv, NULL);
270   sprintf(buf, " %10lu%c%6lu", (unsigned long)tv.tv_sec, '\0', (unsigned long)tv.tv_usec);
271
272   Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d",
273           buf, &buf[12],
274           ircd_ntoa(&pptr->addr.addr), pptr->addr.port,
275           pptr->fd));
276
277   if (os_sendto_nonb(pptr->fd, buf, BUFSIZE, NULL, 0, &pptr->addr) != IO_SUCCESS)
278   {
279     const char* msg = strerror(errno);
280     if (!msg)
281       msg = "Unknown error";
282     if (pptr->client)
283       sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: send failed: "
284                     "%s", pptr->client, msg);
285     Debug((DEBUG_DEBUG, "UPING: send_ping: sendto failed on %d: %s", pptr->fd, msg));
286     uping_end(pptr);
287     return;
288   }
289   ++pptr->sent;
290 }
291
292 /*
293  * read_ping
294  */
295 void uping_read(struct UPing* pptr)
296 {
297   struct irc_sockaddr sin;
298   struct timeval     tv;
299   unsigned int       len;
300   unsigned int       pingtime;
301   char*              s;
302   char               buf[BUFSIZE + 1];
303   IOResult           ior;
304
305   assert(0 != pptr);
306
307   gettimeofday(&tv, NULL);
308
309   ior = os_recvfrom_nonb(pptr->fd, buf, BUFSIZE, &len, &sin);
310   if (IO_BLOCKED == ior)
311     return;
312   else if (IO_FAILURE == ior) {
313     const char* msg = strerror(errno);
314     if (!msg)
315       msg = "Unknown error";
316     sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: receive error: "
317                   "%s", pptr->client, msg);
318     uping_end(pptr);
319     return;
320   }
321
322   if (len < 19)
323     return;                     /* Broken packet */
324
325   ++pptr->received;
326
327   buf[len] = 0;
328   pingtime = (tv.tv_sec - atol(&buf[1])) * 1000
329              + (tv.tv_usec - atol(buf + strlen(buf) + 1)) / 1000;
330
331   pptr->ms_ave += pingtime;
332   if (!pptr->ms_min || pptr->ms_min > pingtime)
333     pptr->ms_min = pingtime;
334   if (pingtime > pptr->ms_max)
335     pptr->ms_max = pingtime;
336
337   timer_chg(&pptr->killer, TT_RELATIVE, UPINGTIMEOUT);
338
339   s = pptr->buf + strlen(pptr->buf);
340   sprintf(s, " %u", pingtime);
341
342   if (pptr->received == pptr->count)
343     uping_end(pptr);
344   return;
345 }
346
347 int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int count)
348 {
349   int fd;
350   struct UPing* pptr;
351
352   assert(0 != sptr);
353   assert(0 != aconf);
354
355   if (!irc_in_addr_valid(&aconf->address.addr)) {
356     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host lookup failed for "
357                   "%s", sptr, aconf->name);
358     return 0;
359   }
360
361   if (IsUPing(sptr))
362     uping_cancel(sptr, sptr);  /* Cancel previous ping request */
363
364   fd = os_socket(NULL, SOCK_DGRAM, "UDP ping socket");
365   if (fd < 0)
366     return 0;
367
368   pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
369   assert(0 != pptr);
370   memset(pptr, 0, sizeof(struct UPing));
371
372   if (!socket_add(&pptr->socket, uping_read_callback, (void*) pptr,
373                   SS_DATAGRAM, SOCK_EVENT_READABLE, fd)) {
374     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Can't queue fd for "
375                   "reading", sptr);
376     close(fd);
377     MyFree(pptr);
378     return 0;
379   }
380
381   pptr->fd                  = fd;
382   memcpy(&pptr->addr.addr, &aconf->address.addr, sizeof(pptr->addr.addr));
383   pptr->addr.port           = port;
384   pptr->count               = IRCD_MIN(20, count);
385   pptr->client              = sptr;
386   pptr->index               = -1;
387   pptr->freeable            = UPING_PENDING_SOCKET;
388   strcpy(pptr->name, aconf->name);
389
390   pptr->next = pingList;
391   pingList   = pptr;
392
393   SetUPing(sptr);
394   uping_start(pptr);
395   return 0;
396 }
397
398
399 void uping_end(struct UPing* pptr)
400 {
401   Debug((DEBUG_DEBUG, "uping_end: %p", pptr));
402
403   if (pptr->client) {
404     if (pptr->lastsent) {
405       if (0 < pptr->received) {
406         sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING %s%s",
407                       pptr->client, pptr->name, pptr->buf);
408         sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING Stats: "
409                       "sent %d recvd %d ; min/avg/max = %1lu/%1lu/%1lu ms",
410                       pptr->client, pptr->sent, pptr->received, pptr->ms_min,
411                       (2 * pptr->ms_ave) / (2 * pptr->received), pptr->ms_max);
412       } else
413         sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: no response "
414                       "from %s within %d seconds", pptr->client, pptr->name,
415                       UPINGTIMEOUT);
416     } else
417       sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: Could not "
418                     "start ping to %s", pptr->client, pptr->name);
419   }
420
421   close(pptr->fd);
422   pptr->fd = -1;
423   uping_erase(pptr);
424   if (pptr->client)
425     ClearUPing(pptr->client);
426   if (pptr->freeable & UPING_PENDING_SOCKET)
427     socket_del(&pptr->socket);
428   if (pptr->freeable & UPING_PENDING_SENDER)
429     timer_del(&pptr->sender);
430   if (pptr->freeable & UPING_PENDING_KILLER)
431     timer_del(&pptr->killer);
432 }
433
434 void uping_cancel(struct Client *sptr, struct Client* acptr)
435 {
436   struct UPing* ping;
437   struct UPing* ping_next;
438
439   Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", cli_name(sptr)));
440   for (ping = pingList; ping; ping = ping_next) {
441     ping_next = ping->next;
442     if (sptr == ping->client) {
443       ping->client = acptr;
444       uping_end(ping);
445     }
446   }
447   ClearUPing(sptr);
448 }
449
450