1 #ifndef R2_SOCKET_H
2 #define R2_SOCKET_H
3 
4 #include "r_types.h"
5 #include "r_bind.h"
6 #include "r_list.h"
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 R_LIB_VERSION_HEADER (r_socket);
13 
14 #if __UNIX__
15 #include <netinet/in.h>
16 #include <sys/un.h>
17 #include <poll.h>
18 #include <arpa/inet.h>
19 #include <netdb.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #endif
23 
24 #if HAVE_LIB_SSL
25 #include <openssl/ssl.h>
26 #include <openssl/err.h>
27 #endif
28 
29 #if __UNIX__
30 #include <netinet/tcp.h>
31 #endif
32 
33 /* For the Mingw-W64 toolchain */
34 #ifndef MSG_DONTWAIT
35 #define MSG_DONTWAIT 0
36 #endif
37 #ifndef SD_BOTH
38 #define SD_RECEIVE  0
39 #define SD_SEND 1
40 #define SD_BOTH 2
41 #endif
42 
43 #if _MSC_VER
44 #define R_INVALID_SOCKET INVALID_SOCKET
45 #else
46 #define R_INVALID_SOCKET -1
47 #endif
48 
49 typedef struct {
50 	int child;
51 #if __WINDOWS__
52 	HANDLE pipe;
53 #else
54 	int input[2];
55 	int output[2];
56 #endif
57 	RCoreBind coreb;
58 } R2Pipe;
59 
60 typedef struct r_socket_t {
61 #ifdef _MSC_VER
62 	SOCKET fd;
63 #else
64 	int fd;
65 #endif
66 	bool is_ssl;
67 	int proto;
68 	int local;	// TODO: merge ssl with local -> flags/options
69 	int port;
70 	struct sockaddr_in sa;
71 #if HAVE_LIB_SSL
72 	SSL_CTX *ctx;
73 	SSL *sfd;
74 	BIO *bio;
75 #endif
76 } RSocket;
77 
78 typedef struct r_socket_http_options {
79 	RList *authtokens;
80 	bool accept_timeout;
81 	int timeout;
82 	bool httpauth;
83 } RSocketHTTPOptions;
84 
85 #define R_SOCKET_PROTO_TCP IPPROTO_TCP
86 #define R_SOCKET_PROTO_UDP IPPROTO_UDP
87 #define R_SOCKET_PROTO_UNIX 0x1337
88 #define R_SOCKET_PROTO_NONE 0
89 #define R_SOCKET_PROTO_DEFAULT R_SOCKET_PROTO_TCP
90 
91 #ifdef R_API
92 R_API RSocket *r_socket_new_from_fd(int fd);
93 R_API RSocket *r_socket_new(bool is_ssl);
94 R_API bool r_socket_spawn(RSocket *s, const char *cmd, unsigned int timeout);
95 R_API bool r_socket_connect(RSocket *s, const char *host, const char *port, int proto, unsigned int timeout);
96 R_API int r_socket_connect_serial(RSocket *sock, const char *path, int speed, int parity);
97 #define r_socket_connect_tcp(a, b, c, d) r_socket_connect (a, b, c, R_SOCKET_PROTO_TCP, d)
98 #define r_socket_connect_udp(a, b, c, d) r_socket_connect (a, b, c, R_SOCKET_PROTO_UDP, d)
99 #if __UNIX__
100 #define r_socket_connect_unix(a, b) r_socket_connect (a, b, b, R_SOCKET_PROTO_UNIX, 0)
101 #else
102 #define r_socket_connect_unix(a, b) (false)
103 #endif
104 R_API bool r_socket_listen(RSocket *s, const char *port, const char *certfile);
105 R_API int r_socket_port_by_name(const char *name);
106 R_API int r_socket_close_fd(RSocket *s);
107 R_API int r_socket_close(RSocket *s);
108 R_API int r_socket_free(RSocket *s);
109 R_API RSocket *r_socket_accept(RSocket *s);
110 R_API RSocket *r_socket_accept_timeout(RSocket *s, unsigned int timeout);
111 R_API bool r_socket_block_time(RSocket *s, bool block, int sec, int usec);
112 R_API int r_socket_flush(RSocket *s);
113 R_API int r_socket_ready(RSocket *s, int secs, int usecs);
114 R_API char *r_socket_to_string(RSocket *s);
115 R_API int r_socket_write(RSocket *s, void *buf, int len);
116 R_API int r_socket_puts(RSocket *s, char *buf);
117 R_API void r_socket_printf(RSocket *s, const char *fmt, ...) R_PRINTF_CHECK(2, 3);
118 R_API int r_socket_read(RSocket *s, ut8 *read, int len);
119 R_API int r_socket_read_block(RSocket *s, unsigned char *buf, int len);
120 R_API int r_socket_gets(RSocket *s, char *buf, int size);
121 R_API ut8 *r_socket_slurp(RSocket *s, int *len);
122 R_API bool r_socket_is_connected(RSocket *);
123 
124 /* process */
125 typedef struct r_socket_proc_t {
126 	int fd0[2];
127 	int fd1[2];
128 	int pid;
129 } RSocketProc;
130 
131 R_API RSocketProc *r_socket_proc_open(char *const argv[]);
132 R_API int r_socket_proc_close(RSocketProc *sp);
133 R_API int r_socket_proc_read(RSocketProc *sp, unsigned char *buf, int len);
134 R_API int r_socket_proc_gets(RSocketProc *sp, char *buf, int size);
135 R_API int r_socket_proc_write(RSocketProc *sp, void *buf, int len);
136 R_API void r_socket_proc_printf(RSocketProc *sp, const char *fmt, ...) R_PRINTF_CHECK(2, 3);
137 R_API int r_socket_proc_ready(RSocketProc *sp, int secs, int usecs);
138 
139 /* HTTP */
140 R_API char *r_socket_http_get(const char *url, int *code, int *rlen);
141 R_API char *r_socket_http_post(const char *url, const char *data, int *code, int *rlen);
142 R_API void r_socket_http_server_set_breaked(bool *b);
143 
144 typedef struct r_socket_http_request {
145 	RSocket *s;
146 	char *path;
147 	char *host;
148 	char *agent;
149 	char *method;
150 	char *referer;
151 	ut8 *data;
152 	int data_length;
153 	bool auth;
154 } RSocketHTTPRequest;
155 
156 R_API RSocketHTTPRequest *r_socket_http_accept(RSocket *s, RSocketHTTPOptions *so);
157 R_API void r_socket_http_response(RSocketHTTPRequest *rs, int code, const char *out, int x, const char *headers);
158 R_API void r_socket_http_close(RSocketHTTPRequest *rs);
159 R_API ut8 *r_socket_http_handle_upload(const ut8 *str, int len, int *olen);
160 R_API void r_socket_http_free(RSocketHTTPRequest *rs);
161 
162 typedef int (*rap_server_open)(void *user, const char *file, int flg, int mode);
163 typedef int (*rap_server_seek)(void *user, ut64 offset, int whence);
164 typedef int (*rap_server_read)(void *user, ut8 *buf, int len);
165 typedef int (*rap_server_write)(void *user, ut8 *buf, int len);
166 typedef char *(*rap_server_cmd)(void *user, const char *command);
167 typedef int (*rap_server_close)(void *user, int fd);
168 
169 enum {
170 	RAP_PACKET_OPEN = 1,
171 	RAP_PACKET_READ = 2,
172 	RAP_PACKET_WRITE = 3,
173 	RAP_PACKET_SEEK = 4,
174 	RAP_PACKET_CLOSE = 5,
175 	// system was deprecated in slot 6,
176 	RAP_PACKET_CMD = 7,
177 	RAP_PACKET_REPLY = 0x80,
178 	RAP_PACKET_MAX = 4096
179 };
180 
181 typedef struct r_socket_rap_server_t {
182 	RSocket *fd;
183 	char *port;
184 	ut8 buf[RAP_PACKET_MAX + 32];	// This should be used as a static buffer for everything done by the server
185 	rap_server_open open;
186 	rap_server_seek seek;
187 	rap_server_read read;
188 	rap_server_write write;
189 	rap_server_cmd system;
190 	rap_server_cmd cmd;
191 	rap_server_close close;
192 	void *user;	// Always first arg for callbacks
193 } RSocketRapServer;
194 
195 R_API RSocketRapServer *r_socket_rap_server_new(bool is_ssl, const char *port);
196 R_API RSocketRapServer *r_socket_rap_server_create(const char *pathname);
197 R_API void r_socket_rap_server_free(RSocketRapServer *rap_s);
198 R_API bool r_socket_rap_server_listen(RSocketRapServer *rap_s, const char *certfile);
199 R_API RSocket *r_socket_rap_server_accept(RSocketRapServer *rap_s);
200 R_API bool r_socket_rap_server_continue(RSocketRapServer *rap_s);
201 
202 /* rap client */
203 R_API int r_socket_rap_client_open(RSocket *s, const char *file, int rw);
204 R_API char *r_socket_rap_client_command(RSocket *s, const char *cmd, RCoreBind *c);
205 R_API int r_socket_rap_client_write(RSocket *s, const ut8 *buf, int count);
206 R_API int r_socket_rap_client_read(RSocket *s, ut8 *buf, int count);
207 R_API int r_socket_rap_client_seek(RSocket *s, ut64 offset, int whence);
208 
209 /* run.c */
210 #define R_RUN_PROFILE_NARGS 512
211 typedef struct r_run_profile_t {
212 	char *_args[R_RUN_PROFILE_NARGS];
213 	int _argc;
214 	bool _daemon;
215 	char *_system;
216 	char *_program;
217 	char *_runlib;
218 	char *_runlib_fcn;
219 	char *_stdio;
220 	char *_stdin;
221 	char *_stdout;
222 	char *_stderr;
223 	char *_chgdir;
224 	char *_chroot;
225 	char *_libpath;
226 	char *_preload;
227 	int _bits;
228 	int _pid;
229 	char *_pidfile;
230 	int _r2preload;
231 	int _docore;
232 	int _dofork;
233 	int _dodebug;
234 	int _aslr;
235 	int _maxstack;
236 	int _maxproc;
237 	int _maxfd;
238 	int _r2sleep;
239 	int _execve;
240 	char *_setuid;
241 	char *_seteuid;
242 	char *_setgid;
243 	char *_setegid;
244 	char *_input;
245 	char *_connect;
246 	char *_listen;
247 	int _pty;
248 	int _timeout;
249 	int _timeout_sig;
250 	int _nice;
251 } RRunProfile;
252 
253 R_API RRunProfile *r_run_new(const char *str);
254 R_API bool r_run_parse(RRunProfile *pf, const char *profile);
255 R_API void r_run_free(RRunProfile *r);
256 R_API bool r_run_parseline(RRunProfile *p, const char *b);
257 R_API const char *r_run_help(void);
258 R_API int r_run_config_env(RRunProfile *p);
259 R_API int r_run_start(RRunProfile *p);
260 R_API void r_run_reset(RRunProfile *p);
261 R_API bool r_run_parsefile(RRunProfile *p, const char *b);
262 R_API char *r_run_get_environ_profile(char **env);
263 
264 /* rapipe */
265 R_API R2Pipe *rap_open(const char *cmd);
266 R_API R2Pipe *rap_open_corebind(RCoreBind *coreb);
267 R_API int rap_close(R2Pipe *rap);
268 
269 R_API char *rap_cmd(R2Pipe *rap, const char *str);
270 R_API char *rap_cmdf(R2Pipe *rap, const char *fmt, ...) R_PRINTF_CHECK(2, 3);
271 
272 R_API int rap_write(R2Pipe *rap, const char *str);
273 R_API char *rap_read(R2Pipe *rap);
274 
275 R_API int r2pipe_write(R2Pipe *r2pipe, const char *str);
276 R_API char *r2pipe_read(R2Pipe *r2pipe);
277 R_API int r2pipe_close(R2Pipe *r2pipe);
278 R_API R2Pipe *r2pipe_open_corebind(RCoreBind *coreb);
279 R_API R2Pipe *r2pipe_open(const char *cmd);
280 R_API R2Pipe *r2pipe_open_dl(const char *file);
281 R_API char *r2pipe_cmd(R2Pipe *r2pipe, const char *str);
282 R_API char *r2pipe_cmdf(R2Pipe *r2pipe, const char *fmt, ...) R_PRINTF_CHECK(2, 3);
283 #endif
284 
285 #ifdef __cplusplus
286 }
287 #endif
288 
289 #endif
290