Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (hide annotations) (download) (as text)
Fri Feb 20 14:38:01 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftpget.c
File MIME type: text/x-csrc
File size: 4825 byte(s)
fix some bugs using fcntl()

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 <time.h>
30     #include <sys/socket.h>
31     #include <sys/stat.h>
32     #include <netinet/in.h>
33     #include <netdb.h>
34     #include <fcntl.h>
35    
36     #include "ftp_main.h"
37    
38     /***
39     ftp_get:
40     - int sck => Socket Descriptor
41     - char *FileName => The file name that you want to download
42     - int verbose => Verbose?
43     . 0 Do not Print
44     . 1 Print
45     - ret => Success?
46     . 0 Success
47     .-1 Error
48     ***/
49    
50     #define DATA_CONNECTION_ERROR -3
51     #define WRONG_DATA_P -3
52     #define USR_TOO_LONG -2
53    
54     time_t res_get;
55    
56 hirohitohigashi 7 int ftp_get (int sck, char *FileName, int verbose)
57     {
58     char *buffer;
59     int n = 0;
60     int flags;
61     time_t start, end;
62     int sock;
63     int con_err = 0;
64     FILE *fd;
65     char buff[4096];
66     int n_bytes;
67     int n_recv=0;
68 hirohitohigashi 1
69    
70 hirohitohigashi 7 extern int data_port;
71     extern struct sockaddr_in ftp_connection;
72 hirohitohigashi 1
73    
74 hirohitohigashi 7 buffer = (char *)malloc(4092*sizeof(char));
75     if (buffer == NULL) {
76     printf("Unable to alloc 4092 bytes to buffer (ftp_get)\n");
77     return -1;
78     }
79 hirohitohigashi 1
80 hirohitohigashi 7 memset(buffer, 0x0, 4092);
81 hirohitohigashi 1
82 hirohitohigashi 7
83     if (data_port == 0) {
84    
85 hirohitohigashi 1 free(buffer);
86    
87     return WRONG_DATA_P;
88    
89 hirohitohigashi 7 }
90 hirohitohigashi 1
91 hirohitohigashi 7 if (strlen(FileName) > 1024) {
92 hirohitohigashi 1
93     return USR_TOO_LONG;
94    
95 hirohitohigashi 7 }
96 hirohitohigashi 1
97 hirohitohigashi 7 sprintf(buffer, "RETR %s\n", FileName);
98 hirohitohigashi 1
99 hirohitohigashi 7 if (write(sck, buffer, strlen(buffer)) == -1) {
100 hirohitohigashi 1
101 hirohitohigashi 7 if (verbose) {
102     if (errno == EBADF) {
103 hirohitohigashi 1 fprintf(stderr, "Socket Descriptor Not Valid!\n");
104     free(buffer);
105    
106     return -1;
107    
108 hirohitohigashi 7 } else if (errno == EIO) {
109 hirohitohigashi 1
110     fprintf(stderr, "I/O Error!\n");
111     free(buffer);
112    
113     return -1;
114    
115 hirohitohigashi 7 } else if (errno == EINTR) {
116 hirohitohigashi 1
117     fprintf(stderr, "Signal Interrupted The write() Function\n");
118     free(buffer);
119    
120     return -1;
121 hirohitohigashi 7 }
122 hirohitohigashi 1
123 hirohitohigashi 7 } else {
124 hirohitohigashi 1
125 hirohitohigashi 7 if (errno == EBADF) {
126 hirohitohigashi 1 free(buffer);
127    
128     return -1;
129    
130 hirohitohigashi 7 } else if (errno == EIO) {
131 hirohitohigashi 1 free(buffer);
132    
133     return -1;
134    
135 hirohitohigashi 7 } else if (errno == EINTR) {
136 hirohitohigashi 1 free(buffer);
137    
138     return -1;
139 hirohitohigashi 7 }
140 hirohitohigashi 1
141 hirohitohigashi 7 }
142 hirohitohigashi 1
143 hirohitohigashi 7 free(buffer);
144 hirohitohigashi 1
145 hirohitohigashi 7 return -1;
146 hirohitohigashi 1
147 hirohitohigashi 7 }
148 hirohitohigashi 1
149 hirohitohigashi 7 memset(buffer, 0x0, 4092);
150 hirohitohigashi 1
151 hirohitohigashi 7 sleep(1);
152     n = recv(sck, buffer, 4092, MSG_DONTWAIT);
153 hirohitohigashi 1
154 hirohitohigashi 7 if (strlen(buffer) != 0) {
155 hirohitohigashi 1
156     if (ftp_file_handler (buffer, 0) != 0) {
157 hirohitohigashi 7 free(buffer);
158 hirohitohigashi 1
159 hirohitohigashi 7 return 1;
160 hirohitohigashi 1 }
161    
162 hirohitohigashi 7 }
163 hirohitohigashi 1
164    
165 hirohitohigashi 7 sock = socket(AF_INET, SOCK_STREAM, 0);
166     if (sock == -1) {
167 hirohitohigashi 1 free(buffer);
168     if (verbose) {
169 hirohitohigashi 7 perror("Socket");
170 hirohitohigashi 1 }
171     return -1;
172 hirohitohigashi 7 }
173 hirohitohigashi 1
174    
175 hirohitohigashi 7 ftp_connection.sin_port = htons(data_port);
176 hirohitohigashi 1
177 hirohitohigashi 7 if ( (flags=fcntl(sock, F_GETFL,0)) < 0) {
178 hirohitohigashi 1 printf("--[ fcntl get flags error.\n");
179     exit(1);
180 hirohitohigashi 7 }
181 hirohitohigashi 1
182 hirohitohigashi 7 flags |= O_NONBLOCK;
183 hirohitohigashi 1
184 hirohitohigashi 8 if ( fcntl (sock, F_SETFL, flags ) < 0 ) {
185 hirohitohigashi 7 printf("--[ Non Blocking Socket Error.\n");
186     exit(1);
187     }
188 hirohitohigashi 1
189    
190     if ( (con_err = connect(sock, (struct sockaddr *)&ftp_connection, sizeof(struct sockaddr)) ) != 0) {
191 hirohitohigashi 7 if (con_err == ECONNREFUSED) {
192     free(buffer);
193 hirohitohigashi 1
194 hirohitohigashi 7 return -1;
195     }
196 hirohitohigashi 1
197 hirohitohigashi 7 data_port = 0;
198     if (verbose) {
199     perror("connect");
200     }
201 hirohitohigashi 1
202 hirohitohigashi 7 free(buffer);
203 hirohitohigashi 1
204 hirohitohigashi 7 return -1;
205 hirohitohigashi 1 }
206    
207    
208 hirohitohigashi 7 buff[4095] = '\0';
209 hirohitohigashi 1
210 hirohitohigashi 7 fd = fopen(FileName, "wb");
211 hirohitohigashi 1
212 hirohitohigashi 7 if (fd == NULL) {
213     if (verbose) {
214     printf("--[ Error: Unable to open '%s' in ftp_get function\n", FileName);
215     }
216 hirohitohigashi 1
217 hirohitohigashi 7 free(buffer);
218 hirohitohigashi 1
219 hirohitohigashi 7 return -1;
220     }
221 hirohitohigashi 1
222 hirohitohigashi 7 time(&start);
223 hirohitohigashi 1
224 hirohitohigashi 7 while (1) {
225 hirohitohigashi 1
226 hirohitohigashi 7 usleep(150);
227     n_recv = recv(sock, buff, 4094, 0);
228     n_bytes = fwrite(buff, sizeof(char), n_recv, fd);
229 hirohitohigashi 1
230 hirohitohigashi 7 if (n_recv == 0) {
231     break;
232     }
233 hirohitohigashi 1
234 hirohitohigashi 7 n_recv = 0;
235 hirohitohigashi 1
236 hirohitohigashi 7 memset(buff, 0x0, n_bytes);
237 hirohitohigashi 1
238 hirohitohigashi 7 }
239 hirohitohigashi 1
240    
241 hirohitohigashi 7 fclose(fd);
242     close(sock);
243 hirohitohigashi 1
244    
245    
246 hirohitohigashi 7 memset(buffer, 0x0, 4092);
247 hirohitohigashi 1
248 hirohitohigashi 7 while (1) {
249 hirohitohigashi 1
250     sleep(1);
251     n = recv(sck, buffer, 4092, MSG_DONTWAIT);
252     if (n == 0)
253 hirohitohigashi 7 break;
254 hirohitohigashi 1
255     if (n < 0) {
256    
257 hirohitohigashi 7 if ( (errno == ESPIPE) || (errno == EAGAIN) ) {
258 hirohitohigashi 1 break;
259 hirohitohigashi 7 }
260 hirohitohigashi 1
261 hirohitohigashi 7 if (verbose) {
262 hirohitohigashi 1 printf("--[ Error: Error in ftp_get() while getting the file\n");
263 hirohitohigashi 7 }
264     free(buffer);
265 hirohitohigashi 1
266    
267 hirohitohigashi 7 return -1;
268 hirohitohigashi 1
269     }
270    
271 hirohitohigashi 7 buffer[n] = '\0';
272 hirohitohigashi 1
273    
274     n = 0;
275     memset(buffer, 0x0, 4092);
276    
277 hirohitohigashi 7 }
278 hirohitohigashi 1
279 hirohitohigashi 7 memset(buffer, 0x0, 4092);
280 hirohitohigashi 1
281 hirohitohigashi 7 time(&end);
282 hirohitohigashi 1
283 hirohitohigashi 7 res_get = (int)(end - start);
284 hirohitohigashi 1
285 hirohitohigashi 7 free(buffer);
286 hirohitohigashi 1
287    
288 hirohitohigashi 7 return 0;
289 hirohitohigashi 1 }

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