[WS2_32_NEW]
[reactos.git] / reactos / dll / win32 / ws2_32_new / src / rasdial.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
4 * FILE: dll/win32/ws2_32_new/src/rasdial.c
5 * PURPOSE: RAS Auto-Dial Support
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include <ws2_32.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16 /* DATA **********************************************************************/
17
18 typedef BOOL
19 (WSAAPI *PWS_ATTEMPT_AUTODIAL_ADDR)(
20 IN CONST SOCKADDR FAR *Name,
21 IN INT NameLength
22 );
23
24 typedef BOOL
25 (WSAAPI *PWS_ATTEMPT_AUTODIAL_NAME)(IN CONST LPWSAQUERYSETW lpqsRestrictions);
26
27 typedef VOID
28 (WSAAPI *PWS_NOTE_SUCCESSFUL_HOSTENT_LOOKUP)(
29 IN CONST CHAR FAR *Name,
30 IN CONST ULONG Address
31 );
32
33 BOOLEAN WsRasInitialized;
34 HINSTANCE WsRasDllHandle;
35 CRITICAL_SECTION WsRasHelperLock;
36 PWS_ATTEMPT_AUTODIAL_ADDR lpfnWSAttemptAutodialAddr;
37 PWS_ATTEMPT_AUTODIAL_NAME lpfnWSAttemptAutodialName;
38 PWS_NOTE_SUCCESSFUL_HOSTENT_LOOKUP lpfnWSNoteSuccessfulHostentLookup;
39
40 #define WsRasLock() EnterCriticalSection(&WsRasHelperLock);
41 #define WsRasUnlock() LeaveCriticalSection(&WsRasHelperLock);
42
43 /* FUNCTIONS *****************************************************************/
44
45 VOID
46 WSAAPI
47 WsRasInitializeAutodial(VOID)
48 {
49 /* Initialize the autodial lock */
50 InitializeCriticalSection(&WsRasHelperLock);
51 }
52
53 VOID
54 WSAAPI
55 WsRasUninitializeAutodial(VOID)
56 {
57 /* Acquire lock */
58 WsRasLock();
59
60 /* Free the library if it's loaded */
61 if (WsRasDllHandle) FreeLibrary(WsRasDllHandle);
62 WsRasDllHandle = NULL;
63
64 /* Release and delete lock */
65 WsRasUnlock();
66 DeleteCriticalSection(&WsRasHelperLock);
67 }
68
69 INT
70 WSAAPI
71 WsRasLoadHelperDll(VOID)
72 {
73 CHAR HelperPath[MAX_PATH];
74 HKEY WinsockKey;
75 INT ErrorCode;
76 DWORD RegType = REG_SZ;
77 DWORD RegSize = MAX_PATH;
78
79 /* Acquire the lock */
80 WsRasLock();
81
82 /* Check if we were already initialiazed */
83 if (!WsRasInitialized)
84 {
85 /* Open the registry root key */
86 WinsockKey = WsOpenRegistryRoot();
87 if (WinsockKey)
88 {
89 /* Read the helper's location */
90 ErrorCode = RegQueryValueEx(WinsockKey,
91 "AutodialDLL",
92 0,
93 &RegType,
94 (LPBYTE)&HelperPath,
95 &RegSize);
96 RegCloseKey(WinsockKey);
97
98 /* Make sure we read the path */
99 if (ErrorCode == ERROR_SUCCESS)
100 {
101 /* Now load it */
102 WsRasDllHandle = LoadLibrary(HelperPath);
103 }
104 }
105
106 /* Check if we weren't able to load it and load the default */
107 if (!WsRasDllHandle) WsRasDllHandle = LoadLibrary("rasadhlp.dll");
108
109 /* Check again if we loaded it */
110 if (WsRasDllHandle)
111 {
112 /* Get function pointers */
113 lpfnWSAttemptAutodialAddr =
114 (PVOID)GetProcAddress(WsRasDllHandle,
115 "WSAttemptAutodialAddr");
116 lpfnWSAttemptAutodialName =
117 (PVOID)GetProcAddress(WsRasDllHandle,
118 "WSAttemptAutodialName");
119 lpfnWSNoteSuccessfulHostentLookup =
120 (PVOID)GetProcAddress(WsRasDllHandle,
121 "WSNoteSuccessfulHostentLookup");
122 }
123
124 /* Mark us as loaded */
125 WsRasInitialized = TRUE;
126 }
127
128 /* Release lock */
129 WsRasUnlock();
130
131 /* Return status */
132 return WsRasInitialized;
133 }
134
135 BOOL
136 WSAAPI
137 WSAttemptAutodialAddr(IN CONST SOCKADDR FAR *Name,
138 IN INT NameLength)
139 {
140 /* Load the helper DLL and make sure it exports this routine */
141 if (!(WsRasLoadHelperDll()) || !(lpfnWSAttemptAutodialAddr)) return FALSE;
142
143 /* Call the function in the helper */
144 return lpfnWSAttemptAutodialAddr(Name, NameLength);
145 }
146
147 BOOL
148 WSAAPI
149 WSAttemptAutodialName(IN CONST LPWSAQUERYSETW lpqsRestrictions)
150 {
151 /* Load the helper DLL and make sure it exports this routine */
152 if (!(WsRasLoadHelperDll()) || !(lpfnWSAttemptAutodialName)) return FALSE;
153
154 /* Call the function in the helper */
155 return lpfnWSAttemptAutodialName(lpqsRestrictions);
156 }
157
158 VOID
159 WSAAPI
160 WSNoteSuccessfulHostentLookup(IN CONST CHAR FAR *Name,
161 IN CONST ULONG Address)
162 {
163 /* Load the helper DLL and make sure it exports this routine */
164 if (!(WsRasLoadHelperDll()) || !(lpfnWSNoteSuccessfulHostentLookup)) return;
165
166 /* Call the function in the helper */
167 lpfnWSNoteSuccessfulHostentLookup(Name, Address);
168 }