473,499 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange problem receiving packets on a socket

I have the following code to send a packet to a remote socket and
receive a response in return:

System.Net.Sockets.Socket locSocket = new System.Net.Sockets.Socket
(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
System.Net.Sockets.Socket remSocket = new System.Net.Sockets.Socket
(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
locSocket.Bind (new IPEndPoint (locIP, 2126));

/*

Code to create a packet of data (byte[] datagram) goes here

*/

byte[] receivedPacket = new byte[96];
Console.WriteLine ("Sending packet...");
remoteSocket.SendTo (datagram, remoteEndPoint);
Console.WriteLine ("Packet sent");
locSocket.Receive (receivedPacket);

So far, it seems to work for external hosts but when I try to send a
packet to a port on my computer (say, port 23 for example) I seem to
receive the original packet that I sent to port 2126. I've checked
with wireshark and the packet goes to port 23 as it should, whereupon
a response is sent to port 2126 as expected (I'm communicating with
the network layer and wireshark shows I get an ACK/RST as expected I
have no server listening on port 23).

Is there something I'm doing wrong? Because of the nature of the
application, I'm unable to use connection oriented transmission in
this part of the program.

Apr 10 '07 #1
6 4618
On Tue, 10 Apr 2007 08:41:33 -0700, White Spirit
<ws*****@homechoice.co.ukwrote:
[...]
Is there something I'm doing wrong? Because of the nature of the
application, I'm unable to use connection oriented transmission in
this part of the program.
I don't know exactly what you're doing wrong, as it's not really even
clear to me what the behavior you're trying to describe is. For example,
you write "So far, it seems to work for external hosts but when I try to
send a packet to a port on my computer (say, port 23 for example) I seem
to receive the original packet that I sent to port 2126". You seem to be
saying that you send the datagram to port 23, but then you say that you
sent it to port 2126. Did you send it to port 23 or port 2126?

You also write "the packet goes to port 23 as it should, whereupon a
response is sent to port 2126 as expected". Reading this statement it
seems as though data in both directions is going exactly where you expect
it to. But if everything's fine, then what is your question?

In other words, I assume you have some problem with the behavior, but you
haven't done very well explaining the problem in an unambiguous way.

All that said however, some things jump out at me in your code...

You don't show us the initialization of "remoteEndPoint", but assuming
this the address of "remoteSocket", then you are sending the datagram from
that socket to itself. The "locSocket" doesn't get involved.

I can't comment on the use of raw sockets with TCP, but it's my
recollection that there is some limitation on the use of raw sockets with
one or the other of TCP and UDP. I just can't recall off the top of my
head where that limitation is. It doesn't sound like this is what you're
running into, but be aware that you don't get carte blanche using raw
sockets under Windows, even running with administrator rights.

I don't think this should affect your code, but there's usually no reason
to use an explicit IP address when binding a socket. Just specify
IPAddress.Any and your socket will bind to whatever the current IP address
(or addresses) exist on the local computer.

Pete
Apr 10 '07 #2
On 10 Apr, 16:41, "White Spirit" <wspi...@homechoice.co.ukwrote:
I have the following code to send a packet to a remote socket and
receive a response in return:

System.Net.Sockets.Socket locSocket = new System.Net.Sockets.Socket
(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
System.Net.Sockets.Socket remSocket = new System.Net.Sockets.Socket
(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
locSocket.Bind (new IPEndPoint (locIP, 2126));

/*

Code to create a packet of data (byte[] datagram) goes here

*/

byte[] receivedPacket = new byte[96];
Console.WriteLine ("Sending packet...");
remoteSocket.SendTo (datagram, remoteEndPoint);
Console.WriteLine ("Packet sent");
locSocket.Receive (receivedPacket);

So far, it seems to work for external hosts but when I try to send a
packet to a port on my computer (say, port 23 for example) I seem to
receive the original packet that I sent to port 2126. I've checked
with wireshark and the packet goes to port 23 as it should, whereupon
a response is sent to port 2126 as expected (I'm communicating with
the network layer and wireshark shows I get an ACK/RST as expected I
have no server listening on port 23).

Is there something I'm doing wrong? Because of the nature of the
application, I'm unable to use connection oriented transmission in
this part of the program.
I think you'll need to submit a small runnable bit of code. You're
using SocketType.Raw, so it could be whatever's in the data causing
chaos. Seeing that ACK and RST are set in a packet doesn't mean it
will make it up the stack. I assume wireshark operates at layer 2 (had
to look it up, didn't realise it was Ethereal :)), as it can operate
on ethernet frames, so it might not be making it to layer 3 or
beyond.

Hope that helps.

Apr 10 '07 #3
Hi,

Basically, I'm sending a SYN packet, in this example to port 80 of a
machine using a raw socket. The header of the packet identifies my
source port as port 2126 together with my IP address. When I use a
remote host such as one of Google's IP addresses in the example below,
the SYN/ACK from Google port 80 is received by my machine on port
2126, as expected. When I try to send a SYN packet to port 80 on my
own computer using any of the available IP addresses (127.0.0.1,
192.168.100.102 and 192.168.0.2), locSocket on port 2126 receives the
packet I sent to myself intended for port 80. In other words, I send
a packet to port 80 on my machine and it shows up on port 2126 in my
application. However, when I use Wireshark to see what is happening,
I don't see any discrepancy in the network layers - wireshark
identifies my SYN packet going from port 2126 to port 80 and the ACK/
RST packet from port 80 to port 2126 in response.

I hope that explains it a bit better. I've attached code below
showing what I'm doing with the remote endpoint. The code I've
skipped deals with building a TCP SYN packet and computing the
checksum.

IPAddress remoteIP = IPAddress.Parse ("64.233.187.99");
IPEndPoint remoteEndPoint = new IPEndPoint (remoteIP, port);

System.Net.Sockets.Socket locSocket = new System.Net.Sockets.Socket
(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
System.Net.Sockets.Socket remSocket = new System.Net.Sockets.Socket
(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
locSocket.Bind (new IPEndPoint (locIP, 2126));

/*

Code to create a packet of data (byte[] datagram) goes here

*/

byte[] receivedPacket = new byte[96];
Console.WriteLine ("Sending packet...");
remSocket.SendTo (datagram, remoteEndPoint);
Console.WriteLine ("Packet sent");
locSocket.Receive (receivedPacket);

Apr 10 '07 #4
On 10 Apr, 21:00, "White Spirit" <wspi...@homechoice.co.ukwrote:
Hi,

Basically, I'm sending a SYN packet, in this example to port 80 of a
machine using a raw socket. The header of the packet identifies my
source port as port 2126 together with my IP address. When I use a
remote host such as one of Google's IP addresses in the example below,
the SYN/ACK from Google port 80 is received by my machine on port
2126, as expected. When I try to send a SYN packet to port 80 on my
own computer using any of the available IP addresses (127.0.0.1,
192.168.100.102 and 192.168.0.2), locSocket on port 2126 receives the
packet I sent to myself intended for port 80. In other words, I send
a packet to port 80 on my machine and it shows up on port 2126 in my
application. However, when I use Wireshark to see what is happening,
I don't see any discrepancy in the network layers - wireshark
identifies my SYN packet going from port 2126 to port 80 and the ACK/
RST packet from port 80 to port 2126 in response.

I hope that explains it a bit better. I've attached code below
showing what I'm doing with the remote endpoint. The code I've
skipped deals with building a TCP SYN packet and computing the
checksum.

IPAddress remoteIP = IPAddress.Parse ("64.233.187.99");
IPEndPoint remoteEndPoint = new IPEndPoint (remoteIP, port);

System.Net.Sockets.Socket locSocket = new System.Net.Sockets.Socket
(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
System.Net.Sockets.Socket remSocket = new System.Net.Sockets.Socket
(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
locSocket.Bind (new IPEndPoint (locIP, 2126));

/*

Code to create a packet of data (byte[] datagram) goes here

*/

byte[] receivedPacket = new byte[96];
Console.WriteLine ("Sending packet...");
remSocket.SendTo (datagram, remoteEndPoint);
Console.WriteLine ("Packet sent");
locSocket.Receive (receivedPacket);
Might have an answer here:

http://www.codeguru.com/forum/showthread.php?t=320739

So it seems your code is fine :)

Apr 10 '07 #5
On Apr 10, 10:49 pm, "DeveloperX" <nntp...@operamail.comwrote:
Might have an answer here:
http://www.codeguru.com/forum/showthread.php?t=320739
So it seems your code is fine :)
The problem is that I send a packet from port 2128 to port 80, try to
receive a packet on port 2128 in response but instead of receiving the
RST/ACK that gets sent *from* port 80, I receive a copy of the packet
I originally sent *to* port 80. I'm aware of the difference between
TCP handshaking on the application and the transport layers - indeed,
I'm relying on it to initiate a half-open connection for a port
scanning application. I just don't understand why my application
behaves as expected when I try to scan a server half way across the
globe but when I try to scan my own machine the packets aren't making
their way to my application. The RST/ACK packets I mentioned were
just a specific instance - if a port is open and I should get a SYN
back from port 80 I still get the original packet that I sent instead.
Apr 10 '07 #6
On 10 Apr, 23:24, "White Spirit" <wspi...@homechoice.co.ukwrote:
On Apr 10, 10:49 pm, "DeveloperX" <nntp...@operamail.comwrote:
Might have an answer here:
http://www.codeguru.com/forum/showthread.php?t=320739
So it seems your code is fine :)

The problem is that I send a packet from port 2128 to port 80, try to
receive a packet on port 2128 in response but instead of receiving the
RST/ACK that gets sent *from* port 80, I receive a copy of the packet
I originally sent *to* port 80. I'm aware of the difference between
TCP handshaking on the application and the transport layers - indeed,
I'm relying on it to initiate a half-open connection for a port
scanning application. I just don't understand why my application
behaves as expected when I try to scan a server half way across the
globe but when I try to scan my own machine the packets aren't making
their way to my application. The RST/ACK packets I mentioned were
just a specific instance - if a port is open and I should get a SYN
back from port 80 I still get the original packet that I sent instead.
I think without a small program that shows the issue it's going to be
nigh on impossible to tell. The CodeGuru post was specifically of
interest in your case because it shows why a hand made SYN doesn't
work on the local machine but will on remote machines. The behaviour
described is a little different to what you're seeing but close enough
to be pertinent. Sorry.

Apr 11 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7784
by: Stephan Steiner | last post by:
Hi I have a small program listening to UDP broadcast datagrams that are periodically sent out. It will stop listening for a certain period if either a sufficient number of packets has been...
4
7351
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that...
10
12165
by: Jim H | last post by:
I have a UDP socket that sends out a request on a multicast socket and waits for a response. This client is not listening on a multicast IP but the local IP. The server (UNIX) responds to the...
2
4732
by: tantiboh | last post by:
I'm not a new programmer, but this one's got me stymied; hopefully it's a fairly trivial problem. I'm using a socket connection to receive communication from a server. Normally, the entire message...
0
1146
by: Piyush | last post by:
Hi, As part of an application I'd like to capture Icmpv6 packets thrown when an application is not listening on a given port. I.e. I send a packet to another host on a port and listen for Icmp...
0
1055
by: Uma - Chellasoft | last post by:
Hai, I am working with UDP sockets in VB.Net. I am receiving UDP packets specified for a particular port from entire network. For restriction of packets from a single host, I am defining an...
5
5497
by: jaco.versfeld | last post by:
Hi There, I have a basic TCP client and TCP server in C++. The TCP client connects to the server, and after a setup phase starts to transmit a file to the TCP server using multiple packets...
7
3160
by: darthghandi | last post by:
I am having mixed results with asynchronous socket receives. Sometimes I get the right information back from the buffer, other times I get some of the data that should be in the buffer printed out...
0
3546
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
0
7006
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7385
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.