2021-01-02 23:22:45 +00:00
# include "net_page.hpp"
2021-09-11 14:48:13 +01:00
2021-01-02 23:22:45 +00:00
# include <arpa/inet.h>
2021-02-10 16:28:47 +00:00
# include <switch.h>
2021-09-11 14:48:13 +01:00
2021-02-10 16:28:47 +00:00
# include <filesystem>
2021-09-11 14:48:13 +01:00
# include <fstream>
2021-02-10 16:28:47 +00:00
# include <iomanip>
2022-05-02 00:04:05 +01:00
# include <thread>
2021-03-16 14:56:46 +00:00
# include <json.hpp>
2021-09-11 14:48:13 +01:00
2021-03-16 02:04:21 +00:00
# include "constants.hpp"
2021-03-16 14:56:46 +00:00
# include "fs.hpp"
2021-09-11 14:48:13 +01:00
# include "main_frame.hpp"
2021-01-02 23:22:45 +00:00
namespace i18n = brls : : i18n ;
using namespace i18n : : literals ;
2021-09-11 13:43:14 +01:00
using json = nlohmann : : ordered_json ;
2021-01-02 23:22:45 +00:00
NetPage : : NetPage ( ) : AppletFrame ( true , true )
{
2021-03-10 20:54:17 +00:00
this - > setTitle ( " menus/net/title " _i18n ) ;
2021-09-11 13:43:14 +01:00
list = new brls : : List ( ) ;
2021-01-02 23:22:45 +00:00
nifmInitialize ( NifmServiceType_User ) ;
NifmNetworkProfileData profile ;
2021-09-11 14:48:13 +01:00
nifmGetCurrentNetworkProfile ( & profile ) ;
2021-01-02 23:22:45 +00:00
nifmExit ( ) ;
2021-03-16 14:56:46 +00:00
int uuid = std : : accumulate ( profile . uuid . uuid , profile . uuid . uuid + 16 , 0 ) ;
2021-01-02 23:22:45 +00:00
2021-09-11 13:43:14 +01:00
std : : string labelText ;
2021-09-11 14:48:13 +01:00
if ( ! uuid | | ! profile . ip_setting_data . mtu ) {
2021-09-11 13:43:14 +01:00
labelText = " Please connect to internet to use this feature. " ;
label = new brls : : Label ( brls : : LabelStyle : : DESCRIPTION , labelText , true ) ;
list - > addView ( label ) ;
cancel = new brls : : ListItem ( " menus/common/go_back " _i18n ) ;
2021-10-01 18:22:52 +01:00
cancel - > getClickEvent ( ) - > subscribe ( [ ] ( brls : : View * view ) { brls : : Application : : pushView ( new MainFrame ( ) ) ; } ) ;
2021-09-11 13:43:14 +01:00
list - > addView ( cancel ) ;
}
2021-09-11 14:48:13 +01:00
else {
if ( profile . ip_setting_data . ip_address_setting . is_automatic ) {
2021-09-11 13:43:14 +01:00
labelText = " IP Adress: Automatic " ;
2021-01-03 13:14:35 +00:00
}
2021-09-11 14:48:13 +01:00
else {
labelText = fmt : : format (
" IP Adress: {} \n Subnet Mask: {} \n Gateway: {} " ,
2021-09-11 13:43:14 +01:00
ipToString ( profile . ip_setting_data . ip_address_setting . current_addr . addr ) ,
ipToString ( profile . ip_setting_data . ip_address_setting . subnet_mask . addr ) ,
2021-09-11 14:48:13 +01:00
ipToString ( profile . ip_setting_data . ip_address_setting . gateway . addr ) ) ;
2021-01-03 13:14:35 +00:00
}
2021-09-11 14:48:13 +01:00
labelText = fmt : : format ( " {} \n Local IP addr: {} \n MTU: {} " , labelText , std : : string ( inet_ntoa ( { ( in_addr_t ) gethostid ( ) } ) ) , std : : to_string ( unsigned ( profile . ip_setting_data . mtu ) ) ) ;
2021-01-02 23:22:45 +00:00
2021-09-11 14:48:13 +01:00
if ( profile . ip_setting_data . dns_setting . is_automatic ) {
2021-09-11 13:43:14 +01:00
labelText = fmt : : format ( " {} \n DNS: Automatic " , labelText ) ;
2021-01-02 23:22:45 +00:00
}
2021-09-11 14:48:13 +01:00
else {
labelText = fmt : : format (
" {} \n Primary DNS: {} \n Secondary DNS: {} " ,
2021-09-11 13:43:14 +01:00
labelText ,
ipToString ( profile . ip_setting_data . dns_setting . primary_dns_server . addr ) ,
2021-09-11 14:48:13 +01:00
ipToString ( profile . ip_setting_data . dns_setting . secondary_dns_server . addr ) ) ;
2021-01-02 23:22:45 +00:00
}
2021-09-11 13:43:14 +01:00
label = new brls : : Label ( brls : : LabelStyle : : DESCRIPTION , labelText , true ) ;
list - > addView ( label ) ;
//ip_addr
//subnet_mask
//gateway
//dns1
//dns2
//mtu
//ip_auto
//dns_auto
json profiles = fs : : parseJsonFile ( INTERNET_JSON ) ;
2021-09-11 14:48:13 +01:00
if ( profiles . empty ( ) ) {
2021-09-11 13:43:14 +01:00
profiles = json : : array ( ) ;
}
2021-01-02 23:22:45 +00:00
2021-09-11 14:48:13 +01:00
profiles . push_back (
json : : object ( { { " name " , " lan-play " } ,
{ " ip_addr " , fmt : : format ( " 10.13.{}.{} " , std : : rand ( ) % 256 , std : : rand ( ) % 253 + 2 ) } ,
{ " subnet_mask " , " 255.255.0.0 " } ,
{ " gateway " , " 10.13.37.1 " } } ) ) ;
profiles . push_back (
json : : object ( { { " name " , " Automatic IP Address " } ,
{ " ip_auto " , true } } ) ) ;
profiles . push_back (
json : : object ( { { " name " , " Automatic DNS " } ,
{ " dns_auto " , true } } ) ) ;
profiles . push_back (
json : : object ( { { " name " , " 90DNS (Europe) " } ,
{ " dns1 " , " 163.172.141.219 " } ,
{ " dns2 " , " 207.246.121.77 " } } ) ) ;
profiles . push_back (
json : : object ( { { " name " , " 90DNS (USA) " } ,
{ " dns1 " , " 207.246.121.77 " } ,
{ " dns2 " , " 163.172.141.219 " } } ) ) ;
profiles . push_back (
json : : object ( { { " name " , " Google DNS " } ,
{ " dns1 " , " 8.8.8.8 " } ,
{ " dns2 " , " 8.8.4.4 " } } ) ) ;
profiles . push_back (
json : : object ( { { " name " , " ACNH mtu " } ,
{ " mtu " , 1500 } } ) ) ;
for ( const auto & p : profiles . items ( ) ) {
2021-09-11 13:43:14 +01:00
json values = p . value ( ) ;
2021-09-11 14:48:13 +01:00
if ( values . find ( " name " ) ! = values . end ( ) )
listItem = new brls : : ListItem ( values [ " name " ] ) ;
else
listItem = new brls : : ListItem ( " Unnamed " ) ;
2021-10-03 18:54:09 +01:00
listItem - > getClickEvent ( ) - > subscribe ( [ this , values ] ( brls : : View * view ) {
2021-09-11 13:43:14 +01:00
brls : : Dialog * dialog = new brls : : Dialog ( values . dump ( 0 ) . substr ( 1 , values . dump ( 0 ) . size ( ) - 2 ) ) ;
2021-10-03 18:54:09 +01:00
brls : : GenericEvent : : Callback callbackOk = [ this , dialog , values ] ( brls : : View * view ) {
2021-09-11 13:43:14 +01:00
nifmInitialize ( NifmServiceType_Admin ) ;
NifmNetworkProfileData profile ;
2021-09-11 14:48:13 +01:00
nifmGetCurrentNetworkProfile ( & profile ) ;
2021-09-11 13:43:14 +01:00
unsigned char buf [ sizeof ( struct in6_addr ) ] ;
2021-09-11 14:48:13 +01:00
if ( values . find ( " ip_addr " ) ! = values . end ( ) ) {
if ( inet_pton ( AF_INET , std : : string ( values [ " ip_addr " ] ) . c_str ( ) , buf ) ) {
2021-09-11 13:43:14 +01:00
profile . ip_setting_data . ip_address_setting . is_automatic = u8 ( 0 ) ;
stringToIp ( std : : string ( values [ " ip_addr " ] ) , profile . ip_setting_data . ip_address_setting . current_addr . addr ) ;
}
2021-01-02 23:22:45 +00:00
}
2021-09-11 14:48:13 +01:00
if ( values . find ( " subnet_mask " ) ! = values . end ( ) ) {
if ( inet_pton ( AF_INET , std : : string ( values [ " subnet_mask " ] ) . c_str ( ) , buf ) ) {
2021-09-11 13:43:14 +01:00
stringToIp ( std : : string ( values [ " subnet_mask " ] ) , profile . ip_setting_data . ip_address_setting . subnet_mask . addr ) ;
}
2021-01-02 23:22:45 +00:00
}
2021-09-11 14:48:13 +01:00
if ( values . find ( " gateway " ) ! = values . end ( ) ) {
if ( inet_pton ( AF_INET , std : : string ( values [ " gateway " ] ) . c_str ( ) , buf ) ) {
2021-09-11 13:43:14 +01:00
stringToIp ( std : : string ( values [ " gateway " ] ) , profile . ip_setting_data . ip_address_setting . gateway . addr ) ;
}
2021-01-02 23:22:45 +00:00
}
2021-09-11 14:48:13 +01:00
if ( values . find ( " dns1 " ) ! = values . end ( ) ) {
if ( inet_pton ( AF_INET , std : : string ( values [ " dns1 " ] ) . c_str ( ) , buf ) ) {
2021-09-11 13:43:14 +01:00
profile . ip_setting_data . dns_setting . is_automatic = u8 ( 0 ) ;
stringToIp ( std : : string ( values [ " dns1 " ] ) , profile . ip_setting_data . dns_setting . primary_dns_server . addr ) ;
}
2021-01-02 23:22:45 +00:00
}
2021-09-11 14:48:13 +01:00
if ( values . find ( " dns2 " ) ! = values . end ( ) ) {
if ( inet_pton ( AF_INET , std : : string ( values [ " dns2 " ] ) . c_str ( ) , buf ) ) {
2021-09-11 13:43:14 +01:00
profile . ip_setting_data . dns_setting . is_automatic = u8 ( 0 ) ;
stringToIp ( std : : string ( values [ " dns2 " ] ) , profile . ip_setting_data . dns_setting . secondary_dns_server . addr ) ;
}
2021-01-02 23:22:45 +00:00
}
2021-09-11 14:48:13 +01:00
if ( values . find ( " mtu " ) ! = values . end ( ) ) {
2021-09-11 13:43:14 +01:00
profile . ip_setting_data . mtu = u16 ( values [ " mtu " ] ) ;
}
2021-09-11 14:48:13 +01:00
if ( values . find ( " ip_auto " ) ! = values . end ( ) ) {
2021-09-11 13:43:14 +01:00
profile . ip_setting_data . ip_address_setting . is_automatic = u8 ( values [ " ip_auto " ] ) ;
}
2021-09-11 14:48:13 +01:00
if ( values . find ( " dns_auto " ) ! = values . end ( ) ) {
2021-09-11 13:43:14 +01:00
profile . ip_setting_data . dns_setting . is_automatic = u8 ( values [ " dns_auto " ] ) ;
}
nifmSetNetworkProfile ( & profile , & profile . uuid ) ;
nifmSetWirelessCommunicationEnabled ( true ) ;
nifmExit ( ) ;
2022-05-02 00:04:05 +01:00
std : : this_thread : : sleep_for ( std : : chrono : : microseconds ( 2500000 ) ) ; //Wait to avoid crashes when leaving too fast
2021-09-11 13:43:14 +01:00
dialog - > close ( ) ;
} ;
brls : : GenericEvent : : Callback callbackNo = [ dialog ] ( brls : : View * view ) {
dialog - > close ( ) ;
} ;
dialog - > addButton ( " menus/common/confirm " _i18n , callbackOk ) ;
dialog - > addButton ( " menus/common/cancel " _i18n , callbackNo ) ;
dialog - > setCancelable ( false ) ;
dialog - > open ( ) ;
} ) ;
list - > addView ( listItem ) ;
}
2021-01-02 23:22:45 +00:00
}
this - > setContentView ( list ) ;
}
2021-09-11 14:48:13 +01:00
std : : string NetPage : : ipToString ( u8 * ip )
{
2021-01-02 23:22:45 +00:00
std : : string res = " " ;
2021-09-11 14:48:13 +01:00
for ( size_t i = 0 ; i < 3 ; i + + ) {
2021-01-02 23:22:45 +00:00
res + = std : : to_string ( unsigned ( ip [ i ] ) ) ;
res + = " . " ;
}
res + = std : : to_string ( unsigned ( ip [ 4 ] ) ) ;
return res ;
}
2021-09-11 14:48:13 +01:00
int NetPage : : stringToIp ( const std : : string & ip , u8 * out )
{
2021-01-02 23:22:45 +00:00
size_t start ;
size_t end = 0 ;
int i = 0 ;
2021-09-11 14:48:13 +01:00
while ( ( start = ip . find_first_not_of ( " . " , end ) ) ! = std : : string : : npos ) {
2021-01-02 23:22:45 +00:00
end = ip . find ( " . " , start ) ;
out [ i ] = u8 ( std : : stoi ( ip . substr ( start , end - start ) ) ) ;
i + + ;
}
return 0 ;
}