net-snmp 5.7
tools.h
Go to the documentation of this file.
00001 
00008 #ifndef _TOOLS_H
00009 #define _TOOLS_H
00010 
00011 #ifdef HAVE_INTTYPES_H
00012 #include <inttypes.h> /* uintptr_t */
00013 #endif
00014 
00015 #ifdef __cplusplus
00016 extern          "C" {
00017 #endif
00018 
00019 
00020 
00021     /*
00022      * General acros and constants.
00023      */
00024 #ifdef WIN32
00025 #  define SNMP_MAXPATH MAX_PATH
00026 #else
00027 #  ifdef PATH_MAX
00028 #    define SNMP_MAXPATH PATH_MAX
00029 #  else
00030 #    ifdef MAXPATHLEN
00031 #      define SNMP_MAXPATH MAXPATHLEN
00032 #    else
00033 #      define SNMP_MAXPATH 1024         /* Should be safe enough */
00034 #    endif
00035 #  endif
00036 #endif
00037 
00038 #define SNMP_MAXBUF             (1024 * 4)
00039 #define SNMP_MAXBUF_MEDIUM      1024
00040 #define SNMP_MAXBUF_SMALL       512
00041 
00042 #define SNMP_MAXBUF_MESSAGE     1500
00043 
00044 #define SNMP_MAXOID             64
00045 #define SNMP_MAX_CMDLINE_OIDS   128
00046 
00047 #define SNMP_FILEMODE_CLOSED    0600
00048 #define SNMP_FILEMODE_OPEN      0644
00049 
00050 #define BYTESIZE(bitsize)       ((bitsize + 7) >> 3)
00051 #define ROUNDUP8(x)             ( ( (x+7) >> 3 ) * 8 )
00052 
00053 #define SNMP_STRORNULL(x)       ( x ? x : "(null)")
00054 
00057 #define SNMP_FREE(s)    do { if (s) { free((void *)s); s=NULL; } } while(0)
00058 
00061 #define SNMP_SWIPE_MEM(n,s) do { if (n) free((void *)n); n = s; s=NULL; } while(0)
00062 
00063     /*
00064      * XXX Not optimal everywhere. 
00065      */
00068 #define SNMP_MALLOC_STRUCT(s)   (struct s *) calloc(1, sizeof(struct s))
00069 
00072 #define SNMP_MALLOC_TYPEDEF(td)  (td *) calloc(1, sizeof(td))
00073 
00076 #define SNMP_ZERO(s,l)  do { if (s) memset(s, 0, l); } while(0)
00077 
00078 
00088 #if defined(__GNUC__)
00089 #define NETSNMP_REMOVE_CONST(t, e)                                      \
00090     (__extension__ ({ const t tmp = (e); (t)(uintptr_t)tmp; }))
00091 #else
00092 #define NETSNMP_REMOVE_CONST(t, e) ((t)(uintptr_t)(e))
00093 #endif
00094 
00095 
00096 #define TOUPPER(c)      (c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c)
00097 #define TOLOWER(c)      (c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c)
00098 
00099 #define HEX2VAL(s) \
00100         ((isalpha(s) ? (TOLOWER(s)-'a'+10) : (TOLOWER(s)-'0')) & 0xf)
00101 #define VAL2HEX(s)      ( (s) + (((s) >= 10) ? ('a'-10) : '0') )
00102 
00103 
00106 #define SNMP_MAX(a,b) ((a) > (b) ? (a) : (b))
00107 
00110 #define SNMP_MIN(a,b) ((a) > (b) ? (b) : (a))
00111 
00120 #define SNMP_MACRO_VAL_TO_STR(s) SNMP_MACRO_VAL_TO_STR_PRIV(s)  
00121 #define SNMP_MACRO_VAL_TO_STR_PRIV(s) #s
00122         
00123 #ifndef FALSE
00124 #define FALSE 0
00125 #endif
00126 #ifndef TRUE
00127 #define TRUE  1
00128 #endif
00129 
00130     /*
00131      * QUIT the FUNction:
00132      *      e       Error code variable
00133      *      l       Label to goto to cleanup and get out of the function.
00134      *
00135      * XXX  It would be nice if the label could be constructed by the
00136      *      preprocessor in context.  Limited to a single error return value.
00137      *      Temporary hack at best.
00138      */
00139 #define QUITFUN(e, l)                   \
00140         if ( (e) != SNMPERR_SUCCESS) {  \
00141                 rval = SNMPERR_GENERR;  \
00142                 goto l ;                \
00143         }
00144 
00145     /*
00146      * DIFFTIMEVAL
00147      *      Set <diff> to the difference between <now> (current) and <then> (past).
00148      *
00149      * ASSUMES that all inputs are (struct timeval)'s.
00150      * Cf. system.c:calculate_time_diff().
00151      */
00152 #define DIFFTIMEVAL(now, then, diff)                    \
00153 {                                                       \
00154         now.tv_sec--;                                   \
00155         now.tv_usec += 1000000L;                        \
00156         diff.tv_sec  = now.tv_sec  - then.tv_sec;       \
00157         diff.tv_usec = now.tv_usec - then.tv_usec;      \
00158         if (diff.tv_usec > 1000000L){                   \
00159                 diff.tv_usec -= 1000000L;               \
00160                 diff.tv_sec++;                          \
00161         }                                               \
00162 }
00163 
00172 #define NETSNMP_TIMERADD(a, b, res)                  \
00173 {                                                    \
00174     (res)->tv_sec  = (a)->tv_sec  + (b)->tv_sec;     \
00175     (res)->tv_usec = (a)->tv_usec + (b)->tv_usec;    \
00176     if ((res)->tv_usec >= 1000000L) {                \
00177         (res)->tv_usec -= 1000000L;                  \
00178         (res)->tv_sec++;                             \
00179     }                                                \
00180 }
00181 
00190 #define NETSNMP_TIMERSUB(a, b, res)                             \
00191 {                                                               \
00192     (res)->tv_sec  = (a)->tv_sec  - (b)->tv_sec - 1;            \
00193     (res)->tv_usec = (a)->tv_usec - (b)->tv_usec + 1000000L;    \
00194     if ((res)->tv_usec >= 1000000L) {                           \
00195         (res)->tv_usec -= 1000000L;                             \
00196         (res)->tv_sec++;                                        \
00197     }                                                           \
00198 }
00199 
00200 
00201     /*
00202      * ISTRANSFORM
00203      * ASSUMES the minimum length for ttype and toid.
00204      */
00205 #define USM_LENGTH_OID_TRANSFORM        10
00206 
00207 #define ISTRANSFORM(ttype, toid)                                        \
00208         !snmp_oid_compare(ttype, USM_LENGTH_OID_TRANSFORM,              \
00209                 usm ## toid ## Protocol, USM_LENGTH_OID_TRANSFORM)
00210 
00211 #define ENGINETIME_MAX  2147483647      /* ((2^31)-1) */
00212 #define ENGINEBOOT_MAX  2147483647      /* ((2^31)-1) */
00213 
00214 
00215 
00216 
00217     /*
00218      * Prototypes.
00219      */
00220 
00221     NETSNMP_IMPORT
00222     int             snmp_realloc(u_char ** buf, size_t * buf_len);
00223 
00224     void            free_zero(void *buf, size_t size);
00225 
00226     u_char         *malloc_random(size_t * size);
00227     u_char         *malloc_zero(size_t size);
00228     NETSNMP_IMPORT
00229     int             memdup(u_char ** to, const void * from, size_t size);
00230 
00231     void            netsnmp_check_definedness(const void *packet,
00232                                               size_t length);
00233 
00234     NETSNMP_IMPORT
00235     u_int           netsnmp_binary_to_hex(u_char ** dest, size_t *dest_len,
00236                                           int allow_realloc,
00237                                           const u_char * input, size_t len);
00238 
00239     NETSNMP_IMPORT
00240     u_int           binary_to_hex(const u_char * input, size_t len,
00241                                   char **output);
00242                     /* preferred */
00243     int             netsnmp_hex_to_binary(u_char ** buf, size_t * buf_len,
00244                                          size_t * offset, int allow_realloc,
00245                                          const char *hex, const char *delim);
00246                     /* calls netsnmp_hex_to_binary w/delim of " " */
00247     NETSNMP_IMPORT
00248     int             snmp_hex_to_binary(u_char ** buf, size_t * buf_len,
00249                                        size_t * offset, int allow_realloc,
00250                                        const char *hex);
00251                     /* handles odd lengths */
00252     NETSNMP_IMPORT
00253     int             hex_to_binary2(const u_char * input, size_t len,
00254                                    char **output);
00255 
00256     NETSNMP_IMPORT
00257     int             snmp_decimal_to_binary(u_char ** buf, size_t * buf_len,
00258                                            size_t * out_len,
00259                                            int allow_realloc,
00260                                            const char *decimal);
00261 #define snmp_cstrcat(b,l,o,a,s) snmp_strcat(b,l,o,a,(const u_char *)s)
00262     NETSNMP_IMPORT
00263     int             snmp_strcat(u_char ** buf, size_t * buf_len,
00264                                 size_t * out_len, int allow_realloc,
00265                                 const u_char * s);
00266     NETSNMP_IMPORT
00267     char           *netsnmp_strdup_and_null(const u_char * from,
00268                                             size_t from_len);
00269 
00270     NETSNMP_IMPORT
00271     void            dump_chunk(const char *debugtoken, const char *title,
00272                                const u_char * buf, int size);
00273     char           *dump_snmpEngineID(const u_char * buf, size_t * buflen);
00274 
00276     typedef void   *marker_t;
00277     typedef const void* const_marker_t;
00278 
00279     NETSNMP_IMPORT
00280     marker_t        atime_newMarker(void);
00281     NETSNMP_IMPORT
00282     void            atime_setMarker(marker_t pm);
00283     NETSNMP_IMPORT
00284     long            atime_diff(const_marker_t first, const_marker_t second);
00285     u_long          uatime_diff(const_marker_t first, const_marker_t second);       /* 1/1000th sec */
00286     NETSNMP_IMPORT
00287     u_long          uatime_hdiff(const_marker_t first, const_marker_t second);      /* 1/100th sec */
00288     NETSNMP_IMPORT
00289     int             atime_ready(const_marker_t pm, int deltaT);
00290     int             uatime_ready(const_marker_t pm, unsigned int deltaT);
00291 
00292     int             marker_tticks(const_marker_t pm);
00293     int             timeval_tticks(const struct timeval *tv);
00294     char            *netsnmp_getenv(const char *name);
00295 
00296     int             netsnmp_addrstr_hton(char *ptr, size_t len);
00297 
00298     NETSNMP_IMPORT
00299     int             netsnmp_string_time_to_secs(const char *time_string);
00300 
00301 #ifdef __cplusplus
00302 }
00303 #endif
00304 #endif                          /* _TOOLS_H */
00305 /* @} */