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; /**< Linked list of UPing structs */
57 static int UPingFileDescriptor = -1; /**< UDP listener socket for upings */
58 static struct Socket upingSock; /**< Socket struct for upings */
60 /** Start iteration of uping list.
61 * @return Start of uping list.
63 struct UPing* uping_begin(void)
68 /** Removes \a p from uping list.
69 * @param[in,out] p UPing to remove from list.
71 static void uping_erase(struct UPing* p)
74 struct UPing* last = 0;
78 for (it = pingList; it; last = it, it = it->next) {
89 /** Callback for uping listener socket.
90 * @param[in] ev I/O event for uping socket.
92 static void uping_echo_callback(struct Event* ev)
94 assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR);
99 /** Initialize a UDP socket for upings.
100 * @returns File descriptor of UDP socket (-1 on error).
104 struct irc_sockaddr from;
107 memcpy(&from, &VirtualHost, sizeof(from));
108 from.port = atoi(UDP_PORT);
110 fd = os_socket(&from, SOCK_DGRAM, "UDP listener socket");
113 if (!socket_add(&upingSock, uping_echo_callback, 0, SS_DATAGRAM,
114 SOCK_EVENT_READABLE, fd)) {
115 Debug((DEBUG_ERROR, "UPING: Unable to queue fd to event system"));
119 UPingFileDescriptor = fd;
124 /** Read a uping from the socket and respond (but not more than 10
129 struct irc_sockaddr from;
130 unsigned int len = 0;
131 static time_t last = 0;
132 static int counter = 0;
133 char buf[BUFSIZE + 1];
135 Debug((DEBUG_DEBUG, "UPING: uping_echo"));
137 if (IO_SUCCESS != os_recvfrom_nonb(UPingFileDescriptor, buf, BUFSIZE, &len, &from))
140 * count em even if we're getting flooded so we can tell we're getting
143 ++ServerStats->uping_recv;
144 if (CurrentTime == last) {
154 os_sendto_nonb(UPingFileDescriptor, buf, len, NULL, 0, &from);
158 /** Callback for socket activity on an outbound uping socket.
159 * @param[in] ev I/O event for socket.
161 static void uping_read_callback(struct Event* ev)
165 assert(0 != ev_socket(ev));
166 assert(0 != s_data(ev_socket(ev)));
168 pptr = (struct UPing*) s_data(ev_socket(ev));
170 Debug((DEBUG_SEND, "uping_read_callback called, %p (%d)", pptr,
173 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
174 pptr->freeable &= ~UPING_PENDING_SOCKET;
177 MyFree(pptr); /* done with it, finally */
179 assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR);
181 uping_read(pptr); /* read uping response */
185 /** Timer callback to send another outbound uping.
186 * @param[in] ev Event for uping timer.
188 static void uping_sender_callback(struct Event* ev)
192 assert(0 != ev_timer(ev));
193 assert(0 != t_data(ev_timer(ev)));
195 pptr = (struct UPing*) t_data(ev_timer(ev));
197 Debug((DEBUG_SEND, "uping_sender_callback called, %p (%d)", pptr,
200 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
201 pptr->freeable &= ~UPING_PENDING_SENDER;
204 MyFree(pptr); /* done with it, finally */
206 assert(ev_type(ev) == ET_EXPIRE);
208 pptr->lastsent = CurrentTime; /* store last ping time */
209 uping_send(pptr); /* send a ping */
211 if (pptr->sent == pptr->count) /* done sending pings, don't send more */
212 timer_del(ev_timer(ev));
216 /** Timer callback to stop upings.
217 * @param[in] ev Event for uping expiration.
219 static void uping_killer_callback(struct Event* ev)
223 assert(0 != ev_timer(ev));
224 assert(0 != t_data(ev_timer(ev)));
226 pptr = (struct UPing*) t_data(ev_timer(ev));
228 Debug((DEBUG_SEND, "uping_killer_callback called, %p (%d)", pptr,
231 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
232 pptr->freeable &= ~UPING_PENDING_KILLER;
235 MyFree(pptr); /* done with it, finally */
237 assert(ev_type(ev) == ET_EXPIRE);
239 uping_end(pptr); /* <FUDD>kill the uping, kill the uping!</FUDD> */
244 * This sets up the timers, UPing flags, and sends a notice to the
247 static void uping_start(struct UPing* pptr)
251 timer_add(timer_init(&pptr->sender), uping_sender_callback, (void*) pptr,
253 timer_add(timer_init(&pptr->killer), uping_killer_callback, (void*) pptr,
254 TT_RELATIVE, UPINGTIMEOUT);
255 pptr->freeable |= UPING_PENDING_SENDER | UPING_PENDING_KILLER;
257 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :Sending %d ping%s to %s",
258 pptr->client, pptr->count, (pptr->count == 1) ? "" : "s",
263 /** Send a uping to another server.
264 * @param[in] pptr Descriptor for uping.
266 void uping_send(struct UPing* pptr)
269 char buf[BUFSIZE + 1];
272 if (pptr->sent == pptr->count)
274 memset(buf, 0, sizeof(buf));
276 gettimeofday(&tv, NULL);
277 sprintf(buf, " %10lu%c%6lu", (unsigned long)tv.tv_sec, '\0', (unsigned long)tv.tv_usec);
279 Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d",
281 ircd_ntoa(&pptr->addr.addr), pptr->addr.port,
284 if (os_sendto_nonb(pptr->fd, buf, BUFSIZE, NULL, 0, &pptr->addr) != IO_SUCCESS)
286 const char* msg = strerror(errno);
288 msg = "Unknown error";
290 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: send failed: "
291 "%s", pptr->client, msg);
292 Debug((DEBUG_DEBUG, "UPING: send_ping: sendto failed on %d: %s", pptr->fd, msg));
299 /** Read the response from an outbound uping.
300 * @param[in] pptr UPing to check.
302 void uping_read(struct UPing* pptr)
304 struct irc_sockaddr sin;
307 unsigned int pingtime;
309 char buf[BUFSIZE + 1];
314 gettimeofday(&tv, NULL);
316 ior = os_recvfrom_nonb(pptr->fd, buf, BUFSIZE, &len, &sin);
317 if (IO_BLOCKED == ior)
319 else if (IO_FAILURE == ior) {
320 const char* msg = strerror(errno);
322 msg = "Unknown error";
323 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: receive error: "
324 "%s", pptr->client, msg);
330 return; /* Broken packet */
335 pingtime = (tv.tv_sec - atol(&buf[1])) * 1000
336 + (tv.tv_usec - atol(buf + strlen(buf) + 1)) / 1000;
338 pptr->ms_ave += pingtime;
339 if (!pptr->ms_min || pptr->ms_min > pingtime)
340 pptr->ms_min = pingtime;
341 if (pingtime > pptr->ms_max)
342 pptr->ms_max = pingtime;
344 timer_chg(&pptr->killer, TT_RELATIVE, UPINGTIMEOUT);
346 s = pptr->buf + strlen(pptr->buf);
347 sprintf(s, " %u", pingtime);
349 if (pptr->received == pptr->count)
354 /** Start sending upings to a server.
355 * @param[in] sptr Client requesting the upings.
356 * @param[in] aconf ConfItem containing the address to ping.
357 * @param[in] port Port number to ping.
358 * @param[in] count Number of times to ping (should be at least 20).
361 int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int count)
369 if (!irc_in_addr_valid(&aconf->address.addr)) {
370 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host lookup failed for "
371 "%s", sptr, aconf->name);
376 uping_cancel(sptr, sptr); /* Cancel previous ping request */
378 fd = os_socket(NULL, SOCK_DGRAM, "UDP ping socket");
382 pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
384 memset(pptr, 0, sizeof(struct UPing));
386 if (!socket_add(&pptr->socket, uping_read_callback, (void*) pptr,
387 SS_DATAGRAM, SOCK_EVENT_READABLE, fd)) {
388 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Can't queue fd for "
396 memcpy(&pptr->addr.addr, &aconf->address.addr, sizeof(pptr->addr.addr));
397 pptr->addr.port = port;
398 pptr->count = IRCD_MIN(20, count);
400 pptr->freeable = UPING_PENDING_SOCKET;
401 strcpy(pptr->name, aconf->name);
403 pptr->next = pingList;
411 /** Clean up a UPing structure, reporting results to the requester.
412 * @param[in,out] pptr UPing results.
414 void uping_end(struct UPing* pptr)
416 Debug((DEBUG_DEBUG, "uping_end: %p", pptr));
419 if (pptr->lastsent) {
420 if (0 < pptr->received) {
421 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING %s%s",
422 pptr->client, pptr->name, pptr->buf);
423 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING Stats: "
424 "sent %d recvd %d ; min/avg/max = %1lu/%1lu/%1lu ms",
425 pptr->client, pptr->sent, pptr->received, pptr->ms_min,
426 (2 * pptr->ms_ave) / (2 * pptr->received), pptr->ms_max);
428 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: no response "
429 "from %s within %d seconds", pptr->client, pptr->name,
432 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: Could not "
433 "start ping to %s", pptr->client, pptr->name);
440 ClearUPing(pptr->client);
441 if (pptr->freeable & UPING_PENDING_SOCKET)
442 socket_del(&pptr->socket);
443 if (pptr->freeable & UPING_PENDING_SENDER)
444 timer_del(&pptr->sender);
445 if (pptr->freeable & UPING_PENDING_KILLER)
446 timer_del(&pptr->killer);
449 /** Change notifications for any upings by \a sptr.
450 * @param[in] sptr Client to stop notifying.
451 * @param[in] acptr New client to notify (or NULL).
453 void uping_cancel(struct Client *sptr, struct Client* acptr)
456 struct UPing* ping_next;
458 Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", cli_name(sptr)));
459 for (ping = pingList; ping; ping = ping_next) {
460 ping_next = ping->next;
461 if (sptr == ping->client) {
462 ping->client = acptr;