Rizin
unix-like reverse engineering framework and cli tools
io_tcp.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2016 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_io.h"
5 #include "rz_lib.h"
6 #include <rz_socket.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "../io_memory.h"
10 
11 static bool __check(RzIO *io, const char *pathname, bool many) {
12  return (!strncmp(pathname, "tcp://", 6));
13 }
14 
15 static ut8 *tcpme(const char *pathname, int *code, int *len) {
16  pathname += 6;
17  *code = 404;
18 #if __UNIX__
19  rz_sys_signal(SIGINT, SIG_IGN);
20 #endif
21  if (*pathname == ':') {
22  /* listen and wait for connection */
23  RzSocket *sl = rz_socket_new(false);
24  if (!rz_socket_listen(sl, pathname + 1, NULL)) {
25  eprintf("Cannot listen\n");
26  rz_socket_free(sl);
27  return NULL;
28  }
30  ut8 *res = rz_socket_slurp(sc, len);
32  rz_socket_free(sl);
33  if (res) {
34  *code = 200;
35  return res;
36  }
37  } else {
38  /* connect and slurp the end point */
39  char *host = strdup(pathname);
40  if (!host) {
41  return NULL;
42  }
43  char *port = strchr(host, ':');
44  if (port) {
45  *port++ = 0;
46  RzSocket *s = rz_socket_new(false);
47  if (rz_socket_connect(s, host, port, RZ_SOCKET_PROTO_TCP, 0)) {
48  ut8 *res = rz_socket_slurp(s, len);
49  if (*len < 1) {
50  RZ_FREE(res);
51  } else {
52  *code = 200;
53  }
55  free(host);
56  return res;
57  }
59  } else {
60  eprintf("Missing port.\n");
61  }
62  free(host);
63  }
64  return NULL;
65 }
66 
67 static RzIODesc *__open(RzIO *io, const char *pathname, int rw, int mode) {
68  if (__check(io, pathname, 0)) {
69  int rlen, code;
71  if (!mal) {
72  return NULL;
73  }
74  mal->offset = 0;
75  mal->buf = tcpme(pathname, &code, &rlen);
76  if (mal->buf && rlen > 0) {
77  mal->size = rlen;
78  return rz_io_desc_new(io, &rz_io_plugin_tcp, pathname, rw, mode, mal);
79  }
80  eprintf("No TCP segment\n");
81  free(mal);
82  }
83  return NULL;
84 }
85 
87  .name = "tcp",
88  .desc = "Load files via TCP (listen or connect)",
89  .uris = "tcp://",
90  .license = "LGPL3",
91  .open = __open,
92  .close = io_memory_close,
93  .read = io_memory_read,
94  .check = __check,
95  .lseek = io_memory_lseek,
96  .write = io_memory_write,
97 };
98 
99 #ifndef RZ_PLUGIN_INCORE
101  .type = RZ_LIB_TYPE_IO,
102  .data = &rz_io_plugin_tcp,
104 };
105 #endif
size_t len
Definition: 6502dis.c:15
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static char sc[]
Definition: egg_cb.c:6
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
int io_memory_read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_memory.c:97
int io_memory_write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_memory.c:56
ut64 io_memory_lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence)
Definition: io_memory.c:125
int io_memory_close(RzIODesc *fd)
Definition: io_memory.c:114
static bool __check(RzIO *io, const char *pathname, bool many)
Definition: io_tcp.c:11
RzIOPlugin rz_io_plugin_tcp
Definition: io_tcp.c:86
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_tcp.c:67
RZ_API RzLibStruct rizin_plugin
Definition: io_tcp.c:100
static ut8 * tcpme(const char *pathname, int *code, int *len)
Definition: io_tcp.c:15
const char int mode
Definition: ioapi.h:137
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")
const char * code
Definition: pal.c:98
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
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_LIB_TYPE_IO
Definition: rz_lib.h:69
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 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 ut8 * rz_socket_slurp(RzSocket *s, int *len)
Definition: socket.c:868
RZ_API int rz_socket_free(RzSocket *s)
Definition: socket.c:453
RZ_API int rz_sys_signal(int sig, void(*handler)(int))
Definition: sys.c:178
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_VERSION
Definition: rz_version.h:8
Definition: inftree9.h:24
const char * name
Definition: rz_io.h:115
const char * version
Definition: rz_io.h:117
Definition: rz_io.h:59
static struct @626 mal