Rizin
unix-like reverse engineering framework and cli tools
rz_socket.h
Go to the documentation of this file.
1 #ifndef RZ_SOCKET_H
2 #define RZ_SOCKET_H
3 
4 #include "rz_types.h"
5 #include "rz_bind.h"
6 #include "rz_list.h"
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
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 #elif __WINDOWS__
23 #include <rz_windows.h>
24 #endif
25 
26 #if HAVE_LIB_SSL
27 #include <openssl/ssl.h>
28 #include <openssl/err.h>
29 #endif
30 
31 #if __UNIX__
32 #include <netinet/tcp.h>
33 #endif
34 
35 /* For the Mingw-W64 toolchain */
36 #ifndef MSG_DONTWAIT
37 #define MSG_DONTWAIT 0
38 #endif
39 #ifndef SD_BOTH
40 #define SD_RECEIVE 0
41 #define SD_SEND 1
42 #define SD_BOTH 2
43 #endif
44 
45 #if _MSC_VER
46 #define RZ_INVALID_SOCKET INVALID_SOCKET
47 #else
48 #define RZ_INVALID_SOCKET -1
49 #endif
50 
51 typedef struct {
52  int child;
53 #if __WINDOWS__
54  HANDLE pipe;
55 #else
56  int input[2];
57  int output[2];
58 #endif
60 } RzPipe;
61 
62 typedef struct rz_socket_t {
63 #ifdef _MSC_VER
64  SOCKET fd;
65 #else
66  int fd;
67 #endif
68  bool is_ssl;
69  int proto;
70  int local; // TODO: merge ssl with local -> flags/options
71  int port;
72  struct sockaddr_in sa;
73 #if HAVE_LIB_SSL
74  SSL_CTX *ctx;
75  SSL *sfd;
76  BIO *bio;
77 #endif
79 
80 typedef struct rz_socket_http_options {
83  int timeout;
84  bool httpauth;
86 
87 #define RZ_SOCKET_PROTO_TCP IPPROTO_TCP
88 #define RZ_SOCKET_PROTO_UDP IPPROTO_UDP
89 #define RZ_SOCKET_PROTO_UNIX 0x1337
90 #define RZ_SOCKET_PROTO_NONE 0
91 #define RZ_SOCKET_PROTO_DEFAULT RZ_SOCKET_PROTO_TCP
92 
93 #ifdef RZ_API
95 RZ_API RzSocket *rz_socket_new(bool is_ssl);
96 RZ_API bool rz_socket_spawn(RzSocket *s, const char *cmd, unsigned int timeout);
97 RZ_API bool rz_socket_connect(RzSocket *s, const char *host, const char *port, int proto, unsigned int timeout);
98 RZ_API int rz_socket_connect_serial(RzSocket *sock, const char *path, int speed, int parity);
99 #define rz_socket_connect_tcp(a, b, c, d) rz_socket_connect(a, b, c, RZ_SOCKET_PROTO_TCP, d)
100 #define rz_socket_connect_udp(a, b, c, d) rz_socket_connect(a, b, c, RZ_SOCKET_PROTO_UDP, d)
101 #if __UNIX__
102 #define rz_socket_connect_unix(a, b) rz_socket_connect(a, b, b, RZ_SOCKET_PROTO_UNIX, 0)
103 #else
104 #define rz_socket_connect_unix(a, b) (false)
105 #endif
106 RZ_API bool rz_socket_listen(RzSocket *s, const char *port, const char *certfile);
107 RZ_API int rz_socket_port_by_name(const char *name);
113 RZ_API bool rz_socket_block_time(RzSocket *s, bool block, int sec, int usec);
115 RZ_API int rz_socket_ready(RzSocket *s, int secs, int usecs);
117 RZ_API int rz_socket_write(RzSocket *s, void *buf, int len);
118 RZ_API int rz_socket_puts(RzSocket *s, char *buf);
119 RZ_API void rz_socket_printf(RzSocket *s, const char *fmt, ...) RZ_PRINTF_CHECK(2, 3);
120 RZ_API int rz_socket_read(RzSocket *s, ut8 *read, int len);
121 RZ_API int rz_socket_read_block(RzSocket *s, unsigned char *buf, int len);
122 RZ_API int rz_socket_gets(RzSocket *s, char *buf, int size);
125 
126 /* process */
127 typedef struct rz_socket_proc_t {
128  int fd0[2];
129  int fd1[2];
130  int pid;
132 
135 RZ_API int rz_socket_proc_read(RzSocketProc *sp, unsigned char *buf, int len);
138 RZ_API void rz_socket_proc_printf(RzSocketProc *sp, const char *fmt, ...) RZ_PRINTF_CHECK(2, 3);
139 RZ_API int rz_socket_proc_ready(RzSocketProc *sp, int secs, int usecs);
140 
141 /* HTTP */
142 RZ_API char *rz_socket_http_get(const char *url, int *code, int *rlen);
143 RZ_API char *rz_socket_http_post(const char *url, const char *data, int *code, int *rlen);
145 
146 typedef struct rz_socket_http_request {
148  char *path;
149  char *host;
150  char *agent;
151  char *method;
152  char *referer;
155  bool auth;
157 
159 RZ_API void rz_socket_http_response(RzSocketHTTPRequest *rs, int code, const char *out, int x, const char *headers);
161 RZ_API ut8 *rz_socket_http_handle_upload(const ut8 *str, int len, int *olen);
162 
163 typedef int (*rap_server_open)(void *user, const char *file, int flg, int mode);
164 typedef int (*rap_server_seek)(void *user, ut64 offset, int whence);
165 typedef int (*rap_server_read)(void *user, ut8 *buf, int len);
166 typedef int (*rap_server_write)(void *user, ut8 *buf, int len);
167 typedef char *(*rap_server_cmd)(void *user, const char *command);
168 typedef int (*rap_server_close)(void *user, int fd);
169 
170 enum {
176  // system was deprecated in slot 6,
179  RAP_PACKET_MAX = 4096
180 };
181 
182 typedef struct rz_socket_rap_server_t {
184  char *port;
185  ut8 buf[RAP_PACKET_MAX + 32]; // This should be used as a static buffer for everything done by the server
193  void *user; // Always first arg for callbacks
195 
196 RZ_API RzSocketRapServer *rz_socket_rap_server_new(bool is_ssl, const char *port);
199 RZ_API bool rz_socket_rap_server_listen(RzSocketRapServer *rap_s, const char *certfile);
202 
203 /* rap client */
204 RZ_API int rz_socket_rap_client_open(RzSocket *s, const char *file, int rw);
209 
210 /* run.c */
211 #define RZ_RUN_PROFILE_NARGS 512
212 typedef struct rz_run_profile_t {
214  int _argc;
215  bool _daemon;
216  char *_system;
217  char *_program;
218  char *_runlib;
219  char *_runlib_fcn;
220  char *_stdio;
221  char *_stdin;
222  char *_stdout;
223  char *_stderr;
224  char *_chgdir;
225  char *_chroot;
226  char *_libpath;
227  char *_preload;
228  int _bits;
229  int _pid;
230  char *_pidfile;
232  int _docore;
233  int _dofork;
234  int _dodebug;
235  int _aslr;
237  int _maxproc;
238  int _maxfd;
239  int _rzsleep;
240  int _execve;
241  char *_setuid;
242  char *_seteuid;
243  char *_setgid;
244  char *_setegid;
245  char *_input;
246  char *_connect;
247  char *_listen;
248  int _pty;
249  int _timeout;
251  int _nice;
253 
254 RZ_API RzRunProfile *rz_run_new(const char *str);
255 RZ_API bool rz_run_parse(RzRunProfile *pf, const char *profile);
257 RZ_API bool rz_run_parseline(RzRunProfile *p, const char *b);
258 RZ_API const char *rz_run_help(void);
262 RZ_API bool rz_run_parsefile(RzRunProfile *p, const char *b);
264 
265 RZ_API int rzpipe_write(RzPipe *rzpipe, const char *str);
266 RZ_API char *rzpipe_read(RzPipe *rzpipe);
267 RZ_API int rzpipe_close(RzPipe *rzpipe);
269 RZ_API RzPipe *rzpipe_open(const char *cmd);
270 RZ_API RzPipe *rzpipe_open_dl(const char *file);
271 RZ_API char *rzpipe_cmd(RzPipe *rzpipe, const char *str);
272 RZ_API char *rzpipe_cmdf(RzPipe *rzpipe, const char *fmt, ...) RZ_PRINTF_CHECK(2, 3);
273 #endif
274 
275 #ifdef __cplusplus
276 }
277 #endif
278 
279 #endif
size_t len
Definition: 6502dis.c:15
#define rs()
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define RZ_API
#define r
Definition: crypto_rc6.c:12
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
Definition: sflib.h:79
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
#define const
Definition: ansidecl.h:240
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char pathname
Definition: sflib.h:66
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname pipe
Definition: sflib.h:73
int x
Definition: mipsasm.c:20
url
Definition: setup.py:262
static RzSocket * s
Definition: rtr.c:28
RZ_API RzSocketProc * rz_socket_proc_open(char *const argv[])
Definition: socket_proc.c:17
RZ_API void rz_run_reset(RzRunProfile *p)
Definition: run.c:97
RZ_API bool rz_socket_rap_server_continue(RzSocketRapServer *rap_s)
RZ_API char * rz_socket_rap_client_command(RzSocket *s, const char *cmd, RzCoreBind *c)
RZ_API int rzpipe_close(RzPipe *rzpipe)
Definition: rzpipe.c:110
RZ_API int rz_socket_close(RzSocket *s)
Definition: socket.c:419
RZ_API int rz_socket_puts(RzSocket *s, char *buf)
Definition: socket.c:768
RZ_API bool rz_socket_rap_server_listen(RzSocketRapServer *rap_s, const char *certfile)
RZ_API RzPipe * rzpipe_open(const char *cmd)
Definition: rzpipe.c:235
RZ_API int rz_socket_read_block(RzSocket *s, unsigned char *buf, int len)
Definition: socket.c:808
RZ_API RzSocketHTTPRequest * rz_socket_http_accept(RzSocket *s, RzSocketHTTPOptions *so)
RZ_API bool rz_socket_is_connected(RzSocket *)
Definition: socket.c:101
RZ_API RzPipe * rzpipe_open_dl(const char *file)
Definition: rzpipe.c:217
RZ_API char * rzpipe_cmdf(RzPipe *rzpipe, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_LIB_VERSION_HEADER(rz_socket)
#define RZ_RUN_PROFILE_NARGS
Definition: rz_socket.h:211
RZ_API int rz_socket_proc_write(RzSocketProc *sp, void *buf, int len)
Definition: socket_proc.c:89
RZ_API RzSocket * rz_socket_rap_server_accept(RzSocketRapServer *rap_s)
RZ_API int rz_socket_close_fd(RzSocket *s)
Definition: socket.c:410
RZ_API int rz_socket_rap_client_open(RzSocket *s, const char *file, int rw)
RZ_API RzPipe * rzpipe_open_corebind(RzCoreBind *coreb)
Definition: rzpipe.c:209
RZ_API int rz_socket_ready(RzSocket *s, int secs, int usecs)
Definition: socket.c:688
RZ_API int rz_socket_port_by_name(const char *name)
Definition: socket.c:469
RZ_API void rz_run_free(RzRunProfile *r)
Definition: run.c:122
char *(* rap_server_cmd)(void *user, const char *command)
Definition: rz_socket.h:167
RZ_API RzSocket * rz_socket_accept(RzSocket *s)
Definition: socket.c:582
RZ_API bool rz_socket_spawn(RzSocket *s, const char *cmd, unsigned int timeout)
Definition: socket.c:208
RZ_API RzSocketRapServer * rz_socket_rap_server_new(bool is_ssl, const char *port)
RZ_API void rz_socket_http_response(RzSocketHTTPRequest *rs, int code, const char *out, int x, const char *headers)
RZ_API int rz_socket_rap_client_write(RzSocket *s, const ut8 *buf, int count)
RZ_API char * rzpipe_read(RzPipe *rzpipe)
Definition: rzpipe.c:61
RZ_API RzRunProfile * rz_run_new(const char *str)
Definition: run.c:86
RZ_API void rz_socket_http_server_set_breaked(bool *b)
RZ_API char * rzpipe_cmd(RzPipe *rzpipe, const char *str)
Definition: rzpipe.c:316
int(* rap_server_seek)(void *user, ut64 offset, int whence)
Definition: rz_socket.h:164
RZ_API bool rz_socket_listen(RzSocket *s, const char *port, const char *certfile)
Definition: socket.c:474
RZ_API void rz_socket_proc_printf(RzSocketProc *sp, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API int rz_socket_connect_serial(RzSocket *sock, const char *path, int speed, int parity)
Definition: socket_serial.c:63
RZ_API void RZ_API int rz_socket_proc_ready(RzSocketProc *sp, int secs, int usecs)
Definition: socket_proc.c:110
RZ_API int rz_socket_proc_read(RzSocketProc *sp, unsigned char *buf, int len)
Definition: socket_proc.c:75
struct rz_socket_http_options RzSocketHTTPOptions
RZ_API int rz_socket_proc_gets(RzSocketProc *sp, char *buf, int size)
Definition: socket_proc.c:82
RZ_API int rzpipe_write(RzPipe *rzpipe, const char *str)
Definition: rzpipe.c:36
int(* rap_server_write)(void *user, ut8 *buf, int len)
Definition: rz_socket.h:166
RZ_API const char * rz_run_help(void)
Definition: run.c:630
struct rz_socket_t RzSocket
RZ_API RzSocketRapServer * rz_socket_rap_server_create(const char *pathname)
RZ_API bool rz_run_parsefile(RzRunProfile *p, const char *b)
Definition: run.c:468
RZ_API int rz_socket_proc_close(RzSocketProc *sp)
Definition: socket_proc.c:60
struct rz_socket_rap_server_t RzSocketRapServer
RZ_API int rz_socket_flush(RzSocket *s)
Definition: socket.c:677
RZ_API ut8 * rz_socket_http_handle_upload(const ut8 *str, int len, int *olen)
RZ_API bool rz_socket_block_time(RzSocket *s, bool block, int sec, int usec)
Definition: socket.c:649
RZ_API char * rz_socket_to_string(RzSocket *s)
Definition: socket.c:699
RZ_API int rz_socket_gets(RzSocket *s, char *buf, int size)
Definition: socket.c:830
RZ_API void RZ_API int rz_socket_read(RzSocket *s, ut8 *read, int len)
Definition: socket.c:783
int(* rap_server_read)(void *user, ut8 *buf, int len)
Definition: rz_socket.h:165
int(* rap_server_close)(void *user, int fd)
Definition: rz_socket.h:168
RZ_API void rz_socket_printf(RzSocket *s, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API bool rz_socket_connect(RzSocket *s, const char *host, const char *port, int proto, unsigned int timeout)
Definition: socket.c:257
RZ_API RzSocket * rz_socket_accept_timeout(RzSocket *s, unsigned int timeout)
Definition: socket.c:626
RZ_API char * rz_socket_http_get(const char *url, int *code, int *rlen)
Definition: socket_http.c:287
RZ_API char * rz_socket_http_post(const char *url, const char *data, int *code, int *rlen)
Definition: socket_http.c:291
RZ_API bool rz_run_parseline(RzRunProfile *p, const char *b)
Definition: run.c:479
RZ_API char * rz_run_get_environ_profile(char **env)
Definition: run.c:1329
RZ_API int rz_run_start(RzRunProfile *p)
Definition: run.c:1081
RZ_API int rz_socket_rap_client_read(RzSocket *s, ut8 *buf, int count)
RZ_API RzSocket * rz_socket_new(bool is_ssl)
Definition: socket.c:179
RZ_API ut8 * rz_socket_slurp(RzSocket *s, int *len)
Definition: socket.c:868
RZ_API void rz_socket_rap_server_free(RzSocketRapServer *rap_s)
RZ_API RzSocket * rz_socket_new_from_fd(int fd)
Definition: socket.c:859
struct rz_run_profile_t RzRunProfile
@ RAP_PACKET_MAX
Definition: rz_socket.h:179
@ RAP_PACKET_READ
Definition: rz_socket.h:172
@ RAP_PACKET_SEEK
Definition: rz_socket.h:174
@ RAP_PACKET_OPEN
Definition: rz_socket.h:171
@ RAP_PACKET_CMD
Definition: rz_socket.h:177
@ RAP_PACKET_REPLY
Definition: rz_socket.h:178
@ RAP_PACKET_CLOSE
Definition: rz_socket.h:175
@ RAP_PACKET_WRITE
Definition: rz_socket.h:173
RZ_API int rz_run_config_env(RzRunProfile *p)
Definition: run.c:809
RZ_API int rz_socket_free(RzSocket *s)
Definition: socket.c:453
int(* rap_server_open)(void *user, const char *file, int flg, int mode)
Definition: rz_socket.h:163
RZ_API bool rz_run_parse(RzRunProfile *pf, const char *profile)
Definition: run.c:103
RZ_API int rz_socket_write(RzSocket *s, void *buf, int len)
Definition: socket.c:724
RZ_API int rz_socket_rap_client_seek(RzSocket *s, ut64 offset, int whence)
struct rz_socket_http_request RzSocketHTTPRequest
struct rz_socket_proc_t RzSocketProc
RZ_API void rz_socket_http_close(RzSocketHTTPRequest *rs)
#define RZ_PRINTF_CHECK(fmt, dots)
Definition: rz_types.h:192
static int
Definition: sfsocketcall.h:114
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
int child
Definition: rz_socket.h:52
RzCoreBind coreb
Definition: rz_socket.h:59
Definition: inftree9.h:24
Definition: gzappend.c:170
Definition: z80asm.h:102
char * _runlib_fcn
Definition: rz_socket.h:219
char * _args[RZ_RUN_PROFILE_NARGS]
Definition: rz_socket.h:213
rap_server_seek seek
Definition: rz_socket.h:187
rap_server_open open
Definition: rz_socket.h:186
rap_server_close close
Definition: rz_socket.h:192
rap_server_cmd system
Definition: rz_socket.h:190
rap_server_write write
Definition: rz_socket.h:189
rap_server_cmd cmd
Definition: rz_socket.h:191
rap_server_read read
Definition: rz_socket.h:188
bool is_ssl
Definition: rz_socket.h:68
struct sockaddr_in sa
Definition: rz_socket.h:72
const char * command
Definition: main.c:7
uv_timer_t timeout
Definition: main.c:9
static char ** env
Definition: sys.c:32
DWORD * HANDLE
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int sp
Definition: z80asm.c:91
static int file
Definition: z80asm.c:58
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115