utils.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* $Id: utils.c 1462 2006-04-13 05:10:27Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2005 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 "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #ifdef DEBUG
  35. extern int debug;
  36. #endif
  37. /*
  38. * this is wrapped up in a #define safe_malloc
  39. * This function, detects failures to malloc memory and zeros out the
  40. * memory before returning
  41. */
  42. void *
  43. _our_safe_malloc(size_t len, const char *funcname, const int line, const char *file)
  44. {
  45. u_char *ptr;
  46. if ((ptr = malloc(len)) == NULL)
  47. _our_verbose_errx(1, "Unable to malloc() %d bytes", funcname, line, file, len);
  48. /* zero memory */
  49. memset(ptr, 0, len);
  50. /* wrapped inside an #ifdef for better performance */
  51. dbgx(5, "Malloc'd %d bytes in %s:%s() line %d", len, file, funcname, line);
  52. return (void *)ptr;
  53. }
  54. /*
  55. * this is wrapped up in a #define safe_realloc
  56. * This function, detects failures to realloc memory and zeros
  57. * out the NEW memory if len > current len
  58. */
  59. void *
  60. _our_safe_realloc(void *ptr, size_t len, const char *funcname, const int line, const char *file)
  61. {
  62. if ((ptr = realloc(ptr, len)) == NULL)
  63. _our_verbose_errx(1, "Unable to remalloc() buffer to %d bytes",
  64. funcname, line, file, len);
  65. dbgx(5, "Remalloc'd buffer to %d bytes in %s:%s() line %d", len, file, funcname, line);
  66. return ptr;
  67. }
  68. /*
  69. * this is wrapped up in a #define safe_strdup
  70. * This function, detects failures to realloc memory
  71. */
  72. char *
  73. _our_safe_strdup(const char *str, const char *funcname, const int line, const char *file)
  74. {
  75. char *newstr;
  76. if ((newstr = (char *)malloc(strlen(str) + 1)) == NULL)
  77. _our_verbose_errx(1, "Unable to strdup() %d bytes\n",
  78. funcname, line, file, strlen(str));
  79. memcpy(newstr, str, strlen(str) + 1);
  80. return newstr;
  81. }
  82. void
  83. packet_stats(struct timeval *begin, struct timeval *end,
  84. COUNTER bytes_sent, COUNTER pkts_sent, COUNTER failed)
  85. {
  86. float bytes_sec = 0.0, mb_sec = 0.0, pkts_sec = 0.0;
  87. char bits[3];
  88. if (gettimeofday(end, NULL) < 0)
  89. errx(1, "Unable to gettimeofday(): %s", strerror(errno));
  90. timersub(end, begin, begin);
  91. if (timerisset(begin)) {
  92. if (bytes_sent) {
  93. bytes_sec =
  94. bytes_sent / (begin->tv_sec + (float)begin->tv_usec / 1000000);
  95. mb_sec = (bytes_sec * 8) / (1024 * 1024);
  96. }
  97. if (pkts_sent)
  98. pkts_sec =
  99. pkts_sent / (begin->tv_sec + (float)begin->tv_usec / 1000000);
  100. }
  101. snprintf(bits, sizeof(bits), "%u", begin->tv_usec);
  102. notice("Actual: " COUNTER_SPEC " packets (" COUNTER_SPEC " bytes) sent in %d.%s seconds",
  103. pkts_sent, bytes_sent, begin->tv_sec, bits);
  104. notice("Rated: %.1f bps, %.2f Mbps/sec, %.2f pps\n",
  105. bytes_sec, mb_sec, pkts_sec);
  106. if (failed)
  107. warnx(COUNTER_SPEC " write attempts failed from full buffers and were repeated\n",
  108. failed);
  109. }
  110. int
  111. read_hexstring(const char *l2string, u_char *hex, const int hexlen)
  112. {
  113. int numbytes = 0;
  114. unsigned int value;
  115. char *l2byte;
  116. u_char databyte;
  117. char *token = NULL;
  118. char *string;
  119. string = safe_strdup(l2string);
  120. if (hexlen <= 0)
  121. err(1, "Hex buffer must be > 0");
  122. memset(hex, '\0', hexlen);
  123. /* data is hex, comma seperated, byte by byte */
  124. /* get the first byte */
  125. l2byte = strtok_r(string, ",", &token);
  126. sscanf(l2byte, "%x", &value);
  127. if (value > 0xff)
  128. errx(1, "Invalid hex byte passed to -2: %s", l2byte);
  129. databyte = (u_char) value;
  130. memcpy(&hex[numbytes], &databyte, 1);
  131. /* get remaining bytes */
  132. while ((l2byte = strtok_r(NULL, ",", &token)) != NULL) {
  133. numbytes++;
  134. if (numbytes + 1 > hexlen) {
  135. warn("Hex buffer too small for data- skipping data");
  136. return (++numbytes);
  137. }
  138. sscanf(l2byte, "%x", &value);
  139. if (value > 0xff)
  140. errx(1, "Invalid hex byte passed to -2: %s", l2byte);
  141. databyte = (u_char) value;
  142. memcpy(&hex[numbytes], &databyte, 1);
  143. }
  144. numbytes++;
  145. free(string);
  146. dbgx(1, "Read %d bytes of hex data", numbytes);
  147. return (numbytes);
  148. }
  149. /* whorishly appropriated from fragroute-1.2 */
  150. int
  151. argv_create(char *p, int argc, char *argv[])
  152. {
  153. int i;
  154. for (i = 0; i < argc - 1; i++) {
  155. while (*p != '\0' && isspace((int)*p))
  156. *p++ = '\0';
  157. if (*p == '\0')
  158. break;
  159. argv[i] = p;
  160. while (*p != '\0' && !isspace((int)*p))
  161. p++;
  162. }
  163. p[0] = '\0';
  164. argv[i] = NULL;
  165. return (i);
  166. }
  167. /*
  168. Local Variables:
  169. mode:c
  170. indent-tabs-mode:nil
  171. c-basic-offset:4
  172. End:
  173. */