]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Close connections when an I/O hook is configured but not loaded.
[user/henk/code/inspircd.git] / src / dynamic.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Oliver Lupton <oliverlupton@gmail.com>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *   Copyright (C) 2003, 2006 Craig Edwards <craigedwards@brainbox.cc>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25
26 #ifndef _WIN32
27 #include <dlfcn.h>
28 #else
29 #define dlopen(path, state) (void*)LoadLibraryA(path)
30 #define dlsym(handle, export) (void*)GetProcAddress((HMODULE)handle, export)
31 #define dlclose(handle) FreeLibrary((HMODULE)handle)
32 #endif
33
34 DLLManager::DLLManager(const char *fname)
35 {
36         if (!strstr(fname,".so"))
37         {
38                 err = "This doesn't look like a module file to me...";
39                 h = NULL;
40                 return;
41         }
42
43         h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
44         if (!h)
45         {
46                 RetrieveLastError();
47         }
48 }
49
50 DLLManager::~DLLManager()
51 {
52         /* close the library */
53         if (h)
54                 dlclose(h);
55 }
56
57 Module* DLLManager::CallInit()
58 {
59         union
60         {
61                 void* vptr;
62                 Module* (*fptr)();
63         };
64
65         vptr = GetSymbol(MODULE_INIT_STR);
66         if (!vptr)
67                 return NULL;
68
69         return (*fptr)();
70 }
71
72 void* DLLManager::GetSymbol(const char* name)
73 {
74         return h ? dlsym(h, name) : NULL;
75 }
76
77 std::string DLLManager::GetVersion()
78 {
79         const char* srcver = static_cast<const char*>(GetSymbol("inspircd_src_version"));
80         return srcver ? srcver : "";
81 }
82
83 void DLLManager::RetrieveLastError()
84 {
85 #if defined _WIN32
86         char errmsg[500];
87         DWORD dwErrorCode = GetLastError();
88         if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)errmsg, _countof(errmsg), NULL) == 0)
89                 sprintf_s(errmsg, _countof(errmsg), "Error code: %u", dwErrorCode);
90         SetLastError(ERROR_SUCCESS);
91         err = errmsg;
92 #else
93         const char* errmsg = dlerror();
94         err = errmsg ? errmsg : "Unknown error";
95 #endif
96
97         std::string::size_type p;
98         while ((p = err.find_last_of("\r\n")) != std::string::npos)
99                 err.erase(p, 1);
100 }