loop.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* $Id: loop.c 1842 2007-04-26 04:34:11Z aturner $ */
  2. /*
  3. * Copyright (c) 2006-2007 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "dlt_plugins-int.h"
  34. #include "dlt_utils.h"
  35. #include "loop.h"
  36. #include "../dlt_null/null.h"
  37. #include "tcpedit.h"
  38. #include "common.h"
  39. #include "tcpr.h"
  40. /*
  41. * Basically, DLT_LOOP and DLT_NULL are the same thing except that the PF_ value
  42. * in the header is always network byte order in DLT_LOOP and host byte order
  43. * in DLT_NULL. So since DLT_NULL has to handle both big & little endian values
  44. * we just send all DLT_LOOP processing over there
  45. */
  46. static char dlt_name[] = "loop";
  47. static char _U_ dlt_prefix[] = "loop";
  48. static u_int16_t dlt_value = DLT_LOOP;
  49. /*
  50. * Function to register ourselves. This function is always called, regardless
  51. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  52. * or anything like that (use the dlt_loop_init() function below for that).
  53. * Tasks:
  54. * - Create a new plugin struct
  55. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  56. * actually in the header.
  57. * - Add the plugin to the context's plugin chain
  58. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  59. */
  60. int
  61. dlt_loop_register(tcpeditdlt_t *ctx)
  62. {
  63. tcpeditdlt_plugin_t *plugin;
  64. assert(ctx);
  65. /* create a new plugin structure */
  66. plugin = tcpedit_dlt_newplugin();
  67. /* set what we provide & require */
  68. plugin->provides += PLUGIN_MASK_PROTO;
  69. plugin->requires += 0;
  70. /* what is our DLT value? */
  71. plugin->dlt = dlt_value;
  72. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  73. plugin->name = safe_strdup(dlt_prefix);
  74. /* we actually call all the DLT_NULL functions since NULL and LOOP are basically the same thing */
  75. plugin->plugin_init = dlt_loop_init;
  76. plugin->plugin_cleanup = dlt_null_cleanup;
  77. plugin->plugin_parse_opts = dlt_null_parse_opts;
  78. plugin->plugin_decode = dlt_null_decode;
  79. plugin->plugin_encode = dlt_null_encode;
  80. plugin->plugin_proto = dlt_null_proto;
  81. plugin->plugin_l2addr_type = dlt_null_l2addr_type;
  82. plugin->plugin_l2len = dlt_null_l2len;
  83. plugin->plugin_get_layer3 = dlt_null_get_layer3;
  84. plugin->plugin_merge_layer3 = dlt_null_merge_layer3;
  85. plugin->plugin_get_mac = dlt_null_get_mac;
  86. /* add it to the available plugin list */
  87. return tcpedit_dlt_addplugin(ctx, plugin);
  88. }
  89. /*
  90. * Initializer function. This function is called only once, if and only if
  91. * this plugin will be utilized. Remember, if you need to keep track of any state,
  92. * store it in your plugin->config, not a global!
  93. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  94. */
  95. int
  96. dlt_loop_init(tcpeditdlt_t *ctx)
  97. {
  98. tcpeditdlt_plugin_t *plugin;
  99. null_config_t *config;
  100. assert(ctx);
  101. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  102. tcpedit_seterr(ctx->tcpedit, "Unable to initalize unregistered plugin %s", dlt_name);
  103. return TCPEDIT_ERROR;
  104. }
  105. /* allocate memory for our deocde extra data */
  106. if (sizeof(null_extra_t) > 0)
  107. ctx->decoded_extra = safe_malloc(sizeof(null_extra_t));
  108. /* allocate memory for our config data */
  109. if (sizeof(null_config_t) > 0)
  110. plugin->config = safe_malloc(sizeof(null_config_t));
  111. config = (null_config_t *)plugin->config;
  112. return TCPEDIT_OK; /* success */
  113. }
  114. /* that's all folks! */