Mikrotik Neighbor Discovery (MNDP)

Mikrotik Neighbor Discovery (MNDP) (C++/Qt)

For all that are interestet in discovering Mikrotik devices in their own programs here is an example how to do, written in C++/Qt

First we send out some UDP packages to request Mikrotik answers:

QByteArray datagram;
datagram.resize(4);
datagram[0] = 0x00;
datagram[1] = 0x00;
datagram[2] = 0x00;
datagram[3] = 0x00;

// Send a few packets to force Mikrotik Discovery
for(int i=0; i<3; i++)
··udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 5678);

After a while we get a UDP Packet back with the required information, we just need to parse it:

QByteArray datagram;
QHostAddress *address = new QHostAddress;
int datatoread = udpSocket->pendingDatagramSize();
char *datagramPtr = 0;
short len = 0;

QByteArray macAddr, identity, version, platform, uptime, softwareid, board, unpack, interface;

datagram.resize(datatoread);
udpSocket->readDatagram(datagram.data(), datatoread, address);

// MNDP packet has a bit size
if(datatoread < 18)
·· continue;

// Work with pointer
datagramPtr = datagram.data();

// Get MAC-address
len = 0;
len |= ((datagramPtr[6])&0xFF << 8) | ((datagramPtr[7])&0xFF);
macAddr.resize(len*2+5);
sprintf(macAddr.data(), “%02x:%02x:%02x:%02x:%02x:%02x”,
datagramPtr[8], datagramPtr[9], datagramPtr[10],
datagramPtr[11], datagramPtr[12], datagramPtr[13]);

// Get Identity
datagramPtr += 16;
len = 0;
len |= ((datagramPtr[0])&0xFF << 8) | ((datagramPtr[1])&0xFF);
identity.resize(len);
strncpy(identity.data(), datagramPtr+2, len);

// Get Version
datagramPtr += len + 4;
len = 0;
len |= ((datagramPtr[0])&0xFF << 8) | ((datagramPtr[1])&0xFF);
version.resize(len);
strncpy(version.data(), datagramPtr+2, len);

// Get Platform
datagramPtr += len + 4;
len = 0;
len |= ((datagramPtr[0])&0xFF << 8) | ((datagramPtr[1])&0xFF);
platform.resize(len);
strncpy(platform.data(), datagramPtr+2, len);

// Get Uptime
datagramPtr += len + 4;
len = 0;
len |= ((datagramPtr[0])&0xFF << 8) | ((datagramPtr[1])&0xFF);
uptime.resize(len);
strncpy(uptime.data(), datagramPtr+2, len);

// Get Software-ID
datagramPtr += len + 4;
len = 0;
len |= ((datagramPtr[0])&0xFF << 8) | ((datagramPtr[1])&0xFF);
softwareid.resize(len);
strncpy(softwareid.data(), datagramPtr+2, len);

// Get Board
datagramPtr += len + 4;
len = 0;
len |= ((datagramPtr[0])&0xFF << 8) | ((datagramPtr[1])&0xFF);
board.resize(len);
strncpy(board.data(), datagramPtr+2, len);

// Get Unpack
datagramPtr += len + 4;
len = 0;
len |= ((datagramPtr[0])&0xFF << 8) | ((datagramPtr[1])&0xFF);
unpack.resize(len);
strncpy(unpack.data(), datagramPtr+2, len);

// Get Interface
datagramPtr += len + 4;
len = 0;
len |= ((datagramPtr[0])&0xFF << 8) | ((datagramPtr[1])&0xFF);
interface.resize(len);
strncpy(interface.data(), datagramPtr+2, len);

Leave a Reply

Your email address will not be published. Required fields are marked *