util.h

Go to the documentation of this file.
00001 /* libspf - Sender Policy Framework library 00002 * 00003 * ANSI C implementation of draft-mengwong-spf-02.9.7.txt 00004 * 00005 * Author: James Couzens <jcouzens@6o4.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 #ifndef _UTIL_H 00046 #define _UTIL_H 1 00047 00048 #include <stdio.h> /* stdin / stdout */ 00049 #include <stdlib.h> /* malloc / free */ 00050 #include <stdarg.h> /* va_sprintf */ 00051 #include <string.h> /* strstr / strdup */ 00052 #include <strings.h> /* strcasecmp */ 00053 #include <unistd.h> /* ? exit? */ 00054 #include <sys/socket.h> /* inet_ functions / structs */ 00055 #include <netinet/in.h> /* inet_ functions / structs */ 00056 #include <time.h> /* time_t */ 00057 #include <ctype.h> /* isdigit .. */ 00058 #include "main.h" /* for the flags man */ 00059 00060 __BEGIN_DECLS 00061 00062 /* 00063 * In ANSI C, and indeed any rational implementation, size_t is also the 00064 * type returned by sizeof(). However, it seems there are some irrational 00065 * implementations out there, in which sizeof() returns an int even though 00066 * size_t is defined as long or unsigned long. To ensure consistent results 00067 * we always use this SIZEOF() macro in place of using sizeof() directly. 00068 */ 00069 #define SIZEOF(object) ((size_t) sizeof(object)) 00070 00071 /* malloc wrapper macros */ 00072 #define xmalloc(n) UTIL_malloc(n, __FILE__, __LINE__, __FUNCTION__) 00073 #define xrealloc(m, n) UTIL_realloc(m, n, __FILE__, __LINE__, __FUNCTION__) 00074 #define xfree(m) UTIL_free(m, __FILE__, __LINE__, __FUNCTION__) 00075 #define xstrdup(m) UTIL_strdup(m) 00076 #define xstrndup(m, n) UTIL_strndup(m, n) 00077 00078 /* printf wrapper macros */ 00079 #define xprintf(format,...) dbg_printf(FL_A, __FUNCTION__, format, __VA_ARGS__) 00080 #define xvprintf(format,...) dbg_printf(FL_B, __FUNCTION__, format, __VA_ARGS__) 00081 #define xpprintf(format,...) dbg_printf(FL_D, __FUNCTION__, format, __VA_ARGS__) 00082 #define xeprintf(s) (fprintf(stderr, s)) 00083 00084 /* for handing debug modes */ 00085 #define f_bit_set(fl_bit_vector, bit) ((int)((fl_bit_vector)&(bit))) 00086 00087 /* this table and macro came from wget more or less */ 00088 #define urlchr_test(c) (urlchr_table[(unsigned char)(c)] & 1) 00089 const static u_char urlchr_table[256] = 00090 { 00091 1, 1, 1, 1, 1, 1, 1, 1, /* NUL SOH STX ETX EOT ENQ ACK BEL */ 00092 1, 1, 1, 1, 1, 1, 1, 1, /* BS HT LF VT FF CR SO SI */ 00093 1, 1, 1, 1, 1, 1, 1, 1, /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */ 00094 1, 1, 1, 1, 1, 1, 1, 1, /* CAN EM SUB ESC FS GS RS US */ 00095 1, 0, 1, 1, 0, 1, 1, 0, /* SP ! " # $ % & ' */ 00096 0, 0, 0, 1, 0, 0, 0, 1, /* ( ) * + , - . / */ 00097 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */ 00098 0, 0, 1, 1, 1, 1, 1, 1, /* 8 9 : ; < = > ? */ 00099 1, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */ 00100 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */ 00101 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */ 00102 0, 0, 0, 1, 1, 1, 1, 0, /* X Y Z [ \ ] ^ _ */ 00103 1, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */ 00104 0, 0, 0, 0, 0, 0, 0, 0, /* h i j k l m n o */ 00105 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */ 00106 0, 0, 0, 1, 1, 1, 1, 1, /* x y z { | } ~ DEL */ 00107 00108 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00109 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00110 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00111 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00112 00113 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00114 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00115 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00116 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00117 }; 00118 00119 /* output file for debugging */ 00120 #define DEBUG_LOG_FILE "/var/log/spf.log" 00121 00122 /* output file for transaction logging */ 00123 #define OUTPUT_LOG_FILE "/var/log/spflog.txt" 00124 00125 #if defined _SPF_DEBUG 00126 #define dbg_printf _printf_dbg 00127 #endif 00128 00129 #ifndef _SPF_DEBUG 00130 #define dbg_printf dummy_debug 00131 #endif 00132 00133 void _printf_dbg(const u_int8_t level, const char *function, 00134 const char *format,...); 00135 void dummy_debug(const u_int8_t level, const char *function, 00136 const char *format,...); 00137 void *UTIL_malloc(const int32_t, const char *, const int32_t, 00138 const char *); 00139 void *UTIL_realloc(void *, const int32_t, const char *, const int32_t, 00140 const char *); 00141 void UTIL_free(void *, const char *, const int32_t, const char *); 00142 00143 00144 char *UTIL_printf(char *format, ...); 00145 void UTIL_log_result(peer_info_t *peer_info); 00146 char *UTIL_get_date(void); 00147 char *UTIL_strndup(const char *s, const size_t n); 00148 char *UTIL_strdup(const char *s); 00149 int16_t UTIL_index(const char *s, const char c); 00150 char *UTIL_split_str(const char *s, const char c, const u_int8_t num); 00151 char *UTIL_split_strr(const char *s, const char c, const u_int8_t num); 00152 u_int8_t UTIL_count_delim(const char *s, const char c); 00153 size_t UTIL_guess_whats_in_the_box(char *s, const char c, const u_int16_t num); 00154 SPF_BOOL UTIL_is_spf_delim(const char c); 00155 SPF_BOOL UTIL_is_spf_result(const char c); 00156 SPF_BOOL UTIL_is_macro(const char *s); 00157 00158 /* MECHANISM PARSING */ 00159 SPF_BOOL UTIL_mx_cmp(peer_info_t *peer_info, const char *s, const int8_t cidr); 00160 SPF_BOOL UTIL_a_cmp(peer_info_t *peer_info, const char *s, const int8_t cidr); 00161 SPF_BOOL UTIL_ptr_cmp(peer_info_t *peer_info, const char *s); 00162 00163 /* GENERAL UTILITY */ 00164 SPF_MECHANISM UTIL_get_policy_mech(const char *s); 00165 SPF_RESULT UTIL_get_mech_prefix(peer_info_t *peer_info, const char *s); 00166 SPF_BOOL UTIL_assoc_prefix(peer_info_t *peer_info, SPF_RESULT p, const char *s); 00167 policy_addr_t *UTIL_expand_ip(const char *s); 00168 SPF_BOOL UTIL_is_spf_delim(const char c); 00169 char *UTIL_rev_addr(const char *s); 00170 char *UTIL_get_dname(const char *s); 00171 SPF_BOOL UTIL_cidr_cmp(peer_info_t *peer_info, const policy_addr_t *policy_addr, 00172 const struct in_addr *peer_addr); 00173 SPF_BOOL UTIL_validate_ptr(peer_info_t *peer_info); 00174 SPF_BOOL UTIL_validate_hostname(peer_info_t *peer_info, const char *s, 00175 const int8_t cidr); 00176 char *UTIL_url_encode(const char *s); 00177 char *UTIL_reverse(const char *s, const char delim); 00178 SPF_BOOL UTIL_addnode(split_str_t *master, const char *s, SPF_BOOL last); 00179 00180 __END_DECLS 00181 00182 #endif /* _UTIL_H */ 00183 00184 /* end util.h */

Generated on Thu Jul 1 14:05:44 2004 for libspf v1.0 by doxygen 1.3.7