utils.c 6.1 KB

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