2009-01-19 07:44:19 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2009-01-24 16:48:43 +01:00
|
|
|
#include <netdb.h>
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-29 19:04:31 +01:00
|
|
|
static int set_sock_opts(int sock)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int d = 1;
|
|
|
|
|
|
|
|
memset(&tv, 0, sizeof(tv));
|
|
|
|
tv.tv_sec = 2;
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
|
|
|
|
&tv, sizeof(struct timeval));
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
|
|
|
|
&tv, sizeof(struct timeval));
|
|
|
|
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR, &d, sizeof(int));
|
|
|
|
}
|
|
|
|
|
2009-01-19 07:44:19 +01:00
|
|
|
/* From glibc docs */
|
|
|
|
static int make_socket (uint16_t port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
2009-01-24 21:57:23 +01:00
|
|
|
int sock;
|
2009-01-19 07:44:19 +01:00
|
|
|
|
|
|
|
/* Create the socket. */
|
2009-02-07 19:24:50 +01:00
|
|
|
sock = socket (PF_INET, SOCK_DGRAM, 0);
|
2009-01-19 07:44:19 +01:00
|
|
|
if (sock < 0)
|
|
|
|
{
|
|
|
|
perror ("socket");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2009-01-29 19:04:31 +01:00
|
|
|
set_sock_opts(sock);
|
2009-01-24 21:57:23 +01:00
|
|
|
|
2009-01-19 07:44:19 +01:00
|
|
|
/* Give the socket a name. */
|
|
|
|
name.sin_family = AF_INET;
|
|
|
|
name.sin_port = htons (port);
|
|
|
|
name.sin_addr.s_addr = htonl (INADDR_ANY);
|
|
|
|
if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
|
|
|
|
{
|
|
|
|
perror ("bind");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2009-01-24 16:48:43 +01:00
|
|
|
bool init_sockaddr (struct sockaddr_in *name,
|
|
|
|
const char *hostname, uint16_t port)
|
|
|
|
{
|
|
|
|
struct hostent *hostinfo;
|
|
|
|
|
|
|
|
name->sin_family = AF_INET;
|
|
|
|
name->sin_port = htons (port);
|
|
|
|
hostinfo = gethostbyname (hostname);
|
|
|
|
if (hostinfo == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Unknown host %s.\n", hostname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-02-08 12:27:56 +01:00
|
|
|
bool Network::InitSocket(const char *remote_host, int port)
|
|
|
|
{
|
|
|
|
/* Create the socket. */
|
|
|
|
this->sock = socket (PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (this->sock < 0)
|
|
|
|
{
|
|
|
|
perror ("socket (client)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_sock_opts(this->sock);
|
|
|
|
|
|
|
|
/* Connect to the server. */
|
|
|
|
init_sockaddr(&this->connection_addr, remote_host, port);
|
|
|
|
|
|
|
|
if (this->is_master)
|
|
|
|
{
|
|
|
|
if (bind(this->sock, (struct sockaddr *)&this->connection_addr,
|
|
|
|
sizeof (this->connection_addr)) < 0)
|
|
|
|
{
|
|
|
|
perror ("bind");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-01-24 16:48:43 +01:00
|
|
|
|
2009-02-08 12:27:56 +01:00
|
|
|
#if 0
|
2009-02-08 09:22:15 +01:00
|
|
|
bool Network::StartNetworkServer(int port)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-02-01 20:47:21 +01:00
|
|
|
Network::listen_sock = make_socket(port);
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-02-01 20:47:21 +01:00
|
|
|
if (Network::listen_sock < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
|
|
|
|
2009-02-01 20:47:21 +01:00
|
|
|
bool Network::CheckNewConnection()
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2009-02-07 19:24:50 +01:00
|
|
|
struct sockaddr_in peer_addr;
|
2009-01-24 16:48:43 +01:00
|
|
|
fd_set listen_fds;
|
2009-02-01 20:47:21 +01:00
|
|
|
|
|
|
|
/* Not initialized yet */
|
|
|
|
if (Network::listen_sock <= 0)
|
|
|
|
return false;
|
2009-01-24 16:48:43 +01:00
|
|
|
|
2009-02-07 19:24:50 +01:00
|
|
|
/* Only one peer allowed */
|
|
|
|
if (Network::n_peers > 0)
|
2009-01-24 16:48:43 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
FD_ZERO(&listen_fds);
|
2009-02-01 20:47:21 +01:00
|
|
|
FD_SET(Network::listen_sock, &listen_fds);
|
2009-01-19 07:44:19 +01:00
|
|
|
|
|
|
|
/* If something connects, create a new client */
|
|
|
|
memset(&tv, 0, sizeof(tv));
|
2009-02-01 20:47:21 +01:00
|
|
|
int v = select(Network::listen_sock + 1, &listen_fds, NULL, NULL, &tv);
|
2009-01-24 16:48:43 +01:00
|
|
|
|
|
|
|
if ( v < 0)
|
|
|
|
{
|
|
|
|
perror("select");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else if ( v == 0 )
|
|
|
|
return false;
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-02-07 19:24:50 +01:00
|
|
|
/* The name, please (but drop the packet after this. FIX this...) */
|
|
|
|
char tmp[1];
|
|
|
|
socklen_t from_sz = sizeof(struct sockaddr_in);
|
|
|
|
recvfrom(Network::listen_sock, tmp, 1, 0,
|
|
|
|
(struct sockaddr*)&peer_addr, &from_sz);
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-02-07 19:24:50 +01:00
|
|
|
/* And add the new one (the connection data will be read by
|
|
|
|
* the network object) */
|
|
|
|
Network::AddPeer(new Network(Network::listen_sock, &peer_addr, true));
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-24 16:48:43 +01:00
|
|
|
return true;
|
|
|
|
}
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-02-01 20:47:21 +01:00
|
|
|
bool Network::ConnectTo(const char *hostname, int port)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-01-24 16:48:43 +01:00
|
|
|
/* Again from glibc docs */
|
2009-02-07 19:24:50 +01:00
|
|
|
struct sockaddr_in server_addr;
|
2009-02-01 20:47:21 +01:00
|
|
|
int sock;
|
2009-01-30 16:37:15 +01:00
|
|
|
|
2009-01-24 16:48:43 +01:00
|
|
|
/* Create the socket. */
|
2009-02-07 19:24:50 +01:00
|
|
|
sock = socket (PF_INET, SOCK_DGRAM, 0);
|
2009-02-01 20:47:21 +01:00
|
|
|
if (sock < 0)
|
2009-01-24 16:48:43 +01:00
|
|
|
{
|
|
|
|
perror ("socket (client)");
|
2009-02-01 20:47:21 +01:00
|
|
|
return false;
|
2009-01-24 16:48:43 +01:00
|
|
|
}
|
|
|
|
|
2009-01-29 19:04:31 +01:00
|
|
|
set_sock_opts(sock);
|
|
|
|
|
2009-01-24 16:48:43 +01:00
|
|
|
/* Connect to the server. */
|
2009-02-07 19:24:50 +01:00
|
|
|
init_sockaddr (&server_addr, hostname, port);
|
2009-02-01 20:47:21 +01:00
|
|
|
|
2009-02-07 19:24:50 +01:00
|
|
|
Network::AddPeer( new Network(sock, &server_addr, false) );
|
2009-02-01 20:47:21 +01:00
|
|
|
|
|
|
|
return true;
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
2009-02-08 12:27:56 +01:00
|
|
|
#endif
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
bool Network::ReceiveData(void *dst, int sock, size_t sz)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-01-26 22:00:23 +01:00
|
|
|
size_t received_sz = 0;
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
while (received_sz < sz)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-01-26 22:00:23 +01:00
|
|
|
int v = read(sock, dst, sz);
|
|
|
|
|
|
|
|
if (v < 0)
|
|
|
|
return false;
|
|
|
|
received_sz += v;
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
2009-01-30 18:49:47 +01:00
|
|
|
this->traffic += received_sz;
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
return sz > 0;
|
|
|
|
}
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-02-07 12:08:50 +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 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 sendto(sock, src, sz, 0, (struct sockaddr*)to, to_sz);
|
|
|
|
}
|
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
bool Network::SendData(void *src, int sock, size_t sz)
|
|
|
|
{
|
|
|
|
size_t sent_sz = 0;
|
|
|
|
|
|
|
|
while (sent_sz < sz)
|
|
|
|
{
|
|
|
|
int v = write(sock, (void*)src, sz);
|
|
|
|
|
|
|
|
if (v < 0)
|
|
|
|
return false;
|
|
|
|
sent_sz += v;
|
|
|
|
}
|
2009-01-19 07:44:19 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
bool Network::Select(int sock, struct timeval *tv)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-01-26 22:00:23 +01:00
|
|
|
fd_set fds;
|
|
|
|
int v;
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2009-01-24 21:57:23 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
v = select(sock + 1, &fds, NULL, NULL, tv);
|
|
|
|
if (v < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Select failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-24 16:48:43 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
/* v is 0 if the sock is not ready */
|
|
|
|
return v > 0;
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
2009-01-29 21:35:06 +01:00
|
|
|
|
2009-02-01 20:47:21 +01:00
|
|
|
void Network::CloseSocket()
|
2009-01-29 21:35:06 +01:00
|
|
|
{
|
2009-02-01 20:47:21 +01:00
|
|
|
close(this->sock);
|
2009-01-29 21:35:06 +01:00
|
|
|
}
|