diff --git a/DS4Windows/DS4Control/ControlService.cs b/DS4Windows/DS4Control/ControlService.cs index fb9405e..8cd04d8 100644 --- a/DS4Windows/DS4Control/ControlService.cs +++ b/DS4Windows/DS4Control/ControlService.cs @@ -212,15 +212,16 @@ namespace DS4Windows Task.Run(() => { var UDP_SERVER_PORT = Global.getUDPServerPortNum(); + var UDP_SERVER_LISTEN_ADDRESS = Global.getUDPServerListenAddress(); try { - _udpServer.Start(UDP_SERVER_PORT); - LogDebug("UDP server listening on port " + UDP_SERVER_PORT); + _udpServer.Start(UDP_SERVER_PORT, UDP_SERVER_LISTEN_ADDRESS); + LogDebug($"UDP server listening on address {UDP_SERVER_LISTEN_ADDRESS} port {UDP_SERVER_PORT}"); } catch (System.Net.Sockets.SocketException ex) { - var errMsg = String.Format("Couldn't start UDP server on port {0}, outside applications won't be able to access pad data ({1})", UDP_SERVER_PORT, ex.SocketErrorCode); + var errMsg = String.Format("Couldn't start UDP server on address {0}:{1}, outside applications won't be able to access pad data ({2})", UDP_SERVER_LISTEN_ADDRESS, UDP_SERVER_PORT, ex.SocketErrorCode); LogDebug(errMsg, true); AppLogger.LogToTray(errMsg, true, true); @@ -286,9 +287,11 @@ namespace DS4Windows await Task.Delay(100); var UDP_SERVER_PORT = Global.getUDPServerPortNum(); + var UDP_SERVER_LISTEN_ADDRESS = Global.getUDPServerListenAddress(); + try { - _udpServer.Start(UDP_SERVER_PORT); + _udpServer.Start(UDP_SERVER_PORT, UDP_SERVER_LISTEN_ADDRESS); foreach (DS4Device dev in devices) { dev.queueEvent(() => @@ -296,11 +299,11 @@ namespace DS4Windows dev.Report += dev.MotionEvent; }); } - LogDebug("UDP server listening on port " + UDP_SERVER_PORT); + LogDebug($"UDP server listening on address {UDP_SERVER_LISTEN_ADDRESS} port {UDP_SERVER_PORT}"); } catch (System.Net.Sockets.SocketException ex) { - var errMsg = String.Format("Couldn't start UDP server on port {0}, outside applications won't be able to access pad data ({1})", UDP_SERVER_PORT, ex.SocketErrorCode); + var errMsg = String.Format("Couldn't start UDP server on address {0}:{1}, outside applications won't be able to access pad data ({2})", UDP_SERVER_LISTEN_ADDRESS, UDP_SERVER_PORT, ex.SocketErrorCode); LogDebug(errMsg, true); AppLogger.LogToTray(errMsg, true, true); @@ -517,15 +520,16 @@ namespace DS4Windows { //var UDP_SERVER_PORT = 26760; var UDP_SERVER_PORT = Global.getUDPServerPortNum(); + var UDP_SERVER_LISTEN_ADDRESS = Global.getUDPServerListenAddress(); try { - _udpServer.Start(UDP_SERVER_PORT); - LogDebug("UDP server listening on port " + UDP_SERVER_PORT); + _udpServer.Start(UDP_SERVER_PORT, UDP_SERVER_LISTEN_ADDRESS); + LogDebug($"UDP server listening on address {UDP_SERVER_LISTEN_ADDRESS} port {UDP_SERVER_PORT}"); } catch (System.Net.Sockets.SocketException ex) { - var errMsg = String.Format("Couldn't start UDP server on port {0}, outside applications won't be able to access pad data ({1})", UDP_SERVER_PORT, ex.SocketErrorCode); + var errMsg = String.Format("Couldn't start UDP server on address {0}:{1}, outside applications won't be able to access pad data ({2})", UDP_SERVER_LISTEN_ADDRESS, UDP_SERVER_PORT, ex.SocketErrorCode); LogDebug(errMsg, true); AppLogger.LogToTray(errMsg, true, true); diff --git a/DS4Windows/DS4Control/ScpUtil.cs b/DS4Windows/DS4Control/ScpUtil.cs index cc76496..86ec00e 100644 --- a/DS4Windows/DS4Control/ScpUtil.cs +++ b/DS4Windows/DS4Control/ScpUtil.cs @@ -709,6 +709,15 @@ namespace DS4Windows m_Config.udpServPort = value; } + public static string getUDPServerListenAddress() + { + return m_Config.udpServListenAddress; + } + public static void setUDPServerListenAddress(string value) + { + m_Config.udpServListenAddress = value.Trim(); + } + public static bool UseWhiteIcon { set { m_Config.useWhiteIcon = value; } @@ -1721,6 +1730,7 @@ namespace DS4Windows public int flashWhenLateAt = 20; public bool useUDPServ = false; public int udpServPort = 26760; + public string udpServListenAddress = "127.0.0.1"; // 127.0.0.1=IPAddress.Loopback (default), 0.0.0.0=IPAddress.Any as all interfaces, x.x.x.x = Specific ipv4 interface address or hostname public bool useCustomSteamFolder; public string customSteamFolder; // Cache whether profile has custom action @@ -3385,6 +3395,8 @@ namespace DS4Windows catch { missingSetting = true; } try { Item = m_Xdoc.SelectSingleNode("/Profile/UDPServerPort"); int temp; int.TryParse(Item.InnerText, out temp); udpServPort = Math.Min(Math.Max(temp, 1024), 65535); } catch { missingSetting = true; } + try { Item = m_Xdoc.SelectSingleNode("/Profile/UDPServerListenAddress"); udpServListenAddress = Item.InnerText; } + catch { missingSetting = true; } try { Item = m_Xdoc.SelectSingleNode("/Profile/UseCustomSteamFolder"); Boolean.TryParse(Item.InnerText, out useCustomSteamFolder); } catch { missingSetting = true; } try { Item = m_Xdoc.SelectSingleNode("/Profile/CustomSteamFolder"); customSteamFolder = Item.InnerText; } @@ -3458,6 +3470,7 @@ namespace DS4Windows XmlNode xmlWhiteIcon = m_Xdoc.CreateNode(XmlNodeType.Element, "WhiteIcon", null); xmlWhiteIcon.InnerText = useWhiteIcon.ToString(); Node.AppendChild(xmlWhiteIcon); XmlNode xmlUseUDPServ = m_Xdoc.CreateNode(XmlNodeType.Element, "UseUDPServer", null); xmlUseUDPServ.InnerText = useUDPServ.ToString(); Node.AppendChild(xmlUseUDPServ); XmlNode xmlUDPServPort = m_Xdoc.CreateNode(XmlNodeType.Element, "UDPServerPort", null); xmlUDPServPort.InnerText = udpServPort.ToString(); Node.AppendChild(xmlUDPServPort); + XmlNode xmlUDPServListenAddress = m_Xdoc.CreateNode(XmlNodeType.Element, "UDPServerListenAddress", null); xmlUDPServListenAddress.InnerText = udpServListenAddress; Node.AppendChild(xmlUDPServListenAddress); XmlNode xmlUseCustomSteamFolder = m_Xdoc.CreateNode(XmlNodeType.Element, "UseCustomSteamFolder", null); xmlUseCustomSteamFolder.InnerText = useCustomSteamFolder.ToString(); Node.AppendChild(xmlUseCustomSteamFolder); XmlNode xmlCustomSteamFolder = m_Xdoc.CreateNode(XmlNodeType.Element, "CustomSteamFolder", null); xmlCustomSteamFolder.InnerText = customSteamFolder; Node.AppendChild(xmlCustomSteamFolder); diff --git a/DS4Windows/DS4Control/UdpServer.cs b/DS4Windows/DS4Control/UdpServer.cs index 4a8ef4c..90a4fa4 100644 --- a/DS4Windows/DS4Control/UdpServer.cs +++ b/DS4Windows/DS4Control/UdpServer.cs @@ -396,7 +396,7 @@ namespace DS4Windows } } - public void Start(int port) + public void Start(int port, string listenAddress = "") { if (running) { @@ -409,7 +409,35 @@ namespace DS4Windows } udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - try { udpSock.Bind(new IPEndPoint(IPAddress.Loopback, port)); } + try + { + IPAddress udpListenIPAddress; + if (listenAddress == "127.0.0.1" || listenAddress == "") + { + // Listen on local looback interface (default option). Does not allow remote client connections + udpListenIPAddress = IPAddress.Loopback; + } + else if (listenAddress == "0.0.0.0") + { + // Listen on all IPV4 interfaces. + // Remote client connections allowed. If the local network is not "safe" then may not be a good idea, because at the moment incoming connections are not authenticated in any way + udpListenIPAddress = IPAddress.Any; + } + else + { + // Listen on a specific hostname or IPV4 interface address. If the hostname has multiple interfaces then use the first IPV4 address because it is usually the primary IP addr. + // Remote client connections allowed. + IPAddress[] ipAddresses = Dns.GetHostAddresses(listenAddress); + udpListenIPAddress = null; + foreach (IPAddress ip4 in ipAddresses.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)) + { + udpListenIPAddress = ip4; + break; + } + if (udpListenIPAddress == null) throw new SocketException(10049 /*WSAEADDRNOTAVAIL*/); + } + udpSock.Bind(new IPEndPoint(udpListenIPAddress, port)); + } catch (SocketException ex) { udpSock.Close(); diff --git a/DS4Windows/DS4Forms/DS4Form.Designer.cs b/DS4Windows/DS4Forms/DS4Form.Designer.cs index 18ccdb1..f843f7a 100644 --- a/DS4Windows/DS4Forms/DS4Form.Designer.cs +++ b/DS4Windows/DS4Forms/DS4Form.Designer.cs @@ -154,6 +154,7 @@ this.label2 = new System.Windows.Forms.Label(); this.ckUdpServ = new System.Windows.Forms.CheckBox(); this.nUDUdpPortNum = new System.Windows.Forms.NumericUpDown(); + this.tBUdpListenAddress = new System.Windows.Forms.TextBox(); this.languagePackComboBox1 = new DS4Windows.DS4Forms.LanguagePackComboBox(); this.cBCustomSteam = new System.Windows.Forms.CheckBox(); this.tBSteamFolder = new System.Windows.Forms.TextBox(); @@ -1171,6 +1172,7 @@ this.panel4.Controls.Add(this.label2); this.panel4.Controls.Add(this.ckUdpServ); this.panel4.Controls.Add(this.nUDUdpPortNum); + this.panel4.Controls.Add(this.tBUdpListenAddress); resources.ApplyResources(this.panel4, "panel4"); this.panel4.Name = "panel4"; // @@ -1204,6 +1206,12 @@ 0, 0, 0}); + // + // tBUdpListenAddress + // + resources.ApplyResources(this.tBUdpListenAddress, "tBUdpListenAddress"); + this.tBUdpListenAddress.Name = "tBUdpListenAddress"; + this.tBUdpListenAddress.TextChanged += new System.EventHandler(this.tBUdpListenAddress_TextChanged); // // languagePackComboBox1 // @@ -1520,6 +1528,7 @@ private System.Windows.Forms.Label label2; private System.Windows.Forms.CheckBox ckUdpServ; private System.Windows.Forms.NumericUpDown nUDUdpPortNum; + private System.Windows.Forms.TextBox tBUdpListenAddress; private System.Windows.Forms.CheckBox cBCustomSteam; private System.Windows.Forms.TextBox tBSteamFolder; //private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2; diff --git a/DS4Windows/DS4Forms/DS4Form.cs b/DS4Windows/DS4Forms/DS4Form.cs index aaef1c7..d056bd1 100644 --- a/DS4Windows/DS4Forms/DS4Form.cs +++ b/DS4Windows/DS4Forms/DS4Form.cs @@ -328,12 +328,14 @@ namespace DS4Windows.Forms StartWindowsCheckBox.CheckedChanged += new EventHandler(StartWindowsCheckBox_CheckedChanged); new ToolTip().SetToolTip(StartWindowsCheckBox, Properties.Resources.RunAtStartup); - ckUdpServ.Checked = nUDUdpPortNum.Enabled = isUsingUDPServer(); + ckUdpServ.Checked = nUDUdpPortNum.Enabled = tBUdpListenAddress.Enabled = isUsingUDPServer(); nUDUdpPortNum.Value = getUDPServerPortNum(); + tBUdpListenAddress.Text = getUDPServerListenAddress(); + new ToolTip().SetToolTip(ckUdpServ, Properties.Resources.UdpServer); ckUdpServ.CheckedChanged += CkUdpServ_CheckedChanged; nUDUdpPortNum.Leave += NUDUdpPortNum_Leave; - + populateHoverTextDict(); cBController1.KeyPress += CBController_KeyPress; @@ -2594,6 +2596,7 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question } nUDUdpPortNum.Enabled = state; + tBUdpListenAddress.Enabled = state; } private void NUDUdpPortNum_Leave(object sender, EventArgs e) @@ -2603,6 +2606,7 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question { setUDPServerPort(curValue); nUDUdpPortNum.Enabled = false; + tBUdpListenAddress.Enabled = false; WaitUDPPortChange(); } } @@ -2614,9 +2618,15 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question { await TaskRunner.Run(() => Program.rootHub.UseUDPPort()); nUDUdpPortNum.Enabled = true; + tBUdpListenAddress.Enabled = true; } } + private void tBUdpListenAddress_TextChanged(object sender, EventArgs e) + { + setUDPServerListenAddress(tBUdpListenAddress.Text.Trim()); + } + private void cBFlashWhenLate_CheckedChanged(object sender, EventArgs e) { FlashWhenLate = cBFlashWhenLate.Checked; diff --git a/DS4Windows/DS4Forms/DS4Form.resx b/DS4Windows/DS4Forms/DS4Form.resx index cacfedf..ed42606 100644 --- a/DS4Windows/DS4Forms/DS4Form.resx +++ b/DS4Windows/DS4Forms/DS4Form.resx @@ -2707,7 +2707,7 @@ NoControl - 118, 2 + 200, 2 26, 13 @@ -2764,7 +2764,7 @@ False - 150, 0 + 230, 0 66, 20 @@ -2784,11 +2784,35 @@ 2 + + False + + + 90, 0 + + + 100, 20 + + + 64 + + + tBUdpListenAddress + + + System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + panel4 + + + 3 + 274, 82 - 219, 23 + 300, 23 64 diff --git a/DS4Windows/Properties/Resources.Designer.cs b/DS4Windows/Properties/Resources.Designer.cs index 02e9824..ef3ce7d 100644 --- a/DS4Windows/Properties/Resources.Designer.cs +++ b/DS4Windows/Properties/Resources.Designer.cs @@ -2099,6 +2099,15 @@ namespace DS4Windows.Properties { } } + /// + /// Looks up a localized string similar to Enable UDP server. Server listen address and port. Address value options: 127.0.0.1 localhost only | 0.0.0.0 all addresses | Specific host name or IP address.. + /// + public static string UdpServer { + get { + return ResourceManager.GetString("UdpServer", resourceCulture); + } + } + /// /// Looks up a localized string similar to Unassigned. /// diff --git a/DS4Windows/Properties/Resources.fi.resx b/DS4Windows/Properties/Resources.fi.resx index 0c79338..13624a9 100644 --- a/DS4Windows/Properties/Resources.fi.resx +++ b/DS4Windows/Properties/Resources.fi.resx @@ -600,4 +600,7 @@ DS4Windows sovellus pitää käynnistää järjestelmävalvojan oikeuksilla tämän toiminnon käyttämiseksi. + + Aktivoi UDP palvelu. Palvelun osoite ja portti. Osoitevaihtoehdot: 127.0.0.1 paikallinen | 0.0.0.0 kaikki liittymät | Tietty palvelinnimi tai IP numero. + \ No newline at end of file diff --git a/DS4Windows/Properties/Resources.resx b/DS4Windows/Properties/Resources.resx index 2569cc4..f3dd3f9 100644 --- a/DS4Windows/Properties/Resources.resx +++ b/DS4Windows/Properties/Resources.resx @@ -823,4 +823,7 @@ Allow touchpad mouse function to get toggled with PS + Touchpad Click. + + Enable UDP server. Server listen address and port. Address value options: 127.0.0.1 localhost only | 0.0.0.0 all addresses | Specific host name or IP address. + \ No newline at end of file