|
|
|
|
 请教socket数据包来源接口的判别问题 - auther_bin [ 2004-01-07 10:00 | 351 byte(s)]
 Re: 请教socket数据包来源接口的判别问题 - yhuang [ 2004-01-28 12:20 | 2,309 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
一般情况下,一旦数据包被socket接收并保存在一个缓冲区后,他的物理接口信息就没有了,
也就是说无法判断物理接口的来源,而根据数据包里的地址信息判断是一个非常不准确的方法,
比如对于arp数据包。否则,就只能对每一个物理接口分配一个socket,这样就不会混淆了,
但是,如果只开一个socket监听所有接口,怎样才能区分收到数据包的物理来源呢?
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
在Linux系统可以使用recvmsg调用来接收报文和它的IP_PKTINFO类型的控制信息,控制信息中有接收接口的索引号。具体做法是:
1、使用setsockopt(socket_fd, SOL_IP, IP_PKTINFO, &on);选择在这个socket上接收控制信息。
2、用recvmsg接收报文,其中的数据msg是要接收的报文的内容,控制msg中有一种类型位IP_PKTINFO的,里面有接口的索引号。
3、如果还不清楚,请man socket; man setsockopt; man recvmsg; man 7 ip;下面说明就是man 7 ip出来的。
IP_PKTINFO
Pass an IP_PKTINFO ancillary message that contains
a pktinfo structure that supplies some information
about the incoming packet. This only works for
datagram oriented sockets. The argument is a flag
that tells the socket whether the IP_PKTINFO message
should be passed or not. The message itself
can only be sent/retrieved as control message with
a packet using recvmsg(2) or sendmsg(2).
struct in_pktinfo {
unsigned int ipi_ifindex; /* Interface index */
struct in_addr ipi_spec_dst; /* Local address */
struct in_addr ipi_addr; /* Header Destination address */
};
ipi_ifindex is the unique index of the interface
the packet was received on. ipi_spec_dst is the
local address of the packet and ipi_addr is the
destination address in the packet header. If
IP_PKTINFO is passed to sendmsg(2) then the outgoing
packet will be sent over the interface specified in
ipi_ifindex with the destination address set to ipi_spec_dst
If enabled the IP_TOS ancillary message is passed
with incoming packets. It contains a byte which
specifies the Type of Service/Precedence field of
the packet header. Expects a boolean integer flag.
|
|
|
[Original]
[Print]
[Top]
|
|
« Previous thread
Linux 下如何上网? |
网络管理技术
第17页 |
Next thread »
网络配置问题 |
|