* Huge r_io refactoring

- Many things are broken in this commit
This commit is contained in:
pancake 2011-01-20 22:52:16 +01:00
parent 0283c92f6e
commit 39f77a430f
21 changed files with 585 additions and 540 deletions

2
TODO
View File

@ -5,6 +5,8 @@
<{include libr/TODO}>
* jk in visual mode for disassembly print format must scroll by opcodes, not bytes
* Add support for aout binaries?
* eprintf should be modified to log into a file
- eprintf_open() -- start log to file

View File

@ -442,7 +442,7 @@ reaccept:
r_socket_read_block (c, ptr, cmd); //filename
ptr[cmd] = 0;
r_core_file_open (core, (const char *)ptr, R_IO_READ); // XXX: mode write?
pipefd = core->file->fd;
pipefd = core->file->fd->fd;
eprintf("(flags: %hhd) len: %hhd filename: '%s'\n",
flg, cmd, ptr); //config.file);
}

View File

@ -189,8 +189,8 @@ R_API RCoreFile *r_core_file_open(RCore *r, const char *file, int mode) {
RCoreFile *fh;
const char *cp;
char *p;
int fd = r_io_open (r->io, file, mode, 0644);
if (fd == -1)
RIODesc *fd = r_io_open (r->io, file, mode, 0644);
if (fd == NULL)
return NULL;
fh = R_NEW (RCoreFile);
@ -227,7 +227,7 @@ R_API struct r_core_file_t *r_core_file_get_fd(struct r_core_t *core, int fd) {
struct list_head *pos;
list_for_each_prev (pos, &core->files) {
RCoreFile *file = list_entry (pos, RCoreFile, list);
if (file->fd == fd)
if (file->fd->fd == fd)
return file;
}
return NULL;
@ -238,7 +238,7 @@ R_API int r_core_file_list(struct r_core_t *core) {
struct list_head *pos;
list_for_each_prev (pos, &core->files) {
RCoreFile *f = list_entry (pos, RCoreFile, list);
eprintf ("%d %s\n", f->fd, f->uri);
eprintf ("%d %s\n", f->fd->fd, f->uri);
count++;
}
return count;

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2010 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */
#include "r_core.h"
@ -120,7 +120,7 @@ R_API int r_core_read_at(RCore *core, ut64 addr, ut8 *buf, int size) {
int ret;
if (!core->io || !core->file || size<1)
return R_FALSE;
r_io_set_fd (core->io, core->file->fd); // XXX ignore ret?
r_io_set_fd (core->io, core->file->fd); // XXX ignore ret? -- ultra slow method.. inverse resolution of io plugin brbrb
ret = r_io_read_at (core->io, addr, buf, size);
if (addr>=core->offset && addr<=core->offset+core->blocksize)
r_core_block_read (core, 0);

View File

@ -56,7 +56,7 @@ typedef struct r_core_file_t {
ut64 seek;
ut64 size;
int rwx;
int fd;
RIODesc *fd;
int dbg;
RBinObj *obj;
struct list_head list;

View File

@ -40,6 +40,19 @@ typedef struct r_io_map_t {
struct list_head list;
} RIOMap;
typedef struct r_io_desc_t {
int fd;
int flags;
int state;
char *name;
void *data;
struct r_io_plugin_t *plugin;
} RIODesc;
// enum?
#define R_IO_DESC_TYPE_OPENED 1
#define R_IO_DESC_TYPE_CLOSED 0
/* stores write and seek changes */
#define R_IO_UNDOS 64
typedef struct r_io_undo_t {
@ -65,7 +78,7 @@ typedef struct r_io_undo_w_t {
} RIOUndoWrite;
typedef struct r_io_t {
int fd;
RIODesc *fd;
int enforce_rwx;
int enforce_seek;
int cached;
@ -85,7 +98,7 @@ typedef struct r_io_t {
struct list_head sections;
/* maps */
RList *maps; /*<RIOMap>*/
struct list_head desc;
RList *desc;
struct list_head cache;
} RIO;
@ -98,20 +111,19 @@ typedef struct r_io_plugin_t {
char *name;
char *desc;
void *widget;
int (*listener)(RIO *io);
int (*listener)(RIODesc *io);
int (*init)();
struct r_io_undo_t undo;
struct debug_t *debug; // ???
int (*system)(RIO *io, int fd, const char *);
int (*open)(RIO *io, const char *, int rw, int mode);
int (*read)(RIO *io, int fd, ut8 *buf, int count);
ut64 (*lseek)(RIO *io, int fildes, ut64 offset, int whence);
int (*write)(RIO *io, int fd, const ut8 *buf, int count);
int (*close)(RIO *io, int fd);
int (*resize)(RIO *io, int fd, ut64 size);
int (*system)(RIO *io, RIODesc *fd, const char *);
RIODesc* (*open)(RIO *io, const char *, int rw, int mode);
int (*read)(RIO *io, RIODesc *fd, ut8 *buf, int count);
ut64 (*lseek)(RIO *io, RIODesc *fd, ut64 offset, int whence);
int (*write)(RIO *io, RIODesc *fd, const ut8 *buf, int count);
int (*close)(RIODesc *desc);
int (*resize)(RIO *io, RIODesc *fd, ut64 size);
int (*plugin_open)(RIO *io, const char *);
//int (*plugin_fd)(RIO *, int);
int fds[R_IO_NFDS];
} RIOPlugin;
typedef struct r_io_list_t {
@ -152,14 +164,6 @@ typedef struct r_io_cache_t {
struct list_head list;
} RIOCache;
typedef struct r_io_desc_t {
int fd;
int flags;
char name[4096];
struct r_io_plugin_t *plugin;
struct list_head list;
} RIODesc;
#ifdef R_API
#define r_io_bind_init(x) memset(&x,0,sizeof(x))
@ -179,10 +183,11 @@ R_API struct r_io_plugin_t *r_io_plugin_resolve_fd(RIO *io, int fd);
/* io/io.c */
R_API int r_io_set_write_mask(RIO *io, const ut8 *buf, int len);
R_API int r_io_open(RIO *io, const char *file, int flags, int mode);
R_API int r_io_open_as(RIO *io, const char *urihandler, const char *file, int flags, int mode);
R_API RIODesc *r_io_open(RIO *io, const char *file, int flags, int mode);
R_API RIODesc *r_io_open_as(RIO *io, const char *urihandler, const char *file, int flags, int mode);
R_API int r_io_redirect(RIO *io, const char *file);
R_API int r_io_set_fd(RIO *io, int fd);
R_API int r_io_set_fd(RIO *io, RIODesc *fd);
R_API int r_io_set_fdn(RIO *io, int fd);
R_API RBuffer *r_io_read_buf(RIO *io, ut64 addr, int len);
R_API int r_io_read(RIO *io, ut8 *buf, int len);
R_API int r_io_read_at(RIO *io, ut64 addr, ut8 *buf, int len);
@ -191,7 +196,7 @@ R_API int r_io_write(RIO *io, const ut8 *buf, int len);
R_API int r_io_write_at(RIO *io, ut64 addr, const ut8 *buf, int len);
R_API ut64 r_io_seek(RIO *io, ut64 offset, int whence);
R_API int r_io_system(RIO *io, const char *cmd);
R_API int r_io_close(RIO *io, int fd);
R_API int r_io_close(RIO *io, RIODesc *fd);
R_API ut64 r_io_size(RIO *io, int fd);
R_API int r_io_resize(struct r_io_t *io, ut64 newsize);
@ -254,13 +259,17 @@ R_API void r_io_wundo_set_all(RIO *io, int set);
R_API int r_io_wundo_set(RIO *io, int n, int set);
/* io/desc.c */
R_API int r_io_desc_init(RIO *io);
R_API int r_io_desc_add(RIO *io, int fd, const char *file, int flags, struct r_io_plugin_t *plugin);
R_API void r_io_desc_init(RIO *io);
R_API void r_io_desc_fini(RIO *io);
R_API RIODesc *r_io_desc_new(RIOPlugin *plugin, int fd, const char *name, int flags, int mode, void *data);
R_API void r_io_desc_free(RIODesc *desc);
//R_API void r_io_desc_add(RIO *io, RIODesc *desc);
R_API int r_io_desc_del(struct r_io_t *io, int fd);
R_API RIODesc *r_io_desc_get(RIO *io, int fd);
R_API void r_io_desc_add(RIO *io, RIODesc *desc); //int fd, const char *file, int flags, struct r_io_plugin_t *plugin);
R_API int r_io_desc_del(RIO *io, int fd);
R_API struct r_io_desc_t *r_io_desc_get(RIO *io, int fd);
R_API int r_io_desc_generate(RIO *io);
#undef r_io_desc_free
#define r_io_desc_free(x) free(x)
/* plugins */
extern struct r_io_plugin_t r_io_plugin_procpid;

View File

@ -57,6 +57,8 @@ typedef void (*PrintfCallback)(const char *str, ...);
#define ZERO_FILL(x) memset (x, 0, sizeof (x))
#define R_NEWS(x,y) (x*)malloc(sizeof(x)*y)
#define R_NEW(x) (x*)malloc(sizeof(x))
// TODO: Make R_NEW_COPY be 1 arg, not two
#define R_NEW_COPY(x,y) x=(y*)malloc(sizeof(y));memcpy(x,y,sizeof(y))
#define IS_PRINTABLE(x) (x>=' '&&x<='~')
#define IS_WHITESPACE(x) (x==' '||x=='\t')

View File

@ -1,51 +1,76 @@
/* radare - LGPL - Copyright 2009-2010 pancake<nopcode.org> */
#include <r_io.h>
// TODO: to be deprecated.. this is slow and boring
R_API int r_io_desc_init(struct r_io_t *io) {
INIT_LIST_HEAD (&io->desc);
return R_TRUE;
R_API void r_io_desc_init(RIO *io) {
io->desc = r_list_new ();
io->desc->free = r_io_desc_free;
}
R_API int r_io_desc_add(struct r_io_t *io, int fd, const char *file, int flags, struct r_io_plugin_t *plugin) {
R_API void r_io_desc_fini(RIO *io) {
r_list_free (io->desc);
}
R_API RIODesc *r_io_desc_new(RIOPlugin *plugin, int fd, const char *name, int flags, int mode, void *data) {
RIODesc *desc = R_NEW (RIODesc);
if (desc == NULL)
return R_FALSE;
strncpy (desc->name, file, sizeof (desc->name));
desc->flags = flags;
desc->fd = fd;
desc->plugin = plugin;
list_add_tail (&(desc->list), &(io->desc));
return R_TRUE;
if (desc != NULL) {
desc->state = R_IO_DESC_TYPE_OPENED;
desc->name = strdup (name);
if (desc->name != NULL) {
desc->plugin = plugin;
desc->flags = flags;
if (fd == -1)
desc->fd = (int) ((size_t) desc) & 0xffffff;
else desc->fd = fd;
desc->data = data;
} else {
free (desc);
desc = NULL;
}
}
return desc;
}
R_API void r_io_desc_free(RIODesc *desc) {
if (desc->plugin && desc->plugin->close)
desc->plugin->close (desc);
free (desc->name);
free (desc);
}
R_API void r_io_desc_add(RIO *io, RIODesc *desc) {
r_list_append (io->desc, desc);
}
R_API int r_io_desc_del(struct r_io_t *io, int fd) {
int ret = R_FALSE;
struct list_head *pos;
list_for_each_prev (pos, &io->desc) {
struct r_io_desc_t *d = list_entry (pos, struct r_io_desc_t, list);
RListIter *iter;
RIODesc *d;
r_list_foreach (io->desc, iter, d) {
if (d->fd == fd) {
list_del ((&d->list));
ret = R_TRUE;
break;
r_list_delete (io->desc, iter);
return R_TRUE;
}
}
return ret;
return R_FALSE;
}
R_API struct r_io_desc_t *r_io_desc_get(RIO *io, int fd) {
struct list_head *pos;
list_for_each_prev (pos, &io->desc) {
RIODesc *d = list_entry (pos, RIODesc, list);
R_API RIODesc *r_io_desc_get(RIO *io, int fd) {
RListIter *iter;
RIODesc *d;
r_list_foreach (io->desc, iter, d) {
if (d->fd == fd)
return d;
}
return NULL;
}
#if 0
// XXX: This must be deprecated in order to promote the cast of dataptr to ut32
R_API int r_io_desc_generate(struct r_io_t *io) {
int fd;
do fd = 0xf000 + rand ()%0xfff;
while (r_io_desc_get(io, fd));
return fd;
}
#endif

View File

@ -10,7 +10,7 @@
R_API struct r_io_t *r_io_new() {
RIO *io = R_NEW (struct r_io_t);
if (io) {
io->fd = -1;
io->fd = NULL;
io->write_mask_fd = -1;
io->redirect = NULL;
io->printf = (void*) printf;
@ -27,7 +27,7 @@ R_API struct r_io_t *r_io_new() {
R_API int r_io_is_listener(RIO *io) {
if (io && io->plugin && io->plugin->listener)
return io->plugin->listener (io);
return io->plugin->listener (io->fd);
return R_FALSE;
}
@ -57,13 +57,13 @@ R_API int r_io_redirect(struct r_io_t *io, const char *file) {
return 0;
}
R_API int r_io_open_as(struct r_io_t *io, const char *urihandler, const char *file, int flags, int mode) {
int ret;
R_API RIODesc *r_io_open_as(struct r_io_t *io, const char *urihandler, const char *file, int flags, int mode) {
RIODesc *ret;
char *uri;
int urilen = strlen (urihandler);
uri = malloc (strlen (urihandler)+strlen (file)+5);
if (uri == NULL)
return -1;
return NULL;
if (urilen>0)
sprintf (uri, "%s://", urihandler);
else *uri = '\0';
@ -73,25 +73,30 @@ R_API int r_io_open_as(struct r_io_t *io, const char *urihandler, const char *fi
return ret;
}
R_API int r_io_open(struct r_io_t *io, const char *file, int flags, int mode) {
R_API RIODesc *r_io_open(struct r_io_t *io, const char *file, int flags, int mode) {
RIODesc *desc = NULL;
int fd = -2;
char *uri = strdup (file);
struct r_io_plugin_t *plugin;
if (io != NULL) {
for (;;) {
plugin = r_io_plugin_resolve (io, uri);
if (plugin) {
fd = plugin->open (io, uri, flags, mode);
if (io->redirect) {
free ((void *)uri);
uri = strdup (io->redirect);
r_io_redirect (io, NULL);
continue;
if (plugin && plugin->open) {
desc = plugin->open (io, uri, flags, mode);
if (desc != NULL) {
r_io_desc_add (io, desc);
fd = desc->fd;
if (io->redirect) {
free ((void *)uri);
uri = strdup (io->redirect);
r_io_redirect (io, NULL);
continue;
}
if (fd != -1)
r_io_plugin_open (io, fd, plugin);
if (desc != io->fd)
io->plugin = plugin;
}
if (fd != -1)
r_io_plugin_open (io, fd, plugin);
if (fd != io->fd)
io->plugin = plugin;
}
break;
}
@ -111,26 +116,38 @@ R_API int r_io_open(struct r_io_t *io, const char *file, int flags, int mode) {
#endif
}
if (fd >= 0) {
r_io_set_fd (io, fd);
r_io_desc_add (io, fd, file, flags, io->plugin);
} else fd = -1;
desc = r_io_desc_new (io->plugin, fd, file, flags, mode, io->plugin);
r_io_desc_add (io, desc);
r_io_set_fd (io, desc);
}
free ((void *)uri);
return fd;
return desc;
}
// TODO: Rename to use_fd ?
R_API int r_io_set_fd(RIO *io, int fd) {
if (fd != -1 && fd != io->fd) {
io->plugin = r_io_plugin_resolve_fd (io, fd);
R_API int r_io_set_fd(RIO *io, RIODesc *fd) {
if (fd != NULL) {
io->fd = fd;
io->plugin = fd->plugin;
return R_TRUE;
}
return io->fd;
return R_FALSE;
}
R_API int r_io_set_fdn(RIO *io, int fd) {
if (fd != -1 && io->fd != NULL && fd != io->fd->fd) {
RIODesc *desc = r_io_desc_get (io, fd);
if (desc) {
io->fd = desc;
io->plugin = desc->plugin;
return R_TRUE;
}
}
return R_FALSE;
}
R_API int r_io_read(struct r_io_t *io, ut8 *buf, int len) {
int ret;
if (io==NULL)
if (io==NULL || io->fd == NULL)
return -1;
/* check section permissions */
if (io->enforce_rwx && !(r_io_section_get_rwx (io, io->off) & R_IO_READ))
@ -160,9 +177,9 @@ R_API int r_io_read(struct r_io_t *io, ut8 *buf, int len) {
}
if (io->plugin && io->plugin->read) {
if (io->plugin->read != NULL)
ret = io->plugin->read(io, io->fd, buf, len);
else eprintf ("IO plugin for fd=%d has no read()\n", io->fd);
} else ret = read (io->fd, buf, len);
ret = io->plugin->read (io, io->fd, buf, len);
else eprintf ("IO plugin for fd=%d has no read()\n", io->fd->fd);
} else ret = read (io->fd->fd, buf, len);
if (ret>0 && ret<len)
memset (buf+ret, 0xff, len-ret);
}
@ -192,14 +209,14 @@ R_API ut64 r_io_read_i(struct r_io_t *io, ut64 addr, int sz, int endian) {
R_API int r_io_resize(struct r_io_t *io, ut64 newsize) {
if (io->plugin && io->plugin->resize)
return io->plugin->resize (io, io->fd, newsize);
else ftruncate (io->fd, newsize);
else ftruncate (io->fd->fd, newsize);
return R_FALSE;
}
R_API int r_io_set_write_mask(struct r_io_t *io, const ut8 *buf, int len) {
int ret = R_FALSE;
if (len) {
io->write_mask_fd = io->fd;
io->write_mask_fd = io->fd->fd;
io->write_mask_buf = (ut8 *)malloc (len);
memcpy (io->write_mask_buf, buf, len);
io->write_mask_len = len;
@ -245,9 +262,9 @@ R_API int r_io_write(struct r_io_t *io, const ut8 *buf, int len) {
if (io->plugin->write)
ret = io->plugin->write (io, io->fd, buf, len);
else eprintf ("r_io_write: io handler with no write callback\n");
} else ret = write (io->fd, buf, len);
} else ret = write (io->fd->fd, buf, len);
if (ret == -1)
eprintf ("r_io_write: cannot write on fd %d\n", io->fd);
eprintf ("r_io_write: cannot write on fd %d\n", io->fd->fd);
} else ret = len;
if (data)
free (data);
@ -270,7 +287,7 @@ R_API ut64 r_io_seek(struct r_io_t *io, ut64 offset, int whence) {
posix_whence = SEEK_SET;
break;
case R_IO_SEEK_CUR:
offset += io->off;
// offset += io->off;
posix_whence = SEEK_CUR;
break;
case R_IO_SEEK_END:
@ -282,23 +299,25 @@ R_API ut64 r_io_seek(struct r_io_t *io, ut64 offset, int whence) {
offset = (!io->debug && io->va && !list_empty (&io->sections))?
r_io_section_vaddr_to_offset (io, offset) : offset;
// TODO: implement io->enforce_seek here!
if (io->plugin && io->plugin->lseek)
ret = io->plugin->lseek (io, io->fd, offset, whence);
// XXX can be problematic on w32..so no 64 bit offset?
else ret = lseek (io->fd, offset, posix_whence);
if (ret != -1) {
io->off = ret;
// XXX this can be tricky.. better not to use this .. must be deprecated
// r_io_sundo_push (io);
ret = (!io->debug && io->va && !list_empty (&io->sections))?
r_io_section_offset_to_vaddr (io, io->off) : io->off;
}
if (io->fd != NULL) {
if (io->plugin && io->plugin->lseek)
ret = io->plugin->lseek (io, io->fd, offset, whence);
// XXX can be problematic on w32..so no 64 bit offset?
else ret = lseek (io->fd->fd, offset, posix_whence);
if (ret != -1) {
io->off = ret;
// XXX this can be tricky.. better not to use this .. must be deprecated
// r_io_sundo_push (io);
ret = (!io->debug && io->va && !list_empty (&io->sections))?
r_io_section_offset_to_vaddr (io, io->off) : io->off;
} else eprintf ("r_io_seek: cannot seek to %"PFMT64x"\n", offset);
} else eprintf ("r_io_seek: null fd\n");
return ret;
}
R_API ut64 r_io_size(RIO *io, int fd) {
ut64 size, here;
r_io_set_fd (io, fd);
r_io_set_fdn (io, fd);
here = r_io_seek (io, 0, R_IO_SEEK_CUR);
size = r_io_seek (io, 0, R_IO_SEEK_END);
r_io_seek (io, here, R_IO_SEEK_SET);
@ -313,17 +332,20 @@ R_API int r_io_system(RIO *io, const char *cmd) {
}
// TODO: remove int fd here???
R_API int r_io_close(struct r_io_t *io, int fd) {
fd = r_io_set_fd (io, fd);
if (fd != -1 && io->plugin) {
r_io_desc_del (io, fd);
r_io_map_del (io, fd);
r_io_plugin_close (io, fd, io->plugin);
if (io->plugin->close)
return io->plugin->close (io, fd);
R_API int r_io_close(struct r_io_t *io, RIODesc *fd) {
if (io == NULL || fd == NULL)
return -1;
int nfd = fd->fd;
if (r_io_set_fd (io, fd)) {
RIODesc *desc = r_io_desc_get (io, fd->fd);
r_io_map_del (io, fd->fd);
r_io_plugin_close (io, fd->fd, io->plugin);
if (io->plugin && io->plugin->close)
return io->plugin->close (desc);
r_io_desc_del (io, desc->fd);
}
io->fd = -1; // unset current fd
return close (fd);
io->fd = NULL; // unset current fd
return close (nfd);
}
R_API int r_io_bind(RIO *io, RIOBind *bnd) {

View File

@ -13,8 +13,9 @@ endif
foo: all
ALL_TARGETS=
DEBUGS=ptrace.mk debug.mk gdb.mk malloc.mk shm.mk mach.mk w32dbg.mk procpid.mk
include ${DEBUGS}
PLUGINS=ptrace.mk debug.mk gdb.mk malloc.mk shm.mk mach.mk w32dbg.mk procpid.mk
#PLUGINS=ptrace.mk debug.mk gdb.mk malloc.mk mach.mk w32dbg.mk procpid.mk
include ${PLUGINS}
#ALL_TARGETS+=io_ewf.so

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2010 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2010-2011 pancake<nopcode.org> */
#include <r_io.h>
#include <r_lib.h>
@ -7,53 +7,55 @@
#include "../../debug/p/libgdbwrap/gdbwrapper.c"
#include "../../debug/p/libgdbwrap/interface.c"
// XXX: is str2bin ok? do reads match reality?
// XXX: This is an ugly singleton!!1
static gdbwrap_t *desc = NULL;
static int _fd = -1;
typedef struct {
int fd;
gdbwrap_t *desc;
} RIOGdb;
#define RIOGDB_FD(x) (((RIOGdb*)(x))->fd)
#define RIOGDB_DESC(x) (((RIOGdb*)(x))->desc)
#define RIOGDB_IS_VALID(x) (x && x->plugin==&r_io_plugin_gdb && x->data)
static int __plugin_open(RIO *io, const char *file) {
if (!memcmp (file, "gdb://", 6))
return R_TRUE;
return R_FALSE;
return (!memcmp (file, "gdb://", 6));
}
static int __open(RIO *io, const char *file, int rw, int mode) {
if (__plugin_open (io, file)) {
char *host = strdup (file+6);
char *port = strchr (host , ':');
_fd = -1;
if (port) {
*port = '\0';
_fd = r_socket_connect (host, atoi (port+1));
if (_fd != -1) desc = gdbwrap_init (_fd);
else eprintf ("Cannot connect to host.\n");
//return gdbwrapper ...();
} else eprintf ("Port not specified. Please use gdb://[host]:[port]\n");
free (host);
return _fd;
static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
char host[128], *port;
int _fd;
RIOGdb *riog;
if (!__plugin_open (io, file))
return NULL;
strncpy (host, file+6, sizeof (host)-1);
port = strchr (host , ':');
if (!port) {
eprintf ("Port not specified. Please use gdb://[host]:[port]\n");
return NULL;
}
return -1;
*port = '\0';
_fd = r_socket_connect (host, atoi (port+1));
if (_fd == -1) {
eprintf ("Cannot connect to host.\n");
return NULL;
}
riog = R_NEW (RIOGdb);
riog->fd = _fd;
riog->desc = gdbwrap_init (_fd);
return r_io_desc_new (&r_io_plugin_shm, _fd, file, rw, mode, riog);
}
static int __init(RIO *io) {
return R_TRUE;
}
static int __write(RIO *io, int fd, const ut8 *buf, int count) {
gdbwrap_writemem (desc, io->off, (void *)buf, count);
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
gdbwrap_writemem (RIOGDB_DESC (fd), io->off, (void *)buf, count);
return count;
}
static ut64 __lseek(RIO *io, int fildes, ut64 offset, int whence) {
static ut64 __lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
return offset;
}
static int __read(RIO *io, int fd, ut8 *buf, int count) {
static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
memset (buf, 0xff, count);
if (fd == _fd) {
char *ptr = gdbwrap_readmem (desc, (la32)io->off, count);
if (RIOGDB_IS_VALID (fd)) {
char *ptr = gdbwrap_readmem (RIOGDB_DESC (fd), (la32)io->off, count);
if (ptr == NULL)
return -1;
//eprintf ("READ %llx (%s)\n", (ut64)io->off, ptr);
@ -62,7 +64,7 @@ static int __read(RIO *io, int fd, ut8 *buf, int count) {
return -1;
}
static int __close(RIO *io, int fd) {
static int __close(RIODesc *fd) {
return -1;
}
@ -78,11 +80,4 @@ struct r_io_plugin_t r_io_plugin_gdb = {
.lseek = __lseek,
.system = NULL,
.debug = (void *)1,
.init = __init,
//void *widget;
/*
struct gdb_t *gdb;
ut32 (*write)(int fd, const ut8 *buf, ut32 count);
int fds[R_IO_NFDS];
*/
};

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2008-2009 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2008-2011 pancake<nopcode.org> */
#if __UNIX__
@ -7,102 +7,103 @@
#include "r_socket.h"
#include <sys/types.h>
int haret_fd = -1;
#define HARET_FD(x) ((int)((size_t)x->data))
static int haret__write(struct r_io_t *io, int fd, const ut8 *buf, int count)
{
//#if (sizeof(int)) > (sizeof(void*))
//#error WTF int>ptr? wrong compiler or architecture?
//#endif
//int haret_fd = -1;
static int haret__write(struct r_io_t *io, RIODesc *fd, const ut8 *buf, int count) {
/* TODO */
return 0;
}
static void haret_wait_until_prompt()
{
static void haret_wait_until_prompt(int ufd) {
unsigned char buf;
int off = 0;
while(1) {
r_socket_read (haret_fd, &buf, 1);
switch(off) {
case 0: if (buf == ')') off =1; break;
case 1: if (buf == '#') return; else off = 0; break;
for (;;) {
if (r_socket_read (ufd, &buf, 1) != 1) {
eprintf ("haret_wait_until_prompt: Unexpected eof in socket\n");
return;
}
switch (off) {
case 0: if (buf == ')') off = 1; break;
case 1: if (buf == '#') return; off = 0; break;
}
}
}
static int haret__read(struct r_io_t *io, int fd, ut8 *buf, int count)
{
static int haret__read(struct r_io_t *io, RIODesc *fd, ut8 *buf, int count) {
char tmp[1024];
int i = 0;
ut64 off, j;
int ufd = HARET_FD (fd);
off = io->off & -4;
sprintf (tmp, "pdump 0x%"PFMT64x" %i\r\n", off, count+4);
r_socket_write (haret_fd, tmp, strlen(tmp));
r_socket_read_block (haret_fd, (unsigned char *) tmp, strlen (tmp)+1);
r_socket_write (ufd, tmp, strlen (tmp));
r_socket_read_block (ufd, (unsigned char *) tmp, strlen (tmp)+1);
j = (io->off - off)*2;
while (i<count && j >= 0) {
r_socket_read_block (haret_fd, (unsigned char *) tmp, 11);
r_socket_read_block (haret_fd, (unsigned char *) tmp, 35);
r_socket_read_block (ufd, (ut8*) tmp, 11);
r_socket_read_block (ufd, (ut8*) tmp, 35);
if (i+16 < count || (io->off-off) == 0) {
tmp[35] = 0;
i += r_hex_str2bin (tmp+j, buf+i);
r_socket_read_block (haret_fd, (unsigned char *) tmp, 21);
r_socket_read_block (ufd, (unsigned char *) tmp, 21);
} else {
tmp[(io->off - off)*2] = 0;
i += r_hex_str2bin (tmp+j, buf+i);
}
j=0;
}
haret_wait_until_prompt ();
haret_wait_until_prompt (ufd);
return i;
}
static int haret__close(struct r_io_t *io, int fd)
{
int ret = -1;
if (haret_fd != -1 && fd==haret_fd) {
ret = r_socket_close (fd);
haret_fd = -1;
}
return ret; // return true/false here?
static int haret__close(RIODesc *fd) {
if (!fd || HARET_FD (fd)==-1)
return -1;
return r_socket_close (HARET_FD (fd));
}
static int haret__plugin_open(struct r_io_t *io, const char *pathname)
{
static int haret__plugin_open(struct r_io_t *io, const char *pathname) {
return (!memcmp (pathname, "haret://", 8));
}
static int haret__open(struct r_io_t *io, const char *pathname, int flags, int mode)
{
char *port, *ptr;
char buf[1024];
int p;
static RIODesc *haret__open(struct r_io_t *io, const char *pathname, int rw, int mode) {
char *port, *ptr, buf[1024];
int p, ufd;
strncpy (buf, pathname, sizeof (buf)-1);
if (!memcmp (buf, "haret://", 8)) {
if (haret__plugin_open (io, pathname)) {
ptr = buf + 8;
if (!(port = strchr (ptr, ':'))) {
eprintf ("haret: wrong url\n");
return -1;
return NULL;
}
*port = 0;
p = atoi (port+1);
if ((haret_fd = r_socket_connect (ptr, p)) == -1) {
if ((ufd = r_socket_connect (ptr, p)) == -1) {
eprintf ("Cannot connect to '%s' (%d)\n", ptr, p);
return -1;
return NULL;
} else eprintf ("Connected to: %s at port %d\n", ptr, p);
haret_wait_until_prompt ();
haret_wait_until_prompt (ufd);
return r_io_desc_new (&r_io_plugin_haret, ufd, pathname, rw, mode, (void*)(size_t)ufd);
}
return haret_fd;
return NULL;
}
static int haret__system(RIO *io, int fd, const char *command) {
static int haret__system(RIO *io, RIODesc *fd, const char *command) {
char buf;
int off = 0;
r_socket_write (haret_fd, (char *)command, strlen(command));
r_socket_write (haret_fd, "\r\n", 2);
while(1) {
r_socket_read_block (haret_fd, (unsigned char *)&buf, 1);
r_socket_write (HARET_FD (fd), (char *)command, strlen(command));
r_socket_write (HARET_FD (fd), "\r\n", 2);
for (;;) {
r_socket_read_block (HARET_FD (fd), (unsigned char *)&buf, 1);
eprintf ("%c", buf);
switch(off) {
case 0: if (buf == ')') off =1; break;
@ -113,19 +114,13 @@ static int haret__system(RIO *io, int fd, const char *command) {
return 0;
}
static ut64 haret__lseek(struct r_io_t *io, int fildes, ut64 offset, int whence) {
static ut64 haret__lseek(struct r_io_t *io, RIODesc *fd, ut64 offset, int whence) {
return offset;
}
static int haret__init(struct r_io_t *io)
{
return R_TRUE;
}
struct r_io_plugin_t r_io_plugin_haret = {
.name = "haret",
.desc = "Attach to Haret WCE application (haret://host:port)",
.init = haret__init,
.system = haret__system,
.open = haret__open,
.read = haret__read,

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2010 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */
#include <r_userconf.h>
@ -7,6 +7,7 @@
#include <r_cons.h>
#if __APPLE__
//#define USE_PTRACE 0
// EXPERIMENTAL
#define EXCEPTION_PORT 0
@ -34,23 +35,21 @@
#define MACH_ERROR_STRING(ret) \
(mach_error_string (ret) ? mach_error_string (ret) : "(unknown)")
typedef struct {
int pid;
task_t task;
} RIOMach;
#define RIOMACH_PID(x) (((RIOMach*)(x))->pid)
#define RIOMACH_TASK(x) (((RIOMach*)(x))->task)
#undef R_IO_NFDS
#define R_IO_NFDS 2
extern int errno;
static int fds[3];
static task_t pid_to_task(int pid) {
static task_t old_pid = -1;
static task_t old_task = -1;
task_t task = 0;
int err;
/* xlr8! */
if (old_task!= -1) //old_pid != -1 && old_pid == pid)
return old_task;
err = task_for_pid (mach_task_self (), (pid_t)pid, &task);
if ((err != KERN_SUCCESS) || !MACH_PORT_VALID(task)) {
int err = task_for_pid (mach_task_self (), (pid_t)pid, &task);
if ((err != KERN_SUCCESS) || !MACH_PORT_VALID (task)) {
eprintf ("Failed to get task %d for pid %d.\n", (int)task, (int)pid);
eprintf ("Reason: 0x%x: %s\n", err, MACH_ERROR_STRING (err));
eprintf ("You probably need to add user to procmod group.\n"
@ -58,16 +57,13 @@ static task_t pid_to_task(int pid) {
eprintf ("FMI: http://developer.apple.com/documentation/Darwin/Reference/ManPages/man8/taskgated.8.html\n");
return -1;
}
old_pid = pid;
old_task = task;
return task;
}
static int __read(RIO *io, int pid, ut8 *buff, int len) {
unsigned int size = 0;
int err = vm_read_overwrite (pid_to_task (pid),
(unsigned int)io->off, len, (pointer_t)buff, &size);
static int __read(RIO *io, RIODesc *fd, ut8 *buf, int len) {
vm_size_t size = 0;
int err = vm_read_overwrite (RIOMACH_TASK (fd),
(unsigned int)io->off, len, (pointer_t)buf, &size);
if (err == -1) {
eprintf ("Cannot read\n");
return -1;
@ -75,9 +71,9 @@ static int __read(RIO *io, int pid, ut8 *buff, int len) {
return (int)size;
}
static int mach_write_at(int tid, const void *buff, int len, ut64 addr) {
static int mach_write_at(RIOMach *riom, const void *buff, int len, ut64 addr) {
kern_return_t err;
task_t task = pid_to_task (tid);
task_t task = riom->task;
// XXX SHOULD RESTORE PERMS LATER!!!
//err = vm_protect (task, addr+(addr%4096), 4096, 0, VM_PROT_READ | VM_PROT_WRITE);
if (vm_protect (task, addr, len, 0, VM_PROT_READ | VM_PROT_WRITE) != KERN_SUCCESS)
@ -93,26 +89,23 @@ static int mach_write_at(int tid, const void *buff, int len, ut64 addr) {
return len;
}
static int __write(struct r_io_t *io, int pid, const ut8 *buf, int len) {
return mach_write_at (pid, buf, len, io->off);
static int __write(struct r_io_t *io, RIODesc *fd, const ut8 *buf, int len) {
return mach_write_at ((RIOMach*)fd->data, buf, len, io->off);
}
static int __plugin_open(struct r_io_t *io, const char *file) {
if (!memcmp (file, "attach://", 9) || !memcmp (file, "mach://", 7))
return R_TRUE;
return R_FALSE;
return (!memcmp (file, "attach://", 9) || !memcmp (file, "mach://", 7));
}
static task_t inferior_task = 0;
static int task = 0;
//static task_t inferior_task = 0;
//static int task = 0;
// s/inferior_task/port/
static int debug_attach(int pid) {
inferior_task = pid_to_task (pid);
if (inferior_task == -1)
task_t task = pid_to_task (pid);
if (task == -1)
return -1;
task = inferior_task; // ugly global asignation
eprintf ("; pid = %d\ntask= %d\n", pid, inferior_task);
eprintf ("; pid = %d\ntask= %d\n", pid, task);
#if 0
// TODO : move this code into debug
if (task_threads (task, &inferior_threads, &inferior_thread_count)
@ -132,7 +125,7 @@ static int debug_attach(int pid) {
/* is this required for arm ? */
#if EXCEPTION_PORT
int exception_port;
if (mach_port_allocate (mach_task_self(), MACH_PORT_RIGHT_RECEIVE,
if (mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE,
&exception_port) != KERN_SUCCESS) {
eprintf ("Failed to create exception port.\n");
return -1;
@ -148,62 +141,75 @@ static int debug_attach(int pid) {
return -1;
}
#endif
return 0;
return task;
}
static int __open(struct r_io_t *io, const char *file, int rw, int mode) {
int ret = -1;
if (__plugin_open (io, file)) {
int pid = atoi(file+(file[0]=='a'?9:7));
if (pid>0) {
ret = debug_attach (pid);
if (ret == -1) {
switch (errno) {
case EPERM:
ret = pid;
eprintf ("Operation not permitted\n");
return -1;
case EINVAL:
perror ("ptrace: Cannot attach");
eprintf ("ERRNO: %d (EINVAL)\n", errno);
break;
}
} ret = pid;
} else ret = pid;
static RIODesc *__open(struct r_io_t *io, const char *file, int rw, int mode) {
RIOMach *riom;
int pid;
task_t task;
if (!__plugin_open (io, file))
return NULL;
pid = atoi (file+(file[0]=='a'?9:7));
if (pid<1)
return NULL;
task = debug_attach (pid);
if ((int)task == -1) {
switch (errno) {
case EPERM:
eprintf ("Operation not permitted\n");
break;
case EINVAL:
perror ("ptrace: Cannot attach");
eprintf ("ERRNO: %d (EINVAL)\n", errno);
break;
default:
eprintf ("unknown error in debug_attach\n");
break;
}
return NULL;
}
fds[0] = ret;
return ret;
riom = R_NEW (RIOMach);
riom->pid = pid;
riom->task = task;
return r_io_desc_new (&r_io_plugin_mach, riom->pid, file, rw, mode, riom);
}
static ut64 __lseek(struct r_io_t *io, int fildes, ut64 offset, int whence) {
static ut64 __lseek(struct r_io_t *io, RIODesc *fd, ut64 offset, int whence) {
return offset;
}
static int __close(struct r_io_t *io, int pid) {
static int __close(RIODesc *fd) {
int pid = RIOMACH_PID (fd);
R_FREE (fd->data);
return ptrace (PT_DETACH, pid, 0, 0);
}
static int __system(struct r_io_t *io, int fd, const char *cmd) {
static int __system(struct r_io_t *io, RIODesc *fd, const char *cmd) {
RIOMach *riom = (RIOMach*)fd->data;
//printf("ptrace io command (%s)\n", cmd);
/* XXX ugly hack for testing purposes */
if (!strcmp (cmd, "pid")) {
int pid = atoi (cmd+4);
if (pid != 0)
io->fd = pid;
//printf("PID=%d\n", io->fd);
return io->fd;
if (pid != 0) {
task_t task = pid_to_task (pid);
if (task != -1) {
eprintf ("PID=%d\n", pid);
riom->pid = pid;
riom->task = task;
return 0;
}
eprintf ("io_mach_system: Invalid pid\n");
return 1;
}
eprintf ("io_mach_system: Invalid pid\n");
return 1;
} else eprintf ("Try: '|pid'\n");
return R_TRUE;
}
static int __init(struct r_io_t *io) {
eprintf ("mach init\n");
return R_TRUE;
}
// TODO: rename ptrace to io_mach .. err io.ptrace ??
struct r_io_plugin_t r_io_plugin_mach = {
//void *plugin;
.name = "mach",
.desc = "mach debug io",
.open = __open,
@ -212,7 +218,6 @@ struct r_io_plugin_t r_io_plugin_mach = {
.plugin_open = __plugin_open,
.lseek = __lseek,
.system = __system,
.init = __init,
.write = __write,
// .debug ?
};

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2008-2009 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2008-2011 pancake<nopcode.org> */
#include "r_io.h"
#include "r_lib.h"
@ -6,96 +6,78 @@
#include <stdlib.h>
#include <sys/types.h>
#define MALLOC_FD 98479
static int malloc_fd = -1;
static unsigned char *malloc_buf = NULL;
static unsigned int malloc_bufsz = 0;
// XXX shitty vars -- should be state
static ut64 malloc_seek = 0;
typedef struct {
int fd;
ut8 *buf;
ut32 size;
} RIOMalloc;
static int __write(struct r_io_t *io, int fd, const ut8 *buf, int count) {
if (malloc_buf == NULL)
return 0;
memcpy (malloc_buf+io->off, buf, count);
return count;
}
#define RIOMALLOC_FD(x) (((RIOMalloc*)x)->fd)
#define RIOMALLOC_SZ(x) (((RIOMalloc*)x)->size)
#define RIOMALLOC_BUF(x) (((RIOMalloc*)x)->buf)
static int __read(struct r_io_t *io, int fd, ut8 *buf, int count) {
if (malloc_buf == NULL)
return 0;
if (malloc_seek + count > malloc_bufsz) {
//config.seek = 0; // ugly hack
//count = config.seek+count-config.size;
return 0;
}
if (malloc_seek + count > malloc_bufsz)
malloc_seek = malloc_bufsz;
memcpy (buf, malloc_buf+malloc_seek, count);
return count;
}
static int __close(struct r_io_t *io, int fd) {
if (malloc_buf == NULL)
static int __write(struct r_io_t *io, RIODesc *fd, const ut8 *buf, int count) {
if (fd == NULL || fd->data == NULL)
return -1;
free(malloc_buf);
malloc_buf = malloc(malloc_bufsz);
if (io->off+count >= RIOMALLOC_SZ (fd))
return -1;
memcpy (RIOMALLOC_BUF (fd)+io->off, buf, count);
return count;
}
static int __read(struct r_io_t *io, RIODesc *fd, ut8 *buf, int count) {
if (fd == NULL || fd->data == NULL)
return -1;
if (io->off+count >= RIOMALLOC_SZ (fd))
return -1;
memcpy (buf, RIOMALLOC_BUF (fd)+io->off, count);
return count;
}
static int __close(RIODesc *fd) {
if (fd == NULL || fd->data == NULL)
return -1;
free (RIOMALLOC_BUF (fd));
free (fd->data);
fd->data = NULL;
fd->state = R_IO_DESC_TYPE_CLOSED;
return 0;
}
extern ut64 posix_lseek(int fildes, ut64 offset, int whence);
static ut64 __lseek(struct r_io_t *io, int fildes, ut64 offset, int whence) {
static ut64 __lseek(struct r_io_t *io, RIODesc *fd, ut64 offset, int whence) {
switch (whence) {
case SEEK_SET:
malloc_seek = offset;
break;
case SEEK_CUR:
malloc_seek += offset;
break;
case SEEK_END:
malloc_seek = malloc_bufsz;
break;
case SEEK_SET: return offset;
case SEEK_CUR: return io->off + offset;
case SEEK_END: return RIOMALLOC_SZ (fd);
}
return malloc_seek;
return offset;
}
static int __plugin_open(struct r_io_t *io, const char *pathname) {
return (!memcmp(pathname, "malloc://", 9));
return (!memcmp (pathname, "malloc://", 9));
}
static int __open(struct r_io_t *io, const char *pathname, int flags, int mode) {
char buf[1024];
char *ptr = buf;
static inline int getmalfd (RIOMalloc *mal) {
return (int)(size_t)mal->buf;
}
strncpy(buf, pathname, 1000);
if (!memcmp(ptr , "malloc://", 9)) {
ptr = ptr+6;
// connect
malloc_fd = MALLOC_FD;
malloc_bufsz = atoi(pathname+9);
malloc_buf = malloc(malloc_bufsz);
if (malloc_buf == NULL) {
printf("Cannot allocate (%s)%d bytes\n", pathname+9, malloc_bufsz);
malloc_buf = NULL;
malloc_bufsz = 0;
malloc_fd = -1;
} else memset(malloc_buf, '\0', malloc_bufsz);
static RIODesc *__open(struct r_io_t *io, const char *pathname, int rw, int mode) {
if (__plugin_open (io, pathname)) {
RIOMalloc *mal = R_NEW (RIOMalloc);
mal->fd = getmalfd (mal);
mal->size = atoi (pathname+9) +1;
mal->buf = malloc (mal->size);
if (mal->buf != NULL) {
memset (mal->buf, '\0', mal->size);
return r_io_desc_new (&r_io_plugin_malloc, mal->fd, pathname, rw, mode, mal);
}
eprintf ("Cannot allocate (%s) %d bytes\n", pathname+9, mal->size);
free (mal);
}
return malloc_fd;
}
static int __init(struct r_io_t *io) {
return R_TRUE;
}
static int __system(struct r_io_t *io, int fd, const char *cmd) {
/* */
return 0;
return NULL;
}
struct r_io_plugin_t r_io_plugin_malloc = {
//void *plugin;
.name = "malloc",
.desc = "memory allocation ( malloc://size-in-bytes )",
.open = __open,
@ -103,15 +85,7 @@ struct r_io_plugin_t r_io_plugin_malloc = {
.read = __read,
.plugin_open = __plugin_open,
.lseek = __lseek,
.system = __system,
.init = __init,
.write = __write,
//void *widget;
/*
struct debug_t *debug;
ut32 (*write)(int fd, const ut8 *buf, ut32 count);
int fds[R_IO_NFDS];
*/
};
#ifndef CORELIB

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2010 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2011 pancake<nopcode.org> */
// TODO: implement the rap API in r_socket ?
#include "r_io.h"
@ -7,23 +7,31 @@
#include "r_socket.h"
#include <sys/types.h>
static int rap_fd = -1;
static int is_listener = 0;
static int endian = 0;
#define ENDIAN (0)
typedef struct {
int fd;
int listener;
} RIORap;
#define RIORAP_FD(x) (((RIORap*)(x->data))->fd)
#define RIORAP_IS_LISTEN(x) (((RIORap*)(x->data))->listener)
#define RIORAP_IS_VALID(x) ((x) && (x->data) && (x->plugin == &r_io_plugin_rap))
static int rap__write(struct r_io_t *io, int fd, const ut8 *buf, int count) {
static int rap__write(struct r_io_t *io, RIODesc *fd, const ut8 *buf, int count) {
int ret;
unsigned int size = (int)count;
ut8 *tmp = (ut8 *)malloc (count+5);
ut8 *tmp;
if (count>RMT_MAX)
count = RMT_MAX;
if ((tmp = (ut8 *)malloc (count+5))) {
eprintf ("rap__write: malloc failed\n");
return -1;
}
tmp[0] = RMT_WRITE;
r_mem_copyendian ((ut8 *)tmp+1, (ut8*)&size, 4, endian);
memcpy (tmp+5, buf, size);
r_mem_copyendian ((ut8 *)tmp+1, (ut8*)&count, 4, ENDIAN);
memcpy (tmp+5, buf, count);
ret = r_socket_write (rap_fd, tmp, size+5);
if (read (rap_fd, tmp, 5) != 5) { // TODO use while'd read here
ret = r_socket_write (RIORAP_FD (fd), tmp, count+5);
// TODO: use r_socket_read here ??
if (read (RIORAP_FD (fd), tmp, 5) != 5) { // TODO use while'd read here
eprintf ("rap__write: error\n");
ret = -1;
}
@ -32,60 +40,67 @@ static int rap__write(struct r_io_t *io, int fd, const ut8 *buf, int count) {
return ret;
}
static int rap__read(struct r_io_t *io, int fd, ut8 *buf, int count) {
ut8 tmp[5];
static int rap__read(struct r_io_t *io, RIODesc *fd, ut8 *buf, int count) {
int ret;
int i = (int)count;
ut8 tmp[5];
if (count>RMT_MAX)
count = RMT_MAX;
// send
tmp[0] = RMT_READ;
r_mem_copyendian (tmp+1, (ut8*)&i, 4, endian);
r_socket_write (rap_fd, tmp, 5);
r_mem_copyendian (tmp+1, (ut8*)&count, 4, ENDIAN);
r_socket_write (RIORAP_FD (fd), tmp, 5);
// recv
read(rap_fd, tmp, 5);
if (tmp[0] != (RMT_READ|RMT_REPLY)) {
eprintf ("Unexpected rap read reply (0x%02x)\n", tmp[0]);
ret = r_socket_read (RIORAP_FD (fd), tmp, 5);
if (ret != 5 || tmp[0] != (RMT_READ|RMT_REPLY)) {
eprintf ("rap__read: Unexpected rap read reply (0x%02x)\n", tmp[0]);
return -1;
}
r_mem_copyendian ((ut8*)&i, tmp+1, 4, endian);
r_mem_copyendian ((ut8*)&i, tmp+1, 4, ENDIAN);
if (i>count) {
eprintf ("Unexpected data size %d\n", i);
eprintf ("rap__read: Unexpected data size %d\n", i);
return -1;
} else r_socket_read_block (rap_fd, buf, i);
if (i>0&&i<RMT_MAX) {
}
r_socket_read_block (RIORAP_FD (fd), buf, i);
if (count>0 && count<RMT_MAX)
eprintf ("READ %d\n" ,i);
} else i = 0;
return i;
else count = 0;
return count;
}
static int rap__close(struct r_io_t *io, int fd) {
static int rap__close(RIODesc *fd) {
int ret = -1;
if (rap_fd != -1 && fd==rap_fd) {
ret = close (fd);
rap_fd = -1;
}
return ret; // return true/false here?
if (RIORAP_IS_VALID (fd)) {
if (RIORAP_FD (fd) != -1) {
RIORap *r = fd->data;
fd->state = R_IO_DESC_TYPE_CLOSED;
ret = r_socket_close (r->fd);
free (fd->data);
fd->data = NULL;
}
} else eprintf ("rap__close: fdesc is not a r_io_rap plugin\n");
return ret;
}
static ut64 rap__lseek(struct r_io_t *io, int fildes, ut64 offset, int whence) {
static ut64 rap__lseek(struct r_io_t *io, RIODesc *fd, ut64 offset, int whence) {
int ret;
ut8 tmp[10];
// query
tmp[0] = RMT_SEEK;
tmp[1] = (ut8)whence;
r_mem_copyendian (tmp+2, (ut8*)&offset, 8, endian);
write (rap_fd, &tmp, 10);
r_mem_copyendian (tmp+2, (ut8*)&offset, 8, ENDIAN);
write (RIORAP_FD (fd), &tmp, 10);
// get reply
ret = r_socket_read_block (fildes, (ut8*)&tmp, 9);
ret = r_socket_read_block (RIORAP_FD (fd), (ut8*)&tmp, 9);
if (ret!=9)
return -1;
if (tmp[0] != (RMT_SEEK | RMT_REPLY)) {
eprintf ("Unexpected lseek reply\n");
return -1;
}
r_mem_copyendian ((ut8 *)&offset, tmp+1, 8, !endian);
r_mem_copyendian ((ut8 *)&offset, tmp+1, 8, !ENDIAN);
return offset;
}
@ -93,70 +108,73 @@ static int rap__plugin_open(struct r_io_t *io, const char *pathname) {
return (!memcmp (pathname, "rap://", 6));
}
static int rap__open(struct r_io_t *io, const char *pathname, int flags, int mode) {
static RIODesc *rap__open(struct r_io_t *io, const char *pathname, int rw, int mode) {
RIORap *rior;
char *file, *port, *ptr;
char buf[1024];
int i, p, listenmode;
int i, p, listenmode, rap_fd;
strncpy (buf, pathname, sizeof (buf)-1);
if (!memcmp (buf, "rap://", 6)) {
ptr = buf + 6;
if (!(port = strchr (ptr, ':'))) {
eprintf ("rap: wrong uri\n");
return -1;
}
listenmode = (*ptr==':');
*port = 0;
p = atoi (port+1);
if ((file = strchr (port+1, '/'))) {
*file = 0;
file++;
}
if (listenmode) {
if (p<=0) {
eprintf ("rap: cannot listen here. Try rap://:9999\n");
return -1;
}
//TODO: Handle ^C signal (SIGINT, exit); // ???
eprintf ("rap: listening at port %d\n", p);
is_listener = R_TRUE;
return r_socket_listen (p);
} else {
if ((rap_fd=r_socket_connect (ptr, p))==-1) {
eprintf ("Cannot connect to '%s' (%d)\n", ptr, p);
return -1;
} else eprintf ("Connected to: %s at port %d\n", ptr, p);
if (file&&*file) {
// send
buf[0] = RMT_OPEN;
buf[1] = flags;
buf[2] = (ut8)strlen(file);
memcpy (buf+3, file, buf[2]);
r_socket_write (rap_fd, buf, 3+buf[2]);
// read
eprintf ("waiting... ");
read (rap_fd, (ut8*)buf, 5);
if (buf[0] != (char)(RMT_OPEN|RMT_REPLY))
return -1;
r_mem_copyendian ((ut8 *)&i, (ut8*)buf+1, 4, endian);
if (i>0) eprintf ("ok\n");
}
is_listener = R_FALSE;
return rap_fd;
}
if (!rap__plugin_open (io, pathname))
return NULL;
ptr += 6;
if (!(port = strchr (ptr, ':'))) {
eprintf ("rap: wrong uri\n");
return NULL;
}
return rap_fd;
listenmode = (*ptr==':');
*port = 0;
p = atoi (port+1);
if ((file = strchr (port+1, '/'))) {
*file = 0;
file++;
}
if (listenmode) {
if (p<=0) {
eprintf ("rap: cannot listen here. Try rap://:9999\n");
return NULL;
}
//TODO: Handle ^C signal (SIGINT, exit); // ???
eprintf ("rap: listening at port %d\n", p);
rior = R_NEW (RIORap);
rior->listener = R_TRUE;
rior->fd = r_socket_listen (p);
return r_io_desc_new (&r_io_plugin_rap, rior->fd, pathname, rw, mode, rior);
}
if ((rap_fd=r_socket_connect (ptr, p))==-1) {
eprintf ("Cannot connect to '%s' (%d)\n", ptr, p);
return NULL;
}
eprintf ("Connected to: %s at port %d\n", ptr, p);
rior = R_NEW (RIORap);
rior->listener = R_FALSE;
rior->fd = rap_fd;
if (file && *file) {
// send
buf[0] = RMT_OPEN;
buf[1] = rw;
buf[2] = (ut8)strlen (file);
memcpy (buf+3, file, buf[2]);
r_socket_write (rap_fd, buf, 3+buf[2]);
// read
eprintf ("waiting... ");
read (rap_fd, (ut8*)buf, 5);
if (buf[0] != (char)(RMT_OPEN|RMT_REPLY)) {
free (rior);
return NULL;
}
r_mem_copyendian ((ut8 *)&i, (ut8*)buf+1, 4, ENDIAN);
if (i>0) eprintf ("ok\n");
}
return r_io_desc_new (&r_io_plugin_rap, rior->fd, pathname, rw, mode, rior);
}
static int rap__init(struct r_io_t *io) {
return R_TRUE;
static int rap__listener(RIODesc *fd) {
if (RIORAP_IS_VALID (fd))
return RIORAP_IS_LISTEN (fd);
return R_FALSE;
}
static int rap__listener(struct r_io_t *io) {
return is_listener;
}
static int rap__system(RIO *io, int fd, const char *command) {
static int rap__system(RIO *io, RIODesc *fd, const char *command) {
ut8 buf[1024];
char *ptr;
int ret, i, j;
@ -164,12 +182,12 @@ static int rap__system(RIO *io, int fd, const char *command) {
// send
buf[0] = RMT_SYSTEM;
i = strlen (command);
r_mem_copyendian (buf+1, (ut8*)&i, 4, endian);
r_mem_copyendian (buf+1, (ut8*)&i, 4, ENDIAN);
memcpy (buf+5, command, i);
r_socket_write (rap_fd, buf, i+5);
r_socket_write (RIORAP_FD (fd), buf, i+5);
// read
ret = r_socket_read_block (rap_fd, buf, 5);
ret = r_socket_read_block (RIORAP_FD (fd), buf, 5);
if (ret != 5) {
return -1;
}
@ -177,14 +195,14 @@ static int rap__system(RIO *io, int fd, const char *command) {
eprintf ("Unexpected system reply\n");
return -1;
}
r_mem_copyendian ((ut8*)&i, buf+1, 4, !endian);
r_mem_copyendian ((ut8*)&i, buf+1, 4, !ENDIAN);
if (i == -1)
return -1;
if (i>RMT_MAX)
i = RMT_MAX;
ptr = (char *)malloc (i);
if (ptr) {
r_socket_read_block (rap_fd, (ut8*)ptr, i);
r_socket_read_block (RIORAP_FD (fd), (ut8*)ptr, i);
j = write (1, ptr, i);
free (ptr);
}
@ -193,7 +211,7 @@ static int rap__system(RIO *io, int fd, const char *command) {
struct r_io_plugin_t r_io_plugin_rap = {
.name = "rap",
.desc = "radare protocol over tcp/ip (rap://:port rap://host:port/file)",
.desc = "radare network protocol (rap://:port rap://host:port/file)",
.listener = rap__listener,
.open = rap__open,
.close = rap__close,
@ -201,7 +219,6 @@ struct r_io_plugin_t r_io_plugin_rap = {
.plugin_open = rap__plugin_open,
.lseek = rap__lseek,
.system = rap__system,
.init = rap__init,
.write = rap__write,
};

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2008-2009 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2008-2011 pancake<nopcode.org> */
#if __UNIX__
@ -8,44 +8,60 @@
#include <sys/ipc.h>
#include <sys/shm.h>
static int shm_fd = -1;
static unsigned char *shm_buf = NULL;
static unsigned int shm_bufsz = 32*1024*1024; /* 32MB */
typedef struct {
int fd;
int id;
ut8 *buf;
ut32 size;
} RIOShm;
#define RIOSHM_FD(x) (((RIOShm*)x)->fd)
static int shm__write(struct r_io_t *io, int fd, const ut8 *buf, int count)
{
if (shm_buf != NULL)
return (ssize_t)memcpy(shm_buf+io->off, buf, count);
#define SHMATSZ 32*1024*1024; /* 32MB : XXX not used correctly? */
static int shm__write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
RIOShm *shm;
if (fd == NULL || fd->data == NULL)
return -1;
shm = fd->data;
if (shm->buf != NULL) {
(void)memcpy (shm->buf+io->off, buf, count);
return count;
}
return -1;
}
static int shm__read(struct r_io_t *io, int fd, ut8 *buf, int count)
{
if (shm_buf == NULL)
static int shm__read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
RIOShm *shm;
if (fd == NULL || fd->data == NULL)
return -1;
if (io->off > shm_bufsz)
io->off = shm_bufsz;
memcpy(buf, shm_buf+io->off , count);
return 0;
shm = fd->data;
if (io->off > shm->size)
io->off = shm->size;
memcpy (buf, shm->buf+io->off , count);
return count;
}
static int shm__close(struct r_io_t *io, int fd)
{
if (shm_buf == NULL)
static int shm__close(RIODesc *fd) {
int ret;
if (fd == NULL || fd->data == NULL)
return -1;
return shmdt(shm_buf);
ret = shmdt (((RIOShm*)(fd->data))->buf);
free (fd->data);
fd->data = NULL;
return ret;
}
static ut64 shm__lseek(struct r_io_t *io, int fildes, ut64 offset, int whence)
{
if (shm_buf == NULL)
static ut64 shm__lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
RIOShm *shm;
if (fd == NULL || fd->data == NULL)
return -1;
switch(whence) {
shm = fd->data;
switch (whence) {
case SEEK_SET:
return offset;
case SEEK_CUR:
if (io->off+offset>shm_bufsz)
return shm_bufsz;
if (io->off+offset>shm->size)
return shm->size;
return io->off + offset;
case SEEK_END:
return 0xffffffff;
@ -53,37 +69,37 @@ static ut64 shm__lseek(struct r_io_t *io, int fildes, ut64 offset, int whence)
return io->off;
}
static int shm__plugin_open(struct r_io_t *io, const char *pathname)
{
return (!memcmp(pathname, "shm://", 6));
static int shm__plugin_open(RIO *io, const char *pathname) {
return (!memcmp (pathname, "shm://", 6));
}
static int shm__open(struct r_io_t *io, const char *pathname, int flags, int mode)
{
char buf[1024];
char *ptr = buf;
static inline int getshmid (const char *str) {
return atoi (str);
}
strncpy(buf, pathname, 1000);
static inline int getshmfd (RIOShm *shm) {
return (int)(size_t)shm->buf;
}
if (!memcmp(ptr , "shm://", 6)) {
ptr= ptr+6;
// connect
shm_buf= shmat(atoi(ptr), 0, 0);
if (((int)(size_t)(shm_buf)) != -1) {
printf("Connected to shared memory 0x%08x\n", atoi(ptr));
shm_fd = (int)(size_t)&shm_buf;
} else {
printf("Cannot connect to shared memory (%d)\n", atoi(ptr));
shm_buf = NULL;
shm_fd = -1;
static RIODesc *shm__open(RIO *io, const char *pathname, int rw, int mode) {
if (!memcmp (pathname, "shm://", 6)) {
RIOShm *shm = R_NEW (RIOShm);
const char *ptr = pathname+6;
shm->id = getshmid (ptr);
shm->buf = shmat (shm->id, 0, 0);
shm->fd = getshmfd (shm);
shm->size = SHMATSZ;
if (shm->fd != -1) {
eprintf ("Connected to shared memory 0x%08x\n", shm->id);
return r_io_desc_new (&r_io_plugin_shm, shm->fd, pathname, rw, mode, shm);
}
eprintf ("Cannot connect to shared memory (%d)\n", shm->id);
free (shm);
}
return shm_fd;
return NULL;
}
static int shm__init(struct r_io_t *io)
{
static int shm__init(RIO *io) {
return R_TRUE;
}

View File

@ -5,4 +5,4 @@ TARGET_MACH=io_mach.${EXT_SO}
ALL_TARGETS+=${TARGET_MACH}
${TARGET_MACH}: ${OBJ_MACH}
${CC} -shared ${CFLAGS} -o ${TARGET_MACH} ${OBJ_MACH}
${CC} -shared ${CFLAGS} -o ${TARGET_MACH} ${OBJ_MACH} -L.. -lr_io

View File

@ -5,4 +5,4 @@ TARGET_MALLOC=io_malloc.${EXT_SO}
ALL_TARGETS+=${TARGET_MALLOC}
${TARGET_MALLOC}: ${OBJ_MALLOC}
${CC} -shared ${CFLAGS} -o ${TARGET_MALLOC} ${OBJ_MALLOC}
${CC} -shared ${CFLAGS} -o ${TARGET_MALLOC} ${OBJ_MALLOC} -lr_io

View File

@ -5,4 +5,4 @@ TARGET_SHM=io_shm.${EXT_SO}
ALL_TARGETS+=${TARGET_SHM}
${TARGET_SHM}: ${OBJ_SHM}
${CC} -shared ${CFLAGS} -o ${TARGET_SHM} ${OBJ_SHM}
${CC} -shared ${CFLAGS} -o ${TARGET_SHM} ${OBJ_SHM} -lr_io

View File

@ -12,7 +12,6 @@ static struct r_io_plugin_t *io_static_plugins[] =
{ R_IO_STATIC_PLUGINS };
R_API int r_io_plugin_add(struct r_io_t *io, struct r_io_plugin_t *plugin) {
int i;
struct r_io_list_t *li;
if (plugin == NULL)
return R_FALSE;
@ -20,8 +19,6 @@ R_API int r_io_plugin_add(struct r_io_t *io, struct r_io_plugin_t *plugin) {
if (li == NULL)
return R_FALSE;
li->plugin = plugin;
for(i=0;i<R_IO_NFDS;i++)
li->plugin->fds[i] = -1;
list_add_tail(&(li->list), &(io->io_list));
return R_TRUE;
}
@ -53,20 +50,16 @@ R_API struct r_io_plugin_t *r_io_plugin_resolve(struct r_io_t *io, const char *f
return NULL;
}
/*
DEPRECATED
R_API struct r_io_plugin_t *r_io_plugin_resolve_fd(struct r_io_t *io, int fd) {
int i;
struct list_head *pos;
list_for_each_prev(pos, &io->io_list) {
struct r_io_list_t *il = list_entry(pos, struct r_io_list_t, list);
for(i=0;i<R_IO_NFDS;i++) {
if (il->plugin->fds[i] == fd)
return il->plugin;
}
}
return NULL;
}
*/
R_API int r_io_plugin_open(struct r_io_t *io, int fd, struct r_io_plugin_t *plugin) {
#if 0
int i=0;
struct list_head *pos;
list_for_each_prev(pos, &io->io_list) {
@ -82,24 +75,12 @@ R_API int r_io_plugin_open(struct r_io_t *io, int fd, struct r_io_plugin_t *plug
}
}
return -1;
#endif
return 0;
}
R_API int r_io_plugin_close(struct r_io_t *io, int fd, struct r_io_plugin_t *plugin) {
int i=0;
struct list_head *pos;
list_for_each_prev (pos, &io->io_list) {
struct r_io_list_t *il = list_entry (pos, struct r_io_list_t, list);
if (plugin == il->plugin) {
for (i=0;i<R_IO_NFDS;i++) {
if (il->plugin->fds[i] == fd) {
il->plugin->fds[i] = -1;
return 0;
}
}
return -1;
}
}
return -1;
return 0;
}
// TODO: must return an r_iter ator

View File

@ -54,6 +54,7 @@ io.w32dbg
io.malloc
io.ptrace
io.procpid
io.shm
parse.dummy
parse.mreplace
parse.x86_pseudo"