Beispiel #1
0
        private SocketError Do_Connect(IntPtr socket, ref SocketAddressIn address, int addressSize)
        {
            var addressIn = address;

            ContinueExecution(() => DebugMessage("Connect: " + addressIn.IPAddress.IpAddress));

            if (!BindSocket(socket, address.Family, address.IPAddress.IpAddress))
            {
                DebugMessage(Socket.WSAGetLastError().ToString());
                return(SocketError.SocketError);
            }

            var returnValue = Socket.Connect(socket, ref address, addressSize);

            //if (returnValue == SocketError.SocketError
            //    && (Socket.WSAGetLastError() == SocketError.WouldBlock
            //        || Socket.WSAGetLastError() == SocketError.Success))
            //{
            //    // Non blocking mode
            //    returnValue = SocketError.Success;
            //}

            if (returnValue == SocketError.SocketError && Socket.WSAGetLastError() == SocketError.Success)
            {
                returnValue = SocketError.Success;
            }

            return(returnValue);
        }
Beispiel #2
0
        private bool BindSocket(IntPtr socket, AddressFamily addressFamily, IPAddress ipAddress, int portNumber = 0)
        {
            switch (addressFamily)
            {
            case AddressFamily.InterNetwork:
                if (ipAddress.Equals(IPAddress.Any))
                {
                    return(true);
                }
                if (IsIpInRange(ipAddress, IPAddress.Parse("127.0.0.0"), IPAddress.Parse("127.255.255.255")))
                {
                    return(true);    // Loopback
                }
                if (IsIpInRange(ipAddress, IPAddress.Parse("10.0.0.0"), IPAddress.Parse("10.255.255.255")))
                {
                    return(true);    // Private Network
                }
                if (IsIpInRange(ipAddress, IPAddress.Parse("172.16.0.0"), IPAddress.Parse("172.31.255.255")))
                {
                    return(true);    // Private Network
                }
                if (IsIpInRange(ipAddress, IPAddress.Parse("192.168.0.0"), IPAddress.Parse("192.168.255.255")))
                {
                    return(true);    // Private Network
                }
                if (IsIpInRange(ipAddress, IPAddress.Parse("224.0.0.0"), IPAddress.Parse("239.255.255.255")))
                {
                    return(true);    // Multicast
                }
                var networkIp = GetNetworkInterfaceIPAddress(addressFamily);
                if (networkIp == null)
                {
                    return(false);
                }
                if (networkIp.Equals(ipAddress))
                {
                    return(true);
                }
                var bindIn = new SocketAddressIn
                {
                    IPAddress = new SocketAddressIn.AddressIn {
                        IpAddress = networkIp
                    },
                    Family = networkIp.AddressFamily,
                    Port   = portNumber
                };
                var addressIn = bindIn;
                ContinueExecution(() => DebugMessage("Auto Bind: " + addressIn.IPAddress.IpAddress));
                return(Socket.Bind(socket, ref bindIn, Marshal.SizeOf(bindIn)) == SocketError.Success);

            default:
                return(true);
            }
        }
Beispiel #3
0
        private SocketError Do_Bind(IntPtr socket, ref SocketAddressIn address, int addressSize)
        {
            var addressIn = address;

            ContinueExecution(
                () => DebugMessage("Bind: " + addressIn.IPAddress.IpAddress + ":" + addressIn.Port));

            var networkIp = GetNetworkInterfaceIPAddress(address.Family);

            if (networkIp != null && !address.IPAddress.IpAddress.Equals(IPAddress.Any) &&
                !address.IPAddress.IpAddress.Equals(networkIp) &&
                !IsIpInRange(address.IPAddress.IpAddress, IPAddress.Parse("127.0.0.0"),
                             IPAddress.Parse("127.255.255.255")))
            {
                address.IPAddress.IpAddress = networkIp;
                ContinueExecution(() => DebugMessage("Modified Bind: " + addressIn.IPAddress.IpAddress));
            }
            return(Socket.Bind(socket, ref address, addressSize));
        }