Skip to content

Add a link speed implementation for macOS and BSD derivates #158

@bpucher

Description

@bpucher

Hi,

thank you for providing this useful library!

At the moment, the link speed (transmit, receive) is only implemented for Android, Linux and Windows.

Because I need the link speed on macOS, I decided to write an implementation for it. On macOS I also added the Wi-Fi transmit rate. Additionally, there's now also a field indicating if the link speed was auto-negotiated.

My hope, that the code would be almost the same for the supported BSD derivates was lost quickly, after realizing that each derivate uses slightly different constants. Nevertheless, it should work now for FreeBSD, OpenBSD and NetBSD as well. To be honest, I did not have the time to test everything on all the supported BSD derivates.

As you are going to see, I did not find a better solution to handle the differences than using a lot of #[cfg(target_os = ...)] macros and defining constants for all different subtypes (using modules made the code a bit nicer at least). While I tried to figure out a solution using enums, I came to the conclusion that it would end up producing even more code and being less readable. Using the current way, one can at least reference the constants to the C source code and copy new values more easily. I left some commented C code just for reference to certain #defines for a better understanding what the code does.

The addresses for SIOCGIFXMEDIA or SIOCGIFMEDIA have been obtained by using a small C program:

#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_media.h>
#include <sys/sockio.h>

int main() {
    printf("SIOCGIFMEDIA: 0x%lx\n", (unsigned long)SIOCGIFMEDIA);
    printf("SIOCGIFXMEDIA: 0x%lx\n", (unsigned long)SIOCGIFXMEDIA);
    return 0;
}

See #159 . Looking forward to your review.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions