Rizin
unix-like reverse engineering framework and cli tools
socket_rap_server.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2019 pancake <pancake@nopcode.org>
2 // SPDX-FileCopyrightText: 2014-2019 condret <condr3t@protonmail.com>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_socket.h>
6 #include <rz_util.h>
7 
8 RZ_API RzSocketRapServer *rz_socket_rap_server_new(bool use_ssl, const char *port) {
11  if (s) {
12  s->port = strdup(port);
13  s->fd = rz_socket_new(use_ssl);
14  if (s->fd) {
15  return s;
16  }
18  free(s);
19  }
20  return NULL;
21 }
22 
25  if (strlen(pathname) < 11) {
26  return NULL;
27  }
28  if (strncmp(pathname, "rap", 3)) {
29  return NULL;
30  }
31  bool is_ssl = (pathname[3] == 's');
32  const char *port = &pathname[7 + is_ssl];
33  return rz_socket_rap_server_new(is_ssl, port);
34 }
35 
37  if (s) {
39  free(s);
40  }
41 }
42 
43 RZ_API bool rz_socket_rap_server_listen(RzSocketRapServer *s, const char *certfile) {
44  rz_return_val_if_fail(s && s->port && *s->port, false);
45  return rz_socket_listen(s->fd, s->port, certfile);
46 }
47 
50  return rz_socket_accept(s->fd);
51 }
52 
54  rz_return_val_if_fail(s && s->fd, false);
55 
56  int i;
57  char *ptr = NULL;
58 
59  if (!rz_socket_is_connected(s->fd)) {
60  return false;
61  }
62  rz_socket_read_block(s->fd, s->buf, 1);
63  switch (s->buf[0]) {
64  case RAP_PACKET_OPEN:
65  rz_socket_read_block(s->fd, &s->buf[1], 2);
66  rz_socket_read_block(s->fd, &s->buf[3], (int)s->buf[2]);
67  {
68  int fd = s->open(s->user, (const char *)&s->buf[3], (int)s->buf[1], 0);
70  eprintf("REPLY BACK %d\n", fd);
71  rz_write_be32(s->buf + 1, fd);
72  }
73  rz_socket_write(s->fd, s->buf, 5);
75  break;
76  case RAP_PACKET_READ:
77  rz_socket_read_block(s->fd, &s->buf[1], 4);
78  i = rz_read_be32(&s->buf[1]);
79  if (i > RAP_PACKET_MAX || i < 0) {
80  i = RAP_PACKET_MAX;
81  }
82  s->read(s->user, &s->buf[5], i);
84  rz_socket_write(s->fd, s->buf, i + 5);
86  break;
87  case RAP_PACKET_WRITE:
88  rz_socket_read_block(s->fd, s->buf + 1, 4);
89  i = rz_read_be32(s->buf + 1);
90  if (i > RAP_PACKET_MAX || i < 0) {
91  i = RAP_PACKET_MAX;
92  }
93  rz_socket_read_block(s->fd, s->buf + 5, i);
94  rz_write_be32(s->buf + 1, s->write(s->user, s->buf + 5, i));
96  rz_socket_write(s->fd, s->buf, 5);
98  break;
99  case RAP_PACKET_SEEK: {
100  rz_socket_read_block(s->fd, &s->buf[1], 9);
101  int whence = s->buf[0];
102  ut64 offset = rz_read_be64(s->buf + 1);
103  offset = s->seek(s->user, offset, whence);
104  /* prepare reply */
105  s->buf[0] = RAP_PACKET_SEEK | RAP_PACKET_REPLY;
106  rz_write_be64(s->buf + 1, offset);
107  rz_socket_write(s->fd, s->buf, 9);
108  rz_socket_flush(s->fd);
109  } break;
110  case RAP_PACKET_CMD:
111  rz_socket_read_block(s->fd, &s->buf[1], 4);
112  i = rz_read_be32(&s->buf[1]);
113  if (rz_socket_read_block(s->fd, &s->buf[5], i) > 0) {
114  ptr = s->cmd(s->user, (const char *)s->buf + 5);
115  i = (ptr) ? strlen(ptr) + 1 : 0;
116  rz_write_be32(&s->buf[1], i);
117  s->buf[0] = RAP_PACKET_CMD | RAP_PACKET_REPLY;
118  rz_socket_write(s->fd, s->buf, 5);
119  if (i) {
120  rz_socket_write(s->fd, ptr, i);
121  }
122  rz_socket_flush(s->fd);
123  RZ_FREE(ptr);
124  }
125  break;
126  case RAP_PACKET_CLOSE:
127  rz_socket_read_block(s->fd, &s->buf[1], 4);
128  i = rz_read_be32(&s->buf[1]);
129  s->close(s->user, i);
130  s->buf[0] = RAP_PACKET_CLOSE | RAP_PACKET_REPLY;
131  rz_socket_write(s->fd, s->buf, 5);
132  rz_socket_flush(s->fd);
133  break;
134  default:
135  eprintf("unknown command 0x%02x\n", (ut8)(s->buf[0] & 0xff));
136  rz_socket_close(s->fd);
137  return false;
138  }
139  return true;
140 }
lzma_index ** i
Definition: index.h:629
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf uLong offset
Definition: ioapi.h:144
uint8_t ut8
Definition: lh5801.h:11
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
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")
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
static ut64 rz_read_be64(const void *src)
Definition: rz_endian.h:108
static void rz_write_be64(void *dest, ut64 val)
Definition: rz_endian.h:119
static ut32 rz_read_be32(const void *src)
Definition: rz_endian.h:87
static void rz_write_be32(void *dest, ut32 val)
Definition: rz_endian.h:98
RZ_API int rz_socket_close(RzSocket *s)
Definition: socket.c:419
RZ_API int rz_socket_read_block(RzSocket *s, unsigned char *buf, int len)
Definition: socket.c:808
RZ_API bool rz_socket_is_connected(RzSocket *)
Definition: socket.c:101
RZ_API RzSocket * rz_socket_accept(RzSocket *s)
Definition: socket.c:582
RZ_API bool rz_socket_listen(RzSocket *s, const char *port, const char *certfile)
Definition: socket.c:474
RZ_API int rz_socket_flush(RzSocket *s)
Definition: socket.c:677
RZ_API RzSocket * rz_socket_new(bool is_ssl)
Definition: socket.c:179
@ 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_socket_free(RzSocket *s)
Definition: socket.c:453
RZ_API int rz_socket_write(RzSocket *s, void *buf, int len)
Definition: socket.c:724
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
RZ_API RzSocket * rz_socket_rap_server_accept(RzSocketRapServer *s)
RZ_API bool rz_socket_rap_server_continue(RzSocketRapServer *s)
RZ_API void rz_socket_rap_server_free(RzSocketRapServer *s)
RZ_API RzSocketRapServer * rz_socket_rap_server_new(bool use_ssl, const char *port)
RZ_API RzSocketRapServer * rz_socket_rap_server_create(const char *pathname)
RZ_API bool rz_socket_rap_server_listen(RzSocketRapServer *s, const char *certfile)
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997