magbone
magbone

Reputation: 1

Can't receive GRE packet in MacOS

When I want to implement the PPTP client in MacOS, the LCP packet encapsulated in GRE packet can't be received from PPTP server in my computer, the OS is Catalina 10.15.2.

This is my code written in C.

int ppp_fd;
      
if ((ppp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_GRE)) <= 0)
{
     perror("[Error] Create a new socket failed");
     return;
}
struct sockaddr_in recv;
recv.sin_addr.s_addr = htonl(INADDR_ANY);
recv.sin_port = 0;
recv.sin_family = AF_INET;

char buffer[BUFSIZ];
int ret;
int addr_len =  sizeof(recv);
if((ret = recvfrom(ppp_fd, buffer, BUFSIZ, 0, (struct sockaddr *)&recv, (socklen_t *)&addr_len)) < 0)
{
    perror("[ERROR] Receive data failed");
    return;
}
printf("ret: %d\n", ret);

The Wireshark can capture the Configure Request correctly, but the code seem to be blocked and print nothing.

Upvotes: 0

Views: 146

Answers (1)

magbone
magbone

Reputation: 1

Ok, perhaps we can't normally receive the data through raw socket in MacOS, but the software like Wireshark, tcpdump and etc. can capture the traffic. So, for solving the above questions, just following a few steps below.

  1. Using software manager such as brew to install the libpcap.
  2. Including the header file #include<pcap/pcap.h> in your source file.
  3. Giving the simple example as follow:

#include <stdio.h>
#include <pcap/pcap.h>      

void get_packet_cb(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
{
      printf("Packet length: %d\n", pkthdr->len);
}

int main()      
{      
      
      char err_msg[PCAP_ERRBUF_SIZE] = {0};
      pcap_t *pcap_handler = pcap_open_live("vmnet8", 65535, 1, 100, err_msg);

      if(pcap_handler == NULL){
            printf("[ERROR] %s\n", err_msg);
            return (-1);
      }
      struct bpf_program filter;
      if(pcap_compile(pcap_handler, &filter, "ip proto gre", 1, 0) == -1){
            pcap_perror(pcap_handler, "[ERROR] Compile the expression of filter failed");
            goto _close;
      }
      pcap_setfilter(pcap_handler, &filter);

      int id = 0;
      
      pcap_loop(pcap_handler, -1, get_packet_cb, NULL);

      
      _close:pcap_close(pcap_handler);
      return 0;
}



  1. Typing the command line gcc source_name.c -lpcap -o output_name in your terminal to compile and link.

In effect, many protocols in MacOS can't be received correctly, in my practice, at least the ICMP datagram can do that.

Upvotes: 0

Related Questions