util.h

Go to the documentation of this file.
00001 /* libspf - Sender Policy Framework library 00002 * 00003 * ANSI C implementation of spf-draft-200405.txt 00004 * 00005 * Author: James Couzens <jcouzens@codeshare.ca> 00006 * Author: Sean Comeau <scomeau@obscurity.org> 00007 * 00008 * File: util.h 00009 * Desc: Utility function header file 00010 * 00011 * License: 00012 * 00013 * The libspf Software License, Version 1.0 00014 * 00015 * Copyright (c) 2004 James Couzens & Sean Comeau All rights 00016 * reserved. 00017 * 00018 * Redistribution and use in source and binary forms, with or without 00019 * modification, are permitted provided that the following conditions 00020 * are met: 00021 * 00022 * 1. Redistributions of source code must retain the above copyright 00023 * notice, this list of conditions and the following disclaimer. 00024 * 00025 * 2. Redistributions in binary form must reproduce the above copyright 00026 * notice, this list of conditions and the following disclaimer in 00027 * the documentation and/or other materials provided with the 00028 * distribution. 00029 * 00030 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00031 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00032 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00033 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS MAKING USE OF THIS LICESEN 00034 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00035 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00036 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 00037 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00038 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00039 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00040 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00041 * SUCH DAMAGE. 00042 * 00043 */ 00044 00045 00046 #ifndef _UTIL_H 00047 #define _UTIL_H 1 00048 00049 #include "../../config.h" /* autoconf */ 00050 #include <stdio.h> /* stdin / stdout */ 00051 #include <stdlib.h> /* malloc / free */ 00052 #include <stdarg.h> /* va_sprintf */ 00053 #include <string.h> /* strstr / strdup */ 00054 #include <strings.h> /* strcasecmp */ 00055 #include <unistd.h> /* ? exit? */ 00056 #include <sys/socket.h> /* inet_ functions / structs */ 00057 #include <netinet/in.h> /* inet_ functions / structs */ 00058 #include <time.h> /* time_t */ 00059 #include <ctype.h> /* isdigit .. */ 00060 00061 #ifdef _WITH_SOLARIS 00062 #include <link.h> /* uintptr_t */ 00063 #elif defined(__FreeBSD__) 00064 #include <inttypes.h> /* uintptr_t */ 00065 #else 00066 #include <stdint.h> /* uintptr_t */ 00067 #endif /* _WITH_SOLARIS */ 00068 00069 #ifdef _HAVE_INTTYPES_H 00070 #include <inttypes.h> 00071 #endif /* HAVE_INTTYPES_H */ 00072 00073 #include "main.h" /* for the flags man */ 00074 00075 #ifdef _WITH_PTHREADS 00076 #include "dns.h" /* xgethostbyname */ 00077 #endif /* _WITH_PTHREADS */ 00078 00079 #ifdef HAVE__BEGIN_DECLS 00080 __BEGIN_DECLS 00081 #else 00082 # ifdef __cplusplus 00083 extern "C" { 00084 # endif /* __cplusplus */ 00085 #endif /* HAVE__BEGIN_DECLS */ 00086 00087 00088 00089 /* 00090 * define declarations 00091 * 00092 */ 00093 00094 /* maximum size of buffer for date time stamp */ 00095 #define SPF_MAX_DATETIME 26 00096 00097 /* max size of gethostbyname_r data buffer */ 00098 #define SPF_MAX_GHBNR_DBUF 2048 00099 00100 00101 /* 00102 * In ANSI C, and indeed any rational implementation, size_t is also the 00103 * type returned by sizeof(). However, it seems there are some irrational 00104 * implementations out there, in which sizeof() returns an int even though 00105 * size_t is defined as long or unsigned long. To ensure consistent results 00106 * we always use this SIZEOF() macro in place of using sizeof() directly. 00107 */ 00108 #define SIZEOF(object) ((size_t) sizeof(object)) 00109 00110 00111 #ifndef HAVE___FUNCTION__ 00112 # ifdef HAVE___FUNC__ 00113 # define __FUNCTION__ __func__ 00114 # else 00115 # define __FUNCTION__ "?" 00116 # endif /* HAVE___FUNC__ */ 00117 #endif /* HAVE___FUNCTION__ */ 00118 00119 00120 /* 00121 * memory allocation and string handling wrapper macros 00122 * 00123 */ 00124 00125 #define xmalloc(n) UTIL_malloc(n, __FILE__, __LINE__, __FUNCTION__) 00126 #define xrealloc(m, n) UTIL_realloc(m, n, __FILE__, __LINE__, __FUNCTION__) 00127 #define xfree(m) UTIL_free(m, __FILE__, __LINE__, __FUNCTION__) 00128 #define xstrdup(m) UTIL_strdup(m) 00129 #define xstrndup(m, n) UTIL_strndup(m, n) 00130 00131 00132 /* 00133 * printf wrapper macros 00134 * 00135 */ 00136 00137 /* printf with variadic macros */ 00138 #define xprintf(format,...) \ 00139 dbg_printf(FL_A, __FUNCTION__, __FILE__, __LINE__, format, __VA_ARGS__) 00140 00141 /* more verbose printf with variadic macros */ 00142 #define xvprintf(format,...) \ 00143 dbg_printf(FL_B, __FUNCTION__, __FILE__, __LINE__, format, __VA_ARGS__) 00144 00145 /* printf without variadic macros (passing only a single string) */ 00146 #define xpprintf(s) \ 00147 dbg_pprintf(FL_D, __FUNCTION__, __FILE__, __LINE__, s) 00148 00149 /* error printf with variadic macros */ 00150 #define xeprintf(format,...) \ 00151 dbg_printf(FL_E, __FUNCTION__, __FILE__, __LINE__, format, __VA_ARGS__) 00152 00153 /* error printf without variadic macros */ 00154 #define xepprintf(s) \ 00155 dbg_pprintf(FL_F, __FUNCTION__, __FILE__, __LINE__, s) 00156 00157 00158 /* 00159 * threading wrapper macros 00160 * 00161 */ 00162 00163 #define xpthread_mutex_lock(m) _UTIL_pthread_mutex(m, SPF_TRUE) 00164 #define xpthread_mutex_unlock(m) _UTIL_pthread_mutex(m, SPF_FALSE) 00165 00166 00167 /* for handing debug modes */ 00168 #define f_bit_set(fl_bit_vector, bit) ((int)((fl_bit_vector)&(bit))) 00169 00170 /* this table and macro came from wget more or less */ 00171 #define urlchr_test(c) (urlchr_table[(unsigned char)(c)] & 1) 00172 static const u_char urlchr_table[256] = 00173 { 00174 1, 1, 1, 1, 1, 1, 1, 1, /* NUL SOH STX ETX EOT ENQ ACK BEL */ 00175 1, 1, 1, 1, 1, 1, 1, 1, /* BS HT LF VT FF CR SO SI */ 00176 1, 1, 1, 1, 1, 1, 1, 1, /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */ 00177 1, 1, 1, 1, 1, 1, 1, 1, /* CAN EM SUB ESC FS GS RS US */ 00178 1, 0, 1, 1, 0, 1, 1, 0, /* SP ! " # $ % & ' */ 00179 0, 0, 0, 1, 0, 0, 0, 1, /* ( ) * + , - . / */ 00180 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */ 00181 0, 0, 1, 1, 1, 1, 1, 1, /* 8 9 : ; < = > ? */ 00182 1, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */ 00183 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */ 00184 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */ 00185 0, 0, 0, 1, 1, 1, 1, 0, /* X Y Z [ \ ] ^ _ */ 00186 1, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */ 00187 0, 0, 0, 0, 0, 0, 0, 0, /* h i j k l m n o */ 00188 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */ 00189 0, 0, 0, 1, 1, 1, 1, 1, /* x y z { | } ~ DEL */ 00190 00191 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00192 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00193 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00194 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00195 00196 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00197 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00198 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00199 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00200 }; 00201 00202 00203 00204 /* 00205 * Debug logging / Debug dummy wrappers 00206 * 00207 */ 00208 00209 /* output file for debugging */ 00210 #define DEBUG_LOG_FILE "/var/log/spf.log" 00211 00212 /* output file for transaction logging */ 00213 #define OUTPUT_LOG_FILE "/var/log/spflog.txt" 00214 00215 #if defined _SPF_DEBUG 00216 #define dbg_printf _printf_dbg 00217 #define dbg_pprintf _pprintf_dbg 00218 #endif 00219 00220 #ifndef _SPF_DEBUG 00221 #define dbg_printf _dummy_debug 00222 #define dbg_pprintf _dummy_pdebug 00223 #endif 00224 00225 00226 00227 /* 00228 * function declarations 00229 * 00230 */ 00231 00232 00233 /* 00234 * Debugging 00235 * 00236 */ 00237 void _printf_dbg(const u_int8_t, const char *, const char *, const size_t, 00238 const char *, ...); 00239 00240 void _pprintf_dbg(const u_int8_t, const char *, const char *, const size_t, 00241 const char *); 00242 00243 void _dummy_debug(const u_int8_t, const char *, const char *, const size_t, 00244 const char *, ...); 00245 00246 void _dummy_pdebug(const u_int8_t, const char *, const char *, const size_t, 00247 const char *); 00248 00249 00250 /* 00251 * Memory allocation / deallocation 00252 * 00253 */ 00254 00255 void *UTIL_malloc(const int32_t, const char *, const int32_t, const char *); 00256 void *UTIL_realloc(void *, const int32_t, const char *, const int32_t, const char *); 00257 void UTIL_free(void *, const char *, const int32_t, const char *); 00258 00259 00260 /* 00261 * String handling / SPF query handling (string related) 00262 * 00263 */ 00264 00265 void UTIL_log_result(peer_info_t *); 00266 char *UTIL_get_date(void); 00267 char *UTIL_strndup(const char *, const size_t); 00268 char *UTIL_strdup(const char *); 00269 int16_t UTIL_index(const char *, const char); 00270 char *UTIL_split_str(const char *, const char, const u_int8_t); 00271 char *UTIL_split_strr(const char *, const char, const u_int8_t); 00272 u_int8_t UTIL_count_delim(const char *, const char); 00273 SPF_BOOL UTIL_is_spf_delim(const char); 00274 SPF_BOOL UTIL_is_spf_result(const char); 00275 SPF_BOOL UTIL_is_macro(const char *); 00276 00277 00278 /* 00279 * SPF Mechanism parsing 00280 * 00281 */ 00282 00283 SPF_BOOL UTIL_mx_cmp(peer_info_t *, const char *, const int8_t); 00284 SPF_BOOL UTIL_a_cmp(peer_info_t *, const char *, const int8_t); 00285 SPF_BOOL UTIL_ptr_cmp(peer_info_t *, const char *); 00286 00287 00288 /* 00289 * General utility 00290 * 00291 */ 00292 00293 SPF_MECHANISM UTIL_get_policy_mech(const char *); 00294 SPF_RESULT UTIL_get_mech_prefix(peer_info_t *, const char *); 00295 SPF_BOOL UTIL_assoc_prefix(peer_info_t *, SPF_RESULT, const char *); 00296 policy_addr_t *UTIL_expand_ip(const char *); 00297 SPF_BOOL UTIL_is_spf_delim(const char ); 00298 SPF_BOOL UTIL_is_ip(const char *); 00299 00300 00301 /* 00302 * DNS related parsing 00303 * 00304 */ 00305 00306 char *UTIL_rev_addr(const char *); 00307 char *UTIL_get_dname(const char *); 00308 SPF_BOOL UTIL_cidr_cmp(const policy_addr_t *, const struct in_addr *); 00309 SPF_BOOL UTIL_validate_ptr(peer_info_t *); 00310 SPF_BOOL UTIL_validate_hostname(peer_info_t *, const char *, const int8_t); 00311 char *UTIL_url_encode(const char *); 00312 char *UTIL_reverse(const char *, const char); 00313 SPF_BOOL UTIL_addnode(split_str_t *, const char *, SPF_BOOL); 00314 00315 00316 /* 00317 * Threading (linux pthreads) 00318 * 00319 */ 00320 00321 void _UTIL_pthread_mutex(void *, SPF_BOOL); 00322 00323 00324 #ifdef _WITH_PTHREADS 00325 00326 /* pthreads utility mutex (used by debugging and date/time) */ 00327 extern pthread_mutex_t util_mutex; 00328 00329 #else 00330 00331 /* pthreads utility mutex dummy wrapper */ 00332 extern void *util_mutex; 00333 00334 #endif /* _WITH_PTHREADS */ 00335 00336 00337 #ifdef HAVE__BEGIN_DECLS 00338 __END_DECLS /* _UTIL_H */ 00339 #else 00340 # ifdef __cplusplus 00341 } 00342 # endif /* __cplusplus */ 00343 #endif /* HAVE__BEGIN_DECLS */ 00344 00345 #endif /* _UTIL_H */ 00346 00347 /* end util.h */

Generated on Thu Sep 16 18:10:47 2004 for libSPF v1.0 by doxygen 1.3.8