/// <summary>
		/// Determines whether the IP address is in the same subnet as the
		/// specified IP address.
		/// </summary>
		/// <param name="address">The IPAddress instance this is being called
		/// for.</param>
		/// <param name="other">The IP address to determine whether it is in the same
		/// subnet.</param>
		/// <param name="netmask">The subnetmask to apply.</param>
		/// <returns>true if both IP address are located in the same subnet; Otherwise
		/// false.</returns>
		/// <exception cref="ArgumentNullException">The other parameter or the netmask
		/// parameter is null.</exception>
		/// <exception cref="ArgumentException">The netmask and IP address must be
		/// of the same address family.</exception>
		/// <remarks>This is an extension method for the IPAddress class.</remarks>
		public static bool InSameSubnet(this IPAddress address, IPAddress other,
			IPAddress netmask) {
			other.ThrowIfNull("other");
			netmask.ThrowIfNull("netmask");

			return address.And(netmask).Equals(other.And(netmask));
		}
		public async Task<ValidateResponseResult> ValidateResponseAsync(IPAddress ipAddress, string challenge, string response)
		{
			ipAddress.ThrowIfNull("ipAddress");
			challenge.ThrowIfNull("challenge");
			response.ThrowIfNull("response");

			if (!_configuration.Enabled)
			{
				return ValidateResponseResult.ServiceDisabled;
			}

			var requestMessage =
				new HttpRequestMessage(HttpMethod.Post, _configuration.Url)
				{
					Content = new FormUrlEncodedContent(
						new[]
						{
							new KeyValuePair<string, string>("privatekey", _configuration.PrivateKey),
							new KeyValuePair<string, string>("remoteip", ipAddress.ToString()),
							new KeyValuePair<string, string>("challenge", challenge),
							new KeyValuePair<string, string>("response", response)
						})
				};

			requestMessage.Headers.Add("User-Agent", _configuration.UserAgent);

			HttpResponseMessage responseMessage = await HttpClientSingleton.Instance.SendAsync(requestMessage);
			string responseContent = await responseMessage.Content.ReadAsStringAsync();
			string[] lines = responseContent.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

			return lines.Length > 0 && lines[0] == "true" ? ValidateResponseResult.Valid : ValidateResponseResult.Invalid;
		}
		/// <summary>
		/// Bitwise ANDs the IP address with the specified netmask.
		/// </summary>
		/// <param name="address">The IPAddress instance to AND with the specified
		/// netmask.</param>
		/// <param name="netmask">The netmask to bitwise AND with the IP
		/// address.</param>
		/// <returns>The result of the bitwise AND.</returns>
		/// <exception cref="ArgumentNullException">The netmask parameter is
		/// null.</exception>
		/// <exception cref="ArgumentException">The netmask and IP address must be
		/// of the same address family.</exception>
		/// <remarks>This is an extension method for the IPAddress class.</remarks>
		static IPAddress And(this IPAddress address, IPAddress netmask) {
			netmask.ThrowIfNull("netmask");
			if (address.AddressFamily != netmask.AddressFamily)
				throw new ArgumentException("The address family of the specified netmask " +
					"is different from the address family of the IP address.");
			byte[] buffer = address.GetAddressBytes();
			byte[] other = netmask.GetAddressBytes();
			for (int i = 0; i < buffer.Length; i++)
				buffer[i] = (byte) (buffer[i] & other[i]);
			return new IPAddress(buffer);
		}
示例#4
0
		/// <summary>
		/// Initializes a new instance of the SocksReply class.
		/// </summary>
		/// <param name="status">The status of the response.</param>
		/// <param name="address">The server bound IP address.</param>
		/// <param name="port">The server bound port.</param>
		/// <exception cref="ArgumentNullException">The address parameter
		/// is null.</exception>
		public SocksReply(ReplyStatus status, IPAddress address, ushort port) {
			address.ThrowIfNull("address");
			Status = status;
			ATyp = address.AddressFamily == AddressFamily.InterNetworkV6 ?
				ATyp.IPv6 : ATyp.IPv4;
			Address = address;
			Port = port;
		}
示例#5
0
 /// <summary>
 /// Initializes a new instance of the Socks5Client class and connects to the
 /// specified port on the specified host.
 /// </summary>
 /// <param name="address">The IP address of the remote host to which you intend
 /// to connect.</param>
 /// <param name="port">The port number of the remote host to which you intend
 /// to connect.</param>
 /// <param name="username">The username with which to authenticate if the
 /// server requires authentication.</param>
 /// <param name="password">The password with which to authenticate if the
 /// server requires authentication.</param>
 /// <exception cref="ArgumentNullException">The address parameter is
 /// null.</exception>
 /// <exception cref="ArgumentOutOfRangeException">The port parameter is not
 /// between 0 and 65535.</exception>
 /// <exception cref="SocketException">An error occurred when accessing the
 /// socket.</exception>
 /// <exception cref="Socks5Exception">The server returned invalid data, or
 /// authentication with the SOCKS5 server failed.</exception>
 public Socks5Client(IPAddress address, int port, string username = null,
     string password = null)
 {
     address.ThrowIfNull("address");
     port.ThrowIfOutOfRange("port", 0, 65535);
     Username = username;
     Password = password;
     client = new TcpClient();
     client.Connect(address, port);
     InitializeConnection();
 }
示例#6
0
		/// <summary>
		/// Initializes a new instance of the BindingResponse class.
		/// </summary>
		/// <param name="id">The transaction id of the binding response.</param>
		/// <param name="address">The IP address containd in the binding
		/// response message.</param>
		/// <exception cref="ArgumentNullException">The id parameter or the
		/// address parameter is null.</exception>
		internal BindingResponse(byte[] id, IPAddress address) {
			id.ThrowIfNull("id");
			address.ThrowIfNull("address");
			Id = id;
			Address = address;
		}
示例#7
0
 /// <summary>
 /// Queries the STUN server with the specified IP address for the public IP
 /// address of the requesting host.
 /// </summary>
 /// <param name="address">The IP address of the STUN server to query.</param>
 /// <param name="port">The port on which the STUN service is running at
 /// the specified host.</param>
 /// <param name="timeout">The maximum number of milliseconds to wait for
 /// a server response before returning to the caller.</param>
 /// <returns>The public IP address of the requesting host.</returns>
 /// <exception cref="ArgumentNullException">The address parameter is
 /// null.</exception>
 /// <exception cref="ArgumentOutOfRangeException">The port is not between
 /// 0 and 65535.</exception>
 /// <exception cref="ProtocolViolationException">The specified STUN
 /// server returned an erroneous response.</exception>
 /// <exception cref="SocketException">The specified hostname could not be
 /// resolved, or an error occurred while sending the request or while
 /// retrieving the response, or the specified STUN server could not be
 /// reached.</exception>
 /// <exception cref="TimeoutException">The specified timeout has
 /// expired.</exception>
 public static IPAddress Query(IPAddress address, int port = 3478,
     int timeout = Int32.MaxValue)
 {
     address.ThrowIfNull("address");
     port.ThrowIfOutOfRange("port", 0, 65535);
     IPEndPoint IpEp = new IPEndPoint(address, port);
     var request = new BindingRequest().Serialize();
     int rto = initialRto;
     using (UdpClient udp = new UdpClient())
     {
         // The timeout mechanism is similar to TCP. For details,
         // refer to RFC 5389, Section 7.2.1. Sending over UDP.
         for (int tries = 0; tries < rc; tries++)
         {
             // Transmit the datagram.
             udp.Send(request, request.Length, IpEp);
             // Set the timeout value on the socket.
             udp.Client.ReceiveTimeout = rto;
             try
             {
                 byte[] datagram = udp.Receive(ref IpEp);
                 return BindingResponse.Deserialize(datagram).Address;
             }
             catch (SocketException e)
             {
                 if (e.ErrorCode != connectionTimeout)
                     throw;
                 timeout = timeout - rto;
                 if (timeout <= 0)
                     throw new TimeoutException("The timeout has expired.");
             }
             catch (SerializationException)
             {
                 throw new ProtocolViolationException("The STUN " +
                     "Binding Response is invalid.");
             }
             // Increase the timeout value.
             if (tries < (rc - 1))
                 rto = rto * 2;
             else
                 rto = initialRto * rm;
             if (timeout < rto)
                 rto = timeout;
         }
         // Give up.
         throw new SocketException(connectionTimeout);
     }
 }
示例#8
0
		/// <summary>
		/// Initializes a new instance of the SocksRequest class.
		/// </summary>
		/// <param name="command">The command of the request.</param>
		/// <param name="destination">The IP address of the destination host.</param>
		/// <param name="port">The port of the destination host.</param>
		/// <exception cref="ArgumentNullException">The destination parameter
		/// is null.</exception>
		public SocksRequest(SocksCommand command, IPAddress destination,
			ushort port) {
			destination.ThrowIfNull("destination");
			Command = command;
			ATyp = destination.AddressFamily == AddressFamily.InterNetworkV6 ?
				ATyp.IPv6 : ATyp.IPv4;
			Destination = destination;
			Port = port;
		}