2013-02-25 00:47:55 +00:00
|
|
|
/* radare - LGPL - Copyright 2006-2013 - pancake */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-07-24 13:38:53 +00:00
|
|
|
#include <errno.h>
|
2009-12-22 12:27:43 +00:00
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_socket.h>
|
2009-02-05 21:08:46 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-11-12 16:06:28 +00:00
|
|
|
#if EMSCRIPTEN
|
|
|
|
/* no network */
|
|
|
|
R_API RSocket *r_socket_new (int is_ssl) { return NULL; }
|
|
|
|
#else
|
2009-02-05 21:08:46 +00:00
|
|
|
#if __UNIX__
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/socket.h>
|
2011-09-25 04:57:13 +00:00
|
|
|
//#include <sys/fcntl.h>
|
2009-02-05 21:08:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 4096
|
|
|
|
|
2011-05-05 15:32:56 +00:00
|
|
|
#if __UNIX__
|
|
|
|
static int r_socket_unix_connect(RSocket *s, const char *file) {
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int sock = socket (PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
free (s);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
// TODO: set socket options
|
|
|
|
addr.sun_family = AF_UNIX;
|
2011-09-18 18:41:36 +00:00
|
|
|
strncpy (addr.sun_path, file, sizeof (addr.sun_path)-1);
|
2011-05-05 15:32:56 +00:00
|
|
|
|
|
|
|
if (connect (sock, (struct sockaddr *)&addr, sizeof(addr))==-1) {
|
|
|
|
close (sock);
|
|
|
|
free (s);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
s->fd = sock;
|
|
|
|
s->is_ssl = R_FALSE;
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_socket_unix_listen (RSocket *s, const char *file) {
|
|
|
|
struct sockaddr_un unix_name;
|
|
|
|
int sock = socket (PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (sock <0)
|
|
|
|
return R_FALSE;
|
|
|
|
// TODO: set socket options
|
|
|
|
unix_name.sun_family = AF_UNIX;
|
2011-09-18 18:41:36 +00:00
|
|
|
strncpy (unix_name.sun_path, file, sizeof (unix_name.sun_path)-1);
|
2011-05-05 15:32:56 +00:00
|
|
|
|
|
|
|
/* just to make sure there is no other socket file */
|
|
|
|
unlink (unix_name.sun_path);
|
|
|
|
|
|
|
|
if (bind (sock, (struct sockaddr *) &unix_name, sizeof (unix_name)) < 0) {
|
|
|
|
close (sock);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
signal (SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
/* change permissions */
|
|
|
|
if (chmod (unix_name.sun_path, 0777) != 0) {
|
|
|
|
close (sock);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
if (listen (sock, 1)) {
|
|
|
|
close (sock);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
s->fd = sock;
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-05 01:41:57 +00:00
|
|
|
R_API RSocket *r_socket_new (int is_ssl) {
|
2011-03-21 00:47:17 +00:00
|
|
|
RSocket *s = R_NEW (RSocket);
|
|
|
|
s->is_ssl = is_ssl;
|
2012-09-06 01:12:54 +00:00
|
|
|
#if __UNIX_
|
|
|
|
signal (SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
s->local = 0;
|
2011-04-15 07:22:34 +00:00
|
|
|
#if HAVE_LIB_SSL
|
2011-03-21 00:47:17 +00:00
|
|
|
if (is_ssl) {
|
|
|
|
s->sfd = NULL;
|
|
|
|
s->ctx = NULL;
|
2011-03-27 15:44:27 +00:00
|
|
|
s->bio = NULL;
|
2012-09-22 18:32:19 +00:00
|
|
|
s->fd = -1;
|
2011-03-27 21:44:24 +00:00
|
|
|
if (!SSL_library_init ()) {
|
|
|
|
r_socket_free (s);
|
2011-03-21 00:47:17 +00:00
|
|
|
return NULL;
|
2011-03-27 21:44:24 +00:00
|
|
|
}
|
2011-03-21 00:47:17 +00:00
|
|
|
SSL_load_error_strings ();
|
2011-05-05 01:41:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2012-10-25 19:40:11 +00:00
|
|
|
R_API int r_socket_connect (RSocket *s, const char *host, const char *port, int proto, int timeout) {
|
2011-05-05 01:41:57 +00:00
|
|
|
#if __WINDOWS__
|
2011-07-06 09:48:16 +00:00
|
|
|
struct sockaddr_in sa;
|
|
|
|
struct hostent *he;
|
2011-05-05 01:41:57 +00:00
|
|
|
WSADATA wsadata;
|
2011-07-06 09:48:16 +00:00
|
|
|
if (WSAStartup (MAKEWORD (1, 1), &wsadata) == SOCKET_ERROR) {
|
2011-05-05 01:41:57 +00:00
|
|
|
eprintf ("Error creating socket.");
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
2011-07-06 09:48:16 +00:00
|
|
|
s->fd = socket (AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (s->fd == -1)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
memset (&sa, 0, sizeof(sa));
|
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
he = (struct hostent *)gethostbyname (host);
|
|
|
|
if (he == (struct hostent*)0) {
|
|
|
|
close (s->fd);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sa.sin_addr = *((struct in_addr *)he->h_addr);
|
|
|
|
sa.sin_port = htons (atoi (port));
|
2012-10-25 19:40:11 +00:00
|
|
|
#warning TODO: implement connect timeout on w32
|
2011-07-06 09:48:16 +00:00
|
|
|
if (connect (s->fd, (const struct sockaddr*)&sa, sizeof (struct sockaddr))) {
|
|
|
|
close (s->fd);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
return R_TRUE;
|
2011-05-05 01:41:57 +00:00
|
|
|
#elif __UNIX__
|
2012-06-07 01:41:21 +00:00
|
|
|
if (proto==0) proto= R_SOCKET_PROTO_TCP;
|
2012-10-25 19:40:11 +00:00
|
|
|
int gai, ret;
|
2011-07-06 09:48:16 +00:00
|
|
|
struct addrinfo hints, *res, *rp;
|
2011-05-05 01:41:57 +00:00
|
|
|
signal (SIGPIPE, SIG_IGN);
|
2011-06-14 02:49:10 +00:00
|
|
|
if (proto == R_SOCKET_PROTO_UNIX) {
|
|
|
|
if (!r_socket_unix_connect (s, host))
|
|
|
|
return R_FALSE;
|
|
|
|
} else {
|
2011-06-03 09:16:08 +00:00
|
|
|
memset (&hints, 0, sizeof (struct addrinfo));
|
2012-10-25 19:40:11 +00:00
|
|
|
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
2011-05-05 15:32:56 +00:00
|
|
|
hints.ai_protocol = proto;
|
|
|
|
gai = getaddrinfo (host, port, &hints, &res);
|
|
|
|
if (gai != 0) {
|
2012-11-05 01:00:34 +00:00
|
|
|
//eprintf ("Error in getaddrinfo: %s\n", gai_strerror (gai));
|
2011-05-05 15:32:56 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
for (rp = res; rp != NULL; rp = rp->ai_next) {
|
|
|
|
s->fd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
if (s->fd == -1)
|
|
|
|
continue;
|
2012-10-25 19:40:11 +00:00
|
|
|
if (timeout>0)
|
2012-12-03 03:01:55 +00:00
|
|
|
r_socket_block_time (s, 1, timeout);
|
|
|
|
//fcntl (s->fd, F_SETFL, O_NONBLOCK, 1);
|
2012-10-25 19:40:11 +00:00
|
|
|
ret = connect (s->fd, rp->ai_addr, rp->ai_addrlen);
|
2013-01-02 02:34:17 +00:00
|
|
|
if (timeout<1) {
|
|
|
|
if (ret == -1) {
|
|
|
|
close (s->fd);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
2012-10-25 19:40:11 +00:00
|
|
|
if (timeout>0) {
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set fdset;
|
|
|
|
FD_ZERO (&fdset);
|
|
|
|
FD_SET (s->fd, &fdset);
|
|
|
|
tv.tv_sec = timeout;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if (select (s->fd + 1, NULL, &fdset, NULL, &tv) == 1) {
|
|
|
|
int so_error;
|
|
|
|
socklen_t len = sizeof so_error;
|
2013-01-02 02:34:17 +00:00
|
|
|
ret = getsockopt (s->fd, SOL_SOCKET,
|
|
|
|
SO_ERROR, &so_error, &len);
|
2012-12-03 03:01:55 +00:00
|
|
|
// fcntl (s->fd, F_SETFL, O_NONBLOCK, 0);
|
2013-01-02 02:34:17 +00:00
|
|
|
// r_socket_block_time (s, 0, 0);
|
|
|
|
freeaddrinfo (res);
|
|
|
|
return R_TRUE;
|
2012-10-25 19:40:11 +00:00
|
|
|
} else {
|
2013-01-02 02:34:17 +00:00
|
|
|
freeaddrinfo (res);
|
2012-10-25 19:40:11 +00:00
|
|
|
close (s->fd);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2011-05-05 15:32:56 +00:00
|
|
|
close (s->fd);
|
2012-10-25 19:40:11 +00:00
|
|
|
s->fd = -1;
|
2011-05-05 15:32:56 +00:00
|
|
|
}
|
2012-10-25 19:40:11 +00:00
|
|
|
freeaddrinfo (res);
|
2011-05-05 15:32:56 +00:00
|
|
|
if (rp == NULL) {
|
2013-01-02 02:34:17 +00:00
|
|
|
eprintf ("Could not resolve address\n");
|
2011-05-05 15:32:56 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
2011-05-05 01:41:57 +00:00
|
|
|
}
|
2011-07-06 09:48:16 +00:00
|
|
|
#endif
|
2011-05-05 01:41:57 +00:00
|
|
|
#if HAVE_LIB_SSL
|
|
|
|
if (s->is_ssl) {
|
2011-03-21 00:47:17 +00:00
|
|
|
s->ctx = SSL_CTX_new (SSLv23_client_method ());
|
2011-03-27 21:44:24 +00:00
|
|
|
if (s->ctx == NULL) {
|
|
|
|
r_socket_free (s);
|
2011-05-05 01:41:57 +00:00
|
|
|
return R_FALSE;
|
2011-03-27 21:44:24 +00:00
|
|
|
}
|
2011-03-21 00:47:17 +00:00
|
|
|
s->sfd = SSL_new (s->ctx);
|
|
|
|
SSL_set_fd (s->sfd, s->fd);
|
2011-03-27 21:44:24 +00:00
|
|
|
if (SSL_connect (s->sfd) != 1) {
|
|
|
|
r_socket_free (s);
|
2011-05-05 01:41:57 +00:00
|
|
|
return R_FALSE;
|
2011-03-27 21:44:24 +00:00
|
|
|
}
|
2011-03-21 00:47:17 +00:00
|
|
|
}
|
2011-03-27 15:44:27 +00:00
|
|
|
#endif
|
2011-05-05 01:41:57 +00:00
|
|
|
return R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2011-05-05 01:41:57 +00:00
|
|
|
R_API int r_socket_close (RSocket *s) {
|
2012-09-26 08:01:43 +00:00
|
|
|
int ret = R_FALSE;
|
2012-10-25 19:40:11 +00:00
|
|
|
if (!s) return R_FALSE;
|
2012-09-22 18:32:19 +00:00
|
|
|
if (s->fd != -1) {
|
2011-05-05 01:41:57 +00:00
|
|
|
#if __WINDOWS__
|
2012-09-22 18:32:19 +00:00
|
|
|
WSACleanup ();
|
|
|
|
ret = closesocket (s->fd);
|
2011-05-05 01:41:57 +00:00
|
|
|
#else
|
2012-12-12 03:10:44 +00:00
|
|
|
shutdown (s->fd, SHUT_RDWR);
|
2012-09-22 18:32:19 +00:00
|
|
|
ret = close (s->fd);
|
2011-05-05 01:41:57 +00:00
|
|
|
#endif
|
2012-09-22 18:32:19 +00:00
|
|
|
}
|
2011-05-05 01:41:57 +00:00
|
|
|
#if HAVE_LIB_SSL
|
2012-12-10 15:25:57 +00:00
|
|
|
if (s->is_ssl && s->sfd) {
|
|
|
|
SSL_free (s->sfd);
|
|
|
|
s->sfd = NULL;
|
|
|
|
}
|
2011-05-05 01:41:57 +00:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_socket_free (RSocket *s) {
|
|
|
|
int res = r_socket_close (s);
|
|
|
|
#if HAVE_LIB_SSL
|
|
|
|
if (s->is_ssl) {
|
|
|
|
if (s->sfd)
|
|
|
|
SSL_free (s->sfd);
|
|
|
|
if (s->ctx)
|
|
|
|
SSL_CTX_free (s->ctx);
|
|
|
|
}
|
|
|
|
#endif
|
2011-03-21 00:47:17 +00:00
|
|
|
free (s);
|
2011-05-05 01:41:57 +00:00
|
|
|
return res;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2011-05-05 01:41:57 +00:00
|
|
|
R_API int r_socket_listen (RSocket *s, const char *port, const char *certfile) {
|
2012-07-13 00:03:36 +00:00
|
|
|
int optval = 1;
|
2009-02-05 21:08:46 +00:00
|
|
|
struct linger linger = { 0 };
|
|
|
|
|
2011-05-05 01:41:57 +00:00
|
|
|
if ((s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
|
|
|
|
return R_FALSE;
|
2010-11-30 12:54:07 +00:00
|
|
|
linger.l_onoff = 1;
|
|
|
|
linger.l_linger = 1;
|
2011-05-05 01:41:57 +00:00
|
|
|
setsockopt (s->fd, SOL_SOCKET, SO_LINGER, (const char *)&linger, sizeof (linger));
|
2012-12-12 03:10:44 +00:00
|
|
|
{ // fix close after write bug //
|
|
|
|
int x = 1500;
|
|
|
|
setsockopt (s->fd, SOL_SOCKET, SO_SNDBUF, (const char *)&x, sizeof (int));
|
|
|
|
}
|
|
|
|
setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
|
2013-02-25 00:47:55 +00:00
|
|
|
memset (&s->sa, 0, sizeof (s->sa));
|
|
|
|
s->sa.sin_family = AF_INET;
|
|
|
|
s->sa.sin_addr.s_addr = htonl (s->local? INADDR_LOOPBACK: INADDR_ANY);
|
|
|
|
s->sa.sin_port = htons (atoi (port)); // WTF we should honor etc/services
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2013-02-25 00:47:55 +00:00
|
|
|
if (bind (s->fd, (struct sockaddr *)&s->sa, sizeof(s->sa)) < 0) {
|
2011-05-05 01:41:57 +00:00
|
|
|
close (s->fd);
|
|
|
|
return R_FALSE;
|
2010-11-30 12:54:07 +00:00
|
|
|
}
|
2011-03-10 10:36:16 +00:00
|
|
|
#if __UNIX_
|
2011-02-03 08:31:50 +00:00
|
|
|
signal (SIGPIPE, SIG_IGN);
|
2011-03-10 10:36:16 +00:00
|
|
|
#endif
|
2012-11-29 03:17:23 +00:00
|
|
|
if (listen (s->fd, 32) < 0) {
|
2011-05-05 01:41:57 +00:00
|
|
|
close (s->fd);
|
|
|
|
return R_FALSE;
|
2010-11-30 12:54:07 +00:00
|
|
|
}
|
2011-04-15 07:22:34 +00:00
|
|
|
#if HAVE_LIB_SSL
|
2011-05-05 01:41:57 +00:00
|
|
|
if (s->is_ssl) {
|
2011-03-27 15:44:27 +00:00
|
|
|
s->ctx = SSL_CTX_new (SSLv23_method ());
|
2011-03-27 21:44:24 +00:00
|
|
|
if (s->ctx == NULL) {
|
|
|
|
r_socket_free (s);
|
2011-05-05 01:41:57 +00:00
|
|
|
return R_FALSE;
|
2011-03-27 21:44:24 +00:00
|
|
|
}
|
|
|
|
if (!SSL_CTX_use_certificate_chain_file (s->ctx, certfile)) {
|
|
|
|
r_socket_free (s);
|
2011-05-05 01:41:57 +00:00
|
|
|
return R_FALSE;
|
2011-03-27 21:44:24 +00:00
|
|
|
}
|
|
|
|
if (!SSL_CTX_use_PrivateKey_file (s->ctx, certfile, SSL_FILETYPE_PEM)) {
|
|
|
|
r_socket_free (s);
|
2011-05-05 01:41:57 +00:00
|
|
|
return R_FALSE;
|
2011-03-27 21:44:24 +00:00
|
|
|
}
|
2011-03-27 15:44:27 +00:00
|
|
|
SSL_CTX_set_verify_depth (s->ctx, 1);
|
|
|
|
}
|
|
|
|
#endif
|
2011-05-05 01:41:57 +00:00
|
|
|
return R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2011-03-27 15:44:27 +00:00
|
|
|
R_API RSocket *r_socket_accept(RSocket *s) {
|
2012-10-25 19:40:11 +00:00
|
|
|
RSocket *sock;
|
2013-02-25 00:47:55 +00:00
|
|
|
socklen_t salen = sizeof (s->sa);
|
2012-10-25 19:40:11 +00:00
|
|
|
if (!s) return NULL;
|
|
|
|
sock = R_NEW (RSocket);
|
|
|
|
if (!sock) return NULL;
|
2012-12-30 22:40:17 +00:00
|
|
|
//signal (SIGPIPE, SIG_DFL);
|
2013-02-25 00:47:55 +00:00
|
|
|
sock->fd = accept (s->fd, (struct sockaddr *)&s->sa, &salen);
|
2011-03-27 21:44:24 +00:00
|
|
|
if (sock->fd == -1) {
|
|
|
|
free (sock);
|
2011-03-27 15:44:27 +00:00
|
|
|
return NULL;
|
2011-03-27 21:44:24 +00:00
|
|
|
}
|
2011-04-15 07:22:34 +00:00
|
|
|
#if HAVE_LIB_SSL
|
2012-12-12 03:10:44 +00:00
|
|
|
sock->is_ssl = s->is_ssl;
|
2011-03-27 15:44:27 +00:00
|
|
|
if (sock->is_ssl) {
|
|
|
|
sock->sfd = NULL;
|
|
|
|
sock->ctx = NULL;
|
|
|
|
sock->bio = NULL;
|
|
|
|
BIO *sbio = BIO_new_socket (sock->fd, BIO_NOCLOSE);
|
|
|
|
sock->sfd = SSL_new (s->ctx);
|
|
|
|
SSL_set_bio (sock->sfd, sbio, sbio);
|
2011-03-27 21:44:24 +00:00
|
|
|
if (SSL_accept (sock->sfd) <= 0) {
|
|
|
|
r_socket_free (sock);
|
2011-03-27 15:44:27 +00:00
|
|
|
return NULL;
|
2011-03-27 21:44:24 +00:00
|
|
|
}
|
2011-03-27 15:44:27 +00:00
|
|
|
sock->bio = BIO_new (BIO_f_buffer ());
|
|
|
|
sbio = BIO_new (BIO_f_ssl ());
|
|
|
|
BIO_set_ssl (sbio, sock->sfd, BIO_CLOSE);
|
|
|
|
BIO_push (sock->bio, sbio);
|
|
|
|
}
|
2012-12-12 03:10:44 +00:00
|
|
|
#else
|
|
|
|
sock->is_ssl = 0;
|
2011-03-27 15:44:27 +00:00
|
|
|
#endif
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2011-05-05 01:41:57 +00:00
|
|
|
R_API int r_socket_block_time (RSocket *s, int block, int sec) {
|
2012-12-03 03:01:55 +00:00
|
|
|
if (!s) return R_FALSE;
|
2011-03-21 00:47:17 +00:00
|
|
|
#if __UNIX__
|
2012-12-03 03:01:55 +00:00
|
|
|
{
|
|
|
|
int flags = fcntl (s->fd, F_GETFL, 0);
|
2013-01-02 02:34:17 +00:00
|
|
|
fcntl (s->fd, F_SETFL, block?
|
|
|
|
(flags & ~O_NONBLOCK):
|
|
|
|
(flags | O_NONBLOCK));
|
2012-12-03 03:01:55 +00:00
|
|
|
}
|
2011-03-21 00:47:17 +00:00
|
|
|
#elif __WINDOWS__
|
|
|
|
ioctlsocket (s->fd, FIONBIO, (u_long FAR*)&block);
|
2009-02-05 21:08:46 +00:00
|
|
|
#endif
|
2011-05-05 01:41:57 +00:00
|
|
|
if (sec > 0) {
|
2012-12-03 03:01:55 +00:00
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = sec;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if (setsockopt (s->fd, SOL_SOCKET, SO_RCVTIMEO,
|
|
|
|
(char *)&tv, sizeof (tv)) < 0)
|
2011-05-05 01:41:57 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
return R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2011-03-21 00:47:17 +00:00
|
|
|
R_API int r_socket_flush(RSocket *s) {
|
2011-04-15 07:22:34 +00:00
|
|
|
#if HAVE_LIB_SSL
|
2011-03-27 15:44:27 +00:00
|
|
|
if (s->is_ssl && s->bio)
|
|
|
|
return BIO_flush(s->bio);
|
2011-04-15 07:22:34 +00:00
|
|
|
#endif
|
2011-03-27 15:44:27 +00:00
|
|
|
return R_TRUE;
|
2009-05-01 01:25:28 +00:00
|
|
|
}
|
|
|
|
|
2011-03-21 00:47:17 +00:00
|
|
|
// 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__
|
|
|
|
struct pollfd fds[1];
|
|
|
|
fds[0].fd = s->fd;
|
|
|
|
fds[0].events = POLLIN|POLLPRI;
|
|
|
|
fds[0].revents = POLLNVAL|POLLHUP|POLLERR;
|
2012-02-01 01:22:43 +00:00
|
|
|
return poll((struct pollfd *)&fds, 1, usecs);
|
2011-03-21 00:47:17 +00:00
|
|
|
#elif __WINDOWS__
|
|
|
|
fd_set rfds;
|
|
|
|
struct timeval tv;
|
|
|
|
if (s->fd==-1)
|
|
|
|
return -1;
|
2012-02-01 01:22:43 +00:00
|
|
|
FD_ZERO (&rfds);
|
|
|
|
FD_SET (s->fd, &rfds);
|
2011-03-21 00:47:17 +00:00
|
|
|
tv.tv_sec = secs;
|
|
|
|
tv.tv_usec = usecs;
|
2012-12-10 15:25:57 +00:00
|
|
|
if (select (s->fd+1, &rfds, NULL, NULL, &tv) == -1)
|
2011-03-21 00:47:17 +00:00
|
|
|
return -1;
|
|
|
|
return FD_ISSET (0, &rfds);
|
|
|
|
#else
|
|
|
|
return R_TRUE; /* always ready if unknown */
|
|
|
|
#endif
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2009-07-24 13:38:53 +00:00
|
|
|
|
2011-03-21 00:47:17 +00:00
|
|
|
R_API char *r_socket_to_string(RSocket *s) {
|
2010-08-08 17:03:51 +00:00
|
|
|
#if __WINDOWS__
|
|
|
|
char *str = malloc (32);
|
2011-03-21 00:47:17 +00:00
|
|
|
snprintf (str, sizeof (str), "fd%d", s->fd);
|
2010-08-08 17:03:51 +00:00
|
|
|
return str;
|
2010-11-30 12:54:07 +00:00
|
|
|
#elif __UNIX__
|
2009-07-24 13:38:53 +00:00
|
|
|
char *str = NULL;
|
|
|
|
struct sockaddr sa;
|
2010-04-14 11:28:56 +00:00
|
|
|
socklen_t sl = sizeof (sa);
|
|
|
|
memset (&sa, 0, sizeof (sa));
|
2011-03-21 00:47:17 +00:00
|
|
|
if (!getpeername (s->fd, &sa, &sl)) {
|
2009-07-24 13:38:53 +00:00
|
|
|
struct sockaddr_in *sain = (struct sockaddr_in*) &sa;
|
|
|
|
ut8 *a = (ut8*) &(sain->sin_addr);
|
2010-11-30 12:54:07 +00:00
|
|
|
if ((str = malloc (32)))
|
2010-04-14 11:28:56 +00:00
|
|
|
sprintf (str, "%d.%d.%d.%d:%d",
|
|
|
|
a[0],a[1],a[2],a[3], ntohs (sain->sin_port));
|
|
|
|
} else eprintf ("getperrname: failed\n"); //r_sys_perror ("getpeername");
|
2009-07-24 13:38:53 +00:00
|
|
|
return str;
|
2010-11-30 12:54:07 +00:00
|
|
|
#else
|
|
|
|
return NULL;
|
2010-08-08 17:03:51 +00:00
|
|
|
#endif
|
2009-07-24 13:38:53 +00:00
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
|
2011-03-21 00:47:17 +00:00
|
|
|
/* Read/Write functions */
|
|
|
|
R_API int r_socket_write(RSocket *s, void *buf, int len) {
|
|
|
|
int ret, delta = 0;
|
2012-12-12 03:10:44 +00:00
|
|
|
#if __UNIX__
|
|
|
|
signal (SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
2011-03-21 00:47:17 +00:00
|
|
|
for (;;) {
|
2013-02-25 00:47:55 +00:00
|
|
|
int b = 1500; //65536; // Use MTU 1500?
|
2012-12-10 15:25:57 +00:00
|
|
|
if (b>len) b = len;
|
2011-04-15 07:22:34 +00:00
|
|
|
#if HAVE_LIB_SSL
|
2011-03-21 00:47:17 +00:00
|
|
|
if (s->is_ssl)
|
2011-03-27 15:44:27 +00:00
|
|
|
if (s->bio)
|
2012-12-10 15:25:57 +00:00
|
|
|
ret = BIO_write (s->bio, buf+delta, b);
|
2011-03-27 15:44:27 +00:00
|
|
|
else
|
2012-12-10 15:25:57 +00:00
|
|
|
ret = SSL_write (s->sfd, buf+delta, b);
|
2011-03-21 00:47:17 +00:00
|
|
|
else
|
|
|
|
#endif
|
2013-02-25 00:47:55 +00:00
|
|
|
ret = send (s->fd, buf+delta, b, 0);
|
2012-12-10 15:25:57 +00:00
|
|
|
//if (ret == 0) return -1;
|
2013-02-25 00:47:55 +00:00
|
|
|
if (ret<1) break;
|
2011-03-21 00:47:17 +00:00
|
|
|
if (ret == len)
|
|
|
|
return len;
|
2013-02-25 00:47:55 +00:00
|
|
|
usleep (100); // take breath, wtf
|
2011-03-21 00:47:17 +00:00
|
|
|
delta += ret;
|
|
|
|
len -= ret;
|
|
|
|
}
|
|
|
|
if (ret == -1)
|
2010-08-24 09:58:09 +00:00
|
|
|
return -1;
|
2011-03-21 00:47:17 +00:00
|
|
|
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);
|
2010-11-12 12:26:58 +00:00
|
|
|
}
|
2011-03-21 00:47:17 +00:00
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
|
2011-03-21 00:47:17 +00:00
|
|
|
R_API int r_socket_read(RSocket *s, unsigned char *buf, int len) {
|
2012-11-05 01:00:34 +00:00
|
|
|
if (!s) return -1;
|
2011-04-15 07:22:34 +00:00
|
|
|
#if HAVE_LIB_SSL
|
2011-03-21 00:47:17 +00:00
|
|
|
if (s->is_ssl)
|
2011-03-27 15:44:27 +00:00
|
|
|
if (s->bio)
|
|
|
|
return BIO_read (s->bio, buf, len);
|
|
|
|
else
|
|
|
|
return SSL_read (s->sfd, buf, len);
|
2011-03-21 00:47:17 +00:00
|
|
|
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);
|
2012-12-30 22:40:17 +00:00
|
|
|
if (r<1) //==-1)
|
2011-03-21 00:47:17 +00:00
|
|
|
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);
|
2013-03-03 16:28:06 +00:00
|
|
|
if (ret==0) {
|
|
|
|
if (i>0) return i;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-21 00:47:17 +00:00
|
|
|
if (ret<0) {
|
|
|
|
r_socket_close (s);
|
2013-03-03 16:28:06 +00:00
|
|
|
return i==0?-1: i;
|
2011-03-21 00:47:17 +00:00
|
|
|
}
|
2013-03-03 16:28:06 +00:00
|
|
|
if (buf[i]=='\r' || buf[i]=='\n') {
|
2011-03-21 00:47:17 +00:00
|
|
|
buf[i]='\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i += ret;
|
|
|
|
}
|
|
|
|
buf[i]='\0';
|
|
|
|
return i;
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
2011-04-06 07:29:25 +00:00
|
|
|
|
|
|
|
R_API RSocket *r_socket_new_from_fd (int fd) {
|
2012-08-13 15:42:25 +00:00
|
|
|
RSocket *s = R_NEW0 (RSocket);
|
2011-04-06 07:29:25 +00:00
|
|
|
s->fd = fd;
|
|
|
|
return s;
|
|
|
|
}
|
2012-11-12 16:06:28 +00:00
|
|
|
#endif
|