| 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/inetgetfile.cpp $ |
| 21 | * $Revision: 1.2 $ |
| 22 | * $Date: 2000/06/03 14:33:51 $ |
| 23 | * $Author: icculus $ |
| 24 | * |
| 25 | * InternetGetFile Class |
| 26 | * |
| 27 | * $Log: inetgetfile.cpp,v $ |
| 28 | * Revision 1.2 2000/06/03 14:33:51 icculus |
| 29 | * Merge with Outrage 1.4 tree... |
| 30 | * |
| 31 | * Revision 1.1.1.1 2000/04/18 00:00:38 icculus |
| 32 | * initial checkin |
| 33 | * |
| 34 | * |
| 35 | * 10 10/21/99 9:28p Jeff |
| 36 | * B.A. Macintosh code merge |
| 37 | * |
| 38 | * 9 9/08/99 6:37p Jeff |
| 39 | * fixed http/ftp downloading for Linux, should all work fine now. |
| 40 | * |
| 41 | * 8 8/23/99 5:12p Kevin |
| 42 | * Proxy support for http |
| 43 | * |
| 44 | * 7 8/21/99 6:48a Jeff |
| 45 | * Linux port |
| 46 | * |
| 47 | * 6 4/14/99 1:20a Jeff |
| 48 | * fixed case mismatched #includes |
| 49 | * |
| 50 | * 5 7/31/98 11:57a Kevin |
| 51 | * Added new functions for getting state |
| 52 | * |
| 53 | * 4 7/31/98 11:40a Kevin |
| 54 | * |
| 55 | * 3 7/31/98 11:17a Nate |
| 56 | * Fixed memory leak and added memory alloc checking. |
| 57 | * |
| 58 | * 2 6/01/98 10:10a Kevin |
| 59 | * Added DLL connection interface and auto update DLL |
| 60 | * |
| 61 | * 1 5/27/98 9:52a Kevin |
| 62 | * |
| 63 | * 1 5/25/98 5:31p Kevin |
| 64 | * Initial version |
| 65 | * |
| 66 | * $NoKeywords: $ |
| 67 | */ |
| 68 | |
| 69 | #if defined(WIN32) |
| 70 | #include <windows.h> |
| 71 | #endif |
| 72 | |
| 73 | #include <stdio.h> |
| 74 | #include <stdlib.h> |
| 75 | #include <string.h> |
| 76 | |
| 77 | |
| 78 | // include inetgetfile.h before CFtp.h and Chttpget.h because it has some winsock defines |
| 79 | // to make Linux happy |
| 80 | #include "inetgetfile.h" |
| 81 | #include "CFtp.h" |
| 82 | #include "Chttpget.h" |
| 83 | |
| 84 | extern char *Proxy_server; |
| 85 | extern int16_t Proxy_port; |
| 86 | |
| 87 | #if defined(POSIX) |
| 88 | /* ryan sez: use SDL. |
| 89 | pthread_create_fp df_pthread_create = NULL; |
| 90 | pthread_exit_fp df_pthread_exit = NULL; |
| 91 | pthread_detach_fp df_pthread_detach = NULL; |
| 92 | pthread_self_fp df_pthread_self = NULL; |
| 93 | */ |
| 94 | bool inet_LoadThreadLib(void) { |
| 95 | return true; |
| 96 | /* |
| 97 | if(df_pthread_create) |
| 98 | return true; |
| 99 | |
| 100 | void *lib; |
| 101 | |
| 102 | lib = dlopen("libpthread.so",RTLD_GLOBAL|RTLD_NOW); |
| 103 | if(!lib) |
| 104 | { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | df_pthread_create = (pthread_create_fp)dlsym(lib,"pthread_create"); |
| 109 | df_pthread_detach = (pthread_detach_fp)dlsym(lib,"pthread_detach"); |
| 110 | df_pthread_self = (pthread_self_fp)dlsym(lib,"pthread_self"); |
| 111 | |
| 112 | if(!df_pthread_create || !df_pthread_detach || !df_pthread_self) |
| 113 | { |
| 114 | df_pthread_create = NULL; //this variable I use to see if the library is loaded |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | // don't close the library...we need it open for future use |
| 119 | // and we can't close it on atexit() because linux will segfault for some stupid reason |
| 120 | return true; |
| 121 | */ |
| 122 | } |
| 123 | #endif |
| 124 | |
| 125 | void InetGetFile::AbortGet() { |
| 126 | if (m_bUseHTTP) { |
| 127 | http->AbortGet(); |
| 128 | } else { |
| 129 | ftp->AbortGet(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | InetGetFile::InetGetFile(char *URL, char *localfile, char *proxyip, int16_t proxyport) { |
| 134 | m_HardError = 0; |
| 135 | http = NULL; |
| 136 | ftp = NULL; |
| 137 | if ((URL == NULL) || (localfile == NULL)) { |
| 138 | m_HardError = INET_ERROR_BADPARMS; |
| 139 | } |
| 140 | if (strstr(URL, "http:")) { |
| 141 | m_bUseHTTP = true; |
| 142 | http = new ChttpGet(URL, localfile, proxyip, proxyport); |
| 143 | if (http == NULL) |
| 144 | m_HardError = INET_ERROR_NO_MEMORY; |
| 145 | } else if (strstr(URL, "HTTP:")) { |
| 146 | m_bUseHTTP = true; |
| 147 | http = new ChttpGet(URL, localfile, proxyip, proxyport); |
| 148 | if (http == NULL) |
| 149 | m_HardError = INET_ERROR_NO_MEMORY; |
| 150 | } else if (strstr(URL, "FTP:")) { |
| 151 | m_bUseHTTP = false; |
| 152 | ftp = new CFtpGet(URL, localfile); |
| 153 | if (ftp == NULL) |
| 154 | m_HardError = INET_ERROR_NO_MEMORY; |
| 155 | } else if (strstr(URL, "ftp:")) { |
| 156 | m_bUseHTTP = false; |
| 157 | ftp = new CFtpGet(URL, localfile); |
| 158 | if (ftp == NULL) |
| 159 | m_HardError = INET_ERROR_NO_MEMORY; |
| 160 | } else { |
| 161 | m_HardError = INET_ERROR_CANT_PARSE_URL; |
| 162 | } |
| 163 | Sleep(1000); |
| 164 | } |
| 165 | |
| 166 | InetGetFile::InetGetFile(char *URL, char *localfile) { |
| 167 | m_HardError = 0; |
| 168 | http = NULL; |
| 169 | ftp = NULL; |
| 170 | if ((URL == NULL) || (localfile == NULL)) { |
| 171 | m_HardError = INET_ERROR_BADPARMS; |
| 172 | } |
| 173 | if (strstr(URL, "http:")) { |
| 174 | m_bUseHTTP = true; |
| 175 | http = new ChttpGet(URL, localfile); |
| 176 | if (http == NULL) |
| 177 | m_HardError = INET_ERROR_NO_MEMORY; |
| 178 | } else if (strstr(URL, "HTTP:")) { |
| 179 | m_bUseHTTP = true; |
| 180 | http = new ChttpGet(URL, localfile); |
| 181 | if (http == NULL) |
| 182 | m_HardError = INET_ERROR_NO_MEMORY; |
| 183 | } else if (strstr(URL, "FTP:")) { |
| 184 | m_bUseHTTP = false; |
| 185 | ftp = new CFtpGet(URL, localfile); |
| 186 | if (ftp == NULL) |
| 187 | m_HardError = INET_ERROR_NO_MEMORY; |
| 188 | } else if (strstr(URL, "ftp:")) { |
| 189 | m_bUseHTTP = false; |
| 190 | ftp = new CFtpGet(URL, localfile); |
| 191 | if (ftp == NULL) |
| 192 | m_HardError = INET_ERROR_NO_MEMORY; |
| 193 | } else { |
| 194 | m_HardError = INET_ERROR_CANT_PARSE_URL; |
| 195 | } |
| 196 | Sleep(1000); |
| 197 | } |
| 198 | |
| 199 | InetGetFile::~InetGetFile() { |
| 200 | if (http != NULL) |
| 201 | delete http; |
| 202 | if (ftp != NULL) |
| 203 | delete ftp; |
| 204 | } |
| 205 | |
| 206 | BOOL InetGetFile::IsConnecting() { |
| 207 | int state; |
| 208 | if (m_bUseHTTP) { |
| 209 | state = http->GetStatus(); |
| 210 | } else if (ftp) { |
| 211 | state = ftp->GetStatus(); |
| 212 | } else { |
| 213 | return false; |
| 214 | } |
| 215 | if (state == FTP_STATE_CONNECTING) { |
| 216 | return true; |
| 217 | } else { |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | BOOL InetGetFile::IsReceiving() { |
| 223 | int state = 0; |
| 224 | if (m_bUseHTTP) { |
| 225 | state = http->GetStatus(); |
| 226 | } else if (ftp) { |
| 227 | state = ftp->GetStatus(); |
| 228 | } |
| 229 | if (state == FTP_STATE_RECEIVING) { |
| 230 | return true; |
| 231 | } else { |
| 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | BOOL InetGetFile::IsFileReceived() { |
| 237 | int state = 0; |
| 238 | if (m_bUseHTTP) { |
| 239 | state = http->GetStatus(); |
| 240 | } else if (ftp) { |
| 241 | state = ftp->GetStatus(); |
| 242 | } |
| 243 | if (state == FTP_STATE_FILE_RECEIVED) { |
| 244 | return true; |
| 245 | } else { |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | BOOL InetGetFile::IsFileError() { |
| 251 | int state = FTP_STATE_INTERNAL_ERROR; |
| 252 | if (m_HardError) |
| 253 | return true; |
| 254 | if (m_bUseHTTP) { |
| 255 | state = http->GetStatus(); |
| 256 | } else if (ftp) { |
| 257 | state = ftp->GetStatus(); |
| 258 | } |
| 259 | switch (state) { |
| 260 | |
| 261 | case FTP_STATE_URL_PARSING_ERROR: |
| 262 | case FTP_STATE_HOST_NOT_FOUND: |
| 263 | case FTP_STATE_DIRECTORY_INVALID: |
| 264 | case FTP_STATE_FILE_NOT_FOUND: |
| 265 | case FTP_STATE_CANT_CONNECT: |
| 266 | case FTP_STATE_LOGIN_ERROR: |
| 267 | case FTP_STATE_INTERNAL_ERROR: |
| 268 | case FTP_STATE_SOCKET_ERROR: |
| 269 | case FTP_STATE_UNKNOWN_ERROR: |
| 270 | case FTP_STATE_RECV_FAILED: |
| 271 | case FTP_STATE_CANT_WRITE_FILE: |
| 272 | return true; |
| 273 | case FTP_STATE_CONNECTING: |
| 274 | return false; |
| 275 | default: |
| 276 | return false; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | int InetGetFile::GetErrorCode() { |
| 281 | int state = FTP_STATE_INTERNAL_ERROR; |
| 282 | if (m_HardError) |
| 283 | return m_HardError; |
| 284 | if (m_bUseHTTP) { |
| 285 | state = http->GetStatus(); |
| 286 | } else if (ftp) { |
| 287 | state = ftp->GetStatus(); |
| 288 | } |
| 289 | switch (state) { |
| 290 | |
| 291 | case FTP_STATE_URL_PARSING_ERROR: |
| 292 | return INET_ERROR_CANT_PARSE_URL; |
| 293 | |
| 294 | case FTP_STATE_HOST_NOT_FOUND: |
| 295 | return INET_ERROR_HOST_NOT_FOUND; |
| 296 | |
| 297 | case FTP_STATE_DIRECTORY_INVALID: |
| 298 | case FTP_STATE_FILE_NOT_FOUND: |
| 299 | return INET_ERROR_BAD_FILE_OR_DIR; |
| 300 | |
| 301 | case FTP_STATE_CANT_CONNECT: |
| 302 | case FTP_STATE_LOGIN_ERROR: |
| 303 | case FTP_STATE_INTERNAL_ERROR: |
| 304 | case FTP_STATE_SOCKET_ERROR: |
| 305 | case FTP_STATE_UNKNOWN_ERROR: |
| 306 | case FTP_STATE_RECV_FAILED: |
| 307 | |
| 308 | return INET_ERROR_UNKNOWN_ERROR; |
| 309 | |
| 310 | case FTP_STATE_CANT_WRITE_FILE: |
| 311 | return INET_ERROR_CANT_WRITE_FILE; |
| 312 | default: |
| 313 | return INET_ERROR_NO_ERROR; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | int InetGetFile::GetTotalBytes() { |
| 318 | if (m_bUseHTTP) { |
| 319 | return http->GetTotalBytes(); |
| 320 | } else if (ftp) { |
| 321 | return ftp->GetTotalBytes(); |
| 322 | } else |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | int InetGetFile::GetBytesIn() { |
| 327 | if (m_bUseHTTP) { |
| 328 | return http->GetBytesIn(); |
| 329 | } else if (ftp) { |
| 330 | return ftp->GetBytesIn(); |
| 331 | } else |
| 332 | return 0; |
| 333 | } |