ze-filter  (ze-filter-0.8.0-develop-180218)
ze-grey-client-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 <ze-filter.h>
28 
29 
30 /* ****************************************************************************
31  * *
32  * *
33  ******************************************************************************/
34 #define DEF_TUPLE "NET,HOST,FULL"
35 #define DEF_TIMES "0,1000,0,1000"
36 
37 struct conf_T {
38  bool random;
39  double randrange;
40  unsigned long upperlimit;
41  int count;
42 
43  char *domain_src;
44  char *domain_dst;
45 
46  char *ip;
47  char *hostname;
48  char *from;
49  char *to;
50 };
51 
52 typedef struct conf_T conf_T;
53 
54 #define CONF_INITIALIZER { \
55  FALSE, 2., 0, 10000, \
56  "nowhere.foss.jose-marcio.org", "foss.jose-marcio.org", \
57  "1.1.1.1", "nowhere.foss.jose-marcio.org", \
58  "nobody@foss.jose-marcio.org", "grey@foss.jose-marcio.org" \
59 }
60 
61 static int do_test(conf_T * cargs);
62 
63 static void usage(char *);
64 
65 int
66 main(argc, argv)
67  int argc;
68  char **argv;
69 {
70  int i;
71 
72  conf_T cargs = CONF_INITIALIZER;
73 
75 
76  ze_logLevel = 7;
77 
78  /*
79  ** 1. Read configuration parameters
80  */
81  {
82  const char *args = "hvl:c:rR:U:";
83  int c;
84 
85  while ((c = getopt(argc, argv, args)) != -1) {
86  switch (c) {
87  /*
88  * help
89  */
90  case 'h':
91  usage(argv[0]);
92  exit(0);
93  break;
94 
95  /*
96  * verbose
97  */
98  case 'v':
99  ze_logLevel++;
100  break;
101 
102  /*
103  * log level
104  */
105  case 'l':
106  ze_logLevel = atoi(optarg);
107  break;
108 
109  case 'c':
110  cargs.count = atoi(optarg);
111  break;
112 
113  case 'r':
114  cargs.random = !cargs.random;
115  break;
116  case 'R':
117  cargs.randrange = atof(optarg);
118  break;
119  case 'U':
120  cargs.upperlimit = atoi(optarg);
121  break;
122 
123  default:
124  usage(argv[0]);
125  printf("Error ... \n");
126  exit(0);
127  }
128  }
129  }
130 
131  configure("ze-grey-client-test", conf_file, FALSE);
132 
133  do_test(&cargs);
134 
135  return 0;
136 }
137 
138 
139 
140 /* ****************************************************************************
141  * *
142  * *
143  ******************************************************************************/
144 static int
145 do_test(conf_T * cargs)
146 {
147  char *ip = "", *from = "", *to = "", *hostname = "";
148  int n = 0;
149  char buf[256];
150  long range;
151  time_t ti, tf;
152  char ipbuf[256];
153 
154  ip = "1.1.1.1";
155  from = "nobody@foss.jose-marcio.org";
156  hostname = "nowhere.foss.jose-marcio.org";
157  to = "grey@foss.jose-marcio.org";
158 
159  if (cargs->upperlimit < 10) {
160  range = cargs->randrange * cargs->count;
161  range = MAX(range, 10);
162  } else
163  range = cargs->upperlimit;
164 
165  atexit(remote_grey_quit);
166 
167  memset(ipbuf, 0, sizeof (ipbuf));
168  ti = zeTime_ms();
169  srandom(ti);
170  for (n = 0; n < cargs->count; n++) {
171  int r = GREY_OK;
172  char *s = NULL;
173  bool new = FALSE;
174  bool can_validate = TRUE;
175  int rind;
176 
177  if (cargs->random) {
178  rind = random() % range;
179 
180  snprintf(ipbuf, sizeof (ipbuf), "1.1.%d.%d", (int ) rind % 255, (int ) (random() % 255));
181  ip = ipbuf;
182  } else
183  rind = n;
184 
185  snprintf(buf, sizeof (buf), "grey-%d@foss.jose-marcio.org", rind);
186  to = buf;
187 
188  r = remote_grey_check(ip, from, to, hostname);
189  switch (r) {
190  case GREY_OK:
191  s = "OK";
192  break;
193  case GREY_WAIT:
194  s = "WAIT";
195  break;
196  case GREY_ERROR:
197  s = "ERROR";
198  break;
199  case GREY_REJECT:
200  s = "REJECT";
201  break;
202  }
203  if (r == GREY_ERROR) {
204  ZE_MessageInfo(1, "* %7d : Error : %s %s %s %s", n, ip, hostname, from,
205  to);
206  sleep(1);
207  }
208  }
209  tf = zeTime_ms();
210 
211  ZE_MessageInfo(1, "Entries checked : %5d\n", n);
212  ZE_MessageInfo(1, "Time elapsed : %5d ms", tf - ti);
213 
214  return 0;
215 }
216 
217 
218 
219 /* ****************************************************************************
220  * *
221  * *
222  ******************************************************************************/
223 void
224 usage(arg)
225  char *arg;
226 {
227  printf("Usage : %s options\n"
228  " %s\n" " Compiled on %s\n", arg, PACKAGE, __DATE__ " " __TIME__);
229 }
#define MAX(a, b)
Definition: macros.h:139
int remote_grey_check(char *ip, char *from, char *to, char *hostname)
uint64_t zeTime_ms()
Definition: zeTime.c:34
int ze_logLevel
Definition: zeSyslog.c:34
int main(int argc, char **argv)
void zeLog_SetOutput(bool, bool)
Definition: zeSyslog.c:490
#define FALSE
Definition: macros.h:160
#define GREY_ERROR
Definition: ze-grey.h:38
#define GREY_REJECT
Definition: ze-grey.h:37
#define GREY_OK
Definition: ze-grey.h:35
double randrange
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
#define TRUE
Definition: macros.h:157
char * domain_src
char * conf_file
Definition: ze-cf.c:38
#define GREY_WAIT
Definition: ze-grey.h:36
int configure(char *, char *, bool)
Definition: ze-cf.c:1203
#define CONF_INITIALIZER
void usage(char *arg)
char * domain_dst
char * hostname
#define PACKAGE
Definition: version.h:28
void remote_grey_quit()
unsigned long upperlimit