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

Fix recvfrom and sendto. Those api calls are with wrong semantics #743

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions include/fast_io_hosted/platforms/posix_netmode.h
Original file line number Diff line number Diff line change
Expand Up @@ -753,45 +753,45 @@ inline int posix_accept_posix_socket_impl(int fd, void *addr, posix_socklen_t *a
#endif
}

inline int posix_recvfrom_posix_socket_impl(int fd, void *buf, ::std::size_t len, int flags, void *src_addr, posix_socklen_t *addrlen)
inline ::std::ptrdiff_t posix_recvfrom_posix_socket_impl(int fd, void *buf, ::std::size_t len, int flags, void *src_addr, posix_socklen_t *addrlen)
{
#if defined(__linux__) && defined(__NR_recvfrom)
int socfd{system_call<__NR_recvfrom, int>(fd, buf, len, flags, src_addr, addrlen)};
system_call_throw_error(socfd);
return socfd;
::std::ptrdiff_t ret{system_call<__NR_recvfrom, ::std::ptrdiff_t>(fd, buf, len, flags, src_addr, addrlen)};
system_call_throw_error(ret);
return ret;
#else
using sockaddr_alias_ptr
#if __has_cpp_attribute(__gnu__::__may_alias__)
[[__gnu__::__may_alias__]]
#endif
= struct sockaddr *;
int socfd{::recvfrom(fd, buf, len, flags, reinterpret_cast<sockaddr_alias_ptr>(src_addr), addrlen)};
if (socfd == -1)
::std::ptrdiff_t ret{::recvfrom(fd, buf, len, flags, reinterpret_cast<sockaddr_alias_ptr>(src_addr), addrlen)};
if (ret == -1)
{
throw_posix_error();
}
return socfd;
return ret;
#endif
}

inline int posix_sendto_posix_socket_impl(int fd, void const *buf, ::std::size_t len, int flags, const void *src_addr, posix_socklen_t addrlen)
inline ::std::ptrdiff_t posix_sendto_posix_socket_impl(int fd, void const *buf, ::std::size_t len, int flags, const void *src_addr, posix_socklen_t addrlen)
{
#if defined(__linux__) && defined(__NR_sendto)
int socfd{system_call<__NR_sendto, int>(fd, buf, len, flags, src_addr, addrlen)};
system_call_throw_error(socfd);
return socfd;
::std::ptrdiff_t ret{system_call<__NR_sendto, ::std::ptrdiff_t>(fd, buf, len, flags, src_addr, addrlen)};
system_call_throw_error(ret);
return ret;
#else
using sockaddr_const_alias_ptr
#if __has_cpp_attribute(__gnu__::__may_alias__)
[[__gnu__::__may_alias__]]
#endif
= struct sockaddr const*;
int socfd{::sendto(fd, buf, len, flags, reinterpret_cast<sockaddr_const_alias_ptr>(src_addr), addrlen)};
if (socfd == -1)
::std::ptrdiff_t ret{::sendto(fd, buf, len, flags, reinterpret_cast<sockaddr_const_alias_ptr>(src_addr), addrlen)};
if (ret == -1)
{
throw_posix_error();
}
return socfd;
return ret;
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions include/fast_io_hosted/platforms/posix_netop.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ inline void posix_listen(basic_posix_io_observer<ch_type> h, int backlog)
}

template <::std::integral ch_type>
inline int posix_recvfrom(basic_posix_io_observer<ch_type> h, void *buf, ::std::size_t len, int flags, void *src_addr, posix_socklen_t *addrlen)
inline ::std::ptrdiff_t posix_recvfrom(basic_posix_io_observer<ch_type> h, void *buf, ::std::size_t len, int flags, void *src_addr, posix_socklen_t *addrlen)
{
return details::posix_recvfrom_posix_socket_impl(h.fd, buf, len, flags, src_addr, addrlen);
}

template <::std::integral ch_type>
inline int posix_sendto(basic_posix_io_observer<ch_type> h, void const *buf, ::std::size_t len, int flags, void const *src_addr, posix_socklen_t addrlen)
inline ::std::ptrdiff_t posix_sendto(basic_posix_io_observer<ch_type> h, void const *buf, ::std::size_t len, int flags, void const *src_addr, posix_socklen_t addrlen)
{
return details::posix_sendto_posix_socket_impl(h.fd, buf, len, flags, src_addr, addrlen);
}
Expand Down
8 changes: 4 additions & 4 deletions include/fast_io_hosted/platforms/win32_network/socket_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ inline ::std::size_t posix_accept_win32_socket_impl(::std::size_t hsocket, void
return accepted_socket;
}

inline int posix_recvfrom_win32_socket_impl(::std::size_t hsocket, void *buf, ::std::size_t len, int flags, void *src_addr, int *addrlen)
inline ::std::ptrdiff_t posix_recvfrom_win32_socket_impl(::std::size_t hsocket, void *buf, ::std::size_t len, int flags, void *src_addr, int *addrlen)
{
auto soc{::fast_io::win32::recvfrom(hsocket, reinterpret_cast<char *>(buf), static_cast<int>(len), flags, src_addr, addrlen)};
if (soc == -1)
Expand All @@ -112,7 +112,7 @@ inline int posix_recvfrom_win32_socket_impl(::std::size_t hsocket, void *buf, ::
return soc;
}

inline int posix_sendto_win32_socket_impl(::std::size_t hsocket, void const *msg, ::std::size_t len, int flags, void const *to, int tolen)
inline ::std::ptrdiff_t posix_sendto_win32_socket_impl(::std::size_t hsocket, void const *msg, ::std::size_t len, int flags, void const *to, int tolen)
{
auto soc{::fast_io::win32::sendto(hsocket, reinterpret_cast<char const *>(msg), static_cast<int>(len), flags, to, tolen)};

Expand Down Expand Up @@ -159,14 +159,14 @@ inline void posix_listen(basic_win32_family_socket_io_observer<family, ch_type>
}

template <win32_family family, ::std::integral ch_type>
inline int posix_recvfrom(basic_win32_family_socket_io_observer<family, ch_type> h, void *buf, ::std::size_t len,
inline ::std::ptrdiff_t posix_recvfrom(basic_win32_family_socket_io_observer<family, ch_type> h, void *buf, ::std::size_t len,
int flags, void *src_addr, win32_socklen_t *addrlen)
{
return win32::details::posix_recvfrom_win32_socket_impl(h.hsocket, buf, len, flags, src_addr, addrlen);
}

template <win32_family family, ::std::integral ch_type>
inline int posix_sendto(basic_win32_family_socket_io_observer<family, ch_type> h, void const *msg, ::std::size_t len,
inline ::std::ptrdiff_t posix_sendto(basic_win32_family_socket_io_observer<family, ch_type> h, void const *msg, ::std::size_t len,
int flags, void const *to, win32_socklen_t tolen)
{
return win32::details::posix_sendto_win32_socket_impl(h.hsocket, msg, len, flags, to, tolen);
Expand Down
Loading