Upstream tarball 20080414
[amule.git] / src / Server.cpp
blob4fdbb83096117849c090cd60bec6a12820e82359
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "Server.h" // Interface declarations.
28 #include <tags/ServerTags.h>
30 #include "NetworkFunctions.h" // Needed for StringIPtoUint32
31 #include "OtherStructs.h" // Needed for ServerMet_Struct
32 #include "amule.h"
34 CServer::CServer(ServerMet_Struct* in_data)
36 port = in_data->port;
37 ip = in_data->ip;
39 Init();
42 CServer::CServer(uint16 in_port, const wxString i_addr)
45 port = in_port;
46 ip = StringIPtoUint32(i_addr);
48 Init();
50 // GonoszTopi - Init() would clear dynip !
51 // Check that the ip isn't in fact 0.0.0.0
52 if (!ip && !StringIPtoUint32( i_addr, ip ) ) {
53 // If ip == 0, the address is a hostname
54 dynip = i_addr;
58 #ifdef CLIENT_GUI
59 CServer::CServer(CEC_Server_Tag *tag)
61 ip = tag->GetIPv4Data().IP();
63 port = tag->GetIPv4Data().m_port;
65 Init();
67 listname = tag->ServerName();
68 description = tag->ServerDesc();
69 maxusers = tag->GetMaxUsers();
71 files = tag->GetFiles();
72 users = tag->GetUsers();
74 preferences = tag->GetPrio(); // SRV_PR_NORMAL = 0, so it's ok
75 staticservermember = tag->GetStatic();
77 ping = tag->GetPing();
78 failedcount = tag->GetFailed();
80 #endif
82 // copy constructor
83 CServer::CServer(CServer* pOld)
85 wxASSERT(pOld != NULL);
87 TagPtrList::iterator it = pOld->m_taglist.begin();
88 for ( ; it != pOld->m_taglist.end(); ++it ) {
89 m_taglist.push_back((*it)->CloneTag());
91 port = pOld->port;
92 ip = pOld->ip;
93 realport = pOld->realport;
94 staticservermember=pOld->IsStaticMember();
95 tagcount = pOld->tagcount;
96 ipfull = pOld->ipfull;
97 files = pOld->files;
98 users = pOld->users;
99 preferences = pOld->preferences;
100 ping = pOld->ping;
101 failedcount = pOld->failedcount;
102 lastpinged = pOld->lastpinged;
103 lastpingedtime = pOld->lastpingedtime;
104 m_dwRealLastPingedTime = pOld->m_dwRealLastPingedTime;
105 description = pOld->description;
106 listname = pOld->listname;
107 dynip = pOld->dynip;
108 maxusers = pOld->maxusers;
109 m_strVersion = pOld->m_strVersion;
110 m_uTCPFlags = pOld->m_uTCPFlags;
111 m_uUDPFlags = pOld->m_uUDPFlags;
112 challenge = pOld->challenge;
113 softfiles = pOld->softfiles;
114 hardfiles = pOld->hardfiles;
115 m_uDescReqChallenge = pOld->m_uDescReqChallenge;
116 m_uLowIDUsers = pOld->m_uLowIDUsers;
117 lastdescpingedcout = pOld->lastdescpingedcout;
118 m_auxPorts = pOld->m_auxPorts;
119 m_dwServerKeyUDP = pOld->m_dwServerKeyUDP;
120 m_bCryptPingReplyPending = pOld->m_bCryptPingReplyPending;
121 m_dwIPServerKeyUDP = pOld->m_dwIPServerKeyUDP;
122 m_nObfuscationPortTCP = pOld->m_nObfuscationPortTCP;
123 m_nObfuscationPortUDP = pOld->m_nObfuscationPortUDP;
126 CServer::~CServer()
128 TagPtrList::iterator it = m_taglist.begin();
129 for ( ; it != m_taglist.end(); ++it ) {
130 delete *it;
133 m_taglist.clear();
136 void CServer::Init() {
138 ipfull = Uint32toStringIP(ip);
140 realport = 0;
141 tagcount = 0;
142 files = 0;
143 users = 0;
144 preferences = 0;
145 ping = 0;
146 description.Clear();
147 listname.Clear();
148 dynip.Clear();
149 failedcount = 0;
150 lastpinged = 0;
151 lastpingedtime = 0;
152 m_dwRealLastPingedTime = 0;
153 staticservermember=false;
154 maxusers=0;
155 m_uTCPFlags = 0;
156 m_uUDPFlags = 0;
157 challenge = 0;
158 softfiles = 0;
159 hardfiles = 0;
160 m_strVersion = _("Unknown");
161 m_uLowIDUsers = 0;
162 m_uDescReqChallenge = 0;
163 lastdescpingedcout = 0;
164 m_auxPorts.Clear();
165 m_lastdnssolve = 0;
166 m_dnsfailure = false;
168 m_dwServerKeyUDP = 0;
169 m_bCryptPingReplyPending = false;
170 m_dwIPServerKeyUDP = 0;
171 m_nObfuscationPortTCP = 0;
172 m_nObfuscationPortUDP = 0;
177 bool CServer::AddTagFromFile(CFileDataIO* servermet)
179 if (servermet == NULL) {
180 return false;
183 CTag tag(*servermet, true);
185 switch(tag.GetNameID()){
186 case ST_SERVERNAME:
187 if (listname.IsEmpty()) {
188 listname = tag.GetStr();
190 break;
192 case ST_DESCRIPTION:
193 if (description.IsEmpty()) {
194 description = tag.GetStr();
196 break;
198 case ST_PREFERENCE:
199 preferences = tag.GetInt();
200 break;
202 case ST_PING:
203 ping = tag.GetInt();
204 break;
206 case ST_DYNIP:
207 if (dynip.IsEmpty()) {
208 dynip = tag.GetStr();
210 break;
212 case ST_FAIL:
213 failedcount = tag.GetInt();
214 break;
216 case ST_LASTPING:
217 lastpinged = tag.GetInt();
218 break;
220 case ST_MAXUSERS:
221 maxusers = tag.GetInt();
222 break;
224 case ST_SOFTFILES:
225 softfiles = tag.GetInt();
226 break;
228 case ST_HARDFILES:
229 hardfiles = tag.GetInt();
230 break;
232 case ST_VERSION:
233 if (tag.IsStr()) {
234 // m_strVersion defaults to _("Unknown"), so check for that as well
235 if ((m_strVersion.IsEmpty()) || (m_strVersion == _("Unknown"))) {
236 m_strVersion = tag.GetStr();
238 } else if (tag.IsInt()) {
239 m_strVersion = wxString::Format(wxT("%u.%u"), tag.GetInt() >> 16, tag.GetInt() & 0xFFFF);
240 } else {
241 wxASSERT(0);
243 break;
245 case ST_UDPFLAGS:
246 m_uUDPFlags = tag.GetInt();
247 break;
249 case ST_AUXPORTSLIST:
250 m_auxPorts = tag.GetStr();
252 // Some server.mets contain empty ST_AUXPORTSLIST tags
253 if (!m_auxPorts.IsEmpty()) {
254 realport = port;
255 port = StrToULong(m_auxPorts.BeforeFirst(','));
257 break;
259 case ST_LOWIDUSERS:
260 m_uLowIDUsers = tag.GetInt();
261 break;
263 case ST_UDPKEY:
264 m_dwServerKeyUDP = tag.GetInt();
265 break;
267 case ST_UDPKEYIP:
268 m_dwIPServerKeyUDP = tag.GetInt();
269 break;
271 case ST_TCPPORTOBFUSCATION:
272 m_nObfuscationPortTCP = (uint16)tag.GetInt();
273 break;
275 case ST_UDPPORTOBFUSCATION:
276 m_nObfuscationPortUDP = (uint16)tag.GetInt();
277 break;
279 default:
280 if (!tag.GetName().IsEmpty()) {
281 if (tag.GetName() == wxT("files")) {
282 files = tag.GetInt();
283 } else if (tag.GetName() == wxT("users")) {
284 users = tag.GetInt();
286 } else {
287 wxASSERT(0);
291 return true;
295 void CServer::SetListName(const wxString& newname)
297 listname = newname;
300 void CServer::SetDescription(const wxString& newname)
302 description = newname;
305 void CServer::SetID(uint32 newip)
307 wxASSERT(newip);
308 ip = newip;
309 ipfull = Uint32toStringIP(ip);
312 void CServer::SetDynIP(const wxString& newdynip)
314 dynip = newdynip;
318 void CServer::SetLastDescPingedCount(bool bReset)
320 if (bReset) {
321 lastdescpingedcout = 0;
322 } else {
323 lastdescpingedcout++;
327 uint32 CServer::GetServerKeyUDP(bool bForce) const
329 if (m_dwIPServerKeyUDP != 0 && m_dwIPServerKeyUDP == theApp->GetPublicIP() || bForce) {
330 return m_dwServerKeyUDP;
331 } else {
332 return 0;
336 void CServer::SetServerKeyUDP(uint32 dwServerKeyUDP)
338 wxASSERT( theApp->GetPublicIP() != 0 || dwServerKeyUDP == 0 );
339 m_dwServerKeyUDP = dwServerKeyUDP;
340 m_dwIPServerKeyUDP = theApp->GetPublicIP();
342 // File_checked_for_headers