Some socket cleanups

This commit is contained in:
pancake 2023-09-20 11:54:16 +02:00 committed by pancake
parent 5ba64265fb
commit fdbde0a1cc
4 changed files with 45 additions and 44 deletions

View File

@ -108,9 +108,9 @@ R_API int r_socket_connect_serial(RSocket *sock, const char *path, int speed, in
#endif
R_API bool r_socket_listen(RSocket *s, const char *port, const char *certfile);
R_API int r_socket_port_by_name(const char *name);
R_API int r_socket_close_fd(RSocket *s);
R_API int r_socket_close(RSocket *s);
R_API int r_socket_free(RSocket *s);
R_API bool r_socket_close_fd(RSocket *s);
R_API bool r_socket_close(RSocket *s);
R_API void r_socket_free(RSocket *s);
R_API RSocket *r_socket_accept(RSocket *s);
R_API RSocket *r_socket_accept_timeout(RSocket *s, unsigned int timeout);
R_API bool r_socket_block_time(RSocket *s, bool block, int sec, int usec);

View File

@ -1,4 +1,6 @@
/* radare - MIT - Copyright 2011-2020 - pancake */
/* radare - MIT - Copyright 2011-2023 - pancake */
#define R_LOG_ORIGIN "io.rap"
#include <r_io.h>
#include <r_lib.h>
@ -29,23 +31,23 @@ static int __rap_read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
return r_socket_rap_client_read (s, buf, count);
}
static bool __rap_close(RIODesc *fd) {
int ret = false;
if (RIORAP_IS_VALID (fd)) {
if (RIORAP_FD (fd)) {
RIORap *r = fd->data;
if (r && fd->fd != -1) {
if (r->fd) {
(void)r_socket_close (r->fd);
static bool __rap_close(RIODesc *desc) {
bool ret = false;
if (RIORAP_IS_VALID (desc)) {
if (RIORAP_FD (desc)) {
RIORap *rap = desc->data;
if (rap && desc->fd != -1) {
if (rap->fd) {
r_socket_close (rap->fd);
}
if (r->client) {
ret = r_socket_close (r->client) != -1;
if (rap->client) {
r_socket_close (rap->client);
}
R_FREE (r);
free (rap);
}
}
} else {
R_LOG_ERROR ("__rap_close: fdesc is not a r_io_rap plugin");
R_LOG_ERROR ("fdesc is not a r_io_rap plugin");
}
return ret;
}
@ -60,8 +62,8 @@ static bool __rap_plugin_open(RIO *io, const char *pathname, bool many) {
}
static RIODesc *__rap_open(RIO *io, const char *pathname, int rw, int mode) {
int i, p, listenmode;
char *file, *port;
int i, listenmode;
char *port;
if (!__rap_plugin_open (io, pathname, 0)) {
return NULL;
@ -77,22 +79,19 @@ static RIODesc *__rap_open(RIO *io, const char *pathname, int rw, int mode) {
if (!*port) {
return NULL;
}
p = atoi (port);
if ((file = strchr (port + 1, '/'))) {
*file = 0;
file++;
}
int p = atoi (port);
char *file = r_str_after (port + 1, '/');
if (r_sandbox_enable (0)) {
R_LOG_ERROR ("sandbox: Cannot use network");
return NULL;
}
if (listenmode) {
if (p <= 0) {
R_LOG_ERROR ("rap: cannot listen here. Try rap://:9999");
R_LOG_ERROR ("cannot listen. Try rap://:9999");
return NULL;
}
//TODO: Handle ^C signal (SIGINT, exit); // ???
R_LOG_INFO ("rap: listening at port %s ssl %s", port, is_ssl? "on": "off");
// TODO: Handle ^C signal (SIGINT, exit); // ???
R_LOG_INFO ("listening at port %s ssl %s", port, is_ssl? "on": "off");
RIORap *rior = R_NEW0 (RIORap);
rior->listener = true;
rior->client = rior->fd = r_socket_new (is_ssl);
@ -101,7 +100,7 @@ static RIODesc *__rap_open(RIO *io, const char *pathname, int rw, int mode) {
return NULL;
}
if (is_ssl) {
if (file && *file) {
if (R_STR_ISNOTEMPTY (file)) {
if (!r_socket_listen (rior->fd, port, file)) {
r_socket_free (rior->fd);
free (rior);
@ -140,7 +139,7 @@ static RIODesc *__rap_open(RIO *io, const char *pathname, int rw, int mode) {
}
rior->listener = false;
rior->client = rior->fd = s;
if (file && *file) {
if (R_STR_ISNOTEMPTY (file)) {
i = r_socket_rap_client_open (s, file, rw);
if (i == -1) {
free (rior);
@ -269,7 +268,6 @@ static char *__rap_system(RIO *io, RIODesc *fd, const char *command) {
}
#endif
#endif
return NULL;
}

View File

@ -32,14 +32,13 @@ R_API bool r_socket_connect(RSocket *s, const char *host, const char *port, int
R_API bool r_socket_spawn(RSocket *s, const char *cmd, unsigned int timeout) {
return -1;
}
R_API int r_socket_close_fd(RSocket *s) {
return -1;
R_API bool r_socket_close_fd(RSocket *s) {
return false;
}
R_API int r_socket_close(RSocket *s) {
return -1;
R_API bool r_socket_close(RSocket *s) {
return false;
}
R_API int r_socket_free(RSocket *s) {
return -1;
R_API void r_socket_free(RSocket *s) {
}
R_API int r_socket_port_by_name(const char *name) {
return -1;
@ -470,7 +469,7 @@ success:
}
/* close the file descriptor associated with the RSocket s */
R_API int r_socket_close_fd(RSocket *s) {
R_API bool r_socket_close_fd(RSocket *s) {
#ifdef _MSC_VER
return s->fd != INVALID_SOCKET ? closesocket (s->fd) : false;
#else
@ -479,7 +478,7 @@ R_API int r_socket_close_fd(RSocket *s) {
}
/* shutdown the socket and close the file descriptor */
R_API int r_socket_close(RSocket *s) {
R_API bool r_socket_close(RSocket *s) {
int ret = false;
if (!s) {
return false;
@ -513,8 +512,8 @@ R_API int r_socket_close(RSocket *s) {
}
/* shutdown the socket, close the file descriptor and free the RSocket */
R_API int r_socket_free(RSocket *s) {
int res = r_socket_close (s);
R_API void r_socket_free(RSocket *s) {
(void)r_socket_close (s);
#if HAVE_LIB_SSL
if (s && s->is_ssl) {
if (s->sfd) {
@ -526,7 +525,6 @@ R_API int r_socket_free(RSocket *s) {
}
#endif
free (s);
return res;
}
R_API int r_socket_port_by_name(const char *name) {

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2011-2020 - pancake */
/* radare - LGPL - Copyright 2011-2023 - pancake */
#include <r_socket.h>
#include <r_util.h>
@ -181,11 +181,16 @@ R_API int r_socket_rap_client_seek(RSocket *s, ut64 offset, int whence) {
tmp[0] = RAP_PACKET_SEEK;
tmp[1] = (ut8)whence;
r_write_be64 (tmp + 2, offset);
(void)r_socket_write (s, &tmp, 10);
int ret = r_socket_write (s, &tmp, 10);
if (ret != 10) {
R_LOG_ERROR ("Truncated socket write %d vs %d", ret, 10);
r_sys_backtrace ();
return -1;
}
r_socket_flush (s);
int ret = r_socket_read_block (s, (ut8*)&tmp, 9);
ret = r_socket_read_block (s, (ut8*)&tmp, 9);
if (ret != 9) {
R_LOG_ERROR ("Truncated socket read");
R_LOG_ERROR ("Truncated socket read %d vs %d", ret, 9);
return -1;
}
if (tmp[0] != (RAP_PACKET_SEEK | RAP_PACKET_REPLY)) {