X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=ircd%2Fuping.c;h=3c9ea1e1de233d34e6149ced2082733de10d98a3;hb=79035436c61e2b58004b47f250ab1e54744a88d6;hp=c84401b2da744d3b579eaf526babc4b25fbc7996;hpb=56ac07961060874ee62cc83a4af85be97efd6c27;p=ircu2.10.12-pk.git diff --git a/ircd/uping.c b/ircd/uping.c index c84401b..3c9ea1e 100644 --- a/ircd/uping.c +++ b/ircd/uping.c @@ -18,10 +18,13 @@ * * $Id$ */ +#include "config.h" + #include "uping.h" #include "client.h" #include "ircd.h" #include "ircd_alloc.h" +#include "ircd_events.h" #include "ircd_log.h" #include "ircd_osdep.h" #include "ircd_string.h" @@ -57,6 +60,8 @@ static struct UPing* pingList = 0; int UPingFileDescriptor = -1; /* UDP listener socket for upings */ +static struct Socket upingSock; + /* * pings_begin - iterator function for ping list */ @@ -86,6 +91,14 @@ static void uping_erase(struct UPing* p) } } +/* Called when the event engine detects activity on the UPing socket */ +static void uping_echo_callback(struct Event* ev) +{ + assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR); + + uping_echo(); +} + /* * Setup a UDP socket and listen for incoming packets */ @@ -105,14 +118,17 @@ int uping_init(void) return -1; } if (!os_set_reuseaddr(fd)) { - ircd_log(L_ERROR, "UPING: setsockopt UDP listener: fd %d", fd); + log_write(LS_SOCKET, L_ERROR, 0, + "UPING: set reuseaddr on UDP listener failed: %m (fd %d)", fd); Debug((DEBUG_ERROR, "UPING: set reuseaddr on UDP listener failed: %s", (strerror(errno)) ? strerror(errno) : "Unknown error")); close(fd); return -1; } if (bind(fd, (struct sockaddr*) &from, sizeof(from)) == -1) { - ircd_log(L_ERROR, "UPING: bind UDP listener %d fd %d", htons(from.sin_port), fd); + log_write(LS_SOCKET, L_ERROR, 0, + "UPING: bind on UDP listener (%d fd %d) failed: %m", + htons(from.sin_port), fd); Debug((DEBUG_ERROR, "UPING: bind on UDP listener failed : %s", (strerror(errno)) ? strerror(errno) : "Unknown error")); close(fd); @@ -124,6 +140,12 @@ int uping_init(void) close(fd); return -1; } + if (!socket_add(&upingSock, uping_echo_callback, 0, SS_DATAGRAM, + SOCK_EVENT_READABLE, fd)) { + Debug((DEBUG_ERROR, "UPING: Unable to queue fd to event system")); + close(fd); + return -1; + } UPingFileDescriptor = fd; return fd; } @@ -163,6 +185,85 @@ void uping_echo() } +/* Callback when socket has data to read */ +static void uping_read_callback(struct Event* ev) +{ + struct UPing *pptr; + + assert(0 != ev_socket(ev)); + assert(0 != s_data(ev_socket(ev))); + + pptr = (struct UPing*) s_data(ev_socket(ev)); + + Debug((DEBUG_SEND, "uping_read_callback called, %p (%d)", pptr, + ev_type(ev))); + + if (ev_type(ev) == ET_DESTROY) { /* being destroyed */ + pptr->freeable &= ~UPING_PENDING_SOCKET; + + if (!pptr->freeable) + MyFree(pptr); /* done with it, finally */ + } else { + assert(ev_type(ev) == ET_READ || ev_type(ev) == ET_ERROR); + + uping_read(pptr); /* read uping response */ + } +} + +/* Callback to send another ping */ +static void uping_sender_callback(struct Event* ev) +{ + struct UPing *pptr; + + assert(0 != ev_timer(ev)); + assert(0 != t_data(ev_timer(ev))); + + pptr = (struct UPing*) t_data(ev_timer(ev)); + + Debug((DEBUG_SEND, "uping_sender_callback called, %p (%d)", pptr, + ev_type(ev))); + + if (ev_type(ev) == ET_DESTROY) { /* being destroyed */ + pptr->freeable &= ~UPING_PENDING_SENDER; + + if (!pptr->freeable) + MyFree(pptr); /* done with it, finally */ + } else { + assert(ev_type(ev) == ET_EXPIRE); + + pptr->lastsent = CurrentTime; /* store last ping time */ + uping_send(pptr); /* send a ping */ + + if (pptr->sent == pptr->count) /* done sending pings, don't send more */ + timer_del(ev_timer(ev)); + } +} + +/* Callback to kill a ping */ +static void uping_killer_callback(struct Event* ev) +{ + struct UPing *pptr; + + assert(0 != ev_timer(ev)); + assert(0 != t_data(ev_timer(ev))); + + pptr = (struct UPing*) t_data(ev_timer(ev)); + + Debug((DEBUG_SEND, "uping_killer_callback called, %p (%d)", pptr, + ev_type(ev))); + + if (ev_type(ev) == ET_DESTROY) { /* being destroyed */ + pptr->freeable &= ~UPING_PENDING_KILLER; + + if (!pptr->freeable) + MyFree(pptr); /* done with it, finally */ + } else { + assert(ev_type(ev) == ET_EXPIRE); + + uping_end(pptr); /* kill the uping, kill the uping! */ + } +} + /* * start_ping */ @@ -170,19 +271,15 @@ static void uping_start(struct UPing* pptr) { assert(0 != pptr); - if (MyUser(pptr->client)) { - sendto_one(pptr->client, - ":%s NOTICE %s :Sending %d ping%s to %s", - me.name, pptr->client->name, pptr->count, - (pptr->count == 1) ? "" : "s", pptr->name); - } - else { - sendto_one(pptr->client, - "%s NOTICE %s%s :Sending %d ping%s to %s", - NumServ(&me), NumNick(pptr->client), pptr->count, - (pptr->count == 1) ? "" : "s", pptr->name); - } - pptr->timeout = CurrentTime + UPINGTIMEOUT; + timer_add(timer_init(&pptr->sender), uping_sender_callback, (void*) pptr, + TT_PERIODIC, 1); + timer_add(timer_init(&pptr->killer), uping_killer_callback, (void*) pptr, + TT_RELATIVE, UPINGTIMEOUT); + pptr->freeable |= UPING_PENDING_SENDER | UPING_PENDING_KILLER; + + sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :Sending %d ping%s to %s", + pptr->client, pptr->count, (pptr->count == 1) ? "" : "s", + pptr->name); pptr->active = 1; } @@ -201,7 +298,7 @@ void uping_send(struct UPing* pptr) memset(buf, 0, sizeof(buf)); gettimeofday(&tv, NULL); - sprintf(buf, " %10lu%c%6lu", tv.tv_sec, '\0', tv.tv_usec); + sprintf(buf, " %10lu%c%6lu", (unsigned long)tv.tv_sec, '\0', (unsigned long)tv.tv_usec); Debug((DEBUG_SEND, "send_ping: sending [%s %s] to %s.%d on %d", buf, &buf[12], @@ -214,14 +311,9 @@ void uping_send(struct UPing* pptr) const char* msg = strerror(errno); if (!msg) msg = "Unknown error"; - if (pptr->client) { - if (MyUser(pptr->client)) - sendto_one(pptr->client, ":%s NOTICE %s :UPING: send failed: %s", - me.name, pptr->client->name, msg); - else - sendto_one(pptr->client, "%s NOTICE %s%s :UPING: sendto() failed: %s", - NumServ(&me), NumNick(pptr->client), msg); - } + if (pptr->client) + sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: send failed: " + "%s", pptr->client, msg); Debug((DEBUG_DEBUG, "UPING: send_ping: sendto failed on %d: %s", pptr->fd, msg)); uping_end(pptr); return; @@ -253,12 +345,8 @@ void uping_read(struct UPing* pptr) const char* msg = strerror(errno); if (!msg) msg = "Unknown error"; - if (MyUser(pptr->client)) - sendto_one(pptr->client, ":%s NOTICE %s :UPING: receive error: %s", - me.name, pptr->client->name, msg); - else - sendto_one(pptr->client, "%s NOTICE %s%s :UPING: receive error: %s", - NumServ(&me), NumNick(pptr->client), msg); + sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: receive error: " + "%s", pptr->client, msg); uping_end(pptr); return; } @@ -277,11 +365,8 @@ void uping_read(struct UPing* pptr) pptr->ms_min = pingtime; if (pingtime > pptr->ms_max) pptr->ms_max = pingtime; - - pptr->timeout = CurrentTime + UPINGTIMEOUT; - Debug((DEBUG_SEND, "read_ping: %d bytes, ti %lu: [%s %s] %lu ms", - len, pptr->timeout, buf, (buf + strlen(buf) + 1), pingtime)); + timer_chg(&pptr->killer, TT_RELATIVE, UPINGTIMEOUT); s = pptr->buf + strlen(pptr->buf); sprintf(s, " %u", pingtime); @@ -300,34 +385,23 @@ int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int coun assert(0 != aconf); if (INADDR_NONE == aconf->ipnum.s_addr) { - if (MyUser(sptr)) - sendto_one(sptr, ":%s NOTICE %s :UPING: Host lookup failed for %s", - me.name, sptr->name, aconf->name); - else - sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Host lookup failed for %s", - NumServ(&me), NumNick(sptr), aconf->name); + sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Host lookup failed for " + "%s", sptr, aconf->name); + return 0; } if (IsUPing(sptr)) uping_cancel(sptr, sptr); /* Cancel previous ping request */ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - if (MyUser(sptr)) - sendto_one(sptr, ":%s NOTICE %s :UPING: Unable to create udp ping socket", - me.name, sptr->name); - else - sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Unable to create udp ping socket", - NumServ(&me), NumNick(sptr)); + sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Unable to create udp " + "ping socket", sptr); return 0; } if (!os_set_nonblocking(fd)) { - if (MyUser(sptr)) - sendto_one(sptr, ":%s NOTICE %s :UPING: Can't set fd non-blocking", - me.name, sptr->name); - else - sendto_one(sptr, "%s " TOK_NOTICE " %s%s :UPING: Can't set fd non-blocking", - NumServ(&me), NumNick(sptr)); + sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Can't set fd non-" + "blocking", sptr); close(fd); return 0; } @@ -335,6 +409,15 @@ int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int coun assert(0 != pptr); memset(pptr, 0, sizeof(struct UPing)); + if (!socket_add(&pptr->socket, uping_read_callback, (void*) pptr, + SS_DATAGRAM, SOCK_EVENT_READABLE, fd)) { + sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :UPING: Can't queue fd for " + "reading", sptr); + close(fd); + MyFree(pptr); + return 0; + } + pptr->fd = fd; pptr->sin.sin_port = htons(port); pptr->sin.sin_addr.s_addr = aconf->ipnum.s_addr; @@ -342,6 +425,7 @@ int uping_server(struct Client* sptr, struct ConfItem* aconf, int port, int coun pptr->count = IRCD_MIN(20, count); pptr->client = sptr; pptr->index = -1; + pptr->freeable = UPING_PENDING_SOCKET; strcpy(pptr->name, aconf->name); pptr->next = pingList; @@ -358,64 +442,34 @@ void uping_end(struct UPing* pptr) Debug((DEBUG_DEBUG, "uping_end: %p", pptr)); if (pptr->client) { - if (MyUser(pptr->client)) { - if (pptr->lastsent) { - if (0 < pptr->received) { - sendto_one(pptr->client, ":%s NOTICE %s :UPING %s%s", - me.name, pptr->client->name, pptr->name, pptr->buf); - /* - * XXX - warning long unsigned int format, unsigned int arg (7, 8, 9) - */ - sendto_one(pptr->client, - ":%s NOTICE %s :UPING Stats: sent %d recvd %d ; " - "min/avg/max = %1lu/%1lu/%1lu ms", - me.name, pptr->client->name, pptr->sent, - pptr->received, pptr->ms_min, - (2 * pptr->ms_ave) / (2 * pptr->received), - pptr->ms_max); - } - else - sendto_one(pptr->client, - ":%s NOTICE %s :UPING: no response from %s within %d seconds", - me.name, pptr->client->name, pptr->name, - UPINGTIMEOUT); - } - else - sendto_one(pptr->client, - ":%s NOTICE %s :UPING: Could not start ping to %s", - me.name, pptr->client->name, pptr->name); - } - else { - if (pptr->lastsent) { - if (0 < pptr->received) { - sendto_one(pptr->client, "%s NOTICE %s%s :UPING %s%s", - NumServ(&me), NumNick(pptr->client), pptr->name, pptr->buf); - /* XXX - warning: long unsigned int format, unsigned int arg(9, 10, 11) */ - sendto_one(pptr->client, - "%s " TOK_NOTICE " %s%s :UPING Stats: sent %d recvd %d ; " - "min/avg/max = %1lu/%1lu/%1lu ms", - NumServ(&me), NumNick(pptr->client), pptr->sent, - pptr->received, pptr->ms_min, - (2 * pptr->ms_ave) / (2 * pptr->received), - pptr->ms_max); - } - else - sendto_one(pptr->client, - "%s " TOK_NOTICE " %s%s :UPING: no response from %s within %d seconds", - NumServ(&me), NumNick(pptr->client), pptr->name, UPINGTIMEOUT); - } - else - sendto_one(pptr->client, - "%s " TOK_NOTICE " %s%s :UPING: Could not start ping to %s", - NumServ(&me), NumNick(pptr->client), pptr->name); - } + if (pptr->lastsent) { + if (0 < pptr->received) { + sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING %s%s", + pptr->client, pptr->name, pptr->buf); + sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING Stats: " + "sent %d recvd %d ; min/avg/max = %1lu/%1lu/%1lu ms", + pptr->client, pptr->sent, pptr->received, pptr->ms_min, + (2 * pptr->ms_ave) / (2 * pptr->received), pptr->ms_max); + } else + sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: no response " + "from %s within %d seconds", pptr->client, pptr->name, + UPINGTIMEOUT); + } else + sendcmdto_one(&me, CMD_NOTICE, pptr->client, "%C :UPING: Could not " + "start ping to %s", pptr->client, pptr->name); } + close(pptr->fd); pptr->fd = -1; uping_erase(pptr); if (pptr->client) ClearUPing(pptr->client); - MyFree(pptr); + if (pptr->freeable & UPING_PENDING_SOCKET) + socket_del(&pptr->socket); + if (pptr->freeable & UPING_PENDING_SENDER) + timer_del(&pptr->sender); + if (pptr->freeable & UPING_PENDING_KILLER) + timer_del(&pptr->killer); } void uping_cancel(struct Client *sptr, struct Client* acptr) @@ -423,7 +477,7 @@ void uping_cancel(struct Client *sptr, struct Client* acptr) struct UPing* ping; struct UPing* ping_next; - Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", sptr->name)); + Debug((DEBUG_DEBUG, "UPING: cancelling uping for %s", cli_name(sptr))); for (ping = pingList; ping; ping = ping_next) { ping_next = ping->next; if (sptr == ping->client) {