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

LibCore: Fix SocketAddress.h compilation errors on Windows #2673

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

stasoid
Copy link
Contributor

@stasoid stasoid commented Dec 1, 2024

Including winsock2.h in a header file is undesirable for architectural reasons, so I pulled the needed types from ws2def.h and ws2ipdef.h into a separate header.

Files inaddr.h and afunix.h are tiny and can be included directly. They each define a single struct (in_addr and sockaddr_un respectively).

I removed the #if (_WIN32_WINNT < 0x0600) code because it is not relevant for us. It is intended for Windows XP/Windows Server 2003 and earlier, but Ladybird doesn't support 32-bit, and 64-bit Windows XP is very rare.

@stasoid stasoid marked this pull request as draft December 1, 2024 06:03
@stasoid stasoid force-pushed the sockaddr branch 5 times, most recently from 8506667 to fca2d78 Compare December 1, 2024 07:44
@stasoid stasoid force-pushed the sockaddr branch 4 times, most recently from e6fa91b to 6ece3e4 Compare December 1, 2024 15:23
@stasoid stasoid marked this pull request as ready for review December 1, 2024 15:23
@stasoid
Copy link
Contributor Author

stasoid commented Dec 1, 2024

@R-Goc Does it work for you?

@R-Goc
Copy link
Contributor

R-Goc commented Dec 4, 2024

It's creating a bit of an issue in system.h and systemWindows.cpp
The issue is that when including system.h we include the sockaddr-win needed there to provide the addrinfo related things. However, this will cause a lot of redefinitions as we need to include WinSock2.h to implement sockets.

@stasoid
Copy link
Contributor Author

stasoid commented Dec 4, 2024

I addressed this issue. You can include winsock2.h after sockaddr-win.h.

@R-Goc
Copy link
Contributor

R-Goc commented Dec 4, 2024

Ok I'll check. Maybe clang format is moving includes around.

@R-Goc
Copy link
Contributor

R-Goc commented Dec 4, 2024

While clang-fromat was indeed moving includes around, turns out the issue is with ws2tcpip.h. Looks like with sockaddr-win.h included the only thing needed from there is gai_strerror(). Though that can be replaced with WSAGetLastError, which would mean different error handling for windows and linux, but I think that's fine. That makes it a bit ugly as getting an error string from windows error messages is a bit difficult, but for now I'll return an int.

@R-Goc
Copy link
Contributor

R-Goc commented Dec 4, 2024

Should we just change AF_UNIX in SocketAddress.h to AF_LOCAL? Or define AF_UNIX to AF_LOCAL here?

@stasoid
Copy link
Contributor Author

stasoid commented Dec 4, 2024

Yes, sockaddr-win.h doesn't work with ws2tcpip.h. If a declaration from ws2tcpip.h is needed it is supposed to be included in sockaddr-win.h. However, I don't want to include gai_strerror because it is inline and Error::from_windows_error can be used instead.

AF_UNIX is not used anywhere in the codebase, that's why I didn't define it.

@R-Goc
Copy link
Contributor

R-Goc commented Dec 4, 2024

It is in SocketAddress.h. sockaddr-win isn't a replacement for SocketAddress.h

@stasoid stasoid mentioned this pull request Dec 7, 2024
@ADKaster
Copy link
Member

ADKaster commented Dec 9, 2024

As mentioned on another PR, please make any and all added header files follow the project conventions for naming

extern "C" __stdcall void freeaddrinfo(PADDRINFOA pAddrInfo);
extern "C" __stdcall PCSTR inet_ntop(int Family, void const* pAddr, char* pStringBuf, size_t StringBufSize);

#define _WS2DEF_ // don't include ws2def.h
Copy link
Member

Choose a reason for hiding this comment

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

Is this sort of header shenanigans common for portable/win32 C++ projects? This particular line makes me a bit uncomfortable. If the declarations here are incompatible with the ones in ws2def.h, then we have a problem.

Copy link
Contributor Author

@stasoid stasoid Dec 9, 2024

Choose a reason for hiding this comment

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

They are not incompatible with ws2def.h, they are the same as in ws2def.h and C++ doesn't allow that. This problem exists for two reasons: 1. SocketAddress.h requires definitions of those structs and 2. including winsock2.h in headers is prohibited. Other projects don't have this problem because they allow including windows.h in headers.

@stasoid stasoid requested a review from ADKaster December 9, 2024 13:17
@stasoid
Copy link
Contributor Author

stasoid commented Dec 9, 2024

@ADKaster I renamed the file.

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.

3 participants