created SDK installer; other minor changes
[public/netxms.git] / src / server / core / ndd.cpp
CommitLineData
64fd495b
VK
1/*
2** NetXMS - Network Management System
3** Copyright (C) 2003-2011 Victor Kirhenshtein
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** File: ndd.cpp
20**
21**/
22
23#include "nxcore.h"
24
25#define MAX_DEVICE_DRIVERS 1024
26
27
28/**
29 * List of loaded drivers
30 */
31static int s_numDrivers = 0;
32static NetworkDeviceDriver *s_drivers[MAX_DEVICE_DRIVERS];
33static NetworkDeviceDriver *s_defaultDriver = new NetworkDeviceDriver();
34
35/**
36 * Load device driver
37 *
38 * @param file Driver's file name
39 */
40static void LoadDriver(const TCHAR *file)
41{
42 TCHAR errorText[256];
43
44 HMODULE hModule = DLOpen(file, errorText);
45 if (hModule != NULL)
46 {
47 int *apiVersion = (int *)DLGetSymbolAddr(hModule, _T("nddAPIVersion"), errorText);
48 NetworkDeviceDriver *(* CreateInstance)() = (NetworkDeviceDriver *(*)())DLGetSymbolAddr(hModule, _T("nddCreateInstance"), errorText);
49
50 if ((apiVersion != NULL) && (CreateInstance != NULL))
51 {
52 if (*apiVersion == NDDRV_API_VERSION)
53 {
54 NetworkDeviceDriver *driver = CreateInstance();
55 if (driver != NULL)
56 {
57 s_drivers[s_numDrivers++] = driver;
58 nxlog_write(MSG_NDD_LOADED, EVENTLOG_INFORMATION_TYPE, "s", driver->getName());
59 }
60 else
61 {
62 nxlog_write(MSG_NDD_INIT_FAILED, EVENTLOG_ERROR_TYPE, "s", file);
63 DLClose(hModule);
64 }
65 }
66 else
67 {
68 nxlog_write(MSG_NDD_API_VERSION_MISMATCH, EVENTLOG_ERROR_TYPE, "sdd", file, NDDRV_API_VERSION, *apiVersion);
69 DLClose(hModule);
70 }
71 }
72 else
73 {
74 nxlog_write(MSG_NO_NDD_ENTRY_POINT, EVENTLOG_ERROR_TYPE, "s", file);
75 DLClose(hModule);
76 }
77 }
78 else
79 {
80 nxlog_write(MSG_DLOPEN_FAILED, EVENTLOG_ERROR_TYPE, "ss", file, errorText);
81 }
82}
83
84/**
85 * Load all available device drivers
86 */
87void LoadNetworkDeviceDrivers()
88{
89 memset(s_drivers, 0, sizeof(NetworkDeviceDriver *) * MAX_DEVICE_DRIVERS);
90
91 TCHAR path[MAX_PATH];
92 _tcscpy(path, g_szLibDir);
93 _tcscat(path, LDIR_NDD);
94
95 DbgPrintf(1, _T("Loading network device drivers from %s"), path);
96 _TDIR *dir = _topendir(path);
97 if (dir != NULL)
98 {
99 _tcscat(path, FS_PATH_SEPARATOR);
8d7a71be 100 int insPos = (int)_tcslen(path);
64fd495b
VK
101
102 struct _tdirent *f;
103 while((f = _treaddir(dir)) != NULL)
104 {
105 if (MatchString(_T("*.ndd"), f->d_name, FALSE))
106 {
107 _tcscpy(&path[insPos], f->d_name);
108 LoadDriver(path);
109 if (s_numDrivers == MAX_DEVICE_DRIVERS)
110 break; // Too many drivers already loaded
111 }
112 }
113 _tclosedir(dir);
114 }
115 DbgPrintf(1, _T("%d network device drivers loaded"), s_numDrivers);
116}
117
118/**
119 * Find appropriate device driver for given node
120 *
121 * @param node Node object to test
122 * @returns Pointer to device driver object
123 */
124NetworkDeviceDriver *FindDriverForNode(Node *node)
125{
126 for(int i = 0; i < s_numDrivers; i++)
127 if (s_drivers[i]->isDeviceSupported(node->getObjectId()))
128 return s_drivers[i];
129 return s_defaultDriver;
130}