Results 1 to 4 of 4

Thread: MAC address of UDP packet's sender

  1. #1
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default MAC address of UDP packet's sender

    Hi,

    I have to write a program to "discover" (retrieving IP and MAC address) some network device. To this end I send difined UDP datagram to broadcast on a certain port. All device present on network will replies.
    I have the following class:
    Qt Code:
    1. #include <QDialog>
    2. #include <QUdpSocket>
    3.  
    4. namespace Ui {
    5. class Discovery;
    6. }
    7.  
    8. class Discovery : public QDialog
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit Discovery(QWidget *parent = 0);
    14. ~Discovery();
    15.  
    16. private:
    17. Ui::Discovery *ui;
    18.  
    19. QUdpSocket sock;
    20.  
    21. public slots:
    22. void processPendingDatagrams(void);
    23. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "discovery.h"
    2. #include "ui_discovery.h"
    3.  
    4. #include <QUdpSocket>
    5.  
    6. Discovery::Discovery(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::Discovery),
    9. sock(this)
    10. {
    11. ui->setupUi(this);
    12.  
    13. connect(&sock, SIGNAL(readyRead ()), this, SLOT (processPendingDatagrams ()));
    14.  
    15. static const char datagramma[] = {00, 01, 00, 0xF6 };
    16. dg.append(datagramma, sizeof(datagramma));
    17. sock.bind();
    18. sock.writeDatagram(dg,QHostAddress("255.255.255.255"), 0x77FE);
    19. }
    20.  
    21. Discovery::~Discovery()
    22. {
    23. delete ui;
    24. }
    25.  
    26. void Discovery::processPendingDatagrams(void)
    27. {
    28. char data[64];
    29. quint16 port;
    30.  
    31. sock.readDatagram(data, sizeof(data), &host, &port);
    32. qDebug("read from %s", host.toString().toAscii().constData());
    33. }
    To copy to clipboard, switch view to plain text mode 
    Doing this I can recover IP address
    Now I must recover the MAC address.

    How can I retrieve MAC address from packet received?

    thanks
    Last edited by mastupristi; 4th January 2011 at 14:55.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: MAC address of UDP packet's sender

    You can't. The UDP packet does not contain the MAC address associated with its transmission unless the sender deliberately includes it in the UDP data. If you are also writing the sender then this is a trivial exercise.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MAC address of UDP packet's sender

    Also please, just for the sake of us all, apply a network mask to the broadcast address before you start emitting such datagrams. One badly configured router and your packet leaks out of your LAN segment.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MAC address of UDP packet's sender

    @mastupristi: i'm getting mac addresses with arp sender/receiver... Look around for that..

Similar Threads

  1. Packet handling advice with QTcpSocket?
    By zAAm in forum General Programming
    Replies: 1
    Last Post: 27th April 2010, 15:23
  2. Packet getting fragmented when sending over QTcpSocket
    By arturo182 in forum Qt Programming
    Replies: 14
    Last Post: 5th August 2009, 23:11
  3. tip on using QObject::sender()
    By drhex in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2008, 20:01
  4. how to send ICMP packet
    By eleanor in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2007, 14:23
  5. QtNetwork send ICMP Packet
    By SlowTree in forum Qt Programming
    Replies: 3
    Last Post: 29th October 2007, 20:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.