Files
aaruremote/wii/network.c

145 lines
2.9 KiB
C
Raw Normal View History

/*
2020-03-01 05:44:49 +00:00
* This file is part of the Aaru Remote Server.
2020-12-31 23:12:05 +00:00
* Copyright (c) 2019-2021 Natalia Portillo.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <network.h>
2019-10-20 22:02:32 +01:00
#include <stdio.h>
#include <stdlib.h>
2020-10-24 04:23:01 +01:00
#include "../aaruremote.h"
#include "wii.h"
int PrintNetworkAddresses()
{
2019-10-20 22:02:32 +01:00
int ret;
char localip[16] = {0};
char gateway[16] = {0};
char netmask[16] = {0};
ret = if_config(localip, netmask, gateway, TRUE, 20);
if(ret < 0) return -1;
printf("Available addresses:\n");
2020-03-01 05:50:46 +00:00
printf("%s port %d\n", localip, AARUREMOTE_PORT);
2019-10-20 22:02:32 +01:00
return 0;
2019-10-20 20:47:36 +01:00
}
2024-04-30 15:42:08 +01:00
char *PrintIpv4Address(struct in_addr addr) { return inet_ntoa(addr); }
void *NetSocket(uint32_t domain, uint32_t type, uint32_t protocol)
2019-10-20 20:47:36 +01:00
{
2024-04-30 15:42:08 +01:00
NetworkContext *ctx;
2019-10-26 22:22:04 +01:00
2020-03-01 19:47:15 +00:00
ctx = malloc(sizeof(NetworkContext));
2019-10-26 22:22:04 +01:00
if(!ctx) return NULL;
ctx->fd = net_socket(domain, type, protocol);
if(ctx->fd < 0)
{
free(ctx);
return NULL;
}
return ctx;
2019-10-26 22:22:04 +01:00
}
2024-04-30 15:42:08 +01:00
int32_t NetBind(void *net_ctx, struct sockaddr *addr, socklen_t addrlen)
2019-10-26 22:22:04 +01:00
{
2024-04-30 15:42:08 +01:00
NetworkContext *ctx = net_ctx;
2019-10-26 22:22:04 +01:00
if(!ctx) return -1;
return net_bind(ctx->fd, addr, addrlen);
}
2024-04-30 15:42:08 +01:00
int32_t NetListen(void *net_ctx, uint32_t backlog)
2019-10-26 22:22:04 +01:00
{
2024-04-30 15:42:08 +01:00
NetworkContext *ctx = net_ctx;
2019-10-26 22:22:04 +01:00
if(!ctx) return -1;
return net_listen(ctx->fd, backlog);
}
2024-04-30 15:42:08 +01:00
void *NetAccept(void *net_ctx, struct sockaddr *addr, socklen_t *addrlen)
2019-10-26 22:22:04 +01:00
{
2024-04-30 15:42:08 +01:00
NetworkContext *ctx = net_ctx;
NetworkContext *cli_ctx;
2019-10-26 22:22:04 +01:00
if(!ctx) return NULL;
2020-03-01 19:47:15 +00:00
cli_ctx = malloc(sizeof(NetworkContext));
2019-10-26 22:22:04 +01:00
if(!cli_ctx) return NULL;
cli_ctx->fd = net_accept(ctx->fd, addr, addrlen);
if(cli_ctx->fd < 0)
{
free(cli_ctx);
return NULL;
}
return cli_ctx;
2019-10-20 20:47:36 +01:00
}
2019-10-26 22:22:04 +01:00
2024-04-30 15:42:08 +01:00
int32_t NetRecv(void *net_ctx, void *buf, int32_t len, uint32_t flags)
2019-10-21 19:05:06 +01:00
{
2024-04-30 15:42:08 +01:00
NetworkContext *ctx = net_ctx;
2019-10-26 22:22:04 +01:00
if(!ctx) return -1;
2019-10-21 19:05:06 +01:00
int32_t got_once;
int32_t got_total = 0;
while(len > 0)
{
2019-10-26 22:22:04 +01:00
got_once = net_recv(ctx->fd, buf, len, flags);
2019-10-21 19:05:06 +01:00
if(got_once <= 0) break;
buf += got_once;
got_total += got_once;
len -= got_once;
}
return got_total;
}
2019-10-26 22:22:04 +01:00
2024-04-30 15:42:08 +01:00
int32_t NetWrite(void *net_ctx, const void *buf, int32_t size)
2019-10-26 22:22:04 +01:00
{
2024-04-30 15:42:08 +01:00
NetworkContext *ctx = net_ctx;
2019-10-26 22:22:04 +01:00
if(!ctx) return -1;
return net_write(ctx->fd, buf, size);
}
2024-04-30 15:42:08 +01:00
int32_t NetClose(void *net_ctx)
2019-10-26 22:22:04 +01:00
{
2020-03-01 19:47:15 +00:00
int ret;
2024-04-30 15:42:08 +01:00
NetworkContext *ctx = net_ctx;
2019-10-26 22:22:04 +01:00
if(!ctx) return -1;
ret = net_close(ctx->fd);
free(ctx);
return ret;
}