From d33b26016dd632e4e3fa2dcf10a70f5f8f59d6d9 Mon Sep 17 00:00:00 2001 From: TuxSH Date: Mon, 19 Feb 2018 19:11:28 +0100 Subject: [PATCH] Add documentation to bsd.h and sfdnsres.h, etc. --- nx/include/net/bpf.h | 2 +- nx/include/netdb.h | 2 +- nx/include/netinet/in.h | 2 +- nx/include/netinet/tcp.h | 2 +- nx/include/netinet/udp.h | 2 +- nx/include/poll.h | 2 +- nx/include/switch/runtime/devices/socket.h | 5 ++--- nx/include/switch/services/bsd.h | 26 +++++++++++++--------- nx/include/switch/services/sfdnsres.h | 12 ++++++++-- nx/include/sys/socket.h | 2 +- nx/include/sys/sysctl.h | 2 +- nx/source/runtime/devices/socket.c | 9 ++------ nx/source/services/bsd.c | 16 ++++++------- nx/source/services/sfdnsres.c | 1 - 14 files changed, 46 insertions(+), 39 deletions(-) diff --git a/nx/include/net/bpf.h b/nx/include/net/bpf.h index 87571883..bc3c2e62 100644 --- a/nx/include/net/bpf.h +++ b/nx/include/net/bpf.h @@ -1,6 +1,6 @@ // TuxSH: removed definitions under _KERNEL ifdef blocks, modify the prototype of some functions, some other cleanup, etc. #ifndef __BSD_VISIBLE -#define __BSD_VISIBLE +#define __BSD_VISIBLE 1 #endif // Stubbed some stuff diff --git a/nx/include/netdb.h b/nx/include/netdb.h index 46dda840..2b51f526 100644 --- a/nx/include/netdb.h +++ b/nx/include/netdb.h @@ -1,6 +1,6 @@ // TuxSH: define __BSD_VISIBLE, change a lot of symbols, etc #ifndef __BSD_VISIBLE -#define __BSD_VISIBLE +#define __BSD_VISIBLE 1 #endif /*- diff --git a/nx/include/netinet/in.h b/nx/include/netinet/in.h index 596e80f2..0f624398 100644 --- a/nx/include/netinet/in.h +++ b/nx/include/netinet/in.h @@ -1,6 +1,6 @@ // TuxSH: removed definitions under _KERNEL ifdef blocks, modify the prototype of some functions, some other cleanup, etc. #ifndef __BSD_VISIBLE -#define __BSD_VISIBLE +#define __BSD_VISIBLE 1 #endif /*- diff --git a/nx/include/netinet/tcp.h b/nx/include/netinet/tcp.h index 8e610735..c4c58ede 100644 --- a/nx/include/netinet/tcp.h +++ b/nx/include/netinet/tcp.h @@ -1,6 +1,6 @@ // TuxSH: removed definitions under _KERNEL ifdef blocks, modify the prototype of some functions, some other cleanup, etc. #ifndef __BSD_VISIBLE -#define __BSD_VISIBLE +#define __BSD_VISIBLE 1 #endif /*- diff --git a/nx/include/netinet/udp.h b/nx/include/netinet/udp.h index 994d9b62..6f8eff76 100644 --- a/nx/include/netinet/udp.h +++ b/nx/include/netinet/udp.h @@ -1,6 +1,6 @@ // TuxSH: removed definitions under _KERNEL ifdef blocks, modify the prototype of some functions, some other cleanup, etc. #ifndef __BSD_VISIBLE -#define __BSD_VISIBLE +#define __BSD_VISIBLE 1 #endif /*- diff --git a/nx/include/poll.h b/nx/include/poll.h index 86f05d4e..9e82e741 100644 --- a/nx/include/poll.h +++ b/nx/include/poll.h @@ -1,6 +1,6 @@ // TuxSH: removed definitions under _KERNEL ifdef blocks, modify the prototype of some functions, some other cleanup, etc. #ifndef __BSD_VISIBLE -#define __BSD_VISIBLE +#define __BSD_VISIBLE 1 #endif /*- diff --git a/nx/include/switch/runtime/devices/socket.h b/nx/include/switch/runtime/devices/socket.h index 7a78012d..155eea6f 100644 --- a/nx/include/switch/runtime/devices/socket.h +++ b/nx/include/switch/runtime/devices/socket.h @@ -29,12 +29,11 @@ Result socketInitialize(const SocketInitConfig *config); Result socketGetLastBsdResult(void); /// Fetch the last sfdnsres Switch result code (thread-local). Result socketGetLastSfdnsresResult(void); -/// Deinitializes the socket driver. +/// Deinitialize the socket driver. void socketExit(void); /// Initalize the socket driver using the default configuration. -static inline Result socketInitializeDefault(void) -{ +static inline Result socketInitializeDefault(void) { return socketInitialize(socketGetDefaultInitConfig()); } diff --git a/nx/include/switch/services/bsd.h b/nx/include/switch/services/bsd.h index f21fbd10..e21121d9 100644 --- a/nx/include/switch/services/bsd.h +++ b/nx/include/switch/services/bsd.h @@ -1,18 +1,18 @@ /** * @file bsd.h - * @brief BSD sockets (bsd:u/s) service IPC wrapper. + * @brief BSD sockets (bsd:u/s) service IPC wrapper. Please use socket.c instead. * @author plutoo * @author TuxSH * @copyright libnx Authors */ #pragma once -#include "../types.h" -#include "../kernel/tmem.h" - #include // for socklen_t #include // for fd_set #include // for struct pollfd, ndfs_t +#include "../types.h" +#include "../kernel/tmem.h" + /// Configuration structure for bsdInitalize typedef struct { u32 version; ///< Observed 1 on 2.0 LibAppletWeb, 2 on 3.0. @@ -31,11 +31,15 @@ typedef struct { extern __thread Result g_bsdResult; ///< Last Switch "result", per-thread extern __thread int g_bsdErrno; ///< Last errno, per-thread -const BsdInitConfig *bsdGetDefaultConfig(void); +/// Fetch the default configuration for bsdInitialize. +const BsdInitConfig *bsdGetDefaultInitConfig(void); +/// Initialize the BSD service. Result bsdInitialize(const BsdInitConfig *config); +/// Deinitialize the BSD service. void bsdExit(void); int bsdSocket(int domain, int type, int protocol); +/// Like @ref bsdSocket but the newly created socket is immediately shut down. int bsdSocketExempt(int domain, int type, int protocol); int bsdOpen(const char *pathname, int flags); int bsdSelect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); @@ -52,8 +56,9 @@ int bsdGetPeerName(int sockfd, struct sockaddr *addr, socklen_t *addrlen); int bsdGetSockName(int sockfd, struct sockaddr *addr, socklen_t *addrlen); int bsdGetSockOpt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); int bsdListen(int sockfd, int backlog); -// The following two functions are supposed to be variadic +/// Made non-variadic for convenience. int bsdIoctl(int fd, int request, void *data); +/// Made non-variadic for convenience. int bsdFcntl(int fd, int cmd, int flags); int bsdSetSockOpt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); int bsdShutdown(int sockfd, int how); @@ -61,11 +66,12 @@ int bsdShutdownAllSockets(int how); ssize_t bsdWrite(int fd, const void *buf, size_t count); ssize_t bsdRead(int fd, void *buf, size_t count); int bsdClose(int fd); +/// Duplicate a socket (bsd:s). int bsdDuplicateSocket(int sockfd); -//TODO: Reverse-engineer GetResourceStatistics, sendmmsg/recvmmsg (custom (un)serialization) +// TODO: Reverse-engineer GetResourceStatistics. Implement sendmmsg/recvmmsg (custom (un)serialization) -static inline Result bsdInitializeDefault(void) -{ - return bsdInitialize(bsdGetDefaultConfig()); +/// Initialize the BSD service using the default configuration. +static inline Result bsdInitializeDefault(void) { + return bsdInitialize(bsdGetDefaultInitConfig()); } diff --git a/nx/include/switch/services/sfdnsres.h b/nx/include/switch/services/sfdnsres.h index d1eb7400..26ca14de 100644 --- a/nx/include/switch/services/sfdnsres.h +++ b/nx/include/switch/services/sfdnsres.h @@ -1,3 +1,9 @@ +/** + * @file sfdnsres.h + * @brief Domain name resolution service IPC wrapper. Please use socket.c instead. + * @author TuxSH + * @copyright libnx Authors + */ #pragma once #include "../types.h" @@ -32,9 +38,11 @@ Result sfdnsresGetNameInfo(SfdnsresRequestResults *ret, const SfdnsresConfig *co const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags); +/// Requests an handle for use with @ref sfdnsresCancelSocketCall. Result sfdnsresRequestCancelHandle(u32 *out_handle); -/// Bug: always sets errno ? +/// Cancels a DNS request (how? which requests?). Bug: always sets errno? Result sfdnsresCancelSocketCall(SfdnsresRequestResults *ret, u32 handle); -/// Bug: always sets errno ? +/// Cancels all DNS requests made by the current process (how? which requests?). Bug: always sets errno? Result sfdnsresCancelAllSocketCalls(SfdnsresRequestResults *ret); +/// Clears up to 4 DNS server IPs registered by bsdcfg (DHCP client, etc.). Result sfdnsresClearDnsIpServerAddressArray(void); diff --git a/nx/include/sys/socket.h b/nx/include/sys/socket.h index 6b50e8f9..c8195dc2 100644 --- a/nx/include/sys/socket.h +++ b/nx/include/sys/socket.h @@ -1,6 +1,6 @@ // TuxSH: removed definitions under _KERNEL ifdef blocks, modify the prototype of some functions, some other cleanup, etc. #ifndef __BSD_VISIBLE -#define __BSD_VISIBLE +#define __BSD_VISIBLE 1 #endif /*- diff --git a/nx/include/sys/sysctl.h b/nx/include/sys/sysctl.h index 1c0b93a1..929c1632 100644 --- a/nx/include/sys/sysctl.h +++ b/nx/include/sys/sysctl.h @@ -1,7 +1,7 @@ // TuxSH: removed definitions under _KERNEL ifdef blocks, modify the prototype of some functions, some other cleanup, etc. // Note: I didn't provide #ifndef __BSD_VISIBLE -#define __BSD_VISIBLE +#define __BSD_VISIBLE 1 #endif /*- diff --git a/nx/source/runtime/devices/socket.c b/nx/source/runtime/devices/socket.c index acf83aa2..5d91913f 100644 --- a/nx/source/runtime/devices/socket.c +++ b/nx/source/runtime/devices/socket.c @@ -147,7 +147,6 @@ Result socketGetLastSfdnsresResult(void) { return g_sfdnsresResult; } -/********************************************* BSD:U FUNCTIONS AND RELATED *********************************************/ /***********************************************************************************************************************/ static int _socketGetFd(int fd) { @@ -659,8 +658,6 @@ int recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags, s return -1; } - -/****************************************** ARPA/INET.H FUNCTIONS AND RELATED ******************************************/ /***********************************************************************************************************************/ // Adapted from ctrulib @@ -1057,7 +1054,6 @@ in_addr_t inet_addr(const char *cp) { return addr.s_addr; } -/******************************************** NETDB.H FUNCTIONS AND RELATED ********************************************/ /***********************************************************************************************************************/ static struct hostent *_socketDeserializeHostent(int *err, const void *out_he_serialized) { @@ -1327,7 +1323,7 @@ struct hostent *gethostbyname(const char *name) { Result rc = 0; void *out_he_serialized = malloc(g_sfdnsresConfig.serialized_out_hostent_max_size); struct hostent *he = NULL; - SfdnsresRequestResults ret; + SfdnsresRequestResults ret; if(out_he_serialized == NULL) { h_errno = NO_RECOVERY; @@ -1371,7 +1367,7 @@ struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type) { Result rc = 0; void *out_he_serialized = malloc(g_sfdnsresConfig.serialized_out_hostent_max_size); struct hostent *he = NULL; - SfdnsresRequestResults ret; + SfdnsresRequestResults ret; if(out_he_serialized == NULL) { h_errno = NO_RECOVERY; @@ -1539,7 +1535,6 @@ void sethostent(int a) { (void)a;} void setnetent(int a) { (void)a;} void setprotoent(int a) { (void)a; } -/******************************************** UNISTD.H FUNCTIONS AND RELATED ********************************************/ /************************************************************************************************************************/ long gethostid(void) { diff --git a/nx/source/services/bsd.c b/nx/source/services/bsd.c index 434ac239..d6d7ee6b 100644 --- a/nx/source/services/bsd.c +++ b/nx/source/services/bsd.c @@ -2,14 +2,6 @@ #include #include -#include "types.h" -#include "result.h" -#include "ipc.h" -#include "services/bsd.h" -#include "services/sm.h" -#include "kernel/shmem.h" -#include "kernel/rwlock.h" - // Complete definition of struct timeout: #include @@ -20,6 +12,14 @@ #include #include +#include "types.h" +#include "result.h" +#include "ipc.h" +#include "services/bsd.h" +#include "services/sm.h" +#include "kernel/shmem.h" +#include "kernel/rwlock.h" + __thread Result g_bsdResult; __thread int g_bsdErrno; diff --git a/nx/source/services/sfdnsres.c b/nx/source/services/sfdnsres.c index 2b2bb4d3..143cf07c 100644 --- a/nx/source/services/sfdnsres.c +++ b/nx/source/services/sfdnsres.c @@ -102,7 +102,6 @@ Result sfdnsresGetHostByName(SfdnsresRequestResults *ret, const SfdnsresConfig * IpcCommand c; ipcInitialize(&c); ipcAddSendBuffer(&c, name, name == NULL ? 0 : strlen(name) + 1, 0); - ipcAddRecvBuffer(&c, out_he_serialized, config->serialized_out_hostent_max_size, 0); return _sfdnsresDnsRequestCommand(&c, 2, ret, config, true, NULL);