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" /* vserv */
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 if (MyUser(pptr->client)) {
174 sendto_one(pptr->client,
175 ":%s NOTICE %s :Sending %d ping%s to %s",
176 me.name, pptr->client->name, pptr->count,
177 (pptr->count == 1) ? "" : "s", pptr->name);
180 sendto_one(pptr->client,
181 "%s NOTICE %s%s :Sending %d ping%s to %s",
182 NumServ(&me), NumNick(pptr->client), pptr->count,
183 (pptr->count == 1) ? "" : "s", pptr->name);
185 pptr->timeout = CurrentTime + UPINGTIMEOUT;
193 void uping_send(struct UPing* pptr)
196 char buf[BUFSIZE + 1];
199 if (pptr->sent == pptr->count)
201 memset(buf, 0, sizeof(buf));
203 gettimeofday(&tv, NULL);
204 sprintf(buf, " %10lu%c%6lu", tv.tv_sec, '\0', tv.tv_usec);
206 Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d",
208 ircd_ntoa((const char*) &pptr->sin.sin_addr), ntohs(pptr->sin.sin_port),
211 if (sendto(pptr->fd, buf, BUFSIZE, 0, (struct sockaddr*) &pptr->sin,
212 sizeof(struct sockaddr_in)) != BUFSIZE)
214 const char* msg = strerror(errno);
216 msg = "Unknown error";
218 if (MyUser(pptr->client))
219 sendto_one(pptr->client, ":%s NOTICE %s :UPING: send failed: %s",
220 me.name, pptr->client->name, msg);
222 sendto_one(pptr->client, "%s NOTICE %s%s :UPING: sendto() failed: %s",
223 NumServ(&me), NumNick(pptr->client), msg);
225 Debug((DEBUG_DEBUG, "UPING: send_ping: sendto failed on %d: %s", pptr->fd, msg));
235 void uping_read(struct UPing* pptr)
237 struct sockaddr_in sin;
240 unsigned int pingtime;
242 char buf[BUFSIZE + 1];
247 gettimeofday(&tv, NULL);
249 ior = os_recvfrom_nonb(pptr->fd, buf, BUFSIZE, &len, &sin);
250 if (IO_BLOCKED == ior)
252 else if (IO_FAILURE == ior) {
253 const char* msg = strerror(errno);
255 msg = "Unknown error";
256 if (MyUser(pptr->client))
257 sendto_one(pptr->client, ":%s NOTICE %s :UPING: receive error: %s",
258 me.name, pptr->client->name, msg);
260 sendto_one(pptr->client, "%s NOTICE %s%s :UPING: receive error: %s",
261 NumServ(&me), NumNick(pptr->client), msg);
267 return; /* Broken packet */
272 pingtime = (tv.tv_sec - atol(&buf[1])) * 1000
273 + (tv.tv_usec - atol(buf + strlen(buf) + 1)) / 1000;
275 pptr->ms_ave += pingtime;
276 if (!pptr->ms_min || pptr->ms_min > pingtime)
277 pptr->ms_min = pingtime;
278 if (pingtime > pptr->ms_max)
279 pptr->ms_max = pingtime;
281 pptr->timeout = CurrentTime + UPINGTIMEOUT;
283 Debug((DEBUG_SEND, "read_ping: %d bytes, ti %lu: [%s %s] %lu ms",
284 len, pptr->timeout, buf, (buf + strlen(buf) + 1), pingtime));
286 s = pptr->buf + strlen(pptr->buf);
287 sprintf(s, " %u", pingtime);
289 if (pptr->received == pptr->count)
294 int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int count)
302 if (INADDR_NONE == aconf->ipnum.s_addr) {
304 sendto_one(sptr, ":%s NOTICE %s :UPING: Host lookup failed for %s",
305 me.name, sptr->name, aconf->name);
307 sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Host lookup failed for %s",
308 NumServ(&me), NumNick(sptr), aconf->name);
312 uping_cancel(sptr, sptr); /* Cancel previous ping request */
314 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
316 sendto_one(sptr, ":%s NOTICE %s :UPING: Unable to create udp ping socket",
317 me.name, sptr->name);
319 sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Unable to create udp ping socket",
320 NumServ(&me), NumNick(sptr));
324 if (!os_set_nonblocking(fd)) {
326 sendto_one(sptr, ":%s NOTICE %s :UPING: Can't set fd non-blocking",
327 me.name, sptr->name);
329 sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Can't set fd non-blocking",
330 NumServ(&me), NumNick(sptr));
334 pptr = (struct UPing*) MyMalloc(sizeof(struct UPing));
336 memset(pptr, 0, sizeof(struct UPing));
339 pptr->sin.sin_port = htons(port);
340 pptr->sin.sin_addr.s_addr = aconf->ipnum.s_addr;
341 pptr->sin.sin_family = AF_INET;
342 pptr->count = IRCD_MIN(20, count);
345 strcpy(pptr->name, aconf->name);
347 pptr->next = pingList;
356 void uping_end(struct UPing* pptr)
358 Debug((DEBUG_DEBUG, "uping_end: %p", pptr));
361 if (MyUser(pptr->client)) {
362 if (pptr->lastsent) {
363 if (0 < pptr->received) {
364 sendto_one(pptr->client, ":%s NOTICE %s :UPING %s%s",
365 me.name, pptr->client->name, pptr->name, pptr->buf);
367 * XXX - warning long unsigned int format, unsigned int arg (7, 8, 9)
369 sendto_one(pptr->client,
370 ":%s NOTICE %s :UPING Stats: sent %d recvd %d ; "
371 "min/avg/max = %1lu/%1lu/%1lu ms",
372 me.name, pptr->client->name, pptr->sent,
373 pptr->received, pptr->ms_min,
374 (2 * pptr->ms_ave) / (2 * pptr->received),
378 sendto_one(pptr->client,
379 ":%s NOTICE %s :UPING: no response from %s within %d seconds",
380 me.name, pptr->client->name, pptr->name,
384 sendto_one(pptr->client,
385 ":%s NOTICE %s :UPING: Could not start ping to %s",
386 me.name, pptr->client->name, pptr->name);
389 if (pptr->lastsent) {
390 if (0 < pptr->received) {
391 sendto_one(pptr->client, "%s NOTICE %s%s :UPING %s%s",
392 NumServ(&me), NumNick(pptr->client), pptr->name, pptr->buf);
393 /* XXX - warning: long unsigned int format, unsigned int arg(9, 10, 11) */
394 sendto_one(pptr->client,
395 "%s " TOK_NOTICE " %s%s :UPING Stats: sent %d recvd %d ; "
396 "min/avg/max = %1lu/%1lu/%1lu ms",
397 NumServ(&me), NumNick(pptr->client), pptr->sent,
398 pptr->received, pptr->ms_min,
399 (2 * pptr->ms_ave) / (2 * pptr->received),
403 sendto_one(pptr->client,
404 "%s " TOK_NOTICE " %s%s :UPING: no response from %s within %d seconds",
405 NumServ(&me), NumNick(pptr->client), pptr->name, UPINGTIMEOUT);
408 sendto_one(pptr->client,
409 "%s " TOK_NOTICE " %s%s :UPING: Could not start ping to %s",
410 NumServ(&me), NumNick(pptr->client), pptr->name);
417 ClearUPing(pptr->client);
421 void uping_cancel(struct Client *sptr, struct Client* acptr)
424 struct UPing* ping_next;
426 Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", sptr->name));
427 for (ping = pingList; ping; ping = ping_next) {
428 ping_next = ping->next;
429 if (sptr == ping->client) {
430 ping->client = acptr;