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

sa_family_t is a uint8_t on Darwin but unsigned short on Linux #112

Closed
wants to merge 1 commit into from

Conversation

djs55
Copy link
Contributor

@djs55 djs55 commented May 7, 2021

On Darwin we have:

typedef __uint8_t               sa_family_t;

struct sockaddr_in {
        __uint8_t       sin_len;
        sa_family_t     sin_family;
        in_port_t       sin_port;
        struct  in_addr sin_addr;
        char            sin_zero[8];
};

> #define AF_UNIX         1               /* local to host (pipes) */
> #define AF_LOCAL        AF_UNIX         /* backward compatibility */
> #define AF_INET         2               /* internetwork: UDP, TCP, etc. */
> #define AF_INET6        30              /* IPv6 */

On Linux we have:

typedef unsigned short sa_family_t;

struct sockaddr_in {
	sa_family_t sin_family;
	in_port_t sin_port;
	struct in_addr sin_addr;
	uint8_t sin_zero[8];
};

> #define PF_LOCAL        1
> #define PF_UNIX         PF_LOCAL
> #define AF_LOCAL        PF_LOCAL
> #define PF_INET         2
> #define PF_INET6        10
> #define AF_INET         PF_INET
> #define AF_INET6        PF_INET6

Since the relevant constants are all small, I think it's ok to use a uint8_t on both platforms. I'm not a Ctypes expert though, so there might be a better way.

Fixes #111

On Darwin we have:
```
typedef __uint8_t               sa_family_t;

struct sockaddr_in {
        __uint8_t       sin_len;
        sa_family_t     sin_family;
        in_port_t       sin_port;
        struct  in_addr sin_addr;
        char            sin_zero[8];
};

> #define AF_UNIX         1               /* local to host (pipes) */
> #define AF_LOCAL        AF_UNIX         /* backward compatibility */
> #define AF_INET         2               /* internetwork: UDP, TCP, etc. */
> #define AF_INET6        30              /* IPv6 */

```

On Linux we have:
```
typedef unsigned short sa_family_t;

struct sockaddr_in {
	sa_family_t sin_family;
	in_port_t sin_port;
	struct in_addr sin_addr;
	uint8_t sin_zero[8];
};

> #define PF_LOCAL        1
> #define PF_UNIX         PF_LOCAL
> #define AF_LOCAL        PF_LOCAL
> #define PF_INET         2
> #define PF_INET6        10
> #define AF_INET         PF_INET
> #define AF_INET6        PF_INET6
```

Since the relevant constants are all small, we can use a uint8_t on
both platforms.

Signed-off-by: David Scott <dave@recoil.org>
aantron added a commit that referenced this pull request May 7, 2021
@aantron
Copy link
Owner

aantron commented May 7, 2021

@djs55 Thanks for looking into that, I would not have found it, as I am on Linux!

The fix in this PR would break access for the family field on big-endian platforms. Can you try the alternative fix, branch sa_family_t in this repo?

@djs55
Copy link
Contributor Author

djs55 commented May 7, 2021

@aantron your alternative fix works fine for me. I can call Luv.TCP.getsockname and query the bound IP and port as expected on macOS. Thanks!

@aantron
Copy link
Owner

aantron commented May 7, 2021

Thanks. I've merged that fix into master. Going to close this PR in favor of it, and do a release.

@avsm
Copy link

avsm commented May 9, 2021

My (forthcoming) S390x OCaml tests thank you for your consideration of big-endian architectures :-)

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

Successfully merging this pull request may close these issues.

TCP.getsockname and getpeername not working as expected
3 participants