2 * IRC - Internet Relay Chat, ircd/uping.c
3 * Copyright (C) 1994 Carlo Wood ( Run @ undernet.org )
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)
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.
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.
26 #include "ircd_alloc.h"
27 #include "ircd_events.h"
29 #include "ircd_osdep.h"
30 #include "ircd_string.h"
35 #include "s_bsd.h" /* VirtualHost */
43 #include <arpa/inet.h>
50 #include <sys/socket.h>
54 #define UPINGTIMEOUT 60 /* Timeout waiting for ping responses */
56 static struct UPing* pingList = 0;
57 int UPingFileDescriptor = -1; /* UDP listener socket for upings */
59 static struct Socket upingSock;
62 * pings_begin - iterator function for ping list
64 struct UPing* uping_begin(void)
70 * pings_erase - removes ping struct from ping list
72 static void uping_erase(struct UPing* p)
75 struct UPing* last = 0;
79 for (it = pingList; it; last = it, it = it->next) {
90 /* Called when the event engine detects activity on the UPing socket */
91 static void uping_echo_callback(struct Event* ev)
93 assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR);
99 * Setup a UDP socket and listen for incoming packets
103 struct irc_sockaddr from;
106 memcpy(&from, &VirtualHost, sizeof(from));
107 from.port = atoi(UDP_PORT);
109 fd = os_socket(&from, SOCK_DGRAM, "UDP listener socket");
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"));
118 UPingFileDescriptor = fd;
124 * max # of pings set to 15/sec.
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];
134 Debug((DEBUG_DEBUG, "UPING: uping_echo"));
136 if (IO_SUCCESS != os_recvfrom_nonb(UPingFileDescriptor, buf, BUFSIZE, &len, &from))
139 * count em even if we're getting flooded so we can tell we're getting
142 ++ServerStats->uping_recv;
143 if (CurrentTime == last) {
153 os_sendto_nonb(UPingFileDescriptor, buf, len, NULL, 0, &from);
157 /* Callback when socket has data to read */
158 static void uping_read_callback(struct Event* ev)
162 assert(0 != ev_socket(ev));
163 assert(0 != s_data(ev_socket(ev)));
165 pptr = (struct UPing*) s_data(ev_socket(ev));
167 Debug((DEBUG_SEND, "uping_read_callback called, %p (%d)", pptr,
170 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
171 pptr->freeable &= ~UPING_PENDING_SOCKET;
174 MyFree(pptr); /* done with it, finally */
176 assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR);
178 uping_read(pptr); /* read uping response */
182 /* Callback to send another ping */
183 static void uping_sender_callback(struct Event* ev)
187 assert(0 != ev_timer(ev));
188 assert(0 != t_data(ev_timer(ev)));
190 pptr = (struct UPing*) t_data(ev_timer(ev));
192 Debug((DEBUG_SEND, "uping_sender_callback called, %p (%d)", pptr,
195 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
196 pptr->freeable &= ~UPING_PENDING_SENDER;
199 MyFree(pptr); /* done with it, finally */
201 assert(ev_type(ev) == ET_EXPIRE);
203 pptr->lastsent = CurrentTime; /* store last ping time */
204 uping_send(pptr); /* send a ping */
206 if (pptr->sent == pptr->count) /* done sending pings, don't send more */
207 timer_del(ev_timer(ev));
211 /* Callback to kill a ping */
212 static void uping_killer_callback(struct Event* ev)
216 assert(0 != ev_timer(ev));
217 assert(0 != t_data(ev_timer(ev)));
219 pptr = (struct UPing*) t_data(ev_timer(ev));
221 Debug((DEBUG_SEND, "uping_killer_callback called, %p (%d)", pptr,
224 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
225 pptr->freeable &= ~UPING_PENDING_KILLER;
228 MyFree(pptr); /* done with it, finally */
230 assert(ev_type(ev) == ET_EXPIRE);
232 uping_end(pptr); /* <FUDD>kill the uping, kill the uping!</FUDD> */
239 static void uping_start(struct UPing* pptr)
243 timer_add(timer_init(&pptr->sender), uping_sender_callback, (void*) pptr,
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;
249 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :Sending %d ping%s to %s",
250 pptr->client, pptr->count, (pptr->count == 1) ? "" : "s",
259 void uping_send(struct UPing* pptr)
262 char buf[BUFSIZE + 1];
265 if (pptr->sent == pptr->count)
267 memset(buf, 0, sizeof(buf));
269 gettimeofday(&tv, NULL);
270 sprintf(buf, " %10lu%c%6lu", (unsigned long)tv.tv_sec, '\0', (unsigned long)tv.tv_usec);
272 Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d",
274 ircd_ntoa(&pptr->addr.addr), pptr->addr.port,
277 if (os_sendto_nonb(pptr->fd, buf, BUFSIZE, NULL, 0, &pptr->addr) != IO_SUCCESS)
279 const char* msg = strerror(errno);
281 msg = "Unknown error";
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));
295 void uping_read(struct UPing* pptr)
297 struct irc_sockaddr sin;
300 unsigned int pingtime;
302 char buf[BUFSIZE + 1];
307 gettimeofday(&tv, NULL);
309 ior = os_recvfrom_nonb(pptr->fd, buf, BUFSIZE, &len, &sin);
310 if (IO_BLOCKED == ior)
312 else if (IO_FAILURE == ior) {
313 const char* msg = strerror(errno);
315 msg = "Unknown error";
316 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: receive error: "
317 "%s", pptr->client, msg);
323 return; /* Broken packet */
328 pingtime = (tv.tv_sec - atol(&buf[1])) * 1000
329 + (tv.tv_usec - atol(buf + strlen(buf) + 1)) / 1000;
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;
337 timer_chg(&pptr->killer, TT_RELATIVE, UPINGTIMEOUT);
339 s = pptr->buf + strlen(pptr->buf);
340 sprintf(s, " %u", pingtime);
342 if (pptr->received == pptr->count)
347 int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int count)
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);
362 uping_cancel(sptr, sptr); /* Cancel previous ping request */
364 fd = os_socket(NULL, SOCK_DGRAM, "UDP ping socket");
368 pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
370 memset(pptr, 0, sizeof(struct UPing));
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 "
382 memcpy(&pptr->addr.addr, &aconf->address.addr, sizeof(pptr->addr.addr));
383 pptr->addr.port = port;
384 pptr->count = IRCD_MIN(20, count);
387 pptr->freeable = UPING_PENDING_SOCKET;
388 strcpy(pptr->name, aconf->name);
390 pptr->next = pingList;
399 void uping_end(struct UPing* pptr)
401 Debug((DEBUG_DEBUG, "uping_end: %p", pptr));
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);
413 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: no response "
414 "from %s within %d seconds", pptr->client, pptr->name,
417 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: Could not "
418 "start ping to %s", pptr->client, pptr->name);
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);
434 void uping_cancel(struct Client *sptr, struct Client* acptr)
437 struct UPing* ping_next;
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;