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 29 by hirohitohigashi, Sat Feb 28 12:48:31 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->timeout_sec = 10;                      /* default timeout */
69    
70        return LIBOFTP_NOERROR;
71    }
72    
73    
74    
75    /****************************************************************************/
76  /*! ftpサーバへ接続  /*! ftpサーバへ接続
77   *   *
78   *@param        ftp     LIBOFTPへのポインタ。   *@param        ftp     LIBOFTPへのポインタ。
# Line 42  Line 82 
82   *@note   *@note
83   * ftp構造体は、呼出側で確保する。   * ftp構造体は、呼出側で確保する。
84   */   */
85  int ftp_connect( LIBOFTP *ftp, const char *host, int port )  int ftp_open( LIBOFTP *ftp, const char *host, int port )
86  {  {
87      struct hostent *p_hostent;      struct hostent *p_hostent;
88      struct timeval tval;      int res;
89    
     /*  
      * initialize  
      */  
     memset( ftp, 0, sizeof( LIBOFTP ) );  
     ftp->timeout_sec = 10;                      /* default timeout */  
90      if( port == 0 ) port = 21;      if( port == 0 ) port = 21;
91    
92      /*      /*
# Line 59  int ftp_connect( LIBOFTP *ftp, const cha Line 94  int ftp_connect( LIBOFTP *ftp, const cha
94       */       */
95      p_hostent = gethostbyname( host );      p_hostent = gethostbyname( host );
96      if( p_hostent == NULL ) {      if( p_hostent == NULL ) {
97          ftp->error_no = h_errno;          DEBUGPRINT1( "gethostbyname() function failed. %s\n", my_hstrerror(h_errno) );
98          return -1;          strncpy( ftp->error_message, my_hstrerror(h_errno), sizeof(ftp->error_message)-1 );
99            return LIBOFTP_ERROR_OS;
100      }      }
101      ftp->saddr.sin_family = AF_INET;      ftp->saddr.sin_family = AF_INET;
102      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 107  int ftp_connect( LIBOFTP *ftp, const cha
107       */       */
108      ftp->socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );      ftp->socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
109      if( ftp->socket < 0 ) {      if( ftp->socket < 0 ) {
110          ftp->error_no = errno;          DEBUGPRINT1( "can't create socket. %s\n", strerror(errno) );
111          return -2;          copy_strerror();
112            ftp->socket = 0;
113            return LIBOFTP_ERROR_OS;
114      }      }
115    
116      if( connect( ftp->socket, (struct sockaddr *)&(ftp->saddr), sizeof(struct sockaddr) ) < 0 ) {      if( connect( ftp->socket, (struct sockaddr *)&(ftp->saddr), sizeof(struct sockaddr) ) < 0 ) {
117          ftp->error_no = errno;          DEBUGPRINT1( "can't connect socket. %s\n", strerror(errno) );
118          return -3;          copy_strerror();
119            close( ftp->socket );
120            ftp->socket = 0;
121            return LIBOFTP_ERROR_OS;
122      }      }
123    
124      tval.tv_sec = ftp->timeout_sec;      res = ftp_timeout( ftp, ftp->timeout_sec );
125      tval.tv_usec = 0;      if( res < 0 ) {
126      if( setsockopt( ftp->socket, SOL_SOCKET, SO_SNDTIMEO, &tval, sizeof(struct timeval) ) < 0 ) {          close( ftp->socket );
127          ftp->error_no = errno;          ftp->socket = 0;
128          return -4;          return res;
     }  
     if( setsockopt( ftp->socket, SOL_SOCKET, SO_RCVTIMEO, &tval, sizeof(struct timeval) ) < 0 ) {  
         ftp->error_no = errno;  
         return -5;  
129      }      }
130    
131      if( ftp_receive_response( ftp, 0, 0 ) != 220 ) {            /* 220: Service ready for new user. */      /*
132          return -7;       * receive banner
133         */
134        res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message) );
135        if( res != 220 ) {                                          /* 220: Service ready for new user. */
136            DEBUGPRINT1( "Illegal response. %d\n", res );
137            close( ftp->socket );
138            ftp->socket = 0;
139            return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
140      }      }
141    
142      return 0;      return LIBOFTP_NOERROR;
143  }  }
   

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

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