Rizin
unix-like reverse engineering framework and cli tools
io_rap.c File Reference
#include <rz_io.h>
#include <rz_lib.h>
#include <rz_core.h>
#include <rz_socket.h>
#include <sys/types.h>

Go to the source code of this file.

Classes

struct  RzIORap
 

Macros

#define RzIORAP_FD(x)   (((x)->data) ? (((RzIORap *)((x)->data))->client) : NULL)
 
#define RzIORAP_IS_LISTEN(x)   (((RzIORap *)((x)->data))->listener)
 
#define RzIORAP_IS_VALID(x)   ((x) && ((x)->data) && ((x)->plugin == &rz_io_plugin_rap))
 

Functions

static int __rap_write (RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
 
static bool __rap_accept (RzIO *io, RzIODesc *desc, int fd)
 
static int __rap_read (RzIO *io, RzIODesc *fd, ut8 *buf, int count)
 
static int __rap_close (RzIODesc *fd)
 
static ut64 __rap_lseek (RzIO *io, RzIODesc *fd, ut64 offset, int whence)
 
static bool __rap_plugin_open (RzIO *io, const char *pathname, bool many)
 
static RzIODesc__rap_open (RzIO *io, const char *pathname, int rw, int mode)
 
static int __rap_listener (RzIODesc *fd)
 
static char * __rap_system (RzIO *io, RzIODesc *fd, const char *command)
 

Variables

RzIOPlugin rz_io_plugin_rap
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ RzIORAP_FD

#define RzIORAP_FD (   x)    (((x)->data) ? (((RzIORap *)((x)->data))->client) : NULL)

Definition at line 16 of file io_rap.c.

◆ RzIORAP_IS_LISTEN

#define RzIORAP_IS_LISTEN (   x)    (((RzIORap *)((x)->data))->listener)

Definition at line 17 of file io_rap.c.

◆ RzIORAP_IS_VALID

#define RzIORAP_IS_VALID (   x)    ((x) && ((x)->data) && ((x)->plugin == &rz_io_plugin_rap))

Definition at line 18 of file io_rap.c.

Function Documentation

◆ __rap_accept()

static bool __rap_accept ( RzIO io,
RzIODesc desc,
int  fd 
)
static

Definition at line 25 of file io_rap.c.

25  {
26  RzIORap *rap = desc ? desc->data : NULL;
27  if (rap && fd != -1) {
29  return true;
30  }
31  return false;
32 }
const char * desc
Definition: bin_vsf.c:19
#define NULL
Definition: cris-opc.c:27
RZ_API RzSocket * rz_socket_new_from_fd(int fd)
Definition: socket.c:859
Definition: core.c:2888
RzSocket * client
Definition: core.c:2890
static const z80_opcode fd[]
Definition: z80_tab.h:997

References RzIORap::client, desc, fd, NULL, and rz_socket_new_from_fd().

◆ __rap_close()

static int __rap_close ( RzIODesc fd)
static

Definition at line 39 of file io_rap.c.

39  {
40  int ret = -1;
41  if (RzIORAP_IS_VALID(fd)) {
42  if (RzIORAP_FD(fd) != NULL) {
43  RzIORap *r = fd->data;
44  if (r && fd->fd != -1) {
45  if (r->fd) {
46  (void)rz_socket_close(r->fd);
47  }
48  if (r->client) {
49  ret = rz_socket_close(r->client);
50  }
51  RZ_FREE(r);
52  }
53  }
54  } else {
55  eprintf("__rap_close: fdesc is not a rz_io_rap plugin\n");
56  }
57  return ret;
58 }
#define r
Definition: crypto_rc6.c:12
#define RzIORAP_FD(x)
Definition: io_rap.c:16
#define RzIORAP_IS_VALID(x)
Definition: io_rap.c:18
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API int rz_socket_close(RzSocket *s)
Definition: socket.c:419
#define RZ_FREE(x)
Definition: rz_types.h:369

References eprintf, fd, NULL, r, RZ_FREE, rz_socket_close(), RzIORAP_FD, and RzIORAP_IS_VALID.

◆ __rap_listener()

static int __rap_listener ( RzIODesc fd)
static

Definition at line 165 of file io_rap.c.

165  {
166  return (RzIORAP_IS_VALID(fd)) ? RzIORAP_IS_LISTEN(fd) : 0; // -1 ?
167 }
#define RzIORAP_IS_LISTEN(x)
Definition: io_rap.c:17

References fd, RzIORAP_IS_LISTEN, and RzIORAP_IS_VALID.

◆ __rap_lseek()

static ut64 __rap_lseek ( RzIO io,
RzIODesc fd,
ut64  offset,
int  whence 
)
static

Definition at line 60 of file io_rap.c.

60  {
61  RzSocket *s = RzIORAP_FD(fd);
62  return rz_socket_rap_client_seek(s, offset, whence);
63 }
voidpf uLong offset
Definition: ioapi.h:144
static RzSocket * s
Definition: rtr.c:28
RZ_API int rz_socket_rap_client_seek(RzSocket *s, ut64 offset, int whence)

References fd, rz_socket_rap_client_seek(), RzIORAP_FD, and s.

◆ __rap_open()

static RzIODesc* __rap_open ( RzIO io,
const char *  pathname,
int  rw,
int  mode 
)
static

Definition at line 69 of file io_rap.c.

69  {
70  int i, p, listenmode;
71  char *file, *port;
72 
73  if (!__rap_plugin_open(io, pathname, 0)) {
74  return NULL;
75  }
76  bool is_ssl = (!strncmp(pathname, "raps://", 7));
77  const char *host = pathname + (is_ssl ? 7 : 6);
78  if (!(port = strchr(host, ':'))) {
79  eprintf("rap: wrong uri\n");
80  return NULL;
81  }
82  listenmode = (*host == ':');
83  *port++ = 0;
84  if (!*port) {
85  return NULL;
86  }
87  p = atoi(port);
88  if ((file = strchr(port + 1, '/'))) {
89  *file = 0;
90  file++;
91  }
92  if (listenmode) {
93  if (p <= 0) {
94  eprintf("rap: cannot listen here. Try rap://:9999\n");
95  return NULL;
96  }
97  // TODO: Handle ^C signal (SIGINT, exit); // ???
98  eprintf("rap: listening at port %s ssl %s\n", port, (is_ssl) ? "on" : "off");
99  RzIORap *rior = RZ_NEW0(RzIORap);
100  rior->listener = true;
101  rior->client = rior->fd = rz_socket_new(is_ssl);
102  if (!rior->fd) {
103  free(rior);
104  return NULL;
105  }
106  if (is_ssl) {
107  if (file && *file) {
108  if (!rz_socket_listen(rior->fd, port, file)) {
109  rz_socket_free(rior->fd);
110  free(rior);
111  return NULL;
112  }
113  } else {
114  free(rior);
115  return NULL;
116  }
117  } else {
118  if (!rz_socket_listen(rior->fd, port, NULL)) {
119  rz_socket_free(rior->fd);
120  free(rior);
121  return NULL;
122  }
123  }
124  return rz_io_desc_new(io, &rz_io_plugin_rap,
125  pathname, rw, mode, rior);
126  }
127  RzSocket *s = rz_socket_new(is_ssl);
128  if (!s) {
129  eprintf("Cannot create new socket\n");
130  return NULL;
131  }
132  eprintf("Connecting to %s, port %s\n", host, port);
133  if (!rz_socket_connect(s, host, port, RZ_SOCKET_PROTO_TCP, 0)) {
134  eprintf("Cannot connect to '%s' (%d)\n", host, p);
135  rz_socket_free(s);
136  return NULL;
137  }
138  eprintf("Connected to: %s at port %s\n", host, port);
139  RzIORap *rior = RZ_NEW0(RzIORap);
140  if (!rior) {
141  rz_socket_free(s);
142  return NULL;
143  }
144  rior->listener = false;
145  rior->client = rior->fd = s;
146  if (file && *file) {
148  if (i == -1) {
149  free(rior);
150  rz_socket_free(s);
151  return NULL;
152  }
153  if (i > 0) {
154  eprintf("rap connection was successful. open %d\n", i);
155  // io->corebind.cmd (io->corebind.core, "e io.va=0");
156  io->corebind.cmd(io->corebind.core, ".R!i*");
157  io->corebind.cmd(io->corebind.core, ".R!f*");
158  io->corebind.cmd(io->corebind.core, ".R!om*");
159  }
160  }
161  return rz_io_desc_new(io, &rz_io_plugin_rap,
162  pathname, rw, mode, rior);
163 }
lzma_index ** i
Definition: index.h:629
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static bool __rap_plugin_open(RzIO *io, const char *pathname, bool many)
Definition: io_rap.c:65
RzIOPlugin rz_io_plugin_rap
Definition: io_rap.c:279
const char int mode
Definition: ioapi.h:137
void * p
Definition: libc.cpp:67
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
RZ_API RzIODesc * rz_io_desc_new(RzIO *io, RzIOPlugin *plugin, const char *uri, int flags, int mode, void *data)
Definition: io_desc.c:11
RZ_API int rz_socket_rap_client_open(RzSocket *s, const char *file, int rw)
RZ_API bool rz_socket_listen(RzSocket *s, const char *port, const char *certfile)
Definition: socket.c:474
RZ_API bool rz_socket_connect(RzSocket *s, const char *host, const char *port, int proto, unsigned int timeout)
Definition: socket.c:257
#define RZ_SOCKET_PROTO_TCP
Definition: rz_socket.h:87
RZ_API RzSocket * rz_socket_new(bool is_ssl)
Definition: socket.c:179
RZ_API int rz_socket_free(RzSocket *s)
Definition: socket.c:453
#define RZ_NEW0(x)
Definition: rz_types.h:284
RzSocket * fd
Definition: core.c:2889
bool listener
Definition: core.c:2891
Definition: gzappend.c:170
RzCoreCmd cmd
Definition: rz_bind.h:32
void * core
Definition: rz_bind.h:31
RzCoreBind corebind
Definition: rz_io.h:92
static int file
Definition: z80asm.c:58

References __rap_plugin_open(), RzIORap::client, rz_core_bind_t::cmd, rz_core_bind_t::core, rz_io_t::corebind, eprintf, RzIORap::fd, file, free(), i, RzIORap::listener, NULL, p, pathname, rz_io_desc_new(), rz_io_plugin_rap, RZ_NEW0, rz_socket_connect(), rz_socket_free(), rz_socket_listen(), rz_socket_new(), RZ_SOCKET_PROTO_TCP, rz_socket_rap_client_open(), and s.

◆ __rap_plugin_open()

static bool __rap_plugin_open ( RzIO io,
const char *  pathname,
bool  many 
)
static

Definition at line 65 of file io_rap.c.

65  {
66  return rz_str_startswith(pathname, "rap://") || rz_str_startswith(pathname, "raps://");
67 }
RZ_API bool rz_str_startswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string starts with a specifc sequence of characters (case sensitive)
Definition: str.c:3286

References pathname, and rz_str_startswith().

Referenced by __rap_open().

◆ __rap_read()

static int __rap_read ( RzIO io,
RzIODesc fd,
ut8 buf,
int  count 
)
static

Definition at line 34 of file io_rap.c.

34  {
35  RzSocket *s = RzIORAP_FD(fd);
37 }
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
voidpf void * buf
Definition: ioapi.h:138
RZ_API int rz_socket_rap_client_read(RzSocket *s, ut8 *buf, int count)

References count, fd, rz_socket_rap_client_read(), RzIORAP_FD, and s.

◆ __rap_system()

static char* __rap_system ( RzIO io,
RzIODesc fd,
const char *  command 
)
static

Definition at line 169 of file io_rap.c.

169  {
170  RzSocket *s = RzIORAP_FD(fd);
171  // TODO: bind core into RzSocket instead of pass the one from io?
173 #if 0
174  int ret, reslen = 0, cmdlen = 0;
175  unsigned int i;
176  char *ptr, *res, *str;
177  ut8 buf[RMT_MAX];
178 
179  buf[0] = RMT_CMD;
180  i = strlen (command) + 1;
181  if (i > RMT_MAX - 5) {
182  eprintf ("Command too long\n");
183  return NULL;
184  }
185  rz_write_be32 (buf + 1, i);
186  memcpy (buf + 5, command, i);
187  (void)rz_socket_write (s, buf, i+5);
188  rz_socket_flush (s);
189 
190  /* read reverse cmds */
191  for (;;) {
192  ret = rz_socket_read_block (s, buf, 1);
193  if (ret != 1) {
194  return NULL;
195  }
196  /* system back in the middle */
197  /* TODO: all pkt handlers should check for reverse queries */
198  if (buf[0] != RMT_CMD) {
199  break;
200  }
201  // run io->cmdstr
202  // return back the string
203  buf[0] |= RMT_REPLY;
204  memset (buf + 1, 0, 4);
205  ret = rz_socket_read_block (s, buf + 1, 4);
206  if (ret != 4) {
207  return NULL;
208  }
209  cmdlen = rz_read_at_be32 (buf, 1);
210  if (cmdlen + 1 == 0) { // check overflow
211  cmdlen = 0;
212  }
213  str = calloc (1, cmdlen + 1);
214  ret = rz_socket_read_block (s, (ut8*)str, cmdlen);
215  eprintf ("RUN %d CMD(%s)\n", ret, str);
216  if (str && *str) {
217  res = io->cb_core_cmdstr (io->user, str);
218  } else {
219  res = strdup ("");
220  }
221  eprintf ("[%s]=>(%s)\n", str, res);
222  reslen = strlen (res);
223  free (str);
224  rz_write_be32 (buf + 1, reslen);
225  memcpy (buf + 5, res, reslen);
226  free (res);
227  (void)rz_socket_write (s, buf, reslen + 5);
228  rz_socket_flush (s);
229  }
230 
231  // read
232  ret = rz_socket_read_block (s, buf + 1, 4);
233  if (ret != 4) {
234  return NULL;
235  }
236  if (buf[0] != (RMT_CMD | RMT_REPLY)) {
237  eprintf ("Unexpected rap cmd reply\n");
238  return NULL;
239  }
240 
241  i = rz_read_at_be32 (buf, 1);
242  ret = 0;
243  if (i > ST32_MAX) {
244  eprintf ("Invalid length\n");
245  return NULL;
246  }
247  ptr = (char *)calloc (1, i + 1);
248  if (ptr) {
249  int ir, tr = 0;
250  do {
251  ir = rz_socket_read_block (s, (ut8*)ptr + tr, i - tr);
252  if (ir < 1) {
253  break;
254  }
255  tr += ir;
256  } while (tr < i);
257  // TODO: use io->cb_printf() with support for \x00
258  ptr[i] = 0;
259  if (io->cb_printf) {
260  io->cb_printf ("%s", ptr);
261  } else {
262  if (write (1, ptr, i) != i) {
263  eprintf ("Failed to write\n");
264  }
265  }
266  free (ptr);
267  }
268 #if DEAD_CODE
269  /* Clean */
270  if (ret > 0) {
271  ret -= rz_socket_read (s, (ut8*)buf, RMT_MAX);
272  }
273 #endif
274 #endif
275 
276  return NULL;
277 }
static static fork write
Definition: sflib.h:33
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
static ut32 rz_read_at_be32(const void *src, size_t offset)
Definition: rz_endian.h:93
static void rz_write_be32(void *dest, ut32 val)
Definition: rz_endian.h:98
RZ_API char * rz_socket_rap_client_command(RzSocket *s, const char *cmd, RzCoreBind *c)
RZ_API int rz_socket_read_block(RzSocket *s, unsigned char *buf, int len)
Definition: socket.c:808
RZ_API int rz_socket_flush(RzSocket *s)
Definition: socket.c:677
RZ_API void RZ_API int rz_socket_read(RzSocket *s, ut8 *read, int len)
Definition: socket.c:783
RZ_API int rz_socket_write(RzSocket *s, void *buf, int len)
Definition: socket.c:724
#define ST32_MAX
Definition: rz_types_base.h:97
#define tr(opcode, mask, lose, flags)
PrintfCallback cb_printf
Definition: rz_io.h:91
const char * command
Definition: main.c:7

References calloc(), rz_io_t::cb_printf, command, rz_io_t::corebind, eprintf, fd, free(), i, memcpy(), memset(), NULL, rz_read_at_be32(), rz_socket_flush(), rz_socket_rap_client_command(), rz_socket_read(), rz_socket_read_block(), rz_socket_write(), rz_write_be32(), RzIORAP_FD, s, ST32_MAX, cmd_descs_generate::str, strdup(), tr, and write.

◆ __rap_write()

static int __rap_write ( RzIO io,
RzIODesc fd,
const ut8 buf,
int  count 
)
static

Definition at line 20 of file io_rap.c.

20  {
21  RzSocket *s = RzIORAP_FD(fd);
23 }
RZ_API int rz_socket_rap_client_write(RzSocket *s, const ut8 *buf, int count)

References count, fd, rz_socket_rap_client_write(), RzIORAP_FD, and s.

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_IO,
.data = &rz_io_plugin_rap,
}
@ RZ_LIB_TYPE_IO
Definition: rz_lib.h:69
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_io.h:117

Definition at line 296 of file io_rap.c.

◆ rz_io_plugin_rap

RzIOPlugin rz_io_plugin_rap
Initial value:
= {
.name = "rap",
.desc = "Remote binary protocol plugin",
.uris = "rap://,raps://",
.license = "MIT",
.listener = __rap_listener,
.open = __rap_open,
.close = __rap_close,
.read = __rap_read,
.lseek = __rap_lseek,
.system = __rap_system,
.write = __rap_write,
.accept = __rap_accept,
}
static RzIODesc * __rap_open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_rap.c:69
static bool __rap_accept(RzIO *io, RzIODesc *desc, int fd)
Definition: io_rap.c:25
static int __rap_close(RzIODesc *fd)
Definition: io_rap.c:39
static char * __rap_system(RzIO *io, RzIODesc *fd, const char *command)
Definition: io_rap.c:169
static int __rap_listener(RzIODesc *fd)
Definition: io_rap.c:165
static ut64 __rap_lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence)
Definition: io_rap.c:60
static int __rap_write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_rap.c:20
static int __rap_read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_rap.c:34

Definition at line 279 of file io_rap.c.

Referenced by __rap_open().