rocksock_cyassl.c: link against -lwolfssl
[rofl0r-rocksock.git] / rocksock.h
blobee8d2f16fc37bca3e617ecd0a4c323f889ab6cde
1 /*
2 * author: rofl0r
3 * License: LGPL 2.1+ with static linking exception
4 */
6 #ifndef _ROCKSOCK_H_
7 #define _ROCKSOCK_H_
9 #include <stddef.h>
10 #include <netdb.h>
11 #include <netinet/in.h>
12 #include <sys/socket.h>
14 #define MAX_PROXIES 16
16 typedef enum {
17 RS_PT_NONE = 0,
18 RS_PT_SOCKS4,
19 RS_PT_SOCKS5,
20 RS_PT_HTTP
21 } rs_proxyType;
23 typedef enum rs_errorType {
24 RS_ET_OWN = 0,
25 RS_ET_SYS,
26 RS_ET_GAI,
27 RS_ET_SSL,
28 RS_ET_MAX
29 } rs_errorType;
31 typedef enum rs_error {
32 RS_E_NO_ERROR = 0,
33 RS_E_NULL = 1,
34 RS_E_EXCEED_PROXY_LIMIT = 2,
35 RS_E_NO_SSL = 3,
36 RS_E_NO_SOCKET = 4,
37 RS_E_HIT_TIMEOUT = 5,
38 RS_E_OUT_OF_BUFFER = 6,
39 RS_E_SSL_GENERIC = 7,
40 RS_E_SOCKS4_NOAUTH = 8,
41 RS_E_SOCKS5_AUTH_EXCEEDSIZE = 9,
42 RS_E_SOCKS4_NO_IP6 = 10,
43 RS_E_PROXY_UNEXPECTED_RESPONSE = 11,
44 RS_E_TARGETPROXY_CONNECT_FAILED = 12,
45 RS_E_PROXY_AUTH_FAILED = 13,
46 RS_E_HIT_READTIMEOUT = 14,
47 RS_E_HIT_WRITETIMEOUT = 15,
48 RS_E_HIT_CONNECTTIMEOUT = 16,
49 RS_E_PROXY_GENERAL_FAILURE = 17,
50 RS_E_TARGETPROXY_NET_UNREACHABLE = 18,
51 RS_E_TARGETPROXY_HOST_UNREACHABLE = 19,
52 RS_E_TARGETPROXY_CONN_REFUSED = 20,
53 RS_E_TARGETPROXY_TTL_EXPIRED = 21,
54 RS_E_PROXY_COMMAND_NOT_SUPPORTED = 22,
55 RS_E_PROXY_ADDRESSTYPE_NOT_SUPPORTED = 23,
56 RS_E_REMOTE_DISCONNECTED = 24,
57 RS_E_MAX_ERROR = 25
58 } rs_error;
60 typedef struct {
61 rs_errorType errortype;
62 int error;
63 int line;
64 const char* file;
65 int failedProxy;
66 } rs_errorInfo;
68 typedef struct {
69 char* host;
70 unsigned short port;
71 } rs_hostInfo;
73 typedef struct {
74 rs_proxyType proxytype;
75 rs_hostInfo hostinfo;
76 char* username;
77 char* password;
78 } rs_proxy;
80 typedef struct rocksock {
81 int socket;
82 int connected;
83 unsigned long timeout;
84 rs_proxy proxies[MAX_PROXIES];
85 ptrdiff_t lastproxy;
86 rs_errorInfo lasterror;
87 void *ssl;
88 void *sslctx;
89 } rocksock;
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
95 #if 1 /* only available if USE_SSL was activated at build time */
96 void rocksock_init_ssl(void);
97 void rocksock_free_ssl(void);
98 #endif
100 /* all rocksock functions that return int return 0 on success or an errornumber on failure */
101 int rocksock_init(rocksock* sock);
102 int rocksock_set_timeout(rocksock* sock, unsigned long timeout_millisec);
103 int rocksock_add_proxy(rocksock* sock, rs_proxyType proxytype, const char* host, unsigned short port, const char* username, const char* password);
104 int rocksock_connect(rocksock* sock, const char* host, unsigned short port, int useSSL);
105 int rocksock_send(rocksock* sock, char* buffer, size_t bufsize, size_t chunksize, size_t* byteswritten);
106 int rocksock_recv(rocksock* sock, char* buffer, size_t bufsize, size_t chunksize, size_t* bytesread);
107 int rocksock_readline(rocksock* sock, char* buffer, size_t bufsize, size_t* bytesread);
108 int rocksock_disconnect(rocksock* sock);
110 /* returns a string describing the last error or NULL */
111 const char* rocksock_strerror(rocksock *sock);
112 /* return a string describing in which subsytem the last error happened, or NULL */
113 const char* rocksock_strerror_type(rocksock *sock);
115 enum rs_errorType rocksock_get_errortype(rocksock *sock);
116 int rocksock_get_error(rocksock *sock);
118 #define rocksock_error_dprintf(FD, RS) \
119 dprintf(FD, "%s:%d - %s error: %s from %s:%d\n", \
120 __FILE__, __LINE__, rocksock_strerror_type(RS), rocksock_strerror(RS), \
121 (RS)->lasterror.file, (RS)->lasterror.line)
123 /* clears/free's/resets all internally used buffers. etc but doesn't free the rocksock itself, since it could be stack-alloced */
124 int rocksock_clear(rocksock* sock);
125 /* check if data is available for read. result will contain 1 if available, 0 if not available.
126 return value 0 indicates success, everything else error. result may not be NULL */
127 int rocksock_peek(rocksock* sock, int *result);
129 /* using these two pulls in malloc from libc - only matters if you static link and dont use SSL */
130 /* returns a new heap alloced rocksock object which must be passed to rocksock_init later on */
131 rocksock* rocksock_new(void);
132 /* free *only* the rocksock object. typically you would call rocksock_clear first */
133 void rocksock_free(rocksock* s);
135 #ifdef __cplusplus
137 #endif
139 #endif
141 //RcB: DEP "rocksock.c"
142 //RcB: DEP "rocksock_add_proxy.c"
143 //RcB: DEP "rocksock_error.c"
144 //RcB: DEP "rocksock_strerror.c"
145 //RcB: DEP "rocksock_strerror_type.c"
146 //RcB: DEP "rocksock_dynamic.c"
147 //RcB: DEP "rocksock_readline.c"
148 //RcB: DEP "rocksock_peek.c"