tools.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * @file library/tools.h
  3. * @defgroup util Memory Utility Routines
  4. * @ingroup library
  5. * @{
  6. *
  7. * Portions of this file are copyrighted by:
  8. * Copyright (c) 2016 VMware, Inc. All rights reserved.
  9. * Use is subject to license terms specified in the COPYING file
  10. * distributed with the Net-SNMP package.
  11. */
  12. #ifndef _TOOLS_H
  13. #define _TOOLS_H
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /*
  18. * General acros and constants.
  19. */
  20. #ifdef WIN32
  21. # define SNMP_MAXPATH MAX_PATH
  22. #else
  23. # ifdef PATH_MAX
  24. # define SNMP_MAXPATH PATH_MAX
  25. # else
  26. # ifdef MAXPATHLEN
  27. # define SNMP_MAXPATH MAXPATHLEN
  28. # else
  29. # define SNMP_MAXPATH 1024 /* Should be safe enough */
  30. # endif
  31. # endif
  32. #endif
  33. #define SNMP_MAXBUF (1024 * 4)
  34. #define SNMP_MAXBUF_MEDIUM 1024
  35. #define SNMP_MAXBUF_SMALL 512
  36. #define SNMP_MAXBUF_MESSAGE 1500
  37. #define SNMP_MAXOID 64
  38. #define SNMP_MAX_CMDLINE_OIDS 128
  39. #define SNMP_FILEMODE_CLOSED 0600
  40. #define SNMP_FILEMODE_OPEN 0644
  41. #define BYTESIZE(bitsize) ((bitsize + 7) >> 3)
  42. #define ROUNDUP8(x) ( ( (x+7) >> 3 ) * 8 )
  43. #define SNMP_STRORNULL(x) ( x ? x : "(null)")
  44. /** @def SNMP_FREE(s)
  45. Frees a pointer only if it is !NULL and sets its value to NULL */
  46. #define SNMP_FREE(s) do { if (s) { free(s); s=NULL; } } while(0)
  47. /*
  48. * XXX Not optimal everywhere.
  49. */
  50. /** @def SNMP_MALLOC_STRUCT(s)
  51. Mallocs memory of sizeof(struct s), zeros it and returns a pointer to it. */
  52. #define SNMP_MALLOC_STRUCT(s) (struct s *) calloc(1, sizeof(struct s))
  53. /** @def SNMP_MALLOC_TYPEDEF(t)
  54. Mallocs memory of sizeof(t), zeros it and returns a pointer to it. */
  55. #define SNMP_MALLOC_TYPEDEF(td) (td *) calloc(1, sizeof(td))
  56. /** @def SNMP_ZERO(s,l)
  57. Zeros l bytes of memory starting at s. */
  58. #define SNMP_ZERO(s,l) do { if (s) memset(s, 0, l); } while(0)
  59. /**
  60. * @def NETSNMP_REMOVE_CONST(t, e)
  61. *
  62. * Cast away constness without that gcc -Wcast-qual prints a compiler warning,
  63. * similar to const_cast<> in C++.
  64. *
  65. * @param[in] t A pointer type.
  66. * @param[in] e An expression of a type that can be assigned to the type (const t).
  67. */
  68. #if defined(__GNUC__)
  69. #define NETSNMP_REMOVE_CONST(t, e) \
  70. (__extension__ ({ const t tmp = (e); (t)(size_t)tmp; }))
  71. #else
  72. #define NETSNMP_REMOVE_CONST(t, e) ((t)(size_t)(e))
  73. #endif
  74. #define TOUPPER(c) (c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c)
  75. #define TOLOWER(c) (c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c)
  76. #define HEX2VAL(s) \
  77. ((isalpha(s) ? (TOLOWER(s)-'a'+10) : (TOLOWER(s)-'0')) & 0xf)
  78. #define VAL2HEX(s) ( (s) + (((s) >= 10) ? ('a'-10) : '0') )
  79. /** @def SNMP_MAX(a, b)
  80. Computers the maximum of a and b. */
  81. #define SNMP_MAX(a,b) ((a) > (b) ? (a) : (b))
  82. /** @def SNMP_MIN(a, b)
  83. Computers the minimum of a and b. */
  84. #define SNMP_MIN(a,b) ((a) > (b) ? (b) : (a))
  85. /** @def SNMP_MACRO_VAL_TO_STR(s)
  86. * Expands to string with value of the s.
  87. * If s is macro, the resulting string is value of the macro.
  88. * Example:
  89. * \#define TEST 1234
  90. * SNMP_MACRO_VAL_TO_STR(TEST) expands to "1234"
  91. * SNMP_MACRO_VAL_TO_STR(TEST+1) expands to "1234+1"
  92. */
  93. #define SNMP_MACRO_VAL_TO_STR(s) SNMP_MACRO_VAL_TO_STR_PRIV(s)
  94. #define SNMP_MACRO_VAL_TO_STR_PRIV(s) #s
  95. #ifndef FALSE
  96. #define FALSE 0
  97. #endif
  98. #ifndef TRUE
  99. #define TRUE 1
  100. #endif
  101. #define NETSNMP_IGNORE_RESULT(e) do { if (e) { } } while (0)
  102. /*
  103. * QUIT the FUNction:
  104. * e Error code variable
  105. * l Label to goto to cleanup and get out of the function.
  106. *
  107. * XXX It would be nice if the label could be constructed by the
  108. * preprocessor in context. Limited to a single error return value.
  109. * Temporary hack at best.
  110. */
  111. #define QUITFUN(e, l) \
  112. if ( (e) != SNMPERR_SUCCESS) { \
  113. rval = SNMPERR_GENERR; \
  114. goto l ; \
  115. }
  116. /**
  117. * Compute res = a + b.
  118. *
  119. * @pre a and b must be normalized 'struct timeval' values.
  120. *
  121. * @note res may be the same variable as one of the operands. In other
  122. * words, &a == &res || &b == &res may hold.
  123. */
  124. #define NETSNMP_TIMERADD(a, b, res) do { \
  125. (res)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  126. (res)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  127. if ((res)->tv_usec >= 1000000L) { \
  128. (res)->tv_usec -= 1000000L; \
  129. (res)->tv_sec++; \
  130. } \
  131. } while (0)
  132. /**
  133. * Compute res = a - b.
  134. *
  135. * @pre a and b must be normalized 'struct timeval' values.
  136. *
  137. * @note res may be the same variable as one of the operands. In other
  138. * words, &a == &res || &b == &res may hold.
  139. */
  140. #define NETSNMP_TIMERSUB(a, b, res) do { \
  141. (res)->tv_sec = (a)->tv_sec - (b)->tv_sec - 1; \
  142. (res)->tv_usec = (a)->tv_usec - (b)->tv_usec + 1000000L; \
  143. if ((res)->tv_usec >= 1000000L) { \
  144. (res)->tv_usec -= 1000000L; \
  145. (res)->tv_sec++; \
  146. } \
  147. } while (0)
  148. #define ENGINETIME_MAX 2147483647 /* ((2^31)-1) */
  149. #define ENGINEBOOT_MAX 2147483647 /* ((2^31)-1) */
  150. struct timeval;
  151. /*
  152. * Prototypes.
  153. */
  154. NETSNMP_IMPORT
  155. int snmp_realloc(u_char ** buf, size_t * buf_len);
  156. void free_zero(void *buf, size_t size);
  157. u_char *malloc_random(size_t * size);
  158. u_char *malloc_zero(size_t size);
  159. NETSNMP_IMPORT
  160. void *netsnmp_memdup(const void * from, size_t size);
  161. NETSNMP_IMPORT
  162. void *netsnmp_memdup_nt(const void *from, size_t from_len, size_t *to_len);
  163. void netsnmp_check_definedness(const void *packet,
  164. size_t length);
  165. NETSNMP_IMPORT
  166. u_int netsnmp_binary_to_hex(u_char ** dest, size_t *dest_len,
  167. int allow_realloc,
  168. const u_char * input, size_t len);
  169. NETSNMP_IMPORT
  170. u_int binary_to_hex(const u_char * input, size_t len,
  171. char **output);
  172. /* preferred */
  173. int netsnmp_hex_to_binary(u_char ** buf, size_t * buf_len,
  174. size_t * offset, int allow_realloc,
  175. const char *hex, const char *delim);
  176. /* calls netsnmp_hex_to_binary w/delim of " " */
  177. NETSNMP_IMPORT
  178. int snmp_hex_to_binary(u_char ** buf, size_t * buf_len,
  179. size_t * offset, int allow_realloc,
  180. const char *hex);
  181. /* handles odd lengths */
  182. NETSNMP_IMPORT
  183. int hex_to_binary2(const u_char * input, size_t len,
  184. char **output);
  185. NETSNMP_IMPORT
  186. int snmp_decimal_to_binary(u_char ** buf, size_t * buf_len,
  187. size_t * out_len,
  188. int allow_realloc,
  189. const char *decimal);
  190. #define snmp_cstrcat(b,l,o,a,s) snmp_strcat(b,l,o,a,(const u_char *)s)
  191. NETSNMP_IMPORT
  192. int snmp_strcat(u_char ** buf, size_t * buf_len,
  193. size_t * out_len, int allow_realloc,
  194. const u_char * s);
  195. NETSNMP_IMPORT
  196. char *netsnmp_strdup_and_null(const u_char * from,
  197. size_t from_len);
  198. NETSNMP_IMPORT
  199. void dump_chunk(const char *debugtoken, const char *title,
  200. const u_char * buf, int size);
  201. char *dump_snmpEngineID(const u_char * buf, size_t * buflen);
  202. /** A pointer to an opaque time marker value. */
  203. typedef void *marker_t;
  204. typedef const void* const_marker_t;
  205. NETSNMP_IMPORT
  206. marker_t atime_newMarker(void);
  207. NETSNMP_IMPORT
  208. void atime_setMarker(marker_t pm);
  209. NETSNMP_IMPORT
  210. void netsnmp_get_monotonic_clock(struct timeval* tv);
  211. NETSNMP_IMPORT
  212. void netsnmp_set_monotonic_marker(marker_t *pm);
  213. NETSNMP_IMPORT
  214. long atime_diff(const_marker_t first, const_marker_t second);
  215. NETSNMP_IMPORT
  216. u_long uatime_diff(const_marker_t first, const_marker_t second); /* 1/1000th sec */
  217. NETSNMP_IMPORT
  218. u_long uatime_hdiff(const_marker_t first, const_marker_t second); /* 1/100th sec */
  219. NETSNMP_IMPORT
  220. int atime_ready(const_marker_t pm, int delta_ms);
  221. NETSNMP_IMPORT
  222. int netsnmp_ready_monotonic(const_marker_t pm, int delta_ms);
  223. int uatime_ready(const_marker_t pm, unsigned int delta_ms);
  224. int marker_tticks(const_marker_t pm);
  225. int timeval_tticks(const struct timeval *tv);
  226. NETSNMP_IMPORT
  227. char *netsnmp_getenv(const char *name);
  228. NETSNMP_IMPORT
  229. int netsnmp_setenv(const char *envname, const char *envval,
  230. int overwrite);
  231. int netsnmp_addrstr_hton(char *ptr, size_t len);
  232. NETSNMP_IMPORT
  233. int netsnmp_string_time_to_secs(const char *time_string);
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #endif /* _TOOLS_H */
  238. /** @} */