1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 Stu Redman ( sturedman@amule.org )
5 // Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
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.
10 //
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.
15 //
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
24 //
25 
26 //
27 // Implementation of amuleIPV4Address for wxWidgets sockets
28 // (Implementation for Asio is in LibSocketAsio.cpp
29 //
30 
31 #include "LibSocket.h"
32 #include "Logger.h"
33 #include "amuleIPV4Address.h"
34 
35 
36 class CamuleIPV4Endpoint : public wxIPV4address {
37 public:
CamuleIPV4Endpoint()38 	CamuleIPV4Endpoint() {}
39 
CamuleIPV4Endpoint(const CamuleIPV4Endpoint & impl)40 	CamuleIPV4Endpoint(const CamuleIPV4Endpoint & impl) : wxIPV4address(impl) {}
41 
42 ///	operator wxSockAddress& () { return * this; }
43 };
44 
45 
46 
Connect(amuleIPV4Address & adr,bool wait)47 bool CLibSocket::Connect(amuleIPV4Address& adr, bool wait)
48 {
49 	return wxSocketClient::Connect(adr.GetEndpoint(), wait);
50 }
51 
GetPeer(amuleIPV4Address & adr)52 bool CLibSocket::GetPeer(amuleIPV4Address& adr)
53 {
54 	return wxSocketClient::GetPeer(adr.GetEndpoint());
55 }
56 
SetLocal(amuleIPV4Address & local)57 void CLibSocket::SetLocal(amuleIPV4Address& local)
58 {
59 	wxSocketClient::SetLocal(local.GetEndpoint());
60 }
61 
62 
CLibSocketServer(const amuleIPV4Address & address,wxSocketFlags flags)63 CLibSocketServer::CLibSocketServer(const amuleIPV4Address &address,	wxSocketFlags flags) : wxSocketServer(address.GetEndpoint(), flags)
64 {
65 }
66 
CLibUDPSocket(amuleIPV4Address & address,wxSocketFlags flags)67 CLibUDPSocket::CLibUDPSocket(amuleIPV4Address &address, wxSocketFlags flags) : wxDatagramSocket(address.GetEndpoint(), flags)
68 {
69 }
70 
RecvFrom(amuleIPV4Address & addr,void * buf,uint32 nBytes)71 uint32 CLibUDPSocket::RecvFrom(amuleIPV4Address& addr, void* buf, uint32 nBytes)
72 {
73 	wxDatagramSocket::RecvFrom(addr.GetEndpoint(), buf, nBytes);
74 	return wxDatagramSocket::LastCount();
75 }
76 
SendTo(const amuleIPV4Address & addr,const void * buf,uint32 nBytes)77 uint32 CLibUDPSocket::SendTo(const amuleIPV4Address& addr, const void* buf, uint32 nBytes)
78 {
79 	wxDatagramSocket::SendTo(addr.GetEndpoint(), buf, nBytes);
80 	return wxDatagramSocket::LastCount();
81 }
82 
83 
amuleIPV4Address()84 amuleIPV4Address::amuleIPV4Address()
85 {
86 	m_endpoint = new CamuleIPV4Endpoint();
87 }
88 
amuleIPV4Address(const amuleIPV4Address & a)89 amuleIPV4Address::amuleIPV4Address(const amuleIPV4Address &a)
90 {
91 	*this = a;
92 }
93 
~amuleIPV4Address()94 amuleIPV4Address::~amuleIPV4Address()
95 {
96 	delete m_endpoint;
97 }
98 
operator =(const amuleIPV4Address & a)99 amuleIPV4Address& amuleIPV4Address::operator=(const amuleIPV4Address &a)
100 {
101 	m_endpoint = new CamuleIPV4Endpoint(* a.m_endpoint);
102 	return *this;
103 }
104 
Hostname(const wxString & name)105 bool amuleIPV4Address::Hostname(const wxString& name)
106 {
107 	if (name.IsEmpty()) {
108 		return false;
109 	}
110 	return m_endpoint->Hostname(name);
111 }
112 
Service(uint16 service)113 bool amuleIPV4Address::Service(uint16 service)
114 {
115 	if (service == 0) {
116 		return false;
117 	}
118 	return m_endpoint->Service(service);
119 }
120 
Service() const121 uint16 amuleIPV4Address::Service() const
122 {
123 	return m_endpoint->Service();
124 }
125 
IsLocalHost() const126 bool amuleIPV4Address::IsLocalHost() const
127 {
128 	return m_endpoint->IsLocalHost();
129 }
130 
IPAddress() const131 wxString amuleIPV4Address::IPAddress() const
132 {
133 	return m_endpoint->IPAddress();
134 }
135 
136 // Set address to any of the addresses of the current machine.
AnyAddress()137 bool amuleIPV4Address::AnyAddress()
138 {
139 	bool ret = m_endpoint->AnyAddress();
140 	AddDebugLogLineN(logGeneral, CFormat(wxT("AnyAddress() returned %s")) % IPAddress());
141 	return ret;
142 }
143 
GetEndpoint() const144 const CamuleIPV4Endpoint & amuleIPV4Address::GetEndpoint() const
145 {
146 	return * m_endpoint;
147 }
148 
GetEndpoint()149 CamuleIPV4Endpoint & amuleIPV4Address::GetEndpoint()
150 {
151 	return * m_endpoint;
152 }
153