2

The problem I am having when I create IP addresses on my network adapter with:

 [DllImport("iphlpapi.dll", SetLastError = true)]
 private static extern UInt32 AddIPAddress(UInt32 address, UInt32 ipMask, int ifIndex, out IntPtr nteContext, out IntPtr nteInstance);

 public void AddIpAddressToInterface(string ipAddress, string subnetMask)
    {
        var ipAdd = System.Net.IPAddress.Parse(ipAddress);
        var subNet = System.Net.IPAddress.Parse(subnetMask);

        var nteContext = 0;
        var nteInstance = 0;
        IntPtr ptrNteContext;
        var ptrNteInstance = new IntPtr(nteInstance);
        UInt32 result = AddIPAddress((uint)BitConverter.ToInt32(ipAdd.GetAddressBytes(), 0), (uint)BitConverter.ToInt32(subNet.GetAddressBytes(), 0), _adapterIndex, out ptrNteContext,
                            out ptrNteInstance);

        string errorFmt = "Cannot create IP address {0} for adapter {1}. Error: {2}";

        if (result == (UInt32)WINERRORS.ERROR_SUCCESS)
        {
            _ipaddressList.Add(ipAddress, ptrNteContext);
        }
        else if (result == (UInt32)WINERRORS.ERROR_OBJECT_ALREADY_EXISTS)
        {
            Logger.LogWarning(string.Format(errorFmt, ipAddress, subNet, (WINERRORS)result));
        }
        else
        {
            try
            {
                Logger.LogError(string.Format(errorFmt, ipAddress, subNet, (WINERRORS)result));
            }
            catch
            {
                Logger.LogError(string.Format(errorFmt, ipAddress, subNet, result));
            }
        }
    }

I attempt to bind to the IP using: Note: LocalPort is 0. LocalAddress is the one created with AddIpAddressToInterface ex. 10.43.55.XXX

 var localEndPoint = new IPEndPoint(LocalAddress, LocalPort);
 _client = new TcpClient(localEndPoint);

but I get this error:

The requested address is not valid in its context
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress      SocketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpClient..ctor(IPEndPoint localEP)
at CPCToolbox.Shared.TCPConnectionHandler.InitializeNewTCP()

I can do a ipconfig /all and see my IP addresses. the static one is is 10.43.99.199.

ipconfig

Routes:

IP Routes

The connect call host is a remote host is 10.43.99.50 port 18000 on subnet 255.255.240.0

 _client.Connect(_Host, _Port);
JBurlison
  • 551
  • 3
  • 12
  • 23

0 Answers0