Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Unable to receive custom Ethernet frame

Unable to receive custom Ethernet frame

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Donnie_Song
    wrote on last edited by
    #1

    I created a socket_raw, the custom protocol type is 0x2328, and I send 0x2328 type data on another machine. The capture packet can be captured, but recvfrom cannot receive it. What should I do? Here is the code received.

    struct sockaddr_ll sll;
    struct ifreq ifr;

    if ((sd = socket (PF_PACKET, SOCK_RAW, htons (0x2328))) < 0) {
    perror ("socket() failed to get socket descriptor for using ioctl() ");
    exit (EXIT_FAILURE);
    }

    // Use ioctl() to look up interface name and get its MAC address.
    memset (&ifr, 0, sizeof (ifr));
    sprintf (ifr.ifr_name, "eth1");

    if (ioctl (sd, SIOCGIFINDEX, &ifr) < 0) {
    perror ("ioctl() failed");
    return (EXIT_FAILURE);
    }

    memset (&sll, 0, sizeof (sll));
    int sll_len;
    sll.sll_family = AF_PACKET;
    sll.sll_ifindex = ifr.ifr_ifindex;
    sll_len = sizeof(sll);

    char buffer[1024];
    recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr *)&sll, &sll_len);

    L 1 Reply Last reply
    0
    • D Donnie_Song

      I created a socket_raw, the custom protocol type is 0x2328, and I send 0x2328 type data on another machine. The capture packet can be captured, but recvfrom cannot receive it. What should I do? Here is the code received.

      struct sockaddr_ll sll;
      struct ifreq ifr;

      if ((sd = socket (PF_PACKET, SOCK_RAW, htons (0x2328))) < 0) {
      perror ("socket() failed to get socket descriptor for using ioctl() ");
      exit (EXIT_FAILURE);
      }

      // Use ioctl() to look up interface name and get its MAC address.
      memset (&ifr, 0, sizeof (ifr));
      sprintf (ifr.ifr_name, "eth1");

      if (ioctl (sd, SIOCGIFINDEX, &ifr) < 0) {
      perror ("ioctl() failed");
      return (EXIT_FAILURE);
      }

      memset (&sll, 0, sizeof (sll));
      int sll_len;
      sll.sll_family = AF_PACKET;
      sll.sll_ifindex = ifr.ifr_ifindex;
      sll_len = sizeof(sll);

      char buffer[1024];
      recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr *)&sll, &sll_len);

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You must check the return value from the recvfrom call to find out why it is not working. See recvfrom function | Microsoft Docs[^].

      D F 2 Replies Last reply
      0
      • L Lost User

        You must check the return value from the recvfrom call to find out why it is not working. See recvfrom function | Microsoft Docs[^].

        D Offline
        D Offline
        Donnie_Song
        wrote on last edited by
        #3

        recvfrom has no return value, because there is no data, so it is blocking. And if I change 0x2328 to 0x0003, I can receive it.

        L 1 Reply Last reply
        0
        • D Donnie_Song

          recvfrom has no return value, because there is no data, so it is blocking. And if I change 0x2328 to 0x0003, I can receive it.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Try making it a non-blocking socket. You should then get a status return.

          D 1 Reply Last reply
          0
          • L Lost User

            Try making it a non-blocking socket. You should then get a status return.

            D Offline
            D Offline
            Donnie_Song
            wrote on last edited by
            #5

            After I set it to non-blocking, I always returned error code 11, but I didn't find the error message corresponding to 11.

            V L J 3 Replies Last reply
            0
            • D Donnie_Song

              After I set it to non-blocking, I always returned error code 11, but I didn't find the error message corresponding to 11.

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              Could you show the actual code used to obtain the error?

              1 Reply Last reply
              0
              • D Donnie_Song

                After I set it to non-blocking, I always returned error code 11, but I didn't find the error message corresponding to 11.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                See recvfrom function | Microsoft Docs[^] for possible status returns.

                1 Reply Last reply
                0
                • D Donnie_Song

                  After I set it to non-blocking, I always returned error code 11, but I didn't find the error message corresponding to 11.

                  J Offline
                  J Offline
                  Jochen Arndt
                  wrote on last edited by
                  #8

                  It won't help you with your problem but error code 11 is EGAIN defined in errno.h. It indicates that you should execute the function again until you get data or an error, or give up (time out). It is returned by non-blocking functions when the corresponding blocking function would block.

                  D 1 Reply Last reply
                  0
                  • J Jochen Arndt

                    It won't help you with your problem but error code 11 is EGAIN defined in errno.h. It indicates that you should execute the function again until you get data or an error, or give up (time out). It is returned by non-blocking functions when the corresponding blocking function would block.

                    D Offline
                    D Offline
                    Donnie_Song
                    wrote on last edited by
                    #9

                    int main(int argv, char *argc[])
                    {
                    struct ifreq ifr;
                    struct sockaddr_ll sll;
                    int sd, sll_len;
                    sll_len = sizeof(sll);
                    if ((sd = socket(PF_PACKET, SOCK_RAW, htons(0x2328))) < 0)
                    printf("create socket failed!\n");

                        sll.sll\_halen = ETH\_ALEN;
                    
                    
                    
                        strcpy(ifr.ifr\_name, "ens33");
                    
                    
                        ioctl(sd, SIOCGIFFLAGS, &ifr);
                        ifr.ifr\_flags |= IFF\_PROMISC;
                    
                        if(fcntl(sd, F\_SETFL, O\_NONBLOCK) == -1) {
                                perror("fcntl");
                                exit(errno);
                        }
                        char recvbuf\[2048\];
                        sleep(5);
                        int n\_read = recvfrom(sd, recvbuf, 2048, 0, (struct sockaddr \*)&sll, &sll\_len);
                        if (n\_read <= 0)
                        {
                                printf("%d\\n", errno);
                        }
                    

                    }

                    I started to cycle data before I hibernate, but I still can't receive it

                    D 1 Reply Last reply
                    0
                    • D Donnie_Song

                      int main(int argv, char *argc[])
                      {
                      struct ifreq ifr;
                      struct sockaddr_ll sll;
                      int sd, sll_len;
                      sll_len = sizeof(sll);
                      if ((sd = socket(PF_PACKET, SOCK_RAW, htons(0x2328))) < 0)
                      printf("create socket failed!\n");

                          sll.sll\_halen = ETH\_ALEN;
                      
                      
                      
                          strcpy(ifr.ifr\_name, "ens33");
                      
                      
                          ioctl(sd, SIOCGIFFLAGS, &ifr);
                          ifr.ifr\_flags |= IFF\_PROMISC;
                      
                          if(fcntl(sd, F\_SETFL, O\_NONBLOCK) == -1) {
                                  perror("fcntl");
                                  exit(errno);
                          }
                          char recvbuf\[2048\];
                          sleep(5);
                          int n\_read = recvfrom(sd, recvbuf, 2048, 0, (struct sockaddr \*)&sll, &sll\_len);
                          if (n\_read <= 0)
                          {
                                  printf("%d\\n", errno);
                          }
                      

                      }

                      I started to cycle data before I hibernate, but I still can't receive it

                      D Offline
                      D Offline
                      Donnie_Song
                      wrote on last edited by
                      #10

                      I found the reason. Previously, because send and recv were sent and received on the same network card, it could not be received.

                      J 1 Reply Last reply
                      0
                      • D Donnie_Song

                        I found the reason. Previously, because send and recv were sent and received on the same network card, it could not be received.

                        J Offline
                        J Offline
                        Jochen Arndt
                        wrote on last edited by
                        #11

                        Fine to hear that. I do know that it does not work on the same interface. But you wrote in your initial post that you are sending from another machine so that I thought it must be something else.

                        1 Reply Last reply
                        0
                        • L Lost User

                          You must check the return value from the recvfrom call to find out why it is not working. See recvfrom function | Microsoft Docs[^].

                          F Offline
                          F Offline
                          farman abdullah
                          wrote on last edited by
                          #12

                          i"m interested in c and c++.im provide the link and please visited my website

                          [url=https://www.imgsrc.com/\]imgsrc[/url]

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups