netdissect.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 1988-1997
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Copyright (c) 1998-2012 Michael Richardson <mcr@tcpdump.org>
  6. * The TCPDUMP project
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that: (1) source code distributions
  10. * retain the above copyright notice and this paragraph in its entirety, (2)
  11. * distributions including binary code include the above copyright notice and
  12. * this paragraph in its entirety in the documentation or other materials
  13. * provided with the distribution, and (3) all advertising materials mentioning
  14. * features or use of this software display the following acknowledgement:
  15. * ``This product includes software developed by the University of California,
  16. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  17. * the University nor the names of its contributors may be used to endorse
  18. * or promote products derived from this software without specific prior
  19. * written permission.
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include <netdissect-stdinc.h>
  28. #include "netdissect.h"
  29. #include <string.h>
  30. #include <stdio.h>
  31. #ifdef USE_LIBSMI
  32. #include <smi.h>
  33. #endif
  34. /*
  35. * Initialize anything that must be initialized before dissecting
  36. * packets.
  37. *
  38. * This should be called at the beginning of the program; it does
  39. * not need to be called, and should not be called, for every
  40. * netdissect_options structure.
  41. */
  42. int
  43. nd_init(char *errbuf, size_t errbuf_size)
  44. {
  45. #ifdef _WIN32
  46. WORD wVersionRequested;
  47. WSADATA wsaData;
  48. int err;
  49. /*
  50. * Request Winsock 2.2; we expect Winsock 2.
  51. */
  52. wVersionRequested = MAKEWORD(2, 2);
  53. err = WSAStartup(wVersionRequested, &wsaData);
  54. if (err != 0) {
  55. strlcpy(errbuf, "Attempting to initialize Winsock failed",
  56. errbuf_size);
  57. return (-1);
  58. }
  59. #endif /* _WIN32 */
  60. #ifdef USE_LIBSMI
  61. /*
  62. * XXX - should we just fail if this fails? Some of the
  63. * libsmi calls may fail.
  64. */
  65. smiInit("tcpdump");
  66. #endif
  67. /*
  68. * Clears the error buffer, and uses it so we don't get
  69. * "unused argument" warnings at compile time.
  70. */
  71. strlcpy(errbuf, "", errbuf_size);
  72. return (0);
  73. }
  74. /*
  75. * Clean up anything that ndo_init() did.
  76. */
  77. void
  78. nd_cleanup(void)
  79. {
  80. #ifdef USE_LIBSMI
  81. /*
  82. * This appears, in libsmi 0.4.8, to do nothing if smiInit()
  83. * wasn't done or failed, so we call it unconditionally.
  84. */
  85. smiExit();
  86. #endif
  87. #ifdef _WIN32
  88. /*
  89. * Undo the WSAStartup() call above.
  90. */
  91. WSACleanup();
  92. #endif
  93. }
  94. int
  95. nd_have_smi_support(void)
  96. {
  97. #ifdef USE_LIBSMI
  98. return (1);
  99. #else
  100. return (0);
  101. #endif
  102. }
  103. /*
  104. * Indicates whether an SMI module has been loaded, so that we can use
  105. * libsmi to translate OIDs.
  106. */
  107. int nd_smi_module_loaded;
  108. int
  109. nd_load_smi_module(const char *module, char *errbuf, size_t errbuf_size)
  110. {
  111. #ifdef USE_LIBSMI
  112. if (smiLoadModule(module) == 0) {
  113. snprintf(errbuf, errbuf_size, "could not load MIB module %s",
  114. module);
  115. return (-1);
  116. }
  117. nd_smi_module_loaded = 1;
  118. return (0);
  119. #else
  120. snprintf(errbuf, errbuf_size, "MIB module %s not loaded: no libsmi support",
  121. module);
  122. return (-1);
  123. #endif
  124. }
  125. const char *
  126. nd_smi_version_string(void)
  127. {
  128. #ifdef USE_LIBSMI
  129. return (smi_version_string);
  130. #else
  131. return (NULL);
  132. #endif
  133. }