radare2/libr/socket/rap_server.c

152 lines
4.1 KiB
C
Raw Normal View History

/* radare - LGPL - Copyright 2014-2016 - condret */
2014-05-05 10:32:10 +00:00
#include <r_socket.h>
#include <string.h>
#include <r_util.h>
2014-05-05 10:32:10 +00:00
R_API RSocketRapServer *r_socket_rap_server_new (int is_ssl, const char *port) {
2014-05-05 10:32:10 +00:00
RSocketRapServer *rap_s;
if (!port) {
2014-05-05 10:32:10 +00:00
return NULL;
}
2014-05-05 10:32:10 +00:00
rap_s = R_NEW0 (RSocketRapServer);
if (!rap_s) {
return NULL;
}
2014-05-05 10:32:10 +00:00
rap_s->fd = r_socket_new (is_ssl);
memcpy (rap_s->port, port, 4);
if (rap_s->fd) {
2014-05-05 10:32:10 +00:00
return rap_s;
}
2014-05-05 10:32:10 +00:00
free (rap_s);
return NULL;
}
R_API RSocketRapServer *r_socket_rap_server_create (const char *pathname) {
const char *port = NULL;
2014-05-05 10:32:10 +00:00
int is_ssl;
if (!pathname) {
return NULL;
}
if (strlen (pathname) < 11) {
return NULL;
}
if (strncmp (pathname, "rap", 3)) {
return NULL;
}
2014-05-05 10:32:10 +00:00
is_ssl = (pathname[3] == 's');
port = &pathname[7 + is_ssl];
return r_socket_rap_server_new (is_ssl, port);
}
R_API void r_socket_rap_server_free (RSocketRapServer *rap_s) {
if (rap_s) {
r_socket_free (rap_s->fd);
}
2014-05-05 10:32:10 +00:00
free (rap_s);
}
R_API int r_socket_rap_server_listen (RSocketRapServer *rap_s, const char *certfile) {
if (!rap_s || !*rap_s->port) {
2016-07-12 20:15:19 +00:00
return false;
}
2014-05-05 10:32:10 +00:00
return r_socket_listen (rap_s->fd, rap_s->port, certfile);
}
2014-06-04 20:33:56 +00:00
R_API RSocket* r_socket_rap_server_accept (RSocketRapServer *rap_s) {
2014-05-05 22:25:29 +00:00
if (!rap_s || !rap_s->fd) {
eprint ("error: r_socket_rap_server_accept");
2014-06-04 20:33:56 +00:00
return NULL;
2014-05-05 10:32:10 +00:00
}
2014-05-05 22:25:29 +00:00
return r_socket_accept (rap_s->fd);
2014-05-05 10:32:10 +00:00
}
R_API bool r_socket_rap_server_continue (RSocketRapServer *rap_s) {
2016-08-15 00:37:45 +00:00
int i, whence, ret = true;
2014-05-05 10:32:10 +00:00
ut64 offset;
2014-05-05 22:25:29 +00:00
char *ptr = NULL;
if (!rap_s || !rap_s->fd) {
return false;
}
if (!r_socket_is_connected (rap_s->fd)) {
return false;
}
2014-05-05 22:25:29 +00:00
r_socket_read_block (rap_s->fd, rap_s->buf, 1);
2014-05-05 10:32:10 +00:00
ret = rap_s->buf[0];
2016-08-15 00:37:45 +00:00
switch (ret) {
case RAP_RMT_OPEN:
r_socket_read_block (rap_s->fd, &rap_s->buf[1], 2);
r_socket_read_block (rap_s->fd, &rap_s->buf[3], (int)rap_s->buf[2]);
rap_s->open (rap_s->user, (const char *)&rap_s->buf[3], (int)rap_s->buf[1], 0);
rap_s->buf[0] = RAP_RMT_OPEN | RAP_RMT_REPLY;
r_socket_write (rap_s->fd, rap_s->buf, 5);
r_socket_flush (rap_s->fd);
break;
case RAP_RMT_READ:
r_socket_read_block (rap_s->fd, &rap_s->buf[1], 4);
i = r_read_be32 (&rap_s->buf[1]);
if (i > RAP_RMT_MAX || i < 0) {
i = RAP_RMT_MAX;
}
rap_s->read (rap_s->user, &rap_s->buf[5], i);
rap_s->buf[0] = RAP_RMT_READ | RAP_RMT_REPLY;
r_socket_write (rap_s->fd, rap_s->buf, i + 5);
r_socket_flush (rap_s->fd);
break;
case RAP_RMT_WRITE:
2016-08-15 00:37:45 +00:00
r_socket_read_block (rap_s->fd, rap_s->buf + 1, 4);
i = r_read_be32 (rap_s->buf + 1);
if (i > RAP_RMT_MAX || i < 0) {
i = RAP_RMT_MAX;
2016-08-15 00:37:45 +00:00
}
r_socket_read_block (rap_s->fd, rap_s->buf + 5, i);
int ret = rap_s->write (rap_s->user, rap_s->buf + 5, i);
r_write_be32 (rap_s->buf + 1, ret);
rap_s->buf[0] = RAP_RMT_WRITE | RAP_RMT_REPLY;
r_socket_write (rap_s->fd, rap_s->buf, 5);
r_socket_flush (rap_s->fd);
break;
case RAP_RMT_SEEK:
r_socket_read_block (rap_s->fd, &rap_s->buf[1], 9);
2016-08-15 00:37:45 +00:00
whence = rap_s->buf[1];
2016-08-16 01:21:50 +00:00
offset = r_read_be64 (rap_s->buf + 2);
2016-08-15 00:37:45 +00:00
offset = rap_s->seek (rap_s->user, offset, whence);
/* prepare reply */
rap_s->buf[0] = RAP_RMT_SEEK | RAP_RMT_REPLY;
2016-08-15 00:37:45 +00:00
r_write_be64 (rap_s->buf + 1, offset);
r_socket_write (rap_s->fd, rap_s->buf, 9);
r_socket_flush (rap_s->fd);
break;
case RAP_RMT_CMD:
r_socket_read_block (rap_s->fd, &rap_s->buf[1], 4);
i = r_read_be32 (&rap_s->buf[1]);
r_socket_read_block (rap_s->fd, &rap_s->buf[5], i);
ptr = rap_s->cmd (rap_s->user, (const char *)&rap_s->buf[5]);
i = (ptr)? strlen (ptr) + 1: 0;
r_write_be32 (&rap_s->buf[1], i);
rap_s->buf[0] = RAP_RMT_CMD | RAP_RMT_REPLY;
r_socket_write (rap_s->fd, rap_s->buf, 5);
if (i) {
r_socket_write (rap_s->fd, ptr, i);
}
r_socket_flush (rap_s->fd);
free (ptr);
ptr = NULL;
break;
case RAP_RMT_CLOSE:
r_socket_read_block (rap_s->fd, &rap_s->buf[1], 4);
i = r_read_be32 (&rap_s->buf[1]);
rap_s->close (rap_s->user, i);
rap_s->buf[0] = RAP_RMT_CLOSE | RAP_RMT_REPLY;
r_socket_write (rap_s->fd, rap_s->buf, 5);
r_socket_flush (rap_s->fd);
break;
default:
eprintf ("unknown command 0x%02x\n", (ut8)(rap_s->buf[0] & 0xff));
r_socket_close (rap_s->fd);
ret = false;
break;
2014-05-05 10:32:10 +00:00
}
return ret;
}