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

DEV9: Improve logic for getting MacAddress #10937

Merged
merged 2 commits into from Apr 28, 2024
Merged

Conversation

fjtrujy
Copy link
Contributor

@fjtrujy fjtrujy commented Mar 16, 2024

Description of Changes

Improving logic for getting MacAddress, now it is working for Apple and FreeBSD

Previously on MacOS:

[   12.2374] DEV9: Opening adapter 'en0'...
[   12.2377] DEV9: Device uses DLT 1: EN10MB
[   12.2379] DEV9: Adapter Ok.
[   12.2418] DEV9: Unsupported OS, can't get MAC address
[   12.2420] DEV9: Failed to get MAC address for adapter
[   12.2422] DEV9: Failed to GetNetAdapter()

Now:

[  443.7172] DEV9: Opening adapter 'en0'...
[  443.7173] DEV9: Device uses DLT 1: EN10MB
[  443.7175] DEV9: Adapter Ok.

@lightningterror lightningterror changed the title Improve logic for getting MacAddress DEV9: Improve logic for getting MacAddress Mar 17, 2024
@lightningterror
Copy link
Contributor

@fjtrujy Status?

@TheLastRar
Copy link
Contributor

Status?

@fjtrujy
Copy link
Contributor Author

fjtrujy commented Apr 10, 2024

I will try to address these issues soon.
Thanks for the review

@fjtrujy fjtrujy force-pushed the networkMacOS branch 3 times, most recently from 2adc08a to 34507bf Compare April 10, 2024 22:46
@fjtrujy
Copy link
Contributor Author

fjtrujy commented Apr 10, 2024

Hello @lightningterror @TheLastRar and @stenzek, could you take a look now? not sure if I did how you wanted

Copy link
Contributor

@TheLastRar TheLastRar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I note that you are iterating though all interfaces, returning the first MAC address found

This function should return the MAC address specific to the adapter passed into it.
On non-windows platforms, Adapter is typedefed to ifaddrs

I also noticed a few formatting errors, you should be able to use clang format to correct those.

pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
@fjtrujy
Copy link
Contributor Author

fjtrujy commented Apr 11, 2024

I note that you are iterating though all interfaces, returning the first MAC address found

This function should return the MAC address specific to the adapter passed into it. On non-windows platforms, Adapter is typedefed to ifaddrs

I also noticed a few formatting errors, you should be able to use clang format to correct those.

Understood!!
I have applied that condition too!

pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
@fjtrujy fjtrujy force-pushed the networkMacOS branch 3 times, most recently from ded282a to b26ddf9 Compare April 11, 2024 21:31
@fjtrujy fjtrujy marked this pull request as draft April 11, 2024 22:25
@fjtrujy fjtrujy marked this pull request as ready for review April 11, 2024 23:32
@fjtrujy
Copy link
Contributor Author

fjtrujy commented Apr 11, 2024

I have done some improvements, I think that now is cleaner.
I was putting some Console.WriteLn to check results:

DEV9: Found adapter lo0, requested en0
DEV9: Found adapter lo0, requested en0
DEV9: Found adapter lo0, requested en0
DEV9: Found adapter lo0, requested en0
DEV9: Found adapter gif0, requested en0
DEV9: Found adapter stf0, requested en0
DEV9: Found adapter XHC20, requested en0
DEV9: Found adapter en0, requested en0
DEV9: Found MAC address F4:5C:89:BF:7D:99

Which fit with the values in the OS
Screenshot 2024-04-12 at 01 37 23

Cheers

Copy link
Contributor

@TheLastRar TheLastRar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking better now overall

pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
return macAddr;
#elif defined(AF_LINK)
struct ifaddrs *tempifs, *po;
getifaddrs(&tempifs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will need to call freeifaddrs() once you are done with tempifs

Alternatively, you can use a std::unique_ptr with a custom deleter to handle this for you, like is done in the posix version of AdapterUtils::GetAdapter()

You may also want to check the return value of getifaddrs(), as it could return -1 indicating an error.

pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
@fjtrujy
Copy link
Contributor Author

fjtrujy commented Apr 12, 2024

Once again I have done some improvements, sorry for that, please take a look again

@fjtrujy
Copy link
Contributor Author

fjtrujy commented Apr 16, 2024

Let me know if I need to something else.
Thanks!

@F0bes
Copy link
Member

F0bes commented Apr 23, 2024

This is good to go if you're happy with it @stenzek

Copy link
Member

@stenzek stenzek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I should've given this a proper review earlier.

pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
Console.Error("DEV9: Failed to get MAC address for adapter using SIOCGENADDR");
return std::nullopt;
}
memcpy(&macAddr, &ifr.ifr_enaddr, 6);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
memcpy(&macAddr, &ifr.ifr_enaddr, 6);
std::memcpy(&macAddr, &ifr.ifr_enaddr, sizeof(&macAddr));

pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
@fjtrujy
Copy link
Contributor Author

fjtrujy commented Apr 23, 2024

Hello @stenzek !
First of all thanks for the review, I have applied all your suggestions, please take a look again 🙏

Cheers

Copy link
Member

@stenzek stenzek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM

pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
pcsx2/DEV9/AdapterUtils.cpp Show resolved Hide resolved
@fjtrujy
Copy link
Contributor Author

fjtrujy commented Apr 23, 2024

I have applied all your recommendations, it is way cleaner now!
Thanks

Copy link
Member

@stenzek stenzek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, untested

Copy link
Contributor

@TheLastRar TheLastRar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did test this on linux (which hit the SIOCGIFHWADDR path) and it worked

However, when testing I noticed a small issue with how you had setup the pre processor conditions

pcsx2/DEV9/AdapterUtils.cpp Outdated Show resolved Hide resolved
@stenzek stenzek merged commit d63966b into PCSX2:master Apr 28, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants