Rizin
unix-like reverse engineering framework and cli tools
rz_socket.h File Reference
#include "rz_types.h"
#include "rz_bind.h"
#include "rz_list.h"

Go to the source code of this file.

Classes

struct  RzPipe
 
struct  rz_socket_t
 
struct  rz_socket_http_options
 
struct  rz_socket_proc_t
 
struct  rz_socket_http_request
 
struct  rz_socket_rap_server_t
 
struct  rz_run_profile_t
 

Macros

#define MSG_DONTWAIT   0
 
#define SD_RECEIVE   0
 
#define SD_SEND   1
 
#define SD_BOTH   2
 
#define RZ_INVALID_SOCKET   -1
 
#define RZ_SOCKET_PROTO_TCP   IPPROTO_TCP
 
#define RZ_SOCKET_PROTO_UDP   IPPROTO_UDP
 
#define RZ_SOCKET_PROTO_UNIX   0x1337
 
#define RZ_SOCKET_PROTO_NONE   0
 
#define RZ_SOCKET_PROTO_DEFAULT   RZ_SOCKET_PROTO_TCP
 
#define rz_socket_connect_tcp(a, b, c, d)   rz_socket_connect(a, b, c, RZ_SOCKET_PROTO_TCP, d)
 
#define rz_socket_connect_udp(a, b, c, d)   rz_socket_connect(a, b, c, RZ_SOCKET_PROTO_UDP, d)
 
#define rz_socket_connect_unix(a, b)   (false)
 
#define RZ_RUN_PROFILE_NARGS   512
 

Typedefs

typedef struct rz_socket_t RzSocket
 
typedef struct rz_socket_http_options RzSocketHTTPOptions
 
typedef struct rz_socket_proc_t RzSocketProc
 
typedef struct rz_socket_http_request RzSocketHTTPRequest
 
typedef int(* rap_server_open) (void *user, const char *file, int flg, int mode)
 
typedef int(* rap_server_seek) (void *user, ut64 offset, int whence)
 
typedef int(* rap_server_read) (void *user, ut8 *buf, int len)
 
typedef int(* rap_server_write) (void *user, ut8 *buf, int len)
 
typedef char *(* rap_server_cmd) (void *user, const char *command)
 
typedef int(* rap_server_close) (void *user, int fd)
 
typedef struct rz_socket_rap_server_t RzSocketRapServer
 
typedef struct rz_run_profile_t RzRunProfile
 

Enumerations

enum  {
  RAP_PACKET_OPEN = 1 , RAP_PACKET_READ = 2 , RAP_PACKET_WRITE = 3 , RAP_PACKET_SEEK = 4 ,
  RAP_PACKET_CLOSE = 5 , RAP_PACKET_CMD = 7 , RAP_PACKET_REPLY = 0x80 , RAP_PACKET_MAX = 4096
}
 

Functions

 RZ_LIB_VERSION_HEADER (rz_socket)
 
RZ_API RzSocketrz_socket_new_from_fd (int fd)
 
RZ_API RzSocketrz_socket_new (bool is_ssl)
 
RZ_API bool rz_socket_spawn (RzSocket *s, const char *cmd, unsigned int timeout)
 
RZ_API bool rz_socket_connect (RzSocket *s, const char *host, const char *port, int proto, unsigned int timeout)
 
RZ_API int rz_socket_connect_serial (RzSocket *sock, const char *path, int speed, int parity)
 
RZ_API bool rz_socket_listen (RzSocket *s, const char *port, const char *certfile)
 
RZ_API int rz_socket_port_by_name (const char *name)
 
RZ_API int rz_socket_close_fd (RzSocket *s)
 
RZ_API int rz_socket_close (RzSocket *s)
 
RZ_API int rz_socket_free (RzSocket *s)
 
RZ_API RzSocketrz_socket_accept (RzSocket *s)
 
RZ_API RzSocketrz_socket_accept_timeout (RzSocket *s, unsigned int timeout)
 
RZ_API bool rz_socket_block_time (RzSocket *s, bool block, int sec, int usec)
 
RZ_API int rz_socket_flush (RzSocket *s)
 
RZ_API int rz_socket_ready (RzSocket *s, int secs, int usecs)
 
RZ_API char * rz_socket_to_string (RzSocket *s)
 
RZ_API int rz_socket_write (RzSocket *s, void *buf, int len)
 
RZ_API int rz_socket_puts (RzSocket *s, char *buf)
 
RZ_API void rz_socket_printf (RzSocket *s, const char *fmt,...) RZ_PRINTF_CHECK(2
 
RZ_API void RZ_API int rz_socket_read (RzSocket *s, ut8 *read, int len)
 
RZ_API int rz_socket_read_block (RzSocket *s, unsigned char *buf, int len)
 
RZ_API int rz_socket_gets (RzSocket *s, char *buf, int size)
 
RZ_API ut8rz_socket_slurp (RzSocket *s, int *len)
 
RZ_API bool rz_socket_is_connected (RzSocket *)
 
RZ_API RzSocketProcrz_socket_proc_open (char *const argv[])
 
RZ_API int rz_socket_proc_close (RzSocketProc *sp)
 
RZ_API int rz_socket_proc_read (RzSocketProc *sp, unsigned char *buf, int len)
 
RZ_API int rz_socket_proc_gets (RzSocketProc *sp, char *buf, int size)
 
RZ_API int rz_socket_proc_write (RzSocketProc *sp, void *buf, int len)
 
RZ_API void rz_socket_proc_printf (RzSocketProc *sp, const char *fmt,...) RZ_PRINTF_CHECK(2
 
RZ_API void RZ_API int rz_socket_proc_ready (RzSocketProc *sp, int secs, int usecs)
 
RZ_API char * rz_socket_http_get (const char *url, int *code, int *rlen)
 
RZ_API char * rz_socket_http_post (const char *url, const char *data, int *code, int *rlen)
 
RZ_API void rz_socket_http_server_set_breaked (bool *b)
 
RZ_API RzSocketHTTPRequestrz_socket_http_accept (RzSocket *s, RzSocketHTTPOptions *so)
 
RZ_API void rz_socket_http_response (RzSocketHTTPRequest *rs, int code, const char *out, int x, const char *headers)
 
RZ_API void rz_socket_http_close (RzSocketHTTPRequest *rs)
 
RZ_API ut8rz_socket_http_handle_upload (const ut8 *str, int len, int *olen)
 
RZ_API RzSocketRapServerrz_socket_rap_server_new (bool is_ssl, const char *port)
 
RZ_API RzSocketRapServerrz_socket_rap_server_create (const char *pathname)
 
RZ_API void rz_socket_rap_server_free (RzSocketRapServer *rap_s)
 
RZ_API bool rz_socket_rap_server_listen (RzSocketRapServer *rap_s, const char *certfile)
 
RZ_API RzSocketrz_socket_rap_server_accept (RzSocketRapServer *rap_s)
 
RZ_API bool rz_socket_rap_server_continue (RzSocketRapServer *rap_s)
 
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)
 
RZ_API RzRunProfilerz_run_new (const char *str)
 
RZ_API bool rz_run_parse (RzRunProfile *pf, const char *profile)
 
RZ_API void rz_run_free (RzRunProfile *r)
 
RZ_API bool rz_run_parseline (RzRunProfile *p, const char *b)
 
RZ_API const char * rz_run_help (void)
 
RZ_API int rz_run_config_env (RzRunProfile *p)
 
RZ_API int rz_run_start (RzRunProfile *p)
 
RZ_API void rz_run_reset (RzRunProfile *p)
 
RZ_API bool rz_run_parsefile (RzRunProfile *p, const char *b)
 
RZ_API char * rz_run_get_environ_profile (char **env)
 
RZ_API int rzpipe_write (RzPipe *rzpipe, const char *str)
 
RZ_API char * rzpipe_read (RzPipe *rzpipe)
 
RZ_API int rzpipe_close (RzPipe *rzpipe)
 
RZ_API RzPiperzpipe_open_corebind (RzCoreBind *coreb)
 
RZ_API RzPiperzpipe_open (const char *cmd)
 
RZ_API RzPiperzpipe_open_dl (const char *file)
 
RZ_API char * rzpipe_cmd (RzPipe *rzpipe, const char *str)
 
RZ_API char * rzpipe_cmdf (RzPipe *rzpipe, const char *fmt,...) RZ_PRINTF_CHECK(2
 

Macro Definition Documentation

◆ MSG_DONTWAIT

#define MSG_DONTWAIT   0

Definition at line 37 of file rz_socket.h.

◆ RZ_INVALID_SOCKET

#define RZ_INVALID_SOCKET   -1

Definition at line 48 of file rz_socket.h.

◆ RZ_RUN_PROFILE_NARGS

#define RZ_RUN_PROFILE_NARGS   512

Definition at line 211 of file rz_socket.h.

◆ rz_socket_connect_tcp

#define rz_socket_connect_tcp (   a,
  b,
  c,
  d 
)    rz_socket_connect(a, b, c, RZ_SOCKET_PROTO_TCP, d)

Definition at line 99 of file rz_socket.h.

◆ rz_socket_connect_udp

#define rz_socket_connect_udp (   a,
  b,
  c,
  d 
)    rz_socket_connect(a, b, c, RZ_SOCKET_PROTO_UDP, d)

Definition at line 100 of file rz_socket.h.

◆ rz_socket_connect_unix

#define rz_socket_connect_unix (   a,
  b 
)    (false)

Definition at line 104 of file rz_socket.h.

◆ RZ_SOCKET_PROTO_DEFAULT

#define RZ_SOCKET_PROTO_DEFAULT   RZ_SOCKET_PROTO_TCP

Definition at line 91 of file rz_socket.h.

◆ RZ_SOCKET_PROTO_NONE

#define RZ_SOCKET_PROTO_NONE   0

Definition at line 90 of file rz_socket.h.

◆ RZ_SOCKET_PROTO_TCP

#define RZ_SOCKET_PROTO_TCP   IPPROTO_TCP

Definition at line 87 of file rz_socket.h.

◆ RZ_SOCKET_PROTO_UDP

#define RZ_SOCKET_PROTO_UDP   IPPROTO_UDP

Definition at line 88 of file rz_socket.h.

◆ RZ_SOCKET_PROTO_UNIX

#define RZ_SOCKET_PROTO_UNIX   0x1337

Definition at line 89 of file rz_socket.h.

◆ SD_BOTH

#define SD_BOTH   2

Definition at line 42 of file rz_socket.h.

◆ SD_RECEIVE

#define SD_RECEIVE   0

Definition at line 40 of file rz_socket.h.

◆ SD_SEND

#define SD_SEND   1

Definition at line 41 of file rz_socket.h.

Typedef Documentation

◆ rap_server_close

typedef int(* rap_server_close) (void *user, int fd)

Definition at line 168 of file rz_socket.h.

◆ rap_server_cmd

typedef char*(* rap_server_cmd) (void *user, const char *command)

Definition at line 167 of file rz_socket.h.

◆ rap_server_open

typedef int(* rap_server_open) (void *user, const char *file, int flg, int mode)

Definition at line 163 of file rz_socket.h.

◆ rap_server_read

typedef int(* rap_server_read) (void *user, ut8 *buf, int len)

Definition at line 165 of file rz_socket.h.

◆ rap_server_seek

typedef int(* rap_server_seek) (void *user, ut64 offset, int whence)

Definition at line 164 of file rz_socket.h.

◆ rap_server_write

typedef int(* rap_server_write) (void *user, ut8 *buf, int len)

Definition at line 166 of file rz_socket.h.

◆ RzRunProfile

◆ RzSocket

typedef struct rz_socket_t RzSocket

◆ RzSocketHTTPOptions

◆ RzSocketHTTPRequest

◆ RzSocketProc

◆ RzSocketRapServer

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
RAP_PACKET_OPEN 
RAP_PACKET_READ 
RAP_PACKET_WRITE 
RAP_PACKET_SEEK 
RAP_PACKET_CLOSE 
RAP_PACKET_CMD 
RAP_PACKET_REPLY 
RAP_PACKET_MAX 

Definition at line 170 of file rz_socket.h.

170  {
171  RAP_PACKET_OPEN = 1,
172  RAP_PACKET_READ = 2,
173  RAP_PACKET_WRITE = 3,
174  RAP_PACKET_SEEK = 4,
175  RAP_PACKET_CLOSE = 5,
176  // system was deprecated in slot 6,
177  RAP_PACKET_CMD = 7,
178  RAP_PACKET_REPLY = 0x80,
179  RAP_PACKET_MAX = 4096
180 };
@ 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

Function Documentation

◆ RZ_LIB_VERSION_HEADER()

RZ_LIB_VERSION_HEADER ( rz_socket  )

◆ rz_run_config_env()

RZ_API int rz_run_config_env ( RzRunProfile p)

Definition at line 809 of file run.c.

809  {
810  int ret;
811 
812 #if HAVE_OPENPTY && HAVE_FORKPTY && HAVE_LOGIN_TTY
813  dyn_init();
814 #endif
815 
816  if (!p->_program && !p->_system && !p->_runlib) {
817  eprintf("No program, system or runlib rule defined\n");
818  return 1;
819  }
820  // when IO is redirected to a process, handle them together
821  if (handle_redirection(p->_stdio, true, true, false) != 0) {
822  return 1;
823  }
824  if (handle_redirection(p->_stdin, true, false, false) != 0) {
825  return 1;
826  }
827  if (handle_redirection(p->_stdout, false, true, false) != 0) {
828  return 1;
829  }
830  if (handle_redirection(p->_stderr, false, false, true) != 0) {
831  return 1;
832  }
833  if (p->_aslr != -1) {
834  setASLR(p, p->_aslr);
835  }
836 #if __UNIX__
837  set_limit(p->_docore, RLIMIT_CORE, RLIM_INFINITY);
838  if (p->_maxfd) {
839  set_limit(p->_maxfd, RLIMIT_NOFILE, p->_maxfd);
840  }
841 #ifdef RLIMIT_NPROC
842  if (p->_maxproc) {
843  set_limit(p->_maxproc, RLIMIT_NPROC, p->_maxproc);
844  }
845 #endif
846  if (p->_maxstack) {
847  set_limit(p->_maxstack, RLIMIT_STACK, p->_maxstack);
848  }
849 #else
850  if (p->_docore || p->_maxfd || p->_maxproc || p->_maxstack)
851  eprintf("Warning: setrlimits not supported for this platform\n");
852 #endif
853  if (p->_connect) {
854  char *q = strchr(p->_connect, ':');
855  if (q) {
856  RzSocket *fd = rz_socket_new(0);
857  *q = 0;
858  if (!rz_socket_connect_tcp(fd, p->_connect, q + 1, 30)) {
859  eprintf("Cannot connect\n");
860  return 1;
861  }
862  if (p->_pty) {
863  if (redirect_socket_to_pty(fd) != 0) {
864  eprintf("socket redirection failed\n");
866  return 1;
867  }
868  } else {
870  }
871  } else {
872  eprintf("Invalid format for connect. missing ':'\n");
873  return 1;
874  }
875  }
876  if (p->_listen) {
877  RzSocket *child, *fd = rz_socket_new(0);
878  bool is_child = false;
879  if (!rz_socket_listen(fd, p->_listen, NULL)) {
880  eprintf("rz-run: cannot listen\n");
882  return 1;
883  }
884  while (true) {
885  child = rz_socket_accept(fd);
886  if (child) {
887  is_child = true;
888 
889  if (p->_dofork && !p->_dodebug) {
890  pid_t child_pid = rz_sys_fork();
891  if (child_pid == -1) {
892  eprintf("rz-run: cannot fork\n");
893  rz_socket_free(child);
895  return 1;
896  } else if (child_pid != 0) {
897  // parent code
898  is_child = false;
899  }
900  }
901 
902  if (is_child) {
904  eprintf("connected\n");
905  if (p->_pty) {
906  if (redirect_socket_to_pty(child) != 0) {
907  eprintf("socket redirection failed\n");
908  rz_socket_free(child);
910  return 1;
911  }
912  } else {
914  }
915  break;
916  } else {
917  rz_socket_close_fd(child);
918  }
919  }
920  }
921  if (!is_child) {
922  rz_socket_free(child);
923  }
925  }
926  if (p->_rzsleep != 0) {
927  rz_sys_sleep(p->_rzsleep);
928  }
929 #if __UNIX__
930  if (p->_chroot) {
931  if (chdir(p->_chroot) == -1) {
932  eprintf("Cannot chdir to chroot in %s\n", p->_chroot);
933  return 1;
934  } else {
935  if (chroot(".") == -1) {
936  eprintf("Cannot chroot to %s\n", p->_chroot);
937  return 1;
938  } else {
939  // Silenting pedantic meson flags...
940  if (chdir("/") == -1) {
941  eprintf("Cannot chdir to /\n");
942  return 1;
943  }
944  if (p->_chgdir) {
945  if (chdir(p->_chgdir) == -1) {
946  eprintf("Cannot chdir after chroot to %s\n", p->_chgdir);
947  return 1;
948  }
949  }
950  }
951  }
952  } else if (p->_chgdir) {
953  if (chdir(p->_chgdir) == -1) {
954  eprintf("Cannot chdir after chroot to %s\n", p->_chgdir);
955  return 1;
956  }
957  }
958 #else
959  if (p->_chgdir) {
960  ret = chdir(p->_chgdir);
961  if (ret < 0) {
962  return 1;
963  }
964  }
965  if (p->_chroot) {
966  ret = chdir(p->_chroot);
967  if (ret < 0) {
968  return 1;
969  }
970  }
971 #endif
972 #if __UNIX__
973  if (p->_setuid) {
974  ret = setgroups(0, NULL);
975  if (ret < 0) {
976  return 1;
977  }
978  ret = setuid(atoi(p->_setuid));
979  if (ret < 0) {
980  return 1;
981  }
982  }
983  if (p->_seteuid) {
984  ret = seteuid(atoi(p->_seteuid));
985  if (ret < 0) {
986  return 1;
987  }
988  }
989  if (p->_setgid) {
990  ret = setgid(atoi(p->_setgid));
991  if (ret < 0) {
992  return 1;
993  }
994  }
995  if (p->_input) {
996  char *inp;
997  int f2[2];
998  if (rz_sys_pipe(f2, true) != -1) {
999  close(0);
1000  dup2(f2[0], 0);
1001  } else {
1002  eprintf("[ERROR] rz-run: Cannot create pipe\n");
1003  return 1;
1004  }
1005  inp = getstr(p->_input);
1006  if (inp) {
1007  size_t inpl = strlen(inp);
1008  if (write(f2[1], inp, inpl) != inpl) {
1009  eprintf("[ERROR] rz-run: Cannot write to the pipe\n");
1010  }
1011  rz_sys_pipe_close(f2[1]);
1012  free(inp);
1013  } else {
1014  eprintf("Invalid input\n");
1015  }
1016  }
1017 #endif
1018  if (p->_rzpreload) {
1019  if (p->_preload) {
1020  eprintf("WARNING: Only one library can be opened at a time\n");
1021  }
1022  char *libdir = rz_path_libdir();
1023  p->_preload = rz_file_path_join(libdir, "librz." RZ_LIB_EXT);
1024  free(libdir);
1025  }
1026  if (p->_libpath) {
1027 #if __WINDOWS__
1028  eprintf("rz-run: libpath unsupported for this platform\n");
1029 #elif __HAIKU__
1030  char *orig = rz_sys_getenv("LIBRARY_PATH");
1031  char *newlib = rz_str_newf("%s:%s", p->_libpath, orig);
1032  rz_sys_setenv("LIBRARY_PATH", newlib);
1033  free(newlib);
1034  free(orig);
1035 #elif __APPLE__
1036  rz_sys_setenv("DYLD_LIBRARY_PATH", p->_libpath);
1037 #else
1038  rz_sys_setenv("LD_LIBRARY_PATH", p->_libpath);
1039 #endif
1040  }
1041  if (p->_preload) {
1042 #if __APPLE__
1043  // 10.6
1044 #ifndef __MAC_10_7
1045  rz_sys_setenv("DYLD_PRELOAD", p->_preload);
1046 #endif
1047  rz_sys_setenv("DYLD_INSERT_LIBRARIES", p->_preload);
1048  // 10.8
1049  rz_sys_setenv("DYLD_FORCE_FLAT_NAMESPACE", "1");
1050 #else
1051  rz_sys_setenv("LD_PRELOAD", p->_preload);
1052 #endif
1053  }
1054  if (p->_timeout) {
1055 #if __UNIX__
1056  int mypid = getpid();
1057  if (!rz_sys_fork()) {
1058  int use_signal = p->_timeout_sig;
1059  if (use_signal < 1) {
1060  use_signal = SIGKILL;
1061  }
1062  sleep(p->_timeout);
1063  if (!kill(mypid, 0)) {
1064  // eprintf ("\nrz_run: Interrupted by timeout\n");
1065  }
1066  kill(mypid, use_signal);
1067  exit(0);
1068  }
1069 #else
1070  if (p->_timeout_sig < 1 || p->_timeout_sig == 9) {
1071  rz_th_new(exit_process, (void *)p->_timeout);
1072  } else {
1073  eprintf("timeout with signal not supported for this platform\n");
1074  }
1075 #endif
1076  }
1077  return 0;
1078 }
#define SIGKILL
#define NULL
Definition: cris-opc.c:27
static static fork const void static count static fd const char const char static newpath chdir
Definition: sflib.h:33
static static fork write
Definition: sflib.h:33
static static fork const void static count close
Definition: sflib.h:33
static static fork const void static count static fd const char const char static newpath const char static path const char static mode static getpid setgid
Definition: sflib.h:41
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * p
Definition: libc.cpp:67
static int redirect_socket_to_pty(RzSocket *sock)
Definition: run.c:731
static int redirect_socket_to_stdio(RzSocket *sock)
Definition: run.c:709
static int handle_redirection(const char *cmd, bool in, bool out, bool err)
Definition: run.c:389
static char * getstr(const char *src)
Definition: run.c:152
static void setASLR(RzRunProfile *r, int enabled)
Definition: run.c:252
RZ_API RZ_OWN RzThread * rz_th_new(RZ_NONNULL RzThreadFunction function, RZ_NULLABLE void *user)
Creates and starts a new thread.
Definition: thread.c:198
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 kill
Definition: sflib.h:64
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path dup2
Definition: sflib.h:94
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path static newfd static getpgrp static euid const sigset_t static mask const char static len setgroups
Definition: sflib.h:112
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid chroot
Definition: sflib.h:92
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API RZ_OWN char * rz_file_path_join(RZ_NONNULL const char *s1, RZ_NULLABLE const char *s2)
Concatenate two paths to create a new one with s1+s2 with the correct path separator.
Definition: file.c:1312
#define RZ_LIB_EXT
Definition: rz_lib.h:31
RZ_API RZ_OWN char * rz_path_libdir(void)
Return the directory where the Rizin libraries are placed.
Definition: path.c:155
RZ_API int rz_socket_close_fd(RzSocket *s)
Definition: socket.c:410
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
#define rz_socket_connect_tcp(a, b, c, d)
Definition: rz_socket.h:99
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
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
RZ_API int rz_sys_setenv(const char *key, const char *value)
Set an environment variable in the calling process.
Definition: sys.c:405
RZ_API int rz_sys_pipe(int pipefd[2], bool close_on_exec)
Definition: sys.c:1458
RZ_API int rz_sys_fork(void)
Definition: sys.c:1679
RZ_API int rz_sys_pipe_close(int fd)
Definition: sys.c:1462
RZ_API int rz_sys_sleep(int secs)
Sleep for secs seconds.
Definition: sys.c:300
int pid_t
Definition: sftypes.h:38
static const z80_opcode fd[]
Definition: z80_tab.h:997

References chdir, chroot, close, dup2, eprintf, test-lz4-list::exit, autogen_x86imm::f2, fd, free(), getstr(), handle_redirection(), kill, NULL, p, redirect_socket_to_pty(), redirect_socket_to_stdio(), rz_file_path_join(), RZ_LIB_EXT, rz_path_libdir(), rz_socket_accept(), rz_socket_close_fd(), rz_socket_connect_tcp, rz_socket_free(), rz_socket_listen(), rz_socket_new(), rz_str_newf(), rz_sys_fork(), rz_sys_getenv(), rz_sys_pipe(), rz_sys_pipe_close(), rz_sys_setenv(), rz_sys_sleep(), rz_th_new(), setASLR(), setgid, setgroups, SIGKILL, and write.

Referenced by rz_main_rz_run().

◆ rz_run_free()

RZ_API void rz_run_free ( RzRunProfile r)

Definition at line 122 of file run.c.

122  {
123  if (r) {
124  free(r->_system);
125  free(r->_program);
126  free(r->_runlib);
127  free(r->_runlib_fcn);
128  free(r->_stdio);
129  free(r->_stdin);
130  free(r->_stdout);
131  free(r->_stderr);
132  free(r->_chgdir);
133  free(r->_chroot);
134  free(r->_libpath);
135  free(r->_preload);
136  free(r);
137  }
138 }
#define r
Definition: crypto_rc6.c:12

References free(), and r.

Referenced by rz_main_rz_run(), and rz_socket_spawn().

◆ rz_run_get_environ_profile()

RZ_API char* rz_run_get_environ_profile ( char **  env)

Definition at line 1329 of file run.c.

1329  {
1330  if (!env) {
1331  return NULL;
1332  }
1334  while (*env) {
1335  char *k = strdup(*env);
1336  char *v = strchr(k, '=');
1337  if (v) {
1338  *v++ = 0;
1339  RzStrEscOptions opt = { 0 };
1340  opt.show_asciidot = false;
1341  opt.esc_bslash = true;
1342  v = rz_str_escape_8bit(v, true, &opt);
1343  if (v) {
1344  rz_strbuf_appendf(sb, "setenv=%s=\"%s\"\n", k, v);
1345  free(v);
1346  }
1347  }
1348  free(k);
1349  env++;
1350  }
1351  return rz_strbuf_drain(sb);
1352 }
static SblHeader sb
Definition: bin_mbn.c:26
const char * k
Definition: dsignal.c:11
const char * v
Definition: dsignal.c:12
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")
RZ_API char * rz_str_escape_8bit(const char *buf, bool colors, RzStrEscOptions *opt)
Definition: str.c:1595
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
Group together some common options used by string escaping functions.
Definition: rz_str.h:39
bool esc_bslash
When true, backslashes \ are quoted with \\
Definition: rz_str.h:41
bool show_asciidot
When true, dots . are placed instead of unprintable characters.
Definition: rz_str.h:40
static char ** env
Definition: sys.c:32

References env, RzStrEscOptions::esc_bslash, free(), k, NULL, rz_str_escape_8bit(), rz_strbuf_appendf(), rz_strbuf_drain(), rz_strbuf_new(), sb, RzStrEscOptions::show_asciidot, strdup(), and v.

Referenced by rz_main_rizin().

◆ rz_run_help()

RZ_API const char* rz_run_help ( void  )

Definition at line 630 of file run.c.

630  {
631  return "program=/bin/ls\n"
632  "arg1=/bin\n"
633  "# arg2=hello\n"
634  "# arg3=\"hello\\nworld\"\n"
635  "# arg4=:048490184058104849\n"
636  "# arg5=:!rz-gg -p n50 -d 10:0x8048123\n"
637  "# arg6=@arg.txt\n"
638  "# arg7=@300@ABCD # 300 chars filled with ABCD pattern\n"
639  "# system=rizin -\n"
640  "# daemon=false\n"
641  "# aslr=no\n"
642  "setenv=FOO=BAR\n"
643  "# unsetenv=FOO\n"
644  "# clearenv=true\n"
645  "# envfile=environ.txt\n"
646  "timeout=3\n"
647  "# timeoutsig=SIGTERM # or 15\n"
648  "# connect=localhost:8080\n"
649  "# listen=8080\n"
650  "# pty=false\n"
651  "# fork=true\n"
652  "# bits=32\n"
653  "# pid=0\n"
654  "# pidfile=/tmp/foo.pid\n"
655  "# #sleep=0\n"
656  "# #maxfd=0\n"
657  "# #execve=false\n"
658  "# #maxproc=0\n"
659  "# #maxstack=0\n"
660  "# #core=false\n"
661  "# #stdio=blah.txt\n"
662  "# #stderr=foo.txt\n"
663  "# stdout=foo.txt\n"
664  "# stdin=input.txt # or !program to redirect input from another program\n"
665  "# input=input.txt\n"
666  "# chdir=/\n"
667  "# chroot=/mnt/chroot\n"
668  "# libpath=$PWD:/tmp/lib\n"
669  "# rzpreload=yes\n"
670  "# preload=/lib/libfoo.so\n"
671  "# setuid=2000\n"
672  "# seteuid=2000\n"
673  "# setgid=2001\n"
674  "# setegid=2001\n"
675  "# nice=5\n";
676 }

Referenced by rz_main_rz_run().

◆ rz_run_new()

RZ_API RzRunProfile* rz_run_new ( const char *  str)

Definition at line 86 of file run.c.

86  {
88  if (p) {
89  rz_run_reset(p);
90  if (str) {
92  }
93  }
94  return p;
95 }
RZ_API void rz_run_reset(RzRunProfile *p)
Definition: run.c:97
RZ_API bool rz_run_parsefile(RzRunProfile *p, const char *b)
Definition: run.c:468
#define RZ_NEW0(x)
Definition: rz_types.h:284

References p, RZ_NEW0, rz_run_parsefile(), rz_run_reset(), and cmd_descs_generate::str.

Referenced by rz_main_rz_run(), and rz_socket_spawn().

◆ rz_run_parse()

RZ_API bool rz_run_parse ( RzRunProfile pf,
const char *  profile 
)

Definition at line 103 of file run.c.

103  {
104  rz_return_val_if_fail(pf && profile, false);
105  char *p, *o, *str = strdup(profile);
106  if (!str) {
107  return false;
108  }
109  rz_str_replace_char(str, '\r', 0);
110  p = str;
111  while (p) {
112  if ((o = strchr(p, '\n'))) {
113  *o++ = 0;
114  }
115  rz_run_parseline(pf, p);
116  p = o;
117  }
118  free(str);
119  return true;
120 }
RZ_API bool rz_run_parseline(RzRunProfile *p, const char *b)
Definition: run.c:479
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API int rz_str_replace_char(char *s, int a, int b)
Definition: str.c:169

References free(), p, rz_return_val_if_fail, rz_run_parseline(), rz_str_replace_char(), cmd_descs_generate::str, and strdup().

Referenced by rz_run_parsefile().

◆ rz_run_parsefile()

RZ_API bool rz_run_parsefile ( RzRunProfile p,
const char *  b 
)

Definition at line 468 of file run.c.

468  {
469  rz_return_val_if_fail(p && b, false);
470  char *s = rz_file_slurp(b, NULL);
471  if (s) {
472  bool ret = rz_run_parse(p, s);
473  free(s);
474  return ret;
475  }
476  return 0;
477 }
RZ_API bool rz_run_parse(RzRunProfile *pf, const char *profile)
Definition: run.c:103
static RzSocket * s
Definition: rtr.c:28
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
#define b(i)
Definition: sha256.c:42

References b, free(), NULL, p, rz_file_slurp(), rz_return_val_if_fail, rz_run_parse(), and s.

Referenced by rz_run_new().

◆ rz_run_parseline()

RZ_API bool rz_run_parseline ( RzRunProfile p,
const char *  b 
)

Definition at line 479 of file run.c.

479  {
480  int must_free = false;
481  char *e = strchr(b, '=');
482  if (!e || *b == '#') {
483  return 0;
484  }
485  *e++ = 0;
486  if (*e == '$') {
487  must_free = true;
488  e = rz_sys_getenv(e);
489  }
490  if (!e) {
491  return 0;
492  }
493  if (!strcmp(b, "program")) {
494  p->_args[0] = p->_program = strdup(e);
495  } else if (!strcmp(b, "daemon")) {
496  p->_daemon = true;
497  } else if (!strcmp(b, "system")) {
498  p->_system = strdup(e);
499  } else if (!strcmp(b, "runlib")) {
500  p->_runlib = strdup(e);
501  } else if (!strcmp(b, "runlib.fcn")) {
502  p->_runlib_fcn = strdup(e);
503  } else if (!strcmp(b, "aslr")) {
504  p->_aslr = parseBool(e);
505  } else if (!strcmp(b, "pid")) {
506  p->_pid = parseBool(e);
507  } else if (!strcmp(b, "pidfile")) {
508  p->_pidfile = strdup(e);
509  } else if (!strcmp(b, "connect")) {
510  p->_connect = strdup(e);
511  } else if (!strcmp(b, "listen")) {
512  p->_listen = strdup(e);
513  } else if (!strcmp(b, "pty")) {
514  p->_pty = parseBool(e);
515  } else if (!strcmp(b, "stdio")) {
516  if (e[0] == '!') {
517  p->_stdio = strdup(e);
518  } else {
519  p->_stdout = strdup(e);
520  p->_stderr = strdup(e);
521  p->_stdin = strdup(e);
522  }
523  } else if (!strcmp(b, "stdout")) {
524  p->_stdout = strdup(e);
525  } else if (!strcmp(b, "stdin")) {
526  p->_stdin = strdup(e);
527  } else if (!strcmp(b, "stderr")) {
528  p->_stderr = strdup(e);
529  } else if (!strcmp(b, "input")) {
530  p->_input = strdup(e);
531  } else if (!strcmp(b, "chdir")) {
532  p->_chgdir = strdup(e);
533  } else if (!strcmp(b, "core")) {
534  p->_docore = parseBool(e);
535  } else if (!strcmp(b, "fork")) {
536  p->_dofork = parseBool(e);
537  } else if (!strcmp(b, "sleep")) {
538  p->_rzsleep = atoi(e);
539  } else if (!strcmp(b, "maxstack")) {
540  p->_maxstack = atoi(e);
541  } else if (!strcmp(b, "maxproc")) {
542  p->_maxproc = atoi(e);
543  } else if (!strcmp(b, "maxfd")) {
544  p->_maxfd = atoi(e);
545  } else if (!strcmp(b, "bits")) {
546  p->_bits = atoi(e);
547  } else if (!strcmp(b, "chroot")) {
548  p->_chroot = strdup(e);
549  } else if (!strcmp(b, "libpath")) {
550  p->_libpath = strdup(e);
551  } else if (!strcmp(b, "preload")) {
552  p->_preload = strdup(e);
553  } else if (!strcmp(b, "rzpreload")) {
554  p->_rzpreload = parseBool(e);
555  } else if (!strcmp(b, "rzpreweb")) {
556  rz_sys_setenv("RZ_RUN_WEB", "yes");
557  } else if (!strcmp(b, "setuid")) {
558  p->_setuid = strdup(e);
559  } else if (!strcmp(b, "seteuid")) {
560  p->_seteuid = strdup(e);
561  } else if (!strcmp(b, "setgid")) {
562  p->_setgid = strdup(e);
563  } else if (!strcmp(b, "setegid")) {
564  p->_setegid = strdup(e);
565  } else if (!strcmp(b, "nice")) {
566  p->_nice = atoi(e);
567  } else if (!strcmp(b, "timeout")) {
568  p->_timeout = atoi(e);
569  } else if (!strcmp(b, "timeoutsig")) {
570  p->_timeout_sig = rz_signal_from_string(e);
571  } else if (!memcmp(b, "arg", 3)) {
572  int n = atoi(b + 3);
573  if (n >= 0 && n < RZ_RUN_PROFILE_NARGS) {
574  p->_args[n] = getstr(e);
575  p->_argc++;
576  } else {
577  eprintf("Out of bounds args index: %d\n", n);
578  }
579  } else if (!strcmp(b, "envfile")) {
580  char *p, buf[1024];
581  size_t len;
582  FILE *fd = rz_sys_fopen(e, "r");
583  if (!fd) {
584  eprintf("Cannot open '%s'\n", e);
585  if (must_free == true) {
586  free(e);
587  }
588  return false;
589  }
590  for (;;) {
591  if (!fgets(buf, sizeof(buf), fd)) {
592  break;
593  }
594  if (feof(fd)) {
595  break;
596  }
597  p = strchr(buf, '=');
598  if (p) {
599  *p++ = 0;
600  len = strlen(p);
601  if (len > 0 && p[len - 1] == '\n') {
602  p[len - 1] = 0;
603  }
604  if (len > 1 && p[len - 2] == '\r') {
605  p[len - 2] = 0;
606  }
607  rz_sys_setenv(buf, p);
608  }
609  }
610  fclose(fd);
611  } else if (!strcmp(b, "unsetenv")) {
612  rz_sys_setenv(e, NULL);
613  } else if (!strcmp(b, "setenv")) {
614  char *V, *v = strchr(e, '=');
615  if (v) {
616  *v++ = 0;
617  V = getstr(v);
618  rz_sys_setenv(e, V);
619  free(V);
620  }
621  } else if (!strcmp(b, "clearenv")) {
622  rz_sys_clearenv();
623  }
624  if (must_free == true) {
625  free(e);
626  }
627  return true;
628 }
size_t len
Definition: 6502dis.c:15
#define e(frag)
voidpf void * buf
Definition: ioapi.h:138
static int parseBool(const char *e)
Definition: run.c:247
int n
Definition: mipsasm.c:19
string FILE
Definition: benchmark.py:21
RZ_API int rz_signal_from_string(const char *str)
Definition: signal.c:52
#define RZ_RUN_PROFILE_NARGS
Definition: rz_socket.h:211
RZ_API int rz_sys_clearenv(void)
Clean all environment variables in the calling process.
Definition: sys.c:346
RZ_API FILE * rz_sys_fopen(const char *path, const char *mode)
Definition: sys.c:1815
#define V(handle, symbol)

References b, e, eprintf, fd, benchmark::FILE, free(), getstr(), len, n, NULL, p, parseBool(), RZ_RUN_PROFILE_NARGS, rz_signal_from_string(), rz_sys_clearenv(), rz_sys_fopen(), rz_sys_getenv(), rz_sys_setenv(), strdup(), v, and V.

Referenced by rz_main_rz_run(), and rz_run_parse().

◆ rz_run_reset()

RZ_API void rz_run_reset ( RzRunProfile p)

Definition at line 97 of file run.c.

97  {
99  memset(p, 0, sizeof(RzRunProfile));
100  p->_aslr = -1;
101 }
return memset(p, 0, total)
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100

References memset(), p, and rz_return_if_fail.

Referenced by rz_run_new().

◆ rz_run_start()

RZ_API int rz_run_start ( RzRunProfile p)

Definition at line 1081 of file run.c.

1081  {
1082 #if HAVE_EXECVE
1083  if (p->_execve) {
1084  exit(rz_sys_execv(p->_program, (char *const *)p->_args));
1085  }
1086 #endif
1087 #if __APPLE__ && HAVE_FORK
1088  posix_spawnattr_t attr = { 0 };
1089  pid_t pid = -1;
1090  int ret;
1091  posix_spawnattr_init(&attr);
1092  if (p->_args[0]) {
1093  char **envp = rz_sys_get_environ();
1094  ut32 spflags = 0; // POSIX_SPAWN_START_SUSPENDED;
1095  spflags |= POSIX_SPAWN_SETEXEC;
1096  if (p->_aslr == 0) {
1097 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
1098  spflags |= _POSIX_SPAWN_DISABLE_ASLR;
1099  }
1100  (void)posix_spawnattr_setflags(&attr, spflags);
1101  if (p->_bits) {
1102  size_t copied = 1;
1103  cpu_type_t cpu;
1104 #if __i386__ || __x86_64__
1105  cpu = CPU_TYPE_I386;
1106  if (p->_bits == 64) {
1107  cpu |= CPU_ARCH_ABI64;
1108  }
1109 #else
1110  cpu = CPU_TYPE_ANY;
1111 #endif
1112  posix_spawnattr_setbinpref_np(
1113  &attr, 1, &cpu, &copied);
1114  }
1115  ret = posix_spawnp(&pid, p->_args[0],
1116  NULL, &attr, p->_args, envp);
1117  switch (ret) {
1118  case 0:
1119  break;
1120  default:
1121  eprintf("posix_spawnp: %s\n", strerror(ret));
1122  break;
1123  }
1124  exit(ret);
1125  }
1126 #endif
1127  if (p->_system) {
1128  if (p->_pid) {
1129  eprintf("PID: Cannot determine pid with 'system' directive. Use 'program'.\n");
1130  }
1131  if (p->_daemon) {
1132 #if __WINDOWS__
1133  // eprintf ("PID: Cannot determine pid with 'system' directive. Use 'program'.\n");
1134 #else
1135  pid_t child = rz_sys_fork();
1136  if (child == -1) {
1137  perror("fork");
1138  exit(1);
1139  }
1140  if (child) {
1141  if (p->_pidfile) {
1142  char pidstr[32];
1143  snprintf(pidstr, sizeof(pidstr), "%d\n", child);
1144  rz_file_dump(p->_pidfile,
1145  (const ut8 *)pidstr,
1146  strlen(pidstr), 0);
1147  }
1148  exit(0);
1149  }
1150  setsid();
1151  if (p->_timeout) {
1152 #if __UNIX__
1153  int mypid = getpid();
1154  if (!rz_sys_fork()) {
1155  int use_signal = p->_timeout_sig;
1156  if (use_signal < 1) {
1157  use_signal = SIGKILL;
1158  }
1159  sleep(p->_timeout);
1160  if (!kill(mypid, 0)) {
1161  // eprintf ("\nrz_run: Interrupted by timeout\n");
1162  }
1163  kill(mypid, use_signal);
1164  exit(0);
1165  }
1166 #else
1167  eprintf("timeout not supported for this platform\n");
1168 #endif
1169  }
1170 #endif
1171 #if __UNIX__
1172  close(0);
1173  close(1);
1174  char *bin_sh = rz_file_binsh();
1175  int ret = rz_sys_execl(bin_sh, "sh", "-c", p->_system, NULL);
1176  free(bin_sh);
1177  exit(ret);
1178 #else
1179  exit(rz_sys_system(p->_system));
1180 #endif
1181  } else {
1182  if (p->_pidfile) {
1183  eprintf("Warning: pidfile doesnt work with 'system'.\n");
1184  }
1185  exit(rz_sys_system(p->_system));
1186  }
1187  }
1188  if (p->_program) {
1189  if (!rz_file_exists(p->_program)) {
1190  char *progpath = rz_file_path(p->_program);
1191  if (progpath && *progpath) {
1192  free(p->_program);
1193  p->_program = progpath;
1194  } else {
1195  free(progpath);
1196  eprintf("rz-run: %s: file not found\n", p->_program);
1197  return 1;
1198  }
1199  }
1200 #if __UNIX__
1201  // XXX HACK close all non-tty fds
1202  {
1203  int i;
1204  for (i = 3; i < 1024; i++) {
1205  close(i);
1206  }
1207  }
1208  // TODO: use posix_spawn
1209  if (p->_setgid) {
1210  int ret = setgid(atoi(p->_setgid));
1211  if (ret < 0) {
1212  return 1;
1213  }
1214  }
1215  if (p->_pid) {
1216  eprintf("PID: %d\n", getpid());
1217  }
1218  if (p->_pidfile) {
1219  char pidstr[32];
1220  snprintf(pidstr, sizeof(pidstr), "%d\n", getpid());
1221  rz_file_dump(p->_pidfile,
1222  (const ut8 *)pidstr,
1223  strlen(pidstr), 0);
1224  }
1225 #endif
1226 
1227  if (p->_nice) {
1228 #if HAVE_NICE
1229  if (nice(p->_nice) == -1) {
1230  return 1;
1231  }
1232 #else
1233  eprintf("nice not supported for this platform\n");
1234 #endif
1235  }
1236  if (p->_daemon) {
1237 #if __WINDOWS__
1238  eprintf("PID: Cannot determine pid with 'system' directive. Use 'program'.\n");
1239 #else
1240  pid_t child = rz_sys_fork();
1241  if (child == -1) {
1242  perror("fork");
1243  exit(1);
1244  }
1245  if (child) {
1246  if (p->_pidfile) {
1247  char pidstr[32];
1248  snprintf(pidstr, sizeof(pidstr), "%d\n", child);
1249  rz_file_dump(p->_pidfile,
1250  (const ut8 *)pidstr,
1251  strlen(pidstr), 0);
1252  exit(0);
1253  }
1254  }
1255  setsid();
1256 #if HAVE_EXECVE
1257  exit(rz_sys_execv(p->_program, (char *const *)p->_args));
1258 #endif
1259 #endif
1260  }
1261 #if HAVE_EXECVE
1262  exit(rz_sys_execv(p->_program, (char *const *)p->_args));
1263 #endif
1264  }
1265  if (p->_runlib) {
1266  if (!p->_runlib_fcn) {
1267  eprintf("No function specified. Please set runlib.fcn\n");
1268  return 1;
1269  }
1270  void *addr = rz_lib_dl_open(p->_runlib);
1271  if (!addr) {
1272  eprintf("Could not load the library '%s'\n", p->_runlib);
1273  return 1;
1274  }
1275  void (*fcn)(void) = rz_lib_dl_sym(addr, p->_runlib_fcn);
1276  if (!fcn) {
1277  eprintf("Could not find the function '%s'\n", p->_runlib_fcn);
1278  return 1;
1279  }
1280  switch (p->_argc) {
1281  case 0:
1282  fcn();
1283  break;
1284  case 1:
1285  rz_run_call1(fcn, p->_args[1]);
1286  break;
1287  case 2:
1288  rz_run_call2(fcn, p->_args[1], p->_args[2]);
1289  break;
1290  case 3:
1291  rz_run_call3(fcn, p->_args[1], p->_args[2], p->_args[3]);
1292  break;
1293  case 4:
1294  rz_run_call4(fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4]);
1295  break;
1296  case 5:
1297  rz_run_call5(fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
1298  p->_args[5]);
1299  break;
1300  case 6:
1301  rz_run_call6(fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
1302  p->_args[5], p->_args[6]);
1303  break;
1304  case 7:
1305  rz_run_call7(fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
1306  p->_args[5], p->_args[6], p->_args[7]);
1307  break;
1308  case 8:
1309  rz_run_call8(fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
1310  p->_args[5], p->_args[6], p->_args[7], p->_args[8]);
1311  break;
1312  case 9:
1313  rz_run_call9(fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
1314  p->_args[5], p->_args[6], p->_args[7], p->_args[8], p->_args[9]);
1315  break;
1316  case 10:
1317  rz_run_call10(fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
1318  p->_args[5], p->_args[6], p->_args[7], p->_args[8], p->_args[9], p->_args[10]);
1319  break;
1320  default:
1321  eprintf("Too many arguments.\n");
1322  return 1;
1323  }
1325  }
1326  return 0;
1327 }
static ut32 cpu[32]
Definition: analysis_or1k.c:21
lzma_index ** i
Definition: index.h:629
uint32_t ut32
snprintf
Definition: kernel.h:364
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 nice
Definition: sflib.h:61
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 pid
Definition: sflib.h:64
@ CPU_ARCH_ABI64
@ CPU_TYPE_I386
@ CPU_TYPE_ANY
RZ_API char * rz_file_binsh(void)
Definition: file.c:393
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
RZ_API bool rz_file_dump(const char *file, const ut8 *buf, int len, bool append)
Definition: file.c:838
RZ_API char * rz_file_path(const char *bin)
Definition: file.c:354
RZ_API void * rz_lib_dl_sym(void *handler, const char *name)
Definition: lib.c:90
RZ_API void * rz_lib_dl_open(const char *libname)
Definition: lib.c:54
RZ_API int rz_lib_dl_close(void *handler)
Definition: lib.c:104
RZ_API int rz_sys_execv(const char *pathname, char *const argv[])
Definition: sys.c:1483
RZ_API int rz_sys_execl(const char *pathname, const char *arg,...)
Definition: sys.c:1575
RZ_API int rz_sys_system(const char *command)
Definition: sys.c:1658
RZ_API char ** rz_sys_get_environ(void)
Definition: sys.c:1115
static void rz_run_call8(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6, void *arg7, void *arg8)
Definition: rz_types.h:624
static void rz_run_call9(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6, void *arg7, void *arg8, void *arg9)
Definition: rz_types.h:629
static void rz_run_call1(void *fcn, void *arg1)
Definition: rz_types.h:594
static void rz_run_call6(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6)
Definition: rz_types.h:614
static void rz_run_call3(void *fcn, void *arg1, void *arg2, void *arg3)
Definition: rz_types.h:602
static void rz_run_call4(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4)
Definition: rz_types.h:606
static void rz_run_call5(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5)
Definition: rz_types.h:610
static void rz_run_call10(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6, void *arg7, void *arg8, void *arg9, void *arg10)
Definition: rz_types.h:634
static void rz_run_call7(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6, void *arg7)
Definition: rz_types.h:619
static void rz_run_call2(void *fcn, void *arg1, void *arg2)
Definition: rz_types.h:598
static int addr
Definition: z80asm.c:58

References addr, close, cpu, CPU_ARCH_ABI64, CPU_TYPE_ANY, CPU_TYPE_I386, eprintf, test-lz4-list::exit, free(), i, kill, nice, NULL, p, pid, rz_file_binsh(), rz_file_dump(), rz_file_exists(), rz_file_path(), rz_lib_dl_close(), rz_lib_dl_open(), rz_lib_dl_sym(), rz_run_call1(), rz_run_call10(), rz_run_call2(), rz_run_call3(), rz_run_call4(), rz_run_call5(), rz_run_call6(), rz_run_call7(), rz_run_call8(), rz_run_call9(), rz_sys_execl(), rz_sys_execv(), rz_sys_fork(), rz_sys_get_environ(), rz_sys_system(), setgid, SIGKILL, and snprintf.

Referenced by rz_main_rz_run(), and rz_socket_spawn().

◆ rz_socket_accept()

RZ_API RzSocket* rz_socket_accept ( RzSocket s)

Definition at line 582 of file socket.c.

582  {
583  RzSocket *sock;
584  socklen_t salen = sizeof(s->sa);
585  if (!s) {
586  return NULL;
587  }
588  sock = RZ_NEW0(RzSocket);
589  if (!sock) {
590  return NULL;
591  }
592  // signal (SIGPIPE, SIG_DFL);
593  sock->fd = accept(s->fd, (struct sockaddr *)&s->sa, &salen);
594  if (sock->fd == RZ_INVALID_SOCKET) {
595  if (errno != EWOULDBLOCK) {
596  // not just a timeout
597  rz_sys_perror("accept");
598  }
599  free(sock);
600  return NULL;
601  }
602 #if HAVE_LIB_SSL
603  sock->is_ssl = s->is_ssl;
604  if (sock->is_ssl) {
605  sock->sfd = NULL;
606  sock->ctx = NULL;
607  sock->bio = NULL;
608  BIO *sbio = BIO_new_socket(sock->fd, BIO_NOCLOSE);
609  sock->sfd = SSL_new(s->ctx);
610  SSL_set_bio(sock->sfd, sbio, sbio);
611  if (SSL_accept(sock->sfd) <= 0) {
612  rz_socket_free(sock);
613  return NULL;
614  }
615  sock->bio = BIO_new(BIO_f_buffer());
616  sbio = BIO_new(BIO_f_ssl());
617  BIO_set_ssl(sbio, sock->sfd, BIO_CLOSE);
618  BIO_push(sock->bio, sbio);
619  }
620 #else
621  sock->is_ssl = 0;
622 #endif
623  return sock;
624 }
static const void static count static fd struct stat static buf struct pollfd unsigned static timeout void static offset void static length char static len const struct iovec static count unsigned long static filedes static sched_yield static flags static oldfd static pause unsigned static seconds static protocol accept
Definition: sflib.h:75
#define RZ_INVALID_SOCKET
Definition: rz_socket.h:48
#define rz_sys_perror(x)
Definition: rz_types.h:336
unsigned int socklen_t
Definition: sftypes.h:219
RZ_API int rz_socket_free(RzSocket *s)
Definition: socket.c:453
bool is_ssl
Definition: rz_socket.h:68
struct sockaddr_in sa
Definition: rz_socket.h:72

References accept, rz_socket_t::fd, free(), rz_socket_t::is_ssl, NULL, RZ_INVALID_SOCKET, RZ_NEW0, rz_socket_free(), rz_sys_perror, s, and rz_socket_t::sa.

Referenced by rz_core_rtr_cmds(), rz_core_rtr_gdb_run(), rz_core_serve(), rz_run_config_env(), rz_socket_accept_timeout(), rz_socket_http_accept(), rz_socket_rap_server_accept(), rz_write_from_socket_handler(), and tcpme().

◆ rz_socket_accept_timeout()

RZ_API RzSocket* rz_socket_accept_timeout ( RzSocket s,
unsigned int  timeout 
)

Definition at line 626 of file socket.c.

626  {
627  fd_set read_fds;
628  fd_set except_fds;
629 
630  FD_ZERO(&read_fds);
631  FD_SET(s->fd, &read_fds);
632 
633  FD_ZERO(&except_fds);
634  FD_SET(s->fd, &except_fds);
635 
636  struct timeval t = { timeout, 0 };
637 
638  int r = select(s->fd + 1, &read_fds, NULL, &except_fds, &t);
639  if (r < 0) {
640  perror("select");
641  } else if (r > 0 && FD_ISSET(s->fd, &read_fds)) {
642  return rz_socket_accept(s);
643  }
644 
645  return NULL;
646 }
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 static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz select
Definition: sflib.h:108
#define FD_ZERO(set)
Definition: sftypes.h:204
#define FD_ISSET(d, set)
Definition: sftypes.h:214
#define FD_SET(d, set)
Definition: sftypes.h:212
RZ_API RzSocket * rz_socket_accept(RzSocket *s)
Definition: socket.c:582
uv_timer_t timeout
Definition: main.c:9

References rz_socket_t::fd, FD_ISSET, FD_SET, FD_ZERO, NULL, r, rz_socket_accept(), s, select, and timeout.

Referenced by rz_socket_http_accept().

◆ rz_socket_block_time()

RZ_API bool rz_socket_block_time ( RzSocket s,
bool  block,
int  sec,
int  usec 
)

Definition at line 649 of file socket.c.

649  {
650 #if __UNIX__
651  int ret, flags;
652 #endif
653  if (!s) {
654  return false;
655  }
656 #if __UNIX__
657  flags = fcntl(s->fd, F_GETFL, 0);
658  if (flags < 0) {
659  return false;
660  }
661  ret = fcntl(s->fd, F_SETFL, block ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK));
662  if (ret < 0) {
663  return false;
664  }
665 #elif __WINDOWS__
666  ioctlsocket(s->fd, FIONBIO, (u_long FAR *)&block);
667 #endif
668  if (sec > 0 || usec > 0) {
669  struct timeval tv = { sec, usec };
670  if (setsockopt(s->fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0) {
671  return false;
672  }
673  }
674  return true;
675 }
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 tv
Definition: sflib.h:79
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 fcntl
Definition: sflib.h:79
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define F_GETFL
Definition: sftypes.h:506
#define O_NONBLOCK
Definition: sftypes.h:494
#define SOL_SOCKET
Definition: sftypes.h:427
#define F_SETFL
Definition: sftypes.h:507
#define SO_RCVTIMEO
Definition: sftypes.h:448
#define FIONBIO
Definition: sftypes.h:736
#define FAR
Definition: zconf.h:387

References F_GETFL, F_SETFL, FAR, fcntl, rz_socket_t::fd, FIONBIO, flags, O_NONBLOCK, s, SO_RCVTIMEO, SOL_SOCKET, and tv.

Referenced by runcmd(), rz_socket_connect(), rz_socket_http_accept(), rz_socket_is_connected(), rz_socket_rap_client_open(), rz_socket_rap_client_read(), and socket_slurp().

◆ rz_socket_close()

RZ_API int rz_socket_close ( RzSocket s)

Definition at line 419 of file socket.c.

419  {
420  int ret = false;
421  if (!s) {
422  return false;
423  }
424  if (s->fd != RZ_INVALID_SOCKET) {
425 #if __UNIX__
426  shutdown(s->fd, SHUT_RDWR);
427 #endif
428 #if __WINDOWS__
429  // https://msdn.microsoft.com/en-us/library/windows/desktop/ms740481(v=vs.85).aspx
430  shutdown(s->fd, SD_SEND);
431  if (rz_socket_ready(s, 0, 250)) {
432  do {
433  char buf = 0;
434  ret = recv(s->fd, &buf, 1, 0);
435  } while (ret != 0 && ret != SOCKET_ERROR);
436  }
437  ret = closesocket(s->fd);
438 #else
439  ret = close(s->fd);
440 #endif
442  }
443 #if HAVE_LIB_SSL
444  if (s->is_ssl && s->sfd) {
445  SSL_free(s->sfd);
446  s->sfd = NULL;
447  }
448 #endif
449  return ret;
450 }
#define SD_SEND
Definition: rz_socket.h:41
RZ_API int rz_socket_ready(RzSocket *s, int secs, int usecs)
Definition: socket.c:688

References close, rz_socket_t::fd, rz_socket_t::is_ssl, NULL, RZ_INVALID_SOCKET, rz_socket_ready(), s, and SD_SEND.

Referenced by __rap_close(), gdbr_connect(), gdbr_disconnect(), iob_net_close(), qnxr_disconnect(), rap_break(), rz_core_rtr_cmd(), rz_core_rtr_cmds(), rz_core_rtr_gdb_run(), rz_core_serve(), rz_socket_connect(), rz_socket_free(), rz_socket_gets(), rz_socket_rap_server_continue(), rz_socket_spawn(), and socket_http_answer().

◆ rz_socket_close_fd()

RZ_API int rz_socket_close_fd ( RzSocket s)

Definition at line 410 of file socket.c.

410  {
411 #ifdef _MSC_VER
412  return s->fd != INVALID_SOCKET ? closesocket(s->fd) : false;
413 #else
414  return s->fd != -1 ? close(s->fd) : false;
415 #endif
416 }
#define false

References close, rz_socket_t::fd, and s.

Referenced by redirect_socket_to_pty(), and rz_run_config_env().

◆ rz_socket_connect()

RZ_API bool rz_socket_connect ( RzSocket s,
const char *  host,
const char *  port,
int  proto,
unsigned int  timeout 
)

Definition at line 257 of file socket.c.

257  {
258  rz_return_val_if_fail(s, false);
259 #if __WINDOWS__
260 #define gai_strerror gai_strerrorA
261  WSADATA wsadata;
262 
263  if (WSAStartup(MAKEWORD(1, 1), &wsadata) == SOCKET_ERROR) {
264  eprintf("Error creating socket.");
265  return false;
266  }
267 #endif
268  int ret;
269  struct addrinfo hints = { 0 };
270  struct addrinfo *res, *rp;
271  if (proto == RZ_SOCKET_PROTO_NONE) {
272  proto = RZ_SOCKET_PROTO_DEFAULT;
273  }
274 #if __UNIX__
275  rz_sys_signal(SIGPIPE, SIG_IGN);
276 #endif
277  if (proto == RZ_SOCKET_PROTO_UNIX) {
278 #if __UNIX__
279  if (!__connect_unix(s, host)) {
280  return false;
281  }
282 #endif
283  } else {
284  hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
285  hints.ai_protocol = proto;
286  int gai = getaddrinfo(host, port, &hints, &res);
287  if (gai != 0) {
288  eprintf("rz_socket_connect: Error in getaddrinfo: %s (%s:%s)\n",
289  gai_strerror(gai), host, port);
290  return false;
291  }
292  for (rp = res; rp != NULL; rp = rp->ai_next) {
293  int flag = 1;
294 
295  s->fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
296  if (s->fd == -1) {
297  perror("socket");
298  continue;
299  }
300 
301  switch (proto) {
302  case RZ_SOCKET_PROTO_TCP:
303  ret = setsockopt(s->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag));
304  if (ret < 0) {
305  perror("setsockopt");
306  close(s->fd);
307  s->fd = -1;
308  continue;
309  }
310  rz_socket_block_time(s, true, 1, 0);
311  ret = connect(s->fd, rp->ai_addr, rp->ai_addrlen);
312  break;
313  case RZ_SOCKET_PROTO_UDP:
314  memset(&s->sa, 0, sizeof(s->sa));
315  s->sa.sin_family = AF_INET;
316  s->sa.sin_addr.s_addr = htonl(s->local ? INADDR_LOOPBACK : INADDR_ANY);
317  s->port = rz_socket_port_by_name(port);
318  if (s->port < 1) {
319  continue;
320  }
321  s->sa.sin_port = htons(s->port);
322  if (bind(s->fd, (struct sockaddr *)&s->sa, sizeof(s->sa)) < 0) {
323  rz_sys_perror("bind");
324 #ifdef __WINDOWS__
325  closesocket(s->fd);
326 #else
327  close(s->fd);
328 #endif
329  continue;
330  }
331  ret = connect(s->fd, rp->ai_addr, rp->ai_addrlen);
332  break;
333  default:
334  rz_socket_block_time(s, true, 1, 0);
335  ret = connect(s->fd, rp->ai_addr, rp->ai_addrlen);
336  break;
337  }
338 
339  if (ret == 0) {
340  freeaddrinfo(res);
341  return true;
342  }
343  if (errno == EINPROGRESS) {
344  struct timeval tv = { timeout, 0 };
345  fd_set wfds;
346  FD_ZERO(&wfds);
347  FD_SET(s->fd, &wfds);
348 
349  if (select(s->fd + 1, NULL, &wfds, NULL, &tv) != -1) {
350  if (rz_socket_is_connected(s)) {
351  freeaddrinfo(res);
352  goto success;
353  }
354  } else {
355  perror("connect");
356  }
357  }
359  }
360  freeaddrinfo(res);
361  if (!rp) {
362  eprintf("Could not resolve address '%s' or failed to connect\n", host);
363  return false;
364  }
365  }
366 success:
367 #if HAVE_LIB_SSL
368  if (s->is_ssl) {
369  s->ctx = SSL_CTX_new(SSLv23_client_method());
370  if (!s->ctx) {
372  return false;
373  }
374  s->sfd = SSL_new(s->ctx);
375  SSL_set_fd(s->sfd, s->fd);
376  int ret = SSL_connect(s->sfd);
377  if (ret != 1) {
378  int error = SSL_get_error(s->sfd, ret);
379  int tries = 10;
380  while (tries && ret && (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)) {
381  struct timeval tv = { 1, 0 };
382  fd_set rfds, wfds;
383  FD_ZERO(&rfds);
384  FD_ZERO(&wfds);
385  if (error == SSL_ERROR_WANT_READ) {
386  FD_SET(s->fd, &rfds);
387  } else {
388  FD_SET(s->fd, &wfds);
389  }
390  if ((ret = select(s->fd + 1, &rfds, &wfds, NULL, &tv)) < 1) {
392  return false;
393  }
394  ret = SSL_connect(s->sfd);
395  if (ret == 1) {
396  return true;
397  }
398  error = SSL_get_error(s->sfd, ret);
399  tries--;
400  }
402  return false;
403  }
404  }
405 #endif
406  return true;
407 }
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 socket
Definition: sflib.h:79
static char * rp[]
Definition: i8080dis.c:36
#define RZ_SOCKET_PROTO_NONE
Definition: rz_socket.h:90
#define RZ_SOCKET_PROTO_UNIX
Definition: rz_socket.h:89
#define RZ_SOCKET_PROTO_UDP
Definition: rz_socket.h:88
#define RZ_SOCKET_PROTO_TCP
Definition: rz_socket.h:87
#define RZ_SOCKET_PROTO_DEFAULT
Definition: rz_socket.h:91
RZ_API int rz_sys_signal(int sig, void(*handler)(int))
Definition: sys.c:178
static bind
Definition: sfsocketcall.h:114
#define htons(x)
Definition: sftypes.h:475
#define EINPROGRESS
Definition: sftypes.h:175
#define AF_INET
Definition: sftypes.h:287
#define AF_UNSPEC
Definition: sftypes.h:283
RZ_API bool rz_socket_is_connected(RzSocket *s)
Definition: socket.c:101
RZ_API int rz_socket_close(RzSocket *s)
Definition: socket.c:419
RZ_API int rz_socket_port_by_name(const char *name)
Definition: socket.c:469
RZ_API bool rz_socket_block_time(RzSocket *s, bool block, int sec, int usec)
Definition: socket.c:649
in_addr_t s_addr
Definition: sftypes.h:337
struct in_addr sin_addr
Definition: sftypes.h:344
in_port_t sin_port
Definition: sftypes.h:343
void error(const char *msg)
Definition: untgz.c:593

References AF_INET, AF_UNSPEC, bind, close, EINPROGRESS, eprintf, error(), rz_socket_t::fd, FD_SET, FD_ZERO, htons, rz_socket_t::is_ssl, rz_socket_t::local, memset(), NULL, rz_socket_t::port, rp, rz_return_val_if_fail, rz_socket_block_time(), rz_socket_close(), rz_socket_is_connected(), rz_socket_port_by_name(), RZ_SOCKET_PROTO_DEFAULT, RZ_SOCKET_PROTO_NONE, RZ_SOCKET_PROTO_TCP, RZ_SOCKET_PROTO_UDP, RZ_SOCKET_PROTO_UNIX, rz_sys_perror, rz_sys_signal(), s, in_addr::s_addr, rz_socket_t::sa, select, sockaddr_in::sin_addr, sockaddr_in::sin_port, socket, timeout, and tv.

Referenced by __rap_open(), rz_core_rtr_cmd(), rz_core_rtr_cmds_query(), rz_core_rtr_http_stop(), rz_socket_spawn(), and tcpme().

◆ rz_socket_connect_serial()

RZ_API int rz_socket_connect_serial ( RzSocket sock,
const char *  path,
int  speed,
int  parity 
)

Definition at line 63 of file socket_serial.c.

63  {
64  return -1;
65 }

Referenced by gdbr_connect().

◆ rz_socket_flush()

RZ_API int rz_socket_flush ( RzSocket s)

Definition at line 677 of file socket.c.

677  {
678 #if HAVE_LIB_SSL
679  if (s->is_ssl && s->bio) {
680  return BIO_flush(s->bio);
681  }
682 #endif
683  return true;
684 }

References rz_socket_t::is_ssl, and s.

Referenced by __rap_system(), rz_core_serve(), rz_socket_rap_client_command(), rz_socket_rap_client_open(), rz_socket_rap_client_read(), rz_socket_rap_client_seek(), rz_socket_rap_client_write(), and rz_socket_rap_server_continue().

◆ rz_socket_free()

◆ rz_socket_gets()

RZ_API int rz_socket_gets ( RzSocket s,
char *  buf,
int  size 
)

Definition at line 830 of file socket.c.

830  {
831  int i = 0;
832  int ret = 0;
833 
834  if (s->fd == RZ_INVALID_SOCKET) {
835  return -1;
836  }
837  while (i < size) {
838  ret = rz_socket_read(s, (ut8 *)buf + i, 1);
839  if (ret == 0) {
840  if (i > 0) {
841  return i;
842  }
843  return -1;
844  }
845  if (ret < 0) {
847  return i == 0 ? -1 : i;
848  }
849  if (buf[i] == '\r' || buf[i] == '\n') {
850  buf[i] = 0;
851  break;
852  }
853  i += ret;
854  }
855  buf[i] = '\0';
856  return i;
857 }
voidpf void uLong size
Definition: ioapi.h:138
RZ_API int rz_socket_read(RzSocket *s, unsigned char *buf, int len)
Definition: socket.c:783

References rz_socket_t::fd, i, RZ_INVALID_SOCKET, rz_socket_close(), rz_socket_read(), and s.

Referenced by rz_socket_http_accept(), and rz_socket_proc_gets().

◆ rz_socket_http_accept()

RZ_API RzSocketHTTPRequest* rz_socket_http_accept ( RzSocket s,
RzSocketHTTPOptions so 
)

Definition at line 13 of file socket_http_server.c.

13  {
14  int content_length = 0, xx, yy;
15  int pxx = 1, first = 0;
16  char buf[1500], *p, *q;
18  if (!hr) {
19  return NULL;
20  }
21  if (so->accept_timeout) {
22  hr->s = rz_socket_accept_timeout(s, 1);
23  } else {
24  hr->s = rz_socket_accept(s);
25  }
26  if (!hr->s) {
27  free(hr);
28  return NULL;
29  }
30  if (so->timeout > 0) {
31  rz_socket_block_time(hr->s, true, so->timeout, 0);
32  }
33  hr->auth = !so->httpauth;
34  for (;;) {
35 #if __WINDOWS__
36  if (breaked && *breaked) {
38  return NULL;
39  }
40 #endif
41  memset(buf, 0, sizeof(buf));
42  xx = rz_socket_gets(hr->s, buf, sizeof(buf));
43  yy = rz_socket_ready(hr->s, 0, 20 * 1000); // this function uses usecs as argument
44  // eprintf ("READ %d (%s) READY %d\n", xx, buf, yy);
45  if (!yy || (!xx && !pxx)) {
46  break;
47  }
48  pxx = xx;
49 
50  if (first == 0) {
51  first = 1;
52  if (strlen(buf) < 3) {
54  return NULL;
55  }
56  p = strchr(buf, ' ');
57  if (p) {
58  *p = 0;
59  }
60  hr->method = strdup(buf);
61  if (p) {
62  q = strstr(p + 1, " HTTP"); // strchr (p+1, ' ');
63  if (q) {
64  *q = 0;
65  }
66  hr->path = strdup(p + 1);
67  }
68  } else {
69  if (!hr->referer && !strncmp(buf, "Referer: ", 9)) {
70  hr->referer = strdup(buf + 9);
71  } else if (!hr->agent && !strncmp(buf, "User-Agent: ", 12)) {
72  hr->agent = strdup(buf + 12);
73  } else if (!hr->host && !strncmp(buf, "Host: ", 6)) {
74  hr->host = strdup(buf + 6);
75  } else if (!strncmp(buf, "Content-Length: ", 16)) {
76  content_length = atoi(buf + 16);
77  } else if (so->httpauth && !strncmp(buf, "Authorization: Basic ", 21)) {
78  char *authtoken = buf + 21;
79  size_t authlen = strlen(authtoken);
80  char *curauthtoken;
82  char *decauthtoken = calloc(4, authlen + 1);
83  if (!decauthtoken) {
84  eprintf("Could not allocate decoding buffer\n");
85  return hr;
86  }
87 
88  if (rz_base64_decode((ut8 *)decauthtoken, authtoken, authlen) == -1) {
89  eprintf("Could not decode authorization token\n");
90  } else {
91  rz_list_foreach (so->authtokens, iter, curauthtoken) {
92  if (!strcmp(decauthtoken, curauthtoken)) {
93  hr->auth = true;
94  break;
95  }
96  }
97  }
98 
99  free(decauthtoken);
100 
101  if (!hr->auth) {
102  eprintf("Failed attempt login from '%s'\n", hr->host);
103  }
104  }
105  }
106  }
107  if (content_length > 0) {
108  rz_socket_read_block(hr->s, (ut8 *)buf, 1); // one missing byte
109  if (ST32_ADD_OVFCHK(content_length, 1)) {
111  eprintf("Could not allocate hr data\n");
112  return NULL;
113  }
114  hr->data = malloc(content_length + 1);
115  hr->data_length = content_length;
116  rz_socket_read_block(hr->s, hr->data, hr->data_length);
117  hr->data[content_length] = 0;
118  }
119  return hr;
120 }
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
RZ_API int rz_base64_decode(ut8 *bout, const char *bin, int len)
Definition: ubase64.c:48
RZ_API int rz_socket_read_block(RzSocket *s, unsigned char *buf, int len)
Definition: socket.c:808
RZ_API int rz_socket_ready(RzSocket *s, int secs, int usecs)
Definition: socket.c:688
RZ_API bool rz_socket_block_time(RzSocket *s, bool block, int sec, int usec)
Definition: socket.c:649
RZ_API int rz_socket_gets(RzSocket *s, char *buf, int size)
Definition: socket.c:830
RZ_API RzSocket * rz_socket_accept_timeout(RzSocket *s, unsigned int timeout)
Definition: socket.c:626
#define ST32_ADD_OVFCHK(a, x)
static bool * breaked
RZ_API void rz_socket_http_close(RzSocketHTTPRequest *rs)

References rz_socket_http_options::accept_timeout, rz_socket_http_request::agent, rz_socket_http_request::auth, rz_socket_http_options::authtokens, breaked, calloc(), rz_socket_http_request::data, rz_socket_http_request::data_length, eprintf, free(), rz_socket_http_request::host, rz_socket_http_options::httpauth, malloc(), memset(), rz_socket_http_request::method, NULL, p, rz_socket_http_request::path, rz_socket_http_request::referer, rz_base64_decode(), RZ_NEW0, rz_socket_accept(), rz_socket_accept_timeout(), rz_socket_block_time(), rz_socket_gets(), rz_socket_http_close(), rz_socket_read_block(), rz_socket_ready(), s, rz_socket_http_request::s, ST32_ADD_OVFCHK, strdup(), and rz_socket_http_options::timeout.

Referenced by rz_core_rtr_http_run(), and rz_main_rz_agent().

◆ rz_socket_http_close()

RZ_API void rz_socket_http_close ( RzSocketHTTPRequest rs)

Definition at line 191 of file socket_http_server.c.

191  {
192  rz_socket_free(rs->s);
193  free(rs->path);
194  free(rs->host);
195  free(rs->agent);
196  free(rs->method);
197  free(rs->data);
198  free(rs);
199 }
#define rs()

References free(), rs, and rz_socket_free().

Referenced by rz_core_rtr_http_run(), rz_main_rz_agent(), and rz_socket_http_accept().

◆ rz_socket_http_get()

RZ_API char* rz_socket_http_get ( const char *  url,
int code,
int rlen 
)

Definition at line 287 of file socket_http.c.

287  {
289 }
url
Definition: setup.py:262
#define SOCKET_HTTP_MAX_REDIRECTS
Definition: socket_http.c:12
static char * socket_http_get_recursive(const char *url, int *code, int *rlen, ut32 redirections)
Definition: socket_http.c:191
Definition: inftree9.h:24

References socket_http_get_recursive(), SOCKET_HTTP_MAX_REDIRECTS, and setup::url.

Referenced by __open(), __read(), __rtr_shell(), __system(), __write(), download_and_write(), rtrcmd(), rz_core_rtr_add(), rz_core_rtr_cmd(), and rz_core_rtr_http_run().

◆ rz_socket_http_handle_upload()

RZ_API ut8* rz_socket_http_handle_upload ( const ut8 str,
int  len,
int olen 
)

Definition at line 144 of file socket_http_server.c.

144  {
145  if (retlen) {
146  *retlen = 0;
147  }
148  if (!strncmp((const char *)str, "----------", 10)) {
149  int datalen;
150  char *ret;
151  const char *data, *token = (const char *)str + 10;
152  const char *end = strchr(token, '\n');
153  if (!end) {
154  return NULL;
155  }
156  data = strstr(end, "Content-Disposition: form-data; ");
157  if (data) {
158  data = strchr(data, '\n');
159  if (data) {
160  data = strchr(data + 1, '\n');
161  }
162  }
163  if (data) {
164  while (*data == 10 || *data == 13) {
165  data++;
166  }
167  end = (const char *)str + len - 40;
168  while (*end == '-') {
169  end--;
170  }
171  if (*end == 10 || *end == 13) {
172  end--;
173  }
174  datalen = (size_t)(end - data);
175  ret = malloc(datalen + 1);
176  if (!ret) {
177  return NULL;
178  }
179  memcpy(ret, data, datalen);
180  ret[datalen] = 0;
181  if (retlen) {
182  *retlen = datalen;
183  }
184  return (ut8 *)ret;
185  }
186  }
187  return NULL;
188 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
int size_t
Definition: sftypes.h:40

References test_evm::end, len, malloc(), memcpy(), NULL, and cmd_descs_generate::str.

Referenced by rz_core_rtr_http_run().

◆ rz_socket_http_post()

RZ_API char* rz_socket_http_post ( const char *  url,
const char *  data,
int code,
int rlen 
)

Definition at line 291 of file socket_http.c.

291  {
292  RzSocket *s;
293  bool ssl = rz_str_startswith(url, "https://");
294  char *uri = strdup(url);
295  if (!uri) {
296  return NULL;
297  }
298 
299  char *host = strstr(uri, "://");
300  if (!host) {
301  free(uri);
302  printf("Invalid URI");
303  return NULL;
304  }
305  host += 3;
306  char *port = strchr(host, ':');
307  if (!port) {
308  port = (ssl) ? "443" : "80";
309  } else {
310  *port++ = 0;
311  }
312  char *path = strchr(host, '/');
313  if (!path) {
314  path = "";
315  } else {
316  *path++ = 0;
317  }
318  s = rz_socket_new(ssl);
319  if (!s) {
320  printf("Cannot create socket\n");
321  free(uri);
322  return NULL;
323  }
324  if (!rz_socket_connect_tcp(s, host, port, 0)) {
325  eprintf("Cannot connect to %s:%s\n", host, port);
326  free(uri);
327  return NULL;
328  }
329  /* Send */
331  "POST /%s HTTP/1.0\r\n"
332  "User-Agent: rizin " RZ_VERSION "\r\n"
333  "Accept: */*\r\n"
334  "Host: %s\r\n"
335  "Content-Length: %i\r\n"
336  "Content-Type: application/x-www-form-urlencoded\r\n"
337  "\r\n",
338  path, host, (int)strlen(data));
339  free(uri);
340  rz_socket_write(s, (void *)data, strlen(data));
341  return socket_http_answer(s, code, rlen, 0);
342 }
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
RZ_API void rz_socket_printf(RzSocket *s, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API int rz_socket_write(RzSocket *s, void *buf, int len)
Definition: socket.c:724
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
#define RZ_VERSION
Definition: rz_version.h:8
static char * socket_http_answer(RzSocket *s, int *code, int *rlen, ut32 redirections)
Definition: socket_http.c:34

References eprintf, free(), NULL, path, printf(), rz_socket_connect_tcp, rz_socket_new(), rz_socket_printf(), rz_socket_write(), rz_str_startswith(), RZ_VERSION, s, socket_http_answer(), strdup(), and setup::url.

◆ rz_socket_http_response()

RZ_API void rz_socket_http_response ( RzSocketHTTPRequest rs,
int  code,
const char *  out,
int  x,
const char *  headers 
)

Definition at line 122 of file socket_http_server.c.

122  {
123  const char *strcode =
124  code == 200 ? "ok" : code == 301 ? "Moved permanently"
125  : code == 302 ? "Found"
126  : code == 401 ? "Unauthorized"
127  : code == 403 ? "Permission denied"
128  : code == 404 ? "not found"
129  : "UNKNOWN";
130  if (len < 1) {
131  len = out ? strlen(out) : 0;
132  }
133  if (!headers) {
134  headers = code == 401 ? "WWW-Authenticate: Basic realm=\"R2 Web UI Access\"\n" : "";
135  }
136  rz_socket_printf(rs->s, "HTTP/1.0 %d %s\r\n%s"
137  "Connection: close\r\nContent-Length: %d\r\n\r\n",
138  code, strcode, headers, len);
139  if (out && len > 0) {
140  rz_socket_write(rs->s, (void *)out, len);
141  }
142 }
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528

References len, out, rs, rz_socket_printf(), and rz_socket_write().

Referenced by rz_core_rtr_http_run(), and rz_main_rz_agent().

◆ rz_socket_http_server_set_breaked()

RZ_API void rz_socket_http_server_set_breaked ( bool b)

Definition at line 9 of file socket_http_server.c.

9  {
10  breaked = b;
11 }

References b, and breaked.

Referenced by rz_core_rtr_http_stop().

◆ rz_socket_is_connected()

RZ_API bool rz_socket_is_connected ( RzSocket s)

Definition at line 101 of file socket.c.

101  {
102 #if __WINDOWS__
103  char buf[2];
104  rz_socket_block_time(s, false, 0, 0);
105 #ifdef _MSC_VER
106  int ret = recv(s->fd, (char *)&buf, 1, MSG_PEEK);
107 #else
108  ssize_t ret = recv(s->fd, (char *)&buf, 1, MSG_PEEK);
109 #endif
110  rz_socket_block_time(s, true, 0, 0);
111  return ret == 1;
112 #else
113  int error = 0;
114  socklen_t len = sizeof(error);
115  int ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len);
116  if (ret != 0) {
117  perror("getsockopt");
118  return false;
119  }
120  return (error == 0);
121 #endif
122 }
#define SO_ERROR
Definition: sftypes.h:432
int ssize_t
Definition: sftypes.h:39

References error(), rz_socket_t::fd, len, rz_socket_block_time(), s, SO_ERROR, and SOL_SOCKET.

Referenced by rz_socket_connect(), rz_socket_rap_server_continue(), and send_vcont().

◆ rz_socket_listen()

RZ_API bool rz_socket_listen ( RzSocket s,
const char *  port,
const char *  certfile 
)

Definition at line 474 of file socket.c.

474  {
475  int optval = 1;
476  int ret;
477  struct linger linger = { 0 };
478 
479  if (s->proto == RZ_SOCKET_PROTO_UNIX) {
480 #if __UNIX__
481  return __listen_unix(s, port);
482 #endif
483  return false;
484  }
485 
486 #if __WINDOWS__
487  WSADATA wsadata;
488  if (WSAStartup(MAKEWORD(1, 1), &wsadata) == SOCKET_ERROR) {
489  eprintf("Error creating socket.");
490  return false;
491  }
492 #endif
493  if (s->proto == RZ_SOCKET_PROTO_NONE) {
495  }
496  switch (s->proto) {
497  case RZ_SOCKET_PROTO_TCP:
499  return false;
500  }
501  break;
502  case RZ_SOCKET_PROTO_UDP:
504  return false;
505  }
506  break;
507  default:
508  eprintf("Invalid protocol for socket\n");
509  return false;
510  }
511 
512  linger.l_onoff = 1;
513  linger.l_linger = 1;
514  ret = setsockopt(s->fd, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
515  if (ret < 0) {
516  return false;
517  }
518  { // fix close after write bug //
519  int x = 1500; // FORCE MTU
520  ret = setsockopt(s->fd, SOL_SOCKET, SO_SNDBUF, (void *)&x, sizeof(int));
521  if (ret < 0) {
522  return false;
523  }
524  }
525  ret = setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&optval, sizeof optval);
526  if (ret < 0) {
527  return false;
528  }
529 
530  memset(&s->sa, 0, sizeof(s->sa));
531  s->sa.sin_family = AF_INET;
532  s->sa.sin_addr.s_addr = htonl(s->local ? INADDR_LOOPBACK : INADDR_ANY);
533  s->port = rz_socket_port_by_name(port);
534  if (s->port < 1) {
535  return false;
536  }
537  s->sa.sin_port = htons(s->port); // TODO honor etc/services
538  if (bind(s->fd, (struct sockaddr *)&s->sa, sizeof(s->sa)) < 0) {
539  rz_sys_perror("bind");
540 #ifdef _MSC_VER
541  closesocket(s->fd);
542 #else
543  close(s->fd);
544 #endif
545  return false;
546  }
547 #if __UNIX__
548  rz_sys_signal(SIGPIPE, SIG_IGN);
549 #endif
550  if (s->proto == RZ_SOCKET_PROTO_TCP) {
551  if (listen(s->fd, 32) < 0) {
552  rz_sys_perror("listen");
553 #ifdef _MSC_VER
554  closesocket(s->fd);
555 #else
556  close(s->fd);
557 #endif
558  return false;
559  }
560  }
561 #if HAVE_LIB_SSL
562  if (s->is_ssl) {
563  s->ctx = SSL_CTX_new(SSLv23_method());
564  if (!s->ctx) {
565  rz_socket_free(s);
566  return false;
567  }
568  if (!SSL_CTX_use_certificate_chain_file(s->ctx, certfile)) {
569  rz_socket_free(s);
570  return false;
571  }
572  if (!SSL_CTX_use_PrivateKey_file(s->ctx, certfile, SSL_FILETYPE_PEM)) {
573  rz_socket_free(s);
574  return false;
575  }
576  SSL_CTX_set_verify_depth(s->ctx, 1);
577  }
578 #endif
579  return true;
580 }
int x
Definition: mipsasm.c:20
static struct sockaddr static addrlen listen
Definition: sfsocketcall.h:116
#define SO_LINGER
Definition: sftypes.h:441
#define SO_REUSEADDR
Definition: sftypes.h:430
@ SOCK_DGRAM
Definition: sftypes.h:227
@ SOCK_STREAM
Definition: sftypes.h:224
#define SO_SNDBUF
Definition: sftypes.h:435

References AF_INET, bind, close, eprintf, rz_socket_t::fd, htons, rz_socket_t::is_ssl, listen, rz_socket_t::local, memset(), rz_socket_t::port, rz_socket_t::proto, RZ_INVALID_SOCKET, rz_socket_free(), rz_socket_port_by_name(), RZ_SOCKET_PROTO_DEFAULT, RZ_SOCKET_PROTO_NONE, RZ_SOCKET_PROTO_TCP, RZ_SOCKET_PROTO_UDP, RZ_SOCKET_PROTO_UNIX, rz_sys_perror, rz_sys_signal(), s, in_addr::s_addr, rz_socket_t::sa, sockaddr_in::sin_addr, sockaddr_in::sin_port, SO_LINGER, SO_REUSEADDR, SO_SNDBUF, SOCK_DGRAM, SOCK_STREAM, socket, SOL_SOCKET, and x.

Referenced by __rap_open(), rz_core_rtr_cmds(), rz_core_rtr_gdb_run(), rz_core_rtr_http_run(), rz_main_rz_agent(), rz_run_config_env(), rz_socket_rap_server_listen(), rz_write_from_socket_handler(), and tcpme().

◆ rz_socket_new()

RZ_API RzSocket* rz_socket_new ( bool  is_ssl)

Definition at line 179 of file socket.c.

179  {
181  if (!s) {
182  return NULL;
183  }
184  s->is_ssl = is_ssl;
185  s->port = 0;
186 #if __UNIX_
187  rz_sys_signal(SIGPIPE, SIG_IGN);
188 #endif
189  s->local = 0;
191 #if HAVE_LIB_SSL
192  if (is_ssl) {
193  s->sfd = NULL;
194  s->ctx = NULL;
195  s->bio = NULL;
196 #if OPENSSL_VERSION_NUMBER < 0x1010000fL
197  if (!SSL_library_init()) {
198  rz_socket_free(s);
199  return NULL;
200  }
201  SSL_load_error_strings();
202 #endif
203  }
204 #endif
205  return s;
206 }

References rz_socket_t::fd, rz_socket_t::is_ssl, rz_socket_t::local, NULL, rz_socket_t::port, RZ_INVALID_SOCKET, RZ_NEW0, rz_socket_free(), rz_sys_signal(), and s.

Referenced by __open(), __rap_open(), gdbr_init(), iob_net_open(), qnxr_connect(), rz_core_rtr_add(), rz_core_rtr_cmds(), rz_core_rtr_cmds_query(), rz_core_rtr_gdb_run(), rz_core_rtr_http_run(), rz_core_rtr_http_stop(), rz_main_rz_agent(), rz_run_config_env(), rz_socket_http_post(), rz_socket_rap_server_new(), rz_write_from_socket_handler(), socket_http_get_recursive(), and tcpme().

◆ rz_socket_new_from_fd()

RZ_API RzSocket* rz_socket_new_from_fd ( int  fd)

Definition at line 859 of file socket.c.

859  {
861  if (s) {
862  s->fd = fd;
864  }
865  return s;
866 }

References fd, rz_socket_t::fd, rz_socket_t::proto, RZ_NEW0, RZ_SOCKET_PROTO_DEFAULT, and s.

Referenced by __rap_accept().

◆ rz_socket_port_by_name()

RZ_API int rz_socket_port_by_name ( const char *  name)

Definition at line 469 of file socket.c.

469  {
470  struct servent *p = getservbyname(name, "tcp");
471  return (p && p->s_port) ? ntohs(p->s_port) : rz_num_get(NULL, name);
472 }
RZ_API ut64 rz_num_get(RzNum *num, const char *str)
Definition: unum.c:172
#define ntohs(x)
Definition: sftypes.h:478
Definition: z80asm.h:102

References ntohs, NULL, p, and rz_num_get().

Referenced by rz_socket_connect(), and rz_socket_listen().

◆ rz_socket_printf()

RZ_API void rz_socket_printf ( RzSocket s,
const char *  fmt,
  ... 
)

◆ rz_socket_proc_close()

RZ_API int rz_socket_proc_close ( RzSocketProc sp)

Definition at line 60 of file socket_proc.c.

60  {
61 #if __UNIX__
62  /* this is wrong */
63  kill(sp->pid, SIGKILL);
64  waitpid(sp->pid, NULL, 0); // WNOHANG);
65  close(sp->fd0[0]);
66  close(sp->fd0[1]);
67  // close(sp->fd1[0]);
68  close(sp->fd1[1]);
69  // sp->fd[0] = -1;
70  // sp->fd[1] = -1;
71 #endif
72  return 0;
73 }
static int sp
Definition: z80asm.c:91

References close, kill, NULL, SIGKILL, and sp.

Referenced by rz_socket_proc_open().

◆ rz_socket_proc_gets()

RZ_API int rz_socket_proc_gets ( RzSocketProc sp,
char *  buf,
int  size 
)

Definition at line 82 of file socket_proc.c.

82  {
83  RzSocket s;
84  s.is_ssl = false;
85  s.fd = sp->fd1[0];
86  return rz_socket_gets(&s, buf, size);
87 }

References rz_socket_t::fd, rz_socket_t::is_ssl, rz_socket_gets(), s, and sp.

◆ rz_socket_proc_open()

RZ_API RzSocketProc* rz_socket_proc_open ( char *const  argv[])

Definition at line 17 of file socket_proc.c.

17  {
18 #if __UNIX__ && HAVE_FORK
20 
21  if (!sp) {
22  return NULL;
23  }
24 
25  if (rz_sys_pipe(sp->fd0, true) == -1) {
26  perror("pipe");
27  goto error;
28  }
29 
30  if (rz_sys_pipe(sp->fd1, true) == -1) {
31  perror("pipe");
32  goto error;
33  }
34 
35  sp->pid = rz_sys_fork();
36  switch (sp->pid) {
37  case 0:
38  close(0);
39  dup2(sp->fd0[0], 0);
40  close(1);
41  dup2(sp->fd1[1], 1);
42  rz_sys_execv(argv[0], argv);
43  exit(1);
44  break;
45  case -1:
46  perror("fork");
48  goto error;
49  // rz_socket_block_time (sp, false, 0);
50  }
51  return sp;
52 error:
53  free(sp);
54  return NULL;
55 #else
56  return NULL;
57 #endif
58 }
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define RZ_NEW(x)
Definition: rz_types.h:285
RZ_API int rz_socket_proc_close(struct rz_socket_proc_t *sp)
Definition: socket_proc.c:60

References argv, close, dup2, error(), test-lz4-list::exit, free(), NULL, RZ_NEW, rz_socket_proc_close(), rz_sys_execv(), rz_sys_fork(), rz_sys_pipe(), and sp.

◆ rz_socket_proc_printf()

RZ_API void rz_socket_proc_printf ( RzSocketProc sp,
const char *  fmt,
  ... 
)

◆ rz_socket_proc_read()

RZ_API int rz_socket_proc_read ( RzSocketProc sp,
unsigned char *  buf,
int  len 
)

Definition at line 75 of file socket_proc.c.

75  {
76  RzSocket s;
77  s.is_ssl = false;
78  s.fd = sp->fd1[0];
79  return rz_socket_read(&s, buf, len);
80 }
RZ_API void RZ_API int rz_socket_read(RzSocket *s, ut8 *read, int len)
Definition: socket.c:783

References rz_socket_t::fd, rz_socket_t::is_ssl, len, rz_socket_read(), s, and sp.

◆ rz_socket_proc_ready()

RZ_API void RZ_API int rz_socket_proc_ready ( RzSocketProc sp,
int  secs,
int  usecs 
)

Definition at line 110 of file socket_proc.c.

110  {
111  RzSocket s;
112  s.is_ssl = false;
113  s.fd = sp->fd1[0];
114  return rz_socket_ready(&s, secs, usecs);
115 }

References rz_socket_t::fd, rz_socket_t::is_ssl, rz_socket_ready(), s, and sp.

◆ rz_socket_proc_write()

RZ_API int rz_socket_proc_write ( RzSocketProc sp,
void *  buf,
int  len 
)

Definition at line 89 of file socket_proc.c.

89  {
90  RzSocket s;
91  s.is_ssl = false;
92  s.fd = sp->fd0[1];
93  return rz_socket_write(&s, buf, len);
94 }

References rz_socket_t::fd, rz_socket_t::is_ssl, len, rz_socket_write(), s, and sp.

◆ rz_socket_puts()

RZ_API int rz_socket_puts ( RzSocket s,
char *  buf 
)

Definition at line 768 of file socket.c.

768  {
769  return rz_socket_write(s, buf, strlen(buf));
770 }
RZ_API int rz_socket_write(RzSocket *s, void *buf, int len)
Definition: socket.c:724

References rz_socket_write(), and s.

◆ 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 }
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
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 int rz_socket_flush(RzSocket *s)
Definition: socket.c:677
#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 ut8
Definition: dcpu16.h:8
Definition: gzappend.c:170

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

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

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 }
static ut32 rz_read_be32(const void *src)
Definition: rz_endian.h:87

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().

◆ rz_socket_rap_server_accept()

RZ_API RzSocket* rz_socket_rap_server_accept ( RzSocketRapServer rap_s)

Definition at line 48 of file socket_rap_server.c.

48  {
50  return rz_socket_accept(s->fd);
51 }

References rz_socket_t::fd, NULL, rz_return_val_if_fail, rz_socket_accept(), and s.

◆ rz_socket_rap_server_continue()

RZ_API bool rz_socket_rap_server_continue ( RzSocketRapServer rap_s)

Definition at line 53 of file socket_rap_server.c.

53  {
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 }
static ut64 rz_read_be64(const void *src)
Definition: rz_endian.h:108
RZ_API int rz_socket_close(RzSocket *s)
Definition: socket.c:419
RZ_API bool rz_socket_is_connected(RzSocket *)
Definition: socket.c:101
#define RZ_FREE(x)
Definition: rz_types.h:369
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References eprintf, fd, rz_socket_t::fd, i, NULL, RAP_PACKET_CLOSE, RAP_PACKET_CMD, RAP_PACKET_MAX, RAP_PACKET_OPEN, RAP_PACKET_READ, RAP_PACKET_REPLY, RAP_PACKET_SEEK, RAP_PACKET_WRITE, RZ_FREE, rz_read_be32(), rz_read_be64(), rz_return_val_if_fail, rz_socket_close(), rz_socket_flush(), rz_socket_is_connected(), rz_socket_read_block(), rz_socket_write(), rz_write_be32(), rz_write_be64(), s, and ut64().

◆ rz_socket_rap_server_create()

RZ_API RzSocketRapServer* rz_socket_rap_server_create ( const char *  pathname)

Definition at line 23 of file socket_rap_server.c.

23  {
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 }
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 RzSocketRapServer * rz_socket_rap_server_new(bool use_ssl, const char *port)

References NULL, pathname, rz_return_val_if_fail, and rz_socket_rap_server_new().

◆ rz_socket_rap_server_free()

RZ_API void rz_socket_rap_server_free ( RzSocketRapServer rap_s)

Definition at line 36 of file socket_rap_server.c.

36  {
37  if (s) {
39  free(s);
40  }
41 }

References rz_socket_t::fd, free(), rz_socket_free(), and s.

◆ rz_socket_rap_server_listen()

RZ_API bool rz_socket_rap_server_listen ( RzSocketRapServer rap_s,
const char *  certfile 
)

Definition at line 43 of file socket_rap_server.c.

43  {
44  rz_return_val_if_fail(s && s->port && *s->port, false);
45  return rz_socket_listen(s->fd, s->port, certfile);
46 }

References rz_socket_t::fd, rz_socket_t::port, rz_return_val_if_fail, rz_socket_listen(), and s.

◆ rz_socket_rap_server_new()

RZ_API RzSocketRapServer* rz_socket_rap_server_new ( bool  is_ssl,
const char *  port 
)

Definition at line 8 of file socket_rap_server.c.

8  {
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 }

References rz_socket_t::fd, free(), NULL, rz_socket_t::port, RZ_NEW0, rz_return_val_if_fail, rz_socket_free(), rz_socket_new(), s, and strdup().

Referenced by rz_socket_rap_server_create().

◆ rz_socket_read()

RZ_API void RZ_API int rz_socket_read ( RzSocket s,
ut8 read,
int  len 
)

Definition at line 783 of file socket.c.

783  {
784  if (!s) {
785  return -1;
786  }
787 #if HAVE_LIB_SSL
788  if (s->is_ssl) {
789  if (s->bio) {
790  return BIO_read(s->bio, buf, len);
791  }
792  return SSL_read(s->sfd, buf, len);
793  }
794 #endif
795  // int r = read (s->fd, buf, len);
796  int r = recv(s->fd, (char *)buf, len, 0);
797  D {
798  eprintf("READ ");
799  int i;
800  for (i = 0; i < len; i++) {
801  eprintf("%02x ", buf[i]);
802  }
803  eprintf("\n");
804  }
805  return r;
806 }
#define D
Definition: socket.c:16

References D, eprintf, rz_socket_t::fd, i, rz_socket_t::is_ssl, len, r, and s.

Referenced by __rap_system(), iob_net_read(), qnxr_read_packet(), read_packet(), runcmd(), rz_core_rtr_cmds(), rz_core_rtr_cmds_query(), rz_socket_gets(), rz_socket_proc_read(), rz_socket_read_block(), rz_socket_slurp(), and rz_write_from_socket_handler().

◆ rz_socket_read_block()

RZ_API int rz_socket_read_block ( RzSocket s,
unsigned char *  buf,
int  len 
)

Definition at line 808 of file socket.c.

808  {
809  int ret = 0;
810  for (ret = 0; ret < len;) {
811  int r = rz_socket_read(s, buf + ret, len - ret);
812  if (r == -1) {
813 #if HAVE_LIB_SSL
814  if (s->is_ssl && SSL_get_error(s->sfd, r) == SSL_ERROR_WANT_READ) {
815  if (rz_socket_ready(s, 1, 0) == 1) {
816  continue;
817  }
818  }
819 #endif
820  return -1;
821  }
822  if (r < 1) {
823  break;
824  }
825  ret += r;
826  }
827  return ret;
828 }

References rz_socket_t::is_ssl, len, r, rz_socket_read(), rz_socket_ready(), and s.

Referenced by __rap_system(), rz_core_rtr_cmd(), rz_core_serve(), rz_socket_http_accept(), rz_socket_rap_client_command(), rz_socket_rap_client_open(), rz_socket_rap_client_read(), rz_socket_rap_client_seek(), rz_socket_rap_client_write(), rz_socket_rap_server_continue(), socket_http_answer(), and socket_slurp().

◆ rz_socket_ready()

RZ_API int rz_socket_ready ( RzSocket s,
int  secs,
int  usecs 
)

Definition at line 688 of file socket.c.

688  {
689  fd_set rfds;
690  struct timeval tv = { secs, usecs };
691  if (s->fd == RZ_INVALID_SOCKET) {
692  return -1;
693  }
694  FD_ZERO(&rfds);
695  FD_SET(s->fd, &rfds);
696  return select(s->fd + 1, &rfds, NULL, NULL, &tv);
697 }

References rz_socket_t::fd, FD_SET, FD_ZERO, NULL, RZ_INVALID_SOCKET, s, select, and tv.

Referenced by qnxr_read_packet(), read_packet(), rz_socket_close(), rz_socket_http_accept(), rz_socket_proc_ready(), rz_socket_read_block(), and socket_slurp().

◆ rz_socket_slurp()

RZ_API ut8* rz_socket_slurp ( RzSocket s,
int len 
)

Definition at line 868 of file socket.c.

868  {
869  int blockSize = 4096;
870  ut8 *ptr, *buf = malloc(blockSize);
871  if (!buf) {
872  return NULL;
873  }
874  int copied = 0;
875  if (len) {
876  *len = 0;
877  }
878  for (;;) {
879  int rc = rz_socket_read(s, buf + copied, blockSize);
880  if (rc > 0) {
881  copied += rc;
882  }
883  ptr = realloc(buf, copied + blockSize);
884  if (!ptr) {
885  break;
886  }
887  buf = ptr;
888  if (rc < 1) {
889  break;
890  }
891  }
892  if (copied == 0) {
893  RZ_FREE(buf);
894  }
895  if (len) {
896  *len = copied;
897  }
898  return buf;
899 }
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144

References len, malloc(), NULL, realloc(), RZ_FREE, rz_socket_read(), and s.

Referenced by tcpme().

◆ rz_socket_spawn()

RZ_API bool rz_socket_spawn ( RzSocket s,
const char *  cmd,
unsigned int  timeout 
)

Definition at line 208 of file socket.c.

208  {
209  // XXX TODO: dont use sockets, we can achieve the same with pipes
210  const int port = 2000 + rz_num_rand(2000);
211  int childPid = rz_sys_fork();
212  if (childPid == 0) {
213  char *a = rz_str_replace(strdup(cmd), "\\", "\\\\", true);
214  int res = rz_sys_cmdf("rz-run system=\"%s\" listen=%d", a, port);
215  free(a);
216 #if 0
217  // TODO: use the api
218  char *profile = rz_str_newf (
219  "system=%s\n"
220  "listen=%d\n", cmd, port);
221  RzRunProfile *rp = rz_run_new (profile);
222  rz_run_start (rp);
223  rz_run_free (rp);
224  free (profile);
225 #endif
226  if (res != 0) {
227  eprintf("rz_socket_spawn: rz-run failed\n");
228  exit(1);
229  }
230  eprintf("rz_socket_spawn: %s is dead\n", cmd);
231  exit(0);
232  }
233  rz_sys_sleep(1);
235 
236  char aport[32];
237  sprintf(aport, "%d", port);
238  // redirect stdin/stdout/stderr
239  bool sock = rz_socket_connect(s, "127.0.0.1", aport, RZ_SOCKET_PROTO_TCP, 2000);
240  if (!sock) {
241  return false;
242  }
243 #if __UNIX__
244  rz_sys_sleep(4);
246 
247  int status = 0;
248  int ret = waitpid(childPid, &status, WNOHANG);
249  if (ret != 0) {
251  return false;
252  }
253 #endif
254  return true;
255 }
sprintf
Definition: kernel.h:365
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
RZ_API int rz_num_rand(int max)
Definition: unum.c:47
RZ_API void rz_run_free(RzRunProfile *r)
Definition: run.c:122
RZ_API RzRunProfile * rz_run_new(const char *str)
Definition: run.c:86
RZ_API int rz_run_start(RzRunProfile *p)
Definition: run.c:1081
RZ_API char * rz_str_replace(char *str, const char *key, const char *val, int g)
Definition: str.c:1110
RZ_API int rz_sys_cmdf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API int rz_sys_usleep(int usecs)
Sleep for usecs microseconds.
Definition: sys.c:317
#define a(i)
Definition: sha256.c:41
RZ_API bool rz_socket_connect(RzSocket *s, const char *host, const char *port, int proto, unsigned int timeout)
Definition: socket.c:257

References a, cmd, eprintf, test-lz4-list::exit, free(), rp, rz_num_rand(), rz_run_free(), rz_run_new(), rz_run_start(), rz_socket_close(), rz_socket_connect(), RZ_SOCKET_PROTO_TCP, rz_str_newf(), rz_str_replace(), rz_sys_cmdf(), rz_sys_fork(), rz_sys_sleep(), rz_sys_usleep(), s, sprintf, status, strdup(), and timeout.

Referenced by __open().

◆ rz_socket_to_string()

RZ_API char* rz_socket_to_string ( RzSocket s)

Definition at line 699 of file socket.c.

699  {
700 #if __WINDOWS__
701  return rz_str_newf("fd%d", (int)(size_t)s->fd);
702 #elif __UNIX__
703  char *str = NULL;
704  struct sockaddr sa;
705  socklen_t sl = sizeof(sa);
706  memset(&sa, 0, sizeof(sa));
707  if (!getpeername(s->fd, &sa, &sl)) {
708  struct sockaddr_in *sain = (struct sockaddr_in *)&sa;
709  ut8 *a = (ut8 *)&(sain->sin_addr);
710  if ((str = malloc(32))) {
711  sprintf(str, "%d.%d.%d.%d:%d",
712  a[0], a[1], a[2], a[3], ntohs(sain->sin_port));
713  }
714  } else {
715  eprintf("getperrname: failed\n"); // rz_sys_perror ("getpeername");
716  }
717  return str;
718 #else
719  return NULL;
720 #endif
721 }

References a, eprintf, rz_socket_t::fd, malloc(), memset(), ntohs, NULL, rz_str_newf(), s, sockaddr_in::sin_addr, sockaddr_in::sin_port, sprintf, and cmd_descs_generate::str.

Referenced by rz_core_rtr_http_run().

◆ rz_socket_write()

RZ_API int rz_socket_write ( RzSocket s,
void *  buf,
int  len 
)

Definition at line 724 of file socket.c.

724  {
725  D {
726  eprintf("WRITE ");
727  int i;
728  ut8 *b = buf;
729  for (i = 0; i < len; i++) {
730  eprintf("%02x ", b[i]);
731  }
732  eprintf("\n");
733  }
734  int ret, delta = 0;
735 #if __UNIX__
736  rz_sys_signal(SIGPIPE, SIG_IGN);
737 #endif
738  for (;;) {
739  int b = 1500; // 65536; // Use MTU 1500?
740  if (b > len) {
741  b = len;
742  }
743 #if HAVE_LIB_SSL
744  if (s->is_ssl) {
745  if (s->bio) {
746  ret = BIO_write(s->bio, buf + delta, b);
747  } else {
748  ret = SSL_write(s->sfd, buf + delta, b);
749  }
750  } else
751 #endif
752  {
753  ret = send(s->fd, (char *)buf + delta, b, 0);
754  }
755  // if (ret == 0) return -1;
756  if (ret < 1) {
757  break;
758  }
759  if (ret == len) {
760  return len;
761  }
762  delta += ret;
763  len -= ret;
764  }
765  return (ret == -1) ? -1 : delta;
766 }
static struct sockaddr static addrlen static backlog send
Definition: sfsocketcall.h:119
static st64 delta
Definition: vmenus.c:2425

References b, D, delta, eprintf, rz_socket_t::fd, i, rz_socket_t::is_ssl, len, rz_sys_signal(), s, and send.

Referenced by __rap_system(), _sendResponsePacket(), iob_net_write(), qnxr_send_ch_debug(), qnxr_send_ch_reset(), qnxr_send_ch_text(), qnxr_send_nak(), qnxr_send_packet(), rz_core_rtr_cmd(), rz_core_rtr_cmds(), rz_core_rtr_cmds_query(), rz_core_rtr_pushout(), rz_core_serve(), rz_socket_http_post(), rz_socket_http_response(), rz_socket_printf(), rz_socket_proc_printf(), rz_socket_proc_write(), rz_socket_puts(), rz_socket_rap_client_command(), rz_socket_rap_client_open(), rz_socket_rap_client_read(), rz_socket_rap_client_seek(), rz_socket_rap_client_write(), rz_socket_rap_server_continue(), send_ack(), send_packet(), and send_vcont().

◆ rzpipe_close()

RZ_API int rzpipe_close ( RzPipe rzpipe)

Definition at line 110 of file rzpipe.c.

110  {
111  if (!rzpipe) {
112  return 0;
113  }
114  /*
115  if (rzpipe->coreb.core && !rzpipe->coreb.puts) {
116  void (*rfre)(void *c) = rz_lib_dl_sym (librz, "rz_core_free");
117  if (rfre) {
118  rfre (rzpipe->coreb.core);
119  }
120  }
121  */
122 #if __WINDOWS__
123  if (rzpipe->pipe) {
124  CloseHandle(rzpipe->pipe);
125  rzpipe->pipe = NULL;
126  }
127 #else
128  if (rzpipe->input[0] != -1) {
129  rz_sys_pipe_close(rzpipe->input[0]);
130  rzpipe->input[0] = -1;
131  }
132  if (rzpipe->input[1] != -1) {
133  rz_sys_pipe_close(rzpipe->input[1]);
134  rzpipe->input[1] = -1;
135  }
136  if (rzpipe->output[0] != -1) {
137  rz_sys_pipe_close(rzpipe->output[0]);
138  rzpipe->output[0] = -1;
139  }
140  if (rzpipe->output[1] != -1) {
141  rz_sys_pipe_close(rzpipe->output[1]);
142  rzpipe->output[1] = -1;
143  }
144  if (rzpipe->child != -1) {
145  kill(rzpipe->child, SIGTERM);
146  waitpid(rzpipe->child, NULL, 0);
147  rzpipe->child = -1;
148  }
149 #endif
150  free(rzpipe);
151  return 0;
152 }
int child
Definition: rz_socket.h:52
int input[2]
Definition: rz_socket.h:56
int output[2]
Definition: rz_socket.h:57

References RzPipe::child, free(), RzPipe::input, kill, NULL, RzPipe::output, and rz_sys_pipe_close().

Referenced by __close(), and rzpipe_open().

◆ rzpipe_cmd()

RZ_API char* rzpipe_cmd ( RzPipe rzpipe,
const char *  str 
)

Definition at line 316 of file rzpipe.c.

316  {
317  rz_return_val_if_fail(rzp && str, NULL);
318  if (!*str || !rzpipe_write(rzp, str)) {
319  perror("rzpipe_write");
320  return NULL;
321  }
322  return rzpipe_read(rzp);
323 }
RZ_API char * rzpipe_read(RzPipe *rzpipe)
Definition: rzpipe.c:61
RZ_API int rzpipe_write(RzPipe *rzpipe, const char *str)
Definition: rzpipe.c:36

References NULL, rz_return_val_if_fail, rzpipe_read(), rzpipe_write(), and cmd_descs_generate::str.

Referenced by rzpipe_cmdf().

◆ rzpipe_cmdf()

RZ_API char* rzpipe_cmdf ( RzPipe rzpipe,
const char *  fmt,
  ... 
)

◆ rzpipe_open()

RZ_API RzPipe* rzpipe_open ( const char *  cmd)

Definition at line 235 of file rzpipe.c.

235  {
236  RzPipe *rzp = rzpipe_new();
237  if (!rzp) {
238  return NULL;
239  }
240  if (RZ_STR_ISEMPTY(cmd)) {
241  rzp->child = -1;
242  return rzp_open_spawn(rzp, cmd);
243  }
244 #if __WINDOWS__
245  w32_createPipe(rzp, cmd);
246  rzp->child = (int)(rzp->pipe);
247 #else
248  int r = rz_sys_pipe(rzp->input, false);
249  if (r != 0) {
250  eprintf("pipe failed on input\n");
251  rzpipe_close(rzp);
252  return NULL;
253  }
254  r = rz_sys_pipe(rzp->output, false);
255  if (r != 0) {
256  eprintf("pipe failed on output\n");
257  rzpipe_close(rzp);
258  return NULL;
259  }
260  rzp->child = rz_sys_fork();
261  if (rzp->child == -1) {
262  rzpipe_close(rzp);
263  return NULL;
264  }
265  env("RZ_PIPE_IN", rzp->input[0]);
266  env("RZ_PIPE_OUT", rzp->output[1]);
267 
268  if (rzp->child) {
269  signed char ch = -1;
270  // eprintf ("[+] rzpipeipe child is %d\n", rzpipe->child);
271  if (read(rzp->output[0], &ch, 1) != 1) {
272  eprintf("Failed to read 1 byte\n");
273  rzpipe_close(rzp);
274  return NULL;
275  }
276  if (ch == -1) {
277  eprintf("[+] rzpipe link error.\n");
278  rzpipe_close(rzp);
279  return NULL;
280  }
281  // Close parent's end of pipes
282  rz_sys_pipe_close(rzp->input[0]);
283  rz_sys_pipe_close(rzp->output[1]);
284  rzp->input[0] = -1;
285  rzp->output[1] = -1;
286  } else {
287  int rc = 0;
288  if (cmd && *cmd) {
289  close(0);
290  close(1);
291  dup2(rzp->input[0], 0);
292  dup2(rzp->output[1], 1);
293  rz_sys_pipe_close(rzp->input[1]);
294  rz_sys_pipe_close(rzp->output[0]);
295  rzp->input[1] = -1;
296  rzp->output[0] = -1;
297  rc = rz_sys_system(cmd);
298  if (rc != 0) {
299  eprintf("return code %d for %s\n", rc, cmd);
300  }
301  // trigger the blocking read
302  rz_xwrite(1, "\xff", 1);
303  rz_sys_pipe_close(rzp->output[1]);
304  close(0);
305  close(1);
306  }
307  rzp->child = -1;
308  rzpipe_close(rzp);
309  exit(rc);
310  return NULL;
311  }
312 #endif
313  return rzp;
314 }
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
#define rz_xwrite(fd, buf, count)
Definition: rz_types.h:642
static RzPipe * rzpipe_new(void)
Definition: rzpipe.c:197
RZ_API int rzpipe_close(RzPipe *rzpipe)
Definition: rzpipe.c:110
static void env(const char *s, int f)
Definition: rzpipe.c:29
static RzPipe * rzp_open_spawn(RzPipe *rzp, const char *cmd)
Definition: rzpipe.c:169
static int
Definition: sfsocketcall.h:114
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References RzPipe::child, close, cmd, dup2, env(), eprintf, test-lz4-list::exit, RzPipe::input, int, NULL, RzPipe::output, r, read(), RZ_STR_ISEMPTY, rz_sys_fork(), rz_sys_pipe(), rz_sys_pipe_close(), rz_sys_system(), rz_xwrite, rzp_open_spawn(), rzpipe_close(), and rzpipe_new().

Referenced by __open().

◆ rzpipe_open_corebind()

RZ_API RzPipe* rzpipe_open_corebind ( RzCoreBind coreb)

Definition at line 209 of file rzpipe.c.

209  {
210  RzPipe *rzpipe = rzpipe_new();
211  if (rzpipe) {
212  memcpy(&rzpipe->coreb, coreb, sizeof(RzCoreBind));
213  }
214  return rzpipe;
215 }
RzCoreBind coreb
Definition: rz_socket.h:59

References RzPipe::coreb, memcpy(), and rzpipe_new().

◆ rzpipe_open_dl()

RZ_API RzPipe* rzpipe_open_dl ( const char *  file)

Definition at line 217 of file rzpipe.c.

217  {
218  void *librz = rz_lib_dl_open(libr_path);
219  void *(*rnew)() = rz_lib_dl_sym(librz, "rz_core_new");
220  char *(*rcmd)(void *c, const char *cmd) = rz_lib_dl_sym(librz, "rz_core_cmd_str");
221 
222  if (rnew && rcmd) {
223  RzPipe *rzpipe = rzpipe_new();
224  if (rzpipe) {
225  rzpipe->coreb.core = rnew();
226  rzpipe->coreb.cmdstr = rcmd;
227  // rzpipe->coreb.free = rfre;
228  }
229  return rzpipe;
230  }
231  eprintf("Cannot resolve rz_core_cmd, rz_core_cmd_str, rz_core_free\n");
232  return NULL;
233 }
RzCoreCmdStr cmdstr
Definition: rz_bind.h:34
void * core
Definition: rz_bind.h:31

References c, cmd, rz_core_bind_t::cmdstr, rz_core_bind_t::core, RzPipe::coreb, eprintf, NULL, rz_lib_dl_open(), rz_lib_dl_sym(), and rzpipe_new().

◆ rzpipe_read()

RZ_API char* rzpipe_read ( RzPipe rzpipe)

Definition at line 61 of file rzpipe.c.

61  {
62  int bufsz = 0;
63  char *buf = NULL;
64  if (!rzpipe) {
65  return NULL;
66  }
67  bufsz = 4096;
68  buf = calloc(1, bufsz);
69  if (!buf) {
70  return NULL;
71  }
72 #if __WINDOWS__
73  BOOL bSuccess = FALSE;
74  DWORD dwRead = 0;
75  // TODO: handle > 4096 buffers here
76  bSuccess = ReadFile(rzpipe->pipe, buf, bufsz, &dwRead, NULL);
77  if (!bSuccess || !buf[0]) {
78  return NULL;
79  }
80  if (dwRead > 0) {
81  buf[dwRead] = 0;
82  }
83  buf[bufsz - 1] = 0;
84 #else
85  char *newbuf;
86  int i, rv;
87  for (i = 0; i < bufsz; i++) {
88  rv = read(rzpipe->output[0], buf + i, 1);
89  if (i + 2 >= bufsz) {
90  bufsz += 4096;
91  newbuf = realloc(buf, bufsz);
92  if (!newbuf) {
93  RZ_FREE(buf);
94  break;
95  }
96  buf = newbuf;
97  }
98  if (rv != 1 || !buf[i]) {
99  break;
100  }
101  }
102  if (buf) {
103  int zpos = (i < bufsz) ? i : i - 1;
104  buf[zpos] = 0;
105  }
106 #endif
107  return buf;
108 }
#define FALSE
Definition: mybfd.h:102
DWORD

References calloc(), DWORD, FALSE, i, NULL, RzPipe::output, read(), realloc(), and RZ_FREE.

Referenced by __read(), __system(), __write(), and rzpipe_cmd().

◆ rzpipe_write()

RZ_API int rzpipe_write ( RzPipe rzpipe,
const char *  str 
)

Definition at line 36 of file rzpipe.c.

36  {
37  char *cmd;
38  int ret, len;
39  if (!rzpipe || !str) {
40  return -1;
41  }
42  len = strlen(str) + 2; /* include \n\x00 */
43  cmd = malloc(len + 2);
44  if (!cmd) {
45  return 0;
46  }
47  memcpy(cmd, str, len - 1);
48  strcpy(cmd + len - 2, "\n");
49 #if __WINDOWS__
50  DWORD dwWritten = -1;
51  WriteFile(rzpipe->pipe, cmd, len, &dwWritten, NULL);
52  ret = (dwWritten == len);
53 #else
54  ret = (write(rzpipe->input[1], cmd, len) == len);
55 #endif
56  free(cmd);
57  return ret;
58 }

References cmd, DWORD, free(), RzPipe::input, len, malloc(), memcpy(), NULL, cmd_descs_generate::str, and write.

Referenced by __read(), __system(), __write(), and rzpipe_cmd().