2021-03-07 13:09:12 +01:00
|
|
|
#include "wut_socket.h"
|
|
|
|
|
|
|
|
#define NSYSNET_UNKNOWN_ERROR_OFFSET 10000
|
|
|
|
|
2021-02-09 17:22:24 +01:00
|
|
|
int h_errno;
|
|
|
|
|
2021-04-25 21:08:20 +02:00
|
|
|
static uint32_t
|
|
|
|
__wut_socket_refcount = 0;
|
2021-03-07 13:36:30 +01:00
|
|
|
|
|
|
|
static devoptab_t
|
|
|
|
__wut_socket_devoptab =
|
|
|
|
{
|
|
|
|
.name = "soc",
|
|
|
|
.structSize = sizeof(int),
|
|
|
|
.open_r = __wut_socket_open,
|
|
|
|
.close_r = __wut_socket_close,
|
|
|
|
.write_r = __wut_socket_write,
|
|
|
|
.read_r = __wut_socket_read,
|
|
|
|
};
|
|
|
|
|
2021-03-07 13:09:12 +01:00
|
|
|
static unsigned char
|
|
|
|
__wut_nsysnet_error_code_map[] =
|
|
|
|
{
|
|
|
|
0, // 0
|
|
|
|
ENOBUFS,
|
|
|
|
ETIMEDOUT,
|
|
|
|
EISCONN,
|
|
|
|
EOPNOTSUPP,
|
|
|
|
ECONNABORTED, // 5
|
|
|
|
EWOULDBLOCK,
|
|
|
|
ECONNREFUSED,
|
|
|
|
ECONNRESET,
|
|
|
|
ENOTCONN,
|
|
|
|
EALREADY, // 10
|
|
|
|
EINVAL,
|
|
|
|
EMSGSIZE,
|
|
|
|
EPIPE,
|
|
|
|
EDESTADDRREQ,
|
|
|
|
ESHUTDOWN, // 15
|
|
|
|
ENOPROTOOPT,
|
|
|
|
EBUSY,
|
|
|
|
ENOMEM,
|
|
|
|
EADDRNOTAVAIL,
|
|
|
|
EADDRINUSE, // 20
|
|
|
|
EAFNOSUPPORT,
|
|
|
|
EINPROGRESS,
|
|
|
|
EIO,
|
|
|
|
ENOTSOCK,
|
|
|
|
EINVAL, // 25
|
|
|
|
EINVAL,
|
|
|
|
EIO,
|
|
|
|
ETOOMANYREFS,
|
|
|
|
EFAULT,
|
|
|
|
ENETUNREACH, // 30
|
|
|
|
EPROTONOSUPPORT,
|
|
|
|
EPROTOTYPE,
|
|
|
|
EINVAL,
|
|
|
|
EINVAL,
|
|
|
|
EINVAL, // 35
|
|
|
|
EINVAL,
|
|
|
|
EINVAL,
|
|
|
|
EINVAL,
|
|
|
|
EINVAL,
|
|
|
|
EINVAL, // 40
|
|
|
|
EINVAL,
|
|
|
|
ENODEV,
|
|
|
|
EBUSY,
|
|
|
|
EBUSY,
|
|
|
|
EINVAL, // 45
|
|
|
|
EINVAL,
|
|
|
|
EINVAL,
|
|
|
|
ENOMEM,
|
|
|
|
EBADFD,
|
|
|
|
ECANCELED, // 50
|
|
|
|
EMFILE,
|
|
|
|
};
|
|
|
|
|
2021-03-07 13:36:30 +01:00
|
|
|
void
|
2021-04-25 21:08:20 +02:00
|
|
|
socket_lib_init()
|
2021-03-07 13:36:30 +01:00
|
|
|
{
|
2021-04-25 21:08:20 +02:00
|
|
|
if (0 == __wut_socket_refcount++) {
|
|
|
|
RPLWRAP(socket_lib_init)();
|
|
|
|
AddDevice(&__wut_socket_devoptab);
|
2021-03-07 13:36:30 +01:00
|
|
|
}
|
2021-04-25 21:08:20 +02:00
|
|
|
}
|
2021-03-07 13:36:30 +01:00
|
|
|
|
2021-04-25 21:08:20 +02:00
|
|
|
void
|
|
|
|
socket_lib_finish()
|
|
|
|
{
|
|
|
|
if (0 == --__wut_socket_refcount) {
|
|
|
|
RemoveDevice("soc:");
|
|
|
|
RPLWRAP(socket_lib_finish)();
|
2021-03-07 13:36:30 +01:00
|
|
|
}
|
2021-04-25 21:08:20 +02:00
|
|
|
}
|
2021-03-07 13:36:30 +01:00
|
|
|
|
2021-04-25 21:08:20 +02:00
|
|
|
void
|
|
|
|
WHBInitializeSocketLibrary() __attribute__((alias("socket_lib_init")));
|
2021-03-07 13:36:30 +01:00
|
|
|
|
2021-04-25 21:08:20 +02:00
|
|
|
void
|
|
|
|
WHBDeinitializeSocketLibrary() __attribute__((alias("socket_lib_finish")));
|
2021-03-07 13:36:30 +01:00
|
|
|
|
2021-04-25 21:08:20 +02:00
|
|
|
void __attribute__((weak))
|
|
|
|
__init_wut_socket()
|
|
|
|
{
|
|
|
|
socket_lib_init();
|
|
|
|
ACInitialize();
|
2021-04-27 13:16:31 +02:00
|
|
|
ACConnectAsync();
|
2021-03-07 13:36:30 +01:00
|
|
|
}
|
|
|
|
|
2021-04-25 21:08:20 +02:00
|
|
|
void __attribute__((weak))
|
2021-03-07 13:36:30 +01:00
|
|
|
__fini_wut_socket()
|
|
|
|
{
|
2021-04-27 13:16:31 +02:00
|
|
|
ACClose();
|
2021-03-07 13:36:30 +01:00
|
|
|
ACFinalize();
|
2021-04-25 21:08:20 +02:00
|
|
|
socket_lib_finish();
|
2021-03-07 13:36:30 +01:00
|
|
|
}
|
|
|
|
|
2021-03-07 13:09:12 +01:00
|
|
|
int
|
|
|
|
__wut_get_nsysnet_fd(int fd)
|
|
|
|
{
|
|
|
|
__handle *handle = __get_handle(fd);
|
|
|
|
if (handle == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2021-03-07 13:36:30 +01:00
|
|
|
if (strcmp(devoptab_list[handle->device]->name, "soc") != 0) {
|
2021-03-07 13:09:12 +01:00
|
|
|
errno = ENOTSOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return *(int *)handle->fileStruct;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
__wut_get_nsysnet_result(struct _reent *r,
|
|
|
|
int rc)
|
|
|
|
{
|
|
|
|
int sockerror, error;
|
|
|
|
|
|
|
|
if (rc >= 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
sockerror = RPLWRAP(socketlasterr)();
|
|
|
|
|
|
|
|
if (sockerror < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sockerror < sizeof(__wut_nsysnet_error_code_map)) {
|
|
|
|
error = __wut_nsysnet_error_code_map[sockerror];
|
|
|
|
} else {
|
2021-03-07 13:36:30 +01:00
|
|
|
error = NSYSNET_UNKNOWN_ERROR_OFFSET + sockerror;
|
2021-03-07 13:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
r->_errno = error;
|
|
|
|
} else {
|
|
|
|
errno = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|