Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to bind sockets to a local IP address #12998

Merged
merged 1 commit into from Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion rpcs3/Emu/Cell/lv2/sys_net/lv2_socket_native.cpp
Expand Up @@ -129,10 +129,18 @@ s32 lv2_socket_native::bind(const sys_net_sockaddr& addr)

const auto* psa_in = reinterpret_cast<const sys_net_sockaddr_in*>(&addr);

auto& nph = g_fxo->get<named_thread<np::np_handler>>();
u32 saddr = nph.get_bind_ip();
if (saddr == 0)
{
// If zero use the supplied address
saddr = std::bit_cast<u32>(psa_in->sin_addr);
}

::sockaddr_in native_addr{};
native_addr.sin_family = AF_INET;
native_addr.sin_port = std::bit_cast<u16>(psa_in->sin_port);
native_addr.sin_addr.s_addr = std::bit_cast<u32>(psa_in->sin_addr);
native_addr.sin_addr.s_addr = saddr;
::socklen_t native_addr_len = sizeof(native_addr);

sys_net.warning("[Native] Trying to bind %s:%d", native_addr.sin_addr, std::bit_cast<be_t<u16>, u16>(native_addr.sin_port));
Expand Down
17 changes: 17 additions & 0 deletions rpcs3/Emu/NP/np_handler.cpp
Expand Up @@ -371,6 +371,18 @@ namespace np
{
dns_ip = conv.s_addr;
}

// Convert bind address
conv = {};
if (!inet_pton(AF_INET, g_cfg.net.bind_address.to_string().c_str(), &conv))
{
// Do not set to disconnected on invalid IP just error and continue using default (0.0.0.0)
nph_log.error("Provided IP(%s) address for bind is invalid!", g_cfg.net.bind_address.to_string());
}
else
{
bind_ip = conv.s_addr;
}
}
}

Expand Down Expand Up @@ -585,6 +597,11 @@ namespace np
return dns_ip;
}

u32 np_handler::get_bind_ip() const
{
return bind_ip;
}

s32 np_handler::get_net_status() const
{
return is_connected ? CELL_NET_CTL_STATE_IPObtained : CELL_NET_CTL_STATE_Disconnected;
Expand Down
2 changes: 2 additions & 0 deletions rpcs3/Emu/NP/np_handler.h
Expand Up @@ -79,6 +79,7 @@ namespace np
u32 get_local_ip_addr() const;
u32 get_public_ip_addr() const;
u32 get_dns_ip() const;
u32 get_bind_ip() const;

s32 get_psn_status() const;
s32 get_net_status() const;
Expand Down Expand Up @@ -290,6 +291,7 @@ namespace np
be_t<u32> local_ip_addr{};
be_t<u32> public_ip_addr{};
be_t<u32> dns_ip = 0x08080808;
be_t<u32> bind_ip = 0x00000000;

// User infos
SceNpId npid{};
Expand Down
1 change: 1 addition & 0 deletions rpcs3/Emu/system_config.h
Expand Up @@ -305,6 +305,7 @@ struct cfg_root : cfg::node

cfg::_enum<np_internet_status> net_active{this, "Internet enabled", np_internet_status::disabled};
cfg::string ip_address{this, "IP address", "0.0.0.0"};
cfg::string bind_address{this, "Bind address", "0.0.0.0"};
cfg::string dns{this, "DNS address", "8.8.8.8"};
cfg::string swap_list{this, "IP swap list", ""};

Expand Down
2 changes: 2 additions & 0 deletions rpcs3/rpcs3qt/emu_settings_type.h
Expand Up @@ -170,6 +170,7 @@ enum class emu_settings_type
DNSAddress,
IpSwapList,
PSNStatus,
BindAddress,

// System
LicenseArea,
Expand Down Expand Up @@ -348,6 +349,7 @@ inline static const QMap<emu_settings_type, cfg_location> settings_location =
{ emu_settings_type::DNSAddress, { "Net", "DNS address"}},
{ emu_settings_type::IpSwapList, { "Net", "IP swap list"}},
{ emu_settings_type::PSNStatus, { "Net", "PSN status"}},
{ emu_settings_type::BindAddress, { "Net", "Bind address"}},

// System
{ emu_settings_type::LicenseArea, { "System", "License Area"}},
Expand Down
3 changes: 3 additions & 0 deletions rpcs3/rpcs3qt/settings_dialog.cpp
Expand Up @@ -1281,6 +1281,9 @@ settings_dialog::settings_dialog(std::shared_ptr<gui_settings> gui_settings, std
m_emu_settings->EnhanceLineEdit(ui->edit_dns, emu_settings_type::DNSAddress);
SubscribeTooltip(ui->gb_edit_dns, tooltips.settings.dns);

m_emu_settings->EnhanceLineEdit(ui->edit_bind, emu_settings_type::BindAddress);
SubscribeTooltip(ui->gb_edit_bind, tooltips.settings.bind);

m_emu_settings->EnhanceLineEdit(ui->edit_swaps, emu_settings_type::IpSwapList);
SubscribeTooltip(ui->gb_edit_swaps, tooltips.settings.dns_swap);

Expand Down
21 changes: 20 additions & 1 deletion rpcs3/rpcs3qt/settings_dialog.ui
Expand Up @@ -2081,7 +2081,7 @@
<property name="title">
<string>Network Configuration</string>
</property>
<layout class="QVBoxLayout" name="gb_network_status_layout" stretch="0,0,0,0">
<layout class="QVBoxLayout" name="gb_network_status_layout" stretch="0,0,0,0,0">
<item>
<widget class="QGroupBox" name="gb_netStatusBox">
<property name="title">
Expand Down Expand Up @@ -2132,6 +2132,25 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="gb_edit_bind">
<property name="title">
<string>Bind address</string>
</property>
<layout class="QVBoxLayout" name="gb_edit_bind_layout">
<item>
<widget class="QLineEdit" name="edit_bind">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="networkTabSpacerLeft">
<property name="orientation">
Expand Down
1 change: 1 addition & 0 deletions rpcs3/rpcs3qt/tooltips.h
Expand Up @@ -228,6 +228,7 @@ class Tooltips : public QObject
const QString psn_status = tr("If set to Simulated, RPCS3 will fake PSN connection as best as it can.");
const QString dns = tr("DNS used to resolve hostnames by applications.");
const QString dns_swap = tr("DNS Swap List.");
const QString bind = tr("Interface IP Address to bind to.");

// system

Expand Down