File: plugin.c

package info (click to toggle)
ntop 3:3.3-11
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 12,772 kB
  • ctags: 7,534
  • sloc: ansic: 71,427; sh: 16,772; awk: 1,504; perl: 792; makefile: 782; php: 123; python: 23; sql: 13; sed: 11
file content (330 lines) | stat: -rw-r--r-- 10,315 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 *  Copyright (C) 1998-2007 Luca Deri <deri@ntop.org>
 *
 *  			    http://www.ntop.org/
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "ntop.h"

#ifdef MAKE_STATIC_PLUGIN
extern PluginInfo* icmpPluginEntryFctn(void);
extern PluginInfo* sflowPluginEntryFctn(void);
#ifndef EMBEDDED
extern PluginInfo* rrdPluginEntryFctn(void);
#endif
extern PluginInfo* netflowPluginEntryFctn(void);
#endif

/* ******************* */

#if (defined(HAVE_DIRENT_H) && defined(HAVE_DLFCN_H)) || defined(WIN32) || defined(DARWIN)
static void loadPlugin(char* dirName, char* pluginName) {
  char pluginPath[256];
  char tmpBuf[LEN_GENERAL_WORK_BUFFER];
  int i;
#ifndef WIN32
  void *pluginPtr = NULL;
  void *pluginEntryFctnPtr;
#endif
  PluginInfo* pluginInfo;
  char key[64], value[16];
  int rc;
#ifndef WIN32
  PluginInfo* (*pluginJumpFunc)();
#endif
  FlowFilterList *newFlow,
                 *work,
                 *prev;

  safe_snprintf(__FILE__, __LINE__, pluginPath, sizeof(pluginPath), "%s/%s", dirName != NULL ? dirName : ".", pluginName);

  traceEvent(CONST_TRACE_NOISY, "Loading plugin '%s'", pluginPath);

#ifndef MAKE_STATIC_PLUGIN
  pluginPtr = (void*)dlopen(pluginPath, RTLD_NOW /* RTLD_LAZY */); /* Load the library */

  if(pluginPtr == NULL) {
    traceEvent(CONST_TRACE_WARNING, "Unable to load plugin '%s'", pluginPath);
    traceEvent(CONST_TRACE_WARNING, "Message is '%s'", dlerror());
    return;
  }

  pluginEntryFctnPtr = (void*)dlsym(pluginPtr, "PluginEntryFctn" /* CONST_PLUGIN_ENTRY_FCTN_NAME */);

  if(pluginEntryFctnPtr == NULL) {
#ifdef WIN32
    traceEvent(CONST_TRACE_WARNING, "Unable to locate plugin '%s' entry function [%li]", 
	       pluginPath, GetLastError());
#else
    traceEvent(CONST_TRACE_WARNING, "Unable to locate plugin '%s' entry function [%s]",
	       pluginPath, dlerror());
#endif /* WIN32 */
    return;
  }

  pluginJumpFunc = (PluginInfo*(*)())pluginEntryFctnPtr;
  pluginInfo = pluginJumpFunc();
#else /* MAKE_STATIC_PLUGIN */

  if(strcmp(pluginName, "icmpPlugin") == 0)
    pluginInfo = icmpPluginEntryFctn();
  else if(strcmp(pluginName, "sflowPlugin") == 0)
    pluginInfo = sflowPluginEntryFctn();
  else if(strcmp(pluginName, "netflowPlugin") == 0)
    pluginInfo = netflowPluginEntryFctn();
#ifndef EMBEDDED
  else if(strcmp(pluginName, "rrdPlugin") == 0)
    pluginInfo = rrdPluginEntryFctn();
#endif
  else
    pluginInfo = NULL;

#endif /* MAKE_STATIC_PLUGIN */

  if(pluginInfo == NULL) {
    traceEvent(CONST_TRACE_WARNING, "%s call of plugin '%s' failed",
	       CONST_PLUGIN_ENTRY_FCTN_NAME, pluginPath);
    return;
  }

  if((pluginInfo->pluginNtopVersion == NULL)
     || strcmp(pluginInfo->pluginNtopVersion, VERSION)) {
    traceEvent(CONST_TRACE_WARNING, "Plugin '%s' discarded: compiled for a different ntop version", pluginName);
    traceEvent(CONST_TRACE_WARNING, "Expected ntop version '%s', actual plugin ntop version '%s'.",
	       pluginInfo->pluginNtopVersion == NULL ? "??" : pluginInfo->pluginNtopVersion,
	       VERSION);
    return;
  }

  newFlow = (FlowFilterList*)calloc(1, sizeof(FlowFilterList));
  
  if(newFlow == NULL) {
    traceEvent(CONST_TRACE_FATALERROR, "Not enough memory for plugin flow filter - aborting");
    exit(42); /* Just in case */
  } else {
    newFlow->fcode = (struct bpf_program*)calloc(MAX_NUM_DEVICES, sizeof(struct bpf_program));
    newFlow->flowName = strdup(pluginInfo->pluginName);

    if((pluginInfo->bpfFilter == NULL) || (pluginInfo->bpfFilter[0] == '\0')) {
      if(pluginInfo->pluginFunct != NULL)
	traceEvent(CONST_TRACE_NOISY, "Note: Plugin '%s' has an empty BPF filter (this may not be wrong)", 
		   pluginPath);
      for(i=0; i<myGlobals.numDevices; i++)
	newFlow->fcode[i].bf_insns = NULL;
    } else {
      strncpy(tmpBuf, pluginInfo->bpfFilter, sizeof(tmpBuf));
      tmpBuf[sizeof(tmpBuf)-1] = '\0'; /* just in case bpfFilter is too long... */
      
      for(i=0; i<myGlobals.numDevices; i++) 
	if((!myGlobals.device[i].virtualDevice)
	   && (!myGlobals.device[i].dummyDevice)
	   && (myGlobals.device[i].pcapPtr)
	   ) {
	  traceEvent(CONST_TRACE_NOISY, "Compiling filter '%s' on interface %s", 
		     tmpBuf, myGlobals.device[i].name);
	  rc = pcap_compile(myGlobals.device[i].pcapPtr, 
			    &newFlow->fcode[i], tmpBuf, 1, 
			    myGlobals.device[i].netmask.s_addr);
      
         if(rc < 0) {
	    traceEvent(CONST_TRACE_WARNING, "Plugin '%s' contains a wrong filter specification",
		       pluginPath);
            traceEvent(CONST_TRACE_WARNING, "    \"%s\" on interface %s (%s)",
		       pluginInfo->bpfFilter, 
		       myGlobals.device[i].name,
		       pcap_geterr((myGlobals.device[i].pcapPtr)));
            traceEvent(CONST_TRACE_INFO, "The filter has been discarded");
	    free(newFlow);
	    return;
	  }
	}
    }

#ifndef WIN32
    newFlow->pluginStatus.pluginMemoryPtr = pluginPtr;
#endif
    newFlow->pluginStatus.pluginPtr       = pluginInfo;

    safe_snprintf(__FILE__, __LINE__, key, sizeof(key), "pluginStatus.%s", pluginInfo->pluginName);

    if(fetchPrefsValue(key, value, sizeof(value)) == -1) {
      storePrefsValue(key, pluginInfo->activeByDefault ? "1" : "0");
      newFlow->pluginStatus.activePlugin = pluginInfo->activeByDefault;
    } else {
      if(strcmp(value, "1") == 0) 
	newFlow->pluginStatus.activePlugin = 1;
      else
	newFlow->pluginStatus.activePlugin = 0;
    }

    /* Find where to insert */
    if((work = myGlobals.flowsList) == NULL) {
      myGlobals.flowsList = newFlow;
    } else {
      while((work != NULL) && (strcasecmp(newFlow->flowName, work->flowName) > 0)) {
        prev = work;
        work = work->next;
      }
      if (work == myGlobals.flowsList) {
        /* 1st in chain */
        newFlow->next = myGlobals.flowsList;
        myGlobals.flowsList = newFlow;
      } else {
        /* Insert in chain */
        newFlow->next = prev->next;
        prev->next = newFlow;
      }
    }

    /* traceEvent(CONST_TRACE_INFO, "Adding: %s", pluginInfo->pluginName); */
  }

#ifdef PLUGIN_DEBUG
  traceEvent(CONST_TRACE_INFO, "Plugin '%s' loaded succesfully.", pluginPath);
#endif
}

/* ******************* */

void loadPlugins(void) {
#ifndef WIN32
  char dirPath[256];
  struct dirent* dp;
  int idx;
  DIR* directoryPointer=NULL;
#endif

  if(static_ntop) return;
  
#ifndef MAKE_STATIC_PLUGIN
  for(idx=0; myGlobals.pluginDirs[idx] != NULL; idx++) {
    safe_snprintf(__FILE__, __LINE__, dirPath, sizeof(dirPath), "%s", myGlobals.pluginDirs[idx]);

    directoryPointer = opendir(dirPath);

    if(directoryPointer != NULL)
      break;
  }

  if(directoryPointer == NULL) {
    traceEvent(CONST_TRACE_WARNING, "Unable to find the plugins/ directory");
    traceEvent(CONST_TRACE_INFO, "ntop continues OK, but without any plugins");
    return;
  } else
    traceEvent(CONST_TRACE_INFO, "Searching for plugins in %s", dirPath);

  while((dp = readdir(directoryPointer)) != NULL) {
    if(dp->d_name[0] == '.')
      continue;
    else if(strlen(dp->d_name) < strlen(CONST_PLUGIN_EXTENSION))
      continue;
    else if(strcmp(&dp->d_name[strlen(dp->d_name)-strlen(CONST_PLUGIN_EXTENSION)],
		   CONST_PLUGIN_EXTENSION))
      continue;
    
    loadPlugin(dirPath, dp->d_name);
  }

  closedir(directoryPointer);
#else /* MAKE_STATIC_PLUGIN */
  loadPlugin(NULL, "icmpPlugin");
  loadPlugin(NULL, "sflowPlugin");
  loadPlugin(NULL, "netflowPlugin");
#ifndef EMBEDDED
  loadPlugin(NULL, "rrdPlugin");
#endif
#endif /* MAKE_STATIC_PLUGIN */
}

/* ******************* */

void unloadPlugins(void) {
  FlowFilterList *flows = myGlobals.flowsList;

  if(static_ntop) return;

  traceEvent(CONST_TRACE_INFO, "PLUGIN_TERM: Unloading plugins (if any)");

  while(flows != NULL) {
    if(flows->pluginStatus.pluginMemoryPtr != NULL) {
#ifdef PLUGIN_DEBUG
      traceEvent(CONST_TRACE_INFO, "PLUGIN_TERM: Unloading plugin '%s'",
		 flows->pluginStatus.pluginPtr->pluginName);
#endif
      if((flows->pluginStatus.pluginPtr->termFunct != NULL)
	 && (flows->pluginStatus.activePlugin))
	flows->pluginStatus.pluginPtr->termFunct(1 /* term ntop */);

#ifndef MAKE_STATIC_PLUGIN
#ifdef WIN32
      FreeLibrary((HANDLE)flows->pluginStatus.pluginMemoryPtr);
#else
      dlclose(flows->pluginStatus.pluginMemoryPtr);
#endif /* WIN32 */
#endif
      flows->pluginStatus.pluginPtr       = NULL;
      flows->pluginStatus.pluginMemoryPtr = NULL;
    }

    flows = flows->next;
  }
}

#endif /* defined(HAVE_DIRENT_H) && defined(HAVE_DLFCN_H) */

/* ******************************* */

/* Courtesy of Andreas Pfaller <apfaller@yahoo.com.au> */

void startPlugins(void) {
  FlowFilterList *flows = myGlobals.flowsList;

  if(static_ntop) return;

  traceEvent(CONST_TRACE_INFO, "Calling plugin start functions (if any)");

  while(flows != NULL) {
    if(flows->pluginStatus.pluginPtr != NULL) {
      traceEvent(CONST_TRACE_NOISY, "Starting '%s'",
		 flows->pluginStatus.pluginPtr->pluginName);
      if((flows->pluginStatus.pluginPtr->startFunct != NULL)
	 && (flows->pluginStatus.activePlugin)) {
	int rc = flows->pluginStatus.pluginPtr->startFunct();
	if(rc != 0)
	  flows->pluginStatus.activePlugin = 0;
      }
    }
    flows = flows->next;
  }
}

/* ******************* */

void handlePluginHostCreationDeletion(HostTraffic *el, u_short deviceId, u_char hostCreation) {
  FlowFilterList *flows = myGlobals.flowsList;

  while(flows != NULL) {
    if(flows->pluginStatus.pluginMemoryPtr != NULL) {
      if(flows->pluginStatus.activePlugin 
	 && (flows->pluginStatus.pluginPtr->crtDltFunct != NULL))
	flows->pluginStatus.pluginPtr->crtDltFunct(el, deviceId, hostCreation);
    }
    
    flows = flows->next;
  }
}