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     log_write(LS_SOCKET, L_ERROR, 0,
109               "UPING: set reuseaddr on UDP listener failed: %m (fd %d)", fd);
110     Debug((DEBUG_ERROR, "UPING: set reuseaddr on UDP listener failed: %s",
111            (strerror(errno)) ? strerror(errno) : "Unknown error"));
112     close(fd);
113     return -1;
114   }
115   if (bind(fd, (struct sockaddr*) &from, sizeof(from)) == -1) {
116     log_write(LS_SOCKET, L_ERROR, 0,
117               "UPING: bind on UDP listener (%d fd %d) failed: %m",
118               htons(from.sin_port), fd);
119     Debug((DEBUG_ERROR, "UPING: bind on UDP listener failed : %s",
120            (strerror(errno)) ? strerror(errno) : "Unknown error"));
121     close(fd);
122     return -1;
123   }
124   if (!os_set_nonblocking(fd)) {
125     Debug((DEBUG_ERROR, "UPING: set non-blocking: %s",
126            (strerror(errno)) ? strerror(errno) : "Unknown error"));
127     close(fd);
128     return -1;
129   }
130   UPingFileDescriptor = fd;
131   return fd;
132 }
133
134
135 /*
136  * max # of pings set to 15/sec.
137  */
138 void uping_echo()
139 {
140   struct sockaddr_in from = { 0 };
141   unsigned int       len = 0;
142   static time_t      last = 0;
143   static int         counter = 0;
144   char               buf[BUFSIZE + 1];
145
146   Debug((DEBUG_DEBUG, "UPING: uping_echo"));
147
148   if (IO_SUCCESS != os_recvfrom_nonb(UPingFileDescriptor, buf, BUFSIZE, &len, &from))
149     return;
150   /*
151    * count em even if we're getting flooded so we can tell we're getting
152    * flooded.
153    */
154   ++ServerStats->uping_recv;
155   if (CurrentTime == last) {
156     if (++counter > 10)
157       return;
158   }
159   else {
160     counter = 0;
161     last    = CurrentTime;
162   }
163   if (len < 19)
164     return;
165   sendto(UPingFileDescriptor, buf, len, 0, (struct sockaddr*) &from, sizeof(from));
166 }
167
168
169 /*
170  * start_ping
171  */
172 static void uping_start(struct UPing* pptr)
173 {
174   assert(0 != pptr);
175
176   sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :Sending %d ping%s to %s",
177                 pptr->client, pptr->count, (pptr->count == 1) ? "" : "s",
178                 pptr->name);
179   pptr->timeout = CurrentTime + UPINGTIMEOUT;
180   pptr->active = 1;
181 }
182
183 /*
184  * uping_send
185  *
186  */
187 void uping_send(struct UPing* pptr)
188 {
189   struct timeval tv;
190   char buf[BUFSIZE + 1];
191
192   assert(0 != pptr);
193   if (pptr->sent == pptr->count)
194     return;
195   memset(buf, 0, sizeof(buf));
196
197   gettimeofday(&tv, NULL);
198   sprintf(buf, " %10lu%c%6lu", tv.tv_sec, '\0', tv.tv_usec);
199
200   Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d",
201           buf, &buf[12],
202           ircd_ntoa((const char*) &pptr->sin.sin_addr), ntohs(pptr->sin.sin_port),
203           pptr->fd));
204
205   if (sendto(pptr->fd, buf, BUFSIZE, 0, (struct sockaddr*) &pptr->sin,
206              sizeof(struct sockaddr_in)) != BUFSIZE)
207   {
208     const char* msg = strerror(errno);
209     if (!msg)
210       msg = "Unknown error";
211     if (pptr->client)
212       sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: send failed: "
213                     "%s", pptr->client, msg);
214     Debug((DEBUG_DEBUG, "UPING: send_ping: sendto failed on %d: %s", pptr->fd, msg));
215     uping_end(pptr);
216     return;
217   }
218   ++pptr->sent;
219 }
220
221 /*
222  * read_ping
223  */
224 void uping_read(struct UPing* pptr)
225 {
226   struct sockaddr_in sin;
227   struct timeval     tv;
228   unsigned int       len;
229   unsigned int       pingtime;
230   char*              s;
231   char               buf[BUFSIZE + 1];
232   IOResult           ior;
233
234   assert(0 != pptr);
235
236   gettimeofday(&tv, NULL);
237
238   ior = os_recvfrom_nonb(pptr->fd, buf, BUFSIZE, &len, &sin);
239   if (IO_BLOCKED == ior)
240     return;
241   else if (IO_FAILURE == ior) {
242     const char* msg = strerror(errno);
243     if (!msg)
244       msg = "Unknown error";
245     sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: receive error: "
246                   "%s", pptr->client, msg);
247     uping_end(pptr);
248     return;
249   }    
250
251   if (len < 19)
252     return;                     /* Broken packet */
253    
254   ++pptr->received;
255
256   buf[len] = 0;
257   pingtime = (tv.tv_sec - atol(&buf[1])) * 1000
258              + (tv.tv_usec - atol(buf + strlen(buf) + 1)) / 1000;
259
260   pptr->ms_ave += pingtime;
261   if (!pptr->ms_min || pptr->ms_min > pingtime)
262     pptr->ms_min = pingtime;
263   if (pingtime > pptr->ms_max)
264     pptr->ms_max = pingtime;
265   
266   pptr->timeout = CurrentTime + UPINGTIMEOUT;
267
268   Debug((DEBUG_SEND, "read_ping: %d bytes, ti %lu: [%s %s] %lu ms",
269          len, pptr->timeout, buf, (buf + strlen(buf) + 1), pingtime));
270
271   s = pptr->buf + strlen(pptr->buf);
272   sprintf(s, " %u", pingtime);
273
274   if (pptr->received == pptr->count)
275     uping_end(pptr);
276   return;
277 }
278
279 int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int count)
280 {
281   int fd;
282   struct UPing* pptr;
283
284   assert(0 != sptr);
285   assert(0 != aconf);
286
287   if (INADDR_NONE == aconf->ipnum.s_addr) {
288     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host lookup failed for "
289                   "%s", sptr, aconf->name);
290     return 0;
291   }
292
293   if (IsUPing(sptr))
294     uping_cancel(sptr, sptr);  /* Cancel previous ping request */
295
296   if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
297     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Unable to create udp "
298                   "ping socket", sptr);
299     return 0;
300   }
301
302   if (!os_set_nonblocking(fd)) {
303     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Can't set fd non-"
304                   "blocking", sptr);
305     close(fd);
306     return 0;
307   }
308   pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
309   assert(0 != pptr);
310   memset(pptr, 0, sizeof(struct UPing));
311
312   pptr->fd                  = fd;
313   pptr->sin.sin_port        = htons(port);
314   pptr->sin.sin_addr.s_addr = aconf->ipnum.s_addr;
315   pptr->sin.sin_family      = AF_INET;
316   pptr->count               = IRCD_MIN(20, count);
317   pptr->client              = sptr;
318   pptr->index               = -1;
319   strcpy(pptr->name, aconf->name);
320
321   pptr->next = pingList;
322   pingList   = pptr;
323
324   SetUPing(sptr);
325   uping_start(pptr);
326   return 0;
327 }
328
329
330 void uping_end(struct UPing* pptr)
331 {
332   Debug((DEBUG_DEBUG, "uping_end: %p", pptr));
333
334   if (pptr->client) {
335     if (pptr->lastsent) {
336       if (0 < pptr->received) {
337         sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING %s%s",
338                       pptr->client, pptr->name, pptr->buf);
339         sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING Stats: "
340                       "sent %d recvd %d ; min/avg/max = %1lu/%1lu/%1lu ms",
341                       pptr->client, pptr->sent, pptr->received, pptr->ms_min,
342                       (2 * pptr->ms_ave) / (2 * pptr->received), pptr->ms_max);
343       } else
344         sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: no response "
345                       "from %s within %d seconds", pptr->client, pptr->name,
346                       UPINGTIMEOUT);
347     } else
348       sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: Could not "
349                     "start ping to %s", pptr->client, pptr->name);
350   }
351
352   close(pptr->fd);
353   pptr->fd = -1;
354   uping_erase(pptr);
355   if (pptr->client)
356     ClearUPing(pptr->client);
357   MyFree(pptr);
358 }
359
360 void uping_cancel(struct Client *sptr, struct Client* acptr)
361 {
362   struct UPing* ping;
363   struct UPing* ping_next;
364
365   Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", sptr->name));
366   for (ping = pingList; ping; ping = ping_next) {
367     ping_next = ping->next;
368     if (sptr == ping->client) {
369       ping->client = acptr;
370       uping_end(ping);
371     }
372   }
373   ClearUPing(sptr);
374 }
375
376