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