组播tcpdump可以抓到,但APP收不到,求策略或解释

piggy_xrh 2014-10-27 04:37:56
如题:
一个组播程序,绑定到了224.188.188.188.30583上, tcpdump可以抓到组播包数据,但应用层且接收不到.

部分代码:
#define GROUP "224.188.188.188"
#define GROUP_SRC_PORT 30583

//初始化
int loop = 0, rcv64K = 1024 * 64;
struct sockaddr_in addr;

memset(&g_ds, 0, sizeof(g_ds));
g_ds.socket = -1;
g_ds.group.sin_family = AF_INET;
g_ds.group.sin_addr.s_addr = inet_addr(GROUP);
g_ds.group.sin_port = htons(GROUP_DST_PORT);

memcpy(&addr, &g_ds.group, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(GROUP_SRC_PORT);

/* Create the discovery socket and join the group */
if ((-1 == (g_ds.socket = socket(AF_INET, SOCK_DGRAM, 0))) ||
(setsockopt(g_ds.socket, SOL_SOCKET, SO_RCVBUF, &rcv64K, sizeof(rcv64K)),
bind(g_ds.socket, (struct sockaddr *)&addr, sizeof(addr))) ||
SCAPI_ds_group_enable(g_ds.socket, g_ds.group.sin_addr.s_addr, 1))
goto err;
setsockopt(g_ds.socket, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));

//组播加入/禁止
int SCAPI_ds_group_enable(int s, in_addr_t group, int enable) {
struct ip_mreqn req;
struct if_nameindex *if_ni, *ifc;

req.imr_multiaddr.s_addr = group;
if ((if_ni = if_nameindex())) {
int count = 0;
struct ifreq ifr;

for (ifc = if_ni; ifc->if_index || ifc->if_name; ++ ifc) {
/*We skip the loopback interface */
if (!strncmp("lo", ifc->if_name, 2))
continue;

strcpy(ifr.ifr_name, ifc->if_name);
if (ioctl(s, SIOCGIFADDR, &ifr, sizeof(ifr))) {
fprintf(stderr, "@%s-ioctl error:%s <%s>\n",
__FUNCTION__, strerror(errno), ifc->if_name);
continue;
}

printf("enable group:%s <%d>\n", ifc->if_name, enable);
req.imr_ifindex = ifc->if_index;
req.imr_address = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
if (setsockopt(s, IPPROTO_IP, enable ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &req, sizeof(req))) {
fprintf(stderr, "@%s-setsockopt(IP_ADD_MEMBERSHIP) error:%s <%s>\n",
__FUNCTION__, strerror(errno), ifc->if_name);
++ count;
}
if_freenameindex(if_ni);

if (count > 0)
return 0;
}

return setsockopt(s, IPPROTO_IP, enable ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &req, sizeof(req));
}

//程序启动接收
for (;;) {
printf("recevive.\n");
int ret = recvfrom(g_ds.socket, buff, sizeof(buff), 0, NULL, NULL);
printf("ok.\n");
if (g_ds.quit)
break;
........
}

##############################程序启动输出
enable group:eth0 <1>
enable group:eth1 <1>
recevive. <一直被阻塞住>


#######################环境 vmware - ubuntu
Ubuntu 10.04.4 LTS (/etc/issue)

Linux version 2.6.32-38-generic (buildd@zirconium) (gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) ) #83-Ubuntu SMP Wed Jan 4 11:13:04 UTC 2012 (/proc/version)

eth0 为bridge模式 (后续的组播数据都来自这个网口)
eth1 为nat模式
##############################网卡信息
eth0 Link encap:Ethernet HWaddr 00:0c:29:c9:96:13
inet addr:192.168.0.237 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fec9:9613/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:137592073 errors:0 dropped:0 overruns:0 frame:0
TX packets:669651 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:14277292 (14.2 MB) TX bytes:971026697 (971.0 MB)
Interrupt:19 Base address:0x2024

eth1 Link encap:Ethernet HWaddr 00:0c:29:c9:96:1d
inet addr:192.168.203.131 Bcast:192.168.203.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fec9:961d/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:41247 errors:0 dropped:0 overruns:0 frame:0
TX packets:26655 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:49735517 (49.7 MB) TX bytes:3237922 (3.2 MB)
Interrupt:19 Base address:0x20a4

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:94666 errors:0 dropped:0 overruns:0 frame:0
TX packets:94666 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5682680 (5.6 MB) TX bytes:5682680 (5.6 MB)

###################################route 信息

Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.203.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
0.0.0.0 192.168.203.2 0.0.0.0 UG 0 0 0 eth1


###################################netstat -gn
IPv6/IPv4 Group Memberships
Interface RefCnt Group
--------------- ------ ---------------------
lo 1 224.0.0.1
eth0 1 224.188.188.188 <eth0上已经加入组播>
eth0 1 224.0.0.251
eth0 1 224.0.0.1
eth1 1 224.188.188.188 <eth1也加入组播>
eth1 1 224.0.0.251
eth1 1 224.0.0.1
lo 1 ff02::1
eth0 1 ff02::1:ffc9:9613
eth0 1 ff02::1
eth1 1 ff02::1:ffc9:961d
eth1 1 ff02::1

######################################netstat -aun
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 0.0.0.0:40126 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:40391 0.0.0.0:*
udp 0 0 0.0.0.0:980 0.0.0.0:*
udp 0 0 0.0.0.0:43753 0.0.0.0:*
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp 0 0 0.0.0.0:111 0.0.0.0:*
udp 0 0 224.188.188.188:30583 0.0.0.0:* //已经在224:188:188:188:30583上监听
udp 0 0 0.0.0.0:2049 0.0.0.0:*
udp 0 0 0.0.0.0:41774 0.0.0.0:*


###########################tcpdump 监听 (一直捕获到组播数据,但app就是收不到)
tcpdump -ieth0 host 224.188.188.188 and udp -n
01:33:10.030728 IP 172.18.27.51.39048 > 224.188.188.188.30583: UDP, length 256
01:33:25.090642 IP 172.18.27.51.39048 > 224.188.188.188.34952: UDP, length 256
01:33:25.091464 IP 172.18.27.51.39048 > 224.188.188.188.30583: UDP, length 256
01:33:40.150817 IP 172.18.27.51.39048 > 224.188.188.188.34952: UDP, length 256
01:33:40.151563 IP 172.18.27.51.39048 > 224.188.188.188.30583: UDP, length 256
01:33:55.211561 IP 172.18.27.51.39048 > 224.188.188.188.34952: UDP, length 256
01:33:55.212332 IP 172.18.27.51.39048 > 224.188.188.188.30583: UDP, length 256
01:34:10.288207 IP 172.18.27.51.39048 > 224.188.188.188.34952: UDP, length 256
01:34:10.289066 IP 172.18.27.51.39048 > 224.188.188.188.30583: UDP, length 256
......


如果执行ifconfig eth0:1 172.18.6.11 为eth0网口添加与172.18同网段后, APP马上打印
ok (即收到了组播数据!), 看起来好像内核过滤了172.18.27.51,发现本地ip没有和其在同一网段?
...全文
1450 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
泽坤 2016-01-12
  • 打赏
  • 举报
回复
跨网段推送组播就会碰到这种问题(tcpdump可以抓取到包,但是应用层收不到)。如果交换机支持igmp v2或v3,需要用命令把两个网段加入到同一个igmp组播组。
david-yue 2014-12-28
  • 打赏
  • 举报
回复
我碰到个问题,ifconfig中可以看到计数,但是tcpdump也是抓不到包,后来才是道是跟驱动的实现由关系,没有将报文上送到协议栈
HBack 2014-12-28
  • 打赏
  • 举报
回复
哦哦哦来了,我以为你要说tcpdump arp
阿良良木月火 2014-12-28
  • 打赏
  • 举报
回复
来了来了,结贴吧
piggy_xrh 2014-12-27
  • 打赏
  • 举报
回复
结贴散分。。。。还没人来

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧