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.
20 * @brief UDP ping implementation.
28 #include "ircd_alloc.h"
29 #include "ircd_events.h"
31 #include "ircd_osdep.h"
32 #include "ircd_string.h"
37 #include "s_bsd.h" /* VirtualHost */
45 #include <arpa/inet.h>
46 /* #include <assert.h> -- Now using assert in ircd_log.h */
52 #include <sys/socket.h>
56 #define UPINGTIMEOUT 60 /**< Timeout waiting for ping responses */
58 static struct UPing* pingList = 0; /**< Linked list of UPing structs */
59 static int UPingFileDescriptor = -1; /**< UDP listener socket for upings */
60 static struct Socket upingSock; /**< Socket struct for upings */
62 /** Start iteration of uping list.
63 * @return Start of uping list.
65 struct UPing* uping_begin(void)
70 /** Removes \a p from uping list.
71 * @param[in,out] p UPing to remove from list.
73 static void uping_erase(struct UPing* p)
76 struct UPing* last = 0;
80 for (it = pingList; it; last = it, it = it->next) {
91 /** Callback for uping listener socket.
92 * @param[in] ev I/O event for uping socket.
94 static void uping_echo_callback(struct Event* ev)
96 assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR);
101 /** Initialize a UDP socket for upings.
102 * @returns File descriptor of UDP socket (-1 on error).
106 struct irc_sockaddr from;
109 memcpy(&from, &VirtualHost, sizeof(from));
110 from.port = atoi(UDP_PORT);
112 fd = os_socket(&from, SOCK_DGRAM, "UDP listener socket");
115 if (!socket_add(&upingSock, uping_echo_callback, 0, SS_DATAGRAM,
116 SOCK_EVENT_READABLE, fd)) {
117 Debug((DEBUG_ERROR, "UPING: Unable to queue fd to event system"));
121 UPingFileDescriptor = fd;
126 /** Read a uping from the socket and respond (but not more than 10
131 struct irc_sockaddr from;
132 unsigned int len = 0;
133 static time_t last = 0;
134 static int counter = 0;
135 char buf[BUFSIZE + 1];
137 Debug((DEBUG_DEBUG, "UPING: uping_echo"));
139 if (IO_SUCCESS != os_recvfrom_nonb(UPingFileDescriptor, buf, BUFSIZE, &len, &from))
142 * count em even if we're getting flooded so we can tell we're getting
145 ++ServerStats->uping_recv;
146 if (CurrentTime == last) {
156 os_sendto_nonb(UPingFileDescriptor, buf, len, NULL, 0, &from);
160 /** Callback for socket activity on an outbound uping socket.
161 * @param[in] ev I/O event for socket.
163 static void uping_read_callback(struct Event* ev)
167 assert(0 != ev_socket(ev));
168 assert(0 != s_data(ev_socket(ev)));
170 pptr = (struct UPing*) s_data(ev_socket(ev));
172 Debug((DEBUG_SEND, "uping_read_callback called, %p (%d)", pptr,
175 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
176 pptr->freeable &= ~UPING_PENDING_SOCKET;
179 MyFree(pptr); /* done with it, finally */
181 assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR);
183 uping_read(pptr); /* read uping response */
187 /** Timer callback to send another outbound uping.
188 * @param[in] ev Event for uping timer.
190 static void uping_sender_callback(struct Event* ev)
194 assert(0 != ev_timer(ev));
195 assert(0 != t_data(ev_timer(ev)));
197 pptr = (struct UPing*) t_data(ev_timer(ev));
199 Debug((DEBUG_SEND, "uping_sender_callback called, %p (%d)", pptr,
202 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
203 pptr->freeable &= ~UPING_PENDING_SENDER;
206 MyFree(pptr); /* done with it, finally */
208 assert(ev_type(ev) == ET_EXPIRE);
210 pptr->lastsent = CurrentTime; /* store last ping time */
211 uping_send(pptr); /* send a ping */
213 if (pptr->sent == pptr->count) /* done sending pings, don't send more */
214 timer_del(ev_timer(ev));
218 /** Timer callback to stop upings.
219 * @param[in] ev Event for uping expiration.
221 static void uping_killer_callback(struct Event* ev)
225 assert(0 != ev_timer(ev));
226 assert(0 != t_data(ev_timer(ev)));
228 pptr = (struct UPing*) t_data(ev_timer(ev));
230 Debug((DEBUG_SEND, "uping_killer_callback called, %p (%d)", pptr,
233 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
234 pptr->freeable &= ~UPING_PENDING_KILLER;
237 MyFree(pptr); /* done with it, finally */
239 assert(ev_type(ev) == ET_EXPIRE);
241 uping_end(pptr); /* <FUDD>kill the uping, kill the uping!</FUDD> */
246 * This sets up the timers, UPing flags, and sends a notice to the
249 static void uping_start(struct UPing* pptr)
253 timer_add(timer_init(&pptr->sender), uping_sender_callback, (void*) pptr,
255 timer_add(timer_init(&pptr->killer), uping_killer_callback, (void*) pptr,
256 TT_RELATIVE, UPINGTIMEOUT);
257 pptr->freeable |= UPING_PENDING_SENDER | UPING_PENDING_KILLER;
259 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :Sending %d ping%s to %s",
260 pptr->client, pptr->count, (pptr->count == 1) ? "" : "s",
265 /** Send a uping to another server.
266 * @param[in] pptr Descriptor for uping.
268 void uping_send(struct UPing* pptr)
271 char buf[BUFSIZE + 1];
274 if (pptr->sent == pptr->count)
276 memset(buf, 0, sizeof(buf));
278 gettimeofday(&tv, NULL);
279 sprintf(buf, " %10lu%c%6lu", (unsigned long)tv.tv_sec, '\0', (unsigned long)tv.tv_usec);
281 Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d",
283 ircd_ntoa(&pptr->addr.addr), pptr->addr.port,
286 if (os_sendto_nonb(pptr->fd, buf, BUFSIZE, NULL, 0, &pptr->addr) != IO_SUCCESS)
288 const char* msg = strerror(errno);
290 msg = "Unknown error";
292 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: send failed: "
293 "%s", pptr->client, msg);
294 Debug((DEBUG_DEBUG, "UPING: send_ping: sendto failed on %d: %s", pptr->fd, msg));
301 /** Read the response from an outbound uping.
302 * @param[in] pptr UPing to check.
304 void uping_read(struct UPing* pptr)
306 struct irc_sockaddr sin;
309 unsigned int pingtime;
311 char buf[BUFSIZE + 1];
316 gettimeofday(&tv, NULL);
318 ior = os_recvfrom_nonb(pptr->fd, buf, BUFSIZE, &len, &sin);
319 if (IO_BLOCKED == ior)
321 else if (IO_FAILURE == ior) {
322 const char* msg = strerror(errno);
324 msg = "Unknown error";
325 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: receive error: "
326 "%s", pptr->client, msg);
332 return; /* Broken packet */
337 pingtime = (tv.tv_sec - atol(&buf[1])) * 1000
338 + (tv.tv_usec - atol(buf + strlen(buf) + 1)) / 1000;
340 pptr->ms_ave += pingtime;
341 if (!pptr->ms_min || pptr->ms_min > pingtime)
342 pptr->ms_min = pingtime;
343 if (pingtime > pptr->ms_max)
344 pptr->ms_max = pingtime;
346 timer_chg(&pptr->killer, TT_RELATIVE, UPINGTIMEOUT);
348 s = pptr->buf + strlen(pptr->buf);
349 sprintf(s, " %u", pingtime);
351 if (pptr->received == pptr->count)
356 /** Start sending upings to a server.
357 * @param[in] sptr Client requesting the upings.
358 * @param[in] aconf ConfItem containing the address to ping.
359 * @param[in] port Port number to ping.
360 * @param[in] count Number of times to ping (should be at least 20).
363 int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int count)
371 if (!irc_in_addr_valid(&aconf->address.addr)) {
372 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host lookup failed for "
373 "%s", sptr, aconf->name);
378 uping_cancel(sptr, sptr); /* Cancel previous ping request */
380 fd = os_socket(NULL, SOCK_DGRAM, "UDP ping socket");
384 pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
386 memset(pptr, 0, sizeof(struct UPing));
388 if (!socket_add(&pptr->socket, uping_read_callback, (void*) pptr,
389 SS_DATAGRAM, SOCK_EVENT_READABLE, fd)) {
390 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Can't queue fd for "
398 memcpy(&pptr->addr.addr, &aconf->address.addr, sizeof(pptr->addr.addr));
399 pptr->addr.port = port;
400 pptr->count = IRCD_MIN(20, count);
402 pptr->freeable = UPING_PENDING_SOCKET;
403 strcpy(pptr->name, aconf->name);
405 pptr->next = pingList;
413 /** Clean up a UPing structure, reporting results to the requester.
414 * @param[in,out] pptr UPing results.
416 void uping_end(struct UPing* pptr)
418 Debug((DEBUG_DEBUG, "uping_end: %p", pptr));
421 if (pptr->lastsent) {
422 if (0 < pptr->received) {
423 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING %s%s",
424 pptr->client, pptr->name, pptr->buf);
425 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING Stats: "
426 "sent %d recvd %d ; min/avg/max = %1lu/%1lu/%1lu ms",
427 pptr->client, pptr->sent, pptr->received, pptr->ms_min,
428 (2 * pptr->ms_ave) / (2 * pptr->received), pptr->ms_max);
430 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: no response "
431 "from %s within %d seconds", pptr->client, pptr->name,
434 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: Could not "
435 "start ping to %s", pptr->client, pptr->name);
442 ClearUPing(pptr->client);
443 if (pptr->freeable & UPING_PENDING_SOCKET)
444 socket_del(&pptr->socket);
445 if (pptr->freeable & UPING_PENDING_SENDER)
446 timer_del(&pptr->sender);
447 if (pptr->freeable & UPING_PENDING_KILLER)
448 timer_del(&pptr->killer);
451 /** Change notifications for any upings by \a sptr.
452 * @param[in] sptr Client to stop notifying.
453 * @param[in] acptr New client to notify (or NULL).
455 void uping_cancel(struct Client *sptr, struct Client* acptr)
458 struct UPing* ping_next;
460 Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", cli_name(sptr)));
461 for (ping = pingList; ping; ping = ping_next) {
462 ping_next = ping->next;
463 if (sptr == ping->client) {
464 ping->client = acptr;