[CONUTILS] Make the headers C++ compatible.
[reactos.git] / sdk / lib / smlib / connect.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: reactos/lib/smlib/connect.c
5 * PURPOSE: Connect to the API LPC port exposed by the SM
6 */
7
8 #include "precomp.h"
9
10 #define NDEBUG
11 #include <debug.h>
12
13 /**********************************************************************
14 * NAME EXPORTED
15 * SmConnectApiPort/4
16 *
17 * DESCRIPTION
18 * Connect to SM API port and register a session "begin" port (Sb)
19 * or to issue API requests to SmApiPort.
20 *
21 * ARGUMENTS
22 * pSbApiPortName: name of the Sb port the calling subsystem
23 * server already created in the system name space;
24 * hSbApiPort: LPC port handle (checked, but not used: the
25 * subsystem is required to have already created
26 * the callback port before it connects to the SM);
27 * wSubsystem: a valid IMAGE_SUBSYSTEM_xxx value;
28 * phSmApiPort: a pointer to a HANDLE, which will be
29 * filled with a valid client-side LPC comm port.
30 *
31 * There should be only two ways to call this API:
32 * a) subsystems willing to register with SM will use it
33 * with full parameters (the function checks them);
34 * b) regular SM clients, will set to 0 the 1st, the 2nd,
35 * and the 3rd parameter.
36 *
37 * RETURN VALUE
38 * If all three optional values are omitted, an LPC status.
39 * STATUS_INVALID_PARAMETER_MIX if PortName is defined and
40 * both hSbApiPort and wSubsystem are 0.
41 */
42 NTSTATUS WINAPI
43 SmConnectApiPort (IN PUNICODE_STRING pSbApiPortName OPTIONAL,
44 IN HANDLE hSbApiPort OPTIONAL,
45 IN WORD wSubSystemId OPTIONAL,
46 IN OUT PHANDLE phSmApiPort)
47 {
48 UNICODE_STRING SmApiPortName;
49 SECURITY_QUALITY_OF_SERVICE SecurityQos;
50 NTSTATUS Status = STATUS_SUCCESS;
51 SM_CONNECT_DATA ConnectData = {0,0,{0}};
52 ULONG ConnectDataLength = 0;
53
54 DPRINT("SMLIB: %s called\n", __FUNCTION__);
55
56 if (pSbApiPortName)
57 {
58 if (pSbApiPortName->Length > (sizeof pSbApiPortName->Buffer[0] * SM_SB_NAME_MAX_LENGTH))
59 {
60 return STATUS_INVALID_PARAMETER_1;
61 }
62 if (NULL == hSbApiPort || IMAGE_SUBSYSTEM_UNKNOWN == wSubSystemId)
63 {
64 return STATUS_INVALID_PARAMETER_MIX;
65 }
66 RtlZeroMemory (& ConnectData, sizeof ConnectData);
67 ConnectData.Unused = 0;
68 ConnectData.SubSystemId = wSubSystemId;
69 if (pSbApiPortName->Length > 0)
70 {
71 RtlCopyMemory (& ConnectData.SbName,
72 pSbApiPortName->Buffer,
73 pSbApiPortName->Length);
74 }
75 }
76 ConnectDataLength = sizeof ConnectData;
77
78 SecurityQos.Length = sizeof (SecurityQos);
79 SecurityQos.ImpersonationLevel = SecurityIdentification;
80 SecurityQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
81 SecurityQos.EffectiveOnly = TRUE;
82
83 RtlInitUnicodeString (& SmApiPortName, SM_API_PORT_NAME);
84
85 Status = NtConnectPort (
86 phSmApiPort,
87 & SmApiPortName,
88 & SecurityQos,
89 NULL,
90 NULL,
91 NULL,
92 & ConnectData,
93 & ConnectDataLength
94 );
95 if (NT_SUCCESS(Status))
96 {
97 return STATUS_SUCCESS;
98 }
99 DPRINT("SMLIB: %s failed (Status=0x%08lx)\n", __FUNCTION__, Status);
100 return Status;
101 }
102
103 /* EOF */