boxscript-interperter-c/source/networkstream.cpp

352 lines
8.3 KiB
C++
Raw Permalink Normal View History

2022-12-01 03:27:30 +00:00
#include "parser.hpp"
#include <cstring>
#if defined(GEKKO)
int net_initialized = 0;
char bba_local_ip[16];
char bba_netmask[16];
char bba_gateway[16];
#if defined(HW_DOL)
#include <ogc/exi.h>
int bba_exists = 0;
unsigned int exi_get_id(int chn, int dev)
{
u32 cid = 0;
EXI_GetID(chn,dev,&cid);
return cid;
}
int exi_bba_exists()
{
return exi_get_id(EXI_CHANNEL_0,EXI_DEVICE_2) == EXI_BBA_ID;
}
#endif
#endif
namespace BoxScript
{
void sockets_free()
{
#if defined(HW_RVL) && defined(LIBWIISOCKET)
wiisocket_deinit();
#elif defined(HW_RVL) && !defined(LIBWIISOCKET)
net_deinit();
#elif defined(_WIN32) || defined(WIN32)
WSACleanup();
#endif
}
int NetworkInit()
{
#if defined(LIBWIISOCKET)
return wiisocket_init();
#endif
#if defined(GEKKO) && !defined(LIBWIISOCKET)
int res;
#if defined(HW_DOL)
bba_exists = exi_bba_exists();
if(bba_exists && !net_initialized) {
#else
if(!net_initialized){
#endif
res = if_config(bba_local_ip, bba_netmask, bba_gateway, true,20);
if(res >= 0 && strcmp("255.255.255.255", bba_local_ip)) {
net_initialized = 1;
}
else {
net_initialized = 0;
}
}
#elif defined(_WIN32) || defined(WIN32)
int err= WSAStartup(0x202, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
return 1;
}
#else
#endif
return 0;
}
NetworkStream::NetworkStream(SOCKET i)
{
this->fd = i;
}
void NetworkStream::Write(uint8_t* data,size_t len)
{
#if defined(OGC_NETWORKING)
net_send(this->fd,data,len,0);
#else
int flags=0;
#if __linux__
flags=MSG_NOSIGNAL;
#endif
send(this->fd,data,len,flags);
#endif
}
size_t NetworkStream::Read(uint8_t* data,size_t len)
{
#if defined(OGC_NETWORKING)
ssize_t v=(ssize_t)net_recv(this->fd,data,len,0);
if(v < 0) return 0;
return (size_t)v;
#else
int flags=0;
#if __linux__
flags=MSG_NOSIGNAL;
#endif
ssize_t v=recv(this->fd,data,len,flags);
if(v < 0) return 0;
return (size_t)v;
#endif
}
int64_t NetworkStream::GetPosition()
{
return 0;
}
int64_t NetworkStream::GetLength()
{
return 0;
}
void NetworkStream::SetPosition(int64_t pos)
{
}
void NetworkStream::Flush()
{
}
void NetworkStream::Close()
{
#if defined(OGC_NETWORKING)
net_close(this->fd);
#else
close(this->fd);
#endif
}
bool NetworkStream::CanRead()
{
return true;
}
bool NetworkStream::CanWrite()
{
return true;
}
bool NetworkStream::CanSeek()
{
return false;
}
struct hostent* _______ghbn(string ip)
{
#if defined(GEKKO) && !defined(LIBWIISOCKET)
return net_gethostbyname(ip.c_str());
#else
return gethostbyname(ip.c_str());
#endif
}
NetworkStream* NetClient(string ip,int64_t port,SOCKET fd)
{
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons((uint16_t)port);
if (inet_aton( ip.c_str(), &serv_addr.sin_addr)
<= 0) {
struct hostent* host=_______ghbn(ip);
struct in_addr **addr_list;
addr_list = (struct in_addr **)host->h_addr_list;
if(host->h_length > 0)
{
memcpy(&serv_addr.sin_addr,addr_list[0],sizeof(struct in_addr));
}else{
return NULL;
}
}
#if defined(GEKKO) && !defined(LIBWIISOCKET)
if(net_connect(fd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr_in)))
{
return NULL;
}
#else
if(connect(fd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr_in)))
{
return NULL;
}
#endif
return new NetworkStream(fd);
}
SOCKET NetServerInit(string ip,int64_t port,int fd)
{
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
if (inet_aton( ip.c_str(), &serv_addr.sin_addr)
<= 0) {
struct hostent* host=_______ghbn(ip);
struct in_addr **addr_list;
addr_list = (struct in_addr **)host->h_addr_list;
if(host->h_length > 0)
{
memcpy(&serv_addr.sin_addr,addr_list[0],sizeof(struct in_addr));
}else{
throw exception();
}
}
#if defined(GEKKO) && !defined(LIBWIISOCKET)
if(net_bind(fd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr_in))< 0)
{
printf("bind error, is your computer already listening on that port\n");
throw exception();
}
if(net_listen(fd,100) > 0)
{
printf("listen error");
throw exception();
}
#else
if(bind(fd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr_in))< 0)
{
printf("bind error, is your computer already listening on that port\n");
throw exception();
}
if(listen(fd,100) > 0)
{
printf("listen error");
throw exception();
}
#endif
return fd;
}
SOCKET TcpServerInit(string ip,int64_t port)
{
SOCKET fd;
#if defined(GEKKO) && !defined(LIBWIISOCKET)
if ((fd = net_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
#else
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
#endif
return NetServerInit(ip,port,fd);
}
SOCKET UdpServerInit(string ip,int64_t port)
{
SOCKET fd;
#if defined(GEKKO) && !defined(LIBWIISOCKET)
if ((fd = net_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
#else
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
#endif
return NetServerInit(ip,port,fd);
}
Stream* AcceptClient(SOCKET fd,string* ip)
{
struct sockaddr_in clt_addr;
socklen_t clt_addr_len = sizeof(struct sockaddr_in);
#if defined(GEKKO) && !defined(LIBWIISOCKET)
int cltFd=net_accept(fd,(struct sockaddr*)&clt_addr,&clt_addr_len);
#else
#if defined(_WIN32) || defined(WIN32)
SOCKET cltFd=accept(fd,(struct sockaddr*)&clt_addr,&clt_addr_len);
#else
int cltFd=accept(fd,(struct sockaddr*)&clt_addr,&clt_addr_len);
#endif
#endif
ip->append(inet_ntoa(clt_addr.sin_addr));
return new NetworkStream(cltFd);
}
NetworkStream* TcpClient(string ip,int64_t port)
{
#if defined(_WIN32) || defined(WIN32)
SOCKET fd;
#else
int fd;
#endif
#if defined(GEKKO) && !defined(LIBWIISOCKET)
if ((fd = net_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return NULL;
}
#else
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return NULL;
}
#endif
return NetClient(ip,port,fd);
}
NetworkStream* UdpClient(string ip,int64_t port)
{
#if defined(_WIN32) || defined(WIN32)
SOCKET fd;
#else
int fd;
#endif
#if defined(GEKKO) && !defined(LIBWIISOCKET)
if ((fd = net_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("\n Socket creation error \n");
return NULL;
}
#else
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("\n Socket creation error \n");
return NULL;
}
#endif
return NetClient(ip,port,fd);
}
};