Strange RAW Socket on Mac OS X

前端 未结 2 1178
时光取名叫无心
时光取名叫无心 2020-12-05 20:53

When i run a simple packet sniffer coded in C on my Mac OS X, i got no output at all, this is a strange thing! can someone help me to understand what going on.



        
2条回答
  •  天涯浪人
    2020-12-05 21:20

    I run it and get:

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

    I'm guessing it's going to be something really odd, like you don't have permission to open a socket and stderr is redirected oddly.

    I'd suggest the good old-fashioned wolf-trap debugging:

       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");
    

    ...and see what it says.

提交回复
热议问题