2012-11-01 16:19:01 +01:00
// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
2012-11-04 23:01:49 +01:00
// the Free Software Foundation, version 2.0 or later versions.
2012-11-01 16:19:01 +01:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
# pragma once
2020-07-27 09:51:53 +07:00
# include <StringUtils.h>
2014-08-01 01:55:42 +07:00
# include "Core/HLE/proAdhoc.h"
2020-07-28 07:38:10 +07:00
// Using constants instead of numbers for readability reason, since PSP_THREAD_ATTR_KERNEL/USER is located in sceKernelThread.cpp instead of sceKernelThread.h
# ifndef PSP_THREAD_ATTR_KERNEL
# define PSP_THREAD_ATTR_KERNEL 0x00001000
# endif
# ifndef PSP_THREAD_ATTR_USER
# define PSP_THREAD_ATTR_USER 0x80000000
# endif
2014-08-01 01:55:42 +07:00
// Option Names
# define PSP_SO_REUSEPORT 0x0200
# define PSP_SO_NBIO 0x1009
// Infrastructure Errno Numbers
# define INET_EAGAIN 0x0B
# define INET_ETIMEDOUT 0x74
# define INET_EINPROGRESS 0x77
# define INET_EISCONN 0x7F
// On-Demand Nonblocking Flag
# define INET_MSG_DONTWAIT 0x80
// Event Flags
# define INET_POLLRDNORM 0x0040
# define INET_POLLWRNORM 0x0004
// TODO: Determine how many handlers we can actually have
const size_t MAX_APCTL_HANDLERS = 32 ;
enum {
ERROR_NET_BUFFER_TOO_SMALL = 0x80400706 ,
ERROR_NET_INET_ALREADY_INITIALIZED = 0x80410201 ,
ERROR_NET_RESOLVER_BAD_ID = 0x80410408 ,
ERROR_NET_RESOLVER_ALREADY_STOPPED = 0x8041040a ,
ERROR_NET_RESOLVER_INVALID_HOST = 0x80410414 ,
ERROR_NET_APCTL_ALREADY_INITIALIZED = 0x80410a01 ,
2020-07-27 09:51:53 +07:00
ERROR_NET_APCTL_INVALID_CODE = 0x80410a02 ,
ERROR_NET_APCTL_INVALID_IP = 0x80410a03 ,
ERROR_NET_APCTL_NOT_DISCONNECTED = 0x80410a04 ,
ERROR_NET_APCTL_NOT_IN_BSS = 0x80410a05 ,
ERROR_NET_APCTL_WLAN_SWITCH_OFF = 0x80410a06 ,
ERROR_NET_APCTL_WLAN_BEACON_LOST = 0x80410a07 ,
ERROR_NET_APCTL_WLAN_DISASSOCIATION = 0x80410a08 ,
ERROR_NET_APCTL_INVALID_ID = 0x80410a09 ,
ERROR_NET_APCTL_WLAN_SUSPENDED = 0x80410a0a ,
ERROR_NET_APCTL_TIMEOUT = 0x80410a0b ,
2014-08-01 01:55:42 +07:00
} ;
enum {
2020-07-27 09:51:53 +07:00
PSP_NET_APCTL_STATE_DISCONNECTED = 0 ,
PSP_NET_APCTL_STATE_SCANNING = 1 ,
PSP_NET_APCTL_STATE_JOINING = 2 ,
PSP_NET_APCTL_STATE_GETTING_IP = 3 ,
PSP_NET_APCTL_STATE_GOT_IP = 4 ,
PSP_NET_APCTL_STATE_EAP_AUTH = 5 ,
PSP_NET_APCTL_STATE_KEY_EXCHANGE = 6
} ;
enum {
PSP_NET_APCTL_EVENT_CONNECT_REQUEST = 0 ,
PSP_NET_APCTL_EVENT_SCAN_REQUEST = 1 ,
PSP_NET_APCTL_EVENT_SCAN_COMPLETE = 2 ,
PSP_NET_APCTL_EVENT_ESTABLISHED = 3 ,
PSP_NET_APCTL_EVENT_GET_IP = 4 ,
2014-08-01 01:55:42 +07:00
PSP_NET_APCTL_EVENT_DISCONNECT_REQUEST = 5 ,
2020-07-27 09:51:53 +07:00
PSP_NET_APCTL_EVENT_ERROR = 6 ,
PSP_NET_APCTL_EVENT_INFO = 7 ,
PSP_NET_APCTL_EVENT_EAP_AUTH = 8 ,
PSP_NET_APCTL_EVENT_KEY_EXCHANGE = 9 ,
PSP_NET_APCTL_EVENT_RECONNECT = 10
2014-08-01 01:55:42 +07:00
} ;
2020-07-27 09:51:53 +07:00
# define PSP_NET_APCTL_INFO_PROFILE_NAME 0
# define PSP_NET_APCTL_INFO_BSSID 1
# define PSP_NET_APCTL_INFO_SSID 2
# define PSP_NET_APCTL_INFO_SSID_LENGTH 3
# define PSP_NET_APCTL_INFO_SECURITY_TYPE 4
# define PSP_NET_APCTL_INFO_STRENGTH 5
# define PSP_NET_APCTL_INFO_CHANNEL 6
# define PSP_NET_APCTL_INFO_POWER_SAVE 7
# define PSP_NET_APCTL_INFO_IP 8
# define PSP_NET_APCTL_INFO_SUBNETMASK 9
# define PSP_NET_APCTL_INFO_GATEWAY 10
# define PSP_NET_APCTL_INFO_PRIMDNS 11
# define PSP_NET_APCTL_INFO_SECDNS 12
# define PSP_NET_APCTL_INFO_USE_PROXY 13
# define PSP_NET_APCTL_INFO_PROXY_URL 14
# define PSP_NET_APCTL_INFO_PROXY_PORT 15
# define PSP_NET_APCTL_INFO_8021_EAP_TYPE 16
# define PSP_NET_APCTL_INFO_START_BROWSER 17
# define PSP_NET_APCTL_INFO_WIFISP 18
# define PSP_NET_APCTL_INFO_SECURITY_TYPE_NONE 0
# define PSP_NET_APCTL_INFO_SECURITY_TYPE_WEP 1
# define PSP_NET_APCTL_INFO_SECURITY_TYPE_WPA 2
# define PSP_NET_APCTL_INFO_SECURITY_TYPE_UNSUPPORTED 3
# define PSP_NET_APCTL_INFO_SECURITY_TYPE_WPA_AES 4
# define PSP_NET_APCTL_DESC_IBSS 0
# define PSP_NET_APCTL_DESC_SSID_NAME 1
# define PSP_NET_APCTL_DESC_SSID_NAME_LENGTH 2
# define PSP_NET_APCTL_DESC_SIGNAL_STRENGTH 4
# define PSP_NET_APCTL_DESC_SECURITY 5
2019-12-25 22:30:39 +07:00
# ifdef _MSC_VER
# pragma pack(push,1)
# endif
2014-08-01 01:55:42 +07:00
// Sockaddr
typedef struct SceNetInetSockaddr {
uint8_t sa_len ;
uint8_t sa_family ;
uint8_t sa_data [ 14 ] ;
2019-12-24 00:04:03 +07:00
} PACK SceNetInetSockaddr ;
2014-08-01 01:55:42 +07:00
// Sockaddr_in
typedef struct SceNetInetSockaddrIn {
uint8_t sin_len ;
uint8_t sin_family ;
u16_le sin_port ; //uint16_t
u32_le sin_addr ; //uint32_t
uint8_t sin_zero [ 8 ] ;
2019-12-24 00:04:03 +07:00
} PACK SceNetInetSockaddrIn ;
2014-08-01 01:55:42 +07:00
// Polling Event Field
typedef struct SceNetInetPollfd { //similar format to pollfd in 32bit (pollfd in 64bit have different size)
s32_le fd ;
s16_le events ;
s16_le revents ;
2019-12-24 00:04:03 +07:00
} PACK SceNetInetPollfd ;
2014-08-01 01:55:42 +07:00
2019-12-24 00:04:03 +07:00
typedef struct ProductStruct { // Similar to SceNetAdhocctlAdhocId ?
2014-08-01 01:55:42 +07:00
s32_le unknown ; // Unknown, set to 0 // Product Type ?
char product [ PRODUCT_CODE_LENGTH ] ; // Game ID (Example: ULUS10000)
2019-12-24 00:04:03 +07:00
} PACK ProductStruct ;
2020-07-27 09:51:53 +07:00
2019-12-25 22:30:39 +07:00
# ifdef _MSC_VER
# pragma pack(pop)
# endif
2014-08-01 01:55:42 +07:00
2020-07-27 09:51:53 +07:00
typedef void ( * sceNetApctlHandler ) ( int oldState , int newState , int event , int error , void * pArg ) ;
# define APCTL_PROFILENAME_MAXLEN 64
# define APCTL_SSID_MAXLEN 32
# define APCTL_IPADDR_MAXLEN 16
# define APCTL_URL_MAXLEN 128
typedef struct SceNetApctlInfoInternal { // Using struct instead of union for internal use
char name [ APCTL_PROFILENAME_MAXLEN ] ;
u8 bssid [ ETHER_ADDR_LEN ] ;
char ssid [ APCTL_SSID_MAXLEN ] ;
unsigned int ssidLength ; // ssid string length (excluding null terminator)
unsigned int securityType ; // a value of PSP_NET_APCTL_INFO_SECURITY_TYPE_NONE..PSP_NET_APCTL_INFO_SECURITY_TYPE_WPA?
u8 strength ; // Signal strength in %
u8 channel ;
u8 powerSave ; // 1 on, 0 off
char ip [ APCTL_IPADDR_MAXLEN ] ; // PSP's IP
char subNetMask [ APCTL_IPADDR_MAXLEN ] ;
char gateway [ APCTL_IPADDR_MAXLEN ] ;
char primaryDns [ APCTL_IPADDR_MAXLEN ] ;
char secondaryDns [ APCTL_IPADDR_MAXLEN ] ;
unsigned int useProxy ; // 1 for proxy, 0 for no proxy
char proxyUrl [ APCTL_URL_MAXLEN ] ;
unsigned short proxyPort ;
unsigned int eapType ; // 0 is none, 1 is EAP-MD5
unsigned int startBrowser ; // 1 = start browser
unsigned int wifisp ; // 1 if connection is for Wifi service providers (WISP) for sharing internet connection
} SceNetApctlInfoInternal ;
2014-08-01 01:55:42 +07:00
struct ApctlHandler {
u32 entryPoint ;
u32 argument ;
} ;
2020-07-28 07:38:10 +07:00
struct ApctlArgs {
u32_le data [ 5 ] ; // OldState, NewState, Event, Error, ArgsAddr
} ;
2014-03-15 11:32:57 -07:00
class PointerWrap ;
2013-03-24 22:41:42 +01:00
2020-07-28 07:38:10 +07:00
class AfterApctlMipsCall : public PSPAction {
public :
AfterApctlMipsCall ( ) { }
static PSPAction * Create ( ) { return new AfterApctlMipsCall ( ) ; }
void DoState ( PointerWrap & p ) override {
auto s = p . Section ( " AfterApctlMipsCall " , 1 , 1 ) ;
if ( ! s )
return ;
2020-08-02 12:57:27 +07:00
// Just in case there are "s" corruption in the future where s.ver is a negative number
2020-07-28 07:38:10 +07:00
if ( s > = 1 ) {
p . Do ( handlerID ) ;
p . Do ( oldState ) ;
p . Do ( newState ) ;
p . Do ( event ) ;
p . Do ( error ) ;
p . Do ( argsAddr ) ;
}
2020-08-02 12:57:27 +07:00
else {
handlerID = - 1 ;
oldState = 0 ;
newState = 0 ;
event = 0 ;
error = 0 ;
argsAddr = 0 ;
}
2020-07-28 07:38:10 +07:00
}
void run ( MipsCall & call ) override ;
void SetData ( int HandlerID , int OldState , int NewState , int Event , int Error , u32_le ArgsAddr ) ;
private :
int handlerID = - 1 ;
int oldState = 0 ;
int newState = 0 ;
int event = 0 ;
int error = 0 ;
u32_le argsAddr = 0 ;
} ;
2019-12-24 00:04:03 +07:00
extern bool netInetInited ;
extern bool netApctlInited ;
2020-07-27 09:51:53 +07:00
template < typename I > std : : string num2hex ( I w , size_t hex_len = sizeof ( I ) < < 1 ) ;
std : : string error2str ( u32 errorcode ) ;
2019-12-24 00:04:03 +07:00
2012-11-01 16:19:01 +01:00
void Register_sceNet ( ) ;
void Register_sceWlanDrv ( ) ;
2015-03-08 20:07:03 +08:00
void Register_sceNetUpnp ( ) ;
2016-02-13 20:27:07 +08:00
void Register_sceNetIfhandle ( ) ;
2013-03-24 22:41:42 +01:00
void __NetInit ( ) ;
void __NetShutdown ( ) ;
void __NetDoState ( PointerWrap & p ) ;
2014-08-01 01:55:42 +07:00
2019-12-24 00:04:03 +07:00
int sceNetInetPoll ( void * fds , u32 nfds , int timeout ) ;
int sceNetInetTerm ( ) ;
int sceNetApctlTerm ( ) ;
static u32 sceNetTerm ( ) ;