utils.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* $Id: utils.c 1921 2007-10-25 18:18:50Z 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. #include <string.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <ctype.h>
  38. #ifdef DEBUG
  39. extern int debug;
  40. #endif
  41. /**
  42. * this is wrapped up in a #define safe_malloc
  43. * This function, detects failures to malloc memory and zeros out the
  44. * memory before returning
  45. */
  46. void *
  47. _our_safe_malloc(size_t len, const char *funcname, const int line, const char *file)
  48. {
  49. u_char *ptr;
  50. if ((ptr = malloc(len)) == NULL)
  51. _our_verbose_errx(1, "Unable to malloc() %d bytes", funcname, line, file, len);
  52. /* zero memory */
  53. memset(ptr, 0, len);
  54. /* wrapped inside an #ifdef for better performance */
  55. dbgx(5, "Malloc'd %d bytes in %s:%s() line %d", len, file, funcname, line);
  56. return (void *)ptr;
  57. }
  58. /**
  59. * this is wrapped up in a #define safe_realloc
  60. * This function, detects failures to realloc memory and zeros
  61. * out the NEW memory if len > current len. As always, remember
  62. * to use it as:
  63. * ptr = safe_realloc(ptr, size)
  64. */
  65. void *
  66. _our_safe_realloc(void *ptr, size_t len, const char *funcname, const int line, const char *file)
  67. {
  68. if ((ptr = realloc(ptr, len)) == NULL)
  69. _our_verbose_errx(1, "Unable to remalloc() buffer to %d bytes",
  70. funcname, line, file, len);
  71. dbgx(5, "Remalloc'd buffer to %d bytes in %s:%s() line %d", len, file, funcname, line);
  72. return ptr;
  73. }
  74. /**
  75. * this is wrapped up in a #define safe_strdup
  76. * This function, detects failures to realloc memory
  77. */
  78. char *
  79. _our_safe_strdup(const char *str, const char *funcname, const int line, const char *file)
  80. {
  81. char *newstr;
  82. if ((newstr = (char *)malloc(strlen(str) + 1)) == NULL)
  83. _our_verbose_errx(1, "Unable to strdup() %d bytes\n",
  84. funcname, line, file, strlen(str));
  85. memcpy(newstr, str, strlen(str) + 1);
  86. return newstr;
  87. }
  88. /**
  89. * calls free and sets to NULL.
  90. */
  91. void
  92. _our_safe_free(void *ptr, const char *funcname, const int line, const char *file)
  93. {
  94. if (ptr == NULL)
  95. _our_verbose_errx(1, "Unable to call free on a NULL ptr", funcname, line, file);
  96. free(ptr);
  97. ptr = NULL;
  98. }
  99. /**
  100. * Print various packet statistics
  101. */
  102. void
  103. packet_stats(struct timeval *begin, struct timeval *end,
  104. COUNTER bytes_sent, COUNTER pkts_sent, COUNTER failed)
  105. {
  106. float bytes_sec = 0.0, mb_sec = 0.0, pkts_sec = 0.0;
  107. char bits[3];
  108. if (gettimeofday(end, NULL) < 0)
  109. errx(1, "Unable to gettimeofday(): %s", strerror(errno));
  110. timersub(end, begin, begin);
  111. if (timerisset(begin)) {
  112. if (bytes_sent) {
  113. bytes_sec =
  114. bytes_sent / (begin->tv_sec + (float)begin->tv_usec / 1000000);
  115. mb_sec = (bytes_sec * 8) / (1024 * 1024);
  116. }
  117. if (pkts_sent)
  118. pkts_sec =
  119. pkts_sent / (begin->tv_sec + (float)begin->tv_usec / 1000000);
  120. }
  121. snprintf(bits, sizeof(bits), "%u", begin->tv_usec);
  122. notice("Actual: " COUNTER_SPEC " packets (" COUNTER_SPEC " bytes) sent in %d.%s seconds",
  123. pkts_sent, bytes_sent, begin->tv_sec, bits);
  124. notice("Rated: %.1f bps, %.2f Mbps/sec, %.2f pps\n",
  125. bytes_sec, mb_sec, pkts_sec);
  126. if (failed)
  127. warnx(COUNTER_SPEC " write attempts failed from full buffers and were repeated\n",
  128. failed);
  129. }
  130. /**
  131. * reads a hexstring in the format of xx,xx,xx,xx spits it back into *hex
  132. * up to hexlen bytes. Returns actual number of bytes returned. On error
  133. * it just calls errx() since all errors are fatal.
  134. */
  135. int
  136. read_hexstring(const char *l2string, u_char *hex, const int hexlen)
  137. {
  138. int numbytes = 0;
  139. unsigned int value;
  140. char *l2byte;
  141. u_char databyte;
  142. char *token = NULL;
  143. char *string;
  144. string = safe_strdup(l2string);
  145. if (hexlen <= 0)
  146. err(1, "Hex buffer must be > 0");
  147. memset(hex, '\0', hexlen);
  148. /* data is hex, comma seperated, byte by byte */
  149. /* get the first byte */
  150. l2byte = strtok_r(string, ",", &token);
  151. sscanf(l2byte, "%x", &value);
  152. if (value > 0xff)
  153. errx(1, "Invalid hex string byte: %s", l2byte);
  154. databyte = (u_char) value;
  155. memcpy(&hex[numbytes], &databyte, 1);
  156. /* get remaining bytes */
  157. while ((l2byte = strtok_r(NULL, ",", &token)) != NULL) {
  158. numbytes++;
  159. if (numbytes + 1 > hexlen) {
  160. warn("Hex buffer too small for data- skipping data");
  161. return (++numbytes);
  162. }
  163. sscanf(l2byte, "%x", &value);
  164. if (value > 0xff)
  165. errx(1, "Invalid hex string byte: %s", l2byte);
  166. databyte = (u_char) value;
  167. memcpy(&hex[numbytes], &databyte, 1);
  168. }
  169. numbytes++;
  170. safe_free(string);
  171. dbgx(1, "Read %d bytes of hex data", numbytes);
  172. return (numbytes);
  173. }
  174. /**
  175. * whorishly appropriated from fragroute-1.2. Parse a string and
  176. * create an argv[] array.
  177. */
  178. int
  179. argv_create(char *p, int argc, char *argv[])
  180. {
  181. int i;
  182. for (i = 0; i < argc - 1; i++) {
  183. while (*p != '\0' && isspace((int)*p))
  184. *p++ = '\0';
  185. if (*p == '\0')
  186. break;
  187. argv[i] = p;
  188. while (*p != '\0' && !isspace((int)*p))
  189. p++;
  190. }
  191. p[0] = '\0';
  192. argv[i] = NULL;
  193. return (i);
  194. }
  195. #ifdef USE_CUSTOM_INET_ATON
  196. int
  197. inet_aton(const char *name, struct in_addr *addr)
  198. {
  199. in_addr_t a = inet_addr (name);
  200. addr->s_addr = a;
  201. return a != (in_addr_t)-1;
  202. }
  203. #endif
  204. /*
  205. Local Variables:
  206. mode:c
  207. indent-tabs-mode:nil
  208. c-basic-offset:4
  209. End:
  210. */