1 //
2 // This file is part of aMule Project
3 //
4 // Copyright (c) 2004-2011 Angel Vidal ( kry@amule.org )
5 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2003-2011 Barry Dunne ( http://www.emule-project.net )
7 
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
21 
22 // This work is based on the java implementation of the Kademlia protocol.
23 // Kademlia: Peer-to-peer routing based on the XOR metric
24 // Copyright (c) 2002  Petar Maymounkov ( petar@post.harvard.edu )
25 // http://kademlia.scs.cs.nyu.edu
26 
27 // Note To Mods //
28 /*
29 Please do not change anything here and release it..
30 There is going to be a new forum created just for the Kademlia side of the client..
31 If you feel there is an error or a way to improve something, please
32 post it in the forum first and let us look at it.. If it is a real improvement,
33 it will be added to the offical client.. Changing something without knowing
34 what all it does can cause great harm to the network if released in mass form..
35 Any mod that changes anything within the Kademlia side will not be allowed to advertise
36 there client on the eMule forum..
37 */
38 
39 #include "Contact.h"
40 
41 #include <common/Macros.h>
42 
43 #include "../../Statistics.h"
44 
45 ////////////////////////////////////////
46 using namespace Kademlia;
47 ////////////////////////////////////////
48 
~CContact()49 CContact::~CContact()
50 {
51 	theStats::RemoveKadNode();
52 }
53 
CContact(const CUInt128 & clientID,uint32_t ip,uint16_t udpPort,uint16_t tcpPort,uint8_t version,const CKadUDPKey & key,bool ipVerified,const CUInt128 & target)54 CContact::CContact(const CUInt128 &clientID, uint32_t ip, uint16_t udpPort, uint16_t tcpPort, uint8_t version, const CKadUDPKey& key, bool ipVerified, const CUInt128 &target)
55 	: m_clientID(clientID),
56 	  m_distance(target ^ clientID),
57 	  m_ip(ip),
58 	  m_tcpPort(tcpPort),
59 	  m_udpPort(udpPort),
60 	  m_type(3),
61 	  m_lastTypeSet(time(NULL)),
62 	  m_expires(0),
63 	  m_created(m_lastTypeSet),
64 	  m_inUse(0),
65 	  m_version(version),
66 	  m_ipVerified(ipVerified),
67 	  m_receivedHelloPacket(false),
68 	  m_udpKey(key)
69 {
70 	wxASSERT(udpPort);
71 	theStats::AddKadNode();
72 }
73 
CContact(const CContact & k1)74 CContact::CContact(const CContact& k1)
75 {
76 	*this = k1;
77 	theStats::AddKadNode();
78 }
79 
CheckingType()80 void CContact::CheckingType() throw()
81 {
82 	time_t now = time(NULL);
83 
84 	if(now - m_lastTypeSet < 10 || m_type == 4) {
85 		return;
86 	}
87 
88 	m_lastTypeSet = now;
89 
90 	m_expires = now + MIN2S(2);
91 	m_type++;
92 }
93 
UpdateType()94 void CContact::UpdateType() throw()
95 {
96 	time_t now = time(NULL);
97 	uint32_t hours = (now - m_created) / HR2S(1);
98 	switch (hours) {
99 		case 0:
100 			m_type = 2;
101 			m_expires = now + HR2S(1);
102 			break;
103 		case 1:
104 			m_type = 1;
105 			m_expires = now + MIN2S(90); //HR2S(1.5)
106 			break;
107 		default:
108 			m_type = 0;
109 			m_expires = now + HR2S(2);
110 	}
111 }
112 
GetLastSeen() const113 time_t CContact::GetLastSeen() const throw()
114 {
115 	// calculating back from expire time, so we don't need an additional field.
116 	// might result in wrong values if doing CheckingType() for example, so don't use for important timing stuff
117 	if (m_expires != 0) {
118 		switch (m_type) {
119 			case 2: return m_expires - HR2S(1);
120 			case 1: return m_expires - MIN2S(90) /*(unsigned)HR2S(1.5)*/;
121 			case 0: return m_expires - HR2S(2);
122 		}
123 	}
124 	return 0;
125 }
126 // File_checked_for_headers
127