]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/pingtimer.cpp
Merge tag 'v2.0.25' into master.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / pingtimer.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21
22 #include "pingtimer.h"
23 #include "treeserver.h"
24 #include "commandbuilder.h"
25
26 PingTimer::PingTimer(TreeServer* ts)
27         : Timer(Utils->PingFreq)
28         , server(ts)
29         , state(PS_SENDPING)
30 {
31 }
32
33 PingTimer::State PingTimer::TickInternal()
34 {
35         // Timer expired, take next action based on what happened last time
36         if (state == PS_SENDPING)
37         {
38                 // Last ping was answered, send next ping
39                 server->GetSocket()->WriteLine(CmdBuilder("PING").push(server->GetID()));
40                 LastPingMsec = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
41                 // Warn next unless warnings are disabled. If they are, jump straight to timeout.
42                 if (Utils->PingWarnTime)
43                         return PS_WARN;
44                 else
45                         return PS_TIMEOUT;
46         }
47         else if (state == PS_WARN)
48         {
49                 // No pong arrived in PingWarnTime seconds, send a warning to opers
50                 ServerInstance->SNO->WriteToSnoMask('l', "Server \002%s\002 has not responded to PING for %d seconds, high latency.", server->GetName().c_str(), GetInterval());
51                 return PS_TIMEOUT;
52         }
53         else // PS_TIMEOUT
54         {
55                 // They didn't answer the last ping, if they are locally connected, get rid of them
56                 if (server->IsLocal())
57                 {
58                         TreeSocket* sock = server->GetSocket();
59                         sock->SendError("Ping timeout");
60                         sock->Close();
61                 }
62
63                 // If the server is non-locally connected, don't do anything until we get a PONG.
64                 // This is to avoid pinging the server and warning opers more than once.
65                 // If they do answer eventually, we will move to the PS_SENDPING state and ping them again.
66                 return PS_IDLE;
67         }
68 }
69
70 void PingTimer::SetState(State newstate)
71 {
72         state = newstate;
73
74         // Set when should the next Tick() happen based on the state
75         if (state == PS_SENDPING)
76                 SetInterval(Utils->PingFreq);
77         else if (state == PS_WARN)
78                 SetInterval(Utils->PingWarnTime);
79         else if (state == PS_TIMEOUT)
80                 SetInterval(Utils->PingFreq - Utils->PingWarnTime);
81
82         // If state == PS_IDLE, do not set the timer, see above why
83 }
84
85 bool PingTimer::Tick(time_t currtime)
86 {
87         if (server->IsDead())
88                 return false;
89
90         SetState(TickInternal());
91         return false;
92 }
93
94 void PingTimer::OnPong()
95 {
96         // Calculate RTT
97         long ts = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
98         server->rtt = ts - LastPingMsec;
99
100         // Change state to send ping next, also reschedules the timer appropriately
101         SetState(PS_SENDPING);
102 }