Get client IP in vb.net - vb.net

I am trying to get client ID with vb.net. My code is as under :
Partial Class foo
Inherits System.Web.UI.Page
Private ipv4 As New clsIPv4
Dim ref As String = 0
Dim client_ip As String
client_ip = Request.UserHostAddress()
Dim myHost As String = System.Net.Dns.GetHostName()
client_ip = ipv4.ResolveAddress(myHost)
This code is returning me list of ip adresses (i.e 182.50.130.143;118.139.172.1;118.139.172.2;118.139.172.3;118.139.172.4;118.139.172.5;118.139.172.6;118.139.172.7;118.139.172.8;118.139.172.9;118.139.172.10;118.139.172.11;118.139.172.12;118.139.172.13;118.139.172.14;118.139.172.15;118.139.172.16;118.139.172.17;118.139.172.18;118.139.172.19;118.139.172.20;118.139.172.21;118.139.172.22;118.139.172.23;118.139.172.24;118.139.172.25;118.139.172.26;118).
Please guide where I am wrong

If you are using ASP.NET, you can use: Request.UserHostAddress
to get your client IP address.
But if you are using a windows application to get local IP address, in fact you may receive multiple IP addresses! In this case, if you want to get a specific address which you know the netid part of the IP address you can compare all IP addresses with a corresponding netid address.

This worked perfectly for me
System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).GetValue(0).ToString()

Related

DKIM Signing an email using MimeKit in a vb.net project

I'm using MimeKit to send emails and the use of DKIM to sign them has been broached. I've looked at the example on the MimKit site, and googled the terms but found no answers.
Public Shared Sub DkimSign(ByVal message As MimeMessage)
Dim headers = New HeaderId() {HeaderId.From, HeaderId.Subject, HeaderId.Date}
Dim headerAlgorithm = DkimCanonicalizationAlgorithm.Simple
Dim bodyAlgorithm = DkimCanonicalizationAlgorithm.Simple
Dim signer = New DkimSigner("filename", "domain", "selector") With {.SignatureAlgorithm = DkimSignatureAlgorithm.RsaSha1, .AgentOrUserIdentifier = "#eng.example.com"}
message.Prepare(EncodingConstraint.SevenBit)
message.Sign(signer, headers, headerAlgorithm, bodyAlgorithm)
End Sub
When instantiating the signer it requires a filename, domain and selector. If I'm sending an email from "bob#website.com" I would assume that the physical file would be placed on the root of the site and the instantiation would look something like this:
Dim signer = New DkimSigner("dkim.txt", "website.com", "") With {.SignatureAlgorithm = DkimSignatureAlgorithm.RsaSha1, .AgentOrUserIdentifier = "???"}
But not sure the format/reason for the AgentOrUserIdentifier ... can anyone edify me or correct me if my assumptions are wrong?
The fileName parameter is the path to the private key that should be used for signing the message. I'm not sure why you expect it to be at the root of the site or why it would be called dkim.txt, but I can almost guarantee that both assumptions are wrong.
A selector is used by the receiving client to locate the correct public key in the DNS records because it's possible for the same domain to have multiple keys that it uses for signing.
The AgentOrUserIdentifier corresponds to the i= parameter that you find in the DKIM-Signature header and specifies the user or agent that is taking responsibility for the signature.

How to get the desired octet in an IP address that includes port number?

If I have this kind of IP address 192.168.110.128 the way to get the desired octet is already answered here, but it's not applicable to the IP that includes port number like this 192.168.110.128:60423.
You can't parse this into an IPAddress object as port is not part of the IP address. It belongs to TCP/UDP.
Instead you will have to parse it into a URI and then get the host value of that, parse that into an IPAddress and there you go:
Dim address As String = "192.168.110.128:60423"
Dim ip = IPAddress.Parse(New Uri("http://" & address).Host)
Debug.WriteLine(ip.GetAddressBytes(3))

Getting IP address of my computer

How can I get the IP address of my computer (on which my application is running)
in vb.net
Thanks
Furqan
See System.Net.DNS.
Something like this should work:
Dim ips As IPAddress() = Dns.GetHostAddresses(Dns.GetHostName())
Dim index As Integer
For Each ip in ips
Console.WriteLine(ip)
Next ip
One way is to use System.Net.Dns.GetHostAddresses, passing it the empty string. Note that it will give you an array of addresses, this is because a host can have multiple addresses, one for each interface. A common example would be the loopback address (127.0.0.1) and one or more public facing IP addresses (eg 10.10.1.1). If your machine has a specific host name you can use that instead of the empty string.

determining country or language using url

I have written simple code to determine country location using the suffix on the url eg. .cn, .se, .br etc.
Does anyone have any ideas or even if its possible to determine country or location using urls that end in .com or .net?
I was just reading up on something and found online apps that can determine location/country from the ip so how would I determine the ip or look it up using the url? can I do this in .net?
OK
So I have the following code and I get an exception when the url is invalid or faulty, can anyone help me to catch the error and add the url to another list and continue with my loop.
Public Sub getIpAddress(ByVal querylist As ArrayList)
Dim IPList As New ArrayList
Dim badList As New ArrayList
Dim badHost As String
Try
For Each prod In querylist
Dim ipEntry As IPHostEntry = Dns.GetHostEntry(prod)
Dim IPAdd As IPAddress() = ipEntry.AddressList
IPList.Add(IPAdd.ToString)
Next
Catch ex As Exception
If ex.Message.Contains("No such host is known?") Then
End If
End Try
End Sub
You could easily use an IP-to-country mapping file such as this to build a little tool to infer location from the IP. I would also supplement this with the TLD of country specific domains such as .co.uk and .co.nz.
You can get the IP from the host name in java using something like:
InetAddress addr = InetAddress.getByName("www.bbc.co.uk");
Long ipNum = ipToInt(addr.getHostAddress());
You'll have to map to the equivalent in your language.
If it is .net or .com you cannot read the location from the url.
You can read the language of the browser, see http://www.west-wind.com/weblog/posts/334.aspx
You could also read the IP address of request, and then use a location service to find where the request came from.
Not from the URL, but from the IP, you can do this. There are publicly available files which map an IP range to a country.
Note that the location within a country is quite hard to get, as you can only know the address of an ISP, not of a Web site.
You can determine the country where the servers are located.
You can find out the location of the domain registrar.
But then, the company or a person behind the site may live in some third place.
Whose geolocation are you trying to find out?

How to display the ip address and port number in an text box that should be generated dynamically

Is there a way to display the system ip address and port number in a text box that is generated dynamically???
I want the system to put the ip address into a text box according to the machine.
Siddharth
Since you mentioned a text box, I can only postulate that you are talking about a web browser, and in that case 99.9% of the time you are talking about http and then 99.999% of the time a TCP connection. This means that your connection will have a 4-Tuple consisting of the source ip:port and the destination ip:port. In most cases the port numbers are fairly standard (80) for the destination (client).
Then you get into the very common issues of NAT and the like, so again I think you need to clarify what type of ip address you want. The publicly routable ip address is obtained server side and the LAN address will be obtained from the localhost.
For the more interesting case (publicly routable ip) I would just use a server side script (python, PHP, C, etc...) to read the incoming ip address and then use a little ajax to set the value of the text box. I did something similar for a project and it worked really well. Our client program was written in Python and C but this will give you an idea...
# Returns the client's public IP address (past any NATs)
def get_public_ip():
return urllib.urlopen('http://ddih.org/ip.php').read().strip()
I think something like set the inner html... from that webpage...
Hope this helps.
Your system does not have a port number. Port numbers are a software concept to differentiate different IP or UDP applications that might want to listen for connections on your IP address.
Also, it is quite possible to have more than one IP address. In fact, your system almost always has two if you count the loopback address (127.0.0.1). Even if you don't these days even many consumer PC's have multiple ethernet jacks.
You didn't say you were using Win32 so I don't know that it will be useful to you, but here's some code I wrote once that puts all local IP addresses (loopback excepted) into a an MFC CComboBox. It's a bit more C-ish than I'd like to see these days, but here it is.
size_t const Max_Expected_Addresses = 20; // Something rediculous
unsigned long IPADDRTBL_Size = sizeof(DWORD) + sizeof(MIB_IPADDRROW) * Max_Expected_Addresses;
PMIB_IPADDRTABLE IP_Address_Table = (PMIB_IPADDRTABLE) malloc (IPADDRTBL_Size);
if (GetIpAddrTable (IP_Address_Table, &IPADDRTBL_Size, TRUE) == NO_ERROR) {
for (DWORD i = 0; i < IP_Address_Table->dwNumEntries; i++) {
// Skip the loopback.
if (IP_Address_Table->table[i].dwAddr == 0x0100007f) continue;
if (m_IP_Address == "") m_IP_Address = String_Address(IP_Address_Table->table[i].dwAddr);
m_IP_Address_List.AddString (String_Address(IP_Address_Table->table[i].dwAddr));
};
}
m_IP_Address_List is an MFC control defined as a CComboBox which gets filled in by this snippet.
m_IP_Address is a CString tied to an MFC textbox control (IIRC) which I use to store the currently selected (or first found on startup) IP address.