static bool IsNetworkAvailable (out NetworkReachabilityFlags flags) { if (defaultRouteReachability == null) { var ipAddress = new IPAddress (0); defaultRouteReachability = new NetworkReachability (ipAddress.MapToIPv6 ()); defaultRouteReachability.SetNotification (OnChange); defaultRouteReachability.Schedule (CFRunLoop.Current, CFRunLoop.ModeDefault); } return defaultRouteReachability.TryGetFlags (out flags) && IsReachableWithoutRequiringConnection (flags); }
public static bool IsAdHocWiFiNetworkAvailable (out NetworkReachabilityFlags flags) { if (adHocWiFiNetworkReachability == null) { var ipAddress = new IPAddress (new byte[] { 169, 254, 0, 0 }); adHocWiFiNetworkReachability = new NetworkReachability (ipAddress.MapToIPv6 ()); adHocWiFiNetworkReachability.SetNotification (OnChange); adHocWiFiNetworkReachability.Schedule (CFRunLoop.Current, CFRunLoop.ModeDefault); } return adHocWiFiNetworkReachability.TryGetFlags (out flags) && IsReachableWithoutRequiringConnection (flags); }
public static string SignCookie(string input, long timestamp = long.MinValue, IPAddress remoteIp = null, uint validTime = uint.MaxValue) { if(input == null) throw new ArgumentNullException(); if(hmacKey == null) throw new InvalidOperationException(); var dataLen = cookieEncoding.GetByteCount(input) + DataOffset; var data = new byte[dataLen]; var encodedLength = cookieEncoding.GetBytes(input, 0, input.Length, data, DataOffset); System.Diagnostics.Debug.Assert(encodedLength == (dataLen - DataOffset)); data.PutInt64LE(TsOffset, timestamp == long.MinValue ? UnixTimestamp.CurrentMillisecondTimestamp : timestamp); data.PutUInt32LE(ValidityOffset, validTime); if(remoteIp == null) { //which means don't care remoteIp = IPAddress.IPv6Any; //all zeroes } else if(remoteIp.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { remoteIp = remoteIp.MapToIPv6(); } Array.Copy(remoteIp.GetAddressBytes(), 0, data, IpAddrOffset, IpAddrLength); Array.Copy(hmac.Value.ComputeHash(data, SigLength, data.Length - SigLength), data, SigLength); return Convert.ToBase64String(data).Replace('+', '.').Replace('/', '_').Replace('=', '-'); }
public static string VerifyCookie(string input, out long signTime, IPAddress remoteIp = null, uint validTime = uint.MaxValue) { if(hmacKey == null) throw new InvalidOperationException(); signTime = long.MinValue; if(input == null) { return null; } if(input.Length < 80) { //Minimum length (60 byte sig+metadata) return null; } if((input.Length & 3) != 0) { //Length of a base64 string must be of multiples of 4 return null; } try { var data = Convert.FromBase64String(input.Replace('.', '+').Replace('_', '/').Replace('-', '=')); //throws FormatException var computed = hmac.Value.ComputeHash(data, SigLength, data.Length - SigLength); //Compute signature int trash = 0; for(int i = 0; i < SigLength; i++) { //Compare the sig. (Avoiding timing attack) trash |= data[i] ^ computed[i]; } if(trash != 0) { //sig mismatch, reject. return null; } signTime = data.GetInt64LE(TsOffset); var currentTs = UnixTimestamp.CurrentMillisecondTimestamp; if(signTime > currentTs) { //Reject anything with a timestamp from the future return null; } //Compute the validity period for this verification. //Subtract 1 to treat value 0 (defined as infinite) as greatest. validTime = Math.Min(data.GetUInt32LE(ValidityOffset) - 1, validTime - 1); if(validTime != uint.MaxValue) { if((signTime + validTime) < currentTs) { //Should be <=, but we've subtracted 1 from the original value. return null; } } //all zeroes denotes don't care. if(!data.TrueForRange(IpAddrOffset, IpAddrLength, x => x == 0)) { if(remoteIp == null) { //No remote ip provided while a verification is required, reject. return null; } if(remoteIp.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { remoteIp = remoteIp.MapToIPv6(); } var ip = remoteIp.GetAddressBytes(); if(!ArrayEx.RangeEquals(data, IpAddrOffset, ip, 0, IpAddrLength)) { return null; } } return cookieEncoding.GetString(data, DataOffset, data.Length - DataOffset); //throws ArgumentException } catch(FormatException) { return null; } catch(ArgumentException) { return null; } }
public static TryResult<Value128> TryFromIPAddress(IPAddress value) { // Check input value if (value == null) { return convertFailResult; } // Map to IPv6 var bytes = value.MapToIPv6().GetAddressBytes(); // Try convert from byte array return TryFromByteArray(bytes, 0); }
public static byte[] IPAddressToBytes(IPAddress ipAddress) { // if address is IPv4, map it onto IPv6 if (!ipAddress.IsIPv4MappedToIPv6) ipAddress = ipAddress.MapToIPv6(); return ipAddress.GetAddressBytes(); }
public static void LoadState(Stream stream) { unconnectedPeers.Clear(); using (BinaryReader reader = new BinaryReader(stream, Encoding.ASCII, true)) { int count = reader.ReadInt32(); for (int i = 0; i < count; i++) { IPAddress address = new IPAddress(reader.ReadBytes(4)); int port = reader.ReadUInt16(); unconnectedPeers.Add(new IPEndPoint(address.MapToIPv6(), port)); } } }
public IPAddress MapToIPv6(IPAddress address) { return address.MapToIPv6(); }
public static byte[] IPAddressToBytes(IPAddress ipAddress) { if (!ipAddress.IsIPv4MappedToIPv6) ipAddress=ipAddress.MapToIPv6(); return ipAddress.GetAddressBytes(); }
/// <summary> /// Check if two ip are equal, using MapToIPv6 if needed /// </summary> /// <param name="ipAddress">IP address</param> /// <param name="other">Other ip address</param> /// <returns>True if ip are equal or equal, false otherwise</returns> public static bool EqualsWithMapToIPv6(this System.Net.IPAddress ipAddress, System.Net.IPAddress other) { if (ipAddress.Equals(other)) { return(true); } try { IPAddress ipv6 = (ipAddress.IsLocalHost() ? IPAddress.Parse("::1") : ipAddress.MapToIPv6()); IPAddress otherIPV6 = (other.IsLocalHost() ? IPAddress.Parse("::1") : other.MapToIPv6()); return(ipv6.Equals(otherIPV6)); } catch { return(false); } }