- wxIPV4address addr;
- wxSocketClient *client;
- int a[6];
- wxString straddr;
- int addr_pos;
- wxUint16 port;
- wxUint32 hostaddr;
-
- if (!SendCommand(wxT("PASV"), '2'))
- return NULL;
-
- addr_pos = m_lastResult.Find(wxT('('));
- if (addr_pos == -1) {
- m_lastError = wxPROTO_PROTERR;
- return NULL;
- }
- straddr = m_lastResult(addr_pos+1, m_lastResult.Length());
- wxSscanf((const wxChar *)straddr,wxT("%d,%d,%d,%d,%d,%d"),&a[2],&a[3],&a[4],&a[5],&a[0],&a[1]);
-
- hostaddr = (wxUint16)a[5] << 24 | (wxUint16)a[4] << 16 |
- (wxUint16)a[3] << 8 | a[2];
- addr.Hostname(hostaddr);
-
- port = (wxUint16)a[0] << 8 | a[1];
- addr.Service(port);
-
- client = new wxSocketClient();
- if (!client->Connect(addr)) {
- delete client;
- return NULL;
- }
- client->Notify(FALSE);
-
- return client;
+ // 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->Ok())
+ {
+ // We use Ok() 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(_T("PORT "), port) )
+ {
+ m_lastError = wxPROTO_PROTERR;
+ delete sockSrv;
+ wxLogError(_("The FTP server doesn't support the PORT command."));
+ return NULL;
+ }
+
+ sockSrv->Notify(false); // Don't send any events
+ return sockSrv;
+}
+
+wxSocketBase *wxFTP::GetPassivePort()
+{
+ if ( !DoSimpleCommand(_T("PASV")) )
+ {
+ wxLogError(_("The FTP server doesn't support passive mode."));
+ return NULL;
+ }
+
+ const wxChar *addrStart = wxStrchr(m_lastResult, _T('('));
+ const wxChar *addrEnd = addrStart ? wxStrchr(addrStart, _T(')')) : NULL;
+ if ( !addrEnd )
+ {
+ m_lastError = wxPROTO_PROTERR;
+
+ return NULL;
+ }
+
+ // get the port number and address
+ int a[6];
+ wxString straddr(addrStart + 1, addrEnd);
+ 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) )
+ {
+ delete client;
+ return NULL;
+ }
+
+ client->Notify(false);
+
+ return client;