mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 08:42:28 +00:00
Fix getpeername() bug on Windows
The WIN32 getpeername() function returns ENOTCONN when it uses connect() the SOCK_NONBLOCK way. So we simply store the address, provided earlier.
This commit is contained in:
parent
908b7a82ca
commit
f3ce684aef
4 changed files with 71 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_CALLS_STRUCT_FD_INTERNAL_H_
|
||||
#define COSMOPOLITAN_LIBC_CALLS_STRUCT_FD_INTERNAL_H_
|
||||
#include "libc/sock/struct/sockaddr.h"
|
||||
#include "libc/thread/thread.h"
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
|
@ -37,6 +38,7 @@ struct Fd {
|
|||
unsigned sndtimeo; /* millis; 0 means wait forever */
|
||||
void *connect_op;
|
||||
struct Cursor *cursor;
|
||||
struct sockaddr_storage peer;
|
||||
};
|
||||
|
||||
struct Fds {
|
||||
|
|
|
@ -18,10 +18,11 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/intrin/fds.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/fds.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nt/enum/wsaid.h"
|
||||
#include "libc/nt/errors.h"
|
||||
|
@ -34,6 +35,7 @@
|
|||
#include "libc/sock/struct/sockaddr.h"
|
||||
#include "libc/sock/syscall_fd.internal.h"
|
||||
#include "libc/sock/wsaid.internal.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/sol.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
@ -109,6 +111,7 @@ static textwindows int sys_connect_nt_impl(struct Fd *f, const void *addr,
|
|||
|
||||
// perform normal connect
|
||||
if (!(f->flags & O_NONBLOCK)) {
|
||||
f->peer.ss_family = AF_UNSPEC;
|
||||
ssize_t rc = __winsock_block(f->handle, 0, false, f->sndtimeo, mask,
|
||||
sys_connect_nt_start,
|
||||
&(struct ConnectArgs){addr, addrsize});
|
||||
|
@ -122,6 +125,10 @@ static textwindows int sys_connect_nt_impl(struct Fd *f, const void *addr,
|
|||
return rc;
|
||||
}
|
||||
|
||||
// win32 getpeername() stops working in non-blocking connect mode
|
||||
if (addrsize)
|
||||
memcpy(&f->peer, addr, MIN(addrsize, sizeof(struct sockaddr_storage)));
|
||||
|
||||
// perform nonblocking connect(), i.e.
|
||||
// 1. connect(O_NONBLOCK) → EINPROGRESS
|
||||
// 2. poll(POLLOUT)
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/intrin/fds.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/fds.h"
|
||||
#include "libc/intrin/strace.h"
|
||||
#include "libc/nt/errors.h"
|
||||
#include "libc/nt/thunk/msabi.h"
|
||||
|
@ -28,6 +28,7 @@
|
|||
#include "libc/sock/struct/sockaddr.h"
|
||||
#include "libc/sock/struct/sockaddr.internal.h"
|
||||
#include "libc/sock/syscall_fd.internal.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
__msabi extern typeof(__sys_getsockname_nt) *const __imp_getsockname;
|
||||
|
@ -45,7 +46,12 @@ static int __getsockpeername(int fd, struct sockaddr *out_addr,
|
|||
if (IsWindows()) {
|
||||
if (__isfdkind(fd, kFdSocket)) {
|
||||
if ((rc = impl_win32(g_fds.p[fd].handle, &ss, &size))) {
|
||||
if (impl_win32 == __imp_getsockname && WSAGetLastError() == WSAEINVAL) {
|
||||
if (impl_win32 == __imp_getpeername &&
|
||||
g_fds.p[fd].peer.ss_family != AF_UNSPEC) {
|
||||
ss = g_fds.p[fd].peer;
|
||||
rc = 0;
|
||||
} else if (impl_win32 == __imp_getsockname &&
|
||||
WSAGetLastError() == WSAEINVAL) {
|
||||
// The socket has not been bound to an address with bind, or
|
||||
// ADDR_ANY is specified in bind but connection has not yet
|
||||
// occurred. -MSDN
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue