I am trying to initiate a TCP Three-Way-Handshake in C. However, it occurred to me that connect may already be establishing such a connection or in some way interfering. Does connect automatically establish a TCP connection whenever the socket it's called on has the IPPROTO_TCP option set?
Connect function in raw socket?
3k views Asked by Arthur James AtThere are 2 answers
                        
                            
                        
                        
                            On
                            
                            
                                                    
                    
                Per MSDN documentation:
Once an application creates a socket of type
SOCK_RAW, this socket may be used to send and receive data. All packets sent or received on a socket of typeSOCK_RAWare treated as datagrams on an unconnected socket.The following rules apply to the operations over
SOCK_RAWsockets:...
Received datagrams are copied into all
SOCK_RAWsockets that satisfy the following conditions:...
- If a foreign address is defined for the socket, it should correspond to the source address as specified in the IP header of the received datagram. An application may specify the foreign IP address by calling the
 connectorWSAConnectfunction. If no foreign IP address is specified for the socket, the datagrams are copied into the socket regardless of the source IP address in the IP header of the received datagram.
This implies that RAW sockets are essentially connection-less sockets, and thus connect() does not perform a 3-way TCP handshake on a RAW socket. It simply associates a remote address with the socket, similar to how connect() works with a UDP (SOCK_DGRAM) socket.
More importantly:
Limitations on Raw Sockets
...
TCP data cannot be sent over raw sockets. ...
A call to the
bindfunction with a raw socket for theIPPROTO_TCPprotocol is not allowed.
So you can't use IPPROTO_TCP with a RAW socket to begin with, so the matter of connect() behavior on a RAW TCP socket is moot. IPPROTO_TCP can only be used with a real TCP (SOCK_STREAM) socket:
IPPROTO_TCP
6The Transmission Control Protocol (TCP). This is a possible value when the
afparameter isAF_INETorAF_INET6and thetypeparameter isSOCK_STREAM.
Yes,
IPPROTO_TCPcreates TCP socket. To use raw socket, you need to pass SOCK_RAW as second argument to thesocketfunction.