Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (hide annotations) (download) (as text)
Fri Feb 20 12:10:40 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftpconn.c
File MIME type: text/x-csrc
File size: 4013 byte(s)
moved variable declaration  for traditional compilers. such as QNX6.2.

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     /* Struct sockaddr_in for the connections of the transfers way */
37     struct sockaddr_in ftp_connection;
38    
39     /***
40     ftp_connect:
41 hirohitohigashi 7 - char *host => HostName/IP
42     - int port => Port to connect to
43     - struct sockaddr_in * => pointer to sockaddr_in structure
44     - int verbose => Verbose?
45     . 0 Do not Print
46     . 1 Print
47     - ret => Success?
48     . sock Socket Descriptor Returned
49     .-1 Error
50 hirohitohigashi 1 ***/
51    
52    
53 hirohitohigashi 7 int ftp_connect(char *host, int port, struct sockaddr_in *ftp_server, int verbose)
54     {
55     struct hostent *he;
56     int sock;
57     struct timeval tm; //tv_sec - tv_usec
58     fd_set readfds;
59     char buf[1024];
60     int n;
61 hirohitohigashi 1
62 hirohitohigashi 7
63     if ( (he=gethostbyname(host)) == NULL) {
64 hirohitohigashi 1
65     if (verbose) {
66    
67 hirohitohigashi 7 if (h_errno == HOST_NOT_FOUND) {
68 hirohitohigashi 1 printf("[-] Host Not Found!\n");
69     printf("exiting..\n");
70     return(HOST_NOT_FOUND);
71    
72 hirohitohigashi 7 } else if (h_errno == TRY_AGAIN) {
73 hirohitohigashi 1 printf("[-] Temporary error.. Try Again!\n");
74     printf("exiting..\n");
75     return(TRY_AGAIN);
76    
77 hirohitohigashi 7 } else if (h_errno == NO_RECOVERY) {
78 hirohitohigashi 1 printf("[-] Unexpected Server Error!\n");
79     printf("exiting..\n");
80     return(NO_RECOVERY);
81    
82 hirohitohigashi 7 } else if (h_errno == NO_DATA) {
83 hirohitohigashi 1 printf("[-] HostName has not IP Address!\n");
84     printf("exiting..\n");
85     return(NO_DATA);
86    
87 hirohitohigashi 7 }
88 hirohitohigashi 1
89     } else {
90    
91 hirohitohigashi 7 if (h_errno == HOST_NOT_FOUND) {
92 hirohitohigashi 1 return(HOST_NOT_FOUND);
93    
94 hirohitohigashi 7 } else if (h_errno == TRY_AGAIN) {
95 hirohitohigashi 1 return(TRY_AGAIN);
96    
97 hirohitohigashi 7 } else if (h_errno == NO_RECOVERY) {
98 hirohitohigashi 1 return(NO_RECOVERY);
99    
100 hirohitohigashi 7 } else if (h_errno == NO_DATA) {
101 hirohitohigashi 1 return(NO_DATA);
102    
103 hirohitohigashi 7 }
104 hirohitohigashi 1
105     }
106    
107     return -1;
108    
109 hirohitohigashi 7 }
110 hirohitohigashi 1
111 hirohitohigashi 7 memset(ftp_server,0,sizeof(ftp_server));
112     ftp_server->sin_port = htons(port);
113     ftp_server->sin_family = AF_INET;
114 hirohitohigashi 1
115 hirohitohigashi 7 memcpy ((char *)&ftp_server->sin_addr,he->h_addr,he->h_length);
116 hirohitohigashi 1
117    
118 hirohitohigashi 7 ftp_connection.sin_family = AF_INET;
119     memcpy ((char *)&ftp_connection.sin_addr, he->h_addr,he->h_length);
120 hirohitohigashi 1
121 hirohitohigashi 7 sock = socket(AF_INET, SOCK_STREAM, 0);
122     if (sock == -1) {
123 hirohitohigashi 1
124     if (verbose) {
125 hirohitohigashi 7 perror("Socket");
126 hirohitohigashi 1 }
127    
128     return -1;
129 hirohitohigashi 7 }
130 hirohitohigashi 1
131 hirohitohigashi 7 sleep(1);
132     if (connect(sock, (struct sockaddr *)ftp_server, sizeof(struct sockaddr)) != 0) {
133     if (verbose) {
134     perror("connect()");
135 hirohitohigashi 6 }
136 hirohitohigashi 7 return -1;
137     }
138 hirohitohigashi 1
139 hirohitohigashi 7 if (verbose) {
140     printf("[ Connected ]\n");
141     }
142 hirohitohigashi 1
143    
144 hirohitohigashi 7 buf[1023] = '\0';
145     FD_ZERO(&readfds);
146 hirohitohigashi 1
147 hirohitohigashi 7 tm.tv_sec = 3;
148     tm.tv_usec = 0;
149 hirohitohigashi 1
150 hirohitohigashi 7 FD_SET(sock, &readfds);
151     sleep(1); //wait the banner..
152 hirohitohigashi 1
153 hirohitohigashi 7 if (select(sock+1, &readfds, NULL, NULL, &tm) < 0 ) {
154     if (verbose) {
155     perror("select()");
156     }
157     return -1;
158     } else {
159    
160     if (FD_ISSET(sock, &readfds)) {
161     if ( (n = recv(sock, buf, 1022, 0)) < 0) {
162 hirohitohigashi 1 if (verbose) {
163 hirohitohigashi 7 perror("recv()");
164 hirohitohigashi 1 }
165     return -1;
166 hirohitohigashi 7 }
167     buf[n] = '\0';
168 hirohitohigashi 1
169 hirohitohigashi 7 if (verbose) {
170     printf("[Server] %s\n", buf);
171     }
172     }
173     else {
174     if (verbose)
175     printf("[-] Timeout\n");
176 hirohitohigashi 1
177 hirohitohigashi 7 return -1;
178 hirohitohigashi 1 }
179 hirohitohigashi 7 }
180 hirohitohigashi 1
181    
182 hirohitohigashi 7 return sock; //Socket Descriptor returned!!
183 hirohitohigashi 1 }
184    

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