OS X中的原始套接字sendto()故障 [英] Raw socket sendto() failure in OS X

查看:183
本文介绍了OS X中的原始套接字sendto()故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开OS X的原始套接字时,构造自己的udp数据包(标头和数据),然后调用sendto(),我得到错误无效参数".这是来自网站 http://www.tenouk.com/的示例程序"rawudp.c".演示此问题的Module43a.html .该程序(在添加了字符串和stdlib #includes之后)在Fedora 10下运行,但在OS X下失败并显示"Invalid Argument".有人可以建议为什么在OS X上失败吗?我看了看,看了看sendto()调用,但是所有参数看起来都不错.我正在以root用户身份运行代码,等等.也许有一个内核设置可以阻止uid 0可执行文件通过OS X Snow Leopard中的原始套接字发送数据包吗?谢谢.

When I open a raw socket is OS X, construct my own udp packet (headers and data), and call sendto(), I get the error "Invalid Argument". Here is a sample program "rawudp.c" from the web site http://www.tenouk.com/Module43a.html that demonstrates this problem. The program (after adding string and stdlib #includes) runs under Fedora 10 but fails with "Invalid Argument" under OS X. Can anyone suggest why this fails in OS X? I have looked and looked and looked at the sendto() call, but all the parameters look good. I'm running the code as root, etc. Is there perhaps a kernel setting that prevents even uid 0 executables from sending packets through raw sockets in OS X Snow Leopard? Thanks.

推荐答案

我可能已经解决了这个奥秘. 我也设计了一个原始套接字示例,该示例在Linux上可以正常运行,但是却收到了"Invalid Argument"(无效参数) OS X 10.6上出现错误.
我遇到了此页面" FreeBSD套接字错误和特点"在谷歌搜索答案时.它说:

I may have solved the mystery. I too crafted a raw socket example, which runs fine on Linux, but got "Invalid Argument" error on OS X 10.6.
I encountered this page "FreeBSD socket bugs and peculiarities" while googling for an answer. And it says:

写入RAW套接字

Writing to RAW sockets

-ip_len和ip_off必须按主机字节顺序

所以我替换

ip.ip_len = htons(len);

使用

ip.ip_len = len;

在OS X上.并且可以正常工作,无论它多么奇怪.

on OS X. And it works, however strange it is.

这篇关于OS X中的原始套接字sendto()故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆