PDA

View Full Version : MAC address of UDP packet's sender



mastupristi
4th January 2011, 14:47
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:


#include <QDialog>
#include <QUdpSocket>

namespace Ui {
class Discovery;
}

class Discovery : public QDialog
{
Q_OBJECT

public:
explicit Discovery(QWidget *parent = 0);
~Discovery();

private:
Ui::Discovery *ui;

QUdpSocket sock;

public slots:
void processPendingDatagrams(void);
};


#include "discovery.h"
#include "ui_discovery.h"

#include <QUdpSocket>

Discovery::Discovery(QWidget *parent) :
QDialog(parent),
ui(new Ui::Discovery),
sock(this)
{
ui->setupUi(this);

connect(&sock, SIGNAL(readyRead ()), this, SLOT (processPendingDatagrams ()));

QByteArray dg;
static const char datagramma[] = {00, 01, 00, 0xF6 };
dg.append(datagramma, sizeof(datagramma));
sock.bind();
sock.writeDatagram(dg,QHostAddress("255.255.255.255"), 0x77FE);
}

Discovery::~Discovery()
{
delete ui;
}

void Discovery::processPendingDatagrams(void)
{
char data[64];
QHostAddress host;
quint16 port;

sock.readDatagram(data, sizeof(data), &host, &port);
qDebug("read from %s", host.toString().toAscii().constData());
}
Doing this I can recover IP address
Now I must recover the MAC address.

How can I retrieve MAC address from packet received?

thanks

ChrisW67
5th January 2011, 22:04
You can't. The UDP (http://en.wikipedia.org/wiki/User_Datagram_Protocol) 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.

wysota
5th January 2011, 22:35
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.

kosasker
31st January 2011, 13:12
@mastupristi: i'm getting mac addresses with arp sender/receiver... Look around for that..