github.com/slackhq/nebula@v1.9.0/dist/windows/wintun/include/wintun.h (about)

     1  /* SPDX-License-Identifier: GPL-2.0 OR MIT
     2   *
     3   * Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  #pragma once
     7  
     8  #include <winsock2.h>
     9  #include <windows.h>
    10  #include <ipexport.h>
    11  #include <ifdef.h>
    12  #include <ws2ipdef.h>
    13  
    14  #ifdef __cplusplus
    15  extern "C" {
    16  #endif
    17  
    18  #ifndef ALIGNED
    19  #    if defined(_MSC_VER)
    20  #        define ALIGNED(n) __declspec(align(n))
    21  #    elif defined(__GNUC__)
    22  #        define ALIGNED(n) __attribute__((aligned(n)))
    23  #    else
    24  #        error "Unable to define ALIGNED"
    25  #    endif
    26  #endif
    27  
    28  /* MinGW is missing this one, unfortunately. */
    29  #ifndef _Post_maybenull_
    30  #    define _Post_maybenull_
    31  #endif
    32  
    33  #pragma warning(push)
    34  #pragma warning(disable : 4324) /* structure was padded due to alignment specifier */
    35  
    36  /**
    37   * A handle representing Wintun adapter
    38   */
    39  typedef struct _WINTUN_ADAPTER *WINTUN_ADAPTER_HANDLE;
    40  
    41  /**
    42   * Creates a new Wintun adapter.
    43   *
    44   * @param Name          The requested name of the adapter. Zero-terminated string of up to MAX_ADAPTER_NAME-1
    45   *                      characters.
    46   *
    47   * @param TunnelType    Name of the adapter tunnel type. Zero-terminated string of up to MAX_ADAPTER_NAME-1
    48   *                      characters.
    49   *
    50   * @param RequestedGUID The GUID of the created network adapter, which then influences NLA generation deterministically.
    51   *                      If it is set to NULL, the GUID is chosen by the system at random, and hence a new NLA entry is
    52   *                      created for each new adapter. It is called "requested" GUID because the API it uses is
    53   *                      completely undocumented, and so there could be minor interesting complications with its usage.
    54   *
    55   * @return If the function succeeds, the return value is the adapter handle. Must be released with
    56   * WintunCloseAdapter. If the function fails, the return value is NULL. To get extended error information, call
    57   * GetLastError.
    58   */
    59  typedef _Must_inspect_result_
    60  _Return_type_success_(return != NULL)
    61  _Post_maybenull_
    62  WINTUN_ADAPTER_HANDLE(WINAPI WINTUN_CREATE_ADAPTER_FUNC)
    63  (_In_z_ LPCWSTR Name, _In_z_ LPCWSTR TunnelType, _In_opt_ const GUID *RequestedGUID);
    64  
    65  /**
    66   * Opens an existing Wintun adapter.
    67   *
    68   * @param Name          The requested name of the adapter. Zero-terminated string of up to MAX_ADAPTER_NAME-1
    69   *                      characters.
    70   *
    71   * @return If the function succeeds, the return value is the adapter handle. Must be released with
    72   * WintunCloseAdapter. If the function fails, the return value is NULL. To get extended error information, call
    73   * GetLastError.
    74   */
    75  typedef _Must_inspect_result_
    76  _Return_type_success_(return != NULL)
    77  _Post_maybenull_
    78  WINTUN_ADAPTER_HANDLE(WINAPI WINTUN_OPEN_ADAPTER_FUNC)(_In_z_ LPCWSTR Name);
    79  
    80  /**
    81   * Releases Wintun adapter resources and, if adapter was created with WintunCreateAdapter, removes adapter.
    82   *
    83   * @param Adapter       Adapter handle obtained with WintunCreateAdapter or WintunOpenAdapter.
    84   */
    85  typedef VOID(WINAPI WINTUN_CLOSE_ADAPTER_FUNC)(_In_opt_ WINTUN_ADAPTER_HANDLE Adapter);
    86  
    87  /**
    88   * Deletes the Wintun driver if there are no more adapters in use.
    89   *
    90   * @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
    91   *         get extended error information, call GetLastError.
    92   */
    93  typedef _Return_type_success_(return != FALSE)
    94  BOOL(WINAPI WINTUN_DELETE_DRIVER_FUNC)(VOID);
    95  
    96  /**
    97   * Returns the LUID of the adapter.
    98   *
    99   * @param Adapter       Adapter handle obtained with WintunCreateAdapter or WintunOpenAdapter
   100   *
   101   * @param Luid          Pointer to LUID to receive adapter LUID.
   102   */
   103  typedef VOID(WINAPI WINTUN_GET_ADAPTER_LUID_FUNC)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _Out_ NET_LUID *Luid);
   104  
   105  /**
   106   * Determines the version of the Wintun driver currently loaded.
   107   *
   108   * @return If the function succeeds, the return value is the version number. If the function fails, the return value is
   109   *         zero. To get extended error information, call GetLastError. Possible errors include the following:
   110   *         ERROR_FILE_NOT_FOUND  Wintun not loaded
   111   */
   112  typedef _Return_type_success_(return != 0)
   113  DWORD(WINAPI WINTUN_GET_RUNNING_DRIVER_VERSION_FUNC)(VOID);
   114  
   115  /**
   116   * Determines the level of logging, passed to WINTUN_LOGGER_CALLBACK.
   117   */
   118  typedef enum
   119  {
   120      WINTUN_LOG_INFO, /**< Informational */
   121      WINTUN_LOG_WARN, /**< Warning */
   122      WINTUN_LOG_ERR   /**< Error */
   123  } WINTUN_LOGGER_LEVEL;
   124  
   125  /**
   126   * Called by internal logger to report diagnostic messages
   127   *
   128   * @param Level         Message level.
   129   *
   130   * @param Timestamp     Message timestamp in in 100ns intervals since 1601-01-01 UTC.
   131   *
   132   * @param Message       Message text.
   133   */
   134  typedef VOID(CALLBACK *WINTUN_LOGGER_CALLBACK)(
   135      _In_ WINTUN_LOGGER_LEVEL Level,
   136      _In_ DWORD64 Timestamp,
   137      _In_z_ LPCWSTR Message);
   138  
   139  /**
   140   * Sets logger callback function.
   141   *
   142   * @param NewLogger     Pointer to callback function to use as a new global logger. NewLogger may be called from various
   143   *                      threads concurrently. Should the logging require serialization, you must handle serialization in
   144   *                      NewLogger. Set to NULL to disable.
   145   */
   146  typedef VOID(WINAPI WINTUN_SET_LOGGER_FUNC)(_In_ WINTUN_LOGGER_CALLBACK NewLogger);
   147  
   148  /**
   149   * Minimum ring capacity.
   150   */
   151  #define WINTUN_MIN_RING_CAPACITY 0x20000 /* 128kiB */
   152  
   153  /**
   154   * Maximum ring capacity.
   155   */
   156  #define WINTUN_MAX_RING_CAPACITY 0x4000000 /* 64MiB */
   157  
   158  /**
   159   * A handle representing Wintun session
   160   */
   161  typedef struct _TUN_SESSION *WINTUN_SESSION_HANDLE;
   162  
   163  /**
   164   * Starts Wintun session.
   165   *
   166   * @param Adapter       Adapter handle obtained with WintunOpenAdapter or WintunCreateAdapter
   167   *
   168   * @param Capacity      Rings capacity. Must be between WINTUN_MIN_RING_CAPACITY and WINTUN_MAX_RING_CAPACITY (incl.)
   169   *                      Must be a power of two.
   170   *
   171   * @return Wintun session handle. Must be released with WintunEndSession. If the function fails, the return value is
   172   *         NULL. To get extended error information, call GetLastError.
   173   */
   174  typedef _Must_inspect_result_
   175  _Return_type_success_(return != NULL)
   176  _Post_maybenull_
   177  WINTUN_SESSION_HANDLE(WINAPI WINTUN_START_SESSION_FUNC)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _In_ DWORD Capacity);
   178  
   179  /**
   180   * Ends Wintun session.
   181   *
   182   * @param Session       Wintun session handle obtained with WintunStartSession
   183   */
   184  typedef VOID(WINAPI WINTUN_END_SESSION_FUNC)(_In_ WINTUN_SESSION_HANDLE Session);
   185  
   186  /**
   187   * Gets Wintun session's read-wait event handle.
   188   *
   189   * @param Session       Wintun session handle obtained with WintunStartSession
   190   *
   191   * @return Pointer to receive event handle to wait for available data when reading. Should
   192   *         WintunReceivePackets return ERROR_NO_MORE_ITEMS (after spinning on it for a while under heavy
   193   *         load), wait for this event to become signaled before retrying WintunReceivePackets. Do not call
   194   *         CloseHandle on this event - it is managed by the session.
   195   */
   196  typedef HANDLE(WINAPI WINTUN_GET_READ_WAIT_EVENT_FUNC)(_In_ WINTUN_SESSION_HANDLE Session);
   197  
   198  /**
   199   * Maximum IP packet size
   200   */
   201  #define WINTUN_MAX_IP_PACKET_SIZE 0xFFFF
   202  
   203  /**
   204   * Retrieves one or packet. After the packet content is consumed, call WintunReleaseReceivePacket with Packet returned
   205   * from this function to release internal buffer. This function is thread-safe.
   206   *
   207   * @param Session       Wintun session handle obtained with WintunStartSession
   208   *
   209   * @param PacketSize    Pointer to receive packet size.
   210   *
   211   * @return Pointer to layer 3 IPv4 or IPv6 packet. Client may modify its content at will. If the function fails, the
   212   *         return value is NULL. To get extended error information, call GetLastError. Possible errors include the
   213   *         following:
   214   *         ERROR_HANDLE_EOF     Wintun adapter is terminating;
   215   *         ERROR_NO_MORE_ITEMS  Wintun buffer is exhausted;
   216   *         ERROR_INVALID_DATA   Wintun buffer is corrupt
   217   */
   218  typedef _Must_inspect_result_
   219  _Return_type_success_(return != NULL)
   220  _Post_maybenull_
   221  _Post_writable_byte_size_(*PacketSize)
   222  BYTE *(WINAPI WINTUN_RECEIVE_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _Out_ DWORD *PacketSize);
   223  
   224  /**
   225   * Releases internal buffer after the received packet has been processed by the client. This function is thread-safe.
   226   *
   227   * @param Session       Wintun session handle obtained with WintunStartSession
   228   *
   229   * @param Packet        Packet obtained with WintunReceivePacket
   230   */
   231  typedef VOID(
   232      WINAPI WINTUN_RELEASE_RECEIVE_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _In_ const BYTE *Packet);
   233  
   234  /**
   235   * Allocates memory for a packet to send. After the memory is filled with packet data, call WintunSendPacket to send
   236   * and release internal buffer. WintunAllocateSendPacket is thread-safe and the WintunAllocateSendPacket order of
   237   * calls define the packet sending order.
   238   *
   239   * @param Session       Wintun session handle obtained with WintunStartSession
   240   *
   241   * @param PacketSize    Exact packet size. Must be less or equal to WINTUN_MAX_IP_PACKET_SIZE.
   242   *
   243   * @return Returns pointer to memory where to prepare layer 3 IPv4 or IPv6 packet for sending. If the function fails,
   244   *         the return value is NULL. To get extended error information, call GetLastError. Possible errors include the
   245   *         following:
   246   *         ERROR_HANDLE_EOF       Wintun adapter is terminating;
   247   *         ERROR_BUFFER_OVERFLOW  Wintun buffer is full;
   248   */
   249  typedef _Must_inspect_result_
   250  _Return_type_success_(return != NULL)
   251  _Post_maybenull_
   252  _Post_writable_byte_size_(PacketSize)
   253  BYTE *(WINAPI WINTUN_ALLOCATE_SEND_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _In_ DWORD PacketSize);
   254  
   255  /**
   256   * Sends the packet and releases internal buffer. WintunSendPacket is thread-safe, but the WintunAllocateSendPacket
   257   * order of calls define the packet sending order. This means the packet is not guaranteed to be sent in the
   258   * WintunSendPacket yet.
   259   *
   260   * @param Session       Wintun session handle obtained with WintunStartSession
   261   *
   262   * @param Packet        Packet obtained with WintunAllocateSendPacket
   263   */
   264  typedef VOID(WINAPI WINTUN_SEND_PACKET_FUNC)(_In_ WINTUN_SESSION_HANDLE Session, _In_ const BYTE *Packet);
   265  
   266  #pragma warning(pop)
   267  
   268  #ifdef __cplusplus
   269  }
   270  #endif