| |
| |
|
|
| |
| |
| |
| |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifdef HAVE_NET_SERVICES |
| #include <string.h> |
|
|
| #include <sys/types.h> |
| #include <netinet/in.h> |
| #include <netinet/tcp.h> |
| #include <sys/socket.h> |
| #include <arpa/inet.h> |
| #include <netdb.h> |
| #include <errno.h> |
| #include <stdio.h> |
| #include <string.h> |
| #include <stdlib.h> |
|
|
| #ifdef CFITSIO_HAVE_CURL |
| #include <curl/curl.h> |
| #endif |
|
|
| #if defined(unix) || defined(__unix__) || defined(__unix) || defined(HAVE_UNISTD_H) |
| #include <unistd.h> |
| #endif |
|
|
| #include <signal.h> |
| #include <setjmp.h> |
| #include "fitsio2.h" |
|
|
| static jmp_buf env; |
| static void signal_handler(int sig); |
|
|
| |
| #define NET_OK 0 |
| #define NOT_INET_ADDRESS -1000 |
| #define UNKNOWN_INET_HOST -1001 |
| #define CONNECTION_ERROR -1002 |
|
|
| |
| #define NET_DEFAULT 0 |
| #define NET_OOB 1 |
| #define NET_PEEK 2 |
|
|
| #define NETTIMEOUT 180 |
|
|
| |
| #define MAXLEN 1200 |
| #define SHORTLEN 100 |
| static char netoutfile[MAXLEN]; |
|
|
|
|
| #define ROOTD_USER 2000 |
| #define ROOTD_PASS 2001 |
| #define ROOTD_AUTH 2002 |
| #define ROOTD_FSTAT 2003 |
| #define ROOTD_OPEN 2004 |
| #define ROOTD_PUT 2005 |
| #define ROOTD_GET 2006 |
| #define ROOTD_FLUSH 2007 |
| #define ROOTD_CLOSE 2008 |
| #define ROOTD_STAT 2009 |
| #define ROOTD_ACK 2010 |
| #define ROOTD_ERR 2011 |
|
|
| typedef struct /* structure containing disk file structure */ |
| { |
| int sock; |
| LONGLONG currentpos; |
| } rootdriver; |
|
|
| typedef struct /* simple mem struct for receiving files from curl */ |
| { |
| char *memory; |
| size_t size; |
| } curlmembuf; |
|
|
| static rootdriver handleTable[NMAXFILES]; |
|
|
| |
|
|
| static int NET_TcpConnect(char *hostname, int port); |
| static int NET_SendRaw(int sock, const void *buf, int length, int opt); |
| static int NET_RecvRaw(int sock, void *buffer, int length); |
| static int NET_ParseUrl(const char *url, char *proto, char *host, int *port, |
| char *fn); |
| static int CreateSocketAddress(struct sockaddr_in *sockaddrPtr, |
| char *host,int port); |
| static int ftp_status(FILE *ftp, char *statusstr); |
| static int http_open_network(char *url, FILE **httpfile, char *contentencoding, |
| int *contentlength); |
| static int https_open_network(char *filename, curlmembuf* buffer); |
| static int ftp_open_network(char *url, FILE **ftpfile, FILE **command, |
| int *sock); |
| static int ftp_file_exist(char *url); |
| static int root_send_buffer(int sock, int op, char *buffer, int buflen); |
| static int root_recv_buffer(int sock, int *op, char *buffer,int buflen); |
| static int root_openfile(char *filename, char *rwmode, int *sock); |
| static int encode64(unsigned s_len, char *src, unsigned d_len, char *dst); |
| static size_t curlToMemCallback(void *buffer, size_t size, size_t nmemb, void *userp); |
|
|
| |
| |
|
|
| static int closehttpfile; |
| static int closememfile; |
| static int closefdiskfile; |
| static int closediskfile; |
| static int closefile; |
| static int closeoutfile; |
| static int closecommandfile; |
| static int closeftpfile; |
| static FILE *diskfile; |
| static FILE *outfile; |
|
|
| static int curl_verbose=0; |
|
|
| |
| |
| |
|
|
| int http_open(char *filename, int rwmode, int *handle) |
| { |
|
|
| FILE *httpfile; |
| char contentencoding[SHORTLEN]; |
| char errorstr[MAXLEN]; |
| char recbuf[MAXLEN]; |
| long len; |
| int contentlength; |
| int status; |
| char firstchar; |
|
|
| closehttpfile = 0; |
| closememfile = 0; |
|
|
| |
| if (rwmode != 0) { |
| ffpmsg("Can't open http:// type file with READWRITE access"); |
| ffpmsg(" Specify an outfile for r/w access (http_open)"); |
| goto error; |
| } |
|
|
| |
| if (setjmp(env) != 0) { |
| |
| |
| ffpmsg("Timeout (http_open)"); |
| goto error; |
| } |
|
|
| (void) signal(SIGALRM, signal_handler); |
| |
| |
|
|
| if (http_open_network(filename,&httpfile,contentencoding, |
| &contentlength)) { |
| alarm(0); |
| ffpmsg("Unable to open http file (http_open):"); |
| ffpmsg(filename); |
| goto error; |
| } |
|
|
| closehttpfile++; |
|
|
| |
| if ((status = mem_create(filename,handle))) { |
| ffpmsg("Unable to create memory file (http_open)"); |
| goto error; |
| } |
|
|
| closememfile++; |
|
|
| |
| |
| firstchar = fgetc(httpfile); |
| ungetc(firstchar,httpfile); |
| if (!strcmp(contentencoding,"x-gzip") || |
| !strcmp(contentencoding,"x-compress") || |
| strstr(filename,".gz") || |
| strstr(filename,".Z") || |
| ('\037' == firstchar)) { |
| |
| |
|
|
| status = 0; |
| |
| |
| |
| |
| alarm(NETTIMEOUT*10); |
| status = mem_uncompress2mem(filename, httpfile, *handle); |
| alarm(0); |
| if (status) { |
| ffpmsg("Error writing compressed memory file (http_open)"); |
| ffpmsg(filename); |
| goto error; |
| } |
| |
| } else { |
| |
| if (contentlength % 2880) { |
| snprintf(errorstr,MAXLEN,"Content-Length not a multiple of 2880 (http_open) %d", |
| contentlength); |
| ffpmsg(errorstr); |
| } |
|
|
| |
| alarm(NETTIMEOUT); |
| while(0 != (len = fread(recbuf,1,MAXLEN,httpfile))) { |
| alarm(0); |
| status = mem_write(*handle,recbuf,len); |
| if (status) { |
| ffpmsg("Error copying http file into memory (http_open)"); |
| ffpmsg(filename); |
| goto error; |
| } |
| alarm(NETTIMEOUT); |
| } |
| } |
| |
| fclose(httpfile); |
|
|
| signal(SIGALRM, SIG_DFL); |
| alarm(0); |
| return mem_seek(*handle,0); |
|
|
| error: |
| alarm(0); |
| if (closehttpfile) { |
| fclose(httpfile); |
| } |
| if (closememfile) { |
| mem_close_free(*handle); |
| } |
| |
| signal(SIGALRM, SIG_DFL); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| |
| |
| |
| |
|
|
| int http_compress_open(char *url, int rwmode, int *handle) |
| { |
| FILE *httpfile; |
| char contentencoding[SHORTLEN]; |
| char recbuf[MAXLEN]; |
| long len; |
| int contentlength; |
| int ii, flen, status; |
| char firstchar; |
|
|
| closehttpfile = 0; |
| closediskfile = 0; |
| closefdiskfile = 0; |
| closememfile = 0; |
|
|
| flen = strlen(netoutfile); |
| if (!flen) { |
| |
| |
| ffpmsg |
| ("Output file not set, shouldn't have happened (http_compress_open)"); |
| goto error; |
| } |
|
|
| if (rwmode != 0) { |
| ffpmsg("Can't open compressed http:// type file with READWRITE access"); |
| ffpmsg(" Specify an UNCOMPRESSED outfile (http_compress_open)"); |
| goto error; |
| } |
| |
| if (setjmp(env) != 0) { |
| |
| |
| ffpmsg("Timeout (http_open)"); |
| goto error; |
| } |
|
|
| signal(SIGALRM, signal_handler); |
| |
| |
| alarm(NETTIMEOUT); |
| if ((status = http_open_network(url,&httpfile,contentencoding, |
| &contentlength))) { |
| alarm(0); |
| ffpmsg("Unable to open http file (http_compress_open)"); |
| ffpmsg(url); |
| goto error; |
| } |
|
|
| closehttpfile++; |
|
|
| |
|
|
| firstchar = fgetc(httpfile); |
| ungetc(firstchar,httpfile); |
| if (!strcmp(contentencoding,"x-gzip") || |
| !strcmp(contentencoding,"x-compress") || |
| ('\037' == firstchar)) { |
|
|
| if (*netoutfile == '!') |
| { |
| |
| for (ii = 0; ii < flen; ii++) |
| netoutfile[ii] = netoutfile[ii + 1]; |
|
|
| status = file_remove(netoutfile); |
| } |
|
|
| |
| if ((status = file_create(netoutfile,handle))) { |
| ffpmsg("Unable to create output disk file (http_compress_open):"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| |
| closediskfile++; |
|
|
| |
| alarm(NETTIMEOUT); |
| while(0 != (len = fread(recbuf,1,MAXLEN,httpfile))) { |
| alarm(0); |
| status = file_write(*handle,recbuf,len); |
| if (status) { |
| ffpmsg("Error writing disk file (http_compres_open)"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| alarm(NETTIMEOUT); |
| } |
| file_close(*handle); |
| fclose(httpfile); |
| closehttpfile--; |
| closediskfile--; |
|
|
| |
|
|
| if (NULL == (diskfile = fopen(netoutfile,"r"))) { |
| ffpmsg("Unable to reopen disk file (http_compress_open)"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| closefdiskfile++; |
|
|
| |
| if ((status = mem_create(url,handle))) { |
| ffpmsg("Unable to create memory file (http_compress_open)"); |
| goto error; |
| } |
| closememfile++; |
|
|
| |
| status = 0; |
| status = mem_uncompress2mem(url,diskfile,*handle); |
| fclose(diskfile); |
| closefdiskfile--; |
| if (status) { |
| ffpmsg("Error uncompressing disk file to memory (http_compress_open)"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| |
| } else { |
| |
| ffpmsg("Can only have compressed files here (http_compress_open)"); |
| goto error; |
| } |
| |
| signal(SIGALRM, SIG_DFL); |
| alarm(0); |
| return mem_seek(*handle,0); |
|
|
| error: |
| alarm(0); |
| if (closehttpfile) { |
| fclose(httpfile); |
| } |
| if (closefdiskfile) { |
| fclose(diskfile); |
| } |
| if (closememfile) { |
| mem_close_free(*handle); |
| } |
| if (closediskfile) { |
| file_close(*handle); |
| } |
| |
| signal(SIGALRM, SIG_DFL); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| |
| |
| |
|
|
| int http_file_open(char *url, int rwmode, int *handle) |
| { |
| FILE *httpfile; |
| char contentencoding[SHORTLEN]; |
| char errorstr[MAXLEN]; |
| char recbuf[MAXLEN]; |
| long len; |
| int contentlength; |
| int ii, flen, status; |
| char firstchar; |
|
|
| |
| if (!strncmp(netoutfile, "mem:", 4) ) |
| { |
| |
| return( http_open(url, READONLY, handle) ); |
| } |
|
|
| closehttpfile = 0; |
| closefile = 0; |
| closeoutfile = 0; |
|
|
| flen = strlen(netoutfile); |
| if (!flen) { |
| |
| ffpmsg("Output file not set, shouldn't have happened (http_file_open)"); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| if (setjmp(env) != 0) { |
| |
| |
| ffpmsg("Timeout (http_open)"); |
| goto error; |
| } |
|
|
| signal(SIGALRM, signal_handler); |
| |
| |
| alarm(NETTIMEOUT); |
| if ((status = http_open_network(url,&httpfile,contentencoding, |
| &contentlength))) { |
| alarm(0); |
| ffpmsg("Unable to open http file (http_file_open)"); |
| ffpmsg(url); |
| goto error; |
| } |
|
|
| closehttpfile++; |
|
|
| if (*netoutfile == '!') |
| { |
| |
| for (ii = 0; ii < flen; ii++) |
| netoutfile[ii] = netoutfile[ii + 1]; |
|
|
| status = file_remove(netoutfile); |
| } |
|
|
| firstchar = fgetc(httpfile); |
| ungetc(firstchar,httpfile); |
| if (!strcmp(contentencoding,"x-gzip") || |
| !strcmp(contentencoding,"x-compress") || |
| ('\037' == firstchar)) { |
|
|
| |
| |
|
|
| |
| if ((status = file_create(netoutfile,handle))) { |
| ffpmsg("Unable to create output file (http_file_open)"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
|
|
| file_close(*handle); |
| if (NULL == (outfile = fopen(netoutfile,"w"))) { |
| ffpmsg("Unable to reopen the output file (http_file_open)"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| closeoutfile++; |
| status = 0; |
|
|
| |
| |
| |
|
|
| alarm(NETTIMEOUT*10); |
| status = uncompress2file(url,httpfile,outfile,&status); |
| alarm(0); |
| if (status) { |
| ffpmsg("Error uncompressing http file to disk file (http_file_open)"); |
| ffpmsg(url); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| fclose(outfile); |
| closeoutfile--; |
| } else { |
| |
| |
| if ((status = file_create(netoutfile,handle))) { |
| ffpmsg("Unable to create output file (http_file_open)"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| |
| |
| |
| closefile++; |
| |
| if (contentlength % 2880) { |
| snprintf(errorstr, MAXLEN, |
| "Content-Length not a multiple of 2880 (http_file_open) %d", |
| contentlength); |
| ffpmsg(errorstr); |
| } |
| |
| |
| alarm(NETTIMEOUT); |
| while(0 != (len = fread(recbuf,1,MAXLEN,httpfile))) { |
| alarm(0); |
| status = file_write(*handle,recbuf,len); |
| if (status) { |
| ffpmsg("Error copying http file to disk file (http_file_open)"); |
| ffpmsg(url); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| } |
| file_close(*handle); |
| closefile--; |
| } |
| |
| fclose(httpfile); |
| closehttpfile--; |
|
|
| signal(SIGALRM, SIG_DFL); |
| alarm(0); |
|
|
| return file_open(netoutfile,rwmode,handle); |
|
|
| error: |
| alarm(0); |
| if (closehttpfile) { |
| fclose(httpfile); |
| } |
| if (closeoutfile) { |
| fclose(outfile); |
| } |
| if (closefile) { |
| file_close(*handle); |
| } |
| |
| signal(SIGALRM, SIG_DFL); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static int http_open_network(char *url, FILE **httpfile, char *contentencoding, |
| int *contentlength) |
| { |
|
|
| int status; |
| int sock; |
| int tmpint; |
| char recbuf[MAXLEN]; |
| char tmpstr[MAXLEN]; |
| char tmpstr1[SHORTLEN]; |
| char tmpstr2[MAXLEN]; |
| char errorstr[MAXLEN]; |
| char proto[SHORTLEN]; |
| char host[SHORTLEN]; |
| char userpass[MAXLEN]; |
| char fn[MAXLEN]; |
| char turl[MAXLEN]; |
| char *scratchstr; |
| char *scratchstr2; |
| char *saveptr; |
| int port; |
| float version; |
|
|
| char pproto[SHORTLEN]; |
| char phost[SHORTLEN]; |
| int pport; |
| char pfn[MAXLEN]; |
| char *proxy; |
|
|
| |
| strcpy(turl,"http://"); |
| strncat(turl,url,MAXLEN - 8); |
| if (NET_ParseUrl(turl,proto,host,&port,fn)) { |
| snprintf(errorstr,MAXLEN,"URL Parse Error (http_open) %s",url); |
| ffpmsg(errorstr); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| strcpy(userpass, url); |
| if ((scratchstr = strchr(userpass, '@')) != NULL) { |
| *scratchstr = '\0'; |
| } else { |
| strcpy(userpass, ""); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| proxy = getenv("http_proxy"); |
|
|
| |
| if (proxy) { |
| if (NET_ParseUrl(proxy,pproto,phost,&pport,pfn)) { |
| snprintf(errorstr,MAXLEN,"URL Parse Error (http_open) %s",proxy); |
| ffpmsg(errorstr); |
| return (FILE_NOT_OPENED); |
| } |
| sock = NET_TcpConnect(phost,pport); |
| } else { |
| sock = NET_TcpConnect(host,port); |
| } |
|
|
| if (sock < 0) { |
| if (proxy) { |
| ffpmsg("Couldn't connect to host via proxy server (http_open_network)"); |
| ffpmsg(proxy); |
| } |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| if (NULL == (*httpfile = fdopen(sock,"r"))) { |
| ffpmsg ("fdopen failed to convert socket to file (http_open_network)"); |
| close(sock); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| |
| |
| |
|
|
| if (proxy) { |
| snprintf(tmpstr,MAXLEN,"GET http://%s:%-d%s HTTP/1.0\r\n",host,port,fn); |
| } else { |
| snprintf(tmpstr,MAXLEN,"GET %s HTTP/1.0\r\n",fn); |
| } |
|
|
| if (strcmp(userpass, "")) { |
| encode64(strlen(userpass), userpass, MAXLEN, tmpstr2); |
| snprintf(tmpstr1, SHORTLEN,"Authorization: Basic %s\r\n", tmpstr2); |
|
|
| if (strlen(tmpstr) + strlen(tmpstr1) > MAXLEN - 1) |
| return (FILE_NOT_OPENED); |
|
|
| strcat(tmpstr,tmpstr1); |
| } |
|
|
| |
|
|
| |
| snprintf(tmpstr1,SHORTLEN,"User-Agent: FITSIO/HEASARC/%-8.3f\r\n",ffvers(&version)); |
| |
| if (strlen(tmpstr) + strlen(tmpstr1) > MAXLEN - 1) |
| return (FILE_NOT_OPENED); |
|
|
| strcat(tmpstr,tmpstr1); |
|
|
| |
| snprintf(tmpstr1,SHORTLEN,"Host: %s:%-d\r\n\r\n",host,port); |
|
|
| if (strlen(tmpstr) + strlen(tmpstr1) > MAXLEN - 1) |
| return (FILE_NOT_OPENED); |
|
|
| strcat(tmpstr,tmpstr1); |
|
|
| status = NET_SendRaw(sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
|
|
| |
| if (!(fgets(recbuf,MAXLEN,*httpfile))) { |
| snprintf (errorstr,MAXLEN,"http header short (http_open_network) %s",recbuf); |
| ffpmsg(errorstr); |
| fclose(*httpfile); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| *contentlength = 0; |
| contentencoding[0] = '\0'; |
|
|
| |
| sscanf(recbuf,"%s %d",tmpstr,&status); |
| if (status != 200){ |
| if (status == 301 || status == 302) { |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| while (fgets(recbuf,MAXLEN,*httpfile)) { |
|
|
| scratchstr = strstr(recbuf,"Location: "); |
| if (scratchstr != NULL) { |
|
|
| |
| |
| scratchstr += 10; |
| |
| |
| tmpint = strlen(scratchstr); |
| if (scratchstr[tmpint-1] == '\r') scratchstr[tmpint-1] = '\0'; |
| tmpint = strlen(scratchstr); |
| if (scratchstr[tmpint-1] == '\n') scratchstr[tmpint-1] = '\0'; |
| tmpint = strlen(scratchstr); |
| if (scratchstr[tmpint-1] == '\r') scratchstr[tmpint-1] = '\0'; |
|
|
| |
| |
| |
| |
| |
| scratchstr2 = strstr(scratchstr,"http://"); |
| if (scratchstr2 != NULL) { |
| |
| |
| |
| scratchstr2 += 7; |
| strcpy(turl, scratchstr2); |
| fclose (*httpfile); |
|
|
| |
| return |
| http_open_network(turl,httpfile,contentencoding,contentlength); |
| } |
|
|
| |
| scratchstr2 = strstr(scratchstr,"ftp://"); |
| if (scratchstr2 != NULL) { |
| |
| |
| scratchstr2 += 6; |
|
|
| |
| |
| |
| if (strlen(scratchstr2) > FLEN_FILENAME-1) |
| { |
| ffpmsg("Error: redirected url string too long (http_open_network)"); |
| fclose(*httpfile); |
| return URL_PARSE_ERROR; |
| } |
| strcpy(url, scratchstr2); |
| strcpy(contentencoding,"ftp://"); |
| fclose (*httpfile); |
| return 0; |
| } |
| |
| |
| scratchstr2 = strstr(scratchstr,"https://"); |
| if (scratchstr2 != NULL) { |
| |
| scratchstr2 += 8; |
| |
| |
| |
| |
| if (strlen(scratchstr2) > FLEN_FILENAME-1) |
| { |
| ffpmsg("Error: redirected url string too long (http_open_network)"); |
| fclose(*httpfile); |
| return URL_PARSE_ERROR; |
| } |
| strcpy(url, scratchstr2); |
| strcpy(contentencoding,"https://"); |
| fclose(*httpfile); |
| return 0; |
| } |
| |
| } |
| } |
|
|
| |
| ffpmsg("but we were unable to find the redirected url in the servers response"); |
| } |
|
|
| |
| fclose(*httpfile); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| |
| while (fgets(recbuf,MAXLEN,*httpfile)) { |
| |
| if (*recbuf == '\r') break; |
| if (strlen(recbuf) > 3) { |
| recbuf[strlen(recbuf)-1] = '\0'; |
| recbuf[strlen(recbuf)-1] = '\0'; |
| } |
| sscanf(recbuf,"%s %d",tmpstr,&tmpint); |
| |
| if (!strcmp(tmpstr,"Content-Length:")) { |
| *contentlength = tmpint; |
| } |
| |
| if (!strcmp(tmpstr,"Content-Encoding:")) { |
| if (NULL != (scratchstr = strstr(recbuf,":"))) { |
| |
| scratchstr++; |
| scratchstr++; |
| if (strlen(scratchstr) > SHORTLEN-1) |
| { |
| ffpmsg("Error: content-encoding string too long (http_open_network)"); |
| fclose(*httpfile); |
| return URL_PARSE_ERROR; |
| } |
| strcpy(contentencoding,scratchstr); |
| } |
| } |
| } |
| |
| |
| return 0; |
| } |
|
|
| |
| |
| |
| |
| int https_open(char *filename, int rwmode, int *handle) |
| { |
| curlmembuf inmem; |
| char errStr[MAXLEN]; |
| int status=0; |
| |
| |
| if (rwmode != 0) { |
| ffpmsg("Can't open https:// type file with READWRITE access"); |
| ffpmsg(" Specify an outfile for r/w access (https_open)"); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| inmem.memory=0; |
| inmem.size=0; |
| if (setjmp(env) != 0) |
| { |
| alarm(0); |
| signal(SIGALRM, SIG_DFL); |
| ffpmsg("Timeout (https_open)"); |
| free(inmem.memory); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| signal(SIGALRM, signal_handler); |
| alarm(NETTIMEOUT); |
|
|
| if (https_open_network(filename, &inmem)) |
| { |
| alarm(0); |
| signal(SIGALRM, SIG_DFL); |
| ffpmsg("Unable to read https file into memory (https_open)"); |
| free(inmem.memory); |
| return (FILE_NOT_OPENED); |
| } |
| alarm(0); |
| signal(SIGALRM, SIG_DFL); |
| |
| |
| if ((status = mem_create(filename, handle))) |
| { |
| ffpmsg("Unable to create memory file (https_open)"); |
| free(inmem.memory); |
| return (FILE_NOT_OPENED); |
| } |
| |
| if (inmem.size % 2880) |
| { |
| snprintf(errStr,MAXLEN,"Content-Length not a multiple of 2880 (https_open) %u", |
| inmem.size); |
| ffpmsg(errStr); |
| } |
| status = mem_write(*handle, inmem.memory, inmem.size); |
| if (status) |
| { |
| ffpmsg("Error copying https file into memory (https_open)"); |
| ffpmsg(filename); |
| free(inmem.memory); |
| mem_close_free(*handle); |
| return (FILE_NOT_OPENED); |
| } |
| free(inmem.memory); |
| return mem_seek(*handle, 0); |
| |
| } |
|
|
| |
| int https_file_open(char *filename, int rwmode, int *handle) |
| { |
| int ii, flen; |
| char errStr[MAXLEN]; |
| curlmembuf inmem; |
| |
| |
| if (!strncmp(netoutfile, "mem:", 4) ) |
| { |
| |
| return( https_open(filename, READONLY, handle) ); |
| } |
|
|
| flen = strlen(netoutfile); |
| if (!flen) |
| { |
| |
| ffpmsg("Output file not set, shouldn't have happened (https_file_open)"); |
| return (FILE_NOT_OPENED); |
| } |
| |
| inmem.memory=0; |
| inmem.size=0; |
| if (setjmp(env) != 0) |
| { |
| alarm(0); |
| signal(SIGALRM, SIG_DFL); |
| ffpmsg("Timeout (https_file_open)"); |
| free(inmem.memory); |
| return (FILE_NOT_OPENED); |
| } |
| signal(SIGALRM, signal_handler); |
| alarm(NETTIMEOUT); |
| if (https_open_network(filename, &inmem)) |
| { |
| alarm(0); |
| signal(SIGALRM, SIG_DFL); |
| ffpmsg("Unable to read https file into memory (https_file_open)"); |
| free(inmem.memory); |
| return (FILE_NOT_OPENED); |
| } |
| alarm(0); |
| signal(SIGALRM, SIG_DFL); |
| |
| if (*netoutfile == '!') |
| { |
| |
| for (ii = 0; ii < flen; ii++) |
| netoutfile[ii] = netoutfile[ii + 1]; |
|
|
| file_remove(netoutfile); |
| } |
|
|
| |
| if (file_create(netoutfile,handle)) |
| { |
| ffpmsg("Unable to create output file (https_file_open)"); |
| ffpmsg(netoutfile); |
| free(inmem.memory); |
| return (FILE_NOT_OPENED); |
| } |
| |
| if (inmem.size % 2880) |
| { |
| snprintf(errStr, MAXLEN, |
| "Content-Length not a multiple of 2880 (https_file_open) %d", |
| inmem.size); |
| ffpmsg(errStr); |
| } |
| |
| if (file_write(*handle, inmem.memory, inmem.size)) |
| { |
| ffpmsg("Error copying https file to disk file (https_file_open)"); |
| ffpmsg(filename); |
| ffpmsg(netoutfile); |
| free(inmem.memory); |
| file_close(*handle); |
| return (FILE_NOT_OPENED); |
| } |
| free(inmem.memory); |
| file_close(*handle); |
| |
| return file_open(netoutfile, rwmode, handle); |
| } |
|
|
| |
| |
| |
| size_t curlToMemCallback(void *buffer, size_t size, size_t nmemb, void *userp) |
| { |
| curlmembuf* inmem = (curlmembuf* )userp; |
| size_t transferSize = size*nmemb; |
| if (!inmem->size) |
| { |
| |
| inmem->memory = (char *)malloc(transferSize); |
| } |
| else |
| inmem->memory = realloc(inmem->memory, inmem->size+transferSize); |
| if (inmem->memory == NULL) |
| { |
| ffpmsg("realloc error - not enough memory (curlToMemCallback)\n"); |
| return 0; |
| } |
| memcpy(&(inmem->memory[inmem->size]), buffer, transferSize); |
| inmem->size += transferSize; |
| |
| return transferSize; |
| } |
|
|
| |
| int https_open_network(char *filename, curlmembuf* buffer) |
| { |
| char *urlname=0; |
| char errStr[MAXLEN]; |
| char agentStr[MAXLEN]; |
| float version=0.0; |
| char *verify=0; |
| |
| |
| |
| |
| long verifyPeer = 1; |
| long verifyHost = 2; |
| #ifdef CFITSIO_HAVE_CURL |
| CURL *curl=0; |
| CURLcode res; |
| char curlErrBuf[CURL_ERROR_SIZE]; |
| |
| if (strstr(filename,".Z")) |
| { |
| ffpmsg("x-compress .Z format not currently supported with https transfers"); |
| return(FILE_NOT_OPENED); |
| } |
|
|
| |
| |
| |
| curl = curl_easy_init(); |
| |
| res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, verifyPeer); |
| if (res != CURLE_OK) |
| { |
| ffpmsg("ERROR: CFITSIO was built with a libcurl library that "); |
| ffpmsg("does not have SSL support, and therefore can't perform https transfers."); |
| return (FILE_NOT_OPENED); |
| } |
| curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verifyHost); |
| |
| curl_easy_setopt(curl, CURLOPT_VERBOSE, (long)curl_verbose); |
| curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlToMemCallback); |
| snprintf(agentStr,MAXLEN,"User-Agent: FITSIO/HEASARC/%-8.3f",ffvers(&version)); |
| curl_easy_setopt(curl, CURLOPT_USERAGENT,agentStr); |
| |
| buffer->memory = 0; |
| buffer->size = 0; |
| curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)buffer); |
| curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlErrBuf); |
| curlErrBuf[0]=0; |
| |
| |
| curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); |
| |
| curl_easy_setopt(curl, CURLOPT_ENCODING, ""); |
| |
| |
| urlname = (char *)malloc(strlen(filename)+12); |
| strcpy(urlname, "https://"); |
| strcat(urlname, filename); |
| |
| |
| if (!strstr(filename,".gz") && !strstr(filename,"?")) |
| strcat(urlname, ".gz"); |
| |
| |
| curl_easy_setopt(curl, CURLOPT_URL, urlname); |
| res = curl_easy_perform(curl); |
| if (res != CURLE_OK && res != CURLE_HTTP_RETURNED_ERROR) |
| { |
| |
| |
| |
| |
| |
| verify = getenv("CFITSIO_VERIFY_HTTPS"); |
| if (verify) |
| { |
| if (verify[0] == 'T' || verify[0] == 't') |
| { |
| snprintf(errStr,MAXLEN,"libcurl error: %d",res); |
| ffpmsg(errStr); |
| if (strlen(curlErrBuf)) |
| ffpmsg(curlErrBuf); |
| curl_easy_cleanup(curl); |
| free(urlname); |
| return (FILE_NOT_OPENED); |
| } |
| } |
| verifyPeer = 0; |
| verifyHost = 0; |
| curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, verifyPeer); |
| curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verifyHost); |
| res = curl_easy_perform(curl); |
| if (res != CURLE_OK) |
| { |
| |
| |
| if (!strstr(filename,".gz") && !strstr(filename,"?")) |
| { |
| strcpy(urlname, "https://"); |
| strcat(urlname, filename); |
| curl_easy_setopt(curl, CURLOPT_URL, urlname); |
| res = curl_easy_perform(curl); |
| if (res != CURLE_OK) |
| { |
| snprintf(errStr,MAXLEN,"libcurl error: %d",res); |
| ffpmsg(errStr); |
| if (strlen(curlErrBuf)) |
| ffpmsg(curlErrBuf); |
| curl_easy_cleanup(curl); |
| free(urlname); |
| return (FILE_NOT_OPENED); |
| } |
| else |
| fprintf(stderr, "Warning: Unable to perform SSL verification on https transfer from: %s\n", |
| urlname); |
| } |
| else |
| { |
| snprintf(errStr,MAXLEN,"libcurl error: %d",res); |
| ffpmsg(errStr); |
| if (strlen(curlErrBuf)) |
| ffpmsg(curlErrBuf); |
| curl_easy_cleanup(curl); |
| free(urlname); |
| return (FILE_NOT_OPENED); |
| } |
| } |
| else |
| fprintf(stderr, "Warning: Unable to perform SSL verification on https transfer from: %s\n", |
| urlname); |
|
|
| } |
| else if (res == CURLE_HTTP_RETURNED_ERROR) |
| { |
| |
| |
| |
| if (!strstr(filename,".gz") && !strstr(filename,"?")) |
| { |
| strcpy(urlname, "https://"); |
| strcat(urlname, filename); |
| curl_easy_setopt(curl, CURLOPT_URL, urlname); |
| res = curl_easy_perform(curl); |
| if (res != CURLE_OK) |
| { |
| snprintf(errStr,MAXLEN,"libcurl error: %d",res); |
| ffpmsg(errStr); |
| if (strlen(curlErrBuf)) |
| ffpmsg(curlErrBuf); |
| curl_easy_cleanup(curl); |
| free(urlname); |
| return (FILE_NOT_OPENED); |
| } |
| } |
| else |
| { |
| snprintf(errStr,MAXLEN,"libcurl error: %d",res); |
| ffpmsg(errStr); |
| if (strlen(curlErrBuf)) |
| ffpmsg(curlErrBuf); |
| curl_easy_cleanup(curl); |
| free(urlname); |
| return (FILE_NOT_OPENED); |
| } |
| } |
| |
| free(urlname); |
| curl_easy_cleanup(curl); |
| |
| return 0; |
|
|
| #else |
| ffpmsg("ERROR: This CFITSIO build was not compiled with the libcurl library package "); |
| ffpmsg("and therefore it cannot perform HTTPS connections."); |
| #endif |
| |
| return (FILE_NOT_OPENED); |
| } |
|
|
| void https_set_verbose(int flag) |
| { |
| if (!flag) |
| curl_verbose = 0; |
| else |
| curl_verbose = 1; |
| } |
|
|
| |
| |
| |
|
|
| int ftp_open(char *filename, int rwmode, int *handle) |
| { |
| FILE *ftpfile; |
| FILE *command; |
| int sock; |
| char recbuf[MAXLEN]; |
| long len; |
| int status; |
| char firstchar; |
|
|
| closememfile = 0; |
| closecommandfile = 0; |
| closeftpfile = 0; |
|
|
| |
| if (rwmode != 0) { |
| ffpmsg("Can't open ftp:// type file with READWRITE access"); |
| ffpmsg("Specify an outfile for r/w access (ftp_open)"); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| if (setjmp(env) != 0) { |
| |
| |
| ffpmsg("Timeout (http_open)"); |
| goto error; |
| } |
|
|
| signal(SIGALRM, signal_handler); |
| |
| |
| |
|
|
| if (strlen(filename) > MAXLEN - 4) { |
| ffpmsg("filename too long (ftp_open)"); |
| ffpmsg(filename); |
| goto error; |
| } |
|
|
| alarm(NETTIMEOUT); |
| if (ftp_open_network(filename,&ftpfile,&command,&sock)) { |
|
|
| alarm(0); |
| ffpmsg("Unable to open following ftp file (ftp_open):"); |
| ffpmsg(filename); |
| goto error; |
| } |
|
|
| closeftpfile++; |
| closecommandfile++; |
|
|
| |
| if ((status = mem_create(filename,handle))) { |
| ffpmsg ("Could not create memory file to passive port (ftp_open)"); |
| ffpmsg(filename); |
| goto error; |
| } |
| closememfile++; |
| |
| |
|
|
| |
| firstchar = fgetc(ftpfile); |
| ungetc(firstchar,ftpfile); |
|
|
| if (strstr(filename,".gz") || |
| strstr(filename,".Z") || |
| ('\037' == firstchar)) { |
| |
| status = 0; |
| |
| alarm(NETTIMEOUT*10); |
| status = mem_uncompress2mem(filename, ftpfile, *handle); |
| alarm(0); |
| if (status) { |
| ffpmsg("Error writing compressed memory file (ftp_open)"); |
| ffpmsg(filename); |
| goto error; |
| } |
| } else { |
| |
| alarm(NETTIMEOUT); |
| while(0 != (len = fread(recbuf,1,MAXLEN,ftpfile))) { |
| alarm(0); |
| status = mem_write(*handle,recbuf,len); |
| if (status) { |
| ffpmsg("Error writing memory file (http_open)"); |
| ffpmsg(filename); |
| goto error; |
| } |
| alarm(NETTIMEOUT); |
| } |
| } |
|
|
| |
| fclose(ftpfile); |
| closeftpfile--; |
|
|
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| closecommandfile--; |
|
|
| signal(SIGALRM, SIG_DFL); |
| alarm(0); |
|
|
| return mem_seek(*handle,0); |
|
|
| error: |
| alarm(0); |
| if (closecommandfile) { |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| } |
| if (closeftpfile) { |
| fclose(ftpfile); |
| } |
| if (closememfile) { |
| mem_close_free(*handle); |
| } |
| |
| signal(SIGALRM, SIG_DFL); |
| return (FILE_NOT_OPENED); |
| } |
| |
| |
| |
|
|
| int ftp_file_open(char *url, int rwmode, int *handle) |
| { |
| FILE *ftpfile; |
| FILE *command; |
| char recbuf[MAXLEN]; |
| long len; |
| int sock; |
| int ii, flen, status; |
| char firstchar; |
|
|
| |
| if (!strncmp(netoutfile, "mem:", 4) ) |
| { |
| |
| return( ftp_open(url, READONLY, handle) ); |
| } |
|
|
| closeftpfile = 0; |
| closecommandfile = 0; |
| closefile = 0; |
| closeoutfile = 0; |
| |
| |
| flen = strlen(netoutfile); |
| if (!flen) |
| { |
| ffpmsg("Output file not set, shouldn't have happened (ftp_file_open)"); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| if (setjmp(env) != 0) { |
| |
| |
| ffpmsg("Timeout (http_open)"); |
| goto error; |
| } |
|
|
| signal(SIGALRM, signal_handler); |
| |
| |
| |
| |
|
|
| alarm(NETTIMEOUT); |
| if ((status = ftp_open_network(url,&ftpfile,&command,&sock))) { |
| alarm(0); |
| ffpmsg("Unable to open http file (ftp_file_open)"); |
| ffpmsg(url); |
| goto error; |
| } |
| closeftpfile++; |
| closecommandfile++; |
|
|
| if (*netoutfile == '!') |
| { |
| |
| for (ii = 0; ii < flen; ii++) |
| netoutfile[ii] = netoutfile[ii + 1]; |
|
|
| status = file_remove(netoutfile); |
| } |
|
|
| |
| firstchar = fgetc(ftpfile); |
| ungetc(firstchar,ftpfile); |
|
|
| if (strstr(url,".gz") || |
| strstr(url,".Z") || |
| ('\037' == firstchar)) { |
|
|
| |
| |
| |
| if ((status = file_create(netoutfile,handle))) { |
| ffpmsg("Unable to create output file (ftp_file_open)"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
|
|
| file_close(*handle); |
| if (NULL == (outfile = fopen(netoutfile,"w"))) { |
| ffpmsg("Unable to reopen the output file (ftp_file_open)"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| closeoutfile++; |
| status = 0; |
|
|
| |
| |
| |
|
|
| alarm(NETTIMEOUT*10); |
| status = uncompress2file(url,ftpfile,outfile,&status); |
| alarm(0); |
| if (status) { |
| ffpmsg("Unable to uncompress the output file (ftp_file_open)"); |
| ffpmsg(url); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| fclose(outfile); |
| closeoutfile--; |
|
|
| } else { |
| |
| |
| if ((status = file_create(netoutfile,handle))) { |
| ffpmsg("Unable to create output file (ftp_file_open)"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| closefile++; |
| |
| |
| alarm(NETTIMEOUT); |
| while(0 != (len = fread(recbuf,1,MAXLEN,ftpfile))) { |
| alarm(0); |
| status = file_write(*handle,recbuf,len); |
| if (status) { |
| ffpmsg("Error writing file (ftp_file_open)"); |
| ffpmsg(url); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| alarm(NETTIMEOUT); |
| } |
| file_close(*handle); |
| } |
| fclose(ftpfile); |
| closeftpfile--; |
| |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| closecommandfile--; |
|
|
| signal(SIGALRM, SIG_DFL); |
| alarm(0); |
|
|
| return file_open(netoutfile,rwmode,handle); |
|
|
| error: |
| alarm(0); |
| if (closeftpfile) { |
| fclose(ftpfile); |
| } |
| if (closecommandfile) { |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| } |
| if (closeoutfile) { |
| fclose(outfile); |
| } |
| if (closefile) { |
| file_close(*handle); |
| } |
| |
| signal(SIGALRM, SIG_DFL); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| |
| |
|
|
| int ftp_compress_open(char *url, int rwmode, int *handle) |
| { |
| FILE *ftpfile; |
| FILE *command; |
| char recbuf[MAXLEN]; |
| long len; |
| int ii, flen, status; |
| int sock; |
| char firstchar; |
|
|
| closeftpfile = 0; |
| closecommandfile = 0; |
| closememfile = 0; |
| closefdiskfile = 0; |
| closediskfile = 0; |
|
|
| |
| if (rwmode != 0) { |
| ffpmsg("Compressed files must be r/o"); |
| return (FILE_NOT_OPENED); |
| } |
| |
| |
| flen = strlen(netoutfile); |
| if (!flen) |
| { |
| ffpmsg( |
| "Output file not set, shouldn't have happened (ftp_compress_open)"); |
| return (FILE_NOT_OPENED); |
| } |
| |
| |
| if (setjmp(env) != 0) { |
| |
| |
| ffpmsg("Timeout (http_open)"); |
| goto error; |
| } |
| |
| signal(SIGALRM, signal_handler); |
| |
| |
| |
| alarm(NETTIMEOUT); |
|
|
| if ((status = ftp_open_network(url,&ftpfile,&command,&sock))) { |
| alarm(0); |
| ffpmsg("Unable to open ftp file (ftp_compress_open)"); |
| ffpmsg(url); |
| goto error; |
| } |
| closeftpfile++; |
| closecommandfile++; |
|
|
| |
| firstchar = fgetc(ftpfile); |
| ungetc(firstchar,ftpfile); |
|
|
| if (strstr(url,".gz") || |
| strstr(url,".Z") || |
| ('\037' == firstchar)) { |
| |
| if (*netoutfile == '!') |
| { |
| |
| for (ii = 0; ii < flen; ii++) |
| netoutfile[ii] = netoutfile[ii + 1]; |
|
|
| status = file_remove(netoutfile); |
| } |
|
|
| |
| if ((status = file_create(netoutfile,handle))) { |
| ffpmsg("Unable to create output file (ftp_compress_open)"); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| closediskfile++; |
| |
| |
| alarm(NETTIMEOUT); |
| while(0 != (len = fread(recbuf,1,MAXLEN,ftpfile))) { |
| alarm(0); |
| status = file_write(*handle,recbuf,len); |
| if (status) { |
| ffpmsg("Error writing file (ftp_compres_open)"); |
| ffpmsg(url); |
| ffpmsg(netoutfile); |
| goto error; |
| } |
| alarm(NETTIMEOUT); |
| } |
|
|
| file_close(*handle); |
| closediskfile--; |
| fclose(ftpfile); |
| closeftpfile--; |
| |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| closecommandfile--; |
|
|
| |
|
|
| if (NULL == (diskfile = fopen(netoutfile,"r"))) { |
| ffpmsg("Unable to reopen disk file (ftp_compress_open)"); |
| ffpmsg(netoutfile); |
| return (FILE_NOT_OPENED); |
| } |
| closefdiskfile++; |
| |
| if ((status = mem_create(url,handle))) { |
| ffpmsg("Unable to create memory file (ftp_compress_open)"); |
| ffpmsg(url); |
| goto error; |
| } |
| closememfile++; |
|
|
| status = 0; |
| status = mem_uncompress2mem(url,diskfile,*handle); |
| fclose(diskfile); |
| closefdiskfile--; |
|
|
| if (status) { |
| ffpmsg("Error writing compressed memory file (ftp_compress_open)"); |
| goto error; |
| } |
| |
| } else { |
| |
| ffpmsg("Can only compressed files here (ftp_compress_open)"); |
| goto error; |
| } |
| |
|
|
| signal(SIGALRM, SIG_DFL); |
| alarm(0); |
| return mem_seek(*handle,0); |
|
|
| error: |
| alarm(0); |
| if (closeftpfile) { |
| fclose(ftpfile); |
| } |
| if (closecommandfile) { |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| } |
| if (closefdiskfile) { |
| fclose(diskfile); |
| } |
| if (closememfile) { |
| mem_close_free(*handle); |
| } |
| if (closediskfile) { |
| file_close(*handle); |
| } |
| |
| signal(SIGALRM, SIG_DFL); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| |
| |
| |
|
|
| static int ftp_open_network(char *filename, FILE **ftpfile, FILE **command, int *sock) |
| { |
| int status; |
| int sock1; |
| int tmpint; |
| char recbuf[MAXLEN]; |
| char errorstr[MAXLEN]; |
| char tmpstr[MAXLEN]; |
| char proto[SHORTLEN]; |
| char host[SHORTLEN]; |
| char *newhost; |
| char *username; |
| char *password; |
| char fn[MAXLEN]; |
| char *newfn; |
| char *passive; |
| char *tstr; |
| char *saveptr; |
| char ip[SHORTLEN]; |
| char turl[MAXLEN]; |
| int port; |
| int ii,tryingtologin = 1; |
|
|
| |
| if (strlen(filename) > MAXLEN - 7) { |
| ffpmsg("ftp filename is too long (ftp_open_network)"); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| strcpy(turl,"ftp://"); |
| strcat(turl,filename); |
| if (NET_ParseUrl(turl,proto,host,&port,fn)) { |
| snprintf(errorstr,MAXLEN,"URL Parse Error (ftp_open) %s",filename); |
| ffpmsg(errorstr); |
| return (FILE_NOT_OPENED); |
| } |
| |
| port = 21; |
| |
| username = "anonymous"; |
| password = "user@host.com"; |
| |
| if (NULL != (newhost = strrchr(host,'@'))) { |
| *newhost = '\0'; |
| newhost++; |
| |
| username = host; |
| |
| if (NULL != strchr(username,':')) { |
| password = strchr(username,':'); |
| *password = '\0'; |
| password++; |
| } |
| } else { |
| newhost = host; |
| } |
|
|
| for (ii = 0; ii < 10; ii++) { |
| |
| |
| *sock = NET_TcpConnect(newhost,port); |
| |
| if (NULL == (*command = fdopen(*sock,"r"))) { |
| ffpmsg ("fdopen failed to convert socket to stdio file (ftp_open_netowrk)"); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| if (ftp_status(*command,"220 ")) { |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
|
|
| |
|
|
| sleep (5); |
|
|
| } else { |
| tryingtologin = 0; |
| break; |
| } |
| } |
|
|
| if (tryingtologin) { |
| ffpmsg ("error connecting to remote server, no 220 seen (ftp_open_network)"); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| snprintf(tmpstr,MAXLEN,"USER %s\r\n",username); |
|
|
| status = NET_SendRaw(*sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
|
|
| if (ftp_status(*command,"331 ")) { |
| ffpmsg ("USER error no 331 seen (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| |
| |
| snprintf(tmpstr,MAXLEN,"PASS %s\r\n",password); |
| status = NET_SendRaw(*sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
| |
| if (ftp_status(*command,"230 ")) { |
| ffpmsg ("PASS error, no 230 seen (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| newfn = strrchr(fn,'/'); |
| if (newfn == NULL) { |
| strcpy(tmpstr,"CWD /\r\n"); |
| newfn = fn; |
| } else { |
| *newfn = '\0'; |
| newfn++; |
| if (strlen(fn) == 0) { |
| strcpy(tmpstr,"CWD /\r\n"); |
| } else { |
| |
| if (fn[0] == '/') { |
| snprintf(tmpstr,MAXLEN,"CWD %s\r\n",&fn[1]); |
| } else { |
| snprintf(tmpstr,MAXLEN,"CWD %s\r\n",fn); |
| } |
| } |
| } |
| |
| status = NET_SendRaw(*sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
| |
| if (ftp_status(*command,"250 ")) { |
| ffpmsg ("CWD error, no 250 seen (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| |
| if (!strlen(newfn)) { |
| ffpmsg("Null file name (ftp_open)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| snprintf(tmpstr,MAXLEN,"TYPE I\r\n"); |
| status = NET_SendRaw(*sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
| |
| if (ftp_status(*command,"200 ")) { |
| ffpmsg ("TYPE I error, 200 not seen (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| |
| status = NET_SendRaw(*sock,"PASV\r\n",6,NET_DEFAULT); |
|
|
| if (!(fgets(recbuf,MAXLEN,*command))) { |
| ffpmsg ("PASV error (ftp_open)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| |
| |
| |
| if (recbuf[0] == '2' && recbuf[1] == '2' && recbuf[2] == '7') { |
| |
| |
| if (!(passive = strchr(recbuf,'('))) { |
| ffpmsg ("PASV error (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| |
| *passive = '\0'; |
| passive++; |
| ip[0] = '\0'; |
| |
| |
| |
| if (!(tstr = ffstrtok(passive,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| strcpy(ip,tstr); |
| strcat(ip,"."); |
| |
| if (!(tstr = ffstrtok(NULL,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| strcat(ip,tstr); |
| strcat(ip,"."); |
| |
| if (!(tstr = ffstrtok(NULL,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| strcat(ip,tstr); |
| strcat(ip,"."); |
| |
| if (!(tstr = ffstrtok(NULL,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| strcat(ip,tstr); |
| |
| |
| if (!(tstr = ffstrtok(NULL,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| sscanf(tstr,"%d",&port); |
| port *= 256; |
| |
| if (!(tstr = ffstrtok(NULL,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| sscanf(tstr,"%d",&tmpint); |
| port += tmpint; |
|
|
| if (!strlen(newfn)) { |
| ffpmsg("Null file name (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| |
| |
| sock1 = NET_TcpConnect(ip,port); |
| if (NULL == (*ftpfile = fdopen(sock1,"r"))) { |
| ffpmsg ("Could not connect to passive port (ftp_open_network)"); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| |
| snprintf(tmpstr,MAXLEN,"RETR %s\r\n",newfn); |
| status = NET_SendRaw(*sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
|
|
| if (ftp_status(*command,"150 ")) { |
| fclose(*ftpfile); |
| NET_SendRaw(sock1,"QUIT\r\n",6,NET_DEFAULT); |
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| return 0; |
| } |
| |
| |
|
|
| fclose(*command); |
| NET_SendRaw(*sock,"QUIT\r\n",6,NET_DEFAULT); |
| return (FILE_NOT_OPENED); |
| } |
| |
| |
|
|
| int ftp_file_exist(char *filename) |
| { |
| FILE *ftpfile; |
| FILE *command; |
| int sock; |
| int status; |
| int sock1; |
| int tmpint; |
| char recbuf[MAXLEN]; |
| char errorstr[MAXLEN]; |
| char tmpstr[MAXLEN]; |
| char proto[SHORTLEN]; |
| char host[SHORTLEN]; |
| char *newhost; |
| char *username; |
| char *password; |
| char fn[MAXLEN]; |
| char *newfn; |
| char *passive; |
| char *tstr; |
| char *saveptr; |
| char ip[SHORTLEN]; |
| char turl[MAXLEN]; |
| int port; |
| int ii, tryingtologin = 1; |
|
|
| |
| if (strlen(filename) > MAXLEN - 7) { |
| ffpmsg("ftp filename is too long (ftp_file_exist)"); |
| return 0; |
| } |
|
|
| strcpy(turl,"ftp://"); |
| strcat(turl,filename); |
| if (NET_ParseUrl(turl,proto,host,&port,fn)) { |
| snprintf(errorstr,MAXLEN,"URL Parse Error (ftp_file_exist) %s",filename); |
| ffpmsg(errorstr); |
| return 0; |
| } |
|
|
| port = 21; |
| |
| username = "anonymous"; |
| password = "user@host.com"; |
| |
| if (NULL != (newhost = strrchr(host,'@'))) { |
| *newhost = '\0'; |
| newhost++; |
| |
| username = host; |
| |
| if (NULL != strchr(username,':')) { |
| password = strchr(username,':'); |
| *password = '\0'; |
| password++; |
| } |
| } else { |
| newhost = host; |
| } |
|
|
| for (ii = 0; ii < 10; ii++) { |
| |
| |
| sock = NET_TcpConnect(newhost,port); |
| |
| if (NULL == (command = fdopen(sock,"r"))) { |
| ffpmsg ("Failed to convert socket to stdio file (ftp_file_exist)"); |
| return 0; |
| } |
|
|
| |
| if (ftp_status(command,"220 ")) { |
| ffpmsg ("error connecting to remote server, no 220 seen (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
|
|
| |
|
|
| sleep (5); |
|
|
| } else { |
| tryingtologin = 0; |
| break; |
| } |
| |
| } |
|
|
| if (tryingtologin) { |
| ffpmsg ("error connecting to remote server, no 220 seen (ftp_open_network)"); |
| return (0); |
| } |
| |
| |
| snprintf(tmpstr,MAXLEN,"USER %s\r\n",username); |
|
|
| status = NET_SendRaw(sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
|
|
| if (ftp_status(command,"331 ")) { |
| ffpmsg ("USER error no 331 seen (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| |
| |
| snprintf(tmpstr,MAXLEN,"PASS %s\r\n",password); |
| status = NET_SendRaw(sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
| |
| if (ftp_status(command,"230 ")) { |
| ffpmsg ("PASS error, no 230 seen (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
|
|
| |
| newfn = strrchr(fn,'/'); |
| if (newfn == NULL) { |
| strcpy(tmpstr,"CWD /\r\n"); |
| newfn = fn; |
| } else { |
| *newfn = '\0'; |
| newfn++; |
| if (strlen(fn) == 0) { |
| strcpy(tmpstr,"CWD /\r\n"); |
| } else { |
| |
| if (fn[0] == '/') { |
| snprintf(tmpstr,MAXLEN,"CWD %s\r\n",&fn[1]); |
| } else { |
| snprintf(tmpstr,MAXLEN,"CWD %s\r\n",fn); |
| } |
| } |
| } |
|
|
| status = NET_SendRaw(sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
| |
| if (ftp_status(command,"250 ")) { |
| ffpmsg ("CWD error, no 250 seen (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| |
| if (!strlen(newfn)) { |
| ffpmsg("Null file name (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
|
|
| |
| snprintf(tmpstr,MAXLEN,"TYPE I\r\n"); |
| status = NET_SendRaw(sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
| |
| if (ftp_status(command,"200 ")) { |
| ffpmsg ("TYPE I error, 200 not seen (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
|
|
| status = NET_SendRaw(sock,"PASV\r\n",6,NET_DEFAULT); |
|
|
| if (!(fgets(recbuf,MAXLEN,command))) { |
| ffpmsg ("PASV error (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| |
| |
| |
| if (recbuf[0] == '2' && recbuf[1] == '2' && recbuf[2] == '7') { |
| |
| |
| if (!(passive = strchr(recbuf,'('))) { |
| ffpmsg ("PASV error (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| |
| *passive = '\0'; |
| passive++; |
| ip[0] = '\0'; |
| |
| |
| |
| if (!(tstr = ffstrtok(passive,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| strcpy(ip,tstr); |
| strcat(ip,"."); |
| |
| if (!(tstr = ffstrtok(NULL,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| strcat(ip,tstr); |
| strcat(ip,"."); |
| |
| if (!(tstr = ffstrtok(NULL,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| strcat(ip,tstr); |
| strcat(ip,"."); |
| |
| if (!(tstr = ffstrtok(NULL,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| strcat(ip,tstr); |
| |
| |
| if (!(tstr = ffstrtok(NULL,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| sscanf(tstr,"%d",&port); |
| port *= 256; |
| |
| if (!(tstr = ffstrtok(NULL,",)",&saveptr))) { |
| ffpmsg ("PASV error (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| sscanf(tstr,"%d",&tmpint); |
| port += tmpint; |
|
|
| if (!strlen(newfn)) { |
| ffpmsg("Null file name (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
|
|
| |
| sock1 = NET_TcpConnect(ip,port); |
| if (NULL == (ftpfile = fdopen(sock1,"r"))) { |
| ffpmsg ("Could not connect to passive port (ftp_file_exist)"); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
|
|
| |
| snprintf(tmpstr,MAXLEN,"RETR %s\r\n",newfn); |
| status = NET_SendRaw(sock,tmpstr,strlen(tmpstr),NET_DEFAULT); |
|
|
| if (ftp_status(command,"150 ")) { |
| fclose(ftpfile); |
| NET_SendRaw(sock1,"QUIT\r\n",6,NET_DEFAULT); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
| |
| |
|
|
| fclose(ftpfile); |
| NET_SendRaw(sock1,"QUIT\r\n",6,NET_DEFAULT); |
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 1; |
| } |
| |
| |
|
|
| fclose(command); |
| NET_SendRaw(sock,"QUIT\r\n",6,NET_DEFAULT); |
| return 0; |
| } |
|
|
| |
| |
| int NET_TcpConnect(char *hostname, int port) |
| { |
| |
| |
| struct sockaddr_in sockaddr; |
| int sock; |
| int stat; |
| int val = 1; |
| |
| CreateSocketAddress(&sockaddr,hostname,port); |
| |
| if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
| ffpmsg("ERROR: NET_TcpConnect can't create socket"); |
| return CONNECTION_ERROR; |
| } |
| |
| if ((stat = connect(sock, (struct sockaddr*) &sockaddr, |
| sizeof(sockaddr))) |
| < 0) { |
| close(sock); |
| |
| |
| |
| |
| return CONNECTION_ERROR; |
| } |
| setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
| setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&val, sizeof(val)); |
|
|
| val = 65536; |
| setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&val, sizeof(val)); |
| setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&val, sizeof(val)); |
| return sock; |
| } |
|
|
| |
| |
| static int NET_SendRaw(int sock, const void *buffer, int length, int opt) |
| { |
|
|
| char * buf = (char *) buffer; |
| |
| int flag; |
| int n, nsent = 0; |
| |
| switch (opt) { |
| case NET_DEFAULT: |
| flag = 0; |
| break; |
| case NET_OOB: |
| flag = MSG_OOB; |
| break; |
| case NET_PEEK: |
| default: |
| flag = 0; |
| break; |
| } |
| |
| if (sock < 0) return -1; |
| |
| for (n = 0; n < length; n += nsent) { |
| if ((nsent = send(sock, buf+n, length-n, flag)) <= 0) { |
| return nsent; |
| } |
| } |
|
|
| return n; |
| } |
|
|
| |
|
|
| static int NET_RecvRaw(int sock, void *buffer, int length) |
| { |
| |
| |
|
|
|
|
| int nrecv, n; |
| char *buf = (char *)buffer; |
|
|
| if (sock < 0) return -1; |
| for (n = 0; n < length; n += nrecv) { |
| while ((nrecv = recv(sock, buf+n, length-n, 0)) == -1 && errno == EINTR) |
| errno = 0; |
| if (nrecv < 0) |
| return nrecv; |
| else if (nrecv == 0) |
| break; |
| } |
|
|
| return n; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| static int NET_ParseUrl(const char *url, char *proto, char *host, int *port, |
| char *fn) |
| { |
| |
| |
|
|
| char *urlcopy, *urlcopyorig; |
| char *ptrstr; |
| char *thost; |
| int isftp = 0; |
|
|
| |
|
|
| urlcopyorig = urlcopy = (char *) malloc(strlen(url)+1); |
| strcpy(urlcopy,url); |
|
|
| |
| *port = 80; |
| strcpy(proto,"http:"); |
| strcpy(host,"localhost"); |
| strcpy(fn,"/"); |
| |
| ptrstr = strstr(urlcopy,"http:"); |
| if (ptrstr == NULL) { |
| |
| ptrstr = strstr(urlcopy,"root:"); |
| if (ptrstr == NULL) { |
| |
| ptrstr = strstr(urlcopy,"ftp:"); |
| if (ptrstr != NULL) { |
| if (ptrstr == urlcopy) { |
| strcpy(proto,"ftp:"); |
| *port = 21; |
| isftp++; |
| urlcopy += 4; |
| } else { |
| |
| free(urlcopyorig); |
| return 1; |
| } |
| } |
| } else { |
| if (ptrstr == urlcopy) { |
| urlcopy += 5; |
| } else { |
| |
| free(urlcopyorig); |
| return 1; |
| } |
| } |
| } else { |
| if (ptrstr == urlcopy) { |
| urlcopy += 5; |
| } else { |
| free(urlcopyorig); |
| return 1; |
| } |
| } |
|
|
| |
| |
| if (urlcopy[0] == '/' && urlcopy[1] == '/') { |
| |
| urlcopy += 2; |
| } |
| |
| if (!strcmp(proto,"http:")) { |
|
|
| |
| if ((thost = strchr(urlcopy, '@')) != NULL) |
| urlcopy = thost+1; |
|
|
| if (strlen(urlcopy) > SHORTLEN-1) |
| { |
| free(urlcopyorig); |
| return 1; |
| } |
| strcpy(host,urlcopy); |
| thost = host; |
| while (*urlcopy != '/' && *urlcopy != ':' && *urlcopy) { |
| thost++; |
| urlcopy++; |
| } |
| |
| *thost = '\0'; |
| if (*urlcopy == ':') { |
| |
| urlcopy++; |
| sscanf(urlcopy,"%d",port); |
| while (*urlcopy != '/' && *urlcopy) urlcopy++; |
| } |
| } else { |
| |
| if (strlen(urlcopy) > SHORTLEN-1) |
| { |
| free(urlcopyorig); |
| return 1; |
| } |
| strcpy(host,urlcopy); |
| thost = host; |
| while (*urlcopy != '/' && *urlcopy) { |
| thost++; |
| urlcopy++; |
| } |
| *thost = '\0'; |
| |
| |
| } |
| |
|
|
| if (*urlcopy) { |
| if (strlen(urlcopy) > MAXLEN-1) |
| { |
| free(urlcopyorig); |
| return 1; |
| } |
| strcpy(fn,urlcopy); |
| } |
| free(urlcopyorig); |
| return 0; |
| } |
|
|
| |
| int http_checkfile (char *urltype, char *infile, char *outfile1) |
| { |
|
|
| |
| |
|
|
| char newinfile[MAXLEN]; |
| FILE *httpfile; |
| char contentencoding[MAXLEN]; |
| int contentlength; |
| int foundfile = 0; |
|
|
| |
| strcpy(urltype,"http://"); |
|
|
| if (strlen(outfile1)) { |
| |
| if (!strncmp(outfile1, "file://", 7) ) { |
| strcpy(netoutfile,outfile1+7); |
| } else { |
| strcpy(netoutfile,outfile1); |
| } |
| } |
|
|
| if (strstr(infile, "?")) { |
| |
| |
| |
| |
| if (strlen(outfile1)) { |
| strcpy(urltype,"httpfile://"); |
|
|
| |
| if (!strncmp(outfile1, "file://", 7) ) { |
| strcpy(netoutfile,outfile1+7); |
| } else { |
| strcpy(netoutfile,outfile1); |
| } |
| } |
| return 0; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| if (!strstr(infile,".gz") && (!strstr(infile,".Z"))) { |
| |
| |
| |
| if (strlen(infile) + 3 > MAXLEN-1) |
| { |
| return URL_PARSE_ERROR; |
| } |
| strcpy(newinfile,infile); |
| strcat(newinfile,".gz"); |
|
|
| if (!http_open_network(newinfile,&httpfile,contentencoding, |
| &contentlength)) { |
| if (!strcmp(contentencoding, "ftp://")) { |
| |
| |
| |
| |
| if (ftp_file_exist(newinfile)) { |
| |
| strcpy(urltype, "ftp://"); |
| strcpy(infile,newinfile); |
|
|
| if (strlen(outfile1)) { |
| |
|
|
| if (!strncmp(outfile1, "mem:", 4) ) { |
| |
| |
| |
|
|
| strcpy(urltype, "ftpmem://"); |
| } else { |
| |
| if (strstr(outfile1,".gz") || (strstr(outfile1,".Z"))) { |
| strcpy(urltype,"ftpcompress://"); |
| } else { |
| strcpy(urltype,"ftpfile://"); |
| } |
| } |
| } |
|
|
| return 0; |
| } |
| |
| } else if (!strcmp(contentencoding, "https://")) { |
| |
| https_checkfile(urltype, infile, outfile1); |
| |
| |
| |
| return 0; |
| } else { |
| |
| fclose(httpfile); |
| foundfile = 1; |
| strcpy(infile,newinfile); |
| } |
| } |
|
|
| if (!foundfile) { |
| |
| |
| if (strlen(infile+2) > MAXLEN-1) |
| { |
| return URL_PARSE_ERROR; |
| } |
| strcpy(newinfile,infile); |
| strcat(newinfile,".Z"); |
| if (!http_open_network(newinfile,&httpfile,contentencoding, |
| &contentlength)) { |
|
|
| if (!strcmp(contentencoding, "ftp://")) { |
| |
| |
| |
| |
| if (ftp_file_exist(newinfile)) { |
| |
| strcpy(urltype, "ftp://"); |
| strcpy(infile,newinfile); |
|
|
| if (strlen(outfile1)) { |
| |
|
|
| if (!strncmp(outfile1, "mem:", 4) ) { |
| |
| |
| |
|
|
| strcpy(urltype, "ftpmem://"); |
| } else { |
| |
| if (strstr(outfile1,".gz") || (strstr(outfile1,".Z"))) { |
| strcpy(urltype,"ftpcompress://"); |
| } else { |
| strcpy(urltype,"ftpfile://"); |
| } |
| } |
| } |
| return 0; |
| } |
| |
| } else { |
| |
| fclose(httpfile); |
| foundfile = 1; |
| strcpy(infile,newinfile); |
| } |
| } |
| } |
| } |
|
|
| if (!foundfile) { |
| |
| |
| strcpy(newinfile,infile); |
| if (!http_open_network(newinfile,&httpfile,contentencoding, |
| &contentlength)) { |
|
|
| if (!strcmp(contentencoding, "ftp://")) { |
| |
| |
| |
| |
| if (ftp_file_exist(newinfile)) { |
| |
| strcpy(urltype, "ftp://"); |
| strcpy(infile,newinfile); |
|
|
| if (strlen(outfile1)) { |
| |
|
|
| if (!strncmp(outfile1, "mem:", 4) ) { |
| |
| |
| |
|
|
| strcpy(urltype, "ftpmem://"); |
| return 0; |
| } else { |
|
|
| |
| strcpy(urltype,"ftpfile://"); |
| } |
| } |
| return 0; |
| } |
| |
| } else if (!strcmp(contentencoding, "https://")) { |
| |
| https_checkfile(urltype, infile, outfile1); |
| |
| |
| |
| return 0; |
| } else { |
| |
| fclose(httpfile); |
| foundfile = 1; |
| strcpy(infile,newinfile); |
| } |
|
|
| } |
| } |
|
|
| if (!foundfile) { |
| return (FILE_NOT_OPENED); |
| } |
|
|
| if (strlen(outfile1)) { |
| |
|
|
| if (!strncmp(outfile1, "mem:", 4) ) { |
| |
| |
| |
|
|
| strcpy(urltype, "httpmem://"); |
| return 0; |
| } |
|
|
| if (strstr(infile, "?")) { |
| |
| strcpy(urltype,"httpfile://"); |
| return 0; |
| } |
|
|
| if (strstr(infile,".gz") || (strstr(infile,".Z"))) { |
| |
| if (strstr(outfile1,".gz") || (strstr(outfile1,".Z"))) { |
| strcpy(urltype,"httpcompress://"); |
| } else { |
| strcpy(urltype,"httpfile://"); |
| } |
| } else { |
| strcpy(urltype,"httpfile://"); |
| } |
| } |
| return 0; |
| } |
|
|
| |
| int https_checkfile (char *urltype, char *infile, char *outfile1) |
| { |
| |
| strcpy(urltype,"https://"); |
| |
| if (strlen(outfile1)) |
| { |
| |
| if (!strncmp(outfile1, "file://", 7) ) { |
| strcpy(netoutfile,outfile1+7); |
| } else { |
| strcpy(netoutfile,outfile1); |
| } |
| |
| if (!strncmp(outfile1, "mem:", 4)) |
| strcpy(urltype,"httpsmem://"); |
| else |
| strcpy(urltype,"httpsfile://"); |
| } |
|
|
| return 0; |
| } |
|
|
| |
| int ftp_checkfile (char *urltype, char *infile, char *outfile1) |
| { |
| char newinfile[MAXLEN]; |
| FILE *ftpfile; |
| FILE *command; |
| int sock; |
| int foundfile = 0; |
|
|
| |
|
|
| |
| strcpy(urltype,"ftp://"); |
|
|
| if (!strstr(infile,".gz") && (!strstr(infile,".Z"))) { |
| |
| |
| |
| if (strlen(infile)+3 > MAXLEN-1) |
| { |
| return URL_PARSE_ERROR; |
| } |
| strcpy(newinfile,infile); |
| strcat(newinfile,".gz"); |
| |
| |
| if (ftp_file_exist(newinfile)) { |
| foundfile = 1; |
| strcpy(infile,newinfile); |
| } |
|
|
| if (!foundfile) { |
| if (strlen(infile)+2 > MAXLEN-1) |
| { |
| return URL_PARSE_ERROR; |
| } |
| strcpy(newinfile,infile); |
| strcat(newinfile,".Z"); |
| |
| |
| if (ftp_file_exist(newinfile)) { |
| foundfile = 1; |
| strcpy(infile,newinfile); |
| } |
| } |
| } |
|
|
| if (!foundfile) { |
| strcpy(newinfile,infile); |
| |
| |
| if (ftp_file_exist(newinfile)) { |
| foundfile = 1; |
| strcpy(infile,newinfile); |
| } |
| } |
|
|
| if (!foundfile) { |
| return (FILE_NOT_OPENED); |
| } |
|
|
| if (strlen(outfile1)) { |
| |
|
|
| |
| if (!strncmp(outfile1, "file://", 7) ) |
| strcpy(netoutfile,outfile1+7); |
| else |
| strcpy(netoutfile,outfile1); |
|
|
| if (!strncmp(outfile1, "mem:", 4) ) { |
| |
| |
| |
|
|
| strcpy(urltype, "ftpmem://"); |
| return 0; |
| } |
| |
| if (strstr(infile,".gz") || (strstr(infile,".Z"))) { |
| |
| if (strstr(outfile1,".gz") || (strstr(outfile1,".Z"))) { |
| strcpy(urltype,"ftpcompress://"); |
| } else { |
| strcpy(urltype,"ftpfile://"); |
| } |
| } else { |
| strcpy(urltype,"ftpfile://"); |
| } |
| } |
| return 0; |
| } |
| |
| |
| |
| static int ftp_status(FILE *ftp, char *statusstr) |
| { |
| |
| |
|
|
| char recbuf[MAXLEN], errorstr[SHORTLEN]; |
| int len; |
|
|
| len = strlen(statusstr); |
| while (1) { |
|
|
| if (!(fgets(recbuf,MAXLEN,ftp))) { |
| snprintf(errorstr,SHORTLEN,"ERROR: ftp_status wants %s but fgets returned 0",statusstr); |
| ffpmsg(errorstr); |
| return 1; |
| } |
|
|
| recbuf[len] = '\0'; |
| if (!strcmp(recbuf,statusstr)) { |
| return 0; |
| } |
| if (recbuf[0] > '3') { |
| |
| snprintf(errorstr,SHORTLEN,"ERROR ftp_status wants %s but got %s", statusstr, recbuf); |
| ffpmsg(errorstr); |
| return 1; |
| } |
| snprintf(errorstr,SHORTLEN,"ERROR ftp_status wants %s but got unexpected %s", statusstr, recbuf); |
| ffpmsg(errorstr); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| static int |
| CreateSocketAddress( |
| struct sockaddr_in *sockaddrPtr, |
| char *host, |
| int port) |
| { |
| struct hostent *hostent; |
| struct in_addr addr; |
| char localhost[MAXLEN]; |
|
|
| strcpy(localhost,host); |
|
|
| memset((void *) sockaddrPtr, '\0', sizeof(struct sockaddr_in)); |
| sockaddrPtr->sin_family = AF_INET; |
| sockaddrPtr->sin_port = htons((unsigned short) (port & 0xFFFF)); |
| if (host == NULL) { |
| addr.s_addr = INADDR_ANY; |
| } else { |
| addr.s_addr = inet_addr(localhost); |
| if (addr.s_addr == 0xFFFFFFFF) { |
| hostent = gethostbyname(localhost); |
| if (hostent != NULL) { |
| memcpy((void *) &addr, |
| (void *) hostent->h_addr_list[0], |
| (size_t) hostent->h_length); |
| } else { |
| #ifdef EHOSTUNREACH |
| errno = EHOSTUNREACH; |
| #else |
| #ifdef ENXIO |
| errno = ENXIO; |
| #endif |
| #endif |
| return 0; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
|
|
| sockaddrPtr->sin_addr.s_addr = addr.s_addr; |
| return 1; |
| } |
|
|
| |
|
|
| static void signal_handler(int sig) { |
|
|
| switch (sig) { |
| case SIGALRM: |
| longjmp(env,sig); |
| |
| default: { |
| |
| exit(sig); |
| } |
| } |
| } |
|
|
| |
|
|
| |
|
|
| |
| int root_init(void) |
| { |
| int ii; |
|
|
| for (ii = 0; ii < NMAXFILES; ii++) |
| { |
| handleTable[ii].sock = 0; |
| handleTable[ii].currentpos = 0; |
| } |
| return(0); |
| } |
| |
| int root_setoptions(int options) |
| { |
| |
| options = 0; |
| return(options); |
| } |
| |
| int root_getoptions(int *options) |
| { |
| *options = 0; |
| return(0); |
| } |
| |
| int root_getversion(int *version) |
| { |
| *version = 10; |
| return(0); |
| } |
| |
| int root_shutdown(void) |
| { |
| return(0); |
| } |
| |
| int root_open(char *url, int rwmode, int *handle) |
| { |
| int ii, status; |
| int sock; |
|
|
| *handle = -1; |
| for (ii = 0; ii < NMAXFILES; ii++) |
| { |
| if (handleTable[ii].sock == 0) |
| { |
| *handle = ii; |
| break; |
| } |
| } |
|
|
| if (*handle == -1) |
| return(TOO_MANY_FILES); |
|
|
| |
| if (rwmode) { |
| status = root_openfile(url, "update", &sock); |
| } else { |
| status = root_openfile(url, "read", &sock); |
| } |
| if (status) |
| return(status); |
| |
| handleTable[ii].sock = sock; |
| handleTable[ii].currentpos = 0; |
| |
| return(0); |
| } |
| |
| int root_create(char *filename, int *handle) |
| { |
| int ii, status; |
| int sock; |
|
|
| *handle = -1; |
| for (ii = 0; ii < NMAXFILES; ii++) |
| { |
| if (handleTable[ii].sock == 0) |
| { |
| *handle = ii; |
| break; |
| } |
| } |
|
|
| if (*handle == -1) |
| return(TOO_MANY_FILES); |
|
|
| |
| status = root_openfile(filename, "create", &sock); |
|
|
| if (status) { |
| ffpmsg("Unable to create file"); |
| return(status); |
| } |
| |
| handleTable[ii].sock = sock; |
| handleTable[ii].currentpos = 0; |
| |
| return(0); |
| } |
| |
| int root_size(int handle, LONGLONG *filesize) |
| |
| |
| |
| { |
|
|
| int sock; |
| int offset; |
| int status; |
| int op; |
|
|
| sock = handleTable[handle].sock; |
|
|
| status = root_send_buffer(sock,ROOTD_STAT,NULL,0); |
| status = root_recv_buffer(sock,&op,(char *)&offset, 4); |
| *filesize = (LONGLONG) ntohl(offset); |
| |
| return(0); |
| } |
| |
| int root_close(int handle) |
| |
| |
| |
| { |
|
|
| int status; |
| int sock; |
|
|
| sock = handleTable[handle].sock; |
| status = root_send_buffer(sock,ROOTD_CLOSE,NULL,0); |
| close(sock); |
| handleTable[handle].sock = 0; |
| return(0); |
| } |
| |
| int root_flush(int handle) |
| |
| |
| |
| { |
| int status; |
| int sock; |
|
|
| sock = handleTable[handle].sock; |
| status = root_send_buffer(sock,ROOTD_FLUSH,NULL,0); |
| return(0); |
| } |
| |
| int root_seek(int handle, LONGLONG offset) |
| |
| |
| |
| { |
| handleTable[handle].currentpos = offset; |
| return(0); |
| } |
| |
| int root_read(int hdl, void *buffer, long nbytes) |
| |
| |
| |
| { |
| char msg[SHORTLEN]; |
| int op; |
| int status; |
| int astat; |
|
|
| |
| snprintf(msg,SHORTLEN,"%ld %ld ",(long) handleTable[hdl].currentpos,nbytes); |
| status = root_send_buffer(handleTable[hdl].sock,ROOTD_GET,msg,strlen(msg)); |
| if ((unsigned) status != strlen(msg)) { |
| return (READ_ERROR); |
| } |
| astat = 0; |
| status = root_recv_buffer(handleTable[hdl].sock,&op,(char *) &astat,4); |
| if (astat != 0) { |
| return (READ_ERROR); |
| } |
|
|
| status = NET_RecvRaw(handleTable[hdl].sock,buffer,nbytes); |
| if (status != nbytes) { |
| return (READ_ERROR); |
| } |
| handleTable[hdl].currentpos += nbytes; |
|
|
| return(0); |
| } |
| |
| int root_write(int hdl, void *buffer, long nbytes) |
| |
| |
| |
| { |
|
|
| char msg[SHORTLEN]; |
| int len; |
| int sock; |
| int status; |
| int astat; |
| int op; |
|
|
| sock = handleTable[hdl].sock; |
| |
| snprintf(msg,SHORTLEN,"%ld %ld ",(long) handleTable[hdl].currentpos,nbytes); |
|
|
| len = strlen(msg); |
| status = root_send_buffer(sock,ROOTD_PUT,msg,len+1); |
| if (status != len+1) { |
| return (WRITE_ERROR); |
| } |
| status = NET_SendRaw(sock,buffer,nbytes,NET_DEFAULT); |
| if (status != nbytes) { |
| return (WRITE_ERROR); |
| } |
| astat = 0; |
| status = root_recv_buffer(handleTable[hdl].sock,&op,(char *) &astat,4); |
|
|
| if (astat != 0) { |
| return (WRITE_ERROR); |
| } |
| handleTable[hdl].currentpos += nbytes; |
| return(0); |
| } |
|
|
| |
| int root_openfile(char *url, char *rwmode, int *sock) |
| |
| |
| |
| { |
| |
| int status; |
| char recbuf[MAXLEN]; |
| char errorstr[MAXLEN]; |
| char proto[SHORTLEN]; |
| char host[SHORTLEN]; |
| char fn[MAXLEN]; |
| char turl[MAXLEN]; |
| int port; |
| int op; |
| int ii; |
| int authstat; |
| |
| |
| |
| if (strlen(url)+7 > MAXLEN-1) |
| { |
| ffpmsg("Error: url too long"); |
| return(FILE_NOT_OPENED); |
| } |
| strcpy(turl,"root://"); |
| strcat(turl,url); |
| if (NET_ParseUrl(turl,proto,host,&port,fn)) { |
| snprintf(errorstr,MAXLEN,"URL Parse Error (root_open) %s",url); |
| ffpmsg(errorstr); |
| return (FILE_NOT_OPENED); |
| } |
| |
| |
| *sock = NET_TcpConnect(host,port); |
| if (*sock < 0) { |
| ffpmsg("Couldn't connect to host (root_openfile)"); |
| return (FILE_NOT_OPENED); |
| } |
| |
| |
| if (NULL != getenv("ROOTUSERNAME")) { |
| if (strlen(getenv("ROOTUSERNAME")) > MAXLEN-1) |
| { |
| ffpmsg("root user name too long (root_openfile)"); |
| return (FILE_NOT_OPENED); |
| } |
| strcpy(recbuf,getenv("ROOTUSERNAME")); |
| } else { |
| printf("Username: "); |
| fgets(recbuf,MAXLEN,stdin); |
| recbuf[strlen(recbuf)-1] = '\0'; |
| } |
| |
| status = root_send_buffer(*sock, ROOTD_USER, recbuf,strlen(recbuf)); |
| if (status < 0) { |
| ffpmsg("error talking to remote system on username "); |
| return (FILE_NOT_OPENED); |
| } |
| |
| status = root_recv_buffer(*sock,&op,(char *)&authstat,4); |
| if (!status) { |
| ffpmsg("error talking to remote system on username"); |
| return (FILE_NOT_OPENED); |
| } |
| |
| if (op != ROOTD_AUTH) { |
| ffpmsg("ERROR on ROOTD_USER"); |
| ffpmsg(recbuf); |
| return (FILE_NOT_OPENED); |
| } |
| |
|
|
| |
| if (NULL != getenv("ROOTPASSWORD")) { |
| if (strlen(getenv("ROOTPASSWORD")) > MAXLEN-1) |
| { |
| ffpmsg("root password too long (root_openfile)"); |
| return (FILE_NOT_OPENED); |
| } |
| strcpy(recbuf,getenv("ROOTPASSWORD")); |
| } else { |
| printf("Password: "); |
| fgets(recbuf,MAXLEN,stdin); |
| recbuf[strlen(recbuf)-1] = '\0'; |
| } |
| |
| for (ii=0;(unsigned) ii<strlen(recbuf);ii++) { |
| recbuf[ii] = ~recbuf[ii]; |
| } |
| |
| status = root_send_buffer(*sock, ROOTD_PASS, recbuf, strlen(recbuf)); |
| if (status < 0) { |
| ffpmsg("error talking to remote system sending password"); |
| return (FILE_NOT_OPENED); |
| } |
| |
| status = root_recv_buffer(*sock,&op,(char *)&authstat,4); |
| if (status < 0) { |
| ffpmsg("error talking to remote system acking password"); |
| return (FILE_NOT_OPENED); |
| } |
| |
| if (op != ROOTD_AUTH) { |
| ffpmsg("ERROR on ROOTD_PASS"); |
| ffpmsg(recbuf); |
| return (FILE_NOT_OPENED); |
| } |
| |
| |
| if (strlen(fn)+strlen(rwmode)+1 > MAXLEN-1) |
| { |
| ffpmsg("root file name too long (root_openfile)"); |
| return (FILE_NOT_OPENED); |
| } |
| strcpy(recbuf,fn); |
| strcat(recbuf," "); |
| strcat(recbuf,rwmode); |
|
|
| status = root_send_buffer(*sock, ROOTD_OPEN, recbuf, strlen(recbuf)); |
| if (status < 0) { |
| ffpmsg("error talking to remote system on open "); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| status = root_recv_buffer(*sock,&op,(char *)&authstat,4); |
| if (status < 0) { |
| ffpmsg("error talking to remote system on open"); |
| return (FILE_NOT_OPENED); |
| } |
| |
| if ((op != ROOTD_OPEN) && (authstat != 0)) { |
| ffpmsg("ERROR on ROOTD_OPEN"); |
| ffpmsg(recbuf); |
| return (FILE_NOT_OPENED); |
| } |
|
|
| return 0; |
|
|
| } |
|
|
| static int root_send_buffer(int sock, int op, char *buffer, int buflen) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| int len; |
| int status; |
|
|
| int hdr[2]; |
|
|
| len = 4; |
|
|
| if (buffer != NULL) { |
| len += buflen; |
| } |
| |
| hdr[0] = htonl(len); |
| hdr[1] = htonl(op); |
|
|
| status = NET_SendRaw(sock,hdr,sizeof(hdr),NET_DEFAULT); |
| if (status < 0) { |
| return status; |
| } |
| if (buffer != NULL) { |
| status = NET_SendRaw(sock,buffer,buflen,NET_DEFAULT); |
| } |
| return status; |
| } |
| |
| static int root_recv_buffer(int sock, int *op, char *buffer, int buflen) |
| { |
| |
| |
| |
| |
| |
|
|
| int recv1 = 0; |
| int len; |
| int status; |
| char recbuf[MAXLEN]; |
|
|
| status = NET_RecvRaw(sock,&len,4); |
|
|
| if (status < 0) { |
| return status; |
| } |
| recv1 += status; |
|
|
| len = ntohl(len); |
|
|
| |
| len -= 4; |
| status = NET_RecvRaw(sock,op,4); |
| if (status < 0) { |
| return status; |
| } |
|
|
| recv1 += status; |
|
|
| *op = ntohl(*op); |
| |
| if (len > MAXLEN) { |
| len = MAXLEN; |
| } |
|
|
| if (len > 0) { |
| status = NET_RecvRaw(sock,recbuf,len); |
| if (len > buflen) { |
| len = buflen; |
| } |
| memcpy(buffer,recbuf,len); |
| if (status < 0) { |
| return status; |
| } |
| } |
|
|
| recv1 += status; |
| return recv1; |
|
|
| } |
|
|
| |
| |
| |
| |
|
|
|
|
| static int encode64(unsigned s_len, char *src, unsigned d_len, char *dst) { |
|
|
| static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| "abcdefghijklmnopqrstuvwxyz" |
| "0123456789" |
| "+/"; |
|
|
| unsigned triad; |
|
|
|
|
| for (triad = 0; triad < s_len; triad += 3) { |
| unsigned long int sr; |
| unsigned byte; |
|
|
| for (byte = 0; (byte<3) && (triad+byte<s_len); ++byte) { |
| sr <<= 8; |
| sr |= (*(src+triad+byte) & 0xff); |
| } |
|
|
| |
| sr <<= (6-((8*byte)%6))%6; |
|
|
| if (d_len < 4) |
| return 1; |
|
|
| *(dst+0) = *(dst+1) = *(dst+2) = *(dst+3) = '='; |
| switch(byte) { |
| case 3: |
| *(dst+3) = base64[sr&0x3f]; |
| sr >>= 6; |
| case 2: |
| *(dst+2) = base64[sr&0x3f]; |
| sr >>= 6; |
| case 1: |
| *(dst+1) = base64[sr&0x3f]; |
| sr >>= 6; |
| *(dst+0) = base64[sr&0x3f]; |
| } |
| dst += 4; |
| d_len -= 4; |
| } |
|
|
| *dst = '\0'; |
| return 0; |
| } |
|
|
| #endif |
|
|