examples/portscanner: quick hack to make it work on single host/portrange
[rofl0r-rocksock.git] / rocksock.h
blob31328888d523b27c22729e682ee0c06c2f0812b8
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 typedef enum {
15 RS_PT_NONE = 0,
16 RS_PT_SOCKS4,
17 RS_PT_SOCKS5,
18 RS_PT_HTTP
19 } rs_proxyType;
21 typedef enum rs_errorType {
22 RS_ET_OWN = 0,
23 RS_ET_SYS,
24 RS_ET_GAI,
25 RS_ET_SSL,
26 RS_ET_MAX
27 } rs_errorType;
29 typedef enum rs_error {
30 RS_E_NO_ERROR = 0,
31 RS_E_NULL = 1,
32 RS_E_EXCEED_PROXY_LIMIT = 2,
33 RS_E_NO_SSL = 3,
34 RS_E_NO_SOCKET = 4,
35 RS_E_HIT_TIMEOUT = 5,
36 RS_E_OUT_OF_BUFFER = 6,
37 RS_E_SSL_GENERIC = 7,
38 RS_E_SOCKS4_NOAUTH = 8,
39 RS_E_SOCKS5_AUTH_EXCEEDSIZE = 9,
40 RS_E_SOCKS4_NO_IP6 = 10,
41 RS_E_PROXY_UNEXPECTED_RESPONSE = 11,
42 RS_E_TARGETPROXY_CONNECT_FAILED = 12,
43 RS_E_PROXY_AUTH_FAILED = 13,
44 RS_E_HIT_READTIMEOUT = 14,
45 RS_E_HIT_WRITETIMEOUT = 15,
46 RS_E_HIT_CONNECTTIMEOUT = 16,
47 RS_E_PROXY_GENERAL_FAILURE = 17,
48 RS_E_TARGETPROXY_NET_UNREACHABLE = 18,
49 RS_E_TARGETPROXY_HOST_UNREACHABLE = 19,
50 RS_E_TARGETPROXY_CONN_REFUSED = 20,
51 RS_E_TARGETPROXY_TTL_EXPIRED = 21,
52 RS_E_PROXY_COMMAND_NOT_SUPPORTED = 22,
53 RS_E_PROXY_ADDRESSTYPE_NOT_SUPPORTED = 23,
54 RS_E_REMOTE_DISCONNECTED = 24,
55 RS_E_NO_PROXYSTORAGE = 25,
56 RS_E_HOSTNAME_TOO_LONG = 26,
57 RS_E_INVALID_PROXY_URL = 27,
58 RS_E_MAX_ERROR = 28
59 } rs_error;
61 typedef struct {
62 rs_errorType errortype;
63 int error;
64 int line;
65 const char* file;
66 int failedProxy;
67 } rs_errorInfo;
69 typedef struct {
70 char host[256];
71 unsigned short port;
72 } rs_hostInfo;
74 typedef struct {
75 char username[256];
76 char password[256];
77 rs_hostInfo hostinfo;
78 rs_proxyType proxytype;
79 } rs_proxy;
81 typedef struct rocksock {
82 int socket;
83 int connected;
84 unsigned long timeout;
85 rs_proxy *proxies;
86 ptrdiff_t lastproxy;
87 rs_errorInfo lasterror;
88 void *ssl;
89 void *sslctx;
90 } rocksock;
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
96 void rocksock_init_ssl(void);
97 void rocksock_free_ssl(void);
99 /* all rocksock functions that return int return 0 on success or an errornumber on failure */
100 /* rocksock_init: pass empty rocksock struct and if you want to use proxies,
101 an array of rs_proxy's that you need to allocate yourself. */
102 int rocksock_init(rocksock* sock, rs_proxy *proxies);
103 int rocksock_set_timeout(rocksock* sock, unsigned long timeout_millisec);
104 int rocksock_add_proxy(rocksock* sock, rs_proxyType proxytype, const char* host, unsigned short port, const char* username, const char* password);
105 int rocksock_add_proxy_fromstring(rocksock* sock, const char *proxystring);
106 int rocksock_connect(rocksock* sock, const char* host, unsigned short port, int useSSL);
107 int rocksock_send(rocksock* sock, char* buffer, size_t bufsize, size_t chunksize, size_t* byteswritten);
108 int rocksock_recv(rocksock* sock, char* buffer, size_t bufsize, size_t chunksize, size_t* bytesread);
109 int rocksock_readline(rocksock* sock, char* buffer, size_t bufsize, size_t* bytesread);
110 int rocksock_disconnect(rocksock* sock);
112 /* returns a string describing the last error or NULL */
113 const char* rocksock_strerror(rocksock *sock);
114 /* return a string describing in which subsytem the last error happened, or NULL */
115 const char* rocksock_strerror_type(rocksock *sock);
116 /* return a string describing the last error, and which proxy caused it */
117 char* rocksock_strerror_detailed(rocksock *sock, char *msgbuf, size_t buflen);
119 enum rs_errorType rocksock_get_errortype(rocksock *sock);
120 int rocksock_get_error(rocksock *sock);
122 #define rocksock_error_dprintf(FD, RS) \
123 dprintf(FD, "%s:%d - %s error: %s from %s:%d\n", \
124 __FILE__, __LINE__, rocksock_strerror_type(RS), rocksock_strerror(RS), \
125 (RS)->lasterror.file, (RS)->lasterror.line)
127 /* clears/free's/resets all internally used buffers. etc but doesn't free the rocksock itself, since it could be stack-alloced */
128 int rocksock_clear(rocksock* sock);
129 /* check if data is available for read. result will contain 1 if available, 0 if not available.
130 return value 0 indicates success, everything else error. result may not be NULL */
131 int rocksock_peek(rocksock* sock, int *result);
133 /* using these two pulls in malloc from libc - only matters if you static link and dont use SSL */
134 /* returns a new heap alloced rocksock object which must be passed to rocksock_init later on */
135 rocksock* rocksock_new(void);
136 /* free *only* the rocksock object. typically you would call rocksock_clear first */
137 void rocksock_free(rocksock* s);
139 #ifdef __cplusplus
141 #endif
143 #endif
145 #pragma RcB2 DEP "rocksock.c"
146 #pragma RcB2 DEP "rocksock_add_proxy.c"
147 #pragma RcB2 DEP "rocksock_add_proxy_fromstring.c"
148 #pragma RcB2 DEP "rocksock_error.c"
149 #pragma RcB2 DEP "rocksock_strerror.c"
150 #pragma RcB2 DEP "rocksock_strerror_type.c"
151 #pragma RcB2 DEP "rocksock_strerror_detailed.c"
152 #pragma RcB2 DEP "rocksock_dynamic.c"
153 #pragma RcB2 DEP "rocksock_readline.c"
154 #pragma RcB2 DEP "rocksock_peek.c"
155 #pragma RcB2 DEP "rocksock_ssl.c"