update README
[rofl0r-rocksock.git] / examples / rocksock_test3.c
blob2840ac29d390939bba3c9bc5e87ff71e9f5006bd
1 // gcc -Wall -g rocksock.c rocksock_test2.c -o rocksock_test2
3 /*
5 * author: rofl0r
7 * License: LGPL 2.1+ with static linking exception
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "../rocksock.h"
16 //RcB: DEP "../rocksock.c"
18 #define checkerr if(ret) { \
19 if(sock.lasterror.failedProxy != -1) printf("failing proxy: %d\n", sock.lasterror.failedProxy); \
20 printf("%s:%d - error #%d: %s from %s:%d\n", __FILE__, __LINE__, sock.lasterror.error, sock.lasterror.errormsg, sock.lasterror.file, sock.lasterror.line); \
21 exit(2); \
24 int main(int argc, char** argv) {
25 rocksock sock;
26 rocksock* psock = &sock;
27 int ret;
28 char inbuf[1024];
29 size_t bytesread;
30 size_t chunksize = 512;
32 if(argc < 2) {
33 puts("need ip or dns name of a ftpserver as argv1");
34 exit(1);
36 #ifdef USE_SSL
37 rocksock_init_ssl();
38 #endif
40 rocksock_init(psock);
41 rocksock_set_timeout(psock, 10000);
42 //ret = rocksock_connect(psock, "b12.wimbli.com", 80, 0);
43 rocksock_add_proxy(psock, RS_PT_SOCKS4, "127.0.0.1", 9050, NULL, NULL);
44 //rocksock_add_proxy(psock, RS_PT_SOCKS5, "127.0.0.1", 31337, NULL, NULL);
45 //rocksock_add_proxy(psock, RS_PT_SOCKS5, "98.216.80.12", 5639, NULL, NULL);
48 ret = rocksock_connect(psock, argv[1],
49 #ifndef USE_SSL
50 21, 0
51 #else
52 443, 1
53 #endif
55 checkerr;
56 do {
57 ret = rocksock_readline(psock, inbuf, sizeof(inbuf)-1, &bytesread);
58 checkerr;
59 if(bytesread) puts(inbuf);
60 } while (bytesread && memcmp(inbuf, "220 ", 4));
62 ret = rocksock_send(psock, inbuf, snprintf(inbuf, sizeof(inbuf), "USER ftp\r\n"), 0, &bytesread);
63 checkerr;
64 puts(inbuf);
66 do {
67 ret = rocksock_readline(psock, inbuf, sizeof(inbuf)-1, &bytesread);
68 checkerr;
69 if(bytesread) puts(inbuf);
70 } while (bytesread && memcmp(inbuf, "331 ", 4));
72 ret = rocksock_send(psock, inbuf, snprintf(inbuf, sizeof(inbuf), "PASS none\r\n"), 0, &bytesread);
73 checkerr;
74 puts(inbuf);
76 do {
77 ret = rocksock_readline(psock, inbuf, sizeof(inbuf)-1, &bytesread);
78 checkerr;
79 if(bytesread) puts(inbuf);
80 } while (bytesread && memcmp(inbuf, "230 ", 4));
82 ret = rocksock_send(psock, inbuf, snprintf(inbuf, sizeof(inbuf), "PASV\r\n"), 0, &bytesread);
83 checkerr;
84 puts(inbuf);
86 do {
87 ret = rocksock_readline(psock, inbuf, sizeof(inbuf)-1, &bytesread);
88 checkerr;
89 if(bytesread) puts(inbuf);
90 } while (bytesread && memcmp(inbuf, "230 ", 4));
92 ret = rocksock_send(psock, inbuf, snprintf(inbuf, sizeof(inbuf), "LIST\r\n"), 0, &bytesread);
93 checkerr;
94 puts(inbuf);
96 do {
97 ret = rocksock_readline(psock, inbuf, sizeof(inbuf)-1, &bytesread);
98 checkerr;
99 if(bytesread) puts(inbuf);
100 } while (bytesread && memcmp(inbuf, "230 ", 4));
102 ret = rocksock_readline(psock, inbuf, sizeof(inbuf)-1, &bytesread);
103 checkerr;
104 if(bytesread) puts(inbuf);
106 rocksock_disconnect(psock);
107 rocksock_clear(psock);
108 #ifdef USE_SSL
109 rocksock_free_ssl();
110 #endif
111 return 0;