+// ----------------------------------------------------------------------------
+// wxFTP port methods
+// ----------------------------------------------------------------------------
+
+wxSocketBase *wxFTP::GetPort()
+{
+ /*
+ PASSIVE: Client sends a "PASV" to the server. The server responds with
+ an address and port number which it will be listening on. Then
+ the client connects to the server at the specified address and
+ port.
+
+ ACTIVE: Client sends the server a PORT command which includes an
+ address and port number which the client will be listening on.
+ The server then connects to the client at that address and
+ port.
+ */
+
+ wxSocketBase *socket = m_bPassive ? GetPassivePort() : GetActivePort();
+ if ( !socket )
+ {
+ m_bEncounteredError = true;
+ return NULL;
+ }
+
+ // Now set the time for the new socket to the default or user selected
+ // timeout period
+ socket->SetTimeout(m_uiDefaultTimeout);
+
+ return socket;
+}
+
+wxString wxFTP::GetPortCmdArgument(const wxIPV4address& addrLocal,
+ const wxIPV4address& addrNew)
+{
+ // Just fills in the return value with the local IP
+ // address of the current socket. Also it fill in the
+ // PORT which the client will be listening on
+
+ wxString addrIP = addrLocal.IPAddress();
+ int portNew = addrNew.Service();
+
+ // We need to break the PORT number in bytes
+ addrIP.Replace(wxT("."), wxT(","));
+ addrIP << wxT(',')
+ << wxString::Format(wxT("%d"), portNew >> 8) << wxT(',')
+ << wxString::Format(wxT("%d"), portNew & 0xff);
+
+ // Now we have a value like "10,0,0,1,5,23"
+ return addrIP;
+}
+
+wxSocketBase *wxFTP::GetActivePort()
+{
+ // we need an address to listen on
+ wxIPV4address addrNew, addrLocal;
+ GetLocal(addrLocal);
+ addrNew.AnyAddress();
+ addrNew.Service(0); // pick an open port number.
+
+ wxSocketServer *sockSrv = new wxSocketServer(addrNew);
+ if (!sockSrv->IsOk())
+ {
+ // We use IsOk() here to see if everything is ok
+ m_lastError = wxPROTO_PROTERR;
+ delete sockSrv;
+ return NULL;
+ }
+
+ //gets the new address, actually it is just the port number
+ sockSrv->GetLocal(addrNew);
+
+ // Now we create the argument of the PORT command, we send in both
+ // addresses because the addrNew has an IP of "0.0.0.0", so we need the
+ // value in addrLocal
+ wxString port = GetPortCmdArgument(addrLocal, addrNew);
+ if ( !DoSimpleCommand(wxT("PORT"), port) )
+ {
+ m_lastError = wxPROTO_PROTERR;
+ delete sockSrv;
+ wxLogError(_("The FTP server doesn't support the PORT command."));
+ return NULL;
+ }
+
+ m_lastError = wxPROTO_NOERR;
+ sockSrv->Notify(false); // Don't send any events
+ return sockSrv;
+}
+
+wxSocketBase *wxFTP::GetPassivePort()
+{
+ if ( !DoSimpleCommand(wxT("PASV")) )
+ {
+ m_lastError = wxPROTO_PROTERR;
+ wxLogError(_("The FTP server doesn't support passive mode."));
+ return NULL;
+ }
+
+ size_t addrStart = m_lastResult.find(wxT('('));
+ size_t addrEnd = (addrStart == wxString::npos)
+ ? wxString::npos
+ : m_lastResult.find(wxT(')'), addrStart);
+
+ if ( addrEnd == wxString::npos )
+ {
+ m_lastError = wxPROTO_PROTERR;
+ return NULL;
+ }
+
+ // get the port number and address
+ int a[6];
+ wxString straddr(m_lastResult, addrStart + 1, addrEnd - (addrStart + 1));
+ wxSscanf(straddr, wxT("%d,%d,%d,%d,%d,%d"),
+ &a[2],&a[3],&a[4],&a[5],&a[0],&a[1]);
+
+ wxUint32 hostaddr = (wxUint16)a[2] << 24 |
+ (wxUint16)a[3] << 16 |
+ (wxUint16)a[4] << 8 |
+ a[5];
+ wxUint16 port = (wxUint16)(a[0] << 8 | a[1]);
+
+ wxIPV4address addr;
+ addr.Hostname(hostaddr);
+ addr.Service(port);
+
+ wxSocketClient *client = new wxSocketClient();
+ if ( !client->Connect(addr) )
+ {
+ m_lastError = wxPROTO_CONNERR;
+ delete client;
+ return NULL;
+ }
+
+ client->Notify(false);
+
+ m_lastError = wxPROTO_NOERR;
+ return client;
+}
+
+