libnx  v4.8.0
bsd.h
Go to the documentation of this file.
1 /**
2  * @file bsd.h
3  * @brief BSD sockets (bsd:u/s) service IPC wrapper. Please use the standard <sys/socket.h> interface instead.
4  * @author plutoo
5  * @author TuxSH
6  * @copyright libnx Authors
7  */
8 #pragma once
9 #include <sys/socket.h> // for socklen_t
10 #include <sys/select.h> // for fd_set
11 #include <poll.h> // for struct pollfd, ndfs_t
12 
13 #include "../types.h"
14 #include "../kernel/tmem.h"
15 #include "../sf/service.h"
16 
17 /// Configuration structure for bsdInitalize
18 typedef struct {
19  u32 version; ///< Observed 1 on [2.0.0+] LibAppletWeb, 2 on [3.0.0+].
20 
21  void *tmem_buffer; ///< User-provided buffer to use as backing for transfer memory. If NULL, a buffer will be allocated automatically. Must be large enough and page-aligned.
22  size_t tmem_buffer_size; ///< Size of the user-provided transfer memory backing buffer. Must be large enough and page-aligned.
23 
24  u32 tcp_tx_buf_size; ///< Size of the TCP transfer (send) buffer (initial or fixed).
25  u32 tcp_rx_buf_size; ///< Size of the TCP receive buffer (initial or fixed).
26  u32 tcp_tx_buf_max_size; ///< Maximum size of the TCP transfer (send) buffer. If it is 0, the size of the buffer is fixed to its initial value.
27  u32 tcp_rx_buf_max_size; ///< Maximum size of the TCP receive buffer. If it is 0, the size of the buffer is fixed to its initial value.
28 
29  u32 udp_tx_buf_size; ///< Size of the UDP transfer (send) buffer (typically 0x2400 bytes).
30  u32 udp_rx_buf_size; ///< Size of the UDP receive buffer (typically 0xA500 bytes).
31 
32  u32 sb_efficiency; ///< Number of buffers for each socket (standard values range from 1 to 8).
34 
35 extern __thread Result g_bsdResult; ///< Last Switch "result", per-thread
36 extern __thread int g_bsdErrno; ///< Last errno, per-thread
37 
38 /// Fetch the default configuration for bsdInitialize.
40 
41 /// Initialize the BSD service.
42 Result bsdInitialize(const BsdInitConfig *config, u32 num_sessions, u32 service_type);
43 
44 /// Exit the BSD service.
45 void bsdExit(void);
46 
47 /// Gets the Service object for the actual BSD service session.
49 
50 /// Creates a socket.
51 int bsdSocket(int domain, int type, int protocol);
52 /// Like @ref bsdSocket but the newly created socket is immediately shut down.
53 int bsdSocketExempt(int domain, int type, int protocol);
54 int bsdOpen(const char *pathname, int flags);
55 int bsdSelect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
56 int bsdPoll(struct pollfd *fds, nfds_t nfds, int timeout);
57 int bsdSysctl(const int *name, unsigned int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen);
58 ssize_t bsdRecv(int sockfd, void *buf, size_t len, int flags);
59 ssize_t bsdRecvFrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
60 ssize_t bsdSend(int sockfd, const void* buf, size_t len, int flags);
61 ssize_t bsdSendTo(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
62 int bsdAccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
63 int bsdBind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
64 int bsdConnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
65 int bsdGetPeerName(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
66 int bsdGetSockName(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
67 int bsdGetSockOpt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);
68 int bsdListen(int sockfd, int backlog);
69 /// Made non-variadic for convenience.
70 int bsdIoctl(int fd, int request, void *data);
71 /// Made non-variadic for convenience.
72 int bsdFcntl(int fd, int cmd, int flags);
73 int bsdSetSockOpt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
74 int bsdShutdown(int sockfd, int how);
75 int bsdShutdownAllSockets(int how);
76 ssize_t bsdWrite(int fd, const void *buf, size_t count);
77 ssize_t bsdRead(int fd, void *buf, size_t count);
78 int bsdClose(int fd);
79 /// Duplicate a socket (bsd:s).
80 int bsdDuplicateSocket(int sockfd);
81 int bsdRecvMMsg(int sockfd, void *buf, size_t size, unsigned int vlen, int flags, struct timespec *timeout);
82 int bsdSendMMsg(int sockfd, void *buf, size_t size, unsigned int vlen, int flags);
83 
84 // TODO: Reverse-engineer GetResourceStatistics.
Result bsdInitialize(const BsdInitConfig *config, u32 num_sessions, u32 service_type)
Initialize the BSD service.
int bsdDuplicateSocket(int sockfd)
Duplicate a socket (bsd:s).
int bsdFcntl(int fd, int cmd, int flags)
Made non-variadic for convenience.
int bsdSocket(int domain, int type, int protocol)
Creates a socket.
const BsdInitConfig * bsdGetDefaultInitConfig(void)
Fetch the default configuration for bsdInitialize.
__thread int g_bsdErrno
Last errno, per-thread.
int bsdIoctl(int fd, int request, void *data)
Made non-variadic for convenience.
Service * bsdGetServiceSession(void)
Gets the Service object for the actual BSD service session.
void bsdExit(void)
Exit the BSD service.
int bsdSocketExempt(int domain, int type, int protocol)
Like bsdSocket but the newly created socket is immediately shut down.
__thread Result g_bsdResult
Last Switch "result", per-thread.
Configuration structure for bsdInitalize.
Definition: bsd.h:18
u32 udp_rx_buf_size
Size of the UDP receive buffer (typically 0xA500 bytes).
Definition: bsd.h:30
u32 sb_efficiency
Number of buffers for each socket (standard values range from 1 to 8).
Definition: bsd.h:32
u32 tcp_tx_buf_max_size
Maximum size of the TCP transfer (send) buffer. If it is 0, the size of the buffer is fixed to its in...
Definition: bsd.h:26
size_t tmem_buffer_size
Size of the user-provided transfer memory backing buffer. Must be large enough and page-aligned.
Definition: bsd.h:22
u32 udp_tx_buf_size
Size of the UDP transfer (send) buffer (typically 0x2400 bytes).
Definition: bsd.h:29
u32 tcp_rx_buf_size
Size of the TCP receive buffer (initial or fixed).
Definition: bsd.h:25
u32 version
Observed 1 on [2.0.0+] LibAppletWeb, 2 on [3.0.0+].
Definition: bsd.h:19
void * tmem_buffer
User-provided buffer to use as backing for transfer memory. If NULL, a buffer will be allocated autom...
Definition: bsd.h:21
u32 tcp_rx_buf_max_size
Maximum size of the TCP receive buffer. If it is 0, the size of the buffer is fixed to its initial va...
Definition: bsd.h:27
u32 tcp_tx_buf_size
Size of the TCP transfer (send) buffer (initial or fixed).
Definition: bsd.h:24
Service object structure.
Definition: service.h:14
u32 Result
Function error code result type.
Definition: types.h:44
uint32_t u32
32-bit unsigned integer.
Definition: types.h:21