Sign in to follow this  

NAT Punch How To?, need help in VB 2005

This topic is 4091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Ok I searched every where and cant reaaly find the code for nat punch, only the theory! Does any one knows how to do this in VB. net (2005)? im using System.Sockets class Thanks!

Share this post


Link to post
Share on other sites
:-(
I dont have C compiler, im running VB Epress Edition

I juess there is no vb implementation because VB socket class returns the port # that is original one and not the one that router assigned :-(

Is it possible to compile your C code to a dll and use it as service file to just get the addresses?

I havent used C since version 4 im even efraid to edit it when I get my compiler installed

Share this post


Link to post
Share on other sites
Quote:
VB socket class returns the port # that is original one and not the one that router assigned


Not true. The library will use the port that is in the packet it receives, when you call something like recvfrom(). However, if your traffic doesn't actually go through the router, then there will be no translation (such as when testing on a local net). Also, some routers will assign the same port as on the inside socket, if that port is available.

Quote:
old C compiler


Visual C++ Express Edition is a free download from Microsoft. Make sure you also download and install the Platform SDK to get all the Win32/WinSock programming goodness.

Share this post


Link to post
Share on other sites
ok yes thats what i assume and the way most packets work that it should have return adress stated rather then general assumed address. But my knowlage of socket calss in VB is like 1% i just found about it last week, and i have no experiance in network programing (thats if we exclude DB apps which are not really network programing)

As to C++ Extress Edition I didntk now it exist, but even if i download it, i will feal like im lost, i havent seen the C code for years...

Oh yea could it be that VB cant read the masked port #?

Share this post


Link to post
Share on other sites
No, VB is no different from any other language running on the system.

I'm assuming you're talking about VB.NET. If so, it uses the same class that you'd use in C# or C++/CLR. Just read the documentation for the class on MSDN; it's all in there.

System.Net.Sockets.Socket on MSDN.

Share this post


Link to post
Share on other sites
ok, i found the error in my code so now i am getting the real remote port BUT when im sending back something to that port i'm getting errro saying host no ready or dosnt accept the the packats.

Why that? Could that be due to TCP or soemthing else?

Share this post


Link to post
Share on other sites
Ok i did some testings and it dosnt make any sense for me... Here are my observations:

My Setup: Laptop w. IP 192.168.1.197 is behind a NAT device which in not UPnP.
Desctop is not on the nated subnet and is "before" the router, its IP is 192.168.101 and it functions as the server for me to find out the port of laptop to send back the socket in that was that I dont need to configure the routher. Oh router IP 192.168.1.1 (Will consider this as WAN address) Finally defult port for my app is 5124

So...
1) Laptop send to 127.0.0.1:5124 - same app says recived from 127.0.0.1:3315
Sending socket to 127.0.0.1:3315 => Got error no such host or EndPoint not ready

2)Latop sending to Desctop to 192.168.0.101:5124 ... on desctop recived from 192.168.0.100:50710
3) Desktop sending to Laptop on just recived port # to 192.168.0.100:50710 => No host, ok fine
4) Repeating step 3 but with standard port to 192.168.0.100:5124 => No Host

Conclusion: Dosnt work!

What can I do here?
Put my Laptop into DMZ?


Share this post


Link to post
Share on other sites
You will need to draw a diagram of your network. How come your desktop has 192.x addresses, when you claim it's outside the NAT router, for example? (and you forgot a dot in that address).

What is the external address of the NAT router, btw? Or is what you listed the external address? Then what is the internal address? It would be less confusing if you numbered your external (but still internal?) net using a different number range, like 10.x.

Last: when sending to 127.0.0.1, NAT is not involved. I believe your problem might be that you don't use htons() correctly on the port ID, perhaps?

Example network for testing NAT:


Internet <--> 20.30.40.50 [NAT Router A] 10.0.0.1
|
192.168.0.1 [NAT Router B] 10.0.0.2 <---> 10.0.0.3 [Your Desktop]
|
192.168.0.2 [Your Laptop]


20.30.40.50 is actually your ISP-assigned public IP address.

Share this post


Link to post
Share on other sites
whats "htons()"?

Your diagram is what i have! Also just tried to open firewall but that didnt help.

Here is diagram of what i have with my IPs:

Internet < - -> 20.30.40.50 [NAT Router A] 192.168.0.1/24
|
192.168.1.1/24 [NAT Router B] 192.168.0.100 < - -> 192.168.0.101 [Your Desktop]
|
192.168.1.197 [Your Laptop]

Share this post


Link to post
Share on other sites
being a dummy i did an very stupid mistake in my code... :-(
I did listen for standelone port while I supose (I issume to listen to an assigned port) well fixed that byt still no luck...

Here is the Nat state of my outer

Source Port Destination Port Protocol Packets Bytes TTL
192.168.1.197 4265 192.168.0.101 5124 tcp 12 520 3:52

So i have sent socket from laptop and it reached the dectop. My router says that socket port is 4265 but when I send packet on that port i got nothing... i even recomplile my app and im setting the the port manually to what the router says, caz for some reason my PP GIVES ME WRON PORT %)

i will atach the code of my app in edit... gota swich to dectop

Edit:
Heere we go with the source code:
Listener:

Imports System.net
Public Class Form1
Private _ClientSocket As Sockets.Socket
Private _HostSocket As Sockets.Socket
Friend _TcpListener As New Sockets.TcpListener(Dns.GetHostEntry(Dns.GetHostName).AddressList(0), 5124)


Private Sub btnListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListen.Click
CType(_TcpListener.LocalEndpoint, IPEndPoint).Port = txtPort.Text
txtSetPort.Text = CType(_TcpListener.LocalEndpoint, IPEndPoint).Port.ToString
Timer.Enabled = True
Timer.Start()
End Sub

Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
Try
_TcpListener.Start()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
If _TcpListener.Pending = True Then
_HostSocket = _TcpListener.AcceptSocket
If _HostSocket.Poll(1, Sockets.SelectMode.SelectRead) Then
Dim numBytes As Int32 = _HostSocket.Available
Dim Data(numBytes) As Byte
_HostSocket.Receive(Data)
HostMessagesReceivedTextBox.Text &= System.Text.ASCIIEncoding.ASCII.GetString(Data)
HostMessagesReceivedTextBox.Text &= ControlChars.CrLf
'Timer Stop
End If
Else
'MessageBox.Show("I dont want the conectiosn, the status is not PENDING")
End If
End Sub

Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Timer.Stop()
Timer.Enabled = False
Try
_TcpListener.Stop()
Catch ex As Exception

End Try
txtSetPort.Text = ""
End Sub
End Class


And here is how i get port numbers that are displayed incorectly

ReplyIP = IPAddress.Parse(CType(_HostSocket.RemoteEndPoint, IPEndPoint).Address.ToString)
ReplyPort = IPAddress.Parse(CType(_HostSocket.RemoteEndPoint, IPEndPoint).Port.ToString)

Share this post


Link to post
Share on other sites

This topic is 4091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this