Author: Kev <klmitch@mit.edu>
[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"    /* VirtualHost */
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   sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :Sending %d ping%s to %s",
174                 pptr->client, pptr->count, (pptr->count == 1) ? "" : "s",
175                 pptr->name);
176
177   pptr->timeout = CurrentTime + UPINGTIMEOUT;
178   pptr->active = 1;
179 }
180
181 /*
182  * uping_send
183  *
184  */
185 void uping_send(struct UPing* pptr)
186 {
187   struct timeval tv;
188   char buf[BUFSIZE + 1];
189
190   assert(0 != pptr);
191   if (pptr->sent == pptr->count)
192     return;
193   memset(buf, 0, sizeof(buf));
194
195   gettimeofday(&tv, NULL);
196   sprintf(buf, " %10lu%c%6lu", tv.tv_sec, '\0', tv.tv_usec);
197
198   Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d",
199           buf, &buf[12],
200           ircd_ntoa((const char*) &pptr->sin.sin_addr), ntohs(pptr->sin.sin_port),
201           pptr->fd));
202
203   if (sendto(pptr->fd, buf, BUFSIZE, 0, (struct sockaddr*) &pptr->sin,
204              sizeof(struct sockaddr_in)) != BUFSIZE)
205   {
206     const char* msg = strerror(errno);
207     if (!msg)
208       msg = "Unknown error";
209     if (pptr->client)
210       sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: send failed: "
211                     "%s", pptr->client, msg);
212     Debug((DEBUG_DEBUG, "UPING: send_ping: sendto failed on %d: %s", pptr->fd, msg));
213     uping_end(pptr);
214     return;
215   }
216   ++pptr->sent;
217 }
218
219 /*
220  * read_ping
221  */
222 void uping_read(struct UPing* pptr)
223 {
224   struct sockaddr_in sin;
225   struct timeval     tv;
226   unsigned int       len;
227   unsigned int       pingtime;
228   char*              s;
229   char               buf[BUFSIZE + 1];
230   IOResult           ior;
231
232   assert(0 != pptr);
233
234   gettimeofday(&tv, NULL);
235
236   ior = os_recvfrom_nonb(pptr->fd, buf, BUFSIZE, &len, &sin);
237   if (IO_BLOCKED == ior)
238     return;
239   else if (IO_FAILURE == ior) {
240     const char* msg = strerror(errno);
241     if (!msg)
242       msg = "Unknown error";
243     sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: receive error: "
244                   "%s", pptr->client, msg);
245     uping_end(pptr);
246     return;
247   }    
248
249   if (len < 19)
250     return;                     /* Broken packet */
251    
252   ++pptr->received;
253
254   buf[len] = 0;
255   pingtime = (tv.tv_sec - atol(&buf[1])) * 1000
256              + (tv.tv_usec - atol(buf + strlen(buf) + 1)) / 1000;
257
258   pptr->ms_ave += pingtime;
259   if (!pptr->ms_min || pptr->ms_min > pingtime)
260     pptr->ms_min = pingtime;
261   if (pingtime > pptr->ms_max)
262     pptr->ms_max = pingtime;
263   
264   pptr->timeout = CurrentTime + UPINGTIMEOUT;
265
266   Debug((DEBUG_SEND, "read_ping: %d bytes, ti %lu: [%s %s] %lu ms",
267          len, pptr->timeout, buf, (buf + strlen(buf) + 1), pingtime));
268
269   s = pptr->buf + strlen(pptr->buf);
270   sprintf(s, " %u", pingtime);
271
272   if (pptr->received == pptr->count)
273     uping_end(pptr);
274   return;
275 }
276
277 int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int count)
278 {
279   int fd;
280   struct UPing* pptr;
281
282   assert(0 != sptr);
283   assert(0 != aconf);
284
285   if (INADDR_NONE == aconf->ipnum.s_addr) {
286     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host lookup failed for "
287                   "%s", sptr, aconf->name);
288     return 0;
289   }
290
291   if (IsUPing(sptr))
292     uping_cancel(sptr, sptr);  /* Cancel previous ping request */
293
294   if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
295     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Unable to create udp "
296                   "ping socket", sptr);
297     return 0;
298   }
299
300   if (!os_set_nonblocking(fd)) {
301     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Can't set fd non-"
302                   "blocking", sptr);
303     close(fd);
304     return 0;
305   }
306   pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
307   assert(0 != pptr);
308   memset(pptr, 0, sizeof(struct UPing));
309
310   pptr->fd                  = fd;
311   pptr->sin.sin_port        = htons(port);
312   pptr->sin.sin_addr.s_addr = aconf->ipnum.s_addr;
313   pptr->sin.sin_family      = AF_INET;
314   pptr->count               = IRCD_MIN(20, count);
315   pptr->client              = sptr;
316   pptr->index               = -1;
317   strcpy(pptr->name, aconf->name);
318
319   pptr->next = pingList;
320   pingList   = pptr;
321
322   SetUPing(sptr);
323   uping_start(pptr);
324   return 0;
325 }
326
327
328 void uping_end(struct UPing* pptr)
329 {
330   Debug((DEBUG_DEBUG, "uping_end: %p", pptr));
331
332   if (pptr->client) {
333     if (pptr->lastsent) {
334       if (0 < pptr->received) {
335         sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING %s%s",
336                       pptr->client, pptr->name, pptr->buf);
337         sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING Stats: "
338                       "sent %d recvd %d ; min/avg/max = %1lu/%1lu/%1lu ms",
339                       pptr->client, pptr->sent, pptr->received, pptr->ms_min,
340                       (2 * pptr->ms_ave) / (2 * pptr->received), pptr->ms_max);
341       } else
342         sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: no response "
343                       "from %s within %d seconds", pptr->client, pptr->name,
344                       UPINGTIMEOUT);
345     } else
346       sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: Could not "
347                     "start ping to %s", pptr->client, pptr->name);
348   }
349
350   close(pptr->fd);
351   pptr->fd = -1;
352   uping_erase(pptr);
353   if (pptr->client)
354     ClearUPing(pptr->client);
355   MyFree(pptr);
356 }
357
358 void uping_cancel(struct Client *sptr, struct Client* acptr)
359 {
360   struct UPing* ping;
361   struct UPing* ping_next;
362
363   Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", sptr->name));
364   for (ping = pingList; ping; ping = ping_next) {
365     ping_next = ping->next;
366     if (sptr == ping->client) {
367       ping->client = acptr;
368       uping_end(ping);
369     }
370   }
371   ClearUPing(sptr);
372 }
373
374