Rizin
unix-like reverse engineering framework and cli tools
socket_rap_client.c File Reference
#include <rz_socket.h>
#include <rz_util.h>

Go to the source code of this file.

Functions

static ut8rz_rap_packet (ut8 type, ut32 len)
 
static void rz_rap_packet_fill (ut8 *buf, const ut8 *src, int len)
 
RZ_API int rz_socket_rap_client_open (RzSocket *s, const char *file, int rw)
 
RZ_API char * rz_socket_rap_client_command (RzSocket *s, const char *cmd, RzCoreBind *c)
 
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)
 
RZ_API int rz_socket_rap_client_seek (RzSocket *s, ut64 offset, int whence)
 

Function Documentation

◆ rz_rap_packet()

static ut8* rz_rap_packet ( ut8  type,
ut32  len 
)
static

Definition at line 7 of file socket_rap_client.c.

7  {
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 }
size_t len
Definition: 6502dis.c:15
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * malloc(size_t size)
Definition: malloc.c:123
int type
Definition: mipsasm.c:17
static void rz_write_be32(void *dest, ut32 val)
Definition: rz_endian.h:98

References len, malloc(), rz_write_be32(), and type.

Referenced by rz_socket_rap_client_command().

◆ rz_rap_packet_fill()

static void rz_rap_packet_fill ( ut8 buf,
const ut8 src,
int  len 
)
static

Definition at line 16 of file socket_rap_client.c.

16  {
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 }
lzma_index * src
Definition: index.h:567
uint32_t ut32
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static ut32 rz_read_be32(const void *src)
Definition: rz_endian.h:87
#define RZ_MIN(x, y)

References len, memcpy(), RZ_MIN, rz_read_be32(), and src.

Referenced by rz_socket_rap_client_command().

◆ rz_socket_rap_client_command()

RZ_API char* rz_socket_rap_client_command ( RzSocket s,
const char *  cmd,
RzCoreBind c 
)

Definition at line 58 of file socket_rap_client.c.

58  {
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 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
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
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
static ut32 rz_read_at_be32(const void *src, size_t offset)
Definition: rz_endian.h:93
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
@ RAP_PACKET_CMD
Definition: rz_socket.h:177
@ RAP_PACKET_REPLY
Definition: rz_socket.h:178
RZ_API int rz_socket_write(RzSocket *s, void *buf, int len)
Definition: socket.c:724
#define c(i)
Definition: sha256.c:43
static ut8 * rz_rap_packet(ut8 type, ut32 len)
static void rz_rap_packet_fill(ut8 *buf, const ut8 *src, int len)

References c, calloc(), cmd, eprintf, free(), i, malloc(), memcpy(), NULL, RAP_PACKET_CMD, RAP_PACKET_REPLY, rz_rap_packet(), rz_rap_packet_fill(), rz_read_at_be32(), rz_socket_flush(), rz_socket_read_block(), rz_socket_write(), rz_write_be32(), and s.

Referenced by __rap_system(), and rz_core_rtr_cmd().

◆ rz_socket_rap_client_open()

RZ_API int rz_socket_rap_client_open ( RzSocket s,
const char *  file,
int  rw 
)

Definition at line 23 of file socket_rap_client.c.

23  {
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 }
#define r
Definition: crypto_rc6.c:12
#define ut8
Definition: dcpu16.h:8
return memset(p, 0, total)
RZ_API bool rz_socket_block_time(RzSocket *s, bool block, int sec, int usec)
Definition: socket.c:649
@ RAP_PACKET_OPEN
Definition: rz_socket.h:171
Definition: gzappend.c:170
static const z80_opcode fd[]
Definition: z80_tab.h:997

References eprintf, fd, free(), malloc(), memcpy(), memset(), r, RAP_PACKET_OPEN, RAP_PACKET_REPLY, rz_read_at_be32(), rz_socket_block_time(), rz_socket_flush(), rz_socket_read_block(), rz_socket_write(), s, and ut8.

Referenced by __rap_open(), and rz_core_rtr_add().

◆ rz_socket_rap_client_read()

RZ_API int rz_socket_rap_client_read ( RzSocket s,
ut8 buf,
int  count 
)

Definition at line 149 of file socket_rap_client.c.

149  {
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 }
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
@ RAP_PACKET_MAX
Definition: rz_socket.h:179
@ RAP_PACKET_READ
Definition: rz_socket.h:172

References count, eprintf, i, RAP_PACKET_MAX, RAP_PACKET_READ, RAP_PACKET_REPLY, rz_read_at_be32(), rz_socket_block_time(), rz_socket_flush(), rz_socket_read_block(), rz_socket_write(), rz_write_be32(), s, and autogen_x86imm::tmp.

Referenced by __rap_read().

◆ rz_socket_rap_client_seek()

RZ_API int rz_socket_rap_client_seek ( RzSocket s,
ut64  offset,
int  whence 
)

Definition at line 181 of file socket_rap_client.c.

181  {
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 }
voidpf uLong offset
Definition: ioapi.h:144
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
@ RAP_PACKET_SEEK
Definition: rz_socket.h:174

References eprintf, RAP_PACKET_REPLY, RAP_PACKET_SEEK, rz_read_at_be64(), rz_socket_flush(), rz_socket_read_block(), rz_socket_write(), rz_write_be64(), s, autogen_x86imm::tmp, and ut8.

Referenced by __rap_lseek().

◆ rz_socket_rap_client_write()

RZ_API int rz_socket_rap_client_write ( RzSocket s,
const ut8 buf,
int  count 
)

Definition at line 116 of file socket_rap_client.c.

116  {
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 }
@ RAP_PACKET_WRITE
Definition: rz_socket.h:173

References count, eprintf, free(), malloc(), memcpy(), RAP_PACKET_MAX, RAP_PACKET_WRITE, rz_read_be32(), rz_socket_flush(), rz_socket_read_block(), rz_socket_write(), rz_write_be32(), s, and autogen_x86imm::tmp.

Referenced by __rap_write().