Develop and Download Open Source Software

Browse Subversion Repository

Contents of /tags/REL-1.1/ftpasv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (show annotations) (download) (as text)
Mon Feb 23 02:24:24 2009 UTC (15 years, 1 month ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 3555 byte(s)
Tag release 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 <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 /* GLOBAL VARIABLES */
38 /* Data Ports */
39 /* Port One from the PASV reply */
40 int data_port1;
41 /* Port Two from the PASV reply */
42 int data_port2;
43 /* Port for the Transfer Connection (p1 * 256) + p2 */
44 int data_port = 0;
45
46 #define WRONG_DATA_P -3
47
48 /***
49 ftp_pasv:
50 - int sck => Socket Descriptor
51 - int verbose => Verbose?
52 . 0 Do not Print
53 . 1 Print
54 - ret => Success?
55 . 0 Success
56 .-1 Error
57 .-3 Error
58 ***/
59
60
61 int ftp_pasv (int sck, int verbose)
62 {
63 char *buffer;
64 int n;
65 struct timeval tm; //tv_sec - tv_usec
66 fd_set readfds;
67
68 buffer = (char *)malloc(1024*sizeof(char));
69 memset(buffer, 0x0, 1024);
70
71 if (write(sck, "PASV\n", strlen("PASV\n")) == -1) {
72
73 if (verbose) {
74
75 if (errno == EBADF) {
76 free(buffer);
77
78 fprintf(stderr, "Socket Descriptor Not Valid!\n");
79 return -1;
80
81 } else if (errno == EIO) {
82 free(buffer);
83
84 fprintf(stderr, "I/O Error!\n");
85 return -1;
86
87 } else if (errno == EINTR) {
88 free(buffer);
89
90 fprintf(stderr, "Signal Interrupted The write() Function\n");
91 return -1;
92 }
93
94 } else {
95
96 if (errno == EBADF) {
97 free(buffer);
98
99 return -1;
100
101 } else if (errno == EIO) {
102 free(buffer);
103
104 return -1;
105
106 } else if (errno == EINTR) {
107 free(buffer);
108
109 return -1;
110 }
111
112 }
113
114 free(buffer);
115
116
117 return -1;
118
119 }
120
121
122 tm.tv_sec = 3;
123 tm.tv_usec = 0;
124
125 FD_ZERO(&readfds);
126
127 FD_SET(sck, &readfds);
128
129 buffer[1023] = '\0';
130
131 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
132 if (verbose) {
133 perror("select()");
134 }
135 free(buffer);
136
137 return -1;
138 } else {
139
140 if (FD_ISSET(sck, &readfds)) {
141
142 if ( (n = recv(sck, buffer, 1022, 0)) < 0) {
143 if (verbose) {
144 perror("recv()");
145 }
146 free(buffer);
147
148 return -1;
149 }
150
151 if (verbose) {
152 printf("[Server] %s\n", buffer);
153 }
154 }
155 else {
156 if (verbose) {
157 printf("[-] Timeout\n");
158 }
159 return -1;
160 }
161 }
162
163 buffer[n] = '\0';
164
165
166 if (find_pasv(buffer, 0) != 0) { //Error found; passive mode message not found!
167 free(buffer);
168 data_port = 0;
169
170 return -1; //return error
171
172 }
173
174 if (verbose)
175 printf("(PASV) port: %d <-> %d\n", data_port1, data_port2);
176
177 memset(buffer, 0x0, 1024);
178
179
180 data_port = ftp_DataPort(data_port1, data_port2);
181
182 if ( (data_port == 0) || (data_port < 0) ) {
183 free(buffer);
184 data_port1 = 0;
185 data_port2 = 0;
186
187 return WRONG_DATA_P;
188 }
189
190 free(buffer);
191
192 return 0;
193
194 }

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