Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/ftp_open.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (hide annotations) (download) (as text)
Wed Feb 25 13:47:38 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftp_connect.c
File MIME type: text/x-csrc
File size: 3123 byte(s)
Starting 2nd version of libOftp project. There are new designs.

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     #include <string.h>
13     #include <sys/time.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     /***** Global functions *****************************************************/
34    
35     /****************************************************************************/
36     /*! ftp������������������
37     *
38     *@param ftp LIBOFTP���������������������
39     *@param host ������������������������IP������������������������
40     *@param port ���������������������������������������������������������������
41     *@retval int ������������������
42     *@note
43     * ftp������������������������������������������
44     */
45     int ftp_connect( LIBOFTP *ftp, const char *host, int port )
46     {
47     struct hostent *p_hostent;
48     struct timeval tval;
49    
50     /*
51     * initialize
52     */
53     memset( ftp, 0, sizeof( LIBOFTP ) );
54     ftp->timeout_sec = 10; /* default timeout */
55     if( port == 0 ) port = 21;
56    
57     /*
58     * search host entry
59     */
60     p_hostent = gethostbyname( host );
61     if( p_hostent == NULL ) {
62     ftp->error_no = h_errno;
63     return -1;
64     }
65     ftp->sockaddr.sin_family = AF_INET;
66     memcpy( &ftp->sockaddr.sin_addr, p_hostent->h_addr, p_hostent->h_length );
67     ftp->sockaddr.sin_port = htons( port );
68    
69     /*
70     * make control connection.
71     */
72     ftp->socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
73     if( ftp->socket < 0 ) {
74     ftp->error_no = errno;
75     return -2;
76     }
77    
78     if( connect( ftp->socket, (struct sockaddr *)&(ftp->sockaddr), sizeof(struct sockaddr) ) < 0 ) {
79     ftp->error_no = errno;
80     return -3;
81     }
82    
83     tval.tv_sec = ftp->timeout_sec;
84     tval.tv_usec = 0;
85     if( setsockopt( ftp->socket, SOL_SOCKET, SO_SNDTIMEO, &tval, sizeof(struct timeval) ) < 0 ) {
86     ftp->error_no = errno;
87     return -4;
88     }
89     if( setsockopt( ftp->socket, SOL_SOCKET, SO_RCVTIMEO, &tval, sizeof(struct timeval) ) < 0 ) {
90     ftp->error_no = errno;
91     return -5;
92     }
93    
94     if( ftp_receive_response( ftp, 0, 0 ) != 220 ) { /* 220: Service ready for new user. */
95     return -7;
96     }
97    
98     return 0;
99     }
100    

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