2009-02-07 12:28:29 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <network.h>
|
|
|
|
|
|
|
|
static int set_sock_opts(int sock)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int d = 1;
|
|
|
|
|
|
|
|
memset(&tv, 0, sizeof(tv));
|
|
|
|
tv.tv_sec = 2;
|
|
|
|
net_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
|
|
|
|
&tv, sizeof(struct timeval));
|
|
|
|
net_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
|
|
|
|
&tv, sizeof(struct timeval));
|
2009-02-21 08:59:45 +01:00
|
|
|
return net_setsockopt(sock,SOL_SOCKET,SO_REUSEADDR, &d, sizeof(int));
|
2009-02-07 12:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* From glibc docs */
|
|
|
|
static int make_socket (uint16_t port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
/* Create the socket. */
|
|
|
|
sock = net_socket (PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock < 0)
|
|
|
|
{
|
|
|
|
perror ("socket");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_sock_opts(sock);
|
|
|
|
|
|
|
|
/* Give the socket a name. */
|
|
|
|
name.sin_family = AF_INET;
|
|
|
|
name.sin_port = htons (port);
|
|
|
|
name.sin_addr.s_addr = htonl (INADDR_ANY);
|
|
|
|
if (net_bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
|
|
|
|
{
|
|
|
|
perror ("bind");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
bool Network::InitSockaddr (struct sockaddr_in *name,
|
2009-02-07 12:28:29 +01:00
|
|
|
const char *hostname, uint16_t port)
|
|
|
|
{
|
|
|
|
struct hostent *hostinfo;
|
|
|
|
|
|
|
|
name->sin_family = AF_INET;
|
|
|
|
name->sin_port = htons (port);
|
|
|
|
hostinfo = net_gethostbyname ((char*)hostname);
|
|
|
|
if (hostinfo == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Unknown host %s.\n", hostname);
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-21 08:59:45 +01:00
|
|
|
#warning this need to be fixed
|
2009-02-07 12:28:29 +01:00
|
|
|
//name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
bool Network::InitSocket(const char *remote_host, int port)
|
2009-02-07 12:28:29 +01:00
|
|
|
{
|
2009-02-21 08:59:45 +01:00
|
|
|
/* Create the socket. */
|
|
|
|
this->sock = net_socket (PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (this->sock < 0)
|
2009-02-07 12:28:29 +01:00
|
|
|
{
|
2009-02-21 08:59:45 +01:00
|
|
|
perror ("socket (client)");
|
2009-02-07 12:28:29 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
set_sock_opts(this->sock);
|
2009-02-07 12:28:29 +01:00
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
/* Connect to the server. */
|
|
|
|
this->InitSockaddr(&this->connection_addr, remote_host, port);
|
2009-02-07 12:28:29 +01:00
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
if (this->is_master)
|
2009-02-07 12:28:29 +01:00
|
|
|
{
|
2009-02-21 08:59:45 +01:00
|
|
|
if (net_bind(this->sock, (struct sockaddr *)&this->connection_addr,
|
|
|
|
sizeof (this->connection_addr)) < 0)
|
|
|
|
{
|
|
|
|
perror ("bind");
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-07 12:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
bool Network::ReceiveData(void *dst, int sock, size_t sz)
|
2009-02-07 12:28:29 +01:00
|
|
|
{
|
2009-02-21 08:59:45 +01:00
|
|
|
size_t received_sz = 0;
|
2009-02-07 12:28:29 +01:00
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
while (received_sz < sz)
|
2009-02-07 12:28:29 +01:00
|
|
|
{
|
2009-02-21 08:59:45 +01:00
|
|
|
int v = net_read(sock, dst, sz);
|
2009-02-07 12:28:29 +01:00
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
if (v < 0)
|
|
|
|
return false;
|
|
|
|
received_sz += v;
|
2009-02-07 12:28:29 +01:00
|
|
|
}
|
2009-02-21 08:59:45 +01:00
|
|
|
this->traffic += received_sz;
|
2009-02-07 12:28:29 +01:00
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
return sz > 0;
|
2009-02-07 12:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Network::ReceiveFrom(void *dst, int sock, size_t sz,
|
|
|
|
struct sockaddr_in *from)
|
|
|
|
{
|
|
|
|
socklen_t from_sz = from ? sizeof(struct sockaddr_in) : 0;
|
|
|
|
|
|
|
|
return net_recvfrom(sock, dst, sz, 0, (struct sockaddr*)from, &from_sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Network::SendTo(void *src, int sock, size_t sz, struct sockaddr_in *to)
|
|
|
|
{
|
|
|
|
socklen_t to_sz = sizeof(struct sockaddr_in);
|
|
|
|
|
|
|
|
assert(to);
|
|
|
|
return net_sendto(sock, src, sz, 0, (struct sockaddr*)to, to_sz);
|
|
|
|
}
|
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
bool Network::SendData(void *src, int sock, size_t sz)
|
|
|
|
{
|
|
|
|
size_t sent_sz = 0;
|
|
|
|
|
|
|
|
while (sent_sz < sz)
|
|
|
|
{
|
|
|
|
int v = net_write(sock, (void*)src, sz);
|
|
|
|
|
|
|
|
if (v < 0)
|
|
|
|
return false;
|
|
|
|
sent_sz += v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-07 12:28:29 +01:00
|
|
|
bool Network::Select(int sock, struct timeval *tv)
|
|
|
|
{
|
|
|
|
fd_set fds;
|
|
|
|
int v;
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
|
|
|
|
|
|
|
v = net_select(sock + 1, &fds, NULL, NULL, tv);
|
|
|
|
if (v < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Select failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* v is 0 if the sock is not ready */
|
|
|
|
return v > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::CloseSocket()
|
|
|
|
{
|
|
|
|
net_close(this->sock);
|
|
|
|
}
|