Debian Codegraph @

netcon/inetfile/CFtp.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/inetfile/CFtp.cpp $
21 * $Revision: 1.3 $
22 * $Date: 2001/01/13 21:48:46 $
23 * $Author: icculus $
24 *
25 * FTP Client class (get only)
26 *
27 * $Log: CFtp.cpp,v $
28 * Revision 1.3 2001/01/13 21:48:46 icculus
29 * patched to (re)compile on win32.
30 *
31 * Revision 1.2 2000/06/03 14:30:21 icculus
32 * 1.4 code merge and pthread->SDL thread conversion.
33 *
34 * Revision 1.1.1.1 2000/04/18 00:00:38 icculus
35 * initial checkin
36 *
37 *
38 * 8 10/21/99 9:27p Jeff
39 * B.A. Macintosh code merge
40 *
41 * 7 9/08/99 6:37p Jeff
42 * fixed http/ftp downloading for Linux, should all work fine now.
43 *
44 * 6 8/22/99 12:32a Jeff
45 * fixed select calls for Linux. Ported Kevin's new http stuff to Linux
46 *
47 * 5 8/21/99 6:48a Jeff
48 * Linux port
49 *
50 * 4 4/14/99 1:20a Jeff
51 * fixed case mismatched #includes
52 *
53 * 3 7/31/98 11:40a Kevin
54 *
55 * 2 6/01/98 10:10a Kevin
56 * Added DLL connection interface and auto update DLL
57 *
58 * 1 5/27/98 9:52a Kevin
59 *
60 * 1 5/25/98 5:31p Kevin
61 * Initial version
62 *
63 * $NoKeywords: $
64 */
65
66 #ifdef WIN32
67 #include <windows.h>
68 #include <winsock.h>
69 #include <process.h>
70 typedef int socklen_t;
71 #endif
72
73 #include <string.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76
77 #if defined(POSIXcxx . . $ `<built-in>:478:9`!)
78 // sorry, I'm lazy, I guess we could copy the defines
79 // that we need to transalte winsock->linux into this header...but no need to now
80 #include "SDL_thread.h"
81 #include "inetgetfile.h"cxx . . $ `<file>/netcon/includes/inetgetfile.h`/
82 #endif
83
84 #include "CFtp.h"cxx . . $ `<file>/netcon/includes/CFtp.h`/
85
86 // MTS: only used in this file?
87 #if defined(POSIXcxx . . $ `<built-in>:478:9`!)
88 int FTPObjThreadcxx . . $ FTPObjThread(dedf1e9b1c614a07).(void *obj)
89 #else
90 void FTPObjThread(void *obj)
91 #endif
92 {
93 ((CFtpGetcxx . . $ CFtpGet# *)obj)->WorkerThreadcxx . . $ CFtpGet#WorkerThread(49f6e7a06ebc5aa8).();
94 #if defined(POSIXcxx . . $ `<built-in>:478:9`!)
95 return 0;
96 #endif
97 }
98
99 void CFtpGetcxx . . $ CFtpGet#::AbortGetcxx . . $ CFtpGet#AbortGet(49f6e7a06ebc5aa8).() {
100 m_Abortingcxx . . $ CFtpGet#m_Aborting. = true;
101 while (!m_Abortedcxx . . $ CFtpGet#m_Aborted.)
102 ; // Wait for the thread to end
103 fclose(LOCALFILEcxx . . $ CFtpGet#LOCALFILE.);
104 }
105
106 CFtpGetcxx . . $ CFtpGet#::CFtpGetcxx . . $ CFtpGet#CFtpGet(c66867f4cf358f2d).(char *URL, char *localfile, char *Username, char *Password) {
107 SOCKADDR_INcxx . . $ `lib/networking.h:219:9`! listensockaddr;
108 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_STARTUPcxx . . $ `netcon/includes/CFtp.h:78:9`!;
109
110 m_ListenSockcxx . . $ CFtpGet#m_ListenSock. = INVALID_SOCKETcxx . . $ `lib/networking.h:221:9`!;
111 m_DataSockcxx . . $ CFtpGet#m_DataSock. = INVALID_SOCKETcxx . . $ `lib/networking.h:221:9`!;
112 m_ControlSockcxx . . $ CFtpGet#m_ControlSock. = INVALID_SOCKETcxx . . $ `lib/networking.h:221:9`!;
113 m_iBytesIncxx . . $ CFtpGet#m_iBytesIn. = 0;
114 m_iBytesTotalcxx . . $ CFtpGet#m_iBytesTotal. = 0;
115 m_Abortingcxx . . $ CFtpGet#m_Aborting. = false;
116 m_Abortedcxx . . $ CFtpGet#m_Aborted. = false;
117
118 LOCALFILEcxx . . $ CFtpGet#LOCALFILE. = fopen(localfile, "wb");
119 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`! == LOCALFILEcxx . . $ CFtpGet#LOCALFILE.) {
120 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_CANT_WRITE_FILEcxx . . $ `netcon/includes/CFtp.h:77:9`!;
121 return;
122 }
123
124 if (Username) {
125 strcpy(m_szUserNamecxx . . $ CFtpGet#m_szUserName., Username);
126 } else {
127 strcpy(m_szUserNamecxx . . $ CFtpGet#m_szUserName., "anonymous");
128 }
129 if (Password) {
130 strcpy(m_szPasswordcxx . . $ CFtpGet#m_szPassword., Password);
131 } else {
132 strcpy(m_szPasswordcxx . . $ CFtpGet#m_szPassword., "[email protected]");
133 }
134 m_ListenSockcxx . . $ CFtpGet#m_ListenSock. = socket(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, 0);
135 if (INVALID_SOCKETcxx . . $ `lib/networking.h:221:9`! == m_ListenSockcxx . . $ CFtpGet#m_ListenSock.) {
136 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
137 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_SOCKET_ERRORcxx . . $ `netcon/includes/CFtp.h:63:9`!;
138 return;
139 } else {
140 listensockaddr.sin_family = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!;
141 listensockaddr.sin_port = 0;
142 listensockaddr.sin_addr.s_addr = INADDR_ANYcxx . . $ `<external>/9581057695539925927/in.h:198:9`!;
143
144 // Bind the listen socket
145 if (bind(m_ListenSockcxx . . $ CFtpGet#m_ListenSock., (SOCKADDRcxx . . $ `lib/networking.h:220:9`! *)&listensockaddr, sizeof(SOCKADDRcxx . . $ `lib/networking.h:220:9`!))) {
146 // Couldn't bind the socket
147 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
148 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_SOCKET_ERRORcxx . . $ `netcon/includes/CFtp.h:63:9`!;
149 return;
150 }
151
152 // Listen for the server connection
153 if (listen(m_ListenSockcxx . . $ CFtpGet#m_ListenSock., 1)) {
154 // Couldn't listen on the socket
155 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
156 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_SOCKET_ERRORcxx . . $ `netcon/includes/CFtp.h:63:9`!;
157 return;
158 }
159 }
160 m_ControlSockcxx . . $ CFtpGet#m_ControlSock. = socket(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, 0);
161 if (INVALID_SOCKETcxx . . $ `lib/networking.h:221:9`! == m_ControlSockcxx . . $ CFtpGet#m_ControlSock.) {
162 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_SOCKET_ERRORcxx . . $ `netcon/includes/CFtp.h:63:9`!;
163 return;
164 }
165 // Parse the URL
166 // Get rid of any extra ftp:// stuff
167 char *pURL = URL;
168 if (strnicmpcxx . . $ `linux/linux_fix.h:44:9`!(URL, "ftp:", 4) == 0) {
169 pURL += 4;
170 while (*pURL == '/') {
171 pURL++;
172 }
173 }
174 // There shouldn't be any : in this string
175 if (strchr(pURL, ':')) {
176 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_URL_PARSING_ERRORcxx . . $ `netcon/includes/CFtp.h:64:9`!;
177 return;
178 }
179 // read the filename by searching backwards for a /
180 // then keep reading until you find the first /
181 // when you found it, you have the host and dir
182 char *filestart = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
183 char *dirstart = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
184 for (int i = strlen(pURL); i >= 0; i--) {
185 if (pURL[i] == '/') {
186 if (!filestart) {
187 filestart = pURL + i + 1;
188 dirstart = pURL + i + 1;
189 strcpy(m_szFilenamecxx . . $ CFtpGet#m_szFilename., filestart);
190 } else {
191 dirstart = pURL + i + 1;
192 }
193 }
194 }
195 if ((dirstart == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) || (filestart == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)) {
196 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_URL_PARSING_ERRORcxx . . $ `netcon/includes/CFtp.h:64:9`!;
197 return;
198 } else {
199 strncpy(m_szDircxx . . $ CFtpGet#m_szDir., dirstart, (filestart - dirstart));
200 m_szDircxx . . $ CFtpGet#m_szDir.[(filestart - dirstart)] = '\0';
201 strncpy(m_szHostcxx . . $ CFtpGet#m_szHost., pURL, (dirstart - pURL));
202 m_szHostcxx . . $ CFtpGet#m_szHost.[(dirstart - pURL) - 1] = '\0';
203 }
204 // At this point we should have a nice host,dir and filename
205
206 #ifdef WIN32
207 if (NULL == _beginthread(FTPObjThread, 0, this)) {
208 m_State = FTP_STATE_INTERNAL_ERROR;
209 return;
210 }
211 #elif defined(POSIXcxx . . $ `<built-in>:478:9`!)
212 // pthread_t thread;
213
214 SDL_Thread *thread;
215
216 if (!inet_LoadThreadLibcxx . . $ inet_LoadThreadLib(c9857a73d68e59ba).()) {
217 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_INTERNAL_ERRORcxx . . $ `netcon/includes/CFtp.h:62:9`!;
218 return;
219 }
220
221 // if(df_pthread_create(&thread,NULL,FTPObjThread,this)!=0)
222 thread = SDL_CreateThread(FTPObjThreadcxx . . $ FTPObjThread(dedf1e9b1c614a07)., "ftpget", this);
223 if (thread == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
224 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_INTERNAL_ERRORcxx . . $ `netcon/includes/CFtp.h:62:9`!;
225 return;
226 }
227 #endif
228 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_CONNECTINGcxx . . $ `netcon/includes/CFtp.h:65:9`!;
229 }
230
231 CFtpGetcxx . . $ CFtpGet#::~cxx . . $ CFtpGet#`~CFtpGet`(ced63f7c635d850d).CFtpGetcxx . . $ CFtpGet#() {
232 if (m_ListenSockcxx . . $ CFtpGet#m_ListenSock. != INVALID_SOCKETcxx . . $ `lib/networking.h:221:9`!) {
233 shutdown(m_ListenSockcxx . . $ CFtpGet#m_ListenSock., 2);
234 #if !defined(POSIXcxx . . $ `<built-in>:478:9`!)
235 closesocket(m_ListenSock);
236 #else
237 close(m_ListenSockcxx . . $ CFtpGet#m_ListenSock.);
238 #endif
239 }
240 if (m_DataSockcxx . . $ CFtpGet#m_DataSock. != INVALID_SOCKETcxx . . $ `lib/networking.h:221:9`!) {
241 shutdown(m_DataSockcxx . . $ CFtpGet#m_DataSock., 2);
242 #if !defined(POSIXcxx . . $ `<built-in>:478:9`!)
243 closesocket(m_DataSock);
244 #else
245 close(m_DataSockcxx . . $ CFtpGet#m_DataSock.);
246 #endif
247 }
248 if (m_ControlSockcxx . . $ CFtpGet#m_ControlSock. != INVALID_SOCKETcxx . . $ `lib/networking.h:221:9`!) {
249 shutdown(m_ControlSockcxx . . $ CFtpGet#m_ControlSock., 2);
250 #if !defined(POSIXcxx . . $ `<built-in>:478:9`!)
251 closesocket(m_ControlSock);
252 #else
253 close(m_ControlSockcxx . . $ CFtpGet#m_ControlSock.);
254 #endif
255 }
256 }
257
258 // Returns a value to specify the status (ie. connecting/connected/transferring/done)
259 int CFtpGetcxx . . $ CFtpGet#::GetStatuscxx . . $ CFtpGet#GetStatus(b126dc7c1de90089).() { return m_Statecxx . . $ CFtpGet#m_State.; }
260
261 uint32_tcxx . . $ uint32_t# CFtpGetcxx . . $ CFtpGet#::GetBytesIncxx . . $ CFtpGet#GetBytesIn(db52760e79bf9054).() { return m_iBytesIncxx . . $ CFtpGet#m_iBytesIn.; }
262
263 uint32_tcxx . . $ uint32_t# CFtpGetcxx . . $ CFtpGet#::GetTotalBytescxx . . $ CFtpGet#GetTotalBytes(db52760e79bf9054).() { return m_iBytesTotalcxx . . $ CFtpGet#m_iBytesTotal.; }
264
265 // This function does all the work -- connects on a blocking socket
266 // then sends the appropriate user and password commands
267 // and then the cwd command, the port command then get and finally the quit
268 void CFtpGetcxx . . $ CFtpGet#::WorkerThreadcxx . . $ CFtpGet#WorkerThread(49f6e7a06ebc5aa8).() {
269 ConnectControlSocketcxx . . $ CFtpGet#ConnectControlSocket(b126dc7c1de90089).();
270 if (m_Statecxx . . $ CFtpGet#m_State. != FTP_STATE_LOGGING_INcxx . . $ `netcon/includes/CFtp.h:68:9`!) {
271 return;
272 }
273 LoginHostcxx . . $ CFtpGet#LoginHost(b126dc7c1de90089).();
274 if (m_Statecxx . . $ CFtpGet#m_State. != FTP_STATE_LOGGED_INcxx . . $ `netcon/includes/CFtp.h:70:9`!) {
275 return;
276 }
277 GetFilecxx . . $ CFtpGet#GetFile(db52760e79bf9054).();
278
279 // We are all done now, and state has the current state.
280 m_Abortedcxx . . $ CFtpGet#m_Aborted. = true;
281 }
282
283 uint32_tcxx . . $ uint32_t# CFtpGetcxx . . $ CFtpGet#::GetFilecxx . . $ CFtpGet#GetFile(db52760e79bf9054).() {
284
285 // Start off by changing into the proper dir.
286 char szCommandString[256];
287 int rcode;
288
289 snprintf(szCommandString, sizeof(szCommandString), "TYPE I\r\n");
290 rcode = SendFTPCommandcxx . . $ CFtpGet#SendFTPCommand(33ab057182e9f89d).(szCommandString);
291 if (rcode >= 400) {
292 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_UNKNOWN_ERRORcxx . . $ `netcon/includes/CFtp.h:75:9`!;
293 return 0;
294 }
295 if (m_Abortingcxx . . $ CFtpGet#m_Aborting.)
296 return 0;
297 if (m_szDircxx . . $ CFtpGet#m_szDir.[0]) {
298 snprintf(szCommandString, sizeof(szCommandString), "CWD %s\r\n", m_szDircxx . . $ CFtpGet#m_szDir.);
299 rcode = SendFTPCommandcxx . . $ CFtpGet#SendFTPCommand(33ab057182e9f89d).(szCommandString);
300 if (rcode >= 400) {
301 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_DIRECTORY_INVALIDcxx . . $ `netcon/includes/CFtp.h:71:9`!;
302 return 0;
303 }
304 }
305 if (m_Abortingcxx . . $ CFtpGet#m_Aborting.)
306 return 0;
307 if (!IssuePortcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).()) {
308 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_UNKNOWN_ERRORcxx . . $ `netcon/includes/CFtp.h:75:9`!;
309 return 0;
310 }
311 if (m_Abortingcxx . . $ CFtpGet#m_Aborting.)
312 return 0;
313 snprintf(szCommandString, sizeof(szCommandString), "RETR %s\r\n", m_szFilenamecxx . . $ CFtpGet#m_szFilename.);
314 rcode = SendFTPCommandcxx . . $ CFtpGet#SendFTPCommand(33ab057182e9f89d).(szCommandString);
315 if (rcode >= 400) {
316 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_FILE_NOT_FOUNDcxx . . $ `netcon/includes/CFtp.h:72:9`!;
317 return 0;
318 }
319 if (m_Abortingcxx . . $ CFtpGet#m_Aborting.)
320 return 0;
321 // Now we will try to determine the file size...
322 char *p, *s;
323 p = strchr(recv_buffercxx . . $ CFtpGet#recv_buffer., '(');
324 if (p) {
325 p++;
326 s = strchr(p, ' ');
327 *s = '\0';
328 m_iBytesTotalcxx . . $ CFtpGet#m_iBytesTotal. = atoicxx . . $ atoi(f5ecf3191ed0cd93).(p);
329 }
330 if (m_Abortingcxx . . $ CFtpGet#m_Aborting.)
331 return 0;
332
333 m_DataSockcxx . . $ CFtpGet#m_DataSock. = accept(m_ListenSockcxx . . $ CFtpGet#m_ListenSock., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); //(SOCKADDR *)&sockaddr,&iAddrLength);
334 // Close the listen socket
335 #if !defined(POSIXcxx . . $ `<built-in>:478:9`!)
336 closesocket(m_ListenSock);
337 #else
338 close(m_ListenSockcxx . . $ CFtpGet#m_ListenSock.);
339 #endif
340 if (m_DataSockcxx . . $ CFtpGet#m_DataSock. == INVALID_SOCKETcxx . . $ `lib/networking.h:221:9`!) {
341 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
342 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_SOCKET_ERRORcxx . . $ `netcon/includes/CFtp.h:63:9`!;
343 return 0;
344 }
345 if (m_Abortingcxx . . $ CFtpGet#m_Aborting.)
346 return 0;
347
348 ReadDataChannelcxx . . $ CFtpGet#ReadDataChannel(db52760e79bf9054).();
349
350 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_FILE_RECEIVEDcxx . . $ `netcon/includes/CFtp.h:74:9`!;
351 return 1;
352 }
353
354 uint32_tcxx . . $ uint32_t# CFtpGetcxx . . $ CFtpGet#::IssuePortcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).() {
355
356 char szCommandString[200];
357 SOCKADDR_INcxx . . $ `lib/networking.h:219:9`! listenaddr; // Socket address structure
358 socklen_t iLength; // Length of the address structure
359 uint32_tcxx . . $ uint32_t# nLocalPort; // Local port for listening
360 uint32_tcxx . . $ uint32_t# nReplyCode; // FTP server reply code
361
362 // Get the address for the hListenSocket
363 iLength = sizeof(listenaddr);
364 if (getsockname(m_ListenSockcxx . . $ CFtpGet#m_ListenSock., (SOCKADDRcxx . . $ `lib/networking.h:220:9`! *)&listenaddr, &iLength) == SOCKET_ERRORcxx . . $ `lib/networking.h:233:9`!) {
365 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
366 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_SOCKET_ERRORcxx . . $ `netcon/includes/CFtp.h:63:9`!;
367 return 0;
368 }
369
370 // Extract the local port from the hListenSocket
371 nLocalPort = listenaddr.sin_port;
372
373 // Now, reuse the socket address structure to
374 // get the IP address from the control socket.
375 if (getsockname(m_ControlSockcxx . . $ CFtpGet#m_ControlSock., (SOCKADDRcxx . . $ `lib/networking.h:220:9`! *)&listenaddr, &iLength) == SOCKET_ERRORcxx . . $ `lib/networking.h:233:9`!) {
376 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
377 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_SOCKET_ERRORcxx . . $ `netcon/includes/CFtp.h:63:9`!;
378 return 0;
379 }
380
381 #ifdef WIN32
382 // Format the PORT command with the correct numbers.
383 sprintf(szCommandString, "PORT %d,%d,%d,%d,%d,%d\r\n", listenaddr.sin_addr.S_un.S_un_b.s_b1,
384 listenaddr.sin_addr.S_un.S_un_b.s_b2, listenaddr.sin_addr.S_un.S_un_b.s_b3,
385 listenaddr.sin_addr.S_un.S_un_b.s_b4, nLocalPort & 0xFF, nLocalPort >> 8);
386 #else
387 unioncxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0# {
388 structcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_1# {
389 uint8_tcxx . . $ uint8_t# s_b1cxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_1#s_b1., s_b2cxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_1#s_b2., s_b3cxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_1#s_b3., s_b4cxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_1#s_b4.;
390 } S_un_bcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#S_un_b.;
391 structcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_2# {
392 uint16_tcxx . . $ uint16_t# s_w1cxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_2#s_w1., s_w2cxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_2#s_w2.;
393 } S_un_wcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#S_un_w.;
394 uint32_tcxx . . $ uint32_t# S_addrcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#S_addr.;
395 } S_un;
396
397 S_un.S_addrcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#S_addr. = (uint32_tcxx . . $ uint32_t#)listenaddr.sin_addr.s_addr;
398 // Format the PORT command with the correct numbers.
399 snprintf(szCommandString, sizeof(szCommandString), "PORT %d,%d,%d,%d,%d,%d\r\n", S_un.S_un_bcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#S_un_b..s_b1cxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_1#s_b1., S_un.S_un_bcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#S_un_b..s_b2cxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_1#s_b2.,
400 S_un.S_un_bcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#S_un_b..s_b3cxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_1#s_b3., S_un.S_un_bcxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#S_un_b..s_b4cxx . . $ CFtpGet#IssuePort(db52760e79bf9054).$anonymous_type_0#$anonymous_type_1#s_b4., nLocalPort & 0xFF, nLocalPort >> 8);
401 #endif
402
403 // Tell the server which port to use for data.
404 nReplyCode = SendFTPCommandcxx . . $ CFtpGet#SendFTPCommand(33ab057182e9f89d).(szCommandString);
405 if (nReplyCode != 200) {
406 #if defined(POSIXcxx . . $ `<built-in>:478:9`!)
407 // I don't know if this is just Linux or do to a bug I fixed while porting to linux
408 // for some reason I kept getting reply 250 here and have to read again to get the
409 // "200 PORT Command OK" or whatever
410 if (nReplyCode != 250 || (ReadFTPServerReplycxx . . $ CFtpGet#ReadFTPServerReply(db52760e79bf9054).() != 200)) // ummmmmmmm
411 #endif
412 {
413 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
414 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_SOCKET_ERRORcxx . . $ `netcon/includes/CFtp.h:63:9`!;
415 return 0;
416 }
417 }
418
419 return 1;
420 }
421
422 int CFtpGetcxx . . $ CFtpGet#::ConnectControlSocketcxx . . $ CFtpGet#ConnectControlSocket(b126dc7c1de90089).() {
423 HOSTENTcxx . . $ `lib/networking.h:232:9`! *he;
424 SERVENTcxx . . $ `netcon/includes/inetgetfile.h:118:9`! *se;
425 SOCKADDR_INcxx . . $ `lib/networking.h:219:9`! hostaddr;
426 he = gethostbyname(m_szHostcxx . . $ CFtpGet#m_szHost.);
427 if (he == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
428 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_HOST_NOT_FOUNDcxx . . $ `netcon/includes/CFtp.h:66:9`!;
429 return 0;
430 }
431 // m_ControlSock
432 if (m_Abortingcxx . . $ CFtpGet#m_Aborting.)
433 return 0;
434 se = getservbyname("ftp", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
435
436 if (se == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
437 hostaddr.sin_port = htons(21);
438 } else {
439 hostaddr.sin_port = se->s_port;
440 }
441 hostaddr.sin_family = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!;
442 memcpy(&hostaddr.sin_addr, he->h_addr_list[0], 4);
443 if (m_Abortingcxx . . $ CFtpGet#m_Aborting.)
444 return 0;
445 // Now we will connect to the host
446 if (connect(m_ControlSockcxx . . $ CFtpGet#m_ControlSock., (SOCKADDRcxx . . $ `lib/networking.h:220:9`! *)&hostaddr, sizeof(SOCKADDRcxx . . $ `lib/networking.h:220:9`!))) {
447 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
448 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_CANT_CONNECTcxx . . $ `netcon/includes/CFtp.h:67:9`!;
449 return 0;
450 }
451 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_LOGGING_INcxx . . $ `netcon/includes/CFtp.h:68:9`!;
452 return 1;
453 }
454
455 int CFtpGetcxx . . $ CFtpGet#::LoginHostcxx . . $ CFtpGet#LoginHost(b126dc7c1de90089).() {
456 char szLoginString[200];
457 int rcode;
458
459 snprintf(szLoginString, sizeof(szLoginString), "USER %s\r\n", m_szUserNamecxx . . $ CFtpGet#m_szUserName.);
460 rcode = SendFTPCommandcxx . . $ CFtpGet#SendFTPCommand(33ab057182e9f89d).(szLoginString);
461 if (rcode >= 400) {
462 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_LOGIN_ERRORcxx . . $ `netcon/includes/CFtp.h:69:9`!;
463 return 0;
464 }
465 snprintf(szLoginString, sizeof(szLoginString), "PASS %s\r\n", m_szPasswordcxx . . $ CFtpGet#m_szPassword.);
466 rcode = SendFTPCommandcxx . . $ CFtpGet#SendFTPCommand(33ab057182e9f89d).(szLoginString);
467 if (rcode >= 400) {
468 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_LOGIN_ERRORcxx . . $ `netcon/includes/CFtp.h:69:9`!;
469 return 0;
470 }
471
472 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_LOGGED_INcxx . . $ `netcon/includes/CFtp.h:70:9`!;
473 return 1;
474 }
475
476 uint32_tcxx . . $ uint32_t# CFtpGetcxx . . $ CFtpGet#::SendFTPCommandcxx . . $ CFtpGet#SendFTPCommand(33ab057182e9f89d).(char *command) {
477
478 FlushControlChannelcxx . . $ CFtpGet#FlushControlChannel(49f6e7a06ebc5aa8).();
479 // Send the FTP command
480 if (SOCKET_ERRORcxx . . $ `lib/networking.h:233:9`! == (send(m_ControlSockcxx . . $ CFtpGet#m_ControlSock., command, strlen(command), 0))) {
481 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
482 // Return 999 to indicate an error has occurred
483 return (999);
484 }
485
486 // Read the server's reply and return the reply code as an integer
487 return (ReadFTPServerReplycxx . . $ CFtpGet#ReadFTPServerReply(db52760e79bf9054).());
488 }
489
490 uint32_tcxx . . $ uint32_t# CFtpGetcxx . . $ CFtpGet#::ReadFTPServerReplycxx . . $ CFtpGet#ReadFTPServerReply(db52760e79bf9054).() {
491 uint32_tcxx . . $ uint32_t# rcode;
492 uint32_tcxx . . $ uint32_t# iBytesRead;
493 char chunk[2];
494 char szcode[5];
495 uint32_tcxx . . $ uint32_t# igotcrlf = 0;
496 memset(recv_buffercxx . . $ CFtpGet#recv_buffer., 0, 1000);
497 do {
498 chunk[0] = '\0';
499 iBytesRead = recv(m_ControlSockcxx . . $ CFtpGet#m_ControlSock., chunk, 1, 0);
500
501 if (iBytesRead == SOCKET_ERRORcxx . . $ `lib/networking.h:233:9`!) {
502 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
503 #if defined(POSIXcxx . . $ `<built-in>:478:9`!)
504 if (0 == iWinsockErr) {
505 continue;
506 }
507 #endif
508 // Return 999 to indicate an error has occurred
509 return (999);
510 }
511
512 if ((chunk[0] == 0x0a) || (chunk[0] == 0x0d)) {
513 if (recv_buffercxx . . $ CFtpGet#recv_buffer.[0] != '\0') {
514 igotcrlf = 1;
515 }
516 } else {
517 chunk[1] = '\0';
518 strcat(recv_buffercxx . . $ CFtpGet#recv_buffer., chunk);
519 }
520
521 } while (igotcrlf == 0);
522
523 if (recv_buffercxx . . $ CFtpGet#recv_buffer.[3] == '-') {
524 // Hack -- must be a MOTD
525 return ReadFTPServerReplycxx . . $ CFtpGet#ReadFTPServerReply(db52760e79bf9054).();
526 }
527 if (recv_buffercxx . . $ CFtpGet#recv_buffer.[3] != ' ') {
528 // We should have 3 numbers then a space
529 return 999;
530 }
531 memcpy(szcode, recv_buffercxx . . $ CFtpGet#recv_buffer., 3);
532 szcode[3] = '\0';
533 rcode = atoicxx . . $ atoi(f5ecf3191ed0cd93).(szcode);
534 // Extract the reply code from the server reply and return as an integer
535 return (rcode);
536 }
537
538 uint32_tcxx . . $ uint32_t# CFtpGetcxx . . $ CFtpGet#::ReadDataChannelcxx . . $ CFtpGet#ReadDataChannel(db52760e79bf9054).() {
539 char sDataBuffer[4096]; // Data-storage buffer for the data channel
540 int nBytesRecv; // Bytes received from the data channel
541 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_RECEIVINGcxx . . $ `netcon/includes/CFtp.h:73:9`!;
542 if (m_Abortingcxx . . $ CFtpGet#m_Aborting.)
543 return 0;
544 do {
545 if (m_Abortingcxx . . $ CFtpGet#m_Aborting.)
546 return 0;
547 nBytesRecv = recv(m_DataSockcxx . . $ CFtpGet#m_DataSock., (char *)&sDataBuffer, sizeof(sDataBuffer), 0);
548
549 #if defined(POSIXcxx . . $ `<built-in>:478:9`!)
550 if (nBytesRecv == -1) {
551 int iWinsockErr = WSAGetLastErrorcxx . . $ `netcon/includes/inetgetfile.h:129:9`!();
552 if (iWinsockErr == 0) {
553 nBytesRecv = 1;
554 continue;
555 }
556 }
557 #endif
558
559 m_iBytesIncxx . . $ CFtpGet#m_iBytesIn. += nBytesRecv;
560 if (nBytesRecv > 0) {
561 fwrite(sDataBuffer, nBytesRecv, 1, LOCALFILEcxx . . $ CFtpGet#LOCALFILE.);
562 // Write sDataBuffer, nBytesRecv
563 }
564 } while (nBytesRecv > 0);
565 fclose(LOCALFILEcxx . . $ CFtpGet#LOCALFILE.);
566 // Close the file and check for error returns.
567 if (nBytesRecv == SOCKET_ERRORcxx . . $ `lib/networking.h:233:9`!) {
568 // Ok, we got a socket error -- xfer aborted?
569 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_RECV_FAILEDcxx . . $ `netcon/includes/CFtp.h:76:9`!;
570 return 0;
571 } else {
572 // done!
573 m_Statecxx . . $ CFtpGet#m_State. = FTP_STATE_FILE_RECEIVEDcxx . . $ `netcon/includes/CFtp.h:74:9`!;
574 return 1;
575 }
576 }
577
578 void CFtpGetcxx . . $ CFtpGet#::FlushControlChannelcxx . . $ CFtpGet#FlushControlChannel(49f6e7a06ebc5aa8).() {
579 fd_set read_fds;
580 TIMEVALcxx . . $ `netcon/includes/inetgetfile.h:119:9`! timeout;
581 int bytesin = 0;
582 char flushbuff[3];
583
584 timeout.tv_sec = 0;
585 timeout.tv_usec = 0;
586
587 FD_ZEROcxx . . $ `<external>/946878812840035423/select.h:88:9`!(&read_fds);
588 FD_SETcxx . . $ `<external>/946878812840035423/select.h:85:9`!(m_ControlSock, &read_fds);
589
590 while (select(m_ControlSockcxx . . $ CFtpGet#m_ControlSock. + 1, &read_fds, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, &timeout)) {
591 recv(m_ControlSockcxx . . $ CFtpGet#m_ControlSock., flushbuff, 1, 0);
592 }
593 }