From 312dfed6ca641fe6d47289e97dac16e842a71d26 Mon Sep 17 00:00:00 2001 From: Lee Walker Date: Mon, 18 May 2015 14:53:50 +0100 Subject: [PATCH] Allowed IP/Port via rather than just IPEndPoint --- SSQLib/EndPointHelpers.cs | 31 ++++++++++++++++++++++++++++++ SSQLib/SSQL.cs | 29 ++++++++++++++++++++++++++-- SSQLib/SSQLib.csproj | 1 + SSQLib/SocketUtils.cs | 4 ++-- Test.SSQLib/Test.SSQLib.csproj | 4 ++++ Test.SSQLib/Tests.SSQLib.Player.cs | 31 ++++++++++++++++++++++++++++++ Test.SSQLib/Tests.SSQLib.Server.cs | 16 +++++++++++++-- 7 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 SSQLib/EndPointHelpers.cs create mode 100644 Test.SSQLib/Tests.SSQLib.Player.cs diff --git a/SSQLib/EndPointHelpers.cs b/SSQLib/EndPointHelpers.cs new file mode 100644 index 0000000..6c76f08 --- /dev/null +++ b/SSQLib/EndPointHelpers.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; + +namespace SSQLib +{ + public class EndPointHelpers + { + public static IPEndPoint GetIPEndPointFromHostName(string hostName, int port, bool throwIfMoreThanOneIP) + { + var addresses = System.Net.Dns.GetHostAddresses(hostName); + if (addresses.Length == 0) + { + throw new ArgumentException( + "Unable to retrieve address from specified host name.", + "hostName" + ); + } + else if (throwIfMoreThanOneIP && addresses.Length > 1) + { + throw new ArgumentException( + "There is more that one IP address to the specified host.", + "hostName" + ); + } + return new IPEndPoint(addresses[0], port); // Port gets validated here. + } + } +} diff --git a/SSQLib/SSQL.cs b/SSQLib/SSQL.cs index 2832770..0986aaa 100644 --- a/SSQLib/SSQL.cs +++ b/SSQLib/SSQL.cs @@ -36,12 +36,25 @@ public SSQL() { } + + /// + /// Pings the specified Source server to retreive information about it such as the server name, max players, current number of players, etc. + /// + /// The string containing the IP address or hostname of the server + /// The port of the server + /// Information about the server or throws an SSQLServerException if it could not be retreived + public ServerInfo Server(string ip, int port) + { + IPEndPoint endPoint = EndPointHelpers.GetIPEndPointFromHostName(ip, port, true); + return Server(endPoint); + } + /// /// Pings the specified Source server to retreive information about it such as the server name, max players, current number of players, etc. /// /// The IPEndPoint object containing the IP address and port of the server /// Information about the server or throws an SSQLServerException if it could not be retreived - public ServerInfo Server(IPEndPoint ip_end) + public ServerInfo Server(EndPoint ip_end) { //Create a new empty server info object ServerInfo info = new ServerInfo(); @@ -188,11 +201,23 @@ public ServerInfo Server(IPEndPoint ip_end) return info; } + /// + /// Retreives information about the players on a Source server + /// + /// The string containing the IP address or hostname of the server + /// The port of the server + /// A List<> of PlayerInfo or throws an SSQLServerException if the server could not be reached + public List Players(string ip, int port) + { + IPEndPoint endPoint = EndPointHelpers.GetIPEndPointFromHostName(ip, port, true); + return Players(endPoint); + } + /// /// Retreives information about the players on a Source server /// /// The IPEndPoint object storing the IP address and port of the server - /// An ArrayList of PlayerInfo or throws an SSQLServerException if the server could not be reached + /// A List<> of PlayerInfo or throws an SSQLServerException if the server could not be reached public List Players(IPEndPoint ip_end) { //Create a new array list to store the player array diff --git a/SSQLib/SSQLib.csproj b/SSQLib/SSQLib.csproj index f01834d..10dd1a1 100644 --- a/SSQLib/SSQLib.csproj +++ b/SSQLib/SSQLib.csproj @@ -51,6 +51,7 @@ + diff --git a/SSQLib/SocketUtils.cs b/SSQLib/SocketUtils.cs index b4410ed..a42644d 100644 --- a/SSQLib/SocketUtils.cs +++ b/SSQLib/SocketUtils.cs @@ -28,7 +28,7 @@ internal class SocketUtils { private SocketUtils() { } - internal static byte[] getInfo(IPEndPoint ipe, Packet packet) + internal static byte[] getInfo(EndPoint ipe, Packet packet) { //Create the socket Socket srvSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); @@ -52,7 +52,7 @@ internal static byte[] getInfo(IPEndPoint ipe, Packet packet) //Create a new receive buffer byte[] rcvPacketInfo = new byte[packetSize]; - EndPoint Remote = (EndPoint)ipe; + EndPoint Remote = ipe; try { diff --git a/Test.SSQLib/Test.SSQLib.csproj b/Test.SSQLib/Test.SSQLib.csproj index f2a7ca1..d49fe02 100644 --- a/Test.SSQLib/Test.SSQLib.csproj +++ b/Test.SSQLib/Test.SSQLib.csproj @@ -46,6 +46,7 @@ + @@ -57,6 +58,9 @@ + + + diff --git a/Test.SSQLib/Tests.SSQLib.Player.cs b/Test.SSQLib/Tests.SSQLib.Player.cs new file mode 100644 index 0000000..95c66d0 --- /dev/null +++ b/Test.SSQLib/Tests.SSQLib.Player.cs @@ -0,0 +1,31 @@ +using SSQLib; +using System.Net; +using Xunit; + +namespace Test.SSQLib +{ + public class PlayerTests + { + [Fact] + public void TestFakePlayerServerThrowsExceptionWithIpEndpoint() + { + Assert.Throws(typeof(SSQLServerException), () => + { + SSQL query = new SSQL(); + //127.0.0.2 set to stop loopback potentially resolving and failing test + query.Players(new IPEndPoint(IPAddress.Parse("127.0.0.2"), 27015)); + }); + } + + [Fact] + public void TestFakePlayerServerThrowsExceptionWithStringIp() + { + Assert.Throws(typeof(SSQLServerException), () => + { + SSQL query = new SSQL(); + //127.0.0.2 set to stop loopback potentially resolving and failing test + query.Players("127.0.0.2", 27015); + }); + } + } +} diff --git a/Test.SSQLib/Tests.SSQLib.Server.cs b/Test.SSQLib/Tests.SSQLib.Server.cs index cf352a1..c474940 100644 --- a/Test.SSQLib/Tests.SSQLib.Server.cs +++ b/Test.SSQLib/Tests.SSQLib.Server.cs @@ -7,13 +7,25 @@ namespace Test.SSQLib public class ServerTests { [Fact] - public void TestFakeServerThrowsException() + public void TestFakeServerThrowsExceptionWithIpEndpoint() { Assert.Throws(typeof(SSQLServerException), () => { SSQL query = new SSQL(); - query.Server(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 27015)); + //127.0.0.2 set to stop loopback potentially resolving and failing test + query.Server(new IPEndPoint(IPAddress.Parse("127.0.0.2"), 27015)); }); } + + [Fact] + public void TestFakeServerThrowsExceptionWithStringIp() + { + Assert.Throws(typeof(SSQLServerException), () => + { + SSQL query = new SSQL(); + //127.0.0.2 set to stop loopback potentially resolving and failing test + query.Server("127.0.0.2", 27015); + }); + } } }