frodo-wii/Src/NetworkWii.h

106 lines
2.1 KiB
C
Raw Normal View History

2009-02-07 12:28:29 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <network.h>
static int set_sock_opts(int sock)
{
int d = 1;
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
}
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 = net_gethostbyname ((char*)hostname);
2009-02-07 12:28:29 +01:00
if (hostinfo == NULL)
{
fprintf (stderr, "Unknown host %s.\n", hostname);
return false;
}
memset(name, 0, sizeof(name));
name->sin_family = AF_INET;
name->sin_port = htons (port);
name->sin_len = 8; // sizeof(struct sockaddr_in);
name->sin_addr.s_addr = *(uint32*)hostinfo->h_addr_list[0];
2009-02-07 12:28:29 +01:00
return true;
}
2010-02-14 09:57:55 +01:00
bool Network::InitSocket()
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
{
fprintf (stderr, "Could not init socket. Failed with %d\n", this->sock);
sleep(5);
2009-02-07 12:28:29 +01:00
return false;
}
int _true = false;
if (net_ioctl (this->sock, FIONBIO, (char *)(&_true)) < 0)
fprintf(stderr, "Could not set FIONBIO\n");
2009-02-07 12:28:29 +01:00
2009-02-21 08:59:45 +01:00
set_sock_opts(this->sock);
2009-02-07 12:28:29 +01:00
return true;
}
ssize_t Network::ReceiveFrom(void *dst, int sock, size_t sz,
struct sockaddr_in *from)
{
return net_recv(sock, dst, sz, 0);
2009-02-07 12:28:29 +01:00
}
ssize_t Network::SendTo(void *src, int sock, size_t sz, struct sockaddr_in *to)
{
socklen_t to_sz = to->sin_len;
2009-02-07 12:28:29 +01:00
assert(to);
return net_sendto(sock, src, sz, 0, (struct sockaddr*)to, to_sz);
}
bool Network::Select(int sock, struct timeval *tv)
{
2009-03-28 08:59:20 +01:00
struct pollsd sds;
int v;
2009-02-07 12:28:29 +01:00
2009-03-28 08:59:20 +01:00
sds.socket = sock;
sds.events = POLLIN;
sds.revents = 0;
2010-03-06 16:34:44 +01:00
if (tv)
v = net_poll(&sds, 1, tv->tv_sec * 1000 + tv->tv_usec / 1000);
else
v = net_poll(&sds, 1, 0);
2009-03-28 08:59:20 +01:00
return v > 0;
2009-02-07 12:28:29 +01:00
}
void Network::CloseSocket()
{
#define SHUT_RDWR 2
2009-03-29 19:23:27 +02:00
net_shutdown(this->sock, SHUT_RDWR);
2009-02-07 12:28:29 +01:00
net_close(this->sock);
}
void Network::InitNetwork()
{
char myIP[16];
2010-03-06 16:34:44 +01:00
memset(myIP, 0, sizeof(myIP));
2010-03-06 16:21:44 +01:00
Gui::gui->status_bar->queueMessage("Getting IP address via DHCP...");
2010-03-06 16:58:38 +01:00
if (if_config(myIP, NULL, NULL, true) < 0) {
Gui::gui->status_bar->queueMessage("No DHCP reply");
2010-03-06 16:34:44 +01:00
return;
}
2010-03-06 16:21:44 +01:00
Gui::gui->status_bar->queueMessage("Got an address");
}
void Network::ShutdownNetwork()
{
}