Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/ftp_open.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 29 - (show annotations) (download) (as text)
Sat Feb 28 12:48:31 2009 UTC (15 years, 1 month ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 4118 byte(s)
changed error messageing system. and added ftp_timeout() function.

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->timeout_sec = 10; /* default timeout */
69
70 return LIBOFTP_NOERROR;
71 }
72
73
74
75 /****************************************************************************/
76 /*! ftp������������������
77 *
78 *@param ftp LIBOFTP���������������������
79 *@param host ������������������������IP������������������������
80 *@param port ���������������������������������������������������������������
81 *@retval int ������������������
82 *@note
83 * ftp������������������������������������������
84 */
85 int ftp_open( LIBOFTP *ftp, const char *host, int port )
86 {
87 struct hostent *p_hostent;
88 int res;
89
90 if( port == 0 ) port = 21;
91
92 /*
93 * search host entry
94 */
95 p_hostent = gethostbyname( host );
96 if( p_hostent == NULL ) {
97 DEBUGPRINT1( "gethostbyname() function failed. %s\n", my_hstrerror(h_errno) );
98 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;
102 memcpy( &ftp->saddr.sin_addr, p_hostent->h_addr, p_hostent->h_length );
103 ftp->saddr.sin_port = htons( port );
104
105 /*
106 * make control connection.
107 */
108 ftp->socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
109 if( ftp->socket < 0 ) {
110 DEBUGPRINT1( "can't create socket. %s\n", strerror(errno) );
111 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 ) {
117 DEBUGPRINT1( "can't connect socket. %s\n", strerror(errno) );
118 copy_strerror();
119 close( ftp->socket );
120 ftp->socket = 0;
121 return LIBOFTP_ERROR_OS;
122 }
123
124 res = ftp_timeout( ftp, ftp->timeout_sec );
125 if( res < 0 ) {
126 close( ftp->socket );
127 ftp->socket = 0;
128 return res;
129 }
130
131 /*
132 * 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 LIBOFTP_NOERROR;
143 }

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