Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (show annotations) (download) (as text)
Mon Feb 23 02:05:06 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftptype.c
File MIME type: text/x-csrc
File size: 3501 byte(s)
changed the terminator of the ftp command from CR to CRLF.
and solved race condition in ftp_get().

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 #define ASCIITYPE 1 //ASCII Type
38 #define NPRINTYPE 2 //Non-Print Type
39 #define IMAGETYPE 3 //Image Type
40 #define EBCDIC 4 //EBCDIC Type
41 #define TELNET 5 //Telnet Type
42
43 /***
44 ftp_type:
45 - int sck => Socket Descriptor
46 - int t_type => What kind of TYPE? ASCII(1), IMAGE(3)...
47 - int verbose => Verbose?
48 . 0 Do not Print
49 . 1 Print
50 - ret => Success?
51 . 0 Success
52 .-1 Error
53 ***/
54
55 int ftp_type(int sck, int t_type, int verbose)
56 {
57 char cmd_type[10]={'\0'};
58 char *buf;
59 int n = 0;
60 struct timeval tm; //tv_sec - tv_usec
61 fd_set readfds;
62
63
64 buf = (char *)malloc(1024*sizeof(char));
65
66 switch(t_type) {
67
68 case ASCIITYPE:
69 sprintf(cmd_type, "TYPE A\r\n");
70 break;
71
72 case NPRINTYPE:
73 sprintf(cmd_type, "TYPE N\r\n");
74 break;
75
76 case IMAGETYPE:
77 sprintf(cmd_type, "TYPE I\r\n");
78 break;
79
80 case EBCDIC:
81 sprintf(cmd_type, "TYPE E\r\n");
82 break;
83
84 case TELNET:
85 sprintf(cmd_type, "TYPE T\r\n");
86 break;
87
88 default:
89 free(buf);
90
91 return -1;
92 break;
93
94 }
95
96 memset(buf, 0x0, 1024);
97
98
99 if (write(sck, cmd_type, strlen(cmd_type)) == -1) {
100
101 if (verbose) {
102 if (errno == EBADF) {
103 fprintf(stderr, "Socket Descriptor Not Valid!\n");
104 free(buf);
105
106 return -1;
107 } else if (errno == EIO) {
108 fprintf(stderr, "I/O Error!\n");
109 free(buf);
110
111 return -1;
112 } else if (errno == EINTR) {
113 fprintf(stderr, "Signal Interrupted The write() Function\n");
114 free(buf);
115
116 return -1;
117 }
118
119 } else {
120
121 if (errno == EBADF) {
122 free(buf);
123
124 return -1;
125 } else if (errno == EIO) {
126 free(buf);
127
128 return -1;
129 } else if (errno == EINTR) {
130 free(buf);
131
132 return -1;
133 }
134
135 }
136
137 free(buf);
138
139 return -1;
140
141 }
142
143 tm.tv_sec = 3;
144 tm.tv_usec = 0;
145
146 FD_ZERO(&readfds);
147
148 FD_SET(sck, &readfds);
149
150 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
151 if (verbose) {
152 perror("select()");
153 }
154 free(buf);
155
156 return -1;
157 } else {
158
159 if (FD_ISSET(sck, &readfds)) {
160
161 if ( (n = recv(sck, buf, 1022, 0)) < 0) {
162 if (verbose) {
163 perror("recv()");
164 }
165 free(buf);
166
167 return -1;
168 }
169
170 if (verbose)
171 printf("[Server] %s\n", buf);
172
173 }
174 else {
175 if (verbose)
176 printf("[-] Timeout\n");
177
178 return -1;
179 }
180 }
181
182 buf[n] = '\0';
183
184 memset(buf, 0x0, 1024);
185
186 free(buf);
187
188
189 return 0; //0 returned; operation sucessfull!
190 }

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