1 2 Previous Next 15 Replies Latest reply on Dec 2, 2016 12:09 PM by Intel Corporation

    Ethernet udp broadcast not working

    Gorghino

      Hi everybody.

      We're working with 2 Galileo and we want to send some UDP packets to the broadcast address.

      We managed to send/receive from a board to another one and viceversa with this sketches:

       

      #include <Ethernet.h>
      #include <EthernetUdp.h>
      
      
      byte mac[6] = { 0x98, 0x4F, 0xEE, 0x00, 0xF2, 0xA1 };
      IPAddress ip(192, 168, 0, 90);
      
      
      EthernetUDP Udp;
      IPAddress to(192, 168, 0, 91);
      
      
      void setup() {
        Serial.begin(9600);
        Ethernet.begin(mac,ip);
        system("ifup eth0");
        Udp.begin(8888);
        system("ifconfig -a eth0 > /dev/ttyGS0");
      }
      
      
      void loop() {
        Udp.beginPacket(to, 8888);
        Udp.write("hello\n");
        Udp.endPacket();
        delay(2000);
      }
      

       

      #include <Ethernet.h>
      #include <EthernetUdp.h>
      
      
      byte mac[6] = { 0x98, 0x4F, 0xEE, 0x01, 0x09, 0xAD };
      IPAddress ip(192, 168, 0, 91);
      
      
      EthernetUDP Udp;
      char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
      
      
      void setup() {
        Serial.begin(9600);
        Ethernet.begin(mac,ip);
        system("ifup eth0");
        Udp.begin(8888);
        system("ifconfig -a eth0 > /dev/ttyGS0");
      }
      
      
      void loop() {
       int packetSize = Udp.parsePacket();
        if(packetSize) {
          Serial.print("Received packet of size ");
          Serial.println(packetSize);
          // read the packet into packetBufffer
          Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
          Serial.println("Contents:");
          Serial.println(packetBuffer);
        }
      }
      

       

      When we tried to set 255.255.255.255 OR 192.168.0.255 as destination, it just doesn't work.

       

      The strange thing is: if we output the return values of beginPacket() and endPacket(), the first call returns 0 and the second -1.

      From the Ethernet lib documentation the endPacket() function should return 0 when there is an error, but weirdly it gives us -1 instead.

       

      Any ideas or solutions?

       

       

       

       

       

        • 1. Re: Ethernet udp broadcast not working
          FGT

          Hi Gorghino,

           

          I've tried to use your sending sketch (the first one in your post) and it seems it does not work with

          SPI flash image (upgraded)

          eglibc based yocto (current one, 20160606)

           

          Edit:

          Using the SPI, the Serial Monitor shows:

          ==============================================================

          eth0  Link encap:Ethernet  HWaddr 98:4F:EE:01:0E:EA 
                UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
                RX packets:0 errors:0 dropped:0 overruns:0 frame:0
                TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
                collisions:0 txqueuelen:1000
                RX bytes:0 (0.0 B)  TX bytes:966 (966.0 B)
                Interrupt:41 Base address:0x8000

          ==============================================================

          i.e. the eth0 does not seem to be Up&Running...

           

          Using the eglibc based yocto there is no output...

           

          What image are you using?

           

          Fernando.

          • 2. Re: Ethernet udp broadcast not working
            Intel Corporation
            This message was posted on behalf of Intel Corporation

            Hello Gorghino,

            I don't have much experience in networking but I believe the issue is on the IP addresses you are setting. You should set the IP addresses to be on the same network, otherwise they won't find each other. I'm not sure what you mean when you mention that you try to set 255.255.255.255 or 192.168.0.255 as destination. Are those the IP addresses of each board? If so, what I mentioned before makes sense as these addresses would not find each other.

            -Peter.

            • 3. Re: Ethernet udp broadcast not working
              FGT

              Hi Peter,

               

              Both, 255.255.255.255 and 192.168.0.255 are two possible broadcast addresses (roughly speaking...) for the IPs in the Gorghino's sketches. At leas one of them should be usable... at least from the point of view of Linux. I'm not so sure about Ethernet and UDPEthernet (EthernetUdp) libraries... I've not used them, and I did not see any example.

               

              Fernando.

              • 4. Re: Ethernet udp broadcast not working
                Gorghino

                Hi Fernando,
                we're using the stock image. Where can we check the image version?
                We upgraded them to the 1.0.4 version with the tool.

                 

                The ethernet interface is up:

                eth0      Link encap:Ethernet  HWaddr 98:4F:EE:01:09:AD 

                          inet addr:192.168.0.90  Bcast:192.168.0.255  Mask:255.255.255.0

                          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

                          RX packets:0 errors:0 dropped:0 overruns:0 frame:0

                          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

                          collisions:0 txqueuelen:1000

                          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

                          Interrupt:41 Base address:0x8000

                • 5. Re: Ethernet udp broadcast not working
                  FGT

                  Hi Gorghino,


                  we're using the stock image. Where can we check the image version?
                  We upgraded them to the 1.0.4 version with the tool.

                   

                  Hmmm... if by "stock image" you mean the one at firmware then we are using the same one, since you upgraded the firmware to the 1.0.4 version. If you are using SD card for boot then please tell me the image you are using.

                   

                  You can check the firmware version with the software updater tool provided by Intel, but if you are using an Arduino IDE version greater than 1.5.3 (which is an old one, actually) then the most likely is that you are using the correct one, which is version 1.0.4.

                   

                  Maybe our differences are given by different Intel Galileo card versions... I'm using Intel Galileo, aka Intel Galileo Gen1. Are you using Intel Galileo Gen2?

                   

                  Fernando.

                  • 6. Re: Ethernet udp broadcast not working
                    Gorghino

                    Hi FGT,
                    We're not using a SD card and the firmware is 1.0.4.
                    We're currently using the Intel Arduino libraries 1.6.7 and We have two Galileo Gen1.

                    • 7. Re: Ethernet udp broadcast not working
                      FGT

                      Gorghino wrote:

                       

                      Hi FGT,
                      We're not using a SD card and the firmware is 1.0.4.
                      We're currently using the Intel Arduino libraries 1.6.7 and We have two Galileo Gen1.

                      Then there is something I can't explain, I'm using the same environment (firmware, IDE, and Galileo cards) but my results are different... I think I'll not be able to help here... maybe you can try via standard networking programming, which is a little beyond Intel Galileo... contact me via PM if you prefer...

                       

                      Fernando.

                      • 8. Re: Ethernet udp broadcast not working
                        FGT

                        Just curious: can you elaborate on the application you are working in?

                         

                        Fernando.

                        • 9. Re: Ethernet udp broadcast not working
                          Intel Corporation
                          This message was posted on behalf of Intel Corporation

                          Hello Gorghino,

                          I agree with Fernando, could you please let us know some more details about your project?

                          -Peter.

                          • 10. Re: Ethernet udp broadcast not working
                            Gorghino

                            We're working on a simple NDN protocol over IP for an university project.
                            We built our UDP packages and we send them to the two Galileo connected via ethernet.
                            We made a GO client that correctly send the packets to broadcast IP but Galileo EthernetUDP library finds 255.255.255.255 as remoteIP() and the send() to 255.255.255.255 fails on beginPacket() or endPacket() like said before.

                            • 11. Re: Ethernet udp broadcast not working
                              Intel Corporation
                              This message was posted on behalf of Intel Corporation

                              I understand, let me try to investigate about this. If I find anything useful I will post it here. I'll try to get back to you as quickly as I can.

                              -Peter.

                              • 13. Re: Ethernet udp broadcast not working
                                Intel Corporation
                                This message was posted on behalf of Intel Corporation

                                Hello Gorghino,

                                When using the remoteIP(), it returns 255.255.255.255 and sending packets to that client address doesn't work. There is a workaround that returns the correct address of the client, but there are some changes needed in the Ethernet library for Galileo.

                                The location of EthernetUdp.cpp (which is the file that needs the modifications) is in a path that should look similar to "C:\Users\Username\AppData\Local\Arduino15\packages\Intel\hardware\i586\1.6.7+1.0\libraries\Ethernet\src\".

                                These are the 4 lines that need to be added in the EthernetUDP::available() function:

                                int EthernetUDP::available() {
                                    struct pollfd ufds;
                                    int ret = 0;
                                    extern int errno;
                                    int timeout = 0; // milliseconds
                                    struct sockaddr_in remoteIP; // struct to save the remote IP
                                    socklen_t size; // size of sockaddr, needed for getting the IP from sockaddr, will save the size of the remote IP
                                    if (_sock == -1)
                                    return 0;
                                    ufds.fd = _sock;
                                    ufds.events = POLLIN;
                                    ufds.revents = 0;
                                    ret = poll(&ufds, 1, timeout);
                                    if ( ret < 0 ){
                                        trace_error("%s error on poll errno %d", __func__, errno);
                                        return 0;
                                    }
                                    if( ret == 0)
                                    return 0;
                                    // only return available if bytes are present to be read
                                    if(ret > 0 && ufds.revents&POLLIN){
                                        size=sizeof(struct sockaddr);
                                        int bytes = 0;
                                        ret = ioctl(_sock, FIONREAD, &bytes);
                                        recvfrom(_sock, NULL, 0, MSG_PEEK ,(struct sockaddr*) &remoteIP, &size); //call to recvfrom with MSG_PEEK to get the remote IP and don't destroy the message
                                        _remoteIP=(byte *)inet_ntoa(remoteIP.sin_addr); //saving ip in _remoteIP library variable
                                        if ( ret < 0){
                                            trace_error("ioctl fail on socket!");
                                            return 0;
                                        }
                                        if ( ret == 0 && bytes != 0){
                                            return bytes;
                                        }
                                    }
                                    return 0;
                                }

                                Could you please try this modification and let us know how it goes?
                                -Peter.

                                • 14. Re: Ethernet udp broadcast not working
                                  tdconway

                                  These changes worked for me.  I also added a line to get the remote port:

                                   

                                   

                                   

                                  _remotePort = htons(remoteIP.sin_port);

                                   

                                  Thanks,

                                   

                                  Tim

                                   

                                   

                                  1 2 Previous Next