libctru  v2.4.1
soc.h
Go to the documentation of this file.
1 /**
2  * @file soc.h
3  * @brief SOC service for sockets communications
4  *
5  * After initializing this service you will be able to use system calls from netdb.h, sys/socket.h etc.
6  */
7 #pragma once
8 #include <netinet/in.h>
9 #include <sys/socket.h>
10 
11 /// The config level to be used with @ref SOCU_GetNetworkOpt
12 #define SOL_CONFIG 0xfffe
13 
14 /// Options to be used with @ref SOCU_GetNetworkOpt
15 typedef enum
16 {
17  NETOPT_MAC_ADDRESS = 0x1004, ///< The mac address of the interface (u32 mac[6])
18  NETOPT_ARP_TABLE = 0x3002, ///< The ARP table @see SOCU_ARPTableEntry
19  NETOPT_IP_INFO = 0x4003, ///< The current IP setup @see SOCU_IPInfo
20  NETOPT_IP_MTU = 0x4004, ///< The value of the IP MTU (u32)
21  NETOPT_ROUTING_TABLE = 0x4006, ///< The routing table @see SOCU_RoutingTableEntry
22  NETOPT_UDP_NUMBER = 0x8002, ///< The number of sockets in the UDP table (u32)
23  NETOPT_UDP_TABLE = 0x8003, ///< The table of opened UDP sockets @see SOCU_UDPTableEntry
24  NETOPT_TCP_NUMBER = 0x9002, ///< The number of sockets in the TCP table (u32)
25  NETOPT_TCP_TABLE = 0x9003, ///< The table of opened TCP sockets @see SOCU_TCPTableEntry
26  NETOPT_DNS_TABLE = 0xB003, ///< The table of the DNS servers @see SOCU_DNSTableEntry -- Returns a buffer of size 336 but only 2 entries are set ?
27  NETOPT_DHCP_LEASE_TIME = 0xC001, ///< The DHCP lease time remaining, in seconds
28 } NetworkOpt;
29 
30 /// One entry of the ARP table retrieved by using @ref SOCU_GetNetworkOpt and @ref NETOPT_ARP_TABLE
31 typedef struct
32 {
33  u32 unk0; // often 2 ? state ?
34  struct in_addr ip; ///< The IPv4 address associated to the entry
35  u8 mac[6]; ///< The MAC address of associated to the entry
36  u8 padding[2];
38 
39 /// Structure returned by @ref SOCU_GetNetworkOpt when using @ref NETOPT_IP_INFO
40 typedef struct
41 {
42  struct in_addr ip; ///< Current IPv4 address
43  struct in_addr netmask; ///< Current network mask
44  struct in_addr broadcast; ///< Current network broadcast address
45 } SOCU_IPInfo;
46 
47 // Linux netstat flags
48 // NOTE : there are probably other flags supported, if you can forge ICMP requests please check for D and M flags
49 
50 /** The route uses a gateway */
51 #define ROUTING_FLAG_G 0x01
52 
53 /// One entry of the routing table retrieved by using @ref SOCU_GetNetworkOpt and @ref NETOPT_ROUTING_TABLE
54 typedef struct
55 {
56  struct in_addr dest_ip; ///< Destination IP address of the route
57  struct in_addr netmask; ///< Mask used for this route
58  struct in_addr gateway; ///< Gateway address to reach the network
59  u32 flags; ///< Linux netstat flags @see ROUTING_FLAG_G
60  u64 time; ///< number of milliseconds since 1st Jan 1900 00:00.
62 
63 /// One entry of the UDP sockets table retrieved by using @ref SOCU_GetNetworkOpt and @ref NETOPT_UDP_TABLE
64 typedef struct
65 {
66  struct sockaddr_storage local; ///< Local address information
67  struct sockaddr_storage remote; ///< Remote address information
69 
70 ///@name TCP states
71 ///@{
72 #define TCP_STATE_CLOSED 1
73 #define TCP_STATE_LISTEN 2
74 #define TCP_STATE_ESTABLISHED 5
75 #define TCP_STATE_FINWAIT1 6
76 #define TCP_STATE_FINWAIT2 7
77 #define TCP_STATE_CLOSE_WAIT 8
78 #define TCP_STATE_LAST_ACK 9
79 #define TCP_STATE_TIME_WAIT 11
80 ///@}
81 
82 /// One entry of the TCP sockets table retrieved by using @ref SOCU_GetNetworkOpt and @ref NETOPT_TCP_TABLE
83 typedef struct
84 {
85  u32 state; ///< @see TCP states defines
86  struct sockaddr_storage local; ///< Local address information
87  struct sockaddr_storage remote; ///< Remote address information
89 
90 /// One entry of the DNS servers table retrieved by using @ref SOCU_GetNetworkOpt and @ref NETOPT_DNS_TABLE
91 typedef struct
92 {
93  u32 family; /// Family of the address of the DNS server
94  struct in_addr ip; /// IP of the DNS server
95  u8 padding[12]; // matches the length required for IPv6 addresses
97 
98 
99 /**
100  * @brief Initializes the SOC service.
101  * @param context_addr Address of a page-aligned (0x1000) buffer to be used.
102  * @param context_size Size of the buffer, a multiple of 0x1000.
103  * @note The specified context buffer can no longer be accessed by the process which called this function, since the userland permissions for this block are set to no-access.
104  */
105 Result socInit(u32 *context_addr, u32 context_size);
106 
107 /**
108  * @brief Closes the soc service.
109  * @note You need to call this in order to be able to use the buffer again.
110  */
112 
113 // this is supposed to be in unistd.h but newlib only puts it for cygwin, waiting for newlib patch from dkA
114 /**
115  * @brief Gets the system's host ID.
116  * @return The system's host ID.
117  */
118 long gethostid(void);
119 
120 // this is supposed to be in unistd.h but newlib only puts it for cygwin, waiting for newlib patch from dkA
121 int gethostname(char *name, size_t namelen);
122 
123 int SOCU_ShutdownSockets(void);
124 
125 int SOCU_CloseSockets(void);
126 
127 /**
128  * @brief Retrieves information from the network configuration. Similar to getsockopt().
129  * @param level Only value allowed seems to be @ref SOL_CONFIG
130  * @param optname The option to be retrieved
131  * @param optval Will contain the output of the command
132  * @param optlen Size of the optval buffer, will be updated to hold the size of the output
133  * @return 0 if successful. -1 if failed, and errno will be set accordingly. Can also return a system error code.
134  */
135 int SOCU_GetNetworkOpt(int level, NetworkOpt optname, void * optval, socklen_t * optlen);
136 
137 /**
138  * @brief Gets the system's IP address, netmask, and subnet broadcast
139  * @return error
140  */
141 int SOCU_GetIPInfo(struct in_addr *ip, struct in_addr *netmask, struct in_addr *broadcast);
142 
143 /**
144  * @brief Adds a global socket.
145  * @param sockfd The socket fd.
146  * @return error
147  */
148 int SOCU_AddGlobalSocket(int sockfd);
int SOCU_GetNetworkOpt(int level, NetworkOpt optname, void *optval, socklen_t *optlen)
Retrieves information from the network configuration.
Result socInit(u32 *context_addr, u32 context_size)
Initializes the SOC service.
int SOCU_AddGlobalSocket(int sockfd)
Adds a global socket.
Result socExit(void)
Closes the soc service.
NetworkOpt
Options to be used with SOCU_GetNetworkOpt.
Definition: soc.h:16
@ NETOPT_MAC_ADDRESS
The mac address of the interface (u32 mac[6])
Definition: soc.h:17
@ NETOPT_DNS_TABLE
The table of the DNS servers.
Definition: soc.h:26
@ NETOPT_TCP_NUMBER
The number of sockets in the TCP table (u32)
Definition: soc.h:24
@ NETOPT_DHCP_LEASE_TIME
The DHCP lease time remaining, in seconds.
Definition: soc.h:27
@ NETOPT_TCP_TABLE
The table of opened TCP sockets.
Definition: soc.h:25
@ NETOPT_ROUTING_TABLE
The routing table.
Definition: soc.h:21
@ NETOPT_UDP_NUMBER
The number of sockets in the UDP table (u32)
Definition: soc.h:22
@ NETOPT_UDP_TABLE
The table of opened UDP sockets.
Definition: soc.h:23
@ NETOPT_ARP_TABLE
The ARP table.
Definition: soc.h:18
@ NETOPT_IP_INFO
The current IP setup.
Definition: soc.h:19
@ NETOPT_IP_MTU
The value of the IP MTU (u32)
Definition: soc.h:20
int SOCU_GetIPInfo(struct in_addr *ip, struct in_addr *netmask, struct in_addr *broadcast)
Gets the system's IP address, netmask, and subnet broadcast.
long gethostid(void)
Gets the system's host ID.
One entry of the ARP table retrieved by using SOCU_GetNetworkOpt and NETOPT_ARP_TABLE.
Definition: soc.h:32
One entry of the DNS servers table retrieved by using SOCU_GetNetworkOpt and NETOPT_DNS_TABLE.
Definition: soc.h:92
Structure returned by SOCU_GetNetworkOpt when using NETOPT_IP_INFO.
Definition: soc.h:41
One entry of the routing table retrieved by using SOCU_GetNetworkOpt and NETOPT_ROUTING_TABLE.
Definition: soc.h:55
u32 flags
Linux netstat flags.
Definition: soc.h:59
One entry of the TCP sockets table retrieved by using SOCU_GetNetworkOpt and NETOPT_TCP_TABLE.
Definition: soc.h:84
One entry of the UDP sockets table retrieved by using SOCU_GetNetworkOpt and NETOPT_UDP_TABLE.
Definition: soc.h:65
Definition: in.h:30
Definition: socket.h:58
uint64_t u64
64-bit unsigned integer
Definition: types.h:24
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
s32 Result
Function result.
Definition: types.h:42
uint32_t u32
32-bit unsigned integer
Definition: types.h:23