* RSocket API refactor

* Fix RAnal vapi
This commit is contained in:
earada 2011-05-05 03:41:57 +02:00
parent ac786a03dd
commit 6eb3578025
11 changed files with 187 additions and 220 deletions

View File

@ -107,11 +107,15 @@ R_API void r_core_rtr_add(RCore *core, const char *_input) {
file = file+1;
port = ptr;
fd = r_socket_new (R_FALSE);
if (!fd) {
eprintf("Error: Cannot create new socket\n");
return;
}
switch (proto) {
case RTR_PROT_RAP:
fd = r_socket_new (host, port, R_FALSE); //TODO: Use rap.ssl
if (fd == NULL) {
eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port);
if (!r_socket_connect_tcp (fd, host, port)) { //TODO: Use rap.ssl
eprintf("Error: Cannot connect to '%s' (%s)\n", host, port);
return;
}
eprintf ("Connected to: %s at port %s\n", host, port);
@ -132,16 +136,14 @@ R_API void r_core_rtr_add(RCore *core, const char *_input) {
eprintf ("ok\n");
break;
case RTR_PROT_TCP:
fd = r_socket_new (host, port, R_FALSE); //TODO: Use rap.ssl
if (fd == NULL) {
eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port);
if (!r_socket_connect_tcp (fd, host, port)) { //TODO: Use rap.ssl
eprintf("Error: Cannot connect to '%s' (%s)\n", host, port);
return;
}
eprintf ("Connected to: %s at port %s\n", host, port);
break;
case RTR_PROT_UDP:
fd = r_socket_udp_connect (host, port, R_FALSE); //TODO: Use rap.ssl
if (fd == NULL) {
if (!r_socket_connect_udp(fd, host, port)) { //TODO: Use rap.ssl
eprintf("Error: Cannot connect to '%s' (%s)\n", host, port);
return;
}

View File

@ -3,6 +3,9 @@
#include "r_types.h"
#if __UNIX__
#include <netinet/in.h>
#endif
#if HAVE_LIB_SSL
#include <openssl/ssl.h>
#include <openssl/err.h>
@ -20,21 +23,22 @@ typedef struct r_socket_t {
#ifdef R_API
R_API RSocket *r_socket_new_from_fd (int fd);
R_API RSocket *r_socket_new (const char *host, const char *port, int is_ssl);
R_API void r_socket_free (RSocket *s);
R_API RSocket *r_socket_new (int is_ssl);
R_API int r_socket_connect (RSocket *s, const char *host, const char *port, int proto);
#define r_socket_connect_tcp(a,b,c) r_socket_connect(a,b,c,IPPROTO_TCP)
#define r_socket_connect_udp(a,b,c) r_socket_connect(a,b,c,IPPROTO_UDP)
R_API int r_socket_close (RSocket *s);
R_API int r_socket_free (RSocket *s);
#if __UNIX__
R_API RSocket *r_socket_unix_connect (const char *file);
R_API int r_socket_unix_connect (RSocket *s, const char *file);
R_API int r_socket_unix_listen (const char *file);
#endif
R_API int r_socket_connect (const char *host, const char *port);
R_API RSocket *r_socket_listen (const char *port, int is_ssl,const char *certfile);
R_API void r_socket_block (RSocket *s, int block);
R_API int r_socket_listen (RSocket *s, const char *port, const char *certfile);
R_API RSocket *r_socket_accept (RSocket *s);
R_API int r_socket_block_time (RSocket *s, int block, int sec);
R_API int r_socket_flush (RSocket *s);
R_API int r_socket_close (RSocket *s);
R_API int r_socket_ready (RSocket *s, int secs, int usecs);
R_API char *r_socket_to_string (RSocket *s);
R_API RSocket *r_socket_udp_connect (const char *host, const char *port, int is_ssl);
R_API int r_socket_write (RSocket *s, void *buf, int len);
R_API int r_socket_puts (RSocket *s, char *buf);
R_API void r_socket_printf (RSocket *s, const char *fmt, ...);

View File

@ -33,12 +33,14 @@ static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
return NULL;
}
*port = '\0';
_fd = r_socket_new (host, port+1, R_FALSE);
_fd = r_socket_new (R_FALSE);
if (_fd) {
riog = R_NEW (RIOGdb);
riog->fd = _fd;
riog->desc = gdbwrap_init (_fd->fd,NUM_REGS,4);
return r_io_desc_new (&r_io_plugin_gdb, _fd->fd, file, rw, mode, riog);
if (r_socket_connect_tcp (_fd, host, port+1)) {
riog = R_NEW (RIOGdb);
riog->fd = _fd;
riog->desc = gdbwrap_init (_fd->fd,NUM_REGS,4);
return r_io_desc_new (&r_io_plugin_gdb, _fd->fd, file, rw, mode, riog);
}
}
eprintf ("gdb.io.open: Cannot connect to host.\n");
return NULL;

View File

@ -83,7 +83,11 @@ static RIODesc *haret__open(struct r_io_t *io, const char *pathname, int rw, int
return NULL;
}
*port++ = 0;
if ((s = r_socket_new (ptr, port, R_FALSE)) == NULL) {
if ((s = r_socket_new (R_FALSE)) == NULL) {
eprintf ("Cannot create new socket\n");
return NULL;
}
if (!r_socket_connect_tcp (s, ptr, port)) {
eprintf ("Cannot connect to '%s' (%s)\n", ptr, port);
return NULL;
} else eprintf ("Connected to: %s at port %s\n", ptr, port);

View File

@ -151,14 +151,20 @@ static RIODesc *rap__open(struct r_io_t *io, const char *pathname, int rw, int m
eprintf ("rap: listening at port %s\n", port);
rior = R_NEW (RIORap);
rior->listener = R_TRUE;
rior->client = rior->fd = r_socket_listen (port, R_FALSE, NULL);
rior->client = rior->fd = r_socket_new (R_FALSE);
if (rior->fd == NULL)
return NULL;
if (!r_socket_listen (rior->fd, port, NULL))
return NULL;
// TODO: listen mode is broken.. here must go the root loop!!
#warning TODO: implement rap:/:9999 listen mode
return r_io_desc_new (&r_io_plugin_rap, rior->fd->fd, pathname, rw, mode, rior);
}
if ((rap_fd = r_socket_new (ptr, port, R_FALSE))==NULL) {
if ((rap_fd = r_socket_new (R_FALSE)) == NULL) {
eprintf ("Cannot create new socket\n");
return NULL;
}
if (r_socket_connect_tcp (rap_fd, ptr, port)) {
eprintf ("Cannot connect to '%s' (%d)\n", ptr, p);
return NULL;
}

View File

@ -4,7 +4,7 @@
static char *r_socket_http_response (RSocket *s, int *code) {
char buf[32768];
char *p, *q;
char *p;
int i, len;
/* Read Header */
@ -22,7 +22,7 @@ static char *r_socket_http_response (RSocket *s, int *code) {
p = strstr (buf, "Content-Length: ");
len = (p)?atoi (p+16):0;
/* Read Content */
len = r_socket_read_block (s, buf+i, len);
len = r_socket_read_block (s, (unsigned char *)buf+i, len);
r_socket_close (s);
return strdup (buf);
}
@ -50,9 +50,14 @@ R_API char *r_socket_http_get (const char *url, int *code) {
path = "";
else
*path++ = 0;
s = r_socket_new (host, port, ssl);
s = r_socket_new (ssl);
if (!s) {
printf ("Cannot connect\n");
printf ("Cannot create socket\n");
free (uri);
return NULL;
}
if (!r_socket_connect_tcp (s, host, port)) {
printf ("Cannot connect to %s:%s\n", host, port);
free (uri);
return NULL;
}
@ -91,9 +96,14 @@ R_API char *r_socket_http_post (const char *url, const char *data, int *code) {
path = "";
else
*path++ = 0;
s = r_socket_new (host, port, ssl);
s = r_socket_new (ssl);
if (!s) {
printf ("Cannot connect\n");
printf ("Cannot create socket\n");
free (uri);
return NULL;
}
if (!r_socket_connect_tcp (s, host, port)) {
printf ("Cannot connect to %s:%s\n", host, port);
free (uri);
return NULL;
}

View File

@ -49,7 +49,7 @@ R_API struct r_socket_proc_t *r_socket_proc_open(char *const argv[]) {
r_socket_proc_close (sp);
free (sp);
break;
//r_socket_block(sp, R_FALSE);
//r_socket_block_time (sp, R_FALSE, 0);
}
return sp;
#else

View File

@ -24,13 +24,9 @@
#define BUFFER_SIZE 4096
R_API RSocket *r_socket_new (const char *host, const char *port, int is_ssl) {
R_API RSocket *r_socket_new (int is_ssl) {
RSocket *s = R_NEW (RSocket);
s->is_ssl = is_ssl;
if ((s->fd = r_socket_connect (host, port)) < 0) {
free (s);
return NULL;
}
#if HAVE_LIB_SSL
if (is_ssl) {
s->sfd = NULL;
@ -41,35 +37,100 @@ R_API RSocket *r_socket_new (const char *host, const char *port, int is_ssl) {
return NULL;
}
SSL_load_error_strings ();
s->ctx = SSL_CTX_new (SSLv23_client_method ());
if (s->ctx == NULL) {
r_socket_free (s);
return NULL;
}
s->sfd = SSL_new (s->ctx);
SSL_set_fd (s->sfd, s->fd);
if (SSL_connect (s->sfd) != 1) {
r_socket_free (s);
return NULL;
}
}
#endif
return s;
}
R_API void r_socket_free (RSocket *s) {
r_socket_close (s);
R_API int r_socket_connect (RSocket *s, const char *host, const char *port, int proto) {
struct addrinfo hints, *res, *rp;
int gai;
#if __WINDOWS__
WSADATA wsadata;
if (WSAStartup (MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
eprintf ("Error creating socket.");
return R_FALSE;
}
#elif __UNIX__
signal (SIGPIPE, SIG_IGN);
#endif
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_protocol = proto;
gai = getaddrinfo (host, port, &hints, &res);
if (gai != 0) {
eprintf ("Error in getaddrinfo: %s\n", gai_strerror (gai));
return R_FALSE;
}
for (rp = res; rp != NULL; rp = rp->ai_next) {
s->fd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (s->fd == -1)
continue;
if (connect (s->fd, rp->ai_addr, rp->ai_addrlen) != -1)
break;
close (s->fd);
}
if (rp == NULL) {
eprintf ("Could not connect\n");
return R_FALSE;
}
freeaddrinfo (res);
#if HAVE_LIB_SSL
if (s->is_ssl) {
s->ctx = SSL_CTX_new (SSLv23_client_method ());
if (s->ctx == NULL) {
r_socket_free (s);
return R_FALSE;
}
s->sfd = SSL_new (s->ctx);
SSL_set_fd (s->sfd, s->fd);
if (SSL_connect (s->sfd) != 1) {
r_socket_free (s);
return R_FALSE;
}
}
#endif
return R_TRUE;
}
R_API int r_socket_close (RSocket *s) {
int ret;
#if __WINDOWS__
WSACleanup ();
ret = closesocket (s->fd);
#else
shutdown (s->fd, SHUT_RDWR);
ret = close (s->fd);
#endif
#if HAVE_LIB_SSL
if (s->is_ssl && s->sfd)
SSL_shutdown (s->sfd);
#endif
return ret;
}
R_API int r_socket_free (RSocket *s) {
int res = r_socket_close (s);
#if HAVE_LIB_SSL
if (s->is_ssl) {
if (s->sfd)
SSL_free (s->sfd);
if (s->ctx)
SSL_CTX_free (s->ctx);
}
#endif
free (s);
return res;
}
#if __UNIX__
R_API RSocket *r_socket_unix_connect(const char *file) {
RSocket *s = R_NEW (RSocket);
R_API int r_socket_unix_connect(RSocket *s, const char *file) {
struct sockaddr_un addr;
int sock = socket (PF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
free (s);
return NULL;
return R_FALSE;
}
// TODO: set socket options
addr.sun_family = AF_UNIX;
@ -78,11 +139,11 @@ R_API RSocket *r_socket_unix_connect(const char *file) {
if (connect (sock, (struct sockaddr *)&addr, sizeof(addr))==-1) {
close (sock);
free (s);
return NULL;
return R_FALSE;
}
s->fd =sock;
s->fd = sock;
s->is_ssl = R_FALSE;
return s;
return R_TRUE;
}
R_API int r_socket_unix_listen(const char *file) {
@ -116,100 +177,50 @@ R_API int r_socket_unix_listen(const char *file) {
}
#endif
R_API int r_socket_connect(const char *host, const char *port) {
struct addrinfo hints, *res, *rp;
int s, gai;
#if __WINDOWS__
WSADATA wsadata;
if (WSAStartup (MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
eprintf ("Error creating socket.");
return -1;
}
#elif __UNIX__
signal (SIGPIPE, SIG_IGN);
#endif
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_protocol = IPPROTO_TCP; /* Any protocol */
gai = getaddrinfo (host, port, &hints, &res);
if (gai != 0) {
eprintf ("Error in getaddrinfo: %s\n", gai_strerror (gai));
return -1;
}
for (rp = res; rp != NULL; rp = rp->ai_next) {
s = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (s == -1)
continue;
if (connect (s, rp->ai_addr, rp->ai_addrlen) != -1)
break;
close (s);
}
if (rp == NULL) {
eprintf ("Could not connect\n");
return -1;
}
freeaddrinfo (res);
return s;
}
R_API RSocket *r_socket_listen(const char *port, int is_ssl, const char *certfile) {
RSocket *s;
int fd;
R_API int r_socket_listen (RSocket *s, const char *port, const char *certfile) {
struct sockaddr_in sa;
struct linger linger = { 0 };
if ((fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
return NULL;
if ((s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
return R_FALSE;
linger.l_onoff = 1;
linger.l_linger = 1;
setsockopt (fd, SOL_SOCKET, SO_LINGER, (const char *) &linger, sizeof (linger));
setsockopt (s->fd, SOL_SOCKET, SO_LINGER, (const char *)&linger, sizeof (linger));
memset (&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons (atoi (port));
if (bind (fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
close (fd);
return NULL;
if (bind (s->fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
close (s->fd);
return R_FALSE;
}
#if __UNIX_
signal (SIGPIPE, SIG_IGN);
#endif
if (listen (fd, 1) < 0) {
close (fd);
return NULL;
if (listen (s->fd, 1) < 0) {
close (s->fd);
return R_FALSE;
}
s = R_NEW (RSocket);
s->fd = fd;
s->is_ssl = is_ssl;
#if HAVE_LIB_SSL
if (is_ssl) {
s->sfd = NULL;
s->ctx = NULL;
s->bio = NULL;
if (!SSL_library_init ()) {
r_socket_free (s);
return NULL;
}
SSL_load_error_strings ();
if (s->is_ssl) {
s->ctx = SSL_CTX_new (SSLv23_method ());
if (s->ctx == NULL) {
r_socket_free (s);
return NULL;
return R_FALSE;
}
if (!SSL_CTX_use_certificate_chain_file (s->ctx, certfile)) {
r_socket_free (s);
return NULL;
return R_FALSE;
}
if (!SSL_CTX_use_PrivateKey_file (s->ctx, certfile, SSL_FILETYPE_PEM)) {
r_socket_free (s);
return NULL;
return R_FALSE;
}
SSL_CTX_set_verify_depth (s->ctx, 1);
}
#endif
return s;
return R_TRUE;
}
R_API RSocket *r_socket_accept(RSocket *s) {
@ -241,12 +252,20 @@ R_API RSocket *r_socket_accept(RSocket *s) {
return sock;
}
R_API void r_socket_block(RSocket *s, int block) {
R_API int r_socket_block_time (RSocket *s, int block, int sec) {
struct timeval sv;
#if __UNIX__
fcntl (s->fd, F_SETFL, O_NONBLOCK, !block);
#elif __WINDOWS__
ioctlsocket (s->fd, FIONBIO, (u_long FAR*)&block);
#endif
if (sec > 0) {
sv.tv_sec = sec;
sv.tv_usec = 0;
if (setsockopt (s->fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&sv, sizeof (sv)) < 0)
return R_FALSE;
}
return R_TRUE;
}
R_API int r_socket_flush(RSocket *s) {
@ -257,28 +276,6 @@ R_API int r_socket_flush(RSocket *s) {
return R_TRUE;
}
R_API int r_socket_close(RSocket *s) {
int ret;
#if __WINDOWS__
WSACleanup ();
ret = closesocket (s->fd);
#else
shutdown (s->fd, SHUT_RDWR);
ret = close (s->fd);
#endif
#if HAVE_LIB_SSL
if (s->is_ssl) {
if (s->sfd) {
SSL_shutdown (s->sfd);
SSL_free (s->sfd);
}
if (s->ctx)
SSL_CTX_free (s->ctx);
}
#endif
return ret;
}
// XXX: rewrite it to use select //
/* waits secs until new data is received. */
/* returns -1 on error, 0 is false, 1 is true */
@ -332,72 +329,6 @@ R_API char *r_socket_to_string(RSocket *s) {
#endif
}
//XXX: Merge with r_new
R_API RSocket *r_socket_udp_connect(const char *host, const char *port, int is_ssl) {
struct addrinfo hints, *res, *rp;
int s, gai;
RSocket *sock;
#if __WINDOWS__
WSADATA wsadata;
if (WSAStartup (MAKEWORD (1,1), &wsadata) == SOCKET_ERROR) {
eprintf ("Error creating socket.");
return NULL;
}
#elif __UNIX__
signal (SIGPIPE, SIG_IGN);
#endif
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_protocol = IPPROTO_UDP; /* Any protocol */
gai = getaddrinfo (host, port, &hints, &res);
if (gai != 0) {
eprintf ("Error in getaddrinfo: %s\n", gai_strerror (gai));
return NULL;
}
for (rp = res; rp != NULL; rp = rp->ai_next) {
s = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (s == -1)
continue;
if (connect (s, rp->ai_addr, rp->ai_addrlen) != -1)
break;
close (s);
}
if (rp == NULL) {
eprintf ("Could not connect\n");
return NULL;
}
freeaddrinfo (res);
sock = R_NEW (RSocket);
sock->fd = s;
sock->is_ssl = is_ssl;
#if HAVE_LIB_SSL
if (is_ssl) {
sock->sfd = NULL;
sock->ctx = NULL;
sock->bio = NULL;
if (!SSL_library_init ()) {
r_socket_free (sock);
return NULL;
}
SSL_load_error_strings ();
sock->ctx = SSL_CTX_new (SSLv23_client_method ());
if (sock->ctx == NULL) {
r_socket_free (sock);
return NULL;
}
sock->sfd = SSL_new (sock->ctx);
SSL_set_fd (sock->sfd, sock->fd);
if (SSL_connect (sock->sfd) != 1) {
r_socket_free (sock);
return NULL;
}
}
#endif
return sock;
}
/* Read/Write functions */
R_API int r_socket_write(RSocket *s, void *buf, int len) {
int ret, delta = 0;

View File

@ -10,8 +10,8 @@ int main (int argc, char ** argv) {
eprintf ("Use %s <cert>\n", argv[0]);
return 1;
}
s = r_socket_listen (PORT, 1, argv[1]);
if (s == NULL) {
s = r_socket_new (R_TRUE);
if (!r_socket_listen (s, PORT, argv[1])) {
eprintf ("Error, cant listen at port: %s\n", PORT);
return 1;
}

View File

@ -8,8 +8,12 @@ int main (int argc, char ** argv) {
ut8 buf [MAX_LINE+1];
memset (buf, 0, MAX_LINE+1);
RSocket *s = r_socket_new (SERVER, PORT, 1);
RSocket *s = r_socket_new (R_TRUE);
if (s == NULL) {
fprintf (stderr, "Error, cannot create new socket \n");
return 1;
}
if (!r_socket_connect_tcp (s, SERVER, PORT)) {
fprintf (stderr, "Error, cannot connect to "SERVER"\n");
return 1;
}

View File

@ -149,12 +149,12 @@ public class RAnal {
UNK,
NOP,
MOV,
TRAP,
SWI,
TRAP,
SWI,
UPUSH,
PUSH,
POP,
CMP,
PUSH,
POP,
CMP,
ADD,
SUB,
MUL,
@ -166,7 +166,7 @@ public class RAnal {
XOR,
NOT,
STORE,
LOAD,
LOAD,
//LAST
}
@ -182,9 +182,13 @@ public class RAnal {
public RList<RAnal.Op> ops;
}
public void bb (Block bb, uint64 addr, uint8 *buf, uint64 len, bool head);
public Block* bb_from_offset (uint64 addr);
[Compact]
[CCode (cprefix="r_anal_op_", cname="RAnalOp")]
public class Op {
public string mnemonic;
public uint64 addr;
public int type;
public int stackop;
@ -268,7 +272,7 @@ public class RAnal {
public int type;
public string str;
}
public RList<RMeta.Item> data;
[CCode (cname="int", cprefix="R_META_WHERE_")]
@ -287,7 +291,7 @@ public class RAnal {
COMMENT
}
//public int count (RMeta.Type type, uint64 from, uint64 to,
//public int count (RMeta.Type type, uint64 from, uint64 to,
//public string get_string(RMeta.Type, uint64 addr);
public bool @add(RMeta.Type type, uint64 from, uint64 size, string str);
public bool del(RMeta.Type type, uint64 from, uint64 size, string str);