1 /* Sender Policy Framework Validator
2 *
3 *  Author: James Couzens <jcouzens@codeshare.ca>
4 *  Author: Travis Anderson <tanderson@codeshare.ca>
5 *
6 *  FILE: util.h
7 *  DESC: Header file for utility functions.
8 *
9 *  License:
10 *
11 *  The CodeShare Software License, Version 1.0
12 *
13 *  Copyright (c) 2004 James Couzens & Travis Anderson.  All rights
14 *  reserved.
15 *
16 *  Redistribution and use in source and binary forms, with or without
17 *  modification, are permitted provided that the following conditions
18 *  are met:
19 *
20 *  1. Redistributions of source code must retain the above copyright
21 *     notice, this list of conditions and the following disclaimer.
22 *
23 *  2. Redistributions in binary form must reproduce the above copyright
24 *     notice, this list of conditions and the following disclaimer in
25 *     the documentation and/or other materials provided with the
26 *     distribution.
27 *
28 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
29 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 *  DISCLAIMED.  IN NO EVENT SHALL THE CODESHARE ORGANIZATION OR
32 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35 *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38 *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 *  SUCH DAMAGE.
40 *
41 */
42 
43 
44 #ifndef _UTIL_H
45 #define _UTIL_H 1
46 
47 #include <stdlib.h>       /* malloc / free */
48 #include <sys/types.h>
49 #include <netinet/in.h>   /* in_addr struct fBSD */
50 #include <arpa/inet.h>    /* in_addr struct */
51 
52 
53 #ifndef _WIN32
54 __BEGIN_DECLS
55 #endif /* _WIN32 */
56 
57 
58 
59 /*
60 *  define declarations
61 *
62 */
63 
64 /* maximum string length */
65 #define SPFVAL_MAX_STR 4096
66 
67 /* logging levels */
68 #define LOG_NORMAL  1
69 #define LOG_VERBOSE 2
70 
71 /* malloc wrapper macros */
72 #define xmalloc(n)     _UTIL_malloc(n, __FILE__, __LINE__, __FUNCTION__)
73 #define xrealloc(m, n) _UTIL_realloc(m, n, __FILE__, __LINE__, __FUNCTION__)
74 #define xfree(m)       _UTIL_free(m, __FILE__, __LINE__, __FUNCTION__)
75 
76 /* string function wrapper macros */
77 #define xstrndup(m, n) _UTIL_strndup(m, n)
78 
79 /* regular output macros */
80 #define xprint(s) \
81   _UTIL_printf(LOG_NORMAL, __FILE__, __LINE__, __FUNCTION__, (s), "")
82 
83 #define xprintf(format,...) \
84   _UTIL_printf(LOG_NORMAL, __FILE__, __LINE__, __FUNCTION__, format, \
85   __VA_ARGS__)
86 
87 /* verbose output macros */
88 #define xvprint(s) \
89   _UTIL_printf(LOG_VERBOSE, __FILE__, __LINE__, __FUNCTION__, (s), "")
90 
91 #define xvprintf(format,...) \
92   _UTIL_printf(LOG_VERBOSE, __FILE__, __LINE__, __FUNCTION__, format, \
93   __VA_ARGS__)
94 
95 
96 #ifdef _SPFVAL_DEBUG
97 
98   /* xreturn wrapper */
99   #define xreturn(m) \
100   { \
101     xvprintf("Returning '%s'", #m); \
102     \
103     return (m); \
104   }
105 
106   /* xenter wrapper */
107   #define xenter() (xvprint("Entering"))
108 
109   /* xassert wrapper */
110   #define xassert(expr)                               \
111   if ((expr))                                         \
112   {                                                   \
113     xvprintf("Assertion '%s' succeeded", #expr);      \
114   }                                                   \
115   else                                                \
116   {                                                   \
117     xprintf("Assertion '%s' failed; exiting", #expr); \
118                                                       \
119     exit(1);                                          \
120   }
121 
122 #else
123 
124   /* xreturn wrapper */
125   #define xreturn(m) \
126   { \
127     return (m); \
128   }
129 
130   /* xenter wrapper */
131   #define xenter()
132 
133   /* xassert wrapper */
134   #define xassert(expr)
135 
136 
137 #endif /* _SPFVAL_DEBUG */
138 
139 #ifdef _SPFVAL_DEBUG
140 #else
141 #endif
142 
143 
144 
145 /*
146 * structure declarations
147 *
148 */
149 
150 
151 /* policy_addr_t
152 *
153 *  Desc:
154 *         Storage container used to store parsed out ip addresses in their
155 *  binary format (in_addr struct) and an unsigned integer containing the
156 *  netmask
157 *
158 */
159 typedef struct policy_addr_s
160 {
161   struct in_addr addr;    /* in_addr struct (unsigned long) */
162   uint8_t        cidr;    /* address cidr length */
163 } policy_addr_t;
164 
165 
166 
167 
168 /*
169 *  function declarations
170 *
171 */
172 
173 
174 /* alloc wrapper functions */
175 void *_UTIL_malloc(const int32_t, const char *, const int32_t, const char *);
176 void *_UTIL_realloc(void *, const int32_t, const char *, const int32_t,
177                     const char *);
178 void  _UTIL_free(void *, const char *, const int32_t, const char *);
179 char *_UTIL_strndup(const char *, const size_t);
180 
181 
182 /* printf wrapper function */
183 void  _UTIL_printf(const int32_t, const char *, const int32_t, const char *,
184                    const char *format, ...);
185 
186 
187 #ifndef _WIN32
188 __END_DECLS
189 #endif /* _WIN32 */
190 
191 #endif /* _UTIL_H */
192 
193 /* end util.h */
194