How to get the mac address of the console

Post Reply
sorunome
Posts: 4
Joined: Fri Apr 12, 2024 12:24 pm

How to get the mac address of the console

Post by sorunome » Sat Apr 13, 2024 4:15 pm

Hello,

Soru is playing around with some stuffs and wanted to, programmatically, get the mac address of the console. The IPC nwm::SOC apparently has a method GetMACAddress, however soru can't get it to work without a kernel panic. Furthermore, the file twln:/sys/log/inspect.log also includes the mac address, but soru can't figure out how to read files in twln.

As for the GetMACAddress, soru attempted with this code:

Code: Select all

Result getMac(u8 mac[6]) {	
	Handle handle;
	res = srvGetServiceHandle(&handle, "nwm::SOC");
	if (R_FAILED(res)) return res;

	u32 *cmdbuf = getThreadCommandBuffer();
	cmdbuf[0] = IPC_MakeHeader(8, 1, 0);
	cmdbuf[1] = 6;
	for (int i = 2; i < 0x100 / 8; i+=2) {
		cmdbuf[i] = IPC_Desc_StaticBuffer(0, 6);
		cmdbuf[i+1] = (u32)mac;
	}
	res = svcSendSyncRequest(handle);
	if (R_FAILED(res)) return res;
	res = (Result)cmdbuf[1];
	memcpy(mac, (u8*)cmdbuf[3], 6);
	return res;
}
Any help in either direction would be greatly appreciated, thank you in advance!

Stary
Posts: 1
Joined: Wed May 20, 2020 5:57 pm

Re: How to get the mac address of the console

Post by Stary » Sun Apr 14, 2024 4:03 pm

nwm::SOC is the internal interface used by soc, so you're not really meant to use it from applications. soc exposes the mac address via SOCU_GetNetworkOpt, here's a complete code sample including soc init:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <3ds.h>

void stall() {
	while(aptMainLoop()) {
		gspWaitForVBlank();
		gfxSwapBuffers();
		hidScanInput();

		u32 kDown = hidKeysDown();
                if (kDown & KEY_START) {
                        break; // break in order to return to hbmenu
		}
	}
}

void *soc_buffer = NULL;
void __attribute__((weak)) userAppInit(void)
{
	soc_buffer = memalign(0x1000, 0x10000);
	socInit(soc_buffer, 0x10000);
}
void __attribute__((weak)) userAppExit(void)
{
	socExit();
}

int main(int argc, char* argv[])
{
	gfxInitDefault();
	consoleInit(GFX_TOP, NULL);
	atexit(gfxExit);

	u8 mac_address[6] = {};
	socklen_t len_out = 6;
	Result res = SOCU_GetNetworkOpt(SOL_CONFIG, NETOPT_MAC_ADDRESS, mac_address, &len_out);
	printf("GetNetworkOpt result: %08lx\n", res);
	printf("%02x:%02x:%02x:%02x:%02x:%02x\n", mac_address[0], mac_address[1],  mac_address[2],  mac_address[3],  mac_address[4],  mac_address[5]);

	stall();

	return 0;
}

sorunome
Posts: 4
Joined: Fri Apr 12, 2024 12:24 pm

Re: How to get the mac address of the console

Post by sorunome » Fri Apr 19, 2024 12:19 pm

Thank you for the reply! It appears to soru, however, that the result of your method just returns 0's while using nwm::SOC actually works

WinterMute
Site Admin
Posts: 1986
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: How to get the mac address of the console

Post by WinterMute » Fri Apr 19, 2024 4:04 pm

sorunome wrote: Fri Apr 19, 2024 12:19 pm Thank you for the reply! It appears to soru, however, that the result of your method just returns 0's while using nwm::SOC actually works
The code Stary posted above prints the mac address on my 3DS. I can only assume that you've misunderstood the function in some way but obviously we can't tell unless you show us the code you used to test. Here's a binary I built earlier, perhaps you could show us the output from this.
mac_test.zip
(52.45 KiB) Downloaded 454 times
Help keep devkitPro toolchains free, Donate today

Personal Blog

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest