callback.c
上传用户:cxs890
上传日期:2021-05-22
资源大小:347k
文件大小:3k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /* callback.c: A generic callback mechanism */
  2. #include <config.h>
  3. #include <sys/types.h>
  4. #include <stdio.h>
  5. #if HAVE_STDLIB_H
  6. #include <stdlib.h>
  7. #endif
  8. #if HAVE_WINSOCK_H
  9. #include <ip/socket.h>
  10. #endif
  11. #if HAVE_NETINET_IN_H
  12. #include <netinet/in.h>
  13. #endif
  14. #if HAVE_STRING_H
  15. #include <string.h>
  16. #else
  17. #include <strings.h>
  18. #endif
  19. #if HAVE_DMALLOC_H
  20. #include <dmalloc.h>
  21. #endif
  22. #include "tools.h"
  23. #include "callback.h"
  24. #include "asn1.h"
  25. #include "snmp_api.h"
  26. #include "snmp_debug.h"
  27. static struct snmp_gen_callback *thecallbacks[MAX_CALLBACK_IDS][MAX_CALLBACK_SUBIDS];
  28. /* the chicken. or the egg.  You pick. */
  29. void
  30. init_callbacks(void) {
  31.   /* probably not needed? Should be full of 0's anyway? */
  32.   /* (poses a problem if you put init_callbacks() inside of
  33.      init_snmp() and then want the app to register a callback before
  34.      init_snmp() is called in the first place.  -- Wes */
  35.   /* memset(thecallbacks, 0, sizeof(thecallbacks)); */
  36. }
  37. int
  38. snmp_register_callback(int major, int minor, SNMPCallback *new_callback,
  39.                        void *arg) {
  40.   struct snmp_gen_callback *scp;
  41.   
  42.   if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
  43.     return SNMPERR_GENERR;
  44.   }
  45.   
  46.   if (thecallbacks[major][minor] != NULL) {
  47.     /* get to the end of the list */
  48.     for(scp = thecallbacks[major][minor]; scp->next != NULL; scp = scp->next);
  49.     /* mallocate a new entry */
  50.     scp->next = SNMP_MALLOC_STRUCT(snmp_gen_callback);
  51.     scp = scp->next;
  52.   } else {
  53.     /* mallocate a new entry */
  54.     scp = SNMP_MALLOC_STRUCT(snmp_gen_callback);
  55.     /* make the new node the head */
  56.     thecallbacks[major][minor] = scp;
  57.   }
  58.   if (scp == NULL)
  59.     return SNMPERR_GENERR;
  60.   scp->sc_client_arg = arg;
  61.   scp->sc_callback = new_callback;
  62.   DEBUGMSGTL(("callback","registered callback for maj=%d min=%dn",
  63.               major, minor));
  64.   return SNMPERR_SUCCESS;
  65. }
  66. int
  67. snmp_call_callbacks(int major, int minor, void *caller_arg) {
  68.   struct snmp_gen_callback *scp;
  69.   if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
  70.     return SNMPERR_GENERR;
  71.   }
  72.   DEBUGMSGTL(("callback","START calling callbacks for maj=%d min=%dn",
  73.               major, minor));
  74.   /* for each registered callback of type major and minor */
  75.   for(scp = thecallbacks[major][minor]; scp != NULL; scp = scp->next) {
  76.     DEBUGMSGTL(("callback","calling a callback for maj=%d min=%dn",
  77.                 major, minor));
  78.     /* call them */
  79.     (*(scp->sc_callback))(major, minor, caller_arg, scp->sc_client_arg);
  80.   }
  81.   
  82.   DEBUGMSGTL(("callback","END calling callbacks for maj=%d min=%dn",
  83.               major, minor));
  84.   return SNMPERR_SUCCESS;
  85. }