loop.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "tcpedit.h"
  22. #include "common.h"
  23. #include "tcpr.h"
  24. #include "dlt_utils.h"
  25. #include "tcpedit_stub.h"
  26. #include "loop.h"
  27. #include "../dlt_null/null.h"
  28. /*
  29. * Basically, DLT_LOOP and DLT_NULL are the same thing except that the PF_ value
  30. * in the header is always network byte order in DLT_LOOP and host byte order
  31. * in DLT_NULL. So since DLT_NULL has to handle both big & little endian values
  32. * we just send all DLT_LOOP processing over there
  33. */
  34. static char dlt_name[] = "loop";
  35. static char _U_ dlt_prefix[] = "loop";
  36. static uint16_t dlt_value = DLT_LOOP;
  37. /*
  38. * Function to register ourselves. This function is always called, regardless
  39. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  40. * or anything like that (use the dlt_loop_init() function below for that).
  41. * Tasks:
  42. * - Create a new plugin struct
  43. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  44. * actually in the header.
  45. * - Add the plugin to the context's plugin chain
  46. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  47. */
  48. int
  49. dlt_loop_register(tcpeditdlt_t *ctx)
  50. {
  51. tcpeditdlt_plugin_t *plugin;
  52. assert(ctx);
  53. /* create a new plugin structure */
  54. plugin = tcpedit_dlt_newplugin();
  55. /* set what we provide & require */
  56. plugin->provides += PLUGIN_MASK_PROTO;
  57. plugin->requires += 0;
  58. /* what is our DLT value? */
  59. plugin->dlt = dlt_value;
  60. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  61. plugin->name = safe_strdup(dlt_prefix);
  62. /* we actually call all the DLT_NULL functions since NULL and LOOP are basically the same thing */
  63. plugin->plugin_init = dlt_loop_init;
  64. plugin->plugin_cleanup = dlt_null_cleanup;
  65. plugin->plugin_parse_opts = dlt_null_parse_opts;
  66. plugin->plugin_decode = dlt_null_decode;
  67. plugin->plugin_encode = dlt_null_encode;
  68. plugin->plugin_proto = dlt_null_proto;
  69. plugin->plugin_l2addr_type = dlt_null_l2addr_type;
  70. plugin->plugin_l2len = dlt_null_l2len;
  71. plugin->plugin_get_layer3 = dlt_null_get_layer3;
  72. plugin->plugin_merge_layer3 = dlt_null_merge_layer3;
  73. plugin->plugin_get_mac = dlt_null_get_mac;
  74. /* add it to the available plugin list */
  75. return tcpedit_dlt_addplugin(ctx, plugin);
  76. }
  77. /*
  78. * Initializer function. This function is called only once, if and only if
  79. * this plugin will be utilized. Remember, if you need to keep track of any state,
  80. * store it in your plugin->config, not a global!
  81. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  82. */
  83. int
  84. dlt_loop_init(tcpeditdlt_t *ctx)
  85. {
  86. tcpeditdlt_plugin_t *plugin;
  87. assert(ctx);
  88. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  89. tcpedit_seterr(ctx->tcpedit, "Unable to initialize unregistered plugin %s", dlt_name);
  90. return TCPEDIT_ERROR;
  91. }
  92. return TCPEDIT_OK; /* success */
  93. }
  94. /* that's all folks! */