Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: MAC Address #106

Open
namandixit opened this issue Oct 8, 2024 · 3 comments
Open

Proposal: MAC Address #106

namandixit opened this issue Oct 8, 2024 · 3 comments

Comments

@namandixit
Copy link

Would there be interest in adding a function to get the MAC address of the machine in SDL? This would be useful in calculating UUIDv1. I could contribute the Windows version, but wanted to know if it is desired (and if yes, should it go here or in SDL_net).

@slouken
Copy link
Collaborator

slouken commented Oct 8, 2024

Not here, maybe it makes sense in SDL_net?

@slouken slouken closed this as not planned Won't fix, can't repro, duplicate, stale Oct 8, 2024
@icculus icculus transferred this issue from libsdl-org/SDL Oct 9, 2024
@icculus icculus reopened this Oct 9, 2024
@icculus
Copy link
Collaborator

icculus commented Oct 9, 2024

I don’t know if I’ll add this yet, but I’ve moved this to SDL_net and reopened it for now.

@namandixit
Copy link
Author

namandixit commented Oct 10, 2024

Here is a pass at Windows version, putting it here instead of filing a PR since it's not clear if it belongs in SDL:

#include <Windows.h>
#include <iphlpapi.h>
#pragma comment(lib, "Iphlpapi.lib")
#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")

// Parameter is an array of size 6
bool macGetFromArbitraryNIC (unsigned char mac[])
{
    static bool is_cached = false;
    static unsigned char mac_cached[6];

    if (!is_cached) {
        IP_ADAPTER_INFO adapters[64]; // Maximum number of adapters we want to iterate over
        DWORD adapters_size = sizeof(adapters);

        DWORD status = GetAdaptersInfo(adapters, &adapters_size);
        if (status == ERROR_SUCCESS) {
            PIP_ADAPTER_INFO adapt = &adapters[0];

            do {
                if (((adapt->Type == MIB_IF_TYPE_ETHERNET) ||
                     (adapt->Type == IF_TYPE_IEEE80211)) &&
                    !StrStrIA(adapt->Description, "virtual")) {
                    CopyMemory(mac,        adapt->Address, 6);
                    CopyMemory(mac_cached, adapt->Address, 6);

                    is_cached = true;

                    return true; // If MAC found
                }

                adapt = adapt->Next;
            } while (adapt);

            return false; // If no valid MAC found
        } else {
            return false; // If couldn't get information about NICs
        }
    } else {
        CopyMemory(mac, mac_cached, 6);

        return true; // If MAC is already cached
    }
}

Different runs of the application may return different MACs (due to NICs appearing in different order), but fixing that will require too much overhead. Also, we filter out address for virtual NICs since they may be same on different machines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants