]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.cpp
Windows: Use the WinAPI for DNS server retrieval
[user/henk/code/inspircd.git] / win / inspircd_win32wrapper.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2011 Adam <Adam@anope.org>
5  *   Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
7  *   Copyright (C) 2007-2009 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2008 John Brooks <john.brooks@dereferenced.net>
9  *   Copyright (C) 2007 Burlex <???@???>
10  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd_win32wrapper.h"
27 #include "inspircd.h"
28 #include "configreader.h"
29 #include <string>
30 #include <errno.h>
31 #include <assert.h>
32 #include <Iphlpapi.h>
33 #define _WIN32_DCOM
34 #include <comdef.h>
35 #include <Wbemidl.h>
36
37 #pragma comment(lib, "wbemuuid.lib")
38 #pragma comment(lib, "comsuppwd.lib")
39 #pragma comment(lib, "winmm.lib")
40 #pragma comment(lib, "Iphlpapi.lib")
41 using namespace std;
42
43 #ifndef INADDR_NONE
44 #define INADDR_NONE 0xffffffff
45 #endif
46
47 #include <mmsystem.h>
48
49 IWbemLocator *pLoc = NULL;
50 IWbemServices *pSvc = NULL;
51
52 /* This MUST remain static and delcared outside the class, so that WriteProcessMemory can reference it properly */
53 static DWORD owner_processid = 0;
54
55
56 int inet_aton(const char *cp, struct in_addr *addr)
57 {
58         unsigned long ip = inet_addr(cp);
59         addr->s_addr = ip;
60         return (addr->s_addr == INADDR_NONE) ? 0 : 1;
61 }
62
63 const char *insp_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
64 {
65
66         if (af == AF_INET)
67         {
68                 struct sockaddr_in in;
69                 memset(&in, 0, sizeof(in));
70                 in.sin_family = AF_INET;
71                 memcpy(&in.sin_addr, src, sizeof(struct in_addr));
72                 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
73                 return dst;
74         }
75         else if (af == AF_INET6)
76         {
77                 struct sockaddr_in6 in;
78                 memset(&in, 0, sizeof(in));
79                 in.sin6_family = AF_INET6;
80                 memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
81                 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
82                 return dst;
83         }
84         return NULL;
85 }
86
87 int geteuid()
88 {
89         return 1;
90 }
91
92 int insp_inet_pton(int af, const char *src, void *dst)
93 {
94         sockaddr_in sa;
95         int len = sizeof(SOCKADDR);
96         int rv = WSAStringToAddress((LPSTR)src, af, NULL, (LPSOCKADDR)&sa, &len);
97         if(rv >= 0)
98         {
99                 if(WSAGetLastError() == 10022)                  // Invalid Argument
100                         rv = 0;
101                 else
102                         rv = 1;
103         }
104         memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
105         return rv;
106 }
107
108 void setcolor(int color_code)
109 {
110         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_code);
111 }
112
113 DIR * opendir(const char * path)
114 {
115         std::string search_path = string(path) + "\\*.*";
116         WIN32_FIND_DATA fd;
117         HANDLE f = FindFirstFile(search_path.c_str(), &fd);
118         if (f != INVALID_HANDLE_VALUE)
119         {
120                 DIR * d = new DIR;
121                 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
122                 d->find_handle = f;
123                 d->first = true;
124                 return d;
125         }
126         else
127         {
128                 return 0;
129         }
130 }
131
132 dirent * readdir(DIR * handle)
133 {
134         if (handle->first)
135                 handle->first = false;
136         else
137         {
138                 if (!FindNextFile(handle->find_handle, &handle->find_data))
139                         return 0;
140         }
141
142         strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
143         return &handle->dirent_pointer;
144 }
145
146 void closedir(DIR * handle)
147 {
148         FindClose(handle->find_handle);
149         delete handle;
150 }
151
152 const char * dlerror()
153 {
154         static char errormessage[500];
155         DWORD error = GetLastError();
156         SetLastError(0);
157         if (error == 0)
158                 return 0;
159
160         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)errormessage, 500, 0);
161         return errormessage;
162 }
163
164 #define TRED FOREGROUND_RED | FOREGROUND_INTENSITY
165 #define TGREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
166 #define TYELLOW FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
167 #define TNORMAL FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE
168 #define TWHITE TNORMAL | FOREGROUND_INTENSITY
169 #define TBLUE FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
170
171 /* Handles colors in printf */
172 int printf_c(const char * format, ...)
173 {
174         // Better hope we're not multithreaded, otherwise we'll have chickens crossing the road other side to get the to :P
175         static char message[MAXBUF];
176         static char temp[MAXBUF];
177         int color1, color2;
178
179         /* parse arguments */
180         va_list ap;
181         va_start(ap, format);
182         vsnprintf(message, 500, format, ap);
183         va_end(ap);
184
185         /* search for unix-style escape sequences */
186         int t;
187         int c = 0;
188         const char * p = message;
189         while (*p != 0)
190         {
191                 if (*p == '\033')
192                 {
193                         // Escape sequence -> copy into the temp buffer, and parse the color.
194                         p++;
195                         t = 0;
196                         while ((*p) && (*p != 'm'))
197                         {
198                                 temp[t++] = *p;
199                                 ++p;
200                         }
201
202                         temp[t] = 0;
203                         p++;
204
205                         if (*temp == '[')
206                         {
207                                 if (sscanf(temp, "[%u;%u", &color1, &color2) == 2)
208                                 {
209                                         switch(color2)
210                                         {
211                                         case 32:                // Green
212                                                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);              // Yellow
213                                                 break;
214
215                                         default:                // Unknown
216                                                 // White
217                                                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
218                                                 break;
219                                         }
220                                 }
221                                 else
222                                 {
223                                         switch (*(temp+1))
224                                         {
225                                                 case '0':
226                                                         // Returning to normal colour.
227                                                         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
228                                                         break;
229
230                                                 case '1':
231                                                         // White
232                                                         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), TWHITE);
233                                                         break;
234
235                                                 default:
236                                                         char message[50];
237                                                         sprintf(message, "Unknown color code: %s", temp);
238                                                         MessageBox(0, message, message, MB_OK);
239                                                         break;
240                                         }
241                                 }
242                         }
243                 }
244
245                 putchar(*p);
246                 ++c;
247                 ++p;
248         }
249
250         return c;
251 }
252
253 int optind = 1;
254 char optarg[514];
255 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
256 {
257         // burlex todo: handle the shortops, at the moment it only works with longopts.
258
259         if (___argc == 1 || optind == ___argc)                  // No arguments (apart from filename)
260                 return -1;
261
262         const char * opt = ___argv[optind];
263         optind++;
264
265         // if we're not an option, return an error.
266         if (strnicmp(opt, "--", 2) != 0)
267                 return 1;
268         else
269                 opt += 2;
270
271
272         // parse argument list
273         int i = 0;
274         for (; __longopts[i].name != 0; ++i)
275         {
276                 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
277                 {
278                         // woot, found a valid argument =)
279                         char * par = 0;
280                         if ((optind) != ___argc)
281                         {
282                                 // grab the parameter from the next argument (if its not another argument)
283                                 if (strnicmp(___argv[optind], "--", 2) != 0)
284                                 {
285 //                                      optind++;               // Trash this next argument, we won't be needing it.
286                                         par = ___argv[optind-1];
287                                 }
288                         }                       
289
290                         // increment the argument for next time
291 //                      optind++;
292
293                         // determine action based on type
294                         if (__longopts[i].has_arg == required_argument && !par)
295                         {
296                                 // parameter missing and its a required parameter option
297                                 return 1;
298                         }
299
300                         // store argument in optarg
301                         if (par)
302                                 strncpy(optarg, par, 514);
303
304                         if (__longopts[i].flag != 0)
305                         {
306                                 // this is a variable, we have to set it if this argument is found.
307                                 *__longopts[i].flag = 1;
308                                 return 0;
309                         }
310                         else
311                         {
312                                 if (__longopts[i].val == -1 || par == 0)
313                                         return 1;
314                                 
315                                 return __longopts[i].val;
316                         }                       
317                         break;
318                 }
319         }
320
321         // return 1 (invalid argument)
322         return 1;
323 }
324
325 void ClearConsole()
326 {
327         COORD coordScreen = { 0, 0 };    /* here's where we'll home the cursor */
328         HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
329         DWORD cCharsWritten;
330         CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ 
331         DWORD dwConSize;                 /* number of character cells in the current buffer */ 
332
333         /* get the number of character cells in the current buffer */ 
334
335         if (GetConsoleScreenBufferInfo( hConsole, &csbi ))
336         {
337                 dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
338                 /* fill the entire screen with blanks */ 
339                 if (FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ))
340                 {
341                         /* get the current text attribute */ 
342                         if (GetConsoleScreenBufferInfo( hConsole, &csbi ))
343                         {
344                                 /* now set the buffer's attributes accordingly */
345                                 if (FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))
346                                 {
347                                         /* put the cursor at (0, 0) */
348                                         SetConsoleCursorPosition( hConsole, coordScreen );
349                                 }
350                         }
351                 }
352         }
353         return;
354 }
355
356 /* Many inspircd classes contain function pointers/functors which can be changed to point at platform specific implementations
357  * of code. This function repoints these pointers and functors so that calls are windows specific.
358  */
359 void ChangeWindowsSpecificPointers()
360 {
361         ServerInstance->Logs->Log("win32",DEBUG,"Changing to windows specific pointer and functor set");
362 }
363
364 DWORD WindowsForkStart()
365 {
366         /* Windows implementation of fork() :P */
367         if (owner_processid)
368                 return 0;
369
370         char module[MAX_PATH];
371         if(!GetModuleFileName(NULL, module, MAX_PATH))
372         {
373                 printf("GetModuleFileName() failed.\n");
374                 return false;
375         }
376
377         STARTUPINFO startupinfo;
378         PROCESS_INFORMATION procinfo;
379         ZeroMemory(&startupinfo, sizeof(STARTUPINFO));
380         ZeroMemory(&procinfo, sizeof(PROCESS_INFORMATION));
381
382         // Fill in the startup info struct
383         GetStartupInfo(&startupinfo);
384
385         /* Default creation flags create the processes suspended */
386         DWORD startupflags = CREATE_SUSPENDED;
387
388         /* On windows 2003/XP and above, we can use the value
389          * CREATE_PRESERVE_CODE_AUTHZ_LEVEL which gives more access
390          * to the process which we may require on these operating systems.
391          */
392         OSVERSIONINFO vi;
393         vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
394         GetVersionEx(&vi);
395         if ((vi.dwMajorVersion >= 5) && (vi.dwMinorVersion > 0))
396                 startupflags |= CREATE_PRESERVE_CODE_AUTHZ_LEVEL;
397
398         // Launch our "forked" process.
399         BOOL bSuccess = CreateProcess ( module, // Module (exe) filename
400                 strdup(GetCommandLine()),       // Command line (exe plus parameters from the OS)
401                                                 // NOTE: We cannot return the direct value of the
402                                                 // GetCommandLine function here, as the pointer is
403                                                 // passed straight to the child process, and will be
404                                                 // invalid once we exit as it goes out of context.
405                                                 // strdup() seems ok, though.
406                 0,                              // PROCESS_SECURITY_ATTRIBUTES
407                 0,                              // THREAD_SECURITY_ATTRIBUTES
408                 TRUE,                           // We went to inherit handles.
409                 startupflags,                   // Allow us full access to the process and suspend it.
410                 0,                              // ENVIRONMENT
411                 0,                              // CURRENT_DIRECTORY
412                 &startupinfo,                   // startup info
413                 &procinfo);                     // process info
414
415         if(!bSuccess)
416         {
417                 printf("CreateProcess() error: %s\n", dlerror());
418                 return false;
419         }
420
421         // Set the owner process id in the target process.
422         SIZE_T written = 0;
423         DWORD pid = GetCurrentProcessId();
424         if(!WriteProcessMemory(procinfo.hProcess, &owner_processid, &pid, sizeof(DWORD), &written) || written != sizeof(DWORD))
425         {
426                 printf("WriteProcessMemory() failed: %s\n", dlerror());
427                 return false;
428         }
429
430         // Resume the other thread (let it start)
431         ResumeThread(procinfo.hThread);
432
433         // Wait for the new process to kill us. If there is some error, the new process will end and we will end up at the next line.
434         WaitForSingleObject(procinfo.hProcess, INFINITE);
435
436         // If we hit this it means startup failed, default to 14 if this fails.
437         DWORD ExitCode = 14;
438         GetExitCodeProcess(procinfo.hProcess, &ExitCode);
439         CloseHandle(procinfo.hThread);
440         CloseHandle(procinfo.hProcess);
441         return ExitCode;
442 }
443
444 void WindowsForkKillOwner()
445 {
446         HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, owner_processid);
447         if(!hProcess || !owner_processid)
448         {
449                 printf("Could not open process id %u: %s.\n", owner_processid, dlerror());
450                 ServerInstance->Exit(14);
451         }
452
453         // die die die
454         if(!TerminateProcess(hProcess, 0))
455         {
456                 printf("Could not TerminateProcess(): %s\n", dlerror());
457                 ServerInstance->Exit(14);
458         }
459
460         CloseHandle(hProcess);
461 }
462
463 void FindDNS(std::string& server)
464 {
465         if (!server.empty())
466                 return;
467
468         PFIXED_INFO pFixedInfo;
469         DWORD dwBufferSize = sizeof(FIXED_INFO);
470         pFixedInfo = (PFIXED_INFO) HeapAlloc(GetProcessHeap(), 0, sizeof(FIXED_INFO));
471
472         if(pFixedInfo)
473         {
474                 if (GetNetworkParams(pFixedInfo, &dwBufferSize) == ERROR_BUFFER_OVERFLOW) {
475                         HeapFree(GetProcessHeap(), 0, pFixedInfo);
476                         pFixedInfo = (PFIXED_INFO) HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
477                 }
478
479                 if(pFixedInfo) {
480                         if (GetNetworkParams(pFixedInfo, &dwBufferSize) == NO_ERROR)
481                                 server = pFixedInfo->DnsServerList.IpAddress.String;
482
483                         HeapFree(GetProcessHeap(), 0, pFixedInfo);
484                 }
485         }
486         
487         /* If empty use default to 127.0.0.1 */
488         if(server.empty())
489         {
490                 ServerInstance->Logs->Log("CONFIG",DEFAULT,"No viable nameserver found! Defaulting to nameserver '127.0.0.1'!");
491                 server = "127.0.0.1";
492         }
493         else
494         {
495                 ServerInstance->Logs->Log("CONFIG",DEFAULT,"<dns:server> set to '%s' as first active resolver.", server.c_str());
496         }
497 }
498
499 int clock_gettime(int clock, struct timespec * tv)
500 {
501         if(tv == NULL)
502                 return -1;
503
504         DWORD mstime = timeGetTime();
505         tv->tv_sec   = time(NULL);
506         tv->tv_nsec  = (mstime - (tv->tv_sec * 1000)) * 1000000;
507         return 0;       
508 }
509
510 /* Initialise WMI. Microsoft have the silliest ideas about easy ways to
511  * obtain the CPU percentage of a running process!
512  * The whole API for this uses evil DCOM and is entirely unicode, giving
513  * all results and accepting queries as wide strings.
514  */
515 bool initwmi()
516 {
517         HRESULT hres;
518
519         /* Initialise COM. This can kill babies. */
520         hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
521         if (FAILED(hres))
522                 return false;
523
524         /* COM security. This stuff kills kittens */
525         hres =  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
526                 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
527
528         if (FAILED(hres))
529         {
530                 CoUninitialize();
531                 return false;
532         }
533     
534         /* Instance to COM object */
535         pLoc = NULL;
536         hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
537  
538         if (FAILED(hres))
539         {
540                 CoUninitialize();
541                 return false;
542         }
543
544         pSvc = NULL;
545
546         /* Connect to DCOM server */
547         hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
548     
549         /* That didn't work, maybe no kittens found to kill? */
550         if (FAILED(hres))
551         {
552                 pLoc->Release();
553                 CoUninitialize();
554                 return false;
555         }
556
557         /* Don't even ASK what this does. I'm still not too sure myself. */
558         hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL,
559                 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
560
561         if (FAILED(hres))
562         {
563                 pSvc->Release();
564                 pLoc->Release();     
565                 CoUninitialize();
566                 return false;
567         }
568         return true;
569 }
570
571 void donewmi()
572 {
573         pSvc->Release();
574         pLoc->Release();
575         CoUninitialize();
576 }
577
578 /* Return the CPU usage in percent of this process */
579 int getcpu()
580 {
581         HRESULT hres;
582         int cpu = -1;
583
584         /* Use WQL, similar to SQL, to construct a query that lists the cpu usage and pid of all processes */
585         IEnumWbemClassObject* pEnumerator = NULL;
586
587         BSTR Language = SysAllocString(L"WQL");
588         BSTR Query    = SysAllocString(L"Select PercentProcessorTime,IDProcess from Win32_PerfFormattedData_PerfProc_Process");
589
590         hres = pSvc->ExecQuery(Language, Query, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
591
592         /* Query didn't work */
593         if (!FAILED(hres))
594         {
595                 IWbemClassObject *pclsObj = NULL;
596                 ULONG uReturn = 0;
597
598                 /* Iterate the query results */
599                 while (pEnumerator)
600                 {
601                         VARIANT vtProp;
602                         VariantInit(&vtProp);
603                         /* Next item */
604                         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
605
606                         /* No more items left */
607                         if (uReturn == 0)
608                                 break;
609
610                         /* Find process ID */
611                         hr = pclsObj->Get(L"IDProcess", 0, &vtProp, 0, 0);
612                         if (!FAILED(hr))
613                         {
614                                 /* Matches our process ID? */
615                                 UINT pid = vtProp.uintVal;
616                                 VariantClear(&vtProp);
617                                 if (pid == GetCurrentProcessId())
618                                 {                                       
619                                         /* Get CPU percentage for this process */
620                                         hr = pclsObj->Get(L"PercentProcessorTime", 0, &vtProp, 0, 0);
621                                         if (!FAILED(hr))
622                                         {
623                                                 /* Deal with wide string ickyness. Who in their right
624                                                  * mind puts a number in a bstrVal wide string item?!
625                                                  */
626                                                 cpu = 0;
627                                                 std::wstringstream out(vtProp.bstrVal);
628                                                 out >> cpu;
629                                                 VariantClear(&vtProp);
630                                         }
631                                         pclsObj->Release();
632                                         break;
633                                 }
634                                 pclsObj->Release();
635                         }
636                 }
637
638                 pEnumerator->Release();
639         }
640
641         SysFreeString(Language);
642         SysFreeString(Query);
643
644         return cpu;
645 }
646
647 int random()
648 {
649         return rand();
650 }
651
652 void srandom(unsigned int seed)
653 {
654         srand(seed);
655 }
656
657 int gettimeofday(timeval *tv, void *)
658 {
659         SYSTEMTIME st;
660         GetSystemTime(&st);
661
662         tv->tv_sec = time(NULL);
663         tv->tv_usec = st.wMilliseconds;
664
665         return 0;
666 }
667
668 /* World's largest hack to make reference<> work */
669 #include "../src/modules/m_spanningtree/link.h"
670 #include "../src/modules/ssl.h"
671 static void unused_function()
672 {
673         reference<Link> unused_Link;
674         reference<Autoconnect> unused_Autoconnect;
675         reference<ssl_cert> unused_Cert;
676         reference<OperInfo> unused_OperInfo;
677
678         if (unused_Link)
679                 unused_Link->Port = -1;
680         if (unused_Autoconnect)
681                 unused_Autoconnect->NextConnectTime = -1;
682         if (unused_Cert)
683                 unused_Cert->dn = "";
684         if (unused_OperInfo)
685                 unused_OperInfo->name = "";
686
687         Autoconnect *a = unused_Autoconnect;
688         Link *l = unused_Link;
689         ssl_cert *s = unused_Cert;
690         OperInfo *o = unused_OperInfo;
691
692         unused_Link = reference<Link>(unused_Link);
693         unused_Autoconnect = reference<Autoconnect>(unused_Autoconnect);
694         unused_Cert = reference<ssl_cert>(unused_Cert);
695         unused_OperInfo = reference<OperInfo>(unused_OperInfo);
696
697         unused_Link = reference<Link>(l);
698         unused_Autoconnect = reference<Autoconnect>(a);
699         unused_Cert = reference<ssl_cert>(s);
700         unused_OperInfo = reference<OperInfo>(o);
701
702         unused_Link = l;
703         unused_Autoconnect = a;
704         unused_Cert = s;
705         unused_OperInfo = o;
706
707         delete unused_Link;
708         delete unused_Autoconnect;
709         delete unused_Cert;
710         delete unused_OperInfo;
711 }