user_api.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "defines.h"
  22. #include "common.h"
  23. #include "tcpedit.h"
  24. #include "user.h"
  25. #include "user_api.h"
  26. /**
  27. * \brief Define the libpcap DLT Type value
  28. */
  29. int
  30. tcpedit_user_set_dlt_type(tcpedit_t *tcpedit, uint16_t type)
  31. {
  32. tcpeditdlt_t *ctx;
  33. tcpeditdlt_plugin_t *plugin;
  34. user_config_t *config;
  35. assert(tcpedit);
  36. ctx = tcpedit->dlt_ctx;
  37. assert(ctx);
  38. plugin = ctx->decoder;
  39. assert(plugin);
  40. config = (user_config_t *)plugin->config;
  41. config->dlt = type;
  42. return TCPEDIT_OK;
  43. }
  44. /**
  45. * \brief Define the actual L2 header content.
  46. *
  47. * You need to set the data, it's length and which direction(s) to apply to.
  48. * BOTH - both directions (or in the case of no tcpprep cache file)
  49. * S2C - server to client (primary interface)
  50. * C2S - client to server (secondary interface)
  51. *
  52. * NOTE: the datalen value must be the same between each call.
  53. */
  54. int
  55. tcpedit_user_set_dlink(tcpedit_t *tcpedit, u_char *data, int datalen, tcpedit_user_dlt_direction direction)
  56. {
  57. tcpeditdlt_t *ctx;
  58. tcpeditdlt_plugin_t *plugin;
  59. user_config_t *config;
  60. assert(tcpedit);
  61. ctx = tcpedit->dlt_ctx;
  62. assert(ctx);
  63. plugin = ctx->decoder;
  64. assert(plugin);
  65. config = (user_config_t *)plugin->config;
  66. /* sanity checks */
  67. if (datalen <= 0) {
  68. tcpedit_seterr(tcpedit, "%s", "user datalink length must be > 0");
  69. return TCPEDIT_ERROR;
  70. } else if (datalen > USER_L2MAXLEN) {
  71. tcpedit_seterr(tcpedit, "user datalink length is > %d. Please increase USER_L2MAXLEN", USER_L2MAXLEN);
  72. return TCPEDIT_ERROR;
  73. }
  74. if ((config->length > 0) && (config->length != datalen)) {
  75. tcpedit_seterr(tcpedit, "%s", "Subsequent calls to tcpedit_user_set_dlink() must use the same datalen");
  76. return TCPEDIT_ERROR;
  77. } else {
  78. config->length = datalen;
  79. switch (direction) {
  80. case TCPEDIT_USER_DLT_BOTH:
  81. memcpy(config->l2server, data, datalen);
  82. memcpy(config->l2client, data, datalen);
  83. break;
  84. case TCPEDIT_USER_DLT_S2C:
  85. memcpy(config->l2server, data, datalen);
  86. break;
  87. case TCPEDIT_USER_DLT_C2S:
  88. memcpy(config->l2client, data, datalen);
  89. break;
  90. }
  91. }
  92. return TCPEDIT_OK;
  93. }