Rizin
unix-like reverse engineering framework and cli tools
socket_rap_client.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_socket.h>
5 #include <rz_util.h>
6 
8  ut8 *buf = malloc(len + 5);
9  if (buf) {
10  buf[0] = type;
11  rz_write_be32(buf + 1, len);
12  }
13  return buf;
14 }
15 
16 static void rz_rap_packet_fill(ut8 *buf, const ut8 *src, int len) {
17  if (buf && src && len > 0) {
18  ut32 curlen = rz_read_be32(buf + 1);
19  memcpy(buf + 5, src, RZ_MIN(curlen, len));
20  }
21 }
22 
23 RZ_API int rz_socket_rap_client_open(RzSocket *s, const char *file, int rw) {
24  rz_socket_block_time(s, true, 1, 0);
25  size_t file_len0 = strlen(file) + 1;
26  if (file_len0 > 255) {
27  eprintf("Filename too long\n");
28  return -1;
29  }
30  char *buf = malloc(file_len0 + 7);
31  if (!buf) {
32  return -1;
33  }
34  // >>
35  buf[0] = RAP_PACKET_OPEN;
36  buf[1] = rw;
37  buf[2] = (ut8)(file_len0 & 0xff);
38  memcpy(buf + 3, file, file_len0);
39  (void)rz_socket_write(s, buf, 3 + file_len0);
41  // <<
42  int fd = -1;
43  memset(buf, 0, 5);
44  int r = rz_socket_read_block(s, (ut8 *)buf, 5);
45  if (r == 5) {
46  if (buf[0] == (char)(RAP_PACKET_OPEN | RAP_PACKET_REPLY)) {
47  fd = rz_read_at_be32(buf + 1, 1);
48  } else {
49  eprintf("RapClientOpen: Bad packet 0x%02x\n", buf[0]);
50  }
51  } else {
52  eprintf("Cannot read 5 bytes from server\n");
53  }
54  free(buf);
55  return fd;
56 }
57 
59  char *buf = malloc(strlen(cmd) + 8);
60  if (!buf) {
61  return NULL;
62  }
63  /* send request */
64  buf[0] = RAP_PACKET_CMD;
65  size_t i = strlen(cmd) + 1;
66  rz_write_be32(buf + 1, i);
67  memcpy(buf + 5, cmd, i);
68  rz_socket_write(s, buf, 5 + i);
70  free(buf);
71  /* read response */
72  char bufr[8];
73  rz_socket_read_block(s, (ut8 *)bufr, 5);
74  while (bufr[0] == (char)(RAP_PACKET_CMD)) {
75  size_t cmd_len = rz_read_at_be32(bufr, 1);
76  char *rcmd = calloc(1, cmd_len + 1);
77  if (rcmd) {
78  rz_socket_read_block(s, (ut8 *)rcmd, cmd_len);
79  // char *res = rz_core_cmd_str (core, rcmd);
80  char *res = c->cmdstr(c->core, rcmd);
81  if (res) {
82  int res_len = strlen(res) + 1;
83  ut8 *pkt = rz_rap_packet((RAP_PACKET_CMD | RAP_PACKET_REPLY), res_len);
84  rz_rap_packet_fill(pkt, (const ut8 *)res, res_len);
85  rz_socket_write(s, pkt, 5 + res_len);
87  free(res);
88  free(pkt);
89  }
90  free(rcmd);
91  }
92  /* read response */
93  bufr[0] = -1;
94  (void)rz_socket_read_block(s, (ut8 *)bufr, 5);
95  }
96  if (bufr[0] != (char)(RAP_PACKET_CMD | RAP_PACKET_REPLY)) {
97  eprintf("Error: Wrong reply for command 0x%02x\n", bufr[0]);
98  return NULL;
99  }
100  size_t cmd_len = rz_read_at_be32(bufr, 1);
101  if (cmd_len < 1 || cmd_len > 16384) {
102  eprintf("Error: cmd_len is wrong\n");
103  return NULL;
104  }
105  char *cmd_output = calloc(1, cmd_len + 1);
106  if (!cmd_output) {
107  eprintf("Error: Allocating cmd output\n");
108  return NULL;
109  }
110  rz_socket_read_block(s, (ut8 *)cmd_output, cmd_len);
111  // ensure the termination
112  cmd_output[cmd_len] = 0;
113  return cmd_output;
114 }
115 
117  ut8 *tmp;
118  int ret;
119  if (count < 1) {
120  return count;
121  }
122  // TOOD: if count > RAP_PACKET_MAX iterate !
123  if (count > RAP_PACKET_MAX) {
125  }
126  if (!(tmp = (ut8 *)malloc(count + 5))) {
127  eprintf("__rap_write: malloc failed\n");
128  return -1;
129  }
130  tmp[0] = RAP_PACKET_WRITE;
131  rz_write_be32(tmp + 1, count);
132  memcpy(tmp + 5, buf, count);
133 
134  (void)rz_socket_write(s, tmp, count + 5);
136  if (rz_socket_read_block(s, tmp, 5) != 5) { // TODO read_block?
137  eprintf("__rap_write: error\n");
138  ret = -1;
139  } else {
140  ret = rz_read_be32(tmp + 1);
141  if (!ret) {
142  ret = -1;
143  }
144  }
145  free(tmp);
146  return ret;
147 }
148 
150  ut8 tmp[32];
151  if (count < 1) {
152  return count;
153  }
154  rz_socket_block_time(s, 1, 1, 0);
155  // XXX. if count is > RAP_PACKET_MAX, just perform multiple queries
156  if (count > RAP_PACKET_MAX) {
158  }
159  // send
160  tmp[0] = RAP_PACKET_READ;
161  rz_write_be32(tmp + 1, count);
162  (void)rz_socket_write(s, tmp, 5);
164  // recv
165  int ret = rz_socket_read_block(s, tmp, 5);
166  if (ret != 5 || tmp[0] != (RAP_PACKET_READ | RAP_PACKET_REPLY)) {
167  eprintf("__rap_read: Unexpected rap read reply "
168  "(%d=0x%02x) expected (%d=0x%02x)\n",
169  ret, tmp[0], 2, (RAP_PACKET_READ | RAP_PACKET_REPLY));
170  return -1;
171  }
172  int i = rz_read_at_be32(tmp, 1);
173  if (i > count) {
174  eprintf("__rap_read: Unexpected data size %d vs %d\n", i, count);
175  return -1;
176  }
178  return count;
179 }
180 
182  ut8 tmp[10];
183  tmp[0] = RAP_PACKET_SEEK;
184  tmp[1] = (ut8)whence;
185  rz_write_be64(tmp + 2, offset);
186  (void)rz_socket_write(s, &tmp, 10);
188  int ret = rz_socket_read_block(s, (ut8 *)&tmp, 9);
189  if (ret != 9) {
190  eprintf("Truncated socket read\n");
191  return -1;
192  }
193  if (tmp[0] != (RAP_PACKET_SEEK | RAP_PACKET_REPLY)) {
194  // eprintf ("%d %d - %02x %02x %02x %02x %02x %02x %02x\n",
195  // ret, whence, tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6]);
196  eprintf("Unexpected seek reply (%02x -> %02x)\n", tmp[0], (RAP_PACKET_SEEK | RAP_PACKET_REPLY));
197  return -1;
198  }
199  return rz_read_at_be64(tmp, 1);
200 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
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
#define ut8
Definition: dcpu16.h:8
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
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 * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
int type
Definition: mipsasm.c:17
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
static void rz_write_be64(void *dest, ut64 val)
Definition: rz_endian.h:119
static ut64 rz_read_at_be64(const void *src, size_t offset)
Definition: rz_endian.h:114
static ut32 rz_read_at_be32(const void *src, size_t offset)
Definition: rz_endian.h:93
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_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 bool rz_socket_block_time(RzSocket *s, bool block, int sec, int usec)
Definition: socket.c:649
@ 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_WRITE
Definition: rz_socket.h:173
RZ_API int rz_socket_write(RzSocket *s, void *buf, int len)
Definition: socket.c:724
#define RZ_MIN(x, y)
#define c(i)
Definition: sha256.c:43
RZ_API char * rz_socket_rap_client_command(RzSocket *s, const char *cmd, RzCoreBind *c)
RZ_API int rz_socket_rap_client_open(RzSocket *s, const char *file, int rw)
static ut8 * rz_rap_packet(ut8 type, ut32 len)
RZ_API int rz_socket_rap_client_write(RzSocket *s, const ut8 *buf, int count)
RZ_API int rz_socket_rap_client_read(RzSocket *s, ut8 *buf, int count)
static void rz_rap_packet_fill(ut8 *buf, const ut8 *src, int len)
RZ_API int rz_socket_rap_client_seek(RzSocket *s, ut64 offset, int whence)
Definition: gzappend.c:170
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997