Esempio n. 1
0
static int rap__read(struct r_io_t *io, RIODesc *fd, ut8 *buf, int count) {
	RSocket *s = RIORAP_FD (fd);
	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*)&count, 4, ENDIAN);
	r_socket_write (s, tmp, 5);
	r_socket_flush (s);
	// recv
	ret = r_socket_read_block (s, tmp, 5);
	if (ret != 5 || tmp[0] != (RMT_READ|RMT_REPLY)) {
		eprintf ("rap__read: Unexpected rap read reply "
			"(%d=0x%02x) expected (%d=0x%02x)\n",
			ret, tmp[0], 2, (RMT_READ|RMT_REPLY));
		return -1;
	}
	r_mem_copyendian ((ut8*)&i, tmp+1, 4, ENDIAN);
	if (i>count) {
		eprintf ("rap__read: Unexpected data size %d\n", i);
		return -1;
	}
	r_socket_read_block (s, buf, i);
	return count;
}
Esempio n. 2
0
static int rap__read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
	RSocket *s = RIORAP_FD (fd);
	int ret, i = (int)count;
	ut8 tmp[5];

	// XXX. if count is > RMT_MAX, just perform multiple queries
	if (count > RMT_MAX) {
		count = RMT_MAX;
	}
	// send
	tmp[0] = RMT_READ;
	r_write_be32 (tmp + 1, count);
	r_socket_write (s, tmp, 5);
	r_socket_flush (s);
	// recv
	ret = r_socket_read_block (s, tmp, 5);
	if (ret != 5 || tmp[0] != (RMT_READ | RMT_REPLY)) {
		eprintf ("rap__read: Unexpected rap read reply "
			"(%d=0x%02x) expected (%d=0x%02x)\n",
			ret, tmp[0], 2, (RMT_READ | RMT_REPLY));
		return -1;
	}
	i = r_read_at_be32 (tmp, 1);
	if (i >count) {
		eprintf ("rap__read: Unexpected data size %d\n", i);
		return -1;
	}
	r_socket_read_block (s, buf, i);
	return count;
}
Esempio n. 3
0
static int rap__write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
	RSocket *s = RIORAP_FD (fd);
	ut8 *tmp;
	int ret;

	if (count < 1) {
		return count;
	}
	// TOOD: if count > RMT_MAX iterate !
	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_write_be32 (tmp + 1, count);
	memcpy (tmp + 5, buf, count);

	ret = r_socket_write (s, tmp, count + 5);
	r_socket_flush (s);
	if (r_socket_read (s, tmp, 5) != 5) { // TODO read_block?
		eprintf ("rap__write: error\n");
		ret = -1;
	} else {
		ret = r_read_be32 (tmp + 1);
		if (!ret) {
			ret = -1;
		}
	}
	free (tmp);
	return ret;
}
Esempio n. 4
0
static int rap__system(RIO *io, RIODesc *fd, const char *command) {
	RSocket *s = RIORAP_FD (fd);
	ut8 buf[RMT_MAX];
	char *ptr;
	int op, ret, i, j = 0;

	// send
	if (*command=='!') {
		op = RMT_SYSTEM;
		command++;
	} else
		op = RMT_CMD;
	buf[0] = op;
	i = strlen (command)+1;
	if (i>RMT_MAX) {
		eprintf ("Command too long\n");
		return -1;
	}
	r_mem_copyendian (buf+1, (ut8*)&i, 4, ENDIAN);
	memcpy (buf+5, command, i);
	r_socket_write (s, buf, i+5);
	r_socket_flush (s);

	// read
	ret = r_socket_read_block (s, buf, 5);
	if (ret != 5)
		return -1;
	if (buf[0] != (op | RMT_REPLY)) {
		eprintf ("Unexpected system reply\n");
		return -1;
	}
	r_mem_copyendian ((ut8*)&i, buf+1, 4, ENDIAN);
	if (i == -1)
		return -1;
	ret = 0;
	ptr = (char *)malloc (i);
	if (ptr) {
		int ir, tr = 0;
		do {
			ir = r_socket_read_block (s, (ut8*)ptr+tr, i-tr);
			if (ir>0) tr += ir;
			else break;
		} while (tr<i);
		// TODO: use io->printf() with support for \x00
		ptr[i] = 0;
		if (io->printf) {
			io->printf ("%s", ptr);
			j = i;
		} else j = write (1, ptr, i);
		free (ptr);
	}
	/* Clean */
	if (ret > 0) {
		ret -= r_socket_read (s, (ut8*)buf, RMT_MAX);
	}
	return i-j;
}
Esempio n. 5
0
static int rap__system(RIO *io, RIODesc *fd, const char *command) {
	RSocket *s = RIORAP_FD (fd);
	ut8 buf[RMT_MAX];
	char *ptr;
	int op, ret, i, j = 0;

	// send
	if (*command=='!') {
		op = RMT_SYSTEM;
		command++;
	} else
		op = RMT_CMD;
	buf[0] = op;
	i = strlen (command);
	if (i>RMT_MAX) {
		eprintf ("Command too long\n");
		return -1;
	}
	r_mem_copyendian (buf+1, (ut8*)&i, 4, ENDIAN);
	memcpy (buf+5, command, i);
	r_socket_write (s, buf, i+5);
	r_socket_flush (s);

	// read
	ret = r_socket_read_block (s, buf, 5);
	if (ret != 5)
		return -1;
	if (buf[0] != (op | RMT_REPLY)) {
		eprintf ("Unexpected system reply\n");
		return -1;
	}
	r_mem_copyendian ((ut8*)&i, buf+1, 4, ENDIAN);
	if (i == -1)
		return -1;
	ret = 0;
	if (i>RMT_MAX) {
		ret = i-RMT_MAX;
		i = RMT_MAX;
	}
	ptr = (char *)malloc (i);
	if (ptr) {
		r_socket_read_block (s, (ut8*)ptr, i);
		j = write (1, ptr, i);
		free (ptr);
	}
	/* Clean */
	if (ret > 0) {
		ret -= r_socket_read (s, (ut8*)buf, RMT_MAX);
	}
	return i-j;
}
Esempio n. 6
0
static int rap__close(RIODesc *fd) {
	int ret = -1;
	if (RIORAP_IS_VALID (fd)) {
		if (RIORAP_FD (fd) != NULL) {
			RIORap *r = fd->data;
			ret = r_socket_close (r->fd);
			ret = r_socket_close (r->client);
			//ret = r_socket_close (r->client);
			free (fd->data);
			fd->data = NULL;
		}
	} else eprintf ("rap__close: fdesc is not a r_io_rap plugin\n");
	return ret;
}
Esempio n. 7
0
static ut64 rap__lseek(struct r_io_t *io, RIODesc *fd, ut64 offset, int whence) {
	RSocket *s = RIORAP_FD (fd);
	int ret;
	ut8 tmp[10];
	// query
	tmp[0] = RMT_SEEK;
	tmp[1] = (ut8)whence;
	r_mem_copyendian (tmp+2, (ut8*)&offset, 8, ENDIAN);
	r_socket_write (s, &tmp, 10);
	r_socket_flush (s);
	// get reply
	ret = r_socket_read_block (s, (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);
	return offset;
}
Esempio n. 8
0
static ut64 rap__lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
	RSocket *s = RIORAP_FD (fd);
	ut8 tmp[10];
	int ret;
	// query
	tmp[0] = RMT_SEEK;
	tmp[1] = (ut8)whence;
	r_write_be64 (tmp + 2, offset);
	r_socket_write (s, &tmp, 10);
	r_socket_flush (s);
	// get reply
	memset (tmp, 0, 9);
	ret = r_socket_read_block (s, (ut8*)&tmp, 9);
	if (ret != 9 || tmp[0] != (RMT_SEEK | RMT_REPLY)) {
		// eprintf ("%d %d  - %02x %02x %02x %02x %02x %02x %02x\n", 
		// ret, whence, tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6]);
		eprintf ("Unexpected lseek reply\n");
		return -1;
	}
	offset = r_read_at_be64 (tmp, 1);
	return offset;
}
Esempio n. 9
0
static int rap__write(struct r_io_t *io, RIODesc *fd, const ut8 *buf, int count) {
	RSocket *s = RIORAP_FD (fd);
	int ret;
	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*)&count, 4, ENDIAN);
	memcpy (tmp+5, buf, count);

	ret = r_socket_write (s, tmp, count+5);
	r_socket_flush (s);
	if (r_socket_read (s, tmp, 5) != 5) { // TODO read_block?
		eprintf ("rap__write: error\n");
		ret = -1;
	}
	free (tmp);
	// TODO: get reply
	return ret;
}
Esempio n. 10
0
static int rap__system(RIO *io, RIODesc *fd, const char *command) {
	int ret, reslen = 0, cmdlen = 0;
	RSocket *s = RIORAP_FD (fd);
	unsigned int i, j = 0;
	char *ptr, *res, *str;
	ut8 buf[RMT_MAX];

	buf[0] = RMT_CMD;
	i = strlen (command) + 1;
	if (i > RMT_MAX - 5) {
		eprintf ("Command too long\n");
		return -1;
	}
	r_write_be32 (buf + 1, i);
	memcpy (buf + 5, command, i);
	r_socket_write (s, buf, i+5);
	r_socket_flush (s);

	/* read reverse cmds */
	for (;;) {
		ret = r_socket_read_block (s, buf, 1);
		if (ret != 1) {
			return -1;
		}
		/* system back in the middle */
		/* TODO: all pkt handlers should check for reverse queries */
		if (buf[0] != RMT_CMD) {
			break;
		}
		// run io->cmdstr
		// return back the string
		buf[0] |= RMT_REPLY;
		memset (buf + 1, 0, 4);
		ret = r_socket_read_block (s, buf + 1, 4);
		cmdlen = r_read_at_be32 (buf, 1);
		if (cmdlen + 1 == 0) // check overflow
			cmdlen = 0;
		str = calloc (1, cmdlen + 1);
		ret = r_socket_read_block (s, (ut8*)str, cmdlen);
		eprintf ("RUN %d CMD(%s)\n", ret, str);
		if (str && *str) {
			res = io->cb_core_cmdstr (io->user, str);
		} else {
			res = strdup ("");
		}
		eprintf ("[%s]=>(%s)\n", str, res);
		reslen = strlen (res);
		free (str);
		r_write_be32 (buf + 1, reslen);
		memcpy (buf + 5, res, reslen);
		free (res);
		r_socket_write (s, buf, reslen + 5);
		r_socket_flush (s);
	}

	// read
	ret = r_socket_read_block (s, buf + 1, 4);
	if (ret != 4) {
		return -1;
	}
	if (buf[0] != (RMT_CMD | RMT_REPLY)) {
		eprintf ("Unexpected rap cmd reply\n");
		return -1;
	}

	i = r_read_at_be32 (buf, 1);
	ret = 0;
	if (i > ST32_MAX) {
		eprintf ("Invalid length\n");
		return -1;
	}
	ptr = (char *)calloc (1, i + 1);
	if (ptr) {
		int ir, tr = 0;
		do {
			ir = r_socket_read_block (s, (ut8*)ptr + tr, i - tr);
			if (ir < 1) break;
			tr += ir;
		} while (tr < i);
		// TODO: use io->cb_printf() with support for \x00
		ptr[i] = 0;
		if (io->cb_printf) {
			io->cb_printf ("%s", ptr);
			j = i;
		} else {
			j = write (1, ptr, i);
		}
		free (ptr);
	}
#if DEAD_CODE
	/* Clean */
	if (ret > 0) {
		ret -= r_socket_read (s, (ut8*)buf, RMT_MAX);
	}
#endif
	return i - j;
}
Esempio n. 11
0
static int rap__system(RIO *io, RIODesc *fd, const char *command) {
	RSocket *s = RIORAP_FD (fd);
	ut8 buf[RMT_MAX];
	char *ptr;
	int op, ret;
	unsigned int i, j = 0;

	// send
	if (*command=='!') {
		op = RMT_SYSTEM;
		command++;
	} else {
		op = RMT_CMD;
	}
	buf[0] = op;
	i = strlen (command)+1;
	if (i>RMT_MAX-5) {
		eprintf ("Command too long\n");
		return -1;
	}
	r_mem_copyendian (buf+1, (ut8*)&i, 4, ENDIAN);
	memcpy (buf+5, command, i);
	r_socket_write (s, buf, i+5);
	r_socket_flush (s);

	/* read reverse cmds */
	for (;;) {
		ret = r_socket_read_block (s, buf, 1);
		if (ret != 1) {
			return -1;
		}
		/* system back in the middle */
		/* TODO: all pkt handlers should check for reverse queries */
		if (buf[0] == RMT_SYSTEM || buf[0] == RMT_CMD) {
			char *res, *str;
			ut32 reslen = 0, cmdlen = 0;
			// run io->cmdstr
			// return back the string
			buf[0] |= RMT_REPLY;
			ret = r_socket_read_block (s, buf+1, 4);
			r_mem_copyendian ((ut8*)&cmdlen, buf+1, 4, ENDIAN);
			if (cmdlen+1==0) // check overflow
				cmdlen = 0;
			str = calloc (1, cmdlen+1);
			ret = r_socket_read_block (s, (ut8*)str, cmdlen);
			//eprintf ("RUN CMD(%s)\n", str);
			res = io->cb_core_cmdstr (io->user, str);
			eprintf ("[%s]=>(%s)\n", str, res);
			reslen = strlen (res);
			free (str);
			r_mem_copyendian ((ut8*)buf+1, (const ut8*)&reslen,
				sizeof(ut32), ENDIAN);
			memcpy (buf+5, res, reslen);
			free (res);
			r_socket_write (s, buf, 5+reslen);
			r_socket_flush (s);
		} else {
			break;
		}
	}

	// read
	ret = r_socket_read_block (s, buf+1, 4);
	if (ret != 4)
		return -1;
	if (buf[0] != (op | RMT_REPLY)) {
		eprintf ("Unexpected system reply\n");
		return -1;
	}

	r_mem_copyendian ((ut8*)&i, buf+1, 4, ENDIAN);
	ret = 0;
	if (i>0xffffffff) {
		eprintf ("Invalid length\n");
		return -1;
	}
	ptr = (char *)malloc (i+1);
	if (ptr) {
		int ir;
		unsigned int tr = 0;
		do {
			ir = r_socket_read_block (s, (ut8*)ptr+tr, i-tr);
			if (ir>0) tr += ir;
			else break;
		} while (tr<i);
		// TODO: use io->cb_printf() with support for \x00
		ptr[i] = 0;
		if (io->cb_printf) {
			io->cb_printf ("%s", ptr);
			j = i;
		} else j = write (1, ptr, i);
		free (ptr);
	}
	/* Clean */
	if (ret > 0) {
		ret -= r_socket_read (s, (ut8*)buf, RMT_MAX);
	}
	return i-j;
}