1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #pragma once
19 
20 #include <StringUtils.h>
21 #include "Core/HLE/proAdhoc.h"
22 
23 // Using constants instead of numbers for readability reason, since PSP_THREAD_ATTR_KERNEL/USER is located in sceKernelThread.cpp instead of sceKernelThread.h
24 #ifndef PSP_THREAD_ATTR_KERNEL
25 #define PSP_THREAD_ATTR_KERNEL 0x00001000
26 #endif
27 #ifndef PSP_THREAD_ATTR_USER
28 #define PSP_THREAD_ATTR_USER 0x80000000
29 #endif
30 
31 // Option Names
32 #define PSP_SO_REUSEPORT		0x0200
33 #define PSP_SO_NBIO				0x1009
34 
35 // Infrastructure Errno Numbers
36 #define INET_EAGAIN			0x0B
37 #define INET_ETIMEDOUT		0x74
38 #define INET_EINPROGRESS	0x77
39 #define INET_EISCONN		0x7F
40 
41 // On-Demand Nonblocking Flag
42 #define INET_MSG_DONTWAIT	0x80
43 
44 // Event Flags
45 #define INET_POLLRDNORM		0x0040
46 #define INET_POLLWRNORM		0x0004
47 
48 // TODO: Determine how many handlers we can actually have
49 const size_t MAX_APCTL_HANDLERS = 32;
50 
51 enum {
52 	// net common errors
53 	ERROR_NET_NO_SPACE						= 0x80410001,
54 	ERROR_NET_INTERNAL						= 0x80410002,
55 	ERROR_NET_INVALID_ARG					= 0x80410003,
56 	ERROR_NET_NO_ENTRY						= 0x80410004,
57 
58 	// pspnet_core
59 	ERROR_NET_CORE_NOT_TERMINATED			= 0x80410101,
60 	ERROR_NET_CORE_INTERFACE_BUSY			= 0x80410102,
61 	ERROR_NET_CORE_INVALID_ARG				= 0x80410103,
62 	ERROR_NET_CORE_THREAD_NOT_FOUND			= 0x80410104,
63 	ERROR_NET_CORE_THREAD_BUSY				= 0x80410105,
64 	ERROR_NET_CORE_80211_NO_BSS				= 0x80410106,
65 	ERROR_NET_CORE_80211_NO_AVAIL_BSS		= 0x80410107,
66 
67 	// pspnet_inet
68 	ERROR_NET_INET_ALREADY_INITIALIZED		= 0x80410201,
69 	ERROR_NET_INET_SOCKET_BUSY				= 0x80410202,
70 	ERROR_NET_INET_CONFIG_INVALID_ARG		= 0x80410203,
71 	ERROR_NET_INET_GET_IFADDR				= 0x80410204,
72 	ERROR_NET_INET_SET_IFADDR				= 0x80410205,
73 	ERROR_NET_INET_DEL_IFADDR				= 0x80410206,
74 	ERROR_NET_INET_NO_DEFAULT_ROUTE			= 0x80410207,
75 	ERROR_NET_INET_GET_ROUTE				= 0x80410208,
76 	ERROR_NET_INET_SET_ROUTE				= 0x80410209,
77 	ERROR_NET_INET_FLUSH_ROUTE				= 0x8041020a,
78 	ERROR_NET_INET_INVALID_ARG				= 0x8041020b,
79 
80 	// pspnet_poeclient
81 	ERROR_NET_POECLIENT_INIT				= 0x80410301,
82 	ERROR_NET_POECLIENT_NO_PADO				= 0x80410302,
83 	ERROR_NET_POECLIENT_NO_PADS				= 0x80410303,
84 	ERROR_NET_POECLIENT_GET_PADT			= 0x80410304,
85 	ERROR_NET_POECLIENT_SERVICE_NAME		= 0x80410305,
86 	ERROR_NET_POECLIENT_AC_SYSTEM			= 0x80410306,
87 	ERROR_NET_POECLIENT_GENERIC				= 0x80410307,
88 	ERROR_NET_POECLIENT_AUTH				= 0x80410308,
89 	ERROR_NET_POECLIENT_NETWORK				= 0x80410309,
90 	ERROR_NET_POECLIENT_TERMINATE			= 0x8041030a,
91 	ERROR_NET_POECLIENT_NOT_STARTED			= 0x8041030b,
92 
93 	// pspnet_resolver
94 	ERROR_NET_RESOLVER_NOT_TERMINATED		= 0x80410401,
95 	ERROR_NET_RESOLVER_NO_DNS_SERVER		= 0x80410402,
96 	ERROR_NET_RESOLVER_INVALID_PTR			= 0x80410403,
97 	ERROR_NET_RESOLVER_INVALID_BUFLEN		= 0x80410404,
98 	ERROR_NET_RESOLVER_INVALID_ID			= 0x80410405,
99 	ERROR_NET_RESOLVER_ID_MAX				= 0x80410406,
100 	ERROR_NET_RESOLVER_NO_MEM				= 0x80410407,
101 	ERROR_NET_RESOLVER_BAD_ID				= 0x80410408, // ERROR_NET_RESOLVER_ID_NOT_FOUND
102 	ERROR_NET_RESOLVER_CTX_BUSY				= 0x80410409,
103 	ERROR_NET_RESOLVER_ALREADY_STOPPED		= 0x8041040a,
104 	ERROR_NET_RESOLVER_NOT_SUPPORTED		= 0x8041040b,
105 	ERROR_NET_RESOLVER_BUF_NO_SPACE			= 0x8041040c,
106 	ERROR_NET_RESOLVER_INVALID_PACKET		= 0x8041040d,
107 	ERROR_NET_RESOLVER_STOPPED				= 0x8041040e,
108 	ERROR_NET_RESOLVER_SOCKET				= 0x8041040f,
109 	ERROR_NET_RESOLVER_TIMEOUT				= 0x80410410,
110 	ERROR_NET_RESOLVER_NO_RECORD			= 0x80410411,
111 	ERROR_NET_RESOLVER_RES_PACKET_FORMAT	= 0x80410412,
112 	ERROR_NET_RESOLVER_RES_SERVER_FAILURE	= 0x80410413,
113 	ERROR_NET_RESOLVER_INVALID_HOST			= 0x80410414, // ERROR_NET_RESOLVER_NO_HOST
114 	ERROR_NET_RESOLVER_RES_NOT_IMPLEMENTED	= 0x80410415,
115 	ERROR_NET_RESOLVER_RES_SERVER_REFUSED	= 0x80410416,
116 	ERROR_NET_RESOLVER_INTERNAL				= 0x80410417,
117 
118 	// pspnet_dhcp
119 	ERROR_NET_DHCP_INVALID_PACKET			= 0x80410501,
120 	ERROR_NET_DHCP_NO_SERVER				= 0x80410502,
121 	ERROR_NET_DHCP_SENT_DECLINE				= 0x80410503,
122 	ERROR_NET_DHCP_LEASE_TIME				= 0x80410504,
123 	ERROR_NET_DHCP_GET_NAK		 			= 0x80410505,
124 
125 	// pspnet_apctl
126 	ERROR_NET_APCTL_ALREADY_INITIALIZED		= 0x80410a01,
127 	ERROR_NET_APCTL_INVALID_CODE			= 0x80410a02,
128 	ERROR_NET_APCTL_INVALID_IP				= 0x80410a03,
129 	ERROR_NET_APCTL_NOT_DISCONNECTED		= 0x80410a04,
130 	ERROR_NET_APCTL_NOT_IN_BSS				= 0x80410a05,
131 	ERROR_NET_APCTL_WLAN_SWITCH_OFF			= 0x80410a06,
132 	ERROR_NET_APCTL_WLAN_BEACON_LOST		= 0x80410a07,
133 	ERROR_NET_APCTL_WLAN_DISASSOCIATION		= 0x80410a08,
134 	ERROR_NET_APCTL_INVALID_ID				= 0x80410a09,
135 	ERROR_NET_APCTL_WLAN_SUSPENDED  		= 0x80410a0a,
136 	ERROR_NET_APCTL_TIMEOUT					= 0x80410a0b,
137 
138 	// wlan errors
139 	ERROR_NET_WLAN_ALREADY_JOINED			= 0x80410d01,
140 	ERROR_NET_WLAN_TRY_JOIN					= 0x80410d02,
141 	ERROR_NET_WLAN_SCANNING					= 0x80410d03,
142 	ERROR_NET_WLAN_INVALID_PARAMETER		= 0x80410d04,
143 	ERROR_NET_WLAN_NOT_SUPPORTED			= 0x80410d05,
144 	ERROR_NET_WLAN_NOT_JOIN_BSS				= 0x80410d06,
145 	ERROR_NET_WLAN_ASSOC_TIMEOUT			= 0x80410d07,
146 	ERROR_NET_WLAN_ASSOC_REFUSED			= 0x80410d08,
147 	ERROR_NET_WLAN_ASSOC_FAIL				= 0x80410d09,
148 	ERROR_NET_WLAN_DISASSOC_FAIL			= 0x80410d0a,
149 	ERROR_NET_WLAN_JOIN_FAIL				= 0x80410d0b,
150 	ERROR_NET_WLAN_POWER_OFF				= 0x80410d0c,
151 	ERROR_NET_WLAN_INTERNAL_FAIL			= 0x80410d0d,
152 	ERROR_NET_WLAN_DEVICE_NOT_READY			= 0x80410d0e,
153 	ERROR_NET_WLAN_ALREADY_ATTACHED			= 0x80410d0f,
154 	ERROR_NET_WLAN_NOT_SET_WEP				= 0x80410d10,
155 	ERROR_NET_WLAN_TIMEOUT					= 0x80410d11,
156 	ERROR_NET_WLAN_NO_SPACE					= 0x80410d12,
157 	ERROR_NET_WLAN_INVALID_ARG				= 0x80410D13,
158 	ERROR_NET_WLAN_NOT_IN_GAMEMODE			= 0x80410d14,
159 	ERROR_NET_WLAN_LEAVE_FAIL				= 0x80410d15,
160 	ERROR_NET_WLAN_SUSPENDED				= 0x80410d16,
161 };
162 
163 enum {
164 	PSP_NET_APCTL_STATE_DISCONNECTED = 0,
165 	PSP_NET_APCTL_STATE_SCANNING = 1,
166 	PSP_NET_APCTL_STATE_JOINING = 2,
167 	PSP_NET_APCTL_STATE_GETTING_IP = 3,
168 	PSP_NET_APCTL_STATE_GOT_IP = 4,
169 	PSP_NET_APCTL_STATE_EAP_AUTH = 5,
170 	PSP_NET_APCTL_STATE_KEY_EXCHANGE = 6
171 };
172 
173 enum {
174 	PSP_NET_APCTL_EVENT_CONNECT_REQUEST		= 0,
175 	PSP_NET_APCTL_EVENT_SCAN_REQUEST		= 1,
176 	PSP_NET_APCTL_EVENT_SCAN_COMPLETE		= 2,
177 	PSP_NET_APCTL_EVENT_ESTABLISHED			= 3,
178 	PSP_NET_APCTL_EVENT_GET_IP				= 4,
179 	PSP_NET_APCTL_EVENT_DISCONNECT_REQUEST	= 5,
180 	PSP_NET_APCTL_EVENT_ERROR				= 6,
181 	PSP_NET_APCTL_EVENT_INFO				= 7,
182 	PSP_NET_APCTL_EVENT_EAP_AUTH			= 8,
183 	PSP_NET_APCTL_EVENT_KEY_EXCHANGE		= 9,
184 	PSP_NET_APCTL_EVENT_RECONNECT			= 10,
185 	PSP_NET_APCTL_EVENT_SCAN_STOP			= 11 // FIXME: not sure what this is, MGS:PW seems to check this value within ApctlHandler during Recruit, related to sceNetApctlScanSSID2 ?
186 };
187 
188 #define 	PSP_NET_APCTL_INFO_PROFILE_NAME			0
189 #define 	PSP_NET_APCTL_INFO_BSSID				1
190 #define 	PSP_NET_APCTL_INFO_SSID					2
191 #define 	PSP_NET_APCTL_INFO_SSID_LENGTH			3
192 #define 	PSP_NET_APCTL_INFO_SECURITY_TYPE		4
193 #define 	PSP_NET_APCTL_INFO_STRENGTH				5
194 #define 	PSP_NET_APCTL_INFO_CHANNEL				6
195 #define 	PSP_NET_APCTL_INFO_POWER_SAVE			7
196 #define 	PSP_NET_APCTL_INFO_IP					8
197 #define 	PSP_NET_APCTL_INFO_SUBNETMASK			9
198 #define 	PSP_NET_APCTL_INFO_GATEWAY				10
199 #define 	PSP_NET_APCTL_INFO_PRIMDNS				11
200 #define 	PSP_NET_APCTL_INFO_SECDNS				12
201 #define 	PSP_NET_APCTL_INFO_USE_PROXY			13
202 #define 	PSP_NET_APCTL_INFO_PROXY_URL			14
203 #define 	PSP_NET_APCTL_INFO_PROXY_PORT			15
204 #define 	PSP_NET_APCTL_INFO_8021_EAP_TYPE		16
205 #define 	PSP_NET_APCTL_INFO_START_BROWSER		17
206 #define 	PSP_NET_APCTL_INFO_WIFISP				18
207 
208 #define 	PSP_NET_APCTL_INFO_SECURITY_TYPE_NONE           0
209 #define 	PSP_NET_APCTL_INFO_SECURITY_TYPE_WEP            1
210 #define 	PSP_NET_APCTL_INFO_SECURITY_TYPE_WPA            2
211 #define 	PSP_NET_APCTL_INFO_SECURITY_TYPE_UNSUPPORTED    3
212 #define 	PSP_NET_APCTL_INFO_SECURITY_TYPE_WPA_AES        4
213 
214 #define 	PSP_NET_APCTL_DESC_IBSS                 0
215 #define 	PSP_NET_APCTL_DESC_SSID_NAME            1
216 #define 	PSP_NET_APCTL_DESC_SSID_NAME_LENGTH     2
217 #define 	PSP_NET_APCTL_DESC_CHANNEL              3 // FIXME: not sure what this 3 is, may be Security Type based on the order of SceNetApctlInfoInternal ?
218 #define 	PSP_NET_APCTL_DESC_SIGNAL_STRENGTH      4
219 #define 	PSP_NET_APCTL_DESC_SECURITY             5
220 
221 #ifdef _MSC_VER
222 #pragma pack(push,1)
223 #endif
224 // Sockaddr
225 typedef struct SceNetInetSockaddr {
226 	uint8_t sa_len;
227 	uint8_t sa_family;
228 	uint8_t sa_data[14];
229 } PACK SceNetInetSockaddr;
230 
231 // Sockaddr_in
232 typedef struct SceNetInetSockaddrIn {
233 	uint8_t sin_len;
234 	uint8_t sin_family;
235 	u16_le sin_port; //uint16_t
236 	u32_le sin_addr; //uint32_t
237 	uint8_t sin_zero[8];
238 } PACK SceNetInetSockaddrIn;
239 
240 // Polling Event Field
241 typedef struct SceNetInetPollfd { //similar format to pollfd in 32bit (pollfd in 64bit have different size)
242 	s32_le fd;
243 	s16_le events;
244 	s16_le revents;
245 } PACK SceNetInetPollfd;
246 
247 typedef struct ProductStruct { // Similar to SceNetAdhocctlAdhocId ?
248 	s32_le unknown; // Unknown, set to 0 // Product Type ?
249 	char product[PRODUCT_CODE_LENGTH]; // Game ID (Example: ULUS10000)
250 } PACK ProductStruct;
251 
252 #ifdef _MSC_VER
253 #pragma pack(pop)
254 #endif
255 
256 typedef void(*sceNetApctlHandler)(int oldState, int newState, int event, int error, void* pArg);
257 
258 #define APCTL_PROFILENAME_MAXLEN 64
259 #define APCTL_SSID_MAXLEN 32
260 #define APCTL_IPADDR_MAXLEN 16
261 #define APCTL_URL_MAXLEN 128
262 typedef struct SceNetApctlInfoInternal { // Using struct instead of union for internal use
263 	char 			name[APCTL_PROFILENAME_MAXLEN];
264 	u8 				bssid[ETHER_ADDR_LEN];
265 	char 			ssid[APCTL_SSID_MAXLEN];
266 	unsigned int 	ssidLength; // ssid string length (excluding null terminator)
267 	unsigned int 	securityType; // a value of PSP_NET_APCTL_INFO_SECURITY_TYPE_NONE..PSP_NET_APCTL_INFO_SECURITY_TYPE_WPA?
268 	u8			 	strength; // Signal strength in %
269 	u8			 	channel;
270 	u8			 	powerSave; // 1 on, 0 off
271 	char 			ip[APCTL_IPADDR_MAXLEN]; // PSP's IP
272 	char 			subNetMask[APCTL_IPADDR_MAXLEN];
273 	char 			gateway[APCTL_IPADDR_MAXLEN];
274 	char 			primaryDns[APCTL_IPADDR_MAXLEN];
275 	char 			secondaryDns[APCTL_IPADDR_MAXLEN];
276 	unsigned int 	useProxy; // 1 for proxy, 0 for no proxy
277 	char 			proxyUrl[APCTL_URL_MAXLEN];
278 	unsigned short 	proxyPort;
279 	unsigned int 	eapType; // 0 is none, 1 is EAP-MD5
280 	unsigned int 	startBrowser; // 1 = start browser
281 	unsigned int 	wifisp; // 1 if connection is for Wifi service providers (WISP) for sharing internet connection
282 } SceNetApctlInfoInternal;
283 
284 struct ApctlHandler {
285 	u32 entryPoint;
286 	u32 argument;
287 };
288 
289 struct ApctlArgs {
290 	u32_le data[5]; // OldState, NewState, Event, Error, ArgsAddr
291 };
292 
293 class PointerWrap;
294 
295 class AfterApctlMipsCall : public PSPAction {
296 public:
AfterApctlMipsCall()297 	AfterApctlMipsCall() {}
Create()298 	static PSPAction* Create() { return new AfterApctlMipsCall(); }
299 	void DoState(PointerWrap& p) override;
300 	void run(MipsCall& call) override;
301 	void SetData(int HandlerID, int OldState, int NewState, int Event, int Error, u32_le ArgsAddr);
302 
303 private:
304 	int handlerID = -1;
305 	int oldState = 0;
306 	int newState = 0;
307 	int event = 0;
308 	int error = 0;
309 	u32_le argsAddr = 0;
310 };
311 
312 extern bool netInetInited;
313 extern bool netApctlInited;
314 extern u32 netApctlState;
315 extern SceNetApctlInfoInternal netApctlInfo;
316 
317 template <typename I> std::string num2hex(I w, size_t hex_len = sizeof(I) << 1);
318 std::string error2str(u32 errorcode);
319 
320 void Register_sceNet();
321 void Register_sceWlanDrv();
322 void Register_sceNetUpnp();
323 void Register_sceNetIfhandle();
324 
325 
326 void __NetInit();
327 void __NetShutdown();
328 void __NetDoState(PointerWrap &p);
329 
330 int NetApctl_GetState();
331 
332 int sceNetApctlConnect(int connIndex);
333 int sceNetInetPoll(void *fds, u32 nfds, int timeout);
334 int sceNetInetTerm();
335 int sceNetApctlTerm();
336