Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (show annotations) (download) (as text)
Mon Feb 23 02:22:49 2009 UTC (15 years, 1 month ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 4898 byte(s)
Creating relase branch for 1.1
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 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
69
70 extern int data_port;
71 extern struct sockaddr_in ftp_connection;
72
73
74 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
80 memset(buffer, 0x0, 4092);
81
82
83 if (data_port == 0) {
84
85 free(buffer);
86
87 return WRONG_DATA_P;
88
89 }
90
91 if (strlen(FileName) > 1024) {
92
93 return USR_TOO_LONG;
94
95 }
96
97 sprintf(buffer, "RETR %s\r\n", FileName);
98
99 if (write(sck, buffer, strlen(buffer)) == -1) {
100
101 if (verbose) {
102 if (errno == EBADF) {
103 fprintf(stderr, "Socket Descriptor Not Valid!\n");
104 free(buffer);
105
106 return -1;
107
108 } else if (errno == EIO) {
109
110 fprintf(stderr, "I/O Error!\n");
111 free(buffer);
112
113 return -1;
114
115 } else if (errno == EINTR) {
116
117 fprintf(stderr, "Signal Interrupted The write() Function\n");
118 free(buffer);
119
120 return -1;
121 }
122
123 } else {
124
125 if (errno == EBADF) {
126 free(buffer);
127
128 return -1;
129
130 } else if (errno == EIO) {
131 free(buffer);
132
133 return -1;
134
135 } else if (errno == EINTR) {
136 free(buffer);
137
138 return -1;
139 }
140
141 }
142
143 free(buffer);
144
145 return -1;
146
147 }
148
149 memset(buffer, 0x0, 4092);
150
151 sleep(1);
152 n = recv(sck, buffer, 4092, MSG_DONTWAIT);
153
154 if (strlen(buffer) != 0) {
155
156 if (ftp_file_handler (buffer, 0) != 0) {
157 free(buffer);
158
159 return 1;
160 }
161
162 }
163
164
165 sock = socket(AF_INET, SOCK_STREAM, 0);
166 if (sock == -1) {
167 free(buffer);
168 if (verbose) {
169 perror("Socket");
170 }
171 return -1;
172 }
173
174
175 ftp_connection.sin_port = htons(data_port);
176
177
178 if ( (con_err = connect(sock, (struct sockaddr *)&ftp_connection, sizeof(struct sockaddr)) ) != 0) {
179 if (con_err == ECONNREFUSED) {
180 free(buffer);
181
182 return -1;
183 }
184
185 data_port = 0;
186 if (verbose) {
187 perror("connect");
188 }
189
190 free(buffer);
191
192 return -1;
193 }
194
195 if ( (flags=fcntl(sock, F_GETFL,0)) < 0) {
196 printf("--[ fcntl get flags error.\n");
197 exit(1);
198 }
199
200 flags |= O_NONBLOCK;
201
202 if ( fcntl (sock, F_SETFL, flags ) < 0 ) {
203 printf("--[ Non Blocking Socket Error.\n");
204 exit(1);
205 }
206
207 buff[4095] = '\0';
208
209 fd = fopen(FileName, "wb");
210
211 if (fd == NULL) {
212 if (verbose) {
213 printf("--[ Error: Unable to open '%s' in ftp_get function\n", FileName);
214 }
215
216 free(buffer);
217
218 return -1;
219 }
220
221 time(&start);
222
223 while (1) {
224
225 usleep(150);
226 n_recv = recv(sock, buff, 4094, 0);
227 if( n_recv < 0 ) {
228 if( errno == EAGAIN ) continue;
229 break;
230 }
231 n_bytes = fwrite(buff, sizeof(char), n_recv, fd);
232
233 if (n_recv == 0) {
234 break;
235 }
236
237 n_recv = 0;
238
239 memset(buff, 0x0, n_bytes);
240
241 }
242
243
244 fclose(fd);
245 close(sock);
246
247
248
249 memset(buffer, 0x0, 4092);
250
251 while (1) {
252
253 sleep(1);
254 n = recv(sck, buffer, 4092, MSG_DONTWAIT);
255 if (n == 0)
256 break;
257
258 if (n < 0) {
259
260 if ( (errno == ESPIPE) || (errno == EAGAIN) ) {
261 break;
262 }
263
264 if (verbose) {
265 printf("--[ Error: Error in ftp_get() while getting the file\n");
266 }
267 free(buffer);
268
269
270 return -1;
271
272 }
273
274 buffer[n] = '\0';
275
276
277 n = 0;
278 memset(buffer, 0x0, 4092);
279
280 }
281
282 memset(buffer, 0x0, 4092);
283
284 time(&end);
285
286 res_get = (int)(end - start);
287
288 free(buffer);
289
290
291 return 0;
292 }

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