Develop and Download Open Source Software

Browse Subversion Repository

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

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