home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / ncftp / older_versions / ncftp-3.2.2-src.tar.bz2 / ncftp-3.2.2-src.tar / ncftp-3.2.2 / libncftp / c_opennologin.c < prev    next >
C/C++ Source or Header  |  2005-01-01  |  2KB  |  90 lines

  1. /* c_opennologin.c
  2.  *
  3.  * Copyright (c) 1996-2005 Mike Gleason, NcFTP Software.
  4.  * All rights reserved.
  5.  *
  6.  */
  7.  
  8. #include "syshdrs.h"
  9. #ifdef PRAGMA_HDRSTOP
  10. #    pragma hdrstop
  11. #endif
  12.  
  13. int
  14. FTPOpenHostNoLogin(const FTPCIPtr cip)
  15. {
  16.     int result;
  17.     time_t t0, t1;
  18.     int elapsed;
  19.  
  20.     if (cip == NULL)
  21.         return (kErrBadParameter);
  22.     if (strcmp(cip->magic, kLibraryMagic))
  23.         return (kErrBadMagic);
  24.  
  25.     if (cip->host[0] == '\0') {
  26.         cip->errNo = kErrBadParameter;
  27.         return (kErrBadParameter);
  28.     }
  29.  
  30.     FTPInitialLogEntry(cip);
  31.  
  32.     for (    result = kErrConnectMiscErr, cip->numDials = 0;
  33.         cip->maxDials < 0 || cip->numDials < cip->maxDials;
  34.     )    
  35.     {
  36.  
  37.         /* Allocate (or if the host was closed, reallocate)
  38.          * the transfer data buffer.
  39.          */
  40.         result = FTPAllocateHost(cip);
  41.         if (result < 0)
  42.             return (result);
  43.  
  44.         memset(&cip->connectTime, 0, sizeof(cip->connectTime));
  45.         memset(&cip->loginTime, 0, sizeof(cip->loginTime));
  46.         memset(&cip->disconnectTime, 0, sizeof(cip->disconnectTime));
  47.  
  48.         cip->totalDials++;
  49.         cip->numDials++;
  50.         if (cip->numDials > 1)
  51.             PrintF(cip, "Retry Number: %d\n", cip->numDials - 1);
  52.         if (cip->redialStatusProc != 0)
  53.             (*cip->redialStatusProc)(cip, kRedialStatusDialing, cip->numDials - 1);
  54.         (void) time(&t0);
  55.         result = OpenControlConnection(cip, cip->host, cip->port);
  56.         (void) time(&t1);
  57.         if (result == kNoErr) {
  58.             /* We were hooked up successfully. */
  59.             PrintF(cip, "Connected to %s.\n", cip->host);
  60.  
  61.             /* Not logging in... */
  62.             if (result == kNoErr)
  63.                 break;
  64.         } else if ((result != kErrConnectRetryableErr) && (result != kErrConnectRefused) && (result != kErrRemoteHostClosedConnection)) {
  65.             /* Irrecoverable error, so don't bother redialing.
  66.              * The error message should have already been printed
  67.              * from OpenControlConnection().
  68.              */
  69.             PrintF(cip, "Cannot recover from miscellaneous open error %d.\n", result);
  70.             return result;
  71.         }
  72.  
  73.         /* Retryable error, wait and then redial. */
  74.         if (cip->redialDelay > 0) {
  75.             /* But don't sleep if this is the last loop. */
  76.             if ((cip->maxDials < 0) || (cip->numDials < (cip->maxDials))) {
  77.                 elapsed = (int) (t1 - t0);
  78.                 if (elapsed < cip->redialDelay) {
  79.                     PrintF(cip, "Sleeping %u seconds.\n",
  80.                         (unsigned) cip->redialDelay - elapsed);
  81.                     if (cip->redialStatusProc != 0)
  82.                         (*cip->redialStatusProc)(cip, kRedialStatusSleeping, cip->redialDelay - elapsed);
  83.                     (void) sleep((unsigned) cip->redialDelay - elapsed);
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     return (result);
  89. }    /* FTPOpenHostNoLogin */
  90.