Alex Henrie (@alexhenrie) commented about server/sock.c:
+ case TCPS_FIN_WAIT_2: return MIB_TCP_STATE_FIN_WAIT2; + case TCPS_TIME_WAIT: return MIB_TCP_STATE_TIME_WAIT; + case TCPS_CLOSE_WAIT: return MIB_TCP_STATE_CLOSE_WAIT; + case TCPS_LAST_ACK: return MIB_TCP_STATE_LAST_ACK; + case TCPS_LISTEN: return MIB_TCP_STATE_LISTEN; + case TCPS_CLOSING: return MIB_TCP_STATE_CLOSING; + default: + case TCPS_CLOSED: return MIB_TCP_STATE_CLOSED; + } +} + +static MIB_TCP_STATE get_tcp_socket_state( int fd ) +{ +#ifdef __APPLE__ + /* The macOS getsockopt name and struct are compatible with those on Linux + and FreeBSD, just named differently. */ It doesn't look like Linux and FreeBSD use the same values to represent each TCP state. [Linux has](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/incl...):
```c enum { TCP_ESTABLISHED = 1, TCP_SYN_SENT, TCP_SYN_RECV, TCP_FIN_WAIT1, TCP_FIN_WAIT2, TCP_TIME_WAIT, TCP_CLOSE, TCP_CLOSE_WAIT, TCP_LAST_ACK, TCP_LISTEN, TCP_CLOSING, /* Now a valid state */ TCP_NEW_SYN_RECV, TCP_BOUND_INACTIVE, /* Pseudo-state for inet_diag */ TCP_MAX_STATES /* Leave at the end! */ }; ``` [FreeBSD has](http://fxr.watson.org/fxr/source/netinet/tcp_fsm.h?v=FREEBSD-13-STABLE#L47): ```c #define TCPS_CLOSED 0 /* closed */ #define TCPS_LISTEN 1 /* listening for connection */ #define TCPS_SYN_SENT 2 /* active, have sent syn */ #define TCPS_SYN_RECEIVED 3 /* have sent and received syn */ /* states < TCPS_ESTABLISHED are those where connections not established */ #define TCPS_ESTABLISHED 4 /* established */ #define TCPS_CLOSE_WAIT 5 /* rcvd fin, waiting for close */ /* states > TCPS_CLOSE_WAIT are those where user has closed */ #define TCPS_FIN_WAIT_1 6 /* have closed, sent fin */ #define TCPS_CLOSING 7 /* closed xchd FIN; await FIN ACK */ #define TCPS_LAST_ACK 8 /* had fin and close; await FIN ACK */ /* states > TCPS_CLOSE_WAIT && < TCPS_FIN_WAIT_2 await ACK of FIN */ #define TCPS_FIN_WAIT_2 9 /* have closed, fin is acked */ #define TCPS_TIME_WAIT 10 /* in 2*msl quiet wait after close */ ``` [macOS has](https://opensource.apple.com/source/xnu/xnu-7195.81.3/bsd/netinet/tcp_fsm.h....) the same values as FreeBSD. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/6399#note_81414