2011-07-29 4 views
8

Mac OS X에서 C로 코딩 된 간단한 패킷 스니퍼를 실행할 때 전혀 출력이 없습니다. 이상한 일입니다! 누군가가 내가 무슨 일이 벌어지고 있는지 이해할 수있게 도와 줄 수 있을까요?Mac OS X의 이상한 원시 소켓

#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 

감사합니다.

+0

원시 소켓을 직접 열지 않고 libpcap을 사용하면 더 편리 할 수 ​​있습니다. – duskwuff

답변

10

* BSD (OSX/Darwin 포함)에서는 작동하지 않습니다. 이야기의

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 

도덕적를 : : 자세한 내용은 조사 here를 참조 이것에 대한 libpcap를 사용합니다. 그것은 당신의 삶을 훨씬 쉽게 만들어 줄 것입니다. (MacPorts를 사용하는 경우 sudo port install libpcap을 사용하십시오.)

+0

답장을 보내 주셔서 감사합니다. lpcap 들어 전에 그것을 사용하고 그것은 잘 작동합니다. 나는 왜이 단순한 원시 소켓이 Mac OS X가 아닌 Linux에서 작동하는지 조사하고 있었고 지금은 확인을 받았습니다. 감사. – funnyCoder

0

나는 그것을 실행하고 얻을 : 나는 당신이 소켓을 열 수있는 권한이 없습니다 및 stderr이 이상한 리디렉션처럼, 뭔가 정말 이상한 될 것 같은데요

# ./a.out 
Got some bytes : 176 
Got some bytes : 168 
Got some bytes : 168 
# 

합니다.

내가 좋은 구식 늑대 트랩 디버깅 좋을 것 :

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

     return -1; 
    } 
    printf("I got to 2\n"); 
    for(i=0; i < 3; i++) { 
     printf("About to read socket.\n"); 
     recv_length = recv(sockfd, buffer, 8000, 0); 
     printf("Got some bytes : %d\n", recv_length); 
    } 
    printf("Past the for loop.\n"); 

를 ...하고 말하는 참조하십시오.

+0

감사합니다 Charlie, MacOS X 또는 Linux에서 실행합니까 (Linux에서는 문제가 없습니다). while 루프에서 멈추는 것 같습니다! 나는 오래된 printf 디버그를 코드에 추가했다 (감사 :-). 그냥 : 소켓을 읽으려고했다. 이 맥 상자에 이상한 것은 내가 바탕 화면에 대한 모든 권한을 가지고. – funnyCoder