Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (hide 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: 3501 byte(s)
Creating relase branch for 1.1
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 <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 hirohitohigashi 7 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 hirohitohigashi 1
63 hirohitohigashi 7
64     buf = (char *)malloc(1024*sizeof(char));
65 hirohitohigashi 1
66 hirohitohigashi 7 switch(t_type) {
67 hirohitohigashi 1
68 hirohitohigashi 7 case ASCIITYPE:
69 hirohitohigashi 11 sprintf(cmd_type, "TYPE A\r\n");
70 hirohitohigashi 1 break;
71    
72 hirohitohigashi 7 case NPRINTYPE:
73 hirohitohigashi 11 sprintf(cmd_type, "TYPE N\r\n");
74 hirohitohigashi 1 break;
75    
76 hirohitohigashi 7 case IMAGETYPE:
77 hirohitohigashi 11 sprintf(cmd_type, "TYPE I\r\n");
78 hirohitohigashi 1 break;
79    
80 hirohitohigashi 7 case EBCDIC:
81 hirohitohigashi 11 sprintf(cmd_type, "TYPE E\r\n");
82 hirohitohigashi 1 break;
83    
84 hirohitohigashi 7 case TELNET:
85 hirohitohigashi 11 sprintf(cmd_type, "TYPE T\r\n");
86 hirohitohigashi 1 break;
87    
88 hirohitohigashi 7 default:
89 hirohitohigashi 1 free(buf);
90    
91     return -1;
92     break;
93    
94 hirohitohigashi 7 }
95 hirohitohigashi 1
96 hirohitohigashi 7 memset(buf, 0x0, 1024);
97 hirohitohigashi 1
98    
99     if (write(sck, cmd_type, strlen(cmd_type)) == -1) {
100    
101 hirohitohigashi 7 if (verbose) {
102     if (errno == EBADF) {
103     fprintf(stderr, "Socket Descriptor Not Valid!\n");
104     free(buf);
105 hirohitohigashi 1
106 hirohitohigashi 7 return -1;
107     } else if (errno == EIO) {
108     fprintf(stderr, "I/O Error!\n");
109     free(buf);
110 hirohitohigashi 1
111 hirohitohigashi 7 return -1;
112     } else if (errno == EINTR) {
113     fprintf(stderr, "Signal Interrupted The write() Function\n");
114     free(buf);
115 hirohitohigashi 1
116 hirohitohigashi 7 return -1;
117     }
118 hirohitohigashi 1
119 hirohitohigashi 7 } else {
120 hirohitohigashi 1
121 hirohitohigashi 7 if (errno == EBADF) {
122     free(buf);
123 hirohitohigashi 1
124 hirohitohigashi 7 return -1;
125     } else if (errno == EIO) {
126     free(buf);
127 hirohitohigashi 1
128 hirohitohigashi 7 return -1;
129     } else if (errno == EINTR) {
130     free(buf);
131 hirohitohigashi 1
132 hirohitohigashi 7 return -1;
133     }
134 hirohitohigashi 1
135 hirohitohigashi 7 }
136 hirohitohigashi 1
137 hirohitohigashi 7 free(buf);
138 hirohitohigashi 1
139 hirohitohigashi 7 return -1;
140 hirohitohigashi 1
141 hirohitohigashi 7 }
142 hirohitohigashi 1
143 hirohitohigashi 7 tm.tv_sec = 3;
144     tm.tv_usec = 0;
145 hirohitohigashi 1
146 hirohitohigashi 7 FD_ZERO(&readfds);
147 hirohitohigashi 1
148 hirohitohigashi 7 FD_SET(sck, &readfds);
149 hirohitohigashi 1
150 hirohitohigashi 7 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 hirohitohigashi 1 if (verbose) {
163 hirohitohigashi 7 perror("recv()");
164 hirohitohigashi 1 }
165     free(buf);
166    
167     return -1;
168 hirohitohigashi 7 }
169 hirohitohigashi 1
170 hirohitohigashi 7 if (verbose)
171     printf("[Server] %s\n", buf);
172 hirohitohigashi 1
173 hirohitohigashi 7 }
174     else {
175     if (verbose)
176     printf("[-] Timeout\n");
177 hirohitohigashi 1
178 hirohitohigashi 7 return -1;
179 hirohitohigashi 1 }
180 hirohitohigashi 7 }
181 hirohitohigashi 1
182 hirohitohigashi 7 buf[n] = '\0';
183 hirohitohigashi 1
184 hirohitohigashi 7 memset(buf, 0x0, 1024);
185 hirohitohigashi 1
186 hirohitohigashi 7 free(buf);
187 hirohitohigashi 1
188    
189     return 0; //0 returned; operation sucessfull!
190     }

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