Develop and Download Open Source Software

Browse Subversion Repository

Diff of /trunk/ftp_open.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/ftp_connect.c revision 20 by hirohitohigashi, Fri Feb 27 11:30:10 2009 UTC trunk/ftp_open.c revision 50 by hirohitohigashi, Wed Dec 2 15:13:30 2009 UTC
# Line 9  Line 9 
9    
10  /***** Feature test switches ************************************************/  /***** Feature test switches ************************************************/
11  /***** System headers *******************************************************/  /***** System headers *******************************************************/
12    #include <unistd.h>
13  #include <string.h>  #include <string.h>
 #include <sys/time.h>  
14  #include <netdb.h>  #include <netdb.h>
15  #include <sys/types.h>  #include <sys/types.h>
16  #include <sys/socket.h>  #include <sys/socket.h>
# Line 30  Line 30 
30  /***** Global variables *****************************************************/  /***** Global variables *****************************************************/
31  /***** Signal catching functions ********************************************/  /***** Signal catching functions ********************************************/
32  /***** Local functions ******************************************************/  /***** Local functions ******************************************************/
33    static const char * my_hstrerror( int no )
34    {
35        switch( no ) {
36        case HOST_NOT_FOUND:
37            return "Unknown host";
38            
39        case TRY_AGAIN:
40            return "Host name lookup failure";
41    
42        case NO_RECOVERY:
43            return "Unknown server error";
44    
45        case NO_DATA:
46            return "No address associated with name";
47    
48        default:
49            return "Unknown error";
50        }
51    }
52    
53    
54    
55  /***** Global functions *****************************************************/  /***** Global functions *****************************************************/
56    
57  /****************************************************************************/  /****************************************************************************/
58    /*! ftp構造体初期化
59     *
60     *@param        ftp     LIBOFTPへのポインタ。
61     *@retval       int     エラーコード
62     *@note
63     * ftp構造体は、呼出側で確保する。
64     */
65    int ftp_initialize( LIBOFTP *ftp )
66    {
67        memset( ftp, 0, sizeof( LIBOFTP ) );
68        ftp->socket = -1;
69        ftp->timeout_sec = 10;                      /* default timeout */
70    
71        return LIBOFTP_NOERROR;
72    }
73    
74    
75    
76    /****************************************************************************/
77  /*! ftpサーバへ接続  /*! ftpサーバへ接続
78   *   *
79   *@param        ftp     LIBOFTPへのポインタ。   *@param        ftp     LIBOFTPへのポインタ。
# Line 42  Line 83 
83   *@note   *@note
84   * ftp構造体は、呼出側で確保する。   * ftp構造体は、呼出側で確保する。
85   */   */
86  int ftp_connect( LIBOFTP *ftp, const char *host, int port )  int ftp_open( LIBOFTP *ftp, const char *host, int port )
87  {  {
88      struct hostent *p_hostent;      struct hostent *p_hostent;
89      struct timeval tval;      int res;
90    
91      /*      if( ftp->socket >= 0 ) return LIBOFTP_ERROR;
      * initialize  
      */  
     memset( ftp, 0, sizeof( LIBOFTP ) );  
     ftp->timeout_sec = 10;                      /* default timeout */  
92      if( port == 0 ) port = 21;      if( port == 0 ) port = 21;
93    
94      /*      /*
# Line 59  int ftp_connect( LIBOFTP *ftp, const cha Line 96  int ftp_connect( LIBOFTP *ftp, const cha
96       */       */
97      p_hostent = gethostbyname( host );      p_hostent = gethostbyname( host );
98      if( p_hostent == NULL ) {      if( p_hostent == NULL ) {
99          ftp->error_no = h_errno;          DEBUGPRINT1( "gethostbyname() function failed. %s\n", my_hstrerror(h_errno) );
100          return -1;          strncpy( ftp->error_message, my_hstrerror(h_errno), sizeof(ftp->error_message)-1 );
101            return LIBOFTP_ERROR_OS;
102      }      }
103      ftp->saddr.sin_family = AF_INET;      ftp->saddr.sin_family = AF_INET;
104      memcpy( &ftp->saddr.sin_addr, p_hostent->h_addr, p_hostent->h_length );      memcpy( &ftp->saddr.sin_addr, p_hostent->h_addr, p_hostent->h_length );
# Line 71  int ftp_connect( LIBOFTP *ftp, const cha Line 109  int ftp_connect( LIBOFTP *ftp, const cha
109       */       */
110      ftp->socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );      ftp->socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
111      if( ftp->socket < 0 ) {      if( ftp->socket < 0 ) {
112          ftp->error_no = errno;          DEBUGPRINT1( "can't create socket. %s\n", strerror(errno) );
113          return -2;          copy_strerror();
114            ftp->socket = -1;
115            return LIBOFTP_ERROR_OS;
116      }      }
117    
118      if( connect( ftp->socket, (struct sockaddr *)&(ftp->saddr), sizeof(struct sockaddr) ) < 0 ) {      if( connect( ftp->socket, (struct sockaddr *)&(ftp->saddr), sizeof(struct sockaddr) ) < 0 ) {
119          ftp->error_no = errno;          DEBUGPRINT1( "can't connect socket. %s\n", strerror(errno) );
120          return -3;          copy_strerror();
121            close( ftp->socket );
122            ftp->socket = -1;
123            return LIBOFTP_ERROR_OS;
124      }      }
125    
126      tval.tv_sec = ftp->timeout_sec;      res = ftp_timeout( ftp, ftp->timeout_sec );
127      tval.tv_usec = 0;      if( res < 0 ) {
128      if( setsockopt( ftp->socket, SOL_SOCKET, SO_SNDTIMEO, &tval, sizeof(struct timeval) ) < 0 ) {          close( ftp->socket );
129          ftp->error_no = errno;          ftp->socket = -1;
130          return -4;          return res;
     }  
     if( setsockopt( ftp->socket, SOL_SOCKET, SO_RCVTIMEO, &tval, sizeof(struct timeval) ) < 0 ) {  
         ftp->error_no = errno;  
         return -5;  
131      }      }
132    
133      if( ftp_receive_response( ftp, 0, 0 ) != 220 ) {            /* 220: Service ready for new user. */      /*
134          return -7;       * receive banner
135         */
136        res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message) );
137        if( res != 220 ) {                                          /* 220: Service ready for new user. */
138            DEBUGPRINT1( "Illegal response. %d\n", res );
139            close( ftp->socket );
140            ftp->socket = -1;
141            return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
142      }      }
143    
144      return 0;      return LIBOFTP_NOERROR;
145  }  }
   

Legend:
Removed from v.20  
changed lines
  Added in v.50

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26