utils.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* $Id: utils.c 767 2004-10-06 12:48:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner, Matt Bing.
  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 * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <sys/types.h>
  31. #include <regex.h>
  32. #include "config.h"
  33. #include "tcpreplay.h"
  34. #include "utils.h"
  35. #include "err.h"
  36. extern int maxpacket;
  37. extern struct options options;
  38. int
  39. read_hexstring(char *l2string, char *hex, int hexlen)
  40. {
  41. int numbytes = 0;
  42. unsigned int value;
  43. char *l2byte;
  44. u_char databyte;
  45. char *token = NULL;
  46. if (hexlen <= 0)
  47. errx(1, "Hex buffer must be > 0");
  48. memset(hex, '\0', hexlen);
  49. /* data is hex, comma seperated, byte by byte */
  50. /* get the first byte */
  51. l2byte = strtok_r(l2string, ",", &token);
  52. sscanf(l2byte, "%x", &value);
  53. if (value > 0xff)
  54. errx(1, "Invalid hex byte passed to -2: %s", l2byte);
  55. databyte = (u_char) value;
  56. memcpy(&hex[numbytes], &databyte, 1);
  57. /* get remaining bytes */
  58. while ((l2byte = strtok_r(NULL, ",", &token)) != NULL) {
  59. numbytes++;
  60. if (numbytes + 1 > hexlen) {
  61. warnx("Hex buffer too small for data- skipping data");
  62. return (++numbytes);
  63. }
  64. sscanf(l2byte, "%x", &value);
  65. if (value > 0xff)
  66. errx(1, "Invalid hex byte passed to -2: %s", l2byte);
  67. databyte = (u_char) value;
  68. memcpy(&hex[numbytes], &databyte, 1);
  69. }
  70. numbytes++;
  71. dbg(1, "Read %d bytes of layer 2 data", numbytes);
  72. return (numbytes);
  73. }
  74. /* whorishly appropriated from fragroute-1.2 */
  75. int
  76. argv_create(char *p, int argc, char *argv[])
  77. {
  78. int i;
  79. for (i = 0; i < argc - 1; i++) {
  80. while (*p != '\0' && isspace((int)*p))
  81. *p++ = '\0';
  82. if (*p == '\0')
  83. break;
  84. argv[i] = p;
  85. while (*p != '\0' && !isspace((int)*p))
  86. p++;
  87. }
  88. p[0] = '\0';
  89. argv[i] = NULL;
  90. return (i);
  91. }
  92. /*
  93. * converts a string representation of a MAC address, based on
  94. * non-portable ether_aton()
  95. */
  96. void
  97. mac2hex(const char *mac, char *dst, int len)
  98. {
  99. int i;
  100. long l;
  101. char *pp;
  102. if (len < 6)
  103. return;
  104. while (isspace(*mac))
  105. mac++;
  106. /* expect 6 hex octets separated by ':' or space/NUL if last octet */
  107. for (i = 0; i < 6; i++) {
  108. l = strtol(mac, &pp, 16);
  109. if (pp == mac || l > 0xFF || l < 0)
  110. return;
  111. if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0'))))
  112. return;
  113. dst[i] = (u_char) l;
  114. mac = pp + 1;
  115. }
  116. }
  117. /*
  118. * returns a pointer to the layer 4 header which is just beyond the IP header
  119. */
  120. void *
  121. get_layer4(ip_hdr_t * ip_hdr)
  122. {
  123. void *ptr;
  124. ptr = (u_int32_t *) ip_hdr + ip_hdr->ip_hl;
  125. return ((void *)ptr);
  126. }