Annotation of libwww/Library/src/HTFTP.c, revision 1.18

1.1       timbl       1: /*                     File Transfer Protocol (FTP) Client
                      2: **                     for a WorldWideWeb browser
                      3: **                     ===================================
                      4: **
                      5: **     A cache of control connections is kept.
                      6: **
                      7: ** Note: Port allocation
                      8: **
                      9: **     It is essential that the port is allocated by the system, rather
                     10: **     than chosen in rotation by us (POLL_PORTS), or the following
                     11: **     problem occurs.
                     12: **
                     13: **     It seems that an attempt by the server to connect to a port which has
                     14: **     been used recently by a listen on the same socket, or by another
                     15: **     socket this or another process causes a hangup of (almost exactly)
                     16: **     one minute. Therefore, we have to use a rotating port number.
                     17: **     The problem remains that if the application is run twice in quick
                     18: **     succession, it will hang for what remains of a minute.
                     19: **
                     20: ** Authors
                     21: **     TBL     Tim Berners-lee <timbl@info.cern.ch>
                     22: **     DD      Denis DeLaRoca 310 825-4580 <CSP1DWD@mvs.oac.ucla.edu>
                     23: ** History:
                     24: **      2 May 91       Written TBL, as a part of the WorldWideWeb project.
                     25: **     15 Jan 92       Bug fix: close() was used for NETCLOSE for control soc
                     26: **     10 Feb 92       Retry if cached connection times out or breaks
                     27: **      8 Dec 92       Bug fix 921208 TBL after DD
                     28: **     17 Dec 92       Anon FTP password now just WWWuser@ suggested by DD
1.2       timbl      29: **                     fails on princeton.edu!
1.1       timbl      30: **
                     31: ** Options:
                     32: **     LISTEN          We listen, the other guy connects for data.
                     33: **                     Otherwise, other way round, but problem finding our
                     34: **                     internet address!
                     35: **
                     36: ** Bugs:
                     37: **     No binary mode! Always uses ASCII! 
                     38: */
                     39: 
                     40: #define LISTEN         /* @@@@ Test */
                     41: 
                     42: /*
                     43: BUGS:  @@@     Limit connection cache size!
                     44:                Error reporting to user.
                     45:                400 & 500 errors are acked by user with windows.
                     46:                Use configuration file for user names
                     47:                Prompt user for password
                     48:                
                     49: **             Note for portablility this version does not use select() and
                     50: **             so does not watch the control and data channels at the
                     51: **             same time.
                     52: */             
                     53: 
1.14      luotonen   54: #include "HTFormat.h"
1.2       timbl      55: #include "HTFTP.h"     /* Implemented here */
                     56: 
1.4       timbl      57: #define CR   FROMASCII('\015') /* Must be converted to ^M for transmission */
                     58: #define LF   FROMASCII('\012') /* Must be converted to ^J for transmission */
                     59: 
1.1       timbl      60: #define REPEAT_PORT    /* Give the port number for each file */
                     61: #define REPEAT_LISTEN  /* Close each listen socket and open a new one */
                     62: 
                     63: /* define POLL_PORTS            If allocation does not work, poll ourselves.*/
                     64: #define LISTEN_BACKLOG 2       /* Number of pending connect requests (TCP)*/
                     65: 
                     66: #define FIRST_TCP_PORT 1024    /* Region to try for a listening port */
                     67: #define LAST_TCP_PORT  5999    
                     68: 
                     69: #define LINE_LENGTH 256
                     70: #define COMMAND_LENGTH 256
                     71: 
                     72: #include "HTParse.h"
                     73: #include "HTUtils.h"
                     74: #include "tcp.h"
                     75: #include "HTTCP.h"
                     76: #include "HTAnchor.h"
1.2       timbl      77: #include "HTFile.h"    /* For HTFileFormat() */
1.6       secret     78: #include "HTBTree.h"
                     79: #include "HTChunk.h"
1.1       timbl      80: #ifndef IPPORT_FTP
                     81: #define IPPORT_FTP     21
                     82: #endif
                     83: 
                     84: #ifdef REMOVED_CODE
                     85: extern char *malloc();
                     86: extern void free();
                     87: extern char *strncpy();
                     88: #endif
                     89: 
                     90: typedef struct _connection {
                     91:     struct _connection *       next;   /* Link on list         */
                     92:     u_long                     addr;   /* IP address           */
                     93:     int                                socket; /* Socket number for communication */
                     94:     BOOL                       binary; /* Binary mode? */
1.13      timbl      95:     HTInputSocket *            isoc;
1.1       timbl      96: } connection;
                     97: 
                     98: #ifndef NIL
                     99: #define NIL 0
                    100: #endif
                    101: 
1.2       timbl     102: /*             Hypertext object building machinery
                    103: */
                    104: #include "HTML.h"
                    105: 
                    106: #define PUTC(c) (*targetClass.put_character)(target, c)
                    107: #define PUTS(s) (*targetClass.put_string)(target, s)
                    108: #define START(e) (*targetClass.start_element)(target, e, 0, 0)
                    109: #define END(e) (*targetClass.end_element)(target, e)
                    110: #define FREE_TARGET (*targetClass.free)(target)
                    111: struct _HTStructured {
                    112:        CONST HTStructuredClass *       isa;
                    113:        /* ... */
                    114: };
1.1       timbl     115: 
1.2       timbl     116: 
1.1       timbl     117: /*     Module-Wide Variables
                    118: **     ---------------------
                    119: */
                    120: PRIVATE connection * connections =0;   /* Linked list of connections */
                    121: PRIVATE char    response_text[LINE_LENGTH+1];/* Last response from NewsHost */
                    122: PRIVATE connection * control;          /* Current connection */
                    123: PRIVATE int    data_soc = -1;          /* Socket for data transfer =invalid */
                    124: 
                    125: #ifdef POLL_PORTS
                    126: PRIVATE        unsigned short  port_number = FIRST_TCP_PORT;
                    127: #endif
                    128: 
                    129: #ifdef LISTEN
                    130: PRIVATE int     master_socket = -1;    /* Listening socket = invalid   */
                    131: PRIVATE char   port_command[255];      /* Command for setting the port */
                    132: PRIVATE fd_set open_sockets;           /* Mask of active channels */
                    133: PRIVATE int    num_sockets;            /* Number of sockets to scan */
                    134: #else
                    135: PRIVATE        unsigned short  passive_port;   /* Port server specified for data */
                    136: #endif
                    137: 
                    138: 
                    139: #define DATA_BUFFER_SIZE 2048
                    140: PRIVATE char data_buffer[DATA_BUFFER_SIZE];            /* Input data buffer */
                    141: PRIVATE char * data_read_pointer;
                    142: PRIVATE char * data_write_pointer;
                    143: #define NEXT_DATA_CHAR next_data_char()
                    144: 
                    145: 
                    146: /*     Procedure: Read a character from the data connection
                    147: **     ----------------------------------------------------
                    148: */
                    149: PRIVATE char next_data_char
                    150: NOARGS
                    151: {
                    152:     int status;
                    153:     if (data_read_pointer >= data_write_pointer) {
                    154:        status = NETREAD(data_soc, data_buffer, DATA_BUFFER_SIZE);
                    155:        /* Get some more data */
                    156:        if (status <= 0) return (char)-1;
                    157:        data_write_pointer = data_buffer + status;
                    158:        data_read_pointer = data_buffer;
                    159:     }
                    160: #ifdef NOT_ASCII
                    161:     {
                    162:         char c = *data_read_pointer++;
                    163:        return FROMASCII(c);
                    164:     }
                    165: #else
                    166:     return *data_read_pointer++;
                    167: #endif
                    168: }
                    169: 
                    170: 
                    171: /*     Close an individual connection
                    172: **
                    173: */
                    174: #ifdef __STDC__
                    175: PRIVATE int close_connection(connection * con)
                    176: #else
                    177: PRIVATE int close_connection(con)
                    178:     connection *con;
                    179: #endif
                    180: {
                    181:     connection * scan;
                    182:     int status = NETCLOSE(con->socket);
1.18    ! frystyk   183:     
        !           184:     if(con->isoc) {
        !           185:        HTInputSocket_free(con->isoc);
        !           186:        con->isoc = NULL;
        !           187:     }
1.1       timbl     188:     if (TRACE) fprintf(stderr, "FTP: Closing control socket %d\n", con->socket);
                    189:     if (connections==con) {
                    190:         connections = con->next;
                    191:        return status;
                    192:     }
                    193:     for(scan=connections; scan; scan=scan->next) {
                    194:         if (scan->next == con) {
                    195:            scan->next = con->next;     /* Unlink */
                    196:            if (control==con) control = (connection*)0;
                    197:            return status;
                    198:        } /*if */
                    199:     } /* for */
                    200:     return -1;         /* very strange -- was not on list. */
                    201: }
                    202: 
                    203: 
                    204: /*     Execute Command and get Response
                    205: **     --------------------------------
                    206: **
                    207: **     See the state machine illustrated in RFC959, p57. This implements
                    208: **     one command/reply sequence.  It also interprets lines which are to
                    209: **     be continued, which are marked with a "-" immediately after the
                    210: **     status code.
                    211: **
1.8       timbl     212: **     Continuation then goes on until a line with a matching reply code
                    213: **     an a space after it.
                    214: **
1.1       timbl     215: ** On entry,
                    216: **     con     points to the connection which is established.
                    217: **     cmd     points to a command, or is NIL to just get the response.
                    218: **
                    219: **     The command is terminated with the CRLF pair.
                    220: **
                    221: ** On exit,
                    222: **     returns:  The first digit of the reply type,
                    223: **               or negative for communication failure.
                    224: */
1.13      timbl     225: PRIVATE int response ARGS1(char *, cmd)
                    226: 
1.1       timbl     227: {
                    228:     int result;                                /* Three-digit decimal code */
1.8       timbl     229:     int        continuation_response = -1;
1.1       timbl     230:     int status;
                    231:     
                    232:     if (!control) {
                    233:           if(TRACE) fprintf(stderr, "FTP: No control connection set up!!\n");
                    234:          return -99;
                    235:     }
                    236:     
                    237:     if (cmd) {
                    238:     
                    239:        if (TRACE) fprintf(stderr, "  Tx: %s", cmd);
                    240: 
                    241: #ifdef NOT_ASCII
                    242:        {
                    243:            char * p;
                    244:            for(p=cmd; *p; p++) {
                    245:                *p = TOASCII(*p);
                    246:            }
                    247:        }
                    248: #endif 
                    249:        status = NETWRITE(control->socket, cmd, (int)strlen(cmd));
                    250:        if (status<0) {
                    251:            if (TRACE) fprintf(stderr, 
                    252:                "FTP: Error %d sending command: closing socket %d\n",
                    253:                status, control->socket);
                    254:            close_connection(control);
                    255:            return status;
                    256:        }
                    257:     }
                    258: 
                    259:     do {
                    260:        char *p = response_text;
                    261:        for(;;) {  
1.13      timbl     262:            if (((*p++=HTInputSocket_getCharacter(control->isoc)) == LF)
1.1       timbl     263:                        || (p == &response_text[LINE_LENGTH])) {
1.8       timbl     264:                char continuation;
1.1       timbl     265:                *p++=0;                 /* Terminate the string */
                    266:                if (TRACE) fprintf(stderr, "    Rx: %s", response_text);
                    267:                sscanf(response_text, "%d%c", &result, &continuation);
1.8       timbl     268:                if  (continuation_response == -1) {
                    269:                        if (continuation == '-')  /* start continuation */
                    270:                            continuation_response = result;
                    271:                } else {        /* continuing */
                    272:                        if (continuation_response == result
                    273:                                && continuation == ' ')
                    274:                            continuation_response = -1; /* ended */
                    275:                }       
1.1       timbl     276:                break;      
                    277:            } /* if end of line */
                    278:            
1.12      timbl     279:            if (*(p-1) == (char) EOF) {
1.1       timbl     280:                if(TRACE) fprintf(stderr, "Error on rx: closing socket %d\n",
                    281:                    control->socket);
                    282:                strcpy(response_text, "000 *** TCP read error on response\n");
                    283:                close_connection(control);
                    284:                return -1;      /* End of file on response */
                    285:            }
                    286:        } /* Loop over characters */
                    287: 
1.8       timbl     288:     } while (continuation_response != -1);
1.1       timbl     289:     
                    290:     if (result==421) {
                    291:        if(TRACE) fprintf(stderr, "FTP: They close so we close socket %d\n",
                    292:            control->socket);
                    293:        close_connection(control);
                    294:        return -1;
                    295:     }
                    296:     return result/100;
                    297: }
                    298: 
                    299: 
                    300: /*     Get a valid connection to the host
                    301: **     ----------------------------------
                    302: **
                    303: ** On entry,
                    304: **     arg     points to the name of the host in a hypertext address
                    305: ** On exit,
                    306: **     returns <0 if error
                    307: **             socket number if success
                    308: **
                    309: **     This routine takes care of managing timed-out connections, and
                    310: **     limiting the number of connections in use at any one time.
                    311: **
                    312: **     It ensures that all connections are logged in if they exist.
                    313: **     It ensures they have the port number transferred.
                    314: */
                    315: PRIVATE int get_connection ARGS1 (CONST char *,arg)
                    316: {
                    317:     struct sockaddr_in soc_address;    /* Binary network address */
                    318:     struct sockaddr_in* sin = &soc_address;
                    319: 
                    320:     char * username=0;
                    321:     char * password=0;
                    322:     
                    323:     if (!arg) return -1;               /* Bad if no name sepcified     */
                    324:     if (!*arg) return -1;              /* Bad if name had zero length  */
                    325: 
                    326: /*  Set up defaults:
                    327: */
                    328:     sin->sin_family = AF_INET;                 /* Family, host order  */
                    329:     sin->sin_port = htons(IPPORT_FTP);         /* Well Known Number    */
                    330: 
                    331:     if (TRACE) fprintf(stderr, "FTP: Looking for %s\n", arg);
                    332: 
                    333: /* Get node name:
                    334: */
                    335:     {
                    336:        char *p1 = HTParse(arg, "", PARSE_HOST);
1.2       timbl     337:        char *p2 = strrchr(p1, '@');    /* user? */
1.1       timbl     338:        char * pw;
                    339:        if (p2) {
                    340:            username = p1;
                    341:            *p2=0;                      /* terminate */
                    342:            p1 = p2+1;                  /* point to host */
                    343:            pw = strchr(username, ':');
                    344:            if (pw) {
                    345:                *pw++ = 0;
                    346:                password = pw;
                    347:            }
                    348:        }
                    349:        if (HTParseInet(sin, p1)) { free(p1); return -1;} /* TBL 920622 */
                    350: 
                    351:         if (!username) free(p1);
                    352:     } /* scope of p1 */
                    353: 
                    354:         
                    355: /*     Now we check whether we already have a connection to that port.
                    356: */
                    357: 
                    358:     {
                    359:        connection * scan;
                    360:        for (scan=connections; scan; scan=scan->next) {
                    361:            if (sin->sin_addr.s_addr == scan->addr) {
                    362:                if (TRACE) fprintf(stderr, 
                    363:                "FTP: Already have connection for %d.%d.%d.%d.\n",
                    364:                    (int)*((unsigned char *)(&scan->addr)+0),
                    365:                    (int)*((unsigned char *)(&scan->addr)+1),
                    366:                    (int)*((unsigned char *)(&scan->addr)+2),
                    367:                    (int)*((unsigned char *)(&scan->addr)+3));
                    368:                if (username) free(username);
                    369:                return scan->socket;            /* Good return */
                    370:            } else {
                    371:                if (TRACE) fprintf(stderr, 
                    372:                "FTP: Existing connection is %d.%d.%d.%d\n",
                    373:                    (int)*((unsigned char *)(&scan->addr)+0),
                    374:                    (int)*((unsigned char *)(&scan->addr)+1),
                    375:                    (int)*((unsigned char *)(&scan->addr)+2),
                    376:                    (int)*((unsigned char *)(&scan->addr)+3));
                    377:            }
                    378:        }
                    379:     }
                    380: 
                    381:    
                    382: /*     Now, let's get a socket set up from the server:
                    383: */      
                    384:     {
                    385:         int status;
1.18    ! frystyk   386:        connection * con = (connection *)calloc(1, sizeof(*con));
1.1       timbl     387:        if (con == NULL) outofmem(__FILE__, "get_connection");
                    388:        con->addr = sin->sin_addr.s_addr;       /* save it */
                    389:        con->binary = NO;
                    390:        status = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                    391:        if (status<0) {
                    392:            (void) HTInetStatus("socket");
                    393:            free(con);
                    394:            if (username) free(username);
                    395:            return status;
                    396:        }
                    397:        con->socket = status;
                    398: 
                    399:        status = connect(con->socket, (struct sockaddr*)&soc_address,
                    400:         sizeof(soc_address));
                    401:         if (status<0){
                    402:            (void) HTInetStatus("connect");
                    403:            if (TRACE) fprintf(stderr, 
                    404:                "FTP: Unable to connect to remote host for `%s'.\n",
                    405:                arg);
                    406:            NETCLOSE(con->socket);
                    407:            free(con);
                    408:            if (username) free(username);
                    409:            return status;                      /* Bad return */
                    410:        }
                    411:        
                    412:        if (TRACE) fprintf(stderr, "FTP connected, socket %d\n", con->socket);
                    413:        control = con;                  /* Current control connection */
                    414:        con->next = connections;        /* Link onto list of good ones */
                    415:        connections = con;
1.13      timbl     416:         con->isoc = HTInputSocket_new(con->socket); /* buffering */
1.1       timbl     417: 
                    418: 
                    419: /*     Now we log in           Look up username, prompt for pw.
                    420: */
                    421:        {
                    422:            int status = response(NIL); /* Get greeting */
                    423:            
                    424:            if (status == 2) {          /* Send username */
                    425:                char * command;
                    426:                if (username) {
                    427:                    command = (char*)malloc(10+strlen(username)+2+1);
                    428:                    if (command == NULL) outofmem(__FILE__, "get_connection");
1.4       timbl     429:                    sprintf(command, "USER %s%c%c", username, CR, LF);
1.1       timbl     430:                } else {
                    431:                    command = (char*)malloc(25);
                    432:                    if (command == NULL) outofmem(__FILE__, "get_connection");
1.4       timbl     433:                    sprintf(command, "USER anonymous%c%c", CR, LF);
1.1       timbl     434:                }
                    435:                status = response(command);
                    436:                free(command);
                    437:            }
                    438:            if (status == 3) {          /* Send password */
                    439:                char * command;
                    440:                if (password) {
                    441:                    command = (char*)malloc(10+strlen(password)+2+1);
                    442:                    if (command == NULL) outofmem(__FILE__, "get_connection");
1.4       timbl     443:                    sprintf(command, "PASS %s%c%c", password, CR, LF);
1.1       timbl     444:                } else {
1.10      timbl     445:                    char * user = getenv("USER");
                    446:                    CONST char *host = HTHostName();
                    447:                    if (!user) user = "WWWuser";
                    448:                    /* If not fully qualified, suppress it as ftp.uu.net
                    449:                       prefers a blank to a bad name */
                    450:                    if (!strchr(host, '.')) host = "";
                    451: 
                    452:                    command = (char*)malloc(20+strlen(host)+2+1);
1.1       timbl     453:                    if (command == NULL) outofmem(__FILE__, "get_connection");
                    454:                    sprintf(command,
1.10      timbl     455:                    "PASS %s@%s%c%c", user ? user : "WWWuser",
                    456:                    host, CR, LF); /*@@*/
1.1       timbl     457:                }
                    458:                status = response(command);
                    459:                free(command);
                    460:            }
                    461:             if (username) free(username);
                    462: 
1.4       timbl     463:            if (status == 3) {
                    464:                char temp[80];
                    465:                sprintf(temp, "ACCT noaccount%c%c", CR, LF);
                    466:                status = response(temp);
                    467:            }
1.1       timbl     468:            if (status !=2) {
                    469:                if (TRACE) fprintf(stderr, "FTP: Login fail: %s", response_text);
                    470:                if (control) close_connection(control);
                    471:                return -1;              /* Bad return */
                    472:            }
                    473:            if (TRACE) fprintf(stderr, "FTP: Logged in.\n");
                    474:        }
                    475: 
                    476: /*     Now we inform the server of the port number we will listen on
                    477: */
                    478: #ifndef REPEAT_PORT
                    479:        {
                    480:            int status = response(port_command);
                    481:            if (status !=2) {
                    482:                if (control) close_connection(control);
                    483:                return -status;         /* Bad return */
                    484:            }
                    485:            if (TRACE) fprintf(stderr, "FTP: Port defined.\n");
                    486:        }
                    487: #endif
                    488:        return con->socket;                     /* Good return */
                    489:     } /* Scope of con */
1.13      timbl     490: }  /* get_connection */
1.1       timbl     491: 
                    492: 
                    493: #ifdef LISTEN
                    494: 
                    495: /*     Close Master (listening) socket
                    496: **     -------------------------------
                    497: **
                    498: **
                    499: */
                    500: #ifdef __STDC__
                    501: PRIVATE int close_master_socket(void)
                    502: #else
                    503: PRIVATE int close_master_socket()
                    504: #endif
                    505: {
                    506:     int status;
                    507:     FD_CLR(master_socket, &open_sockets);
                    508:     status = NETCLOSE(master_socket);
                    509:     if (TRACE) fprintf(stderr, "FTP: Closed master socket %d\n", master_socket);
                    510:     master_socket = -1;
                    511:     if (status<0) return HTInetStatus("close master socket");
                    512:     else return status;
                    513: }
                    514: 
                    515: 
                    516: /*     Open a master socket for listening on
                    517: **     -------------------------------------
                    518: **
                    519: **     When data is transferred, we open a port, and wait for the server to
                    520: **     connect with the data.
                    521: **
                    522: ** On entry,
                    523: **     master_socket   Must be negative if not set up already.
                    524: ** On exit,
                    525: **     Returns         socket number if good
                    526: **                     less than zero if error.
                    527: **     master_socket   is socket number if good, else negative.
                    528: **     port_number     is valid if good.
                    529: */
                    530: #ifdef __STDC__
                    531: PRIVATE int get_listen_socket(void)
                    532: #else
                    533: PRIVATE int get_listen_socket()
                    534: #endif
                    535: {
                    536:     struct sockaddr_in soc_address;    /* Binary network address */
                    537:     struct sockaddr_in* sin = &soc_address;
                    538:     int new_socket;                    /* Will be master_socket */
                    539:     
                    540:     
                    541:     FD_ZERO(&open_sockets);    /* Clear our record of open sockets */
                    542:     num_sockets = 0;
                    543:     
                    544: #ifndef REPEAT_LISTEN
                    545:     if (master_socket>=0) return master_socket;  /* Done already */
                    546: #endif
                    547: 
                    548: /*  Create internet socket
                    549: */
                    550:     new_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                    551:        
                    552:     if (new_socket<0)
                    553:        return HTInetStatus("socket for master socket");
                    554:     
                    555:     if (TRACE) fprintf(stderr, "FTP: Opened master socket number %d\n", new_socket);
                    556:     
                    557: /*  Search for a free port.
                    558: */
                    559:     sin->sin_family = AF_INET;     /* Family = internet, host order  */
                    560:     sin->sin_addr.s_addr = INADDR_ANY; /* Any peer address */
                    561: #ifdef POLL_PORTS
                    562:     {
                    563:         unsigned short old_port_number = port_number;
                    564:        for(port_number=old_port_number+1;;port_number++){ 
                    565:            int status;
                    566:            if (port_number > LAST_TCP_PORT)
                    567:                port_number = FIRST_TCP_PORT;
                    568:            if (port_number == old_port_number) {
                    569:                return HTInetStatus("bind");
                    570:            }
                    571:            soc_address.sin_port = htons(port_number);
                    572:            if ((status=bind(new_socket,
                    573:                    (struct sockaddr*)&soc_address,
                    574:                            /* Cast to generic sockaddr */
                    575:                    sizeof(soc_address))) == 0)
                    576:                break;
                    577:            if (TRACE) fprintf(stderr, 
                    578:                "TCP bind attempt to port %d yields %d, errno=%d\n",
                    579:                port_number, status, errno);
                    580:        } /* for */
                    581:     }
                    582: #else
                    583:     {
                    584:         int status;
                    585:        int address_length = sizeof(soc_address);
                    586:        status = getsockname(control->socket,
                    587:                        (struct sockaddr *)&soc_address,
                    588:                         &address_length);
                    589:        if (status<0) return HTInetStatus("getsockname");
                    590:        CTRACE(tfp, "FTP: This host is %s\n",
                    591:            HTInetString(sin));
                    592:        
                    593:        soc_address.sin_port = 0;       /* Unspecified: please allocate */
                    594:        status=bind(new_socket,
                    595:                (struct sockaddr*)&soc_address,
                    596:                        /* Cast to generic sockaddr */
                    597:                sizeof(soc_address));
                    598:        if (status<0) return HTInetStatus("bind");
                    599:        
                    600:        address_length = sizeof(soc_address);
                    601:        status = getsockname(new_socket,
                    602:                        (struct sockaddr*)&soc_address,
                    603:                        &address_length);
                    604:        if (status<0) return HTInetStatus("getsockname");
                    605:     }
                    606: #endif    
                    607: 
                    608:     CTRACE(tfp, "FTP: bound to port %d on %s\n",
                    609:                (int)ntohs(sin->sin_port),
                    610:                HTInetString(sin));
                    611: 
                    612: #ifdef REPEAT_LISTEN
                    613:     if (master_socket>=0)
                    614:         (void) close_master_socket();
                    615: #endif    
                    616:     
                    617:     master_socket = new_socket;
                    618:     
                    619: /*     Now we must find out who we are to tell the other guy
                    620: */
                    621:     (void)HTHostName();        /* Make address valid - doesn't work*/
1.4       timbl     622:     sprintf(port_command, "PORT %d,%d,%d,%d,%d,%d%c%c",
1.1       timbl     623:                    (int)*((unsigned char *)(&sin->sin_addr)+0),
                    624:                    (int)*((unsigned char *)(&sin->sin_addr)+1),
                    625:                    (int)*((unsigned char *)(&sin->sin_addr)+2),
                    626:                    (int)*((unsigned char *)(&sin->sin_addr)+3),
                    627:                    (int)*((unsigned char *)(&sin->sin_port)+0),
1.4       timbl     628:                    (int)*((unsigned char *)(&sin->sin_port)+1),
                    629:                    CR, LF);
1.1       timbl     630: 
                    631: 
                    632: /*     Inform TCP that we will accept connections
                    633: */
                    634:     if (listen(master_socket, 1)<0) {
                    635:        master_socket = -1;
                    636:        return HTInetStatus("listen");
                    637:     }
                    638:     CTRACE(tfp, "TCP: Master socket(), bind() and listen() all OK\n");
                    639:     FD_SET(master_socket, &open_sockets);
                    640:     if ((master_socket+1) > num_sockets) num_sockets=master_socket+1;
                    641: 
                    642:     return master_socket;              /* Good */
                    643: 
                    644: } /* get_listen_socket */
                    645: #endif
                    646: 
                    647: 
                    648: 
                    649: /*     Read a directory into an hypertext object from the data socket
                    650: **     --------------------------------------------------------------
                    651: **
                    652: ** On entry,
1.2       timbl     653: **     anchor          Parent anchor to link the this node to
1.1       timbl     654: **     address         Address of the directory
                    655: ** On exit,
                    656: **     returns         HT_LOADED if OK
                    657: **                     <0 if error.
                    658: */
                    659: PRIVATE int read_directory
1.13      timbl     660: ARGS6 (
                    661:   HTRequest *,                 request,
                    662:   void *,                      param,
1.2       timbl     663:   HTParentAnchor *,            parent,
                    664:   CONST char *,                        address,
1.4       timbl     665:   HTFormat,                    format_out,
1.6       secret    666:   HTStream *,                  sink )
1.1       timbl     667: {
1.13      timbl     668:     HTStructured* target = HTML_new(request, NULL, WWW_HTML, format_out, sink);
1.6       secret    669:     HTStructuredClass targetClass;
                    670:     char *filename = HTParse(address, "", PARSE_PATH + PARSE_PUNCTUATION);
                    671:     char *lastpath;  /* prefix for link, either "" (for root) or xxx  */
                    672: 
                    673:     targetClass = *(target->isa);
                    674: 
                    675:     HTDirTitles(target, (HTAnchor*)parent);
1.2       timbl     676:   
1.6       secret    677:     data_read_pointer = data_write_pointer = data_buffer;
                    678: 
1.16      luotonen  679:     if (*filename == 0) { /* Empty filename : use root */
                    680:        lastpath = (char*)malloc(2);
1.6       secret    681:         strcpy (lastpath, "/");
1.16      luotonen  682:     }
1.6       secret    683:     else 
                    684:     {
                    685:         char * p = strrchr(filename, '/');  /* find lastslash */
                    686:         lastpath = (char*)malloc(strlen(p));
                    687:        if (!lastpath) outofmem(__FILE__, "read_directory");
                    688:         strcpy(lastpath, p+1);    /* take slash off the beginning */
                    689:     }
                    690:     free (filename);
                    691: 
                    692:    
                    693:     {
1.7       secret    694:         HTBTree * bt = HTBTree_new((HTComparer)strcasecomp);
1.6       secret    695:         char c;
                    696:        HTChunk * chunk = HTChunkCreate(128);
                    697:        START(HTML_DIR);
                    698:        for (c=0; c!=(char)EOF;)   /* For each entry in the directory */
                    699:        {
                    700:            char * filename = NULL;
                    701:            HTChunkClear(chunk);
                    702:            /*   read directory entry
                    703:             */
                    704:            for(;;) {                 /* Read in one line as filename */
                    705:                c = NEXT_DATA_CHAR;
                    706:                if (c == '\r' || c == LF) {    /* Terminator? */ 
                    707:                    if (chunk->size != 0)   /* got some text */
                    708:                      break;                /* finish getting one entry */
                    709:                  } else if (c == (char)EOF) {
                    710:                    break;             /* End of file */
                    711:                  } else {
                    712:                    HTChunkPutc(chunk, c);
                    713:                  }
                    714:             }
                    715:            HTChunkTerminate(chunk);
                    716:            if (c == (char) EOF && chunk->size == 1)  /* 1 means empty: includes terminating 0 */
                    717:                break;
                    718:             if(TRACE) fprintf(stderr, "HTFTP: file name in %s is %s\n", lastpath, chunk->data);
                    719:            StrAllocCopy(filename, chunk->data);
                    720:            HTBTree_add(bt,filename); /* sort filename in the tree bt */
                    721: 
                    722:        }  /* next entry */
                    723:         HTChunkFree(chunk);
1.1       timbl     724: 
1.6       secret    725:        /* Run through tree printing out in order 
                    726:         */
                    727:        {
                    728:            HTBTElement * ele;
                    729:            for (ele = HTBTree_next(bt, NULL);
                    730:                 ele != NULL;
                    731:                 ele = HTBTree_next(bt, ele))
                    732:            {
                    733:                START(HTML_LI);
                    734:                HTDirEntry(target, lastpath, (char *)HTBTree_object(ele));
                    735:            }
                    736:        }
                    737:        END(HTML_DIR);
                    738:        FREE_TARGET;
                    739:        HTBTreeAndObject_free(bt);
                    740:     }
1.2       timbl     741: 
1.6       secret    742:     if (lastpath) free(lastpath);
                    743:     return response(NIL) == 2 ? HT_LOADED : -1;
1.1       timbl     744: }
                    745: 
                    746: 
                    747: /*     Retrieve File from Server
                    748: **     -------------------------
                    749: **
                    750: ** On entry,
                    751: **     name            WWW address of a file: document, including hostname
                    752: ** On exit,
                    753: **     returns         Socket number for file if good.
                    754: **                     <0 if bad.
                    755: */
1.2       timbl     756: PUBLIC int HTFTPLoad
1.13      timbl     757: ARGS6 (
                    758:   HTRequest *,                 request,
                    759:   void *,                      param,
1.1       timbl     760:   CONST char *,                        name,
1.2       timbl     761:   HTParentAnchor *,            anchor,
                    762:   HTFormat,                    format_out,
                    763:   HTStream *,                  sink
1.1       timbl     764: )
                    765: {
                    766:     BOOL isDirectory = NO;
                    767:     int status;
                    768:     int retry;                 /* How many times tried? */
1.2       timbl     769:     HTFormat format;
1.1       timbl     770:     
                    771:     for (retry=0; retry<2; retry++) {  /* For timed out/broken connections */
                    772:     
                    773:        status = get_connection(name);
                    774:        if (status<0) return status;
                    775: 
                    776: #ifdef LISTEN
                    777:        status = get_listen_socket();
                    778:        if (status<0) return status;
                    779:     
                    780: #ifdef REPEAT_PORT
                    781: /*     Inform the server of the port number we will listen on
                    782: */
                    783:        {
                    784:            status = response(port_command);
                    785:            if (status !=2) {           /* Could have timed out */
                    786:                if (status<0) continue;         /* try again - net error*/
                    787:                return -status;                 /* bad reply */
                    788:            }
                    789:            if (TRACE) fprintf(stderr, "FTP: Port defined.\n");
                    790:        }
                    791: #endif
                    792: #else  /* Use PASV */
                    793: /*     Tell the server to be passive
                    794: */
                    795:        {
                    796:            char *p;
                    797:            int reply, h0, h1, h2, h3, p0, p1;  /* Parts of reply */
1.4       timbl     798:            status = response("PASV%c%c", CR, LF);
1.1       timbl     799:            if (status !=2) {
                    800:                if (status<0) continue;         /* retry or Bad return */
                    801:                return -status;                 /* bad reply */
                    802:            }
                    803:            for(p=response_text; *p; p++)
                    804:                if ((*p<'0')||(*p>'9')) *p = ' ';       /* Keep only digits */
                    805:            status = sscanf(response_text, "%d%d%d%d%d%d%d",
                    806:                    &reply, &h0, &h1, &h2, &h3, &p0, &p1);
                    807:            if (status<5) {
                    808:                if (TRACE) fprintf(stderr, "FTP: PASV reply has no inet address!\n");
                    809:                return -99;
                    810:            }
                    811:            passive_port = (p0<<8) + p1;
                    812:            if (TRACE) fprintf(stderr, "FTP: Server is listening on port %d\n",
                    813:                    passive_port);
                    814:        }
                    815: 
                    816: /*     Open connection for data:
                    817: */
                    818:        {
                    819:            struct sockaddr_in soc_address;
                    820:            int status = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                    821:            if (status<0) {
                    822:                (void) HTInetStatus("socket for data socket");
                    823:                return status;
                    824:            }
                    825:            data_soc = status;
                    826:            
                    827:            soc_address.sin_addr.s_addr = control->addr;
                    828:            soc_address.sin_port = htons(passive_port);
                    829:            soc_address.sin_family = AF_INET;       /* Family, host order  */
                    830:            if (TRACE) fprintf(stderr,  
                    831:                "FTP: Data remote address is port %d, inet %d.%d.%d.%d\n",
                    832:                        (unsigned int)ntohs(soc_address.sin_port),
                    833:                        (int)*((unsigned char *)(&soc_address.sin_addr)+0),
                    834:                        (int)*((unsigned char *)(&soc_address.sin_addr)+1),
                    835:                        (int)*((unsigned char *)(&soc_address.sin_addr)+2),
                    836:                        (int)*((unsigned char *)(&soc_address.sin_addr)+3));
                    837:     
                    838:            status = connect(data_soc, (struct sockaddr*)&soc_address,
                    839:                    sizeof(soc_address));
                    840:            if (status<0){
                    841:                (void) HTInetStatus("connect for data");
                    842:                NETCLOSE(data_soc);
                    843:                return status;                  /* Bad return */
                    844:            }
                    845:            
                    846:            if (TRACE) fprintf(stderr, "FTP data connected, socket %d\n", data_soc);
                    847:        }
                    848: #endif /* use PASV */
                    849:        status = 0;
                    850:         break; /* No more retries */
                    851: 
                    852:     } /* for retries */
                    853:     if (status<0) return status;       /* Failed with this code */
                    854:     
                    855: /*     Ask for the file:
                    856: */    
                    857:     {
                    858:         char *filename = HTParse(name, "", PARSE_PATH + PARSE_PUNCTUATION);
                    859:        char command[LINE_LENGTH+1];
1.17      luotonen  860:        BOOL binary = NO;
                    861: 
1.1       timbl     862:        if (!*filename) StrAllocCopy(filename, "/");
1.15      luotonen  863:        HTUnEscape(filename);   /* Fix 25.1.94 -- thanks to Lou Montulli */
                    864:        if (TRACE) fprintf(stderr, "FTP: UnEscaped %s\n", filename);
1.17      luotonen  865:        format = HTFileFormat(filename,
                    866:                              &request->content_encoding,
                    867:                              &request->content_language);
                    868:        binary = (request->content_encoding != HTAtom_for("8bit")
                    869:                  && request->content_encoding != HTAtom_for("7bit"));
1.9       timbl     870:         if (binary != control->binary) {
                    871:            char * mode = binary ? "I" : "A";
                    872:            sprintf(command, "TYPE %s%c%c", mode, CR, LF);
                    873:            status = response(command);
                    874:            if (status != 2) return -status;
                    875:            control->binary = binary;
                    876:        }
1.4       timbl     877:        sprintf(command, "RETR %s%c%c", filename, CR, LF);
1.1       timbl     878:        status = response(command);
                    879:        if (status != 1) {  /* Failed : try to CWD to it */
1.4       timbl     880:          sprintf(command, "CWD %s%c%c", filename, CR, LF);
1.1       timbl     881:          status = response(command);
                    882:          if (status == 2) {  /* Successed : let's NAME LIST it */
                    883:            isDirectory = YES;
1.4       timbl     884:            sprintf(command, "NLST%c%c", CR, LF);
1.1       timbl     885:            status = response (command);
                    886:          }
                    887:        }
                    888:        free(filename);
                    889:        if (status != 1) return -status;                /* Action not started */
                    890:     }
                    891: 
                    892: #ifdef LISTEN
                    893: /*     Wait for the connection
                    894: */
                    895:     {
                    896:        struct sockaddr_in soc_address;
                    897:         int    soc_addrlen=sizeof(soc_address);
                    898:        status = accept(master_socket,
                    899:                        (struct sockaddr *)&soc_address,
                    900:                        &soc_addrlen);
                    901:        if (status<0)
                    902:            return HTInetStatus("accept");
                    903:        CTRACE(tfp, "TCP: Accepted new socket %d\n", status);
                    904:        data_soc = status;
                    905:     }
                    906: #else
                    907: /* @@ */
                    908: #endif
1.2       timbl     909:     if (isDirectory) {
1.13      timbl     910:        return read_directory(request, NULL, anchor, name, format_out, sink);
1.1       timbl     911:       /* returns HT_LOADED or error */
1.2       timbl     912:     } else {
1.13      timbl     913:        HTParseSocket(format, data_soc, request);
1.2       timbl     914:     
                    915:        status = NETCLOSE(data_soc);
                    916:        if (TRACE) fprintf(stderr, "FTP: Closing data socket %d\n", data_soc);
                    917:        if (status<0) (void) HTInetStatus("close");     /* Comment only */
                    918:        data_soc = -1;  /* invalidate it */
                    919:        
                    920:        status = response(NIL);         /* Pick up final reply */
1.15      luotonen  921:        if (status!=2) return HTLoadError(request, 500, response_text);
1.2       timbl     922: 
                    923:        return HT_LOADED;
                    924:     }       
1.1       timbl     925: } /* open_file_read */

Webmaster