.NET Remoting Bind to All NICs - c#

I am working on my companies .NET remoting server application to try and make it bind to all available NICs, so that it listens on multiple subnets.
The current way we do it is by telling the server to bind to a specific IP address and that has worked in the past, but when hosted on a server with multiple subnets the clients all need to be on the same subnet or they won't be able to speak to the server.
One solution I tried was to tell the server to bind to the machine name of the server it was running on using the following code:
// If there is an IP present in the config to bind to, then bind on that specific IP.
if (!string.IsNullOrEmpty(remotingBindTo))
{
props.Add("bindTo", remotingBindTo);
}
// Otherwise bind on the machine name, meaning the server should bind on all IP addresses.
else
{
props.Add("machineName", Environment.MachineName);
props.Add("useIpAddress", false);
}
// Create a new channel
IChannel channel = new TcpServerChannel(props, null);
// Register the channel
ChannelServices.RegisterChannel(channel, remotingSecure);
That basically says if the servers config setting "RemotingBindTo" is set then we bind to the provided IP address otherwise we bind to the machine name.
The problem with this is if you use a VPN connection to connect a client to the server. When the client is creating a proxy to a server object it is trying to connect via the machine name and the client PC can not resolve that to an IP address. A work around to this was to place the name into the Windows hosts file and tell it to resolve to the VPN IP address, but because the VPN gives a different IP address every time this is not a viable solution.
I suppose my question is: Is it possible AT ALL to get a .NET remoting server to bind to all available NICs/IP addresses?
Has anyone else experienced anything similar?

Related

Handling ever changing baseAddress in WCF host

As stated in the question, I need to handle a changing baseAddress property of a WCF service host. I have a simple object that contains a picture's binary value. This will be sent from a remote computer (actually a multitude of different ones) to the server through a Web app on the server that requests a specific file name based on given files in a database.
To do this I have made the service and implemented the necessary contracts, but I am stuck at the baseAddress element in the service's config file.
The dilemma is that the remote machine's IP is dynamic. It changes at about 5 min intervals (externally provided) and the machine is not part of a VPN or any (inter)network and has no DNS configured. It only has its pc name set. I am however able to pull the IP of this device from a table to provide to the client app with the 'correct' IP to bind to, but my issue is rather at the host side.
The question is basically - in this described host, how do I specify the baseAddress that clients will bind to? I imagine I cannot rebind every time an IP address change is detected, or would that be no issue?
I have tried specifying "http://localhost:6090/BasicService/" which was unreachable when I attempted from the client to call "http://192.168.0.4:6090/BasicService/". However when I specify the exact IP in both client and host, the client successfully contacts the host.
Note that the binding of my service is basicHttpBinding and the private IP addresses are just in a test environment. When live, these devices receive their IP from m2m providers.
EDIT:
For better understanding, the remote device will be the server and contains the binding information which is XYZ and XYZ is what I am trying to figure out. The client can connect to a local machine to retrieve a specific remote device's IP and then bind the client to that given IP, but the issue is that in the host I am unsure as to what to use in the following:
host
baseAddresses
add baseAddress="http://????????:6090/"
baseAddresses
host
The suggested answer below does help when these devices are in a closed network and you call the server's private IP, but does not work with the server's public IP.
There is some confusion about your question (see comments). In this answer I assumed your clients do know the IP address of your server, but your problem is binding the server to an unkown IP address.
Server side
In order to listen to any ip address on your server side, you could use ip address 0.0.0.0. This way, on the server side, you don't need to worry about the ip address. Like so:
<host>
<baseAddresses>
<add baseAddress="http://0.0.0.0:6090/BasicService" />
</baseAddresses>
</host>
Client side
And on the client side, you can make the binding dynamic, by specifying it in code:
string ipAddress = GetIpFromTable(); // Assuming client can do this...
string url = $"http://{ipAddress}:6090/BasicService";
var binding = new BasicHttpBinding();
var address = new EndpointAddress(url);
var client = new BasicServiceClient(binding, address);
You may run into some trouble when the ip address changes during/before a request though, so make sure you have your error handling in order and retry if the first attempt failed.

how to get a device name for a given ip address

I need to get the network device name for the given IP address.For example a printer connected in a network.I have the IP address of the printer but I need to access its name using the corresponding IP address.
I tried with dns.getHostByaddress(), but it didn't worked out properly.Let me know if any other solution exists.
string c = Dns.GetHostByAddress("xxx.xxx.x.xxx").HostName;
pdoc.PrinterSettings.PrinterName =c ;
It is a windows stand alone application using c# vs2010.
You cannot get the "DeviceName" by the ip itself. DNS-Names only exist at a DNS-Server (or netbios in such an environment) which hold the information about it - if you call "Dns.GetHostByAddress" you are always query the DNS-Server. If you are using DHCP on the devices, the dns entry will be updated automatically (if configured well). Else you have to configure the DNS-Server ip-address on the device and hope that this device is able to Register at the DNS (permissions and supported by your device).
Another way (which i not prefer) is to add the entries to the DNS manually.
You can force dnsregistration on a Windows machine by executing "ipconfig /registerdns"

How do I specify Server Ip address in my Client program?

I'm currently working with TCP/IP Sockets, My client console program has to connect with server which is my PC, If the destination in the client program is specified as a local host it works fine, what I need to get done with is to connecting the client with my server through internet, what I did is looked up for my IP address on http://www.whatismyip.com/ and tried but it didn't work as I think it's a Network Interface IP address, then I altered destination IP address in client program specifically to the address of my computer which I want it to be a server, but that didn't work also. Here's my code.
Ip = (IPAddress.Parse("192.168.1.4"));
MyClient.Connect(Ip,6000);
GetStream = MyClient.GetStream();
Console.WriteLine("CONNECTED TO SERVER");
Read = new BinaryReader(GetStream);
Write = new BinaryWriter(GetStream);
There 2 things (at least) that you should be aware of:
1. To access your computer from Internet (from the public address, the one you get with whatismyip.com) you need to open the port (6000) at the router, and tell the router to what IP it should forward the incoming connection. You could specified specifics ports or put a DMZ host where all the incoming connections will be routed to that host/PC. Read your router manual to see how that is done.
2. You cannot access your public IP from the inner side of the router (intranet), if you want to connect to your public IP you need to be in another network.
If you have a dynamic IP (is the default) every time the router is powered off and on then, most probably, that IP would change, you need to investigate thru whatismyip.com in order to know what IP has been assigned. You could connect to dyndns.org and ask for a host name myhost.mydomain.com (.es, .fr, etc), and in the router tell the DDNS (Dynamic DNS) to update that host every time the IP changes. In your client program you then connect to MyClient.Connect("myhost.mydomain.com", 6000);
Hope I explain myself well enough, anyway, if you have any question let me know.

How to get my correct IP address?

I try to get my correct IP but I can't
I'm using this code:
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
But it does not work for me!
For example my real IP is 151.246.147.86 but with this code I get 192.168.1.2.
Note: I have 4 Network adapters and in DOC with IPConfig I see this:
Network adapter: Local Area Connection 6
Media state : Media disconnected
Network adapter: Local Area Connection 4
Media state : Media disconnected
Network adapter: Local Area Connection 3
IP Address: 10.10.255.222
Network adapter: Local Area Connection
IP Address: 192.168.1.2
Now I connect to net and using with "Local Area Connection"; the public IP of my WAN connection is 151.246.147.86. I want to get this public IP (151.246.147.86), how can I do that?
Note : I don't want (and I can't, since I am using a VPN) use third party websites to get my IP
Please help!
-------EDIT :-------------
Note : I using from VPN and my VPN IP (for example) is : 176.227.197.111. But the IP of my WAN is: 151.246.147.86 and i want to get this address.
You (probably) are using a router/modem, therefore 192.168.1.2 is your "real IP". Your modem/router will be on the same (private) network, and its public interface (WAN) will have a public IP. So you need to get the IP on the public interface of your modem/router.
How to get the WAN IP of your modem depends on your mark and model; if it supports UPnP you can probably do it, or maybe if it is an enterprise class router it may also support SNMP...
You should specify your make and model.
Another way without using external sites: do a tracert to a known site (google?) the first hop will be your route.
Also, if you are on a VPN, you may be able to use the same technique. For example, if you know another host on the VPN (A server maybe?) you can ping/tracert it and discover your router from there. I don't know if in this case you will obtain what you call "a real IP" (by the way, how do you know this IP in the first place? You may be able to obtain it in the same way, programmatically).
Another solution for your VPN-based scenario: you can use Windows to help you. Windows has some kind of VPN management (RAS) which may help you; I would suggest starting from here to understand the basics, and then look for a library/SDK to help you (a quick google returned http://dotras.codeplex.com/).
What you ask is not possible unless there is some way to query your router/modem/external-most endpoint for its WAN address. The only IP address your computer knows about is its own (internal IP).
Technical note: there is nothing non-"real" about the IP address 192.168.1.2 - this is your computer's address. It is simply local to your given internal network and all but useless to anything outside.
If your router supports uPnP, you will need to query GetExternalIPAddress (starting point Is there a UPnP Library for .NET (C# or VB.NET)?). However since uPnP is considered dangerous and many security-conscious users turn it off, I would not count on it being enabled.
Querying an external service will be your most reliable bet for getting your external IP, whether it is one you write, or a third party service (see also https://superuser.com/questions/420969/services-that-just-return-your-public-ip-as-text).
There are many methods and tutorials that teach you how to find an ip address,I use this tool http://www.ipgp.net/ to display information about any ip address, just enter any IP address into the box and you will get the country, city, region, ISP, street address and the satellite location map for every query.
you're trying to get your IP address from your computer or a server? It will make a lot of difference. If you're trying to get your IP address from your computer/laptop use http://www.howtochangeipaddress.com. Your IP will pop up in front of you soon you enter the site.

C# - How to get IP Address when connected to (RAS) VPN

Good afternoon,
Can anyone give any examples of how to obtain the IP Address of the local machine when it's connected to a remote windows domain network via VPN (RAS)? i.e. I need the VPN address and not the remote users local network address.
For example, my Server Side Windows Service communicates with my client side application and needs to create a log of all connected users and their IP Addresses.
This solution is easy enough when using a computer on the local network, but I wondered how I can go about getting the IP addresses of the users who are connected to the server via VPN. Please note that the IP address get method will be executed client side and sent to the server.
Here's my current code that works only when locally connected to the domain network:
public static string GetLocalIPv4()
{
string ipv4Address = String.Empty;
foreach (IPAddress currrentIPAddress in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (currrentIPAddress.AddressFamily.ToString() == System.Net.Sockets.AddressFamily.InterNetwork.ToString())
{
ipv4Address = currrentIPAddress.ToString();
break;
}
}
return ipv4Address;
}
Our internal network is controlled by Windows SBS and uses a domain such as mycompany.local.
Thank you very much for your time and I look forward to reading your responses.
Rob
As the comment from #MarcB notes, cannot think of a good reason why you might want that info... so would be interesting if you could explain a use for this in an application just out of curiosity.
However, there are a lot of incorrect answers on here and online regarding enumerating IP addresses for a machine by using Dns.GetHostAddresses. It appears most people are not realizing the difference between looking up a machine name in the configured DNS resolver versus enumerating the machine address. These are very different things and while it might seem to return the right results in many cases, this is absolutely the wrong way to go about it because they are not the same thing. For example, see this link to an article on here where the original poster flagged an incorrect response as the answer but the correct response is below by #StephanCleary. See:
Get IPv4 Addresses
The difference is you want to look at the machines configuration and enumerate whatever IP address you are interested in locating from the machines own TCPIP stack. The code above and many of the incorrect responses try to lookup the name in the DNS resolver. Once you have that part correct, then you should be able to determine the VPN connection based on the network adaptor (by name or other attribute).

Categories

Resources