Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /tags/REL-2.2/ftp_open.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 55 - (hide annotations) (download) (as text)
Wed Mar 24 08:04:56 2010 UTC (14 years ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 4193 byte(s)
Tag release 2.2
1 hirohitohigashi 17 /*
2     liboftp: this is an FTP library to simplify the work to a Developer
3     who want to work with FTP servers (RFC 959).
4    
5     Copyright (c) 2009 hirohito higashi. All rights reserved.
6     This file is distributed under BSD license.
7     */
8    
9    
10     /***** Feature test switches ************************************************/
11     /***** System headers *******************************************************/
12 hirohitohigashi 29 #include <unistd.h>
13 hirohitohigashi 17 #include <string.h>
14     #include <netdb.h>
15     #include <sys/types.h>
16     #include <sys/socket.h>
17     #include <errno.h>
18    
19    
20     /***** Local headers ********************************************************/
21     #include "liboftp.h"
22     #include "sub.h"
23    
24    
25     /***** Constat values *******************************************************/
26     /***** Macros ***************************************************************/
27     /***** Typedefs *************************************************************/
28     /***** Function prototypes **************************************************/
29     /***** Local variables ******************************************************/
30     /***** Global variables *****************************************************/
31     /***** Signal catching functions ********************************************/
32     /***** Local functions ******************************************************/
33 hirohitohigashi 29 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 hirohitohigashi 17 /***** Global functions *****************************************************/
56    
57     /****************************************************************************/
58 hirohitohigashi 29 /*! 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 hirohitohigashi 50 ftp->socket = -1;
69 hirohitohigashi 29 ftp->timeout_sec = 10; /* default timeout */
70    
71     return LIBOFTP_NOERROR;
72     }
73    
74    
75    
76     /****************************************************************************/
77 hirohitohigashi 17 /*! ftp������������������
78     *
79     *@param ftp LIBOFTP���������������������
80     *@param host ������������������������IP������������������������
81     *@param port ���������������������������������������������������������������
82     *@retval int ������������������
83     *@note
84     * ftp������������������������������������������
85     */
86 hirohitohigashi 25 int ftp_open( LIBOFTP *ftp, const char *host, int port )
87 hirohitohigashi 17 {
88     struct hostent *p_hostent;
89 hirohitohigashi 29 int res;
90 hirohitohigashi 17
91 hirohitohigashi 50 if( ftp->socket >= 0 ) return LIBOFTP_ERROR;
92 hirohitohigashi 17 if( port == 0 ) port = 21;
93    
94     /*
95     * search host entry
96     */
97     p_hostent = gethostbyname( host );
98     if( p_hostent == NULL ) {
99 hirohitohigashi 29 DEBUGPRINT1( "gethostbyname() function failed. %s\n", my_hstrerror(h_errno) );
100     strncpy( ftp->error_message, my_hstrerror(h_errno), sizeof(ftp->error_message)-1 );
101     return LIBOFTP_ERROR_OS;
102 hirohitohigashi 17 }
103 hirohitohigashi 20 ftp->saddr.sin_family = AF_INET;
104     memcpy( &ftp->saddr.sin_addr, p_hostent->h_addr, p_hostent->h_length );
105     ftp->saddr.sin_port = htons( port );
106 hirohitohigashi 17
107     /*
108     * make control connection.
109     */
110     ftp->socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
111     if( ftp->socket < 0 ) {
112 hirohitohigashi 29 DEBUGPRINT1( "can't create socket. %s\n", strerror(errno) );
113     copy_strerror();
114 hirohitohigashi 50 ftp->socket = -1;
115 hirohitohigashi 29 return LIBOFTP_ERROR_OS;
116 hirohitohigashi 17 }
117    
118 hirohitohigashi 20 if( connect( ftp->socket, (struct sockaddr *)&(ftp->saddr), sizeof(struct sockaddr) ) < 0 ) {
119 hirohitohigashi 29 DEBUGPRINT1( "can't connect socket. %s\n", strerror(errno) );
120     copy_strerror();
121     close( ftp->socket );
122 hirohitohigashi 50 ftp->socket = -1;
123 hirohitohigashi 29 return LIBOFTP_ERROR_OS;
124 hirohitohigashi 17 }
125    
126 hirohitohigashi 29 res = ftp_timeout( ftp, ftp->timeout_sec );
127     if( res < 0 ) {
128     close( ftp->socket );
129 hirohitohigashi 50 ftp->socket = -1;
130 hirohitohigashi 29 return res;
131 hirohitohigashi 17 }
132    
133 hirohitohigashi 29 /*
134     * 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 hirohitohigashi 50 ftp->socket = -1;
141 hirohitohigashi 29 return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
142 hirohitohigashi 17 }
143    
144 hirohitohigashi 29 return LIBOFTP_NOERROR;
145 hirohitohigashi 17 }

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