Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations) (download) (as text)
Fri Feb 20 02:19:48 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftpconn.c
File MIME type: text/x-csrc
File size: 3870 byte(s)
I changed it to work in FreeBSD and MacOSX.

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
56 struct hostent *he;
57
58 if ( (he=gethostbyname(host)) == NULL) {
59
60 if (verbose) {
61
62 if (h_errno == HOST_NOT_FOUND) {
63 printf("[-] Host Not Found!\n");
64 printf("exiting..\n");
65 return(HOST_NOT_FOUND);
66
67 } else if (h_errno == TRY_AGAIN) {
68 printf("[-] Temporary error.. Try Again!\n");
69 printf("exiting..\n");
70 return(TRY_AGAIN);
71
72 } else if (h_errno == NO_RECOVERY) {
73 printf("[-] Unexpected Server Error!\n");
74 printf("exiting..\n");
75 return(NO_RECOVERY);
76
77 } else if (h_errno == NO_DATA) {
78 printf("[-] HostName has not IP Address!\n");
79 printf("exiting..\n");
80 return(NO_DATA);
81
82 }
83
84 } else {
85
86 if (h_errno == HOST_NOT_FOUND) {
87 return(HOST_NOT_FOUND);
88
89 } else if (h_errno == TRY_AGAIN) {
90 return(TRY_AGAIN);
91
92 } else if (h_errno == NO_RECOVERY) {
93 return(NO_RECOVERY);
94
95 } else if (h_errno == NO_DATA) {
96 return(NO_DATA);
97
98 }
99
100 }
101
102 return -1;
103
104 }
105
106 memset(ftp_server,0,sizeof(ftp_server));
107 ftp_server->sin_port = htons(port);
108 ftp_server->sin_family = AF_INET;
109
110 memcpy ((char *)&ftp_server->sin_addr,he->h_addr,he->h_length);
111
112
113 ftp_connection.sin_family = AF_INET;
114 memcpy ((char *)&ftp_connection.sin_addr, he->h_addr,he->h_length);
115
116 int sock;
117
118 sock = socket(AF_INET, SOCK_STREAM, 0);
119 if (sock == -1) {
120
121 if (verbose) {
122 perror("Socket");
123 }
124
125 return -1;
126 }
127
128 sleep(1);
129 struct timeval tm; //tv_sec - tv_usec
130 fd_set readfds;
131
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 char buf[1024];
145 buf[1023] = '\0';
146 int n;
147 FD_ZERO(&readfds);
148
149 tm.tv_sec = 3;
150 tm.tv_usec = 0;
151
152 FD_SET(sock, &readfds);
153 sleep(1); //wait the banner..
154
155 if (select(sock+1, &readfds, NULL, NULL, &tm) < 0 ) {
156 if (verbose) {
157 perror("select()");
158 }
159 return -1;
160 } else {
161
162 if (FD_ISSET(sock, &readfds)) {
163 if ( (n = recv(sock, buf, 1022, 0)) < 0) {
164 if (verbose) {
165 perror("recv()");
166 }
167 return -1;
168 }
169 buf[n] = '\0';
170
171 if (verbose) {
172 printf("[Server] %s\n", buf);
173 }
174 }
175 else {
176 if (verbose)
177 printf("[-] Timeout\n");
178
179 return -1;
180 }
181 }
182
183
184 return sock; //Socket Descriptor returned!!
185 }
186

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