golang.zx2c4.com/wireguard/windows@v0.5.4-0.20230123132234-dcc0eb72a04b/embeddable-dll-service/csharp/TunnelDll/Win32.cs (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  using System;
     7  using System.Runtime.InteropServices;
     8  
     9  namespace Tunnel
    10  {
    11      static class Win32
    12      {
    13          [Flags]
    14          public enum ScmAccessRights
    15          {
    16              Connect = 0x0001,
    17              CreateService = 0x0002,
    18              EnumerateService = 0x0004,
    19              Lock = 0x0008,
    20              QueryLockStatus = 0x0010,
    21              ModifyBootConfig = 0x0020,
    22              StandardRightsRequired = 0xF0000,
    23              AllAccess = (StandardRightsRequired | Connect | CreateService | EnumerateService | Lock | QueryLockStatus | ModifyBootConfig)
    24          }
    25  
    26          [Flags]
    27          public enum ServiceAccessRights
    28          {
    29              QueryConfig = 0x1,
    30              ChangeConfig = 0x2,
    31              QueryStatus = 0x4,
    32              EnumerateDependants = 0x8,
    33              Start = 0x10,
    34              Stop = 0x20,
    35              PauseContinue = 0x40,
    36              Interrogate = 0x80,
    37              UserDefinedControl = 0x100,
    38              Delete = 0x00010000,
    39              StandardRightsRequired = 0xF0000,
    40              AllAccess = (StandardRightsRequired | QueryConfig | ChangeConfig | QueryStatus | EnumerateDependants | Start | Stop | PauseContinue | Interrogate | UserDefinedControl)
    41          }
    42  
    43          [Flags]
    44          public enum ServiceStartType
    45          {
    46              Boot = 0x00000000,
    47              System = 0x00000001,
    48              Auto = 0x00000002,
    49              Demand = 0x00000003,
    50              Disabled = 0x00000004
    51          }
    52  
    53          [Flags]
    54          public enum ServiceControl
    55          {
    56              Stop = 0x00000001,
    57              Pause = 0x00000002,
    58              Continue = 0x00000003,
    59              Interrogate = 0x00000004,
    60              Shutdown = 0x00000005,
    61              ParamChange = 0x00000006,
    62              NetBindAdd = 0x00000007,
    63              NetBindRemove = 0x00000008,
    64              NetBindEnable = 0x00000009,
    65              NetBindDisable = 0x0000000A
    66          }
    67  
    68          [Flags]
    69          public enum ServiceError
    70          {
    71              Ignore = 0x00000000,
    72              Normal = 0x00000001,
    73              Severe = 0x00000002,
    74              Critical = 0x00000003
    75          }
    76  
    77          [Flags]
    78          public enum ServiceSidType
    79          {
    80              None = 0x00000000,
    81              Unrestricted = 0x00000001,
    82              Restricted = 0x00000003
    83          }
    84  
    85          [Flags]
    86          public enum ServiceType
    87          {
    88              KernelDriver = 0x00000001,
    89              FileSystemDriver = 0x00000002,
    90              Win32OwnProcess = 0x00000010,
    91              Win32ShareProcess = 0x00000020,
    92              InteractiveProcess = 0x00000100
    93          }
    94  
    95          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Size = 8192), ComVisible(false)]
    96          public struct ServiceSidInfo
    97          {
    98              public ServiceSidType serviceSidType;
    99          };
   100  
   101          public enum ServiceState
   102          {
   103              Unknown = -1,
   104              NotFound = 0,
   105              Stopped = 1,
   106              StartPending = 2,
   107              StopPending = 3,
   108              Running = 4,
   109              ContinuePending = 5,
   110              PausePending = 6,
   111              Paused = 7
   112          }
   113  
   114          [StructLayout(LayoutKind.Sequential)]
   115          public class ServiceStatus
   116          {
   117              public int dwServiceType = 0;
   118              public ServiceState dwCurrentState = 0;
   119              public int dwControlsAccepted = 0;
   120              public int dwWin32ExitCode = 0;
   121              public int dwServiceSpecificExitCode = 0;
   122              public int dwCheckPoint = 0;
   123              public int dwWaitHint = 0;
   124          }
   125  
   126          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Size = 8192), ComVisible(false)]
   127          public struct ServiceDescription
   128          {
   129              public String lpDescription;
   130          };
   131  
   132          public enum ServiceConfigType
   133          {
   134              Description = 1,
   135              SidInfo = 5
   136          }
   137  
   138          [StructLayout(LayoutKind.Sequential)]
   139          public unsafe struct IN_ADDR
   140          {
   141              public fixed byte bytes[4];
   142          }
   143  
   144          [StructLayout(LayoutKind.Sequential)]
   145          public unsafe struct IN6_ADDR
   146          {
   147              public fixed byte bytes[16];
   148          }
   149  
   150          [StructLayout(LayoutKind.Sequential)]
   151          public struct SOCKADDR_IN
   152          {
   153              public ushort sin_family;
   154              public ushort sin_port;
   155              public IN_ADDR sin_addr;
   156          }
   157  
   158          [StructLayout(LayoutKind.Sequential)]
   159          public struct SOCKADDR_IN6
   160          {
   161              public ushort sin6_family;
   162              public ushort sin6_port;
   163              public uint sin6_flowinfo;
   164              public IN6_ADDR sin6_addr;
   165              public uint sin6_scope_id;
   166          }
   167  
   168          [StructLayout(LayoutKind.Explicit)]
   169          public struct SOCKADDR_INET
   170          {
   171              [FieldOffset(0)]
   172              [MarshalAs(UnmanagedType.Struct)]
   173              public SOCKADDR_IN Ipv4;
   174  
   175              [FieldOffset(0)]
   176              [MarshalAs(UnmanagedType.Struct)]
   177              public SOCKADDR_IN6 Ipv6;
   178  
   179              [FieldOffset(0)]
   180              public ADDRESS_FAMILY si_family;
   181          }
   182  
   183          public enum ADDRESS_FAMILY : UInt16
   184          {
   185              AF_UNSPEC = 0,
   186              AF_INET = 2,
   187              AF_INET6 = 23
   188          }
   189  
   190          [DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
   191          public static extern IntPtr OpenSCManager(string machineName, string databaseName, ScmAccessRights dwDesiredAccess);
   192  
   193          [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
   194          public static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, ServiceAccessRights dwDesiredAccess);
   195  
   196          [DllImport("advapi32.dll", SetLastError = true)]
   197          [return: MarshalAs(UnmanagedType.Bool)]
   198          public static extern bool CloseServiceHandle(IntPtr hSCObject);
   199  
   200          [DllImport("advapi32.dll", SetLastError = true)]
   201          [return: MarshalAs(UnmanagedType.Bool)]
   202          public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);
   203  
   204          [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
   205          public static extern IntPtr CreateService(IntPtr hSCManager, string lpServiceName, string lpDisplayName, ServiceAccessRights dwDesiredAccess, ServiceType dwServiceType, ServiceStartType dwStartType, ServiceError dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId, string lpDependencies, string lp, string lpPassword);
   206  
   207          [DllImport("advapi32.dll", SetLastError = true)]
   208          [return: MarshalAs(UnmanagedType.Bool)]
   209          public static extern bool DeleteService(IntPtr hService);
   210  
   211          [DllImport("advapi32.dll", SetLastError = true)]
   212          [return: MarshalAs(UnmanagedType.Bool)]
   213          public static extern bool ControlService(IntPtr hService, ServiceControl dwControl, ServiceStatus lpServiceStatus);
   214  
   215          [DllImport("advapi32.dll", SetLastError = true)]
   216          [return: MarshalAs(UnmanagedType.Bool)]
   217          public static extern bool QueryServiceStatus(IntPtr hService, ServiceStatus lpServiceStatus);
   218  
   219          [DllImport("advapi32.dll", EntryPoint = "ChangeServiceConfig2", CharSet = CharSet.Unicode, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
   220          [return: MarshalAs(UnmanagedType.Bool)]
   221          public static extern bool ChangeServiceConfig2(IntPtr hService, ServiceConfigType dwInfoLevel, ref ServiceSidType lpInfo);
   222  
   223          [DllImport("advapi32.dll", EntryPoint = "ChangeServiceConfig2", CharSet = CharSet.Unicode, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
   224          [return: MarshalAs(UnmanagedType.Bool)]
   225          public static extern bool ChangeServiceConfig2(IntPtr hService, ServiceConfigType dwInfoLevel, ref ServiceDescription lpInfo);
   226      }
   227  }