原始套接字中的连接功能?
Connect function in raw socket?
我正尝试在 C 中启动 TCP 三向握手。但是,我想到 connect
可能已经在建立这样的连接或以某种方式进行干扰。只要调用它的套接字设置了 IPPROTO_TCP
选项,connect
是否会自动建立 TCP 连接?
是的,IPPROTO_TCP
创建了 TCP 套接字。要使用原始套接字,您需要将 SOCK_RAW 作为第二个参数传递给 socket
函数。
根据 MSDN 文档:
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 type SOCK_RAW
are treated as datagrams on an unconnected socket.
The following rules apply to the operations over SOCK_RAW
sockets:
...
Received datagrams are copied into all SOCK_RAW
sockets 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
connect
or WSAConnect
function. 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.
这意味着 RAW 套接字本质上是无连接套接字,因此 connect()
不会 在 RAW 套接字上执行 3 次 TCP 握手。它只是将远程地址与套接字相关联,类似于 connect()
与 UDP (SOCK_DGRAM
) 套接字的工作方式。
更重要的是:
Limitations on Raw Sockets
...
TCP data cannot be sent over raw sockets.
...
A call to the bind
function with a raw socket for the IPPROTO_TCP
protocol is not allowed.
所以您不能将 IPPROTO_TCP
与 RAW 套接字一起使用,因此 connect()
在 RAW TCP 套接字上的行为问题没有实际意义。 IPPROTO_TCP
can only be used with a real TCP (SOCK_STREAM
) socket:
IPPROTO_TCP
6
The Transmission Control Protocol (TCP). This is a possible value when the af
parameter is AF_INET
or AF_INET6
and the type
parameter is SOCK_STREAM
.
我正尝试在 C 中启动 TCP 三向握手。但是,我想到 connect
可能已经在建立这样的连接或以某种方式进行干扰。只要调用它的套接字设置了 IPPROTO_TCP
选项,connect
是否会自动建立 TCP 连接?
是的,IPPROTO_TCP
创建了 TCP 套接字。要使用原始套接字,您需要将 SOCK_RAW 作为第二个参数传递给 socket
函数。
根据 MSDN 文档:
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_RAW
are treated as datagrams on an unconnected socket.The following rules apply to the operations over
SOCK_RAW
sockets:...
Received datagrams are copied into all
SOCK_RAW
sockets 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
connect
orWSAConnect
function. 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.
这意味着 RAW 套接字本质上是无连接套接字,因此 connect()
不会 在 RAW 套接字上执行 3 次 TCP 握手。它只是将远程地址与套接字相关联,类似于 connect()
与 UDP (SOCK_DGRAM
) 套接字的工作方式。
更重要的是:
Limitations on Raw Sockets
...
TCP data cannot be sent over raw sockets. ...
A call to the
bind
function with a raw socket for theIPPROTO_TCP
protocol is not allowed.
所以您不能将 IPPROTO_TCP
与 RAW 套接字一起使用,因此 connect()
在 RAW TCP 套接字上的行为问题没有实际意义。 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
af
parameter isAF_INET
orAF_INET6
and thetype
parameter isSOCK_STREAM
.