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 <assert.h> -- Now using assert in ircd_log.h */
51 #include <sys/socket.h>
55 #define UPINGTIMEOUT 60 /**< Timeout waiting for ping responses */
57 static struct UPing* pingList = 0; /**< Linked list of UPing structs */
58 static int UPingFileDescriptor = -1; /**< UDP listener socket for upings */
59 static struct Socket upingSock; /**< Socket struct for upings */
61 /** Start iteration of uping list.
62 * @return Start of uping list.
64 struct UPing* uping_begin(void)
69 /** Removes \a p from uping list.
70 * @param[in,out] p UPing to remove from 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 /** Callback for uping listener socket.
91 * @param[in] ev I/O event for uping socket.
93 static void uping_echo_callback(struct Event* ev)
95 assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR);
100 /** Initialize a UDP socket for upings.
101 * @returns File descriptor of UDP socket (-1 on error).
105 struct irc_sockaddr from;
108 memcpy(&from, &VirtualHost, sizeof(from));
109 from.port = atoi(UDP_PORT);
111 fd = os_socket(&from, SOCK_DGRAM, "UDP listener socket");
114 if (!socket_add(&upingSock, uping_echo_callback, 0, SS_DATAGRAM,
115 SOCK_EVENT_READABLE, fd)) {
116 Debug((DEBUG_ERROR, "UPING: Unable to queue fd to event system"));
120 UPingFileDescriptor = fd;
125 /** Read a uping from the socket and respond (but not more than 10
130 struct irc_sockaddr from;
131 unsigned int len = 0;
132 static time_t last = 0;
133 static int counter = 0;
134 char buf[BUFSIZE + 1];
136 Debug((DEBUG_DEBUG, "UPING: uping_echo"));
138 if (IO_SUCCESS != os_recvfrom_nonb(UPingFileDescriptor, buf, BUFSIZE, &len, &from))
141 * count em even if we're getting flooded so we can tell we're getting
144 ++ServerStats->uping_recv;
145 if (CurrentTime == last) {
155 os_sendto_nonb(UPingFileDescriptor, buf, len, NULL, 0, &from);
159 /** Callback for socket activity on an outbound uping socket.
160 * @param[in] ev I/O event for socket.
162 static void uping_read_callback(struct Event* ev)
166 assert(0 != ev_socket(ev));
167 assert(0 != s_data(ev_socket(ev)));
169 pptr = (struct UPing*) s_data(ev_socket(ev));
171 Debug((DEBUG_SEND, "uping_read_callback called, %p (%d)", pptr,
174 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
175 pptr->freeable &= ~UPING_PENDING_SOCKET;
178 MyFree(pptr); /* done with it, finally */
180 assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR);
182 uping_read(pptr); /* read uping response */
186 /** Timer callback to send another outbound uping.
187 * @param[in] ev Event for uping timer.
189 static void uping_sender_callback(struct Event* ev)
193 assert(0 != ev_timer(ev));
194 assert(0 != t_data(ev_timer(ev)));
196 pptr = (struct UPing*) t_data(ev_timer(ev));
198 Debug((DEBUG_SEND, "uping_sender_callback called, %p (%d)", pptr,
201 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
202 pptr->freeable &= ~UPING_PENDING_SENDER;
205 MyFree(pptr); /* done with it, finally */
207 assert(ev_type(ev) == ET_EXPIRE);
209 pptr->lastsent = CurrentTime; /* store last ping time */
210 uping_send(pptr); /* send a ping */
212 if (pptr->sent == pptr->count) /* done sending pings, don't send more */
213 timer_del(ev_timer(ev));
217 /** Timer callback to stop upings.
218 * @param[in] ev Event for uping expiration.
220 static void uping_killer_callback(struct Event* ev)
224 assert(0 != ev_timer(ev));
225 assert(0 != t_data(ev_timer(ev)));
227 pptr = (struct UPing*) t_data(ev_timer(ev));
229 Debug((DEBUG_SEND, "uping_killer_callback called, %p (%d)", pptr,
232 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
233 pptr->freeable &= ~UPING_PENDING_KILLER;
236 MyFree(pptr); /* done with it, finally */
238 assert(ev_type(ev) == ET_EXPIRE);
240 uping_end(pptr); /* <FUDD>kill the uping, kill the uping!</FUDD> */
245 * This sets up the timers, UPing flags, and sends a notice to the
248 static void uping_start(struct UPing* pptr)
252 timer_add(timer_init(&pptr->sender), uping_sender_callback, (void*) pptr,
254 timer_add(timer_init(&pptr->killer), uping_killer_callback, (void*) pptr,
255 TT_RELATIVE, UPINGTIMEOUT);
256 pptr->freeable |= UPING_PENDING_SENDER | UPING_PENDING_KILLER;
258 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :Sending %d ping%s to %s",
259 pptr->client, pptr->count, (pptr->count == 1) ? "" : "s",
264 /** Send a uping to another server.
265 * @param[in] pptr Descriptor for uping.
267 void uping_send(struct UPing* pptr)
270 char buf[BUFSIZE + 1];
273 if (pptr->sent == pptr->count)
275 memset(buf, 0, sizeof(buf));
277 gettimeofday(&tv, NULL);
278 sprintf(buf, " %10lu%c%6lu", (unsigned long)tv.tv_sec, '\0', (unsigned long)tv.tv_usec);
280 Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d",
282 ircd_ntoa(&pptr->addr.addr), pptr->addr.port,
285 if (os_sendto_nonb(pptr->fd, buf, BUFSIZE, NULL, 0, &pptr->addr) != IO_SUCCESS)
287 const char* msg = strerror(errno);
289 msg = "Unknown error";
291 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: send failed: "
292 "%s", pptr->client, msg);
293 Debug((DEBUG_DEBUG, "UPING: send_ping: sendto failed on %d: %s", pptr->fd, msg));
300 /** Read the response from an outbound uping.
301 * @param[in] pptr UPing to check.
303 void uping_read(struct UPing* pptr)
305 struct irc_sockaddr sin;
308 unsigned int pingtime;
310 char buf[BUFSIZE + 1];
315 gettimeofday(&tv, NULL);
317 ior = os_recvfrom_nonb(pptr->fd, buf, BUFSIZE, &len, &sin);
318 if (IO_BLOCKED == ior)
320 else if (IO_FAILURE == ior) {
321 const char* msg = strerror(errno);
323 msg = "Unknown error";
324 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: receive error: "
325 "%s", pptr->client, msg);
331 return; /* Broken packet */
336 pingtime = (tv.tv_sec - atol(&buf[1])) * 1000
337 + (tv.tv_usec - atol(buf + strlen(buf) + 1)) / 1000;
339 pptr->ms_ave += pingtime;
340 if (!pptr->ms_min || pptr->ms_min > pingtime)
341 pptr->ms_min = pingtime;
342 if (pingtime > pptr->ms_max)
343 pptr->ms_max = pingtime;
345 timer_chg(&pptr->killer, TT_RELATIVE, UPINGTIMEOUT);
347 s = pptr->buf + strlen(pptr->buf);
348 sprintf(s, " %u", pingtime);
350 if (pptr->received == pptr->count)
355 /** Start sending upings to a server.
356 * @param[in] sptr Client requesting the upings.
357 * @param[in] aconf ConfItem containing the address to ping.
358 * @param[in] port Port number to ping.
359 * @param[in] count Number of times to ping (should be at least 20).
362 int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int count)
370 if (!irc_in_addr_valid(&aconf->address.addr)) {
371 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host lookup failed for "
372 "%s", sptr, aconf->name);
377 uping_cancel(sptr, sptr); /* Cancel previous ping request */
379 fd = os_socket(NULL, SOCK_DGRAM, "UDP ping socket");
383 pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
385 memset(pptr, 0, sizeof(struct UPing));
387 if (!socket_add(&pptr->socket, uping_read_callback, (void*) pptr,
388 SS_DATAGRAM, SOCK_EVENT_READABLE, fd)) {
389 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Can't queue fd for "
397 memcpy(&pptr->addr.addr, &aconf->address.addr, sizeof(pptr->addr.addr));
398 pptr->addr.port = port;
399 pptr->count = IRCD_MIN(20, count);
401 pptr->freeable = UPING_PENDING_SOCKET;
402 strcpy(pptr->name, aconf->name);
404 pptr->next = pingList;
412 /** Clean up a UPing structure, reporting results to the requester.
413 * @param[in,out] pptr UPing results.
415 void uping_end(struct UPing* pptr)
417 Debug((DEBUG_DEBUG, "uping_end: %p", pptr));
420 if (pptr->lastsent) {
421 if (0 < pptr->received) {
422 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING %s%s",
423 pptr->client, pptr->name, pptr->buf);
424 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING Stats: "
425 "sent %d recvd %d ; min/avg/max = %1lu/%1lu/%1lu ms",
426 pptr->client, pptr->sent, pptr->received, pptr->ms_min,
427 (2 * pptr->ms_ave) / (2 * pptr->received), pptr->ms_max);
429 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: no response "
430 "from %s within %d seconds", pptr->client, pptr->name,
433 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: Could not "
434 "start ping to %s", pptr->client, pptr->name);
441 ClearUPing(pptr->client);
442 if (pptr->freeable & UPING_PENDING_SOCKET)
443 socket_del(&pptr->socket);
444 if (pptr->freeable & UPING_PENDING_SENDER)
445 timer_del(&pptr->sender);
446 if (pptr->freeable & UPING_PENDING_KILLER)
447 timer_del(&pptr->killer);
450 /** Change notifications for any upings by \a sptr.
451 * @param[in] sptr Client to stop notifying.
452 * @param[in] acptr New client to notify (or NULL).
454 void uping_cancel(struct Client *sptr, struct Client* acptr)
457 struct UPing* ping_next;
459 Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", cli_name(sptr)));
460 for (ping = pingList; ping; ping = ping_next) {
461 ping_next = ping->next;
462 if (sptr == ping->client) {
463 ping->client = acptr;