Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /branches/RB-1.1/ftpconn.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations) (download) (as text)
Wed Feb 18 08:06:53 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftpconn.c
File MIME type: text/x-csrc
File size: 4190 byte(s)
Initial import. original is libOftp_1_0.tar.gz.
1 hirohitohigashi 1 /*
2    
3     libftp 1.0 (stable): this is an FTP library to simplify the work to a Developer
4     who want to work with FTP servers (RFC 959).
5     Copyright (C) 2007/2008 omnipresent - omnipresent[at]email.it
6    
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18     along with this program. If not, see <http://www.gnu.org/licenses/>
19    
20     */
21    
22     #include <stdio.h>
23     #include <string.h>
24     #include <errno.h>
25     #include <stdlib.h>
26     #include <unistd.h>
27     #include <sys/types.h>
28     #include <sys/time.h>
29     #include <sys/socket.h>
30     #include <sys/stat.h>
31     #include <netinet/in.h>
32     #include <netdb.h>
33     #include <fcntl.h>
34    
35     #include "ftp_main.h"
36     /* Struct sockaddr_in for the connections of the transfers way */
37     struct sockaddr_in ftp_connection;
38    
39     /***
40     ftp_connect:
41     - char *host => HostName/IP
42     - int port => Port to connect to
43     - struct sockaddr_in * => pointer to sockaddr_in structure
44     - int verbose => Verbose?
45     . 0 Do not Print
46     . 1 Print
47     - ret => Success?
48     . sock Socket Descriptor Returned
49     .-1 Error
50     ***/
51    
52    
53     int ftp_connect(char *host, int port, struct sockaddr_in *ftp_server, int verbose) {
54    
55    
56     struct hostent *he;
57    
58     if ( (he=gethostbyname(host)) == NULL) {
59    
60     if (verbose) {
61    
62     if (h_errno == HOST_NOT_FOUND) {
63     printf("[-] Host Not Found!\n");
64     printf("exiting..\n");
65     return(HOST_NOT_FOUND);
66    
67     } else if (h_errno == TRY_AGAIN) {
68     printf("[-] Temporary error.. Try Again!\n");
69     printf("exiting..\n");
70     return(TRY_AGAIN);
71    
72     } else if (h_errno == NO_RECOVERY) {
73     printf("[-] Unexpected Server Error!\n");
74     printf("exiting..\n");
75     return(NO_RECOVERY);
76    
77     } else if (h_errno == NO_DATA) {
78     printf("[-] HostName has not IP Address!\n");
79     printf("exiting..\n");
80     return(NO_DATA);
81    
82     }
83    
84     } else {
85    
86     if (h_errno == HOST_NOT_FOUND) {
87     return(HOST_NOT_FOUND);
88    
89     } else if (h_errno == TRY_AGAIN) {
90     return(TRY_AGAIN);
91    
92     } else if (h_errno == NO_RECOVERY) {
93     return(NO_RECOVERY);
94    
95     } else if (h_errno == NO_DATA) {
96     return(NO_DATA);
97    
98     }
99    
100     }
101    
102     return -1;
103    
104     }
105    
106     memset(ftp_server,0,sizeof(ftp_server));
107     ftp_server->sin_port = htons(port);
108     ftp_server->sin_family = AF_INET;
109    
110     memcpy ((char *)&ftp_server->sin_addr,he->h_addr,he->h_length);
111    
112    
113     ftp_connection.sin_family = AF_INET;
114     memcpy ((char *)&ftp_connection.sin_addr, he->h_addr,he->h_length);
115    
116     int sock;
117    
118     sock = socket(AF_INET, SOCK_STREAM, 0);
119     if (sock == -1) {
120    
121     if (verbose) {
122     perror("Socket");
123     }
124    
125     return -1;
126     }
127    
128     sleep(1);
129     struct timeval tm; //tv_sec - tv_usec
130     fd_set readfds;
131    
132     tm.tv_sec = 4;
133     tm.tv_usec = 0;
134    
135     FD_ZERO(&readfds);
136    
137     FD_SET(sock, &readfds);
138    
139     if (select(sock+1, &readfds, NULL, NULL, &tm) < 0 ) {
140     if (verbose) {
141     perror("select()");
142     }
143     return -1;
144     } else {
145    
146     if (FD_ISSET(sock, &readfds)) {
147    
148     if (connect(sock, (struct sockaddr *)ftp_server, sizeof(struct sockaddr)) != 0) {
149     if (verbose) {
150     perror("connect()");
151     }
152     return -1;
153     }
154    
155     if (verbose) {
156     printf("[ Connected ]\n");
157     }
158     }
159     else {
160     if (verbose)
161     printf("[-] Timeout\n");
162    
163     return -1;
164     }
165     }
166    
167    
168     char buf[1024];
169     buf[1023] = '\0';
170     int n;
171     FD_ZERO(&readfds);
172    
173     tm.tv_sec = 3;
174     tm.tv_usec = 0;
175    
176     FD_SET(sock, &readfds);
177     sleep(1); //wait the banner..
178    
179     if (select(sock+1, &readfds, NULL, NULL, &tm) < 0 ) {
180     if (verbose) {
181     perror("select()");
182     }
183     return -1;
184     } else {
185    
186     if (FD_ISSET(sock, &readfds)) {
187     if ( (n = recv(sock, buf, 1022, 0)) < 0) {
188     if (verbose) {
189     perror("recv()");
190     }
191     return -1;
192     }
193    
194     if (verbose) {
195     printf("[Server] %s\n", buf);
196     }
197     }
198     else {
199     if (verbose)
200     printf("[-] Timeout\n");
201    
202     return -1;
203     }
204     }
205    
206     buf[n] = '\0';
207    
208     return sock; //Socket Descriptor returned!!
209     }
210    

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