get_mac_address()

This commit is contained in:
David Rose 2013-03-22 21:36:40 +00:00
parent 0a167d9bfb
commit c50dcd9195
2 changed files with 39 additions and 12 deletions

View File

@ -534,17 +534,21 @@ scan_interfaces() {
if (result == ERROR_SUCCESS) {
IP_ADAPTER_ADDRESSES *p = addresses;
while (p != NULL) {
// p->AdapterName appears to be a GUID. Not sure if this is
// actually useful to anyone; we'll store the "friendly name"
// instead.
TextEncoder encoder;
encoder.set_wtext(wstring(p->FriendlyName));
string friendly_name = encoder.get_text();
Interface interface;
interface.set_name(friendly_name);
if (p->PhysicalAddressLength > 0) {
interface.set_mac_address(format_mac_address((const unsigned char *)p->PhysicalAddress, p->PhysicalAddressLength));
}
if (p->OperStatus == IfOperStatusUp) {
// p->AdapterName appears to be a GUID. Not sure if this is
// actually useful to anyone; we'll store the "friendly name"
// instead.
TextEncoder encoder;
encoder.set_wtext(wstring(p->FriendlyName));
string friendly_name = encoder.get_text();
Interface interface;
interface.set_name(friendly_name);
// Prefixes are a linked list, in the order Network IP,
// Adapter IP, Broadcast IP (plus more).
NetAddress addresses[3];
@ -571,9 +575,9 @@ scan_interfaces() {
sa.set_host(netmask, 0);
interface.set_netmask(NetAddress(sa));
}
_interfaces.push_back(interface);
}
_interfaces.push_back(interface);
p = p->Next;
}
}
@ -777,6 +781,24 @@ remove_writer(ConnectionWriter *writer) {
_writers.erase(writer);
}
////////////////////////////////////////////////////////////////////
// Function: ConnectionManager::format_mac_address
// Access: Protected
// Description: Formats a device's MAC address into a string.
////////////////////////////////////////////////////////////////////
string ConnectionManager::
format_mac_address(const unsigned char *data, int data_size) {
stringstream strm;
for (int di = 0; di < data_size; ++di) {
if (di != 0) {
strm << "-";
}
strm << hex << setw(2) << setfill('0') << (unsigned int)data[di];
}
return strm.str();
}
////////////////////////////////////////////////////////////////////
// Function: ConnectionManager::Interface::Output
// Access: Published

View File

@ -70,6 +70,7 @@ PUBLISHED:
class Interface {
PUBLISHED:
const string &get_name() const { return _name; }
const string &get_mac_address() const { return _mac_address; }
bool has_ip() const { return (_flags & F_has_ip) != 0; }
const NetAddress &get_ip() const { return _ip; }
bool has_netmask() const { return (_flags & F_has_netmask) != 0; }
@ -84,6 +85,7 @@ PUBLISHED:
public:
Interface() { _flags = 0; }
void set_name(const string &name) { _name = name; }
void set_mac_address(const string &mac_address) { _mac_address = mac_address; }
void set_ip(const NetAddress &ip) { _ip = ip; _flags |= F_has_ip; }
void set_netmask(const NetAddress &ip) { _netmask = ip; _flags |= F_has_netmask; }
void set_broadcast(const NetAddress &ip) { _broadcast = ip; _flags |= F_has_broadcast; }
@ -91,6 +93,7 @@ PUBLISHED:
private:
string _name;
string _mac_address;
NetAddress _ip;
NetAddress _netmask;
@ -122,6 +125,8 @@ protected:
void add_writer(ConnectionWriter *writer);
void remove_writer(ConnectionWriter *writer);
string format_mac_address(const unsigned char *data, int data_size);
typedef phash_set< PT(Connection) > Connections;
typedef phash_set<ConnectionReader *, pointer_hash> Readers;
typedef phash_set<ConnectionWriter *, pointer_hash> Writers;