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.
24 #include "ircd_alloc.h"
26 #include "ircd_osdep.h"
27 #include "ircd_string.h"
32 #include "s_bsd.h" /* VirtualHost */
40 #include <arpa/inet.h>
47 #include <sys/socket.h>
51 #define UPINGTIMEOUT 60 /* Timeout waiting for ping responses */
54 #define INADDR_NONE 0xffffffff
57 static struct UPing* pingList = 0;
58 int UPingFileDescriptor = -1; /* UDP listener socket for upings */
61 * pings_begin - iterator function for ping list
63 struct UPing* uping_begin(void)
69 * pings_erase - removes ping struct from ping list
71 static void uping_erase(struct UPing* p)
74 struct UPing* last = 0;
78 for (it = pingList; it; last = it, it = it->next) {
90 * Setup a UDP socket and listen for incoming packets
94 struct sockaddr_in from = { 0 };
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;
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"));
107 if (!os_set_reuseaddr(fd)) {
108 ircd_log(L_ERROR, "UPING: setsockopt UDP listener: fd %d", fd);
109 Debug((DEBUG_ERROR, "UPING: set reuseaddr on UDP listener failed: %s",
110 (strerror(errno)) ? strerror(errno) : "Unknown error"));
114 if (bind(fd, (struct sockaddr*) &from, sizeof(from)) == -1) {
115 ircd_log(L_ERROR, "UPING: bind UDP listener %d fd %d", htons(from.sin_port), fd);
116 Debug((DEBUG_ERROR, "UPING: bind on UDP listener failed : %s",
117 (strerror(errno)) ? strerror(errno) : "Unknown error"));
121 if (!os_set_nonblocking(fd)) {
122 Debug((DEBUG_ERROR, "UPING: set non-blocking: %s",
123 (strerror(errno)) ? strerror(errno) : "Unknown error"));
127 UPingFileDescriptor = fd;
133 * max # of pings set to 15/sec.
137 struct sockaddr_in from = { 0 };
138 unsigned int len = 0;
139 static time_t last = 0;
140 static int counter = 0;
141 char buf[BUFSIZE + 1];
143 Debug((DEBUG_DEBUG, "UPING: uping_echo"));
145 if (IO_SUCCESS != os_recvfrom_nonb(UPingFileDescriptor, buf, BUFSIZE, &len, &from))
148 * count em even if we're getting flooded so we can tell we're getting
151 ++ServerStats->uping_recv;
152 if (CurrentTime == last) {
162 sendto(UPingFileDescriptor, buf, len, 0, (struct sockaddr*) &from, sizeof(from));
169 static void uping_start(struct UPing* pptr)
173 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :Sending %d ping%s to %s",
174 pptr->client, pptr->count, (pptr->count == 1) ? "" : "s",
176 pptr->timeout = CurrentTime + UPINGTIMEOUT;
184 void uping_send(struct UPing* pptr)
187 char buf[BUFSIZE + 1];
190 if (pptr->sent == pptr->count)
192 memset(buf, 0, sizeof(buf));
194 gettimeofday(&tv, NULL);
195 sprintf(buf, " %10lu%c%6lu", tv.tv_sec, '\0', tv.tv_usec);
197 Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d",
199 ircd_ntoa((const char*) &pptr->sin.sin_addr), ntohs(pptr->sin.sin_port),
202 if (sendto(pptr->fd, buf, BUFSIZE, 0, (struct sockaddr*) &pptr->sin,
203 sizeof(struct sockaddr_in)) != BUFSIZE)
205 const char* msg = strerror(errno);
207 msg = "Unknown error";
209 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: send failed: "
210 "%s", pptr->client, msg);
211 Debug((DEBUG_DEBUG, "UPING: send_ping: sendto failed on %d: %s", pptr->fd, msg));
221 void uping_read(struct UPing* pptr)
223 struct sockaddr_in sin;
226 unsigned int pingtime;
228 char buf[BUFSIZE + 1];
233 gettimeofday(&tv, NULL);
235 ior = os_recvfrom_nonb(pptr->fd, buf, BUFSIZE, &len, &sin);
236 if (IO_BLOCKED == ior)
238 else if (IO_FAILURE == ior) {
239 const char* msg = strerror(errno);
241 msg = "Unknown error";
242 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: receive error: "
243 "%s", pptr->client, msg);
249 return; /* Broken packet */
254 pingtime = (tv.tv_sec - atol(&buf[1])) * 1000
255 + (tv.tv_usec - atol(buf + strlen(buf) + 1)) / 1000;
257 pptr->ms_ave += pingtime;
258 if (!pptr->ms_min || pptr->ms_min > pingtime)
259 pptr->ms_min = pingtime;
260 if (pingtime > pptr->ms_max)
261 pptr->ms_max = pingtime;
263 pptr->timeout = CurrentTime + UPINGTIMEOUT;
265 Debug((DEBUG_SEND, "read_ping: %d bytes, ti %lu: [%s %s] %lu ms",
266 len, pptr->timeout, buf, (buf + strlen(buf) + 1), pingtime));
268 s = pptr->buf + strlen(pptr->buf);
269 sprintf(s, " %u", pingtime);
271 if (pptr->received == pptr->count)
276 int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int count)
284 if (INADDR_NONE == aconf->ipnum.s_addr) {
285 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host lookup failed for "
286 "%s", sptr, aconf->name);
291 uping_cancel(sptr, sptr); /* Cancel previous ping request */
293 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
294 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Unable to create udp "
295 "ping socket", sptr);
299 if (!os_set_nonblocking(fd)) {
300 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Can't set fd non-"
305 pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
307 memset(pptr, 0, sizeof(struct UPing));
310 pptr->sin.sin_port = htons(port);
311 pptr->sin.sin_addr.s_addr = aconf->ipnum.s_addr;
312 pptr->sin.sin_family = AF_INET;
313 pptr->count = IRCD_MIN(20, count);
316 strcpy(pptr->name, aconf->name);
318 pptr->next = pingList;
327 void uping_end(struct UPing* pptr)
329 Debug((DEBUG_DEBUG, "uping_end: %p", pptr));
332 if (pptr->lastsent) {
333 if (0 < pptr->received) {
334 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING %s%s",
335 pptr->client, pptr->name, pptr->buf);
336 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING Stats: "
337 "sent %d recvd %d ; min/avg/max = %1lu/%1lu/%1lu ms",
338 pptr->client, pptr->sent, pptr->received, pptr->ms_min,
339 (2 * pptr->ms_ave) / (2 * pptr->received), pptr->ms_max);
341 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: no response "
342 "from %s within %d seconds", pptr->client, pptr->name,
345 sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: Could not "
346 "start ping to %s", pptr->client, pptr->name);
353 ClearUPing(pptr->client);
357 void uping_cancel(struct Client *sptr, struct Client* acptr)
360 struct UPing* ping_next;
362 Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", sptr->name));
363 for (ping = pingList; ping; ping = ping_next) {
364 ping_next = ping->next;
365 if (sptr == ping->client) {
366 ping->client = acptr;