utils.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* $Id: utils.c 2103 2009-01-05 05:03:51Z 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. fprintf(stderr, "ERROR in %s:%s() line %d: Unable to malloc() %zu bytes", file, funcname, line, len);
  52. exit(-1);
  53. }
  54. /* zero memory */
  55. memset(ptr, 0, len);
  56. /* wrapped inside an #ifdef for better performance */
  57. dbgx(5, "Malloc'd %zu bytes in %s:%s() line %d", len, file, funcname, line);
  58. return (void *)ptr;
  59. }
  60. /**
  61. * this is wrapped up in a #define safe_realloc
  62. * This function, detects failures to realloc memory and zeros
  63. * out the NEW memory if len > current len. As always, remember
  64. * to use it as:
  65. * ptr = safe_realloc(ptr, size)
  66. */
  67. void *
  68. _our_safe_realloc(void *ptr, size_t len, const char *funcname, const int line, const char *file)
  69. {
  70. if ((ptr = realloc(ptr, len)) == NULL) {
  71. fprintf(stderr, "ERROR: in %s:%s() line %d: Unable to remalloc() buffer to %zu bytes", file, funcname, line, len);
  72. exit(-1);
  73. }
  74. dbgx(5, "Remalloc'd buffer to %zu bytes in %s:%s() line %d", len, file, funcname, line);
  75. return ptr;
  76. }
  77. /**
  78. * this is wrapped up in a #define safe_strdup
  79. * This function, detects failures to realloc memory
  80. */
  81. char *
  82. _our_safe_strdup(const char *str, const char *funcname, const int line, const char *file)
  83. {
  84. char *newstr;
  85. if ((newstr = (char *)malloc(strlen(str) + 1)) == NULL) {
  86. fprintf(stderr, "ERROR in %s:%s() line %d: Unable to strdup() %zu bytes\n", file, funcname, line, strlen(str));
  87. exit(-1);
  88. }
  89. memcpy(newstr, str, strlen(str) + 1);
  90. return newstr;
  91. }
  92. /**
  93. * calls free and sets to NULL.
  94. */
  95. void
  96. _our_safe_free(void *ptr, const char *funcname, const int line, const char *file)
  97. {
  98. if (ptr == NULL) {
  99. fprintf(stderr, "ERROR in %s:%s() line %d: Unable to call free on a NULL ptr", file, funcname, line);
  100. exit(-1);
  101. }
  102. free(ptr);
  103. ptr = NULL;
  104. }
  105. /**
  106. * Print various packet statistics
  107. */
  108. void
  109. packet_stats(struct timeval *begin, struct timeval *end,
  110. COUNTER bytes_sent, COUNTER pkts_sent, COUNTER failed)
  111. {
  112. float bytes_sec = 0.0, mb_sec = 0.0, pkts_sec = 0.0;
  113. double frac_sec;
  114. if (gettimeofday(end, NULL) < 0)
  115. errx(-1, "Unable to gettimeofday(): %s", strerror(errno));
  116. timersub(end, begin, begin);
  117. timer2float(begin, frac_sec);
  118. if (timerisset(begin)) {
  119. if (bytes_sent) {
  120. bytes_sec =
  121. bytes_sent / frac_sec;
  122. mb_sec = (bytes_sec * 8) / (1024 * 1024);
  123. }
  124. if (pkts_sent)
  125. pkts_sec =
  126. pkts_sent / frac_sec;
  127. }
  128. printf("Actual: " COUNTER_SPEC " packets (" COUNTER_SPEC " bytes) sent in %.02f seconds\n",
  129. pkts_sent, bytes_sent, frac_sec);
  130. printf("Rated: %.1f bps, %.2f Mbps, %.2f pps\n",
  131. bytes_sec, mb_sec, pkts_sec);
  132. if (failed)
  133. printf(COUNTER_SPEC " write attempts failed from full buffers and were repeated\n",
  134. failed);
  135. }
  136. /**
  137. * reads a hexstring in the format of xx,xx,xx,xx spits it back into *hex
  138. * up to hexlen bytes. Returns actual number of bytes returned. On error
  139. * it just calls errx() since all errors are fatal.
  140. */
  141. int
  142. read_hexstring(const char *l2string, u_char *hex, const int hexlen)
  143. {
  144. int numbytes = 0;
  145. unsigned int value;
  146. char *l2byte;
  147. u_char databyte;
  148. char *token = NULL;
  149. char *string;
  150. string = safe_strdup(l2string);
  151. if (hexlen <= 0)
  152. err(-1, "Hex buffer must be > 0");
  153. memset(hex, '\0', hexlen);
  154. /* data is hex, comma seperated, byte by byte */
  155. /* get the first byte */
  156. l2byte = strtok_r(string, ",", &token);
  157. sscanf(l2byte, "%x", &value);
  158. if (value > 0xff)
  159. errx(-1, "Invalid hex string byte: %s", l2byte);
  160. databyte = (u_char) value;
  161. memcpy(&hex[numbytes], &databyte, 1);
  162. /* get remaining bytes */
  163. while ((l2byte = strtok_r(NULL, ",", &token)) != NULL) {
  164. numbytes++;
  165. if (numbytes + 1 > hexlen) {
  166. warn("Hex buffer too small for data- skipping data");
  167. return (++numbytes);
  168. }
  169. sscanf(l2byte, "%x", &value);
  170. if (value > 0xff)
  171. errx(-1, "Invalid hex string byte: %s", l2byte);
  172. databyte = (u_char) value;
  173. memcpy(&hex[numbytes], &databyte, 1);
  174. }
  175. numbytes++;
  176. safe_free(string);
  177. dbgx(1, "Read %d bytes of hex data", numbytes);
  178. return (numbytes);
  179. }
  180. #ifdef USE_CUSTOM_INET_ATON
  181. int
  182. inet_aton(const char *name, struct in_addr *addr)
  183. {
  184. in_addr_t a = inet_addr (name);
  185. addr->s_addr = a;
  186. return a != (in_addr_t)-1;
  187. }
  188. #endif