public static unsafe bool Ipv4StringToAddress(ReadOnlySpan <char> ipSpan, out long address) { int end = ipSpan.Length; long tmpAddr; fixed(char *ipStringPtr = &MemoryMarshal.GetReference(ipSpan)) { tmpAddr = IPv4AddressHelper.ParseNonCanonical(ipStringPtr, 0, ref end, notImplicitFile: true); } if (tmpAddr != IPv4AddressHelper.Invalid && end == ipSpan.Length) { // IPv4AddressHelper.ParseNonCanonical returns the bytes in the inverse order. // Reverse them and return success. address = ((0xFF000000 & tmpAddr) >> 24) | ((0x00FF0000 & tmpAddr) >> 8) | ((0x0000FF00 & tmpAddr) << 8) | ((0x000000FF & tmpAddr) << 24); return(true); } else { // Failed to parse the address. address = 0; return(false); } }
public static unsafe bool Ipv4StringToAddress(string ipString, out long address) { Debug.Assert(ipString != null); long tmpAddr; int end = ipString.Length; fixed(char *ipStringPtr = ipString) { tmpAddr = IPv4AddressHelper.ParseNonCanonical(ipStringPtr, 0, ref end, notImplicitFile: true); } if (tmpAddr != IPv4AddressHelper.Invalid && end == ipString.Length) { // IPv4AddressHelper.ParseNonCanonical returns the bytes in the inverse order. // Reverse them and return success. address = ((0xFF000000 & tmpAddr) >> 24) | ((0x00FF0000 & tmpAddr) >> 8) | ((0x0000FF00 & tmpAddr) << 8) | ((0x000000FF & tmpAddr) << 24); return(true); } else { // Failed to parse the address. address = 0; return(false); } }
public static unsafe bool Ipv4StringToAddress(ReadOnlySpan <char> ipSpan, out long address) { int end = ipSpan.Length; long tmpAddr; fixed(char *ipStringPtr = &MemoryMarshal.GetReference(ipSpan)) { tmpAddr = IPv4AddressHelper.ParseNonCanonical(ipStringPtr, 0, ref end, notImplicitFile: true); } if (tmpAddr != IPv4AddressHelper.Invalid && end == ipSpan.Length) { // IPv4AddressHelper.ParseNonCanonical returns the bytes in host order. // Convert to network order and return success. address = (uint)IPAddress.HostToNetworkOrder(unchecked ((int)tmpAddr)); return(true); } else { // Failed to parse the address. address = 0; return(false); } }
private static IPAddress InternalParse(string ipString, bool tryParse) { if (ipString == null) { if (tryParse) { return(null); } throw new ArgumentNullException("ipString"); } // // IPv6 Changes: Detect probable IPv6 addresses and use separate // parse method. // if (ipString.IndexOf(':') != -1) { #if !FEATURE_PAL // // If the address string contains the colon character // then it can only be an IPv6 address. Use a separate // parse method to unpick it all. Note: we don't support // port specification at the end of address and so can // make this decision. // // We need to make sure that Socket is initialized for this // call ! // SocketException e = null; long scope = 0; if (Socket.OSSupportsIPv6) { byte[] bytes = new byte[IPv6AddressBytes]; SocketAddress saddr = new SocketAddress(AddressFamily.InterNetworkV6, SocketAddress.IPv6AddressSize); SocketError errorCode = UnsafeNclNativeMethods.OSSOCK.WSAStringToAddress( ipString, AddressFamily.InterNetworkV6, IntPtr.Zero, saddr.m_Buffer, ref saddr.m_Size); if (errorCode == SocketError.Success) { // // We have to set up the address by hand now, to avoid initialization // recursion problems that we get if we use IPEndPoint.Create. // for (int i = 0; i < IPv6AddressBytes; i++) { bytes[i] = saddr[i + 8]; } // // Scope // scope = (long)((saddr[27] << 24) + (saddr[26] << 16) + (saddr[25] << 8) + (saddr[24])); return(new IPAddress(bytes, scope)); } if (tryParse) { return(null); } e = new SocketException(); } else { unsafe { int offset = 0; if (ipString[0] != '[') { ipString = ipString + ']'; //for Uri parser to find the terminator. } else { offset = 1; } int end = ipString.Length; fixed(char *name = ipString) { if (IPv6AddressHelper.IsValidStrict(name, offset, ref end) || (end != ipString.Length)) { ushort[] numbers = new ushort[NumberOfLabels]; string scopeId = null; fixed(ushort *numbPtr = numbers) { IPv6AddressHelper.Parse(ipString, numbPtr, 0, ref scopeId); } // // Scope // if (scopeId == null || scopeId.Length == 0) { return(new IPAddress(numbers, 0)); } uint result; scopeId = scopeId.Substring(1); if (UInt32.TryParse(scopeId, NumberStyles.None, null, out result)) { return(new IPAddress(numbers, result)); } } } } if (tryParse) { return(null); } e = new SocketException(SocketError.InvalidArgument); } throw new FormatException(SR.GetString(SR.dns_bad_ip_address), e); #else // !FEATURE_PAL // IPv6 addresses not supported for FEATURE_PAL throw new SocketException(SocketError.OperationNotSupported); #endif // !FEATURE_PAL } else // The new IPv4 parser is better than the native one, it can parse 0xFFFFFFFF. (It's faster too). { // App-Compat: The .NET 4.0 parser used Winsock. When we removed this initialization in 4.5 it // uncovered bugs in IIS's management APIs where they failed to initialize Winsock themselves. // DDCC says we need to keep this for an in place release, but to remove it in the next SxS release. Socket.InitializeSockets(); /////////////////////////// int end = ipString.Length; long result; unsafe { fixed(char *name = ipString) { result = IPv4AddressHelper.ParseNonCanonical(name, 0, ref end, true); } } if (result == IPv4AddressHelper.Invalid || end != ipString.Length) { if (tryParse) { return(null); } throw new FormatException(SR.GetString(SR.dns_bad_ip_address)); } // IPv4AddressHelper always returns IP address in a format that we need to reverse. result = (((result & 0x000000FF) << 24) | (((result & 0x0000FF00) << 8) | (((result & 0x00FF0000) >> 8) | ((result & 0xFF000000) >> 24)))); return(new IPAddress(result)); } } // Parse