Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 55 - (show annotations) (download) (as text)
Wed Mar 24 08:04:56 2010 UTC (13 years, 11 months ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 4193 byte(s)
Tag release 2.2
1 /*
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 #include <unistd.h>
13 #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 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 *****************************************************/
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������������������
78 *
79 *@param ftp LIBOFTP���������������������
80 *@param host ������������������������IP������������������������
81 *@param port ���������������������������������������������������������������
82 *@retval int ������������������
83 *@note
84 * ftp������������������������������������������
85 */
86 int ftp_open( LIBOFTP *ftp, const char *host, int port )
87 {
88 struct hostent *p_hostent;
89 int res;
90
91 if( ftp->socket >= 0 ) return LIBOFTP_ERROR;
92 if( port == 0 ) port = 21;
93
94 /*
95 * search host entry
96 */
97 p_hostent = gethostbyname( host );
98 if( p_hostent == NULL ) {
99 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 }
103 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
107 /*
108 * make control connection.
109 */
110 ftp->socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
111 if( ftp->socket < 0 ) {
112 DEBUGPRINT1( "can't create socket. %s\n", strerror(errno) );
113 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 ) {
119 DEBUGPRINT1( "can't connect socket. %s\n", strerror(errno) );
120 copy_strerror();
121 close( ftp->socket );
122 ftp->socket = -1;
123 return LIBOFTP_ERROR_OS;
124 }
125
126 res = ftp_timeout( ftp, ftp->timeout_sec );
127 if( res < 0 ) {
128 close( ftp->socket );
129 ftp->socket = -1;
130 return res;
131 }
132
133 /*
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 ftp->socket = -1;
141 return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
142 }
143
144 return LIBOFTP_NOERROR;
145 }

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