Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk_1/ftpnlist.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (show annotations) (download) (as text)
Wed Feb 25 13:29:32 2009 UTC (15 years ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 4355 byte(s)
renewal all sources.

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
37
38 /***
39 ftp_list:
40 - int sck => Socket Descriptor
41 - int verbose => Verbose?
42 . 0 Do not Print
43 . 1 Print
44 - ret => Success?
45 . 0 Success
46 .-1 Error
47 .-3 Error
48 ***/
49
50 #define DATA_CONNECTION_ERROR -3
51 #define WRONG_DATA_P -3
52
53
54 int ftp_nlist (int sck, int verbose)
55 {
56
57 FILE *nlist;
58 char *buffer;
59 int n = 0;
60 int flags;
61 int sock;
62 int con_err = 0;
63 struct timeval tm; //tv_sec - tv_usec
64 fd_set readfds;
65
66 extern int data_port;
67 extern struct sockaddr_in ftp_connection;
68
69
70 buffer = (char *) malloc(1024 * sizeof(char));
71
72 if (buffer == NULL) {
73 printf("Unable to alloc 1024 bytes to buffer (ftp_nlist)\n");
74 return -1;
75 }
76
77 memset(buffer, 0x0, 1024);
78
79
80 if (data_port == 0) {
81 free(buffer);
82 return WRONG_DATA_P;
83 }
84
85 if (write(sck, "NLST\n", strlen("NLST\n")) == -1) {
86
87 if (verbose) {
88 if (errno == EBADF) {
89 fprintf(stderr, "Socket Descriptor Not Valid!\n");
90 free(buffer);
91
92 return -1;
93
94 } else if (errno == EIO) {
95
96 fprintf(stderr, "I/O Error!\n");
97 free(buffer);
98
99 return -1;
100
101 } else if (errno == EINTR) {
102
103 fprintf(stderr, "Signal Interrupted The write() Function\n");
104 free(buffer);
105
106 return -1;
107 }
108
109 } else {
110
111 if (errno == EBADF) {
112 free(buffer);
113
114 return -1;
115
116 } else if (errno == EIO) {
117 free(buffer);
118
119 return -1;
120
121 } else if (errno == EINTR) {
122 free(buffer);
123
124 return -1;
125 }
126
127 }
128
129 free(buffer);
130
131 return -1;
132
133 }
134
135 sock = socket(AF_INET, SOCK_STREAM, 0);
136 if (sock == -1) {
137 if (verbose) {
138 perror("Socket");
139 }
140
141 free(buffer);
142
143 return -1;
144 }
145
146
147 ftp_connection.sin_port = htons(data_port);
148
149 sleep(1);
150
151 if ( (con_err = connect(sock, (struct sockaddr *)&ftp_connection, sizeof(struct sockaddr)) ) != 0) {
152
153 data_port = 0;
154 n = recv(sck, buffer, 1023, 0);
155 memset(buffer, 0x0, 1024);
156 return DATA_CONNECTION_ERROR;
157 }
158
159 if ( (flags=fcntl(sock, F_GETFL,0)) < 0) {
160 printf("--[ fcntl get flags error.\n");
161 free(buffer);
162
163 exit(1);
164 }
165
166 flags |= O_NONBLOCK;
167
168 if ( fcntl (sock, F_SETFL, flags ) < 0 ) {
169 printf("--[ Non Blocking Socket Error.\n");
170 free(buffer);
171
172 exit(1);
173 }
174
175 n = 0;
176 nlist = fopen("NLIST.txt", "w");
177 if (nlist == NULL) {
178 printf("Unable to open NLIST file..\n");
179 free(buffer);
180
181 return -1;
182 }
183
184 buffer[1023] = '\0';
185
186 while (1) {
187
188 n = recv(sock, buffer, 1022, 0);
189
190 if (n == 0) {
191 sleep(1); //end of Data.. wait a second..
192 break;
193 }
194
195 buffer[n] = '\0';
196
197 fprintf(nlist, "%s", buffer);
198
199 n = 0;
200
201 }
202
203 close(sock);
204 fclose(nlist);
205
206 tm.tv_sec = 3;
207 tm.tv_usec = 0;
208
209 sleep(1);
210
211 FD_ZERO(&readfds);
212
213 FD_SET(sck, &readfds);
214
215 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
216
217 if (verbose) {
218 perror("select()");
219 }
220
221 free(buffer);
222
223 return -1;
224 } else {
225
226 if (FD_ISSET(sck, &readfds)) {
227
228 if ( (n = recv(sck, buffer, 1022, 0)) < 0) {
229 if (verbose) {
230 perror("recv()");
231 }
232 free(buffer);
233
234 return -1;
235 }
236
237 if (verbose)
238 printf("[Server] %s\n", buffer);
239
240 }
241 else {
242 if (verbose)
243 printf("[-] Timeout\n");
244
245 return -1;
246 }
247 }
248
249 buffer[n] = '\0';
250
251
252 free(buffer);
253
254 return 0;
255 }

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