C# Raw Socket Issues starting TCP Connections

B

banduraj

I am working on starting and managing TCP connections manually. I build
the IP headers and TCP packets manually and send them on my own. The
problems I run into seems to be related to the Sockets. Maybe someone
can help me out.

1) I can build the packets and put them out fine, however, I can only
read incomming data if I use IOControl and set the socket SIO_RCVALL.
But doing this seems to apply to other sockets I maybe running in a
diffrent thread.

2) The biggest problem I have seems to be starting a TCP connection. If
I send a SYN, I get a SYN/ACK or SYN/RST like I am suppose to, but for
some reason the Socket replys with a RST with out me doing anything.

Here is how I am building the socket and sending the data...


Socket remote = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
ProtocolType.IP);
remote.Bind(this.l_ipep);
remote.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, this.timeout_time);
remote.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.HeaderIncluded, 1);
int ret = remote.IOControl(SIO_RCVALL, oin, oout);

if (BitConverter.ToInt32(oout, 0) != 0) {
return error;
}

....

this.remote.SendTo(ip_packet.get_packet(), this.r_ipep);

What am I doing wrong??? Why does the socket send a RST without me
doing anything? Why can't I read data without setting the socket in
promiscious mode. And why is it that when I do set the socket to
promiscious mode that it seems to apply to other sockets running in
diffrent threads? Thanks.
 
B

Bernd

Hi,
just in case you still have this problem.

here is an explanation:

Since you are using raw sockets (SOCK_RAW) and not TCP/Stream sockets
(SOCK_STREAM) the TCP stack has no information about what you are doing
at program level. And since the IP_HDRINCL allows you to build any type
of IP packet and send it along with the data, you can build a SYN packet
and send it to the TCP server program which is actively listening. But
the point is that the SYN packet is being sent from your program and not
the stack. In other words the TCP stack of your machine has no idea how
of sending the SYN packet.

On the other side the SYN packet is received by the stack at the remote
machine and not exactly by the program. As with the case of the arrival
of any SYN packet, the stack at the remote machine responds with a
SYN/ACK packet. This packet is now received by the TCP stack of your
machine. In other words, the incoming TCP packet (SYN/ACK) will be
processed by the stack. Since it has no information of the previous sent
SYN packet, it responds with a RST packet, as in the case of any
improper or unacceptable packet for a connection.
..
..

full article can be found at
http://www.codeguru.com/forum/showthread.php?t=320739

regards,
b
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top