* Add r_socket_to_string() function in r_socket API

- R_APIzation of r_socket
* Added r_hex_bin2strdup()
  - Bind it into the VAPI
This commit is contained in:
pancake 2009-07-24 13:38:53 +00:00
parent 7e3c696785
commit ce97b40202
6 changed files with 64 additions and 25 deletions

View file

@ -1,13 +1,16 @@
#ifndef _INCLUDE_SOCKET_H_
#define _INCLUDE_SOCKET_H_
int r_socket_ready(int fd, int secs, int usecs);
int r_socket_read(int fd, unsigned char *read, int len);
int r_socket_write(int fd, unsigned char *buf, int len);
int r_socket_connect(char *host, int port);
int r_socket_listen(int port);
int r_socket_accept(int fd);
int r_socket_fgets(int fd, char *buf, int size);
void r_socket_printf(int fd, const char *fmt, ...);
#include "r_types.h"
R_API int r_socket_ready(int fd, int secs, int usecs);
R_API int r_socket_read(int fd, unsigned char *read, int len);
R_API int r_socket_write(int fd, unsigned char *buf, int len);
R_API int r_socket_connect(char *host, int port);
R_API int r_socket_listen(int port);
R_API int r_socket_accept(int fd);
R_API int r_socket_fgets(int fd, char *buf, int size);
R_API void r_socket_printf(int fd, const char *fmt, ...);
R_API char *r_socket_to_string(int fd);
#endif

View file

@ -107,6 +107,7 @@ R_API inline void r_str_concatch(char *x, char y);
R_API int r_hex_pair2bin(const char *arg);
R_API int r_hex_str2bin(const char *in, ut8 *out);
R_API int r_hex_bin2str(const ut8 *in, int len, char *out);
R_API char *r_hex_bin2strdup(const ut8 *in, int len);
R_API int r_hex_to_byte(ut8 *val, ut8 c);
/* file */

View file

@ -1,6 +1,7 @@
/* radare - LGPL - Copyright 2006-2009 pancake<nopcode.org> */
#define USE_SOCKETS
#include <errno.h>
#include "r_types.h"
#include "r_socket.h"
#include <sys/types.h>
@ -24,7 +25,7 @@
#define BUFFER_SIZE 4096
int r_socket_write(int fd, unsigned char *buf, int len)
R_API int r_socket_write(int fd, unsigned char *buf, int len)
{
int delta = 0;
int ret;
@ -48,7 +49,7 @@ int r_socket_write(int fd, unsigned char *buf, int len)
// XXX: rewrite it to use select //
/* waits secs until new data is received. */
/* returns -1 on error, 0 is false, 1 is true */
int r_socket_ready(int fd, int secs,int usecs)
R_API int r_socket_ready(int fd, int secs,int usecs)
{
int ret;
#if __UNIX__
@ -79,7 +80,7 @@ int r_socket_ready(int fd, int secs,int usecs)
#endif
}
void r_socket_block(int fd, int block)
R_API void r_socket_block(int fd, int block)
{
#if _UNIX_
fcntl(fd, F_SETFL, O_NONBLOCK, !block);
@ -88,7 +89,7 @@ void r_socket_block(int fd, int block)
#endif
}
void r_socket_printf(int fd, const char *fmt, ...)
R_API void r_socket_printf(int fd, const char *fmt, ...)
{
#if !__linux__
char buf[BUFFER_SIZE];
@ -109,7 +110,7 @@ void r_socket_printf(int fd, const char *fmt, ...)
}
#if __UNIX__
int r_socket_unix_connect(char *file)
R_API int r_socket_unix_connect(char *file)
{
struct sockaddr_un addr;
int sock;
@ -129,7 +130,7 @@ int r_socket_unix_connect(char *file)
return sock;
}
int r_socket_unix_listen(const char *file)
R_API int r_socket_unix_listen(const char *file)
{
struct sockaddr_un unix_name;
int sock;
@ -158,7 +159,7 @@ int r_socket_unix_listen(const char *file)
}
#endif
int r_socket_connect(char *host, int port)
R_API int r_socket_connect(char *host, int port)
{
struct sockaddr_in sa;
struct hostent *he;
@ -194,7 +195,7 @@ int r_socket_connect(char *host, int port)
return s;
}
int r_socket_listen(int port)
R_API int r_socket_listen(int port)
{
int s;
int ret;
@ -226,7 +227,7 @@ int r_socket_listen(int port)
return s;
}
int r_socket_close(int fd)
R_API int r_socket_close(int fd)
{
#if __UNIX__
shutdown(fd, SHUT_RDWR);
@ -237,7 +238,7 @@ int r_socket_close(int fd)
#endif
}
int r_socket_read(int fd, unsigned char *buf, int len)
R_API int r_socket_read(int fd, unsigned char *buf, int len)
{
#if __UNIX__
return read(fd, buf, len);
@ -246,19 +247,19 @@ int r_socket_read(int fd, unsigned char *buf, int len)
#endif
}
int r_socket_accept(int fd)
R_API int r_socket_accept(int fd)
{
return accept(fd, NULL, NULL);
}
int r_socket_flush(int fd)
R_API int r_socket_flush(int fd)
{
/* TODO */
return 0;
}
int r_socket_fgets(int fd, char *buf, int size)
R_API int r_socket_fgets(int fd, char *buf, int size)
{
int i = 0;
int ret = 0;
@ -282,3 +283,22 @@ int r_socket_fgets(int fd, char *buf, int size)
return ret;
}
R_API char *r_socket_to_string(int fd)
{
char *str = NULL;
struct sockaddr sa;
socklen_t sl = sizeof(sa);
memset(&sa, 0, sizeof(sa));
if (getpeername(fd, &sa, &sl) != 0) {
printf("ERRNO IS %d\n", errno);
perror("getpeername");
} else {
struct sockaddr_in *sain = (struct sockaddr_in*) &sa;
ut8 *a = (ut8*) &(sain->sin_addr);
if ((str = malloc(32)))
sprintf(str, "%d.%d.%d.%d:%d",
a[0],a[1],a[2],a[3], ntohs(sain->sin_port));
}
return str;
}

View file

@ -5,7 +5,7 @@
#include <stdio.h>
/* int c; ret = hex_to_byet(&c, 'c'); */
int r_hex_to_byte(ut8 *val, ut8 c)
R_API int r_hex_to_byte(ut8 *val, ut8 c)
{
if ('0' <= c && c <= '9') *val = (unsigned char)(*val) * 16 + ( c - '0');
else if (c >= 'A' && c <= 'F') *val = (unsigned char)(*val) * 16 + ( c - 'A' + 10);
@ -15,7 +15,7 @@ int r_hex_to_byte(ut8 *val, ut8 c)
}
/* int byte = hexpair2bin("A0"); */
int r_hex_pair2bin(const char *arg) // (0A) => 10 || -1 (on error)
R_API int r_hex_pair2bin(const char *arg) // (0A) => 10 || -1 (on error)
{
unsigned char *ptr;
unsigned char c = '\0';
@ -37,7 +37,7 @@ int r_hex_pair2bin(const char *arg) // (0A) => 10 || -1 (on error)
return (int)c;
}
int r_hex_bin2str(const ut8 *in, int len, char *out)
R_API int r_hex_bin2str(const ut8 *in, int len, char *out)
{
int i;
char tmp[5];
@ -48,6 +48,19 @@ int r_hex_bin2str(const ut8 *in, int len, char *out)
}
return len;
}
R_API char *r_hex_bin2strdup(const ut8 *in, int len)
{
int i;
char tmp[5];
char *out = malloc((len+1)*2);
out[0]='\0';
for(i=0;i<len;i++) {
sprintf(tmp, "%02x", in[i]);
strcat(out, tmp);
}
return out;
}
/* char buf[1024]; int len = hexstr2binstr("0a 33 45", buf); */
// XXX control out bytes
int r_hex_str2bin(const char *in, ut8 *out) // 0A 3B 4E A0

View file

@ -14,4 +14,5 @@ namespace Radare.Socket {
public int fgets(int fd, string *buf, int len);
public int printf(int fd, string *str, ...);
public int accept(int fd);
public string to_string(int fd);
}

View file

@ -6,7 +6,8 @@ namespace Radare {
[CCode (cprefix="r_")]
public static class Util {
public static int hex_str2bin (string input, out uint8 *buf);
public static int hex_bin2str (uint8 *buf, int len, out string str);
//public static int hex_bin2str (uint8 *buf, int len, out string str);
public static string hex_bin2strdup (uint8 *buf, int len);
/* mem */
public static uint8 *mem_mem (uint8 *a, int al, uint8 *b, int bl);
public static void mem_copyendian (uint8 *dest, uint8 *orig, int size, int endian);