ze-filter  (ze-filter-0.8.0-develop-180218)
ze-grey-test.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * ze-filter - Mail Server Filter for sendmail
5  *
6  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
7  *
8  * Auteur : Jose Marcio Martins da Cruz
9  * jose.marcio.mc@gmail.org
10  *
11  * Historique :
12  * Creation : janvier 2002
13  *
14  * This program is free software, but with restricted license :
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * More details about ze-filter license can be found at ze-filter
22  * web site : http://foss.jose-marcio.org
23  */
24 
25 
26 #include <ze-sys.h>
27 #include <libze.h>
28 #include <ze-filter.h>
29 
30 
31 /* ****************************************************************************
32  * *
33  * *
34  ******************************************************************************/
35 #define DEF_TUPLE "NET,HOST,FULL"
36 #define DEF_TIMES "0,1000,0,1000"
37 
38 struct conf_T {
39  char *workdir;
40  int count;
41 
42  char *times;
43  char *tuples;
44 };
45 
46 typedef struct conf_T conf_T;
47 
48 #define CONF_INITIALIZER {"/tmp/ze-greydb", 10000, DEF_TIMES, DEF_TUPLE}
49 
50 static char *workdir = "/tmp/ze-greydb";
51 
52 static char *ntuple = NULL;
53 
54 static char *tconst = NULL;
55 
56 static int do_test(conf_T * cargs);
57 
58 static void usage(char *);
59 
60 
61 int
62 main(argc, argv)
63  int argc;
64  char **argv;
65 {
66  int i;
67 
68  conf_T cargs = CONF_INITIALIZER;
69 
71 
72  ze_logLevel = 7;
73 
74  /*
75  ** 1. Read configuration parameters
76  */
77  {
78  const char *args = "ht:dvw:n:l:c:";
79  int c;
80 
81  while ((c = getopt(argc, argv, args)) != -1) {
82  switch (c) {
83  /*
84  * help
85  */
86  case 'h':
87  usage(argv[0]);
88  exit(0);
89  break;
90 
91  /*
92  * time constants
93  */
94  case 't':
95  tconst = optarg;
96  break;
97 
98  /*
99  * verbose
100  */
101  case 'v':
102  ze_logLevel++;
103  break;
104 
105  /*
106  * group
107  */
108  case 'l':
109  ze_logLevel = atoi(optarg);
110  break;
111 
112  case 'c':
113  cargs.count = atoi(optarg);
114  break;
115 
116  /*
117  * Work directory
118  */
119  case 'w':
120  workdir = optarg;
121  break;
122 
123  case 'n':
124  ntuple = optarg;
125  break;
126 
127  default:
128  usage(argv[0]);
129  printf("Error ... \n");
130  exit(0);
131  }
132  }
133 
134  }
135 
136 
138 
139  return 1;
140  }
141 
142  ntuple = STRNULL(ntuple, DEF_TUPLE);
143  if (ntuple != NULL) {
144 #define NTP 3
145  int argc;
146  char *argv[NTP];
147  char *s = NULL;
148 
149  if ((s = strdup(ntuple)) != NULL) {
150  memset(argv, 0, sizeof (argv));
151  argc = zeStr2Tokens(s, NTP, argv, ",");
152  for (i = 0; i < NTP; i++)
153  argv[i] = STRNULL(argv[i], "");
154  (void) grey_set_tuples(argv[0], argv[1], argv[2]);
155  }
156  }
157 
158  tconst = STRNULL(tconst, DEF_TIMES);
159  if (tconst != NULL) {
160 #define NTC 4
161  int argc;
162  char *argv[NTC];
163  time_t tc[NTC];
164  char *s = NULL;
165 
166  if ((s = strdup(tconst)) != NULL) {
167  memset(argv, 0, sizeof (argv));
168  memset(tc, 0, sizeof (tc));
169  argc = zeStr2Tokens(s, NTC, argv, ",");
170  for (i = 0; i < NTC; i++) {
171  argv[i] = STRNULL(argv[i], "0");
172  tc[i] = (time_t) zeStr2time(argv[i], NULL, 0);
173  }
174  (void) grey_set_delays(tc[0], tc[1], tc[2], tc[3]);
175  }
176  }
177 
178  do_test(&cargs);
179 
180  return 0;
181 }
182 
183 
184 
185 /* ****************************************************************************
186  * *
187  * *
188  ******************************************************************************/
189 static int
190 do_test(conf_T * cargs)
191 {
192  char *ip = "", *from = "", *to = "", *hostname = "";
193  int n = 0;
194  char buf[256];
195 
196  time_t ti, tf;
197 
198  ip = "1.1.1.1";
199  from = "nobody@foss.jose-marcio.org";
200  hostname = "nowhere.foss.jose-marcio.org";
201  to = "grey@foss.jose-marcio.org";
202 
203  ti = zeTime_ms();
204  for (n = 0; n < cargs->count; n++) {
205  int r = GREY_OK;
206  char *s = NULL;
207  bool new = FALSE;
208  bool can_validate = TRUE;
209 
210  snprintf(buf, sizeof (buf), "grey-%d@foss.jose-marcio.org", n);
211  to = buf;
212 
213  r = grey_check(ip, from, to, hostname, &new, can_validate);
214  switch (r) {
215  case GREY_OK:
216  s = "OK";
217  break;
218  case GREY_WAIT:
219  s = "WAIT";
220  break;
221  case GREY_ERROR:
222  s = "ERROR";
223  break;
224  case GREY_REJECT:
225  s = "REJECT";
226  break;
227  }
228  }
229  tf = zeTime_ms();
230 
231  ZE_MessageInfo(1, "Entries checked : %5d\n", n);
232  ZE_MessageInfo(1, "Time elapsed : %5d ms", tf - ti);
233 
234  return 0;
235 }
236 
237 
238 
239 /* ****************************************************************************
240  * *
241  * *
242  ******************************************************************************/
243 void
244 usage(arg)
245  char *arg;
246 {
247  printf("Usage : %s options\n"
248  " %s\n" " Compiled on %s\n", arg, PACKAGE, __DATE__ " " __TIME__);
249 }
int main(int argc, char **argv)
Definition: ze-grey-test.c:62
#define DEF_TIMES
Definition: ze-grey-test.c:36
bool grey_set_tuples(char *ip, char *from, char *to)
Definition: ze-grey.c:355
uint64_t zeTime_ms()
Definition: zeTime.c:34
#define DEF_TUPLE
Definition: ze-grey-test.c:35
int ze_logLevel
Definition: zeSyslog.c:34
#define STRNULL(x, r)
Definition: macros.h:81
bool grey_set_delays(time_t tp_min_norm, time_t tp_max_norm, time_t tp_min_null, time_t tp_max_null)
Definition: ze-grey.c:399
void zeLog_SetOutput(bool, bool)
Definition: zeSyslog.c:490
#define NTP
#define CONF_INITIALIZER
Definition: ze-grey-test.c:48
#define FALSE
Definition: macros.h:160
#define GREY_ERROR
Definition: ze-grey.h:38
#define GREY_REJECT
Definition: ze-grey.h:37
#define NTC
int grey_check(char *, char *, char *, char *, bool *, bool)
Definition: ze-grey.c:491
int zeStr2Tokens(char *, int, char **, char *)
Definition: zeStrings.c:610
#define GREY_OK
Definition: ze-grey.h:35
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
bool grey_init(char *, bool, int)
Definition: ze-grey.c:1326
#define TRUE
Definition: macros.h:157
char * times
#define GREY_SERVER
Definition: ze-grey.h:52
#define GREY_WAIT
Definition: ze-grey.h:36
void usage(char *arg)
char * hostname
#define PACKAGE
Definition: version.h:28
char * workdir
time_t zeStr2time(char *s, int *error, time_t dval)
Definition: zeStrConvert.c:291
char * tuples