11cb0ef41Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
41cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
51cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the
61cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
71cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
81cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
91cb0ef41Sopenharmony_ci *
101cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
111cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software.
121cb0ef41Sopenharmony_ci *
131cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
141cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
151cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
161cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
171cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
181cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
191cb0ef41Sopenharmony_ci * IN THE SOFTWARE.
201cb0ef41Sopenharmony_ci */
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#ifndef UV_WIN_WINSOCK_H_
231cb0ef41Sopenharmony_ci#define UV_WIN_WINSOCK_H_
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci#include <winsock2.h>
261cb0ef41Sopenharmony_ci#include <iptypes.h>
271cb0ef41Sopenharmony_ci#include <mswsock.h>
281cb0ef41Sopenharmony_ci#include <ws2tcpip.h>
291cb0ef41Sopenharmony_ci#include <windows.h>
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci#include "winapi.h"
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci/*
351cb0ef41Sopenharmony_ci * MinGW is missing these too
361cb0ef41Sopenharmony_ci */
371cb0ef41Sopenharmony_ci#ifndef SO_UPDATE_CONNECT_CONTEXT
381cb0ef41Sopenharmony_ci# define SO_UPDATE_CONNECT_CONTEXT 0x7010
391cb0ef41Sopenharmony_ci#endif
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci#ifndef TCP_KEEPALIVE
421cb0ef41Sopenharmony_ci# define TCP_KEEPALIVE 3
431cb0ef41Sopenharmony_ci#endif
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci#ifndef IPV6_V6ONLY
461cb0ef41Sopenharmony_ci# define IPV6_V6ONLY 27
471cb0ef41Sopenharmony_ci#endif
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci#ifndef IPV6_HOPLIMIT
501cb0ef41Sopenharmony_ci# define IPV6_HOPLIMIT 21
511cb0ef41Sopenharmony_ci#endif
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci#ifndef SIO_BASE_HANDLE
541cb0ef41Sopenharmony_ci# define SIO_BASE_HANDLE 0x48000022
551cb0ef41Sopenharmony_ci#endif
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci#ifndef MCAST_JOIN_SOURCE_GROUP
581cb0ef41Sopenharmony_ci# define MCAST_JOIN_SOURCE_GROUP 45
591cb0ef41Sopenharmony_ci#endif
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci#ifndef MCAST_LEAVE_SOURCE_GROUP
621cb0ef41Sopenharmony_ci# define MCAST_LEAVE_SOURCE_GROUP 46
631cb0ef41Sopenharmony_ci#endif
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci/*
661cb0ef41Sopenharmony_ci * TDI defines that are only in the DDK.
671cb0ef41Sopenharmony_ci * We only need receive flags so far.
681cb0ef41Sopenharmony_ci */
691cb0ef41Sopenharmony_ci#ifndef TDI_RECEIVE_NORMAL
701cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_BROADCAST           0x00000004
711cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_MULTICAST           0x00000008
721cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_PARTIAL             0x00000010
731cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_NORMAL              0x00000020
741cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_EXPEDITED           0x00000040
751cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_PEEK                0x00000080
761cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_NO_RESPONSE_EXP     0x00000100
771cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_COPY_LOOKAHEAD      0x00000200
781cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_ENTIRE_MESSAGE      0x00000400
791cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_AT_DISPATCH_LEVEL   0x00000800
801cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_CONTROL_INFO        0x00001000
811cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_FORCE_INDICATION    0x00002000
821cb0ef41Sopenharmony_ci  #define TDI_RECEIVE_NO_PUSH             0x00004000
831cb0ef41Sopenharmony_ci#endif
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci/*
861cb0ef41Sopenharmony_ci * The "Auxiliary Function Driver" is the windows kernel-mode driver that does
871cb0ef41Sopenharmony_ci * TCP, UDP etc. Winsock is just a layer that dispatches requests to it.
881cb0ef41Sopenharmony_ci * Having these definitions allows us to bypass winsock and make an AFD kernel
891cb0ef41Sopenharmony_ci * call directly, avoiding a bug in winsock's recvfrom implementation.
901cb0ef41Sopenharmony_ci */
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci#define AFD_NO_FAST_IO   0x00000001
931cb0ef41Sopenharmony_ci#define AFD_OVERLAPPED   0x00000002
941cb0ef41Sopenharmony_ci#define AFD_IMMEDIATE    0x00000004
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci#define AFD_POLL_RECEIVE_BIT            0
971cb0ef41Sopenharmony_ci#define AFD_POLL_RECEIVE                (1 << AFD_POLL_RECEIVE_BIT)
981cb0ef41Sopenharmony_ci#define AFD_POLL_RECEIVE_EXPEDITED_BIT  1
991cb0ef41Sopenharmony_ci#define AFD_POLL_RECEIVE_EXPEDITED      (1 << AFD_POLL_RECEIVE_EXPEDITED_BIT)
1001cb0ef41Sopenharmony_ci#define AFD_POLL_SEND_BIT               2
1011cb0ef41Sopenharmony_ci#define AFD_POLL_SEND                   (1 << AFD_POLL_SEND_BIT)
1021cb0ef41Sopenharmony_ci#define AFD_POLL_DISCONNECT_BIT         3
1031cb0ef41Sopenharmony_ci#define AFD_POLL_DISCONNECT             (1 << AFD_POLL_DISCONNECT_BIT)
1041cb0ef41Sopenharmony_ci#define AFD_POLL_ABORT_BIT              4
1051cb0ef41Sopenharmony_ci#define AFD_POLL_ABORT                  (1 << AFD_POLL_ABORT_BIT)
1061cb0ef41Sopenharmony_ci#define AFD_POLL_LOCAL_CLOSE_BIT        5
1071cb0ef41Sopenharmony_ci#define AFD_POLL_LOCAL_CLOSE            (1 << AFD_POLL_LOCAL_CLOSE_BIT)
1081cb0ef41Sopenharmony_ci#define AFD_POLL_CONNECT_BIT            6
1091cb0ef41Sopenharmony_ci#define AFD_POLL_CONNECT                (1 << AFD_POLL_CONNECT_BIT)
1101cb0ef41Sopenharmony_ci#define AFD_POLL_ACCEPT_BIT             7
1111cb0ef41Sopenharmony_ci#define AFD_POLL_ACCEPT                 (1 << AFD_POLL_ACCEPT_BIT)
1121cb0ef41Sopenharmony_ci#define AFD_POLL_CONNECT_FAIL_BIT       8
1131cb0ef41Sopenharmony_ci#define AFD_POLL_CONNECT_FAIL           (1 << AFD_POLL_CONNECT_FAIL_BIT)
1141cb0ef41Sopenharmony_ci#define AFD_POLL_QOS_BIT                9
1151cb0ef41Sopenharmony_ci#define AFD_POLL_QOS                    (1 << AFD_POLL_QOS_BIT)
1161cb0ef41Sopenharmony_ci#define AFD_POLL_GROUP_QOS_BIT          10
1171cb0ef41Sopenharmony_ci#define AFD_POLL_GROUP_QOS              (1 << AFD_POLL_GROUP_QOS_BIT)
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci#define AFD_NUM_POLL_EVENTS             11
1201cb0ef41Sopenharmony_ci#define AFD_POLL_ALL                    ((1 << AFD_NUM_POLL_EVENTS) - 1)
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_citypedef struct _AFD_RECV_DATAGRAM_INFO {
1231cb0ef41Sopenharmony_ci    LPWSABUF BufferArray;
1241cb0ef41Sopenharmony_ci    ULONG BufferCount;
1251cb0ef41Sopenharmony_ci    ULONG AfdFlags;
1261cb0ef41Sopenharmony_ci    ULONG TdiFlags;
1271cb0ef41Sopenharmony_ci    struct sockaddr* Address;
1281cb0ef41Sopenharmony_ci    int* AddressLength;
1291cb0ef41Sopenharmony_ci} AFD_RECV_DATAGRAM_INFO, *PAFD_RECV_DATAGRAM_INFO;
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_citypedef struct _AFD_RECV_INFO {
1321cb0ef41Sopenharmony_ci    LPWSABUF BufferArray;
1331cb0ef41Sopenharmony_ci    ULONG BufferCount;
1341cb0ef41Sopenharmony_ci    ULONG AfdFlags;
1351cb0ef41Sopenharmony_ci    ULONG TdiFlags;
1361cb0ef41Sopenharmony_ci} AFD_RECV_INFO, *PAFD_RECV_INFO;
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci#define _AFD_CONTROL_CODE(operation, method) \
1401cb0ef41Sopenharmony_ci    ((FSCTL_AFD_BASE) << 12 | (operation << 2) | method)
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci#define FSCTL_AFD_BASE FILE_DEVICE_NETWORK
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci#define AFD_RECEIVE            5
1451cb0ef41Sopenharmony_ci#define AFD_RECEIVE_DATAGRAM   6
1461cb0ef41Sopenharmony_ci#define AFD_POLL               9
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci#define IOCTL_AFD_RECEIVE \
1491cb0ef41Sopenharmony_ci    _AFD_CONTROL_CODE(AFD_RECEIVE, METHOD_NEITHER)
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci#define IOCTL_AFD_RECEIVE_DATAGRAM \
1521cb0ef41Sopenharmony_ci    _AFD_CONTROL_CODE(AFD_RECEIVE_DATAGRAM, METHOD_NEITHER)
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci#define IOCTL_AFD_POLL \
1551cb0ef41Sopenharmony_ci    _AFD_CONTROL_CODE(AFD_POLL, METHOD_BUFFERED)
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
1581cb0ef41Sopenharmony_citypedef struct _IP_ADAPTER_UNICAST_ADDRESS_XP {
1591cb0ef41Sopenharmony_ci  /* FIXME: __C89_NAMELESS was removed */
1601cb0ef41Sopenharmony_ci  /* __C89_NAMELESS */ union {
1611cb0ef41Sopenharmony_ci    ULONGLONG Alignment;
1621cb0ef41Sopenharmony_ci    /* __C89_NAMELESS */ struct {
1631cb0ef41Sopenharmony_ci      ULONG Length;
1641cb0ef41Sopenharmony_ci      DWORD Flags;
1651cb0ef41Sopenharmony_ci    };
1661cb0ef41Sopenharmony_ci  };
1671cb0ef41Sopenharmony_ci  struct _IP_ADAPTER_UNICAST_ADDRESS_XP *Next;
1681cb0ef41Sopenharmony_ci  SOCKET_ADDRESS Address;
1691cb0ef41Sopenharmony_ci  IP_PREFIX_ORIGIN PrefixOrigin;
1701cb0ef41Sopenharmony_ci  IP_SUFFIX_ORIGIN SuffixOrigin;
1711cb0ef41Sopenharmony_ci  IP_DAD_STATE DadState;
1721cb0ef41Sopenharmony_ci  ULONG ValidLifetime;
1731cb0ef41Sopenharmony_ci  ULONG PreferredLifetime;
1741cb0ef41Sopenharmony_ci  ULONG LeaseLifetime;
1751cb0ef41Sopenharmony_ci} IP_ADAPTER_UNICAST_ADDRESS_XP,*PIP_ADAPTER_UNICAST_ADDRESS_XP;
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_citypedef struct _IP_ADAPTER_UNICAST_ADDRESS_LH {
1781cb0ef41Sopenharmony_ci  union {
1791cb0ef41Sopenharmony_ci    ULONGLONG Alignment;
1801cb0ef41Sopenharmony_ci    struct {
1811cb0ef41Sopenharmony_ci      ULONG Length;
1821cb0ef41Sopenharmony_ci      DWORD Flags;
1831cb0ef41Sopenharmony_ci    };
1841cb0ef41Sopenharmony_ci  };
1851cb0ef41Sopenharmony_ci  struct _IP_ADAPTER_UNICAST_ADDRESS_LH *Next;
1861cb0ef41Sopenharmony_ci  SOCKET_ADDRESS Address;
1871cb0ef41Sopenharmony_ci  IP_PREFIX_ORIGIN PrefixOrigin;
1881cb0ef41Sopenharmony_ci  IP_SUFFIX_ORIGIN SuffixOrigin;
1891cb0ef41Sopenharmony_ci  IP_DAD_STATE DadState;
1901cb0ef41Sopenharmony_ci  ULONG ValidLifetime;
1911cb0ef41Sopenharmony_ci  ULONG PreferredLifetime;
1921cb0ef41Sopenharmony_ci  ULONG LeaseLifetime;
1931cb0ef41Sopenharmony_ci  UINT8 OnLinkPrefixLength;
1941cb0ef41Sopenharmony_ci} IP_ADAPTER_UNICAST_ADDRESS_LH,*PIP_ADAPTER_UNICAST_ADDRESS_LH;
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci#endif
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ciint uv__convert_to_localhost_if_unspecified(const struct sockaddr* addr,
1991cb0ef41Sopenharmony_ci                                            struct sockaddr_storage* storage);
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci#endif /* UV_WIN_WINSOCK_H_ */
202