Wes Hardaker wrote:
"NC" == Nicklas Chien <[EMAIL PROTECTED]> writes:

NC> The "Tutorial Sections" talk how to compile a module in unix-like
NC> system. Can the net-snmp support dlmod in windows?If yes,how to compile
NC> the module? I had readed the readme.win32,it just says that add the .c
NC> & .h files and rebuild the snmpd not the dynamical load.Thanks

I believe it has been done and is possible.  But I don't know who does
know how.  If you do figure it out, please help us by documenting what
you find!

Net-SNMP doesn't currently support DLMOD for Windows. Last December I spent some time trying to implement it. I modified dlmod.c to support DLLs, and I compiled a module as a DLL. I was able to load the module, but it didn't work because there was a problem with global variables.

I can't go into much detail because it's been a while since I looked at it, but there are issues with global variables and DLLs. For a DLL to access global variables from the application that loaded the DLL (the agent), the global variable may need to be 'shared' using #pragma in MSVC or they have to be imported / exported. The module would also have to import the global variables used in the library (netsnmp.dll).

At the time, I didn't want to try to change all the global variable definitions in the agent as seemed like too much work and I figured it wouldn't be an easy task.

The library has already been put in to a DLL (netsnmp.dll) which was originally done to support the Perl module. Maybe most of the work is already done and it's not as hard as I originally thought.

I lost the notes I wrote while working on this, but I do have a copy of dlmod.c that contains the code to load DLLs. Hopefully it's the latest working version. I have attached a patch.

Hopefully someone can make this work. Once it is figured out it may allow us to easily separate the library DLL in to the multiple libraries used on other systems (libnetsnmp, agent, helpers, mibs, trapd).

Alex

--- dlmod.c	2007-06-21 08:33:56.000000000 -0400
+++ dlmod-win32.c	2007-08-24 12:11:19.000000000 -0400
@@ -28,9 +28,12 @@
 #include "struct.h"
 #include "util_funcs.h"
 
-#if defined(HAVE_DLFCN_H) && ( defined(HAVE_DLOPEN) || defined(HAVE_LIBDL) )
+#if (defined(HAVE_DLFCN_H) && ( defined(HAVE_DLOPEN) || defined(HAVE_LIBDL) ) || defined(WIN32))
 
+#ifndef WIN32
 #include <dlfcn.h>
+#endif
+
 #include "dlmod.h"
 
 static struct dlmod *dlmods = NULL;
@@ -77,10 +80,10 @@
     strncpy(dlmod_path, SNMPDLMODPATH, sizeof(dlmod_path));
     dlmod_path[ sizeof(dlmod_path)-1 ] = 0;
     if (p) {
-        if (p[0] == ':') {
+        if (p[0] == ENV_SEPARATOR_CHAR) {
             len = strlen(dlmod_path);
-            if (dlmod_path[len - 1] != ':') {
-                strncat(dlmod_path, ":", sizeof(dlmod_path) - len -1);
+            if (dlmod_path[len - 1] != ENV_SEPARATOR_CHAR) {
+                strncat(dlmod_path, ENV_SEPARATOR, sizeof(dlmod_path) - len -1);
                 len++;
             }
             strncat(dlmod_path, p + 1,   sizeof(dlmod_path) - len);
@@ -139,7 +142,13 @@
 {
     char            sym_init[64];
     char           *p, tmp_path[255];
+#ifdef WIN32
+    void            (WINAPI *dl_init)(void);
+    char           *tmp_path2 = NULL;
+    HANDLE hInst = NULL;
+#else
     int             (*dl_init) (void);
+#endif
     char           *st;
 
     DEBUGMSGTL(("dlmod", "dlmod_load_module %s: %s\n", dlm->name,
@@ -149,6 +158,65 @@
         (dlm->status != DLMOD_UNLOADED && dlm->status != DLMOD_ERROR))
         return;
 
+#ifdef WIN32
+    if (dlm->path[1] == ':') {          /* driveletter:path */
+      hInst = LoadLibrary(dlm->path);
+
+      if (hInst == NULL)
+      {
+        DEBUGMSGTL(("dlmod","Could not load DLL %s.\n", dlm->path));
+        dlm->status = DLMOD_ERROR;
+        return;
+      }
+      else {
+        DEBUGMSGTL(("dlmod","DLL %s loaded.\n", dlm->path));
+      }     
+    } else {
+      tmp_path[0] = '\0';
+      for (p = strtok_r(dlmod_path, ENV_SEPARATOR, &st); p; p = strtok_r(NULL, ENV_SEPARATOR, &st)) {
+            DEBUGMSGTL(("dlmod", "1: %s 2: %s\n", p, tmp_path));
+
+            tmp_path2 = _strlwr( _strdup( dlm->path ) );
+            if (strstr(tmp_path2,".dll"))
+              snprintf(tmp_path, sizeof(tmp_path), "%s\\%s", p, dlm->path);
+            else
+              snprintf(tmp_path, sizeof(tmp_path), "%s\\%s.dll", p, dlm->path);
+            if (tmp_path2)
+              free (tmp_path2);
+            DEBUGMSGTL(("dlmod", "p: %s tmp_path: %s\n", p, tmp_path));
+            hInst = LoadLibrary(tmp_path);
+
+            if (hInst == NULL)
+            {
+              DEBUGMSGTL(("dlmod","Could not load DLL %s.\n", tmp_path));
+              dlm->status = DLMOD_ERROR;
+            }
+            else {
+              DEBUGMSGTL(("dlmod","DLL %s loaded.\n", tmp_path));
+            }     
+        }
+        strncpy(dlm->path, tmp_path, sizeof(dlm->path));
+        if (dlm->status == DLMOD_ERROR)
+            return;
+    }
+
+    snprintf(sym_init, sizeof(sym_init), "init_%s", dlm->name);
+
+    dl_init = (void (WINAPI *)(void)) GetProcAddress ((HMODULE) hInst, sym_init);
+
+    if (dl_init == NULL) {
+        FreeLibrary(hInst);
+        snprintf(dlm->error, sizeof(dlm->error),
+                 "dlsym failed: can't find \'%s\'", sym_init);
+        dlm->status = DLMOD_ERROR;
+        return;
+    }
+
+    dl_init();
+    dlm->error[0] = '\0';
+    dlm->status = DLMOD_LOADED;
+
+#else   /* WIN32 */
     if (dlm->path[0] == '/') {
 #ifdef RTLD_NOW
         dlm->handle = dlopen(dlm->path, RTLD_NOW);
@@ -162,7 +230,8 @@
             return;
         }
     } else {
-        for (p = strtok_r(dlmod_path, ":", &st); p; p = strtok_r(NULL, ":", &st)) {
+        tmp_path[0] = '\0';
+        for (p = strtok_r(dlmod_path, ENV_SEPARATOR, &st); p; p = strtok_r(NULL, ENV_SEPARATOR, &st)) {
             snprintf(tmp_path, sizeof(tmp_path), "%s/%s.so", p, dlm->path);
             DEBUGMSGTL(("dlmod", "p: %s tmp_path: %s\n", p, tmp_path));
 #ifdef RTLD_NOW
@@ -193,18 +262,34 @@
     dl_init();
     dlm->error[0] = '\0';
     dlm->status = DLMOD_LOADED;
+#endif  /* WIN32 */
 }
 
 void
 dlmod_unload_module(struct dlmod *dlm)
 {
     char            sym_deinit[64];
+#ifdef WIN32
+    void            (WINAPI *dl_deinit)(void);
+    HANDLE hInst = NULL;
+#else
     int             (*dl_deinit) (void);
+#endif
 
     if (!dlm || dlm->status != DLMOD_LOADED)
         return;
 
     snprintf(sym_deinit, sizeof(sym_deinit), "deinit_%s", dlm->name);
+#ifdef WIN32
+    dl_deinit = (void (WINAPI *)(void)) GetProcAddress ((HMODULE) hInst, sym_deinit);
+    if (dl_deinit == NULL) {
+        snprintf(dlm->error, sizeof(dlm->error),
+                 "dlsym failed: can't find \'%s\'", sym_deinit);
+    } else {
+        dl_deinit();
+    }
+    FreeLibrary(hInst);
+#else   /* WIN32 */
     dl_deinit = dlsym(dlm->handle, sym_deinit);
     if (dl_deinit == NULL) {
         snprintf(dlm->error, sizeof(dlm->error),
@@ -213,6 +298,7 @@
         dl_deinit();
     }
     dlclose(dlm->handle);
+#endif  /* WIN32 */
     dlm->status = DLMOD_UNLOADED;
     DEBUGMSGTL(("dlmod", "Module %s unloaded\n", dlm->name));
 }
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to