c - Mac OS X 上的奇怪 RAW 套接字

标签 c macos sockets packet-sniffers sniffing

当我在我的 Mac OS X 上运行一个用 C 编码的简单数据包嗅探器时,我根本没有输出,这是一件奇怪的事情!有人可以帮助我了解发生了什么。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(void) {
   int i, recv_length, sockfd;

   u_char buffer[9000];

   if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_TCP)) == -1) {
        printf("Socket failed!!\n");

        return -1;
   }

   for(i=0; i < 3; i++) {
      recv_length = recv(sockfd, buffer, 8000, 0);
      printf("Got some bytes : %d\n", recv_length);
   }

   return 0;
}

我编译它并在我的盒子上运行它,但没有任何进展:

MacOsxBox:Desktop evariste$sudo ./simpleSniffer

感谢您的帮助。

最佳答案

这不适用于 *BSD(包括 OSX/Darwin)。看调查here了解更多详情:

b. FreeBSD
**********

FreeBSD takes another approach. It *never* passes TCP or UDP packets to raw
sockets. Such packets need to be read directly at the datalink layer by using
libraries like libpcap or the bpf API. It also *never* passes any fragmented 
datagram. Each datagram has to be completeley reassembled before it is passed
to a raw socket.
FreeBSD passes to a raw socket:
    a) every IP datagram with a protocol field that is not registered in
    the kernel
    b) all IGMP packets after kernel finishes processing them
    c) all ICMP packets (except echo request, timestamp request and address
    mask request) after kernel finishes processes them

故事的寓意:为此使用libpcap。它会让你的生活更轻松。 (如果您使用 MacPorts,请执行 sudo port install libpcap。)

关于c - Mac OS X 上的奇怪 RAW 套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6878603/

相关文章:

c - 在 C 中处理比特流

c - 我在代码块中收到一个错误,作为func的冲突类型。我只想知道在定义函数时我在做什么错

python - C:初始化二叉堆

macos - Docker使用OSX禁用旧式注册表

java - SSL 套接字 - Java 和证书?

c - 结构声明中的结构数组 : "array type has incomplete element type"

macos - OSX : How do you allow user to resize window under auto layout?

java - GUI 对象在 Mac 上的 Java 中不显示

python - 如何刷新 socket.gethostbyname 响应的缓存?

python - 如何重现 "Connection reset by peer"