* Initial import RSocket refactor

- Add SSL client support and testcase
* Update TODO
* Rename some r_hash structs to avoid openssl.h conflict
* io_rap plugin its broken now ... refactor time
This commit is contained in:
earada 2011-03-21 01:47:17 +01:00
parent 44fa1dc253
commit 498e531479
19 changed files with 466 additions and 306 deletions

2
TODO
View File

@ -68,10 +68,12 @@ earada
------
* p7 : 7bit encoding (sms)
* Add print support for bitfields (pm b...)
* Fix io_haret memory dump
* r_socket
- Add SSL support
- http API in r_socket module
- allow to get/post data/files with continue on plain/ssl
* refactor rap and raps
* remove all uses of alloca() // mingw and grep reports them all :)
* typedef all function pointers, like in r_bp
* Implement case-insensitive search (e search.casematters ?) any better name? Use /i?

View File

@ -473,11 +473,12 @@ R_API RAnalOp *r_core_op_anal(RCore *core, ut64 addr) {
// TODO: move into core/io/rap? */
R_API int r_core_serve(RCore *core, RIODesc *file) {
ut8 cmd, flg, *ptr, buf[1024];
int i, c, pipefd, fd;
int i, pipefd;
ut64 x;
RSocket *c, *fd;
fd = file->fd;
if (fd == -1) {
fd = (RSocket *)file->data;
if (fd == NULL) {
eprintf ("rap: cannot listen.\n");
return -1;
}
@ -491,14 +492,14 @@ R_API int r_core_serve(RCore *core, RIODesc *file) {
reaccept:
core->io->plugin = NULL;
while ((c = r_socket_accept (fd))) {
if (c == -1) {
if (c == NULL) {
eprintf ("rap: cannot accept\n");
r_socket_close (c);
return -1;
}
eprintf ("rap: client connected\n");
r_io_accept (core->io, c);
r_io_accept (core->io, c->fd);
for (;;) {
i = r_socket_read (c, &cmd, 1);
if (i==0) {
@ -557,7 +558,7 @@ reaccept:
r_core_block_size (core, i);
r_mem_copyendian (ptr+1, (ut8 *)&i, 4, !endian);
memcpy (ptr+5, core->block, i); //core->blocksize);
write (c, ptr, i+5);
r_socket_write (c, ptr, i+5);
}
break;
case RMT_CMD:
@ -596,10 +597,10 @@ reaccept:
break;
}
case RMT_WRITE:
read (c, buf, 5);
r_socket_read (c, buf, 5);
r_mem_copyendian((ut8 *)&x, buf+1, 4, endian);
ptr = malloc (x);
read (c, ptr, x);
r_socket_read (c, ptr, x);
r_core_write_at (core, core->offset, ptr, x);
free (ptr);
break;
@ -620,7 +621,8 @@ reaccept:
r_socket_read_block (c, buf, 4);
r_mem_copyendian ((ut8*)&i, buf, 4, endian);
{
int ret = r_socket_close (i);
//FIXME: Use r_socket_close
int ret = close (i);
r_mem_copyendian (buf+1, (ut8*)&ret, 4, !endian);
buf[0] = RMT_CLOSE | RMT_REPLY;
r_socket_write (c, buf, 5);

View File

@ -23,7 +23,7 @@ R_API void r_core_rtr_pushout(RCore *core, const char *input) {
const char *cmd = NULL;
char *str = NULL;
if (fd) {
for (rtr_n = 0; rtr_host[rtr_n].fd != fd \
for (rtr_n = 0; rtr_host[rtr_n].fd->fd != fd \
&& rtr_n < RTR_MAX_HOSTS; rtr_n++);
if (!(cmd = strchr (input, ' '))) {
eprintf ("Error\n");
@ -31,7 +31,7 @@ R_API void r_core_rtr_pushout(RCore *core, const char *input) {
}
} else cmd = input;
if (!rtr_host[rtr_n].fd) {
if (!rtr_host[rtr_n].fd->fd) {
eprintf("Error: Unknown host\n");
return;
}
@ -58,7 +58,7 @@ R_API void r_core_rtr_list(RCore *core) {
int i;
for (i = 0; i < RTR_MAX_HOSTS; i++)
if (rtr_host[i].fd) {
r_cons_printf("%i - ", rtr_host[i].fd);
r_cons_printf("%i - ", rtr_host[i].fd->fd);
if (rtr_host[i].proto == RTR_PROT_TCP)
r_cons_printf("tcp://");
else if (rtr_host[i].proto == RTR_PROT_UDP)
@ -71,7 +71,8 @@ R_API void r_core_rtr_list(RCore *core) {
R_API void r_core_rtr_add(RCore *core, const char *_input) {
char input[1024], *host = NULL, *file = NULL, *ptr = NULL, buf[1024];
int proto, port, fd, i;
int proto, port, i;
RSocket *fd;
strncpy (input, _input, sizeof (input)-4);
/* Parse uri */
@ -108,8 +109,8 @@ R_API void r_core_rtr_add(RCore *core, const char *_input) {
switch (proto) {
case RTR_PROT_RAP:
fd = r_socket_connect (host, port);
if (fd == -1) {
fd = r_socket_new (host, port, R_FALSE); //TODO: Use rap.ssl
if (fd == NULL) {
eprintf ("Error: Cannot connect to '%s' (%d)\n", host, port);
return;
}
@ -131,16 +132,16 @@ R_API void r_core_rtr_add(RCore *core, const char *_input) {
eprintf ("ok\n");
break;
case RTR_PROT_TCP:
fd = r_socket_connect (host, port);
if (fd == -1) {
fd = r_socket_new (host, port, R_FALSE); //TODO: Use rap.ssl
if (fd == NULL) {
eprintf ("Error: Cannot connect to '%s' (%d)\n", host, port);
return;
}
eprintf ("Connected to: %s at port %d\n", host, port);
break;
case RTR_PROT_UDP:
fd = r_socket_udp_connect (host, port);
if (fd == -1) {
fd = r_socket_udp_connect (host, port, R_FALSE); //TODO: Use rap.ssl
if (fd == NULL) {
eprintf("Error: Cannot connect to '%s' (%d)\n", host, port);
return;
}
@ -168,9 +169,9 @@ R_API void r_core_rtr_remove(RCore *core, const char *input) {
if (input[0] >= '0' && input[0] <= '9') {
fd = r_num_math (core->num, input);
for (i = 0; i < RTR_MAX_HOSTS; i++)
if (rtr_host[i].fd == fd) {
r_socket_close (rtr_host[i].fd);
rtr_host[i].fd = 0;
if (rtr_host[i].fd->fd == fd) {
r_socket_free (rtr_host[i].fd);
rtr_host[i].fd = NULL;
if (rtr_n == i)
for (rtr_n = 0; !rtr_host[rtr_n].fd && rtr_n < RTR_MAX_HOSTS; rtr_n++);
break;
@ -178,7 +179,7 @@ R_API void r_core_rtr_remove(RCore *core, const char *input) {
} else {
for (i = 0; i < RTR_MAX_HOSTS; i++)
if (rtr_host[i].fd)
r_socket_close (rtr_host[i].fd);
r_socket_free (rtr_host[i].fd);
memset (rtr_host, '\0', RTR_MAX_HOSTS * sizeof(RCoreRtrHost));
rtr_n = 0;
}
@ -190,11 +191,11 @@ R_API void r_core_rtr_session(RCore *core, const char *input) {
if (input[0] >= '0' && input[0] <= '9') {
fd = r_num_math (core->num, input);
for (rtr_n = 0; rtr_host[rtr_n].fd != fd && rtr_n < RTR_MAX_HOSTS; rtr_n++);
for (rtr_n = 0; rtr_host[rtr_n].fd->fd != fd && rtr_n < RTR_MAX_HOSTS; rtr_n++);
}
for (;;) {
snprintf (prompt, sizeof (prompt), "fd:%d> ", rtr_host[rtr_n].fd);
snprintf (prompt, sizeof (prompt), "fd:%d> ", rtr_host[rtr_n].fd->fd);
r_line_singleton ()->prompt = prompt;
if ((r_cons_fgets (buf, sizeof (buf), 0, NULL))) {
if (*buf == 'q')
@ -215,7 +216,7 @@ R_API void r_core_rtr_cmd(RCore *core, const char *input) {
int i, cmd_len, fd = atoi (input);
if (fd != 0) {
for (rtr_n = 0; rtr_host[rtr_n].fd != fd && rtr_n < RTR_MAX_HOSTS; rtr_n++);
for (rtr_n = 0; rtr_host[rtr_n].fd->fd != fd && rtr_n < RTR_MAX_HOSTS; rtr_n++);
if (!(cmd = strchr (input, ' '))) {
eprintf("Error\n");
return;

View File

@ -38,7 +38,7 @@
#define SHA_ROT(X,n) (((X) << (n)) | ((X) >> (32-(n))))
static void shaHashBlock(SHA_CTX *ctx)
static void shaHashBlock(R_SHA_CTX *ctx)
{
int t;
unsigned int A,B,C,D,E,TEMP;
@ -77,7 +77,7 @@ static void shaHashBlock(SHA_CTX *ctx)
ctx->H[4] += E;
}
void SHA1_Init(SHA_CTX *ctx) {
void SHA1_Init(R_SHA_CTX *ctx) {
int i;
ctx->lenW = 0;
@ -95,7 +95,7 @@ void SHA1_Init(SHA_CTX *ctx) {
ctx->W[i] = 0;
}
void SHA1_Update(SHA_CTX *ctx, const void *_dataIn, int len) {
void SHA1_Update(R_SHA_CTX *ctx, const void *_dataIn, int len) {
const unsigned char *dataIn = _dataIn;
int i;
@ -113,7 +113,7 @@ void SHA1_Update(SHA_CTX *ctx, const void *_dataIn, int len) {
}
}
void SHA1_Final(unsigned char hashout[20], SHA_CTX *ctx)
void SHA1_Final(unsigned char hashout[20], R_SHA_CTX *ctx)
{
unsigned char pad0x80 = 0x80;
unsigned char pad0x00 = 0x00;

View File

@ -42,6 +42,6 @@ typedef struct {
} SHA_CTX;
#endif
void SHA1_Init(SHA_CTX *ctx);
void SHA1_Update(SHA_CTX *ctx, const void *dataIn, int len);
void SHA1_Final(unsigned char hashout[20], SHA_CTX *ctx);
void SHA1_Init(R_SHA_CTX *ctx);
void SHA1_Update(R_SHA_CTX *ctx, const void *dataIn, int len);
void SHA1_Final(unsigned char hashout[20], R_SHA_CTX *ctx);

View File

@ -228,9 +228,9 @@ typedef u_int64_t sha2_word64; /* Exactly 8 bytes */
* library -- they are intended for private internal visibility/use
* only.
*/
void SHA512_Last(SHA512_CTX*);
void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
void SHA512_Last(R_SHA512_CTX*);
void SHA256_Transform(R_SHA256_CTX*, const sha2_word32*);
void SHA512_Transform(R_SHA512_CTX*, const sha2_word64*);
/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
@ -342,8 +342,8 @@ static const char *sha2_hex_digits = "0123456789abcdef";
/*** SHA-256: *********************************************************/
void SHA256_Init(SHA256_CTX* context) {
if (context == (SHA256_CTX*)0) {
void SHA256_Init(R_SHA256_CTX* context) {
if (context == (R_SHA256_CTX*)0) {
return;
}
MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
@ -388,7 +388,7 @@ void SHA256_Init(SHA256_CTX* context) {
(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
j++
void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
void SHA256_Transform(R_SHA256_CTX* context, const sha2_word32* data) {
sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
sha2_word32 T1, *W256;
int j;
@ -446,7 +446,7 @@ void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
#else /* SHA2_UNROLL_TRANSFORM */
void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
void SHA256_Transform(R_SHA256_CTX* context, const sha2_word32* data) {
sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
sha2_word32 T1, T2, *W256;
int j;
@ -526,7 +526,7 @@ void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
#endif /* SHA2_UNROLL_TRANSFORM */
void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
void SHA256_Update(R_SHA256_CTX* context, const sha2_byte *data, size_t len) {
unsigned int freespace, usedspace;
if (len == 0) {
@ -535,7 +535,7 @@ void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
}
/* Sanity check: */
assert(context != (SHA256_CTX*)0 && data != (sha2_byte*)0);
assert(context != (R_SHA256_CTX*)0 && data != (sha2_byte*)0);
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
if (usedspace > 0) {
@ -574,12 +574,12 @@ void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
usedspace = freespace = 0;
}
void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
void SHA256_Final(sha2_byte digest[], R_SHA256_CTX* context) {
sha2_word32 *d = (sha2_word32*)digest;
unsigned int usedspace;
/* Sanity check: */
assert(context != (SHA256_CTX*)0);
assert(context != (R_SHA256_CTX*)0);
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != (sha2_byte*)0) {
@ -637,12 +637,12 @@ void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
usedspace = 0;
}
char *SHA256_End(SHA256_CTX* context, char buffer[]) {
char *SHA256_End(R_SHA256_CTX* context, char buffer[]) {
sha2_byte digest[SHA256_DIGEST_LENGTH], *d = digest;
int i;
/* Sanity check: */
assert(context != (SHA256_CTX*)0);
assert(context != (R_SHA256_CTX*)0);
if (buffer != (char*)0) {
SHA256_Final(digest, context);
@ -661,7 +661,7 @@ char *SHA256_End(SHA256_CTX* context, char buffer[]) {
}
char* SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
SHA256_CTX context;
R_SHA256_CTX context;
SHA256_Init(&context);
SHA256_Update(&context, data, len);
@ -670,8 +670,8 @@ char* SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_S
/*** SHA-512: *********************************************************/
void SHA512_Init(SHA512_CTX* context) {
if (context == (SHA512_CTX*)0) {
void SHA512_Init(R_SHA512_CTX* context) {
if (context == (R_SHA512_CTX*)0) {
return;
}
MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
@ -715,7 +715,7 @@ void SHA512_Init(SHA512_CTX* context) {
(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
j++
void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
void SHA512_Transform(R_SHA512_CTX* context, const sha2_word64* data) {
sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
int j;
@ -770,7 +770,7 @@ void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
#else /* SHA2_UNROLL_TRANSFORM */
void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
void SHA512_Transform(R_SHA512_CTX* context, const sha2_word64* data) {
sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
int j;
@ -848,7 +848,7 @@ void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
#endif /* SHA2_UNROLL_TRANSFORM */
void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
void SHA512_Update(R_SHA512_CTX* context, const sha2_byte *data, size_t len) {
unsigned int freespace, usedspace;
if (len == 0) {
@ -857,7 +857,7 @@ void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
}
/* Sanity check: */
assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
assert(context != (R_SHA512_CTX*)0 && data != (sha2_byte*)0);
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
if (usedspace > 0) {
@ -896,7 +896,7 @@ void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
usedspace = freespace = 0;
}
void SHA512_Last(SHA512_CTX* context) {
void SHA512_Last(R_SHA512_CTX* context) {
unsigned int usedspace;
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
@ -937,11 +937,11 @@ void SHA512_Last(SHA512_CTX* context) {
SHA512_Transform(context, (sha2_word64*)context->buffer);
}
void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
void SHA512_Final(sha2_byte digest[], R_SHA512_CTX* context) {
sha2_word64 *d = (sha2_word64*)digest;
/* Sanity check: */
assert(context != (SHA512_CTX*)0);
assert(context != (R_SHA512_CTX*)0);
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != (sha2_byte*)0) {
@ -966,12 +966,12 @@ void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
MEMSET_BZERO(context, sizeof(context));
}
char *SHA512_End(SHA512_CTX* context, char buffer[]) {
char *SHA512_End(R_SHA512_CTX* context, char buffer[]) {
sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
int i;
/* Sanity check: */
assert(context != (SHA512_CTX*)0);
assert(context != (R_SHA512_CTX*)0);
if (buffer != (char*)0) {
SHA512_Final(digest, context);
@ -990,7 +990,7 @@ char *SHA512_End(SHA512_CTX* context, char buffer[]) {
}
char* SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
SHA512_CTX context;
R_SHA512_CTX context;
SHA512_Init(&context);
SHA512_Update(&context, data, len);
@ -999,8 +999,8 @@ char* SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_S
/*** SHA-384: *********************************************************/
void SHA384_Init(SHA384_CTX* context) {
if (context == (SHA384_CTX*)0) {
void SHA384_Init(R_SHA384_CTX* context) {
if (context == (R_SHA384_CTX*)0) {
return;
}
MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
@ -1008,19 +1008,19 @@ void SHA384_Init(SHA384_CTX* context) {
context->bitcount[0] = context->bitcount[1] = 0;
}
void SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
SHA512_Update((SHA512_CTX*)context, data, len);
void SHA384_Update(R_SHA384_CTX* context, const sha2_byte* data, size_t len) {
SHA512_Update((R_SHA512_CTX*)context, data, len);
}
void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
void SHA384_Final(sha2_byte digest[], R_SHA384_CTX* context) {
sha2_word64 *d = (sha2_word64*)digest;
/* Sanity check: */
assert(context != (SHA384_CTX*)0);
assert(context != (R_SHA384_CTX*)0);
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != (sha2_byte*)0) {
SHA512_Last((SHA512_CTX*)context);
SHA512_Last((R_SHA512_CTX*)context);
/* Save the hash data for output: */
#if BYTE_ORDER == LITTLE_ENDIAN
@ -1041,12 +1041,12 @@ void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
MEMSET_BZERO(context, sizeof(context));
}
char *SHA384_End(SHA384_CTX* context, char buffer[]) {
char *SHA384_End(R_SHA384_CTX* context, char buffer[]) {
sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
int i;
/* Sanity check: */
assert(context != (SHA384_CTX*)0);
assert(context != (R_SHA384_CTX*)0);
if (buffer != (char*)0) {
SHA384_Final(digest, context);
@ -1065,7 +1065,7 @@ char *SHA384_End(SHA384_CTX* context, char buffer[]) {
}
char* SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
SHA384_CTX context;
R_SHA384_CTX context;
SHA384_Init(&context);
SHA384_Update(&context, data, len);

View File

@ -135,42 +135,42 @@ typedef SHA512_CTX SHA384_CTX;
#ifndef NOPROTO
#ifdef SHA2_USE_INTTYPES_H
void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
void SHA256_Init(R_SHA256_CTX *);
void SHA256_Update(R_SHA256_CTX*, const uint8_t*, size_t);
void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], R_SHA256_CTX*);
char* SHA256_End(R_SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
void SHA384_Init(SHA384_CTX*);
void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
void SHA384_Init(R_SHA384_CTX*);
void SHA384_Update(R_SHA384_CTX*, const uint8_t*, size_t);
void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], R_SHA384_CTX*);
char* SHA384_End(R_SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
void SHA512_Init(SHA512_CTX*);
void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
void SHA512_Init(R_SHA512_CTX*);
void SHA512_Update(R_SHA512_CTX*, const uint8_t*, size_t);
void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], R_SHA512_CTX*);
char* SHA512_End(R_SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
#else /* SHA2_USE_INTTYPES_H */
void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t);
void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
void SHA256_Init(R_SHA256_CTX *);
void SHA256_Update(R_SHA256_CTX*, const u_int8_t*, size_t);
void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], R_SHA256_CTX*);
char* SHA256_End(R_SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
void SHA384_Init(SHA384_CTX*);
void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t);
void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
void SHA384_Init(R_SHA384_CTX*);
void SHA384_Update(R_SHA384_CTX*, const u_int8_t*, size_t);
void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], R_SHA384_CTX*);
char* SHA384_End(R_SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
void SHA512_Init(SHA512_CTX*);
void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t);
void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
void SHA512_Init(R_SHA512_CTX*);
void SHA512_Update(R_SHA512_CTX*, const u_int8_t*, size_t);
void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], R_SHA512_CTX*);
char* SHA512_End(R_SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
#endif /* SHA2_USE_INTTYPES_H */

View File

@ -19,6 +19,7 @@
#include "r_flags.h"
#include "r_config.h"
#include "r_bin.h"
#include "r_socket.h"
#define R_CORE_CMD_EXIT -2
#define R_CORE_BLOCKSIZE 64
@ -44,7 +45,7 @@ typedef struct r_core_rtr_host_t {
char host[512];
int port;
char file[1024];
int fd;
RSocket *fd;
} RCoreRtrHost;
/* rtr */

View File

@ -15,14 +15,14 @@ typedef struct {
unsigned int W[80];
int lenW;
unsigned int sizeHi, sizeLo;
} SHA_CTX;
} R_SHA_CTX;
#define SHA256_BLOCK_LENGTH 64
typedef struct _SHA256_CTX {
ut32 state[8];
ut64 bitcount;
ut8 buffer[SHA256_BLOCK_LENGTH];
} SHA256_CTX;
} R_SHA256_CTX;
#define SHA384_BLOCK_LENGTH 128
#define SHA512_BLOCK_LENGTH 128
@ -30,15 +30,15 @@ typedef struct _SHA512_CTX {
ut64 state[8];
ut64 bitcount[2];
ut8 buffer[SHA512_BLOCK_LENGTH];
} SHA512_CTX;
typedef SHA512_CTX SHA384_CTX;
} R_SHA512_CTX;
typedef R_SHA512_CTX R_SHA384_CTX;
typedef struct r_hash_t {
MD5_CTX md5;
SHA_CTX sha1;
SHA256_CTX sha256;
SHA384_CTX sha384;
SHA512_CTX sha512;
R_SHA_CTX sha1;
R_SHA256_CTX sha256;
R_SHA384_CTX sha384;
R_SHA512_CTX sha512;
int rst;
ut8 digest[128];
} RHash;

View File

@ -1,33 +1,39 @@
#ifndef _INCLUDE_SOCKET_H_
#define _INCLUDE_SOCKET_H_
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "r_types.h"
/* TODO: major refactoring of r_socket */
/* make it work like the rest of apis */
/* struct r_socket_t *sock = r_socket_new(R_SOCKET_TCP, "gogle.com", 80); */
/* struct r_socket_t *sock = r_socket_proc_new(R_SOCKET_PROCESS, "/bin/ls", 80); */
typedef struct r_socket_t {
int fd;
int is_ssl;
SSL_CTX *ctx;
SSL *sfd;
} RSocket;
#ifdef R_API
R_API RSocket *r_socket_new (const char *host, int port, int is_ssl);
R_API void r_socket_free (RSocket *s);
#if __UNIX__
R_API int r_socket_unix_connect(const char *file);
R_API int r_socket_unix_listen(const char *file);
R_API RSocket *r_socket_unix_connect (const char *file);
R_API int r_socket_unix_listen (const char *file);
#endif
R_API int r_socket_udp_connect(const char *host, int port);
R_API int r_socket_flush(int fd);
R_API void r_socket_block(int fd, int block);
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_read_block(int fd, unsigned char *buf, int len);
R_API int r_socket_puts(int fd, char *buf);
R_API int r_socket_write(int fd, void *buf, int len);
R_API int r_socket_connect(const 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_gets(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);
R_API int r_socket_close(int fd);
R_API int r_socket_connect (const char *host, int port);
R_API RSocket *r_socket_listen (int port);
R_API void r_socket_block (RSocket *s, int block);
R_API RSocket *r_socket_accept (RSocket *s);
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, int 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, ...);
R_API int r_socket_read (RSocket *s, unsigned char *read, int len);
R_API int r_socket_read_block (RSocket *s, unsigned char *buf, int len);
R_API int r_socket_gets (RSocket *s, char *buf, int size);
/* process */
typedef struct r_socket_proc_t {
@ -38,11 +44,11 @@ typedef struct r_socket_proc_t {
R_API RSocketProc *r_socket_proc_open(char *const argv[]);
R_API int r_socket_proc_close(RSocketProc *sp);
#define r_socket_proc_read(x,y,z) r_socket_read(x->fd1[0],y,z)
#define r_socket_proc_gets(x,y,z) r_socket_gets(x->fd1[0],y,z)
#define r_socket_proc_write(x,y,z) r_socket_write(x->fd0[1],y,z)
#define r_socket_proc_printf(x,y) r_socket_printf(x->fd0[1],y)
#define r_socket_proc_ready(x,y,z) r_socket_ready(x->fd1[0],y,z)
R_API int r_socket_proc_read (RSocketProc *sp, unsigned char *buf, int len);
R_API int r_socket_proc_gets (RSocketProc *sp, char *buf, int size);
R_API int r_socket_proc_write (RSocketProc *sp, void *buf, int len);
R_API void r_socket_proc_printf (RSocketProc *sp, const char *fmt, ...);
R_API int r_socket_proc_ready (RSocketProc *sp, int secs, int usecs);
#endif
#endif

View File

@ -13,6 +13,7 @@
#define R2_VERSION "@VERSION@"
#define HAVE_LIB_MAGIC @HAVE_LIB_MAGIC@
#define HAVE_LIB_SSL @HAVE_LIB_SSL@
#define CPU_ENDIAN @BIG_ENDIAN@

View File

@ -5,7 +5,7 @@
#include "r_socket.h"
#include <sys/types.h>
#define HARET_FD(x) ((int)((size_t)x->data))
#define HARET_FD(x) ((RSocket*)(x->data))
//#if (sizeof(int)) > (sizeof(void*))
//#error WTF int>ptr? wrong compiler or architecture?
@ -18,11 +18,11 @@ static int haret__write(struct r_io_t *io, RIODesc *fd, const ut8 *buf, int coun
return 0;
}
static void haret_wait_until_prompt(int ufd) {
static void haret_wait_until_prompt(RSocket *s) {
unsigned char buf;
int off = 0;
for (;;) {
if (r_socket_read (ufd, &buf, 1) != 1) {
if (r_socket_read (s, &buf, 1) != 1) {
eprintf ("haret_wait_until_prompt: Unexpected eof in socket\n");
return;
}
@ -37,32 +37,32 @@ 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);
RSocket *s = HARET_FD (fd);
off = io->off & -4;
sprintf (tmp, "pdump 0x%"PFMT64x" %i\r\n", off, count+4);
r_socket_write (ufd, tmp, strlen (tmp));
r_socket_read_block (ufd, (unsigned char *) tmp, strlen (tmp)+1);
r_socket_write (s, tmp, strlen (tmp));
r_socket_read_block (s, (unsigned char *) tmp, strlen (tmp)+1);
j = (io->off - off)*2;
while (i<count && j >= 0) {
r_socket_read_block (ufd, (ut8*) tmp, 11);
r_socket_read_block (ufd, (ut8*) tmp, 35);
r_socket_read_block (s, (ut8*) tmp, 11);
r_socket_read_block (s, (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 (ufd, (unsigned char *) tmp, 21);
r_socket_read_block (s, (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 (ufd);
haret_wait_until_prompt (s);
return i;
}
static int haret__close(RIODesc *fd) {
if (!fd || HARET_FD (fd)==-1)
if (!fd || HARET_FD (fd)->fd==-1)
return -1;
return r_socket_close (HARET_FD (fd));
}
@ -73,7 +73,8 @@ static int haret__plugin_open(struct r_io_t *io, const char *pathname) {
static RIODesc *haret__open(struct r_io_t *io, const char *pathname, int rw, int mode) {
char *port, *ptr, buf[1024];
int p, ufd;
int p;
RSocket *s;
strncpy (buf, pathname, sizeof (buf)-1);
if (haret__plugin_open (io, pathname)) {
@ -84,12 +85,12 @@ static RIODesc *haret__open(struct r_io_t *io, const char *pathname, int rw, int
}
*port = 0;
p = atoi (port+1);
if ((ufd = r_socket_connect (ptr, p)) == -1) {
if ((s = r_socket_new (ptr, p, R_FALSE)) == NULL) {
eprintf ("Cannot connect to '%s' (%d)\n", ptr, p);
return NULL;
} else eprintf ("Connected to: %s at port %d\n", ptr, p);
haret_wait_until_prompt (ufd);
return r_io_desc_new (&r_io_plugin_haret, ufd, pathname, rw, mode, (void*)(size_t)ufd);
haret_wait_until_prompt (s);
return r_io_desc_new (&r_io_plugin_haret, s->fd, pathname, rw, mode, (void*)s);
}
return NULL;
}

View File

@ -8,7 +8,7 @@ int main()
r_cons_new();
p = r_print_new();
r_print_hexdump(p, (ut64)(ut32)(main), buf, 128, 16, 1);
r_print_hexdump(p, (ut64)(main), buf, 128, 16, 1);
r_cons_flush();
return 0;

View File

@ -3,6 +3,7 @@ include ../config.mk
NAME=r_socket
#DEPS=r_util
OBJ=socket.o proc.o
LDFLAGS+=-lssl
# on solaris only
ifeq (${OSTYPE},solaris)

View File

@ -8,6 +8,8 @@
#include <sys/wait.h>
#endif
#define BUFFER_SIZE 4096
R_API struct r_socket_proc_t *r_socket_proc_open(char *const argv[]) {
#if __UNIX__
RSocketProc *sp = R_NEW (RSocketProc);
@ -69,3 +71,45 @@ R_API int r_socket_proc_close(struct r_socket_proc_t *sp) {
#endif
return 0;
}
R_API int r_socket_proc_read (RSocketProc *sp, unsigned char *buf, int len) {
RSocket s;
s.is_ssl = R_FALSE;
s.fd = sp->fd1[0];
return r_socket_read (&s, buf, len);
}
R_API int r_socket_proc_gets (RSocketProc *sp, char *buf, int size) {
RSocket s;
s.is_ssl = R_FALSE;
s.fd = sp->fd1[0];
return r_socket_gets (&s, buf, size);
}
R_API int r_socket_proc_write (RSocketProc *sp, void *buf, int len) {
RSocket s;
s.is_ssl = R_FALSE;
s.fd = sp->fd0[1];
return r_socket_write (&s, buf, len);
}
R_API void r_socket_proc_printf (RSocketProc *sp, const char *fmt, ...) {
RSocket s;
char buf[BUFFER_SIZE];
va_list ap;
s.is_ssl = R_FALSE;
s.fd = sp->fd0[1];
if (s.fd >= 0) {
va_start (ap, fmt);
vsnprintf (buf, BUFFER_SIZE, fmt, ap);
r_socket_write (&s, buf, strlen(buf));
va_end (ap);
}
}
R_API int r_socket_proc_ready (RSocketProc *sp, int secs, int usecs) {
RSocket s;
s.is_ssl = R_FALSE;
s.fd = sp->fd1[0];
return r_socket_ready (&s, secs, usecs);
}

View File

@ -26,92 +26,53 @@
#define BUFFER_SIZE 4096
R_API int r_socket_write(int fd, void *buf, int len) {
int ret, delta = 0;
for (;;) {
ret = send (fd, buf+delta, len, 0);
if (ret == 0)
return -1;
if (ret == len)
return len;
if (ret<0)
break;
delta += ret;
len -= ret;
}
if (ret == -1)
return -1;
return delta;
}
R_API int r_socket_puts(int fd, char *buf) {
return r_socket_write (fd, buf, strlen (buf));
}
// XXX: rewrite it to use select //
/* waits secs until new data is received. */
/* returns -1 on error, 0 is false, 1 is true */
R_API int r_socket_ready(int fd, int secs, int usecs) {
#if __UNIX__
int ret;
struct pollfd fds[1];
fds[0].fd = fd;
fds[0].events = POLLIN|POLLPRI;
fds[0].revents = POLLNVAL|POLLHUP|POLLERR;
ret = poll((struct pollfd *)&fds, 1, usecs);
return ret;
#elif __WINDOWS__
fd_set rfds;
struct timeval tv;
int retval;
if (fd==-1)
return -1;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = secs;
tv.tv_usec = usecs;
if (select (1, &rfds, NULL, NULL, &tv) == -1)
return -1;
return FD_ISSET (0, &rfds);
#else
return R_TRUE; /* always ready if unknown */
R_API RSocket *r_socket_new (const char *host, int port, int is_ssl) {
RSocket *s = R_NEW (RSocket);
s->is_ssl = is_ssl;
if ((s->fd = r_socket_connect (host, port)) < 0)
return NULL;
if (is_ssl) {
s->sfd = NULL;
s->ctx = NULL;
#ifdef HAVE_LIB_SSL
if (!SSL_library_init ())
return NULL;
SSL_load_error_strings ();
s->ctx = SSL_CTX_new (SSLv23_client_method ());
if (s->ctx == NULL)
return NULL;
s->sfd = SSL_new (s->ctx);
SSL_set_fd (s->sfd, s->fd);
if (SSL_connect (s->sfd) != 1)
return NULL;
#endif
}
R_API void r_socket_block(int fd, int block) {
#if __UNIX__
fcntl (fd, F_SETFL, O_NONBLOCK, !block);
#elif __WINDOWS__
ioctlsocket (fd, FIONBIO, (u_long FAR*)&block);
#endif
}
R_API void r_socket_printf(int fd, const char *fmt, ...) {
char buf[BUFFER_SIZE];
va_list ap;
if (fd >= 0) {
va_start (ap, fmt);
vsnprintf (buf, BUFFER_SIZE, fmt, ap);
r_socket_write (fd, buf, strlen(buf));
va_end (ap);
}
return s;
}
R_API void r_socket_free (RSocket *s) {
r_socket_close (s);
free (s);
}
#if __UNIX__
R_API int r_socket_unix_connect(const char *file) {
R_API RSocket *r_socket_unix_connect(const char *file) {
RSocket *s = R_NEW (RSocket);
struct sockaddr_un addr;
int sock = socket (PF_UNIX, SOCK_STREAM, 0);
if (sock <0)
return -1;
if (sock < 0)
return NULL;
// TODO: set socket options
addr.sun_family = AF_UNIX;
strncpy (addr.sun_path, file, sizeof(addr.sun_path));
if (connect (sock, (struct sockaddr *)&addr, sizeof(addr))==-1) {
close (sock);
return -1;
return NULL;
}
return sock;
s->fd =sock;
s->is_ssl = R_FALSE;
return s;
}
R_API int r_socket_unix_listen(const char *file) {
@ -180,110 +141,121 @@ R_API int r_socket_connect(const char *host, int port) {
return s;
}
R_API int r_socket_listen(int port) {
int s;
R_API RSocket *r_socket_listen(int port) {
RSocket *s;
int fd;
struct sockaddr_in sa;
struct linger linger = { 0 };
if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
return -1;
if ((fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
return NULL;
linger.l_onoff = 1;
linger.l_linger = 1;
setsockopt (s, SOL_SOCKET, SO_LINGER, (const char *) &linger, sizeof (linger));
setsockopt (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 (port);
if (bind (s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
close (s);
return -1;
if (bind (fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
close (fd);
return NULL;
}
#if __UNIX_
signal (SIGPIPE, SIG_IGN);
#endif
if (listen (s, 1) < 0) {
close (s);
return -1;
if (listen (fd, 1) < 0) {
close (fd);
return NULL;
}
s = R_NEW (RSocket);
s->fd = fd;
s->is_ssl = R_FALSE;
return s;
}
R_API int r_socket_close(int fd) {
#if __WINDOWS__
WSACleanup ();
return closesocket (fd);
#else
shutdown (fd, SHUT_RDWR);
return close (fd);
R_API void r_socket_block(RSocket *s, int block) {
#if __UNIX__
fcntl (s->fd, F_SETFL, O_NONBLOCK, !block);
#elif __WINDOWS__
ioctlsocket (s->fd, FIONBIO, (u_long FAR*)&block);
#endif
}
R_API int r_socket_read_block(int fd, unsigned char *buf, int len) {
int r, ret = 0;
for (ret=0;ret<len;) {
r = r_socket_read (fd, buf+ret, len-ret);
if (r==-1)
break;
ret += r;
}
return ret;
//TODO: Support SSL
R_API RSocket *r_socket_accept(RSocket *s) {
RSocket *sock = R_NEW (RSocket);
sock->is_ssl = R_FALSE;
sock->fd = accept (s->fd, NULL, NULL);
return sock;
}
R_API int r_socket_read(int fd, unsigned char *buf, int len) {
#if __WINDOWS__
return recv (fd, (void *)buf, len, 0);
#else
return read (fd, buf, len);
#endif
}
R_API int r_socket_accept(int fd) {
return accept (fd, NULL, NULL);
}
R_API int r_socket_flush(int fd) {
R_API int r_socket_flush(RSocket *s) {
/* TODO */
return 0;
}
R_API int r_socket_gets(int fd, char *buf, int size) {
int i = 0;
int ret = 0;
R_API int r_socket_close(RSocket *s) {
int ret;
if (fd == -1)
return -1;
while (i<size) {
ret = r_socket_read (fd, (ut8 *)buf+i, 1);
if (ret==0)
break;
if (ret<0) {
r_socket_close (fd);
return i;
}
if (buf[i]=='\r'||buf[i]=='\n') {
buf[i]='\0';
break;
}
i += ret;
#if __WINDOWS__
WSACleanup ();
ret = closesocket (s->fd);
#else
shutdown (s->fd, SHUT_RDWR);
ret = close (s->fd);
#endif
#ifdef HAVE_LIB_SSL
if (s->is_ssl) {
SSL_shutdown (s->sfd);
SSL_free (s->sfd);
SSL_CTX_free (s->ctx);
}
buf[i]='\0';
return i;
#endif
return ret;
}
R_API char *r_socket_to_string(int fd) {
// XXX: rewrite it to use select //
/* waits secs until new data is received. */
/* returns -1 on error, 0 is false, 1 is true */
R_API int r_socket_ready(RSocket *s, int secs, int usecs) {
#if __UNIX__
int ret;
struct pollfd fds[1];
fds[0].fd = s->fd;
fds[0].events = POLLIN|POLLPRI;
fds[0].revents = POLLNVAL|POLLHUP|POLLERR;
ret = poll((struct pollfd *)&fds, 1, usecs);
return ret;
#elif __WINDOWS__
fd_set rfds;
struct timeval tv;
int retval;
if (s->fd==-1)
return -1;
FD_ZERO(&rfds);
FD_SET(s->fd, &rfds);
tv.tv_sec = secs;
tv.tv_usec = usecs;
if (select (1, &rfds, NULL, NULL, &tv) == -1)
return -1;
return FD_ISSET (0, &rfds);
#else
return R_TRUE; /* always ready if unknown */
#endif
}
R_API char *r_socket_to_string(RSocket *s) {
#if __WINDOWS__
char *str = malloc (32);
snprintf (str, sizeof (str), "fd%d", fd);
snprintf (str, sizeof (str), "fd%d", s->fd);
return str;
#elif __UNIX__
char *str = NULL;
struct sockaddr sa;
socklen_t sl = sizeof (sa);
memset (&sa, 0, sizeof (sa));
if (!getpeername (fd, &sa, &sl)) {
if (!getpeername (s->fd, &sa, &sl)) {
struct sockaddr_in *sain = (struct sockaddr_in*) &sa;
ut8 *a = (ut8*) &(sain->sin_addr);
if ((str = malloc (32)))
@ -296,11 +268,14 @@ R_API char *r_socket_to_string(int fd) {
#endif
}
R_API int r_socket_udp_connect(const char *host, int port) {
//XXX: Merge with r_new
R_API RSocket *r_socket_udp_connect(const char *host, int port, int is_ssl) {
struct sockaddr_in sa;
struct hostent *he;
int s;
RSocket *sock = R_NEW (RSocket);
sock->is_ssl = is_ssl;
#if __WINDOWS__
WSADATA wsadata;
if (WSAStartup (MAKEWORD (1,1), &wsadata) == SOCKET_ERROR) {
@ -312,14 +287,14 @@ R_API int r_socket_udp_connect(const char *host, int port) {
#endif
s = socket (AF_INET, SOCK_DGRAM, 0);
if (s == -1)
return -1;
return NULL;
memset (&sa, 0, sizeof (sa));
sa.sin_family = AF_INET;
he = (struct hostent *)gethostbyname (host);
if (he == (struct hostent*)0) {
close (s);
return -1;
return NULL;
}
sa.sin_addr = *((struct in_addr *)he->h_addr);
@ -327,8 +302,113 @@ R_API int r_socket_udp_connect(const char *host, int port) {
if (connect (s, (const struct sockaddr*)&sa, sizeof (struct sockaddr))) {
close (s);
return -1;
return NULL;
}
return s;
sock->fd = s;
if (is_ssl) {
sock->sfd = NULL;
sock->ctx = NULL;
#ifdef HAVE_LIB_SSL
if (!SSL_library_init ())
return NULL;
SSL_load_error_strings ();
sock->ctx = SSL_CTX_new (SSLv23_client_method ());
if (sock->ctx == NULL)
return NULL;
sock->sfd = SSL_new (sock->ctx);
SSL_set_fd (sock->sfd, sock->fd);
if (SSL_connect (sock->sfd) != 1)
return NULL;
#endif
}
return sock;
}
/* Read/Write functions */
R_API int r_socket_write(RSocket *s, void *buf, int len) {
int ret, delta = 0;
for (;;) {
#ifdef HAVE_LIB_SSL
if (s->is_ssl)
ret = SSL_write (s->sfd, buf+delta, len);
else
#endif
ret = send (s->fd, buf+delta, len, 0);
if (ret == 0)
return -1;
if (ret == len)
return len;
if (ret<0)
break;
delta += ret;
len -= ret;
}
if (ret == -1)
return -1;
return delta;
}
R_API int r_socket_puts(RSocket *s, char *buf) {
return r_socket_write (s, buf, strlen (buf));
}
R_API void r_socket_printf(RSocket *s, const char *fmt, ...) {
char buf[BUFFER_SIZE];
va_list ap;
if (s->fd >= 0) {
va_start (ap, fmt);
vsnprintf (buf, BUFFER_SIZE, fmt, ap);
r_socket_write (s, buf, strlen(buf));
va_end (ap);
}
}
R_API int r_socket_read(RSocket *s, unsigned char *buf, int len) {
#ifdef HAVE_LIB_SSL
if (s->is_ssl)
return SSL_read (s->sfd, buf, len);
else
#endif
#if __WINDOWS__
return recv (s->fd, (void *)buf, len, 0);
#else
return read (s->fd, buf, len);
#endif
}
R_API int r_socket_read_block(RSocket *s, unsigned char *buf, int len) {
int r, ret = 0;
for (ret=0;ret<len;) {
r = r_socket_read (s, buf+ret, len-ret);
if (r==-1)
break;
ret += r;
}
return ret;
}
R_API int r_socket_gets(RSocket *s, char *buf, int size) {
int i = 0;
int ret = 0;
if (s->fd == -1)
return -1;
while (i<size) {
ret = r_socket_read (s, (ut8 *)buf+i, 1);
if (ret==0)
break;
if (ret<0) {
r_socket_close (s);
return i;
}
if (buf[i]=='\r'||buf[i]=='\n') {
buf[i]='\0';
break;
}
i += ret;
}
buf[i]='\0';
return i;
}

View File

@ -1,7 +1,7 @@
include ../../config.mk
OBJ=test.o
BIN=test
OBJ=testssl.o
BIN=testssl
BINDEPS=r_socket
include ../../rules.mk

View File

@ -1,23 +1,24 @@
#include <r_socket.h>
int main()
{
int ret;
struct r_socket_proc_t *sp;
char buf[256];
char *const args[3] = { "/bin/ls", "-l", 0 };
int main() {
int ret;
struct r_socket_proc_t *sp;
char buf[256];
char *const args[3] = { "/bin/ls", "-l", 0 };
sp = r_socket_proc_open(args);
// ret = r_socket_proc_read(sp, buf, 128);
sp = r_socket_proc_open(args);
for(;;){
if (!r_socket_proc_ready(sp, 0,0))
for(;;){
if (r_socket_proc_ready(sp, 0,0) < 0)
break;
ret = r_socket_proc_fgets(sp, buf, 128);
if (ret>0)
printf("%d=\"%s\"\n", ret, buf);
else break;
}
r_socket_proc_close(sp);
return 0;
ret = r_socket_proc_gets(sp, buf, 128);
if (ret>0)
printf("%d=\"%s\"\n", ret, buf);
else {
printf("%d=\n", ret);
break;
}
}
r_socket_proc_close(sp);
return 0;
}

20
libr/socket/t/testssl.c Normal file
View File

@ -0,0 +1,20 @@
#include <r_socket.h>
#define MAX_LINE 2048
#define SERVER "www.openssl.org"
#define PORT 443
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);
if (s == NULL) {
fprintf (stderr, "Error, cannot connect to "SERVER"\n");
return 1;
}
printf ("%i\n",r_socket_puts (s, "GET /\r\n\r\n"));
while(r_socket_read (s, buf, MAX_LINE)>0)
printf ("%s", buf);
r_socket_free (s);
return 0;
}