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));
|
2009-02-21 08:59:45 +01:00
|
|
|
return setsockopt(sock,SOL_SOCKET,SO_REUSEADDR, &d, sizeof(int));
|
2009-01-29 19:04:31 +01:00
|
|
|
}
|
|
|
|
|
2009-02-13 08:58:33 +01:00
|
|
|
bool Network::InitSockaddr (struct sockaddr_in *name,
|
2009-01-24 16:48:43 +01:00
|
|
|
const char *hostname, uint16_t port)
|
|
|
|
{
|
2009-03-01 16:47:50 +01:00
|
|
|
struct hostent *hostinfo = gethostbyname (hostname);
|
2009-01-24 16:48:43 +01:00
|
|
|
if (hostinfo == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Unknown host %s.\n", hostname);
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-01 16:47:50 +01:00
|
|
|
|
|
|
|
name->sin_family = AF_INET;
|
|
|
|
name->sin_port = htons (port);
|
2009-01-24 16:48:43 +01:00
|
|
|
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2010-02-14 09:57:55 +01:00
|
|
|
bool Network::InitSocket()
|
2009-02-08 12:27:56 +01:00
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-01-24 16:48:43 +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::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
|
|
|
}
|
2009-03-08 20:02:19 +01:00
|
|
|
|
|
|
|
void Network::InitNetwork()
|
|
|
|
{
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
2009-03-28 10:34:31 +01:00
|
|
|
|
|
|
|
void Network::ShutdownNetwork()
|
|
|
|
{
|
|
|
|
}
|