API change: put the onus to allocate proxy array on the user
[rofl0r-rocksock.git] / examples / rocksock_test2.c
blobd55574f80ec2b5571209b0a9802d32d7baeddd01
1 // gcc -Wall -g rocksock.c rocksock_test2.c -o rocksock_test2
2 /*
3 * author: rofl0r
5 * License: LGPL 2.1+ with static linking exception
6 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "../rocksock.h"
11 #define chk(X, ACTION) if(X) { rocksock_error_dprintf(2, psock); ACTION; }
12 #define checkerr chk(ret, exit(2))
14 char peer0_0[] = {
15 0x47, 0x45, 0x54, 0x20, 0x2f, 0x64, 0x66, 0x5f,
16 0x33, 0x31, 0x5f, 0x32, 0x35, 0x5f, 0x77, 0x69,
17 0x6e, 0x2e, 0x7a, 0x69, 0x70, 0x20, 0x48, 0x54,
18 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a,
19 0x55, 0x73, 0x65, 0x72, 0x2d, 0x41, 0x67, 0x65,
20 0x6e, 0x74, 0x3a, 0x20, 0x4f, 0x70, 0x65, 0x72,
21 0x61, 0x2f, 0x39, 0x2e, 0x38, 0x30, 0x20, 0x28,
22 0x58, 0x31, 0x31, 0x3b, 0x20, 0x4c, 0x69, 0x6e,
23 0x75, 0x78, 0x20, 0x78, 0x38, 0x36, 0x5f, 0x36,
24 0x34, 0x3b, 0x20, 0x55, 0x3b, 0x20, 0x65, 0x6e,
25 0x29, 0x20, 0x50, 0x72, 0x65, 0x73, 0x74, 0x6f,
26 0x2f, 0x32, 0x2e, 0x37, 0x2e, 0x36, 0x32, 0x20,
27 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x2f,
28 0x31, 0x31, 0x2e, 0x30, 0x31, 0x0d, 0x0a, 0x48,
29 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x62, 0x31, 0x32,
30 0x2e, 0x77, 0x69, 0x6d, 0x62, 0x6c, 0x69, 0x2e,
31 0x63, 0x6f, 0x6d, 0x0d, 0x0a, 0x41, 0x63, 0x63,
32 0x65, 0x70, 0x74, 0x3a, 0x20, 0x74, 0x65, 0x78,
33 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2c, 0x20,
34 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74,
35 0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x6d, 0x6c, 0x3b,
36 0x71, 0x3d, 0x30, 0x2e, 0x39, 0x2c, 0x20, 0x61,
37 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69,
38 0x6f, 0x6e, 0x2f, 0x78, 0x68, 0x74, 0x6d, 0x6c,
39 0x2b, 0x78, 0x6d, 0x6c, 0x2c, 0x20, 0x69, 0x6d,
40 0x61, 0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x2c,
41 0x20, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x6a,
42 0x70, 0x65, 0x67, 0x2c, 0x20, 0x69, 0x6d, 0x61,
43 0x67, 0x65, 0x2f, 0x67, 0x69, 0x66, 0x2c, 0x20,
44 0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x78, 0x2d,
45 0x78, 0x62, 0x69, 0x74, 0x6d, 0x61, 0x70, 0x2c,
46 0x20, 0x2a, 0x2f, 0x2a, 0x3b, 0x71, 0x3d, 0x30,
47 0x2e, 0x31, 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65,
48 0x70, 0x74, 0x2d, 0x4c, 0x61, 0x6e, 0x67, 0x75,
49 0x61, 0x67, 0x65, 0x3a, 0x20, 0x65, 0x6e, 0x2d,
50 0x55, 0x53, 0x2c, 0x65, 0x6e, 0x3b, 0x71, 0x3d,
51 0x30, 0x2e, 0x39, 0x0d, 0x0a, 0x41, 0x63, 0x63,
52 0x65, 0x70, 0x74, 0x2d, 0x43, 0x68, 0x61, 0x72,
53 0x73, 0x65, 0x74, 0x3a, 0x20, 0x69, 0x73, 0x6f,
54 0x2d, 0x38, 0x38, 0x35, 0x39, 0x2d, 0x31, 0x2c,
55 0x20, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x2c, 0x20,
56 0x75, 0x74, 0x66, 0x2d, 0x31, 0x36, 0x2c, 0x20,
57 0x2a, 0x3b, 0x71, 0x3d, 0x30, 0x2e, 0x31, 0x0d,
58 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
59 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67,
60 0x3a, 0x20, 0x64, 0x65, 0x66, 0x6c, 0x61, 0x74,
61 0x65, 0x2c, 0x20, 0x67, 0x7a, 0x69, 0x70, 0x2c,
62 0x20, 0x78, 0x2d, 0x67, 0x7a, 0x69, 0x70, 0x2c,
63 0x20, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74,
64 0x79, 0x2c, 0x20, 0x2a, 0x3b, 0x71, 0x3d, 0x30,
65 0x0d, 0x0a, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65,
66 0x72, 0x3a, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a,
67 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x62, 0x61,
68 0x79, 0x31, 0x32, 0x67, 0x61, 0x6d, 0x65, 0x73,
69 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x64, 0x77, 0x61,
70 0x72, 0x76, 0x65, 0x73, 0x2f, 0x0d, 0x0a, 0x43,
71 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f,
72 0x6e, 0x3a, 0x20, 0x4b, 0x65, 0x65, 0x70, 0x2d,
73 0x41, 0x6c, 0x69, 0x76, 0x65, 0x0d, 0x0a, 0x0d,
74 0x0a };
76 int main(int argc, char** argv) {
77 rocksock sock;
78 rocksock* psock = &sock;
79 int ret;
80 char inbuf[1024];
81 size_t bytesread;
82 size_t chunksize = 512;
84 if(argc < 2) {
85 puts("need ip or dns name of a webserver as argv1");
86 exit(1);
88 #ifdef USE_SSL
89 rocksock_init_ssl();
90 #endif
91 rs_proxy proxies[4];
92 rocksock_init(psock, proxies);
93 rocksock_set_timeout(psock, 10000);
94 //ret = rocksock_connect(psock, "b12.wimbli.com", 80, 0);
95 rocksock_add_proxy(psock, RS_PT_SOCKS4, "127.0.0.1", 9050, NULL, NULL);
96 //rocksock_add_proxy(psock, RS_PT_SOCKS5, "127.0.0.1", 31337, NULL, NULL);
97 //rocksock_add_proxy(psock, RS_PT_SOCKS5, "98.216.80.12", 5639, NULL, NULL);
100 ret = rocksock_connect(psock, argv[1],
101 #ifndef USE_SSL
102 80, 0
103 #else
104 443, 1
105 #endif
108 checkerr;
109 ret = rocksock_send(psock, "GET / HTTP/1.0\r\n\r\n", 0, 0, &bytesread);
110 //ret = rocksock_send(psock, peer0_0, 0, 0, &bytesread);
111 checkerr;
112 do {
113 puts("loop");
114 ret = rocksock_recv(psock, inbuf, sizeof(inbuf)-1, chunksize, &bytesread);
115 checkerr;
116 if(bytesread) {
117 inbuf[bytesread] = '\0';
118 puts(inbuf);
120 printf("bytesread %zu\n", bytesread);
121 } while (bytesread == chunksize);
122 do {
124 ret = rocksock_recv(psock, inbuf, sizeof(inbuf)-1, chunksize, &bytesread);
125 checkerr;
126 if(bytesread) {
127 inbuf[bytesread] = '\0';
128 puts(inbuf);
130 printf("bytesread %zu\n", bytesread);
131 } while (bytesread);
132 rocksock_disconnect(psock);
133 rocksock_clear(psock);
134 #ifdef USE_SSL
135 rocksock_free_ssl();
136 #endif
137 return 0;