URN Logo
UNIX Resources » Linux » China Linux Forum » 网络管理技术 » 17 » 请教socket数据包来源接口的判别问题
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
请教socket数据包来源接口的判别问题
 
 
Subject: 请教socket数据包来源接口的判别问题
Author: auther_bin    Posted: 2004-01-07 10:00    Length: 351 byte(s)
[Original] [Print] [Top]
一般情况下,一旦数据包被socket接收并保存在一个缓冲区后,他的物理接口信息就没有了,
也就是说无法判断物理接口的来源,而根据数据包里的地址信息判断是一个非常不准确的方法,
比如对于arp数据包。否则,就只能对每一个物理接口分配一个socket,这样就不会混淆了,
但是,如果只开一个socket监听所有接口,怎样才能区分收到数据包的物理来源呢?
[Original] [Print] [Top]
Subject: Re: 请教socket数据包来源接口的判别问题
Author: yhuang    Posted: 2004-01-28 12:20    Length: 2,309 byte(s)
[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 »
网络配置问题
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 02:16:32, cost 0.043798923492432 ms.