ze-filter  (ze-filter-0.8.0-develop-180218)
ze-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  char *workdir;
39  int count;
40 
41  char *times;
42  char *tuples;
43 };
44 
45 typedef struct conf_T conf_T;
46 
47 #define CONF_INITIALIZER {"/tmp/ze-grey-bench", 10000, DEF_TIMES, DEF_TUPLE}
48 
49 static char *workdir = "/var/ze-greyd";
50 
51 static char *ntuple = NULL;
52 
53 static char *tconst = NULL;
54 
55 static int do_test(conf_T * cargs);
56 
57 static void usage(char *);
58 
59 
60 int
61 main(argc, argv)
62  int argc;
63  char **argv;
64 {
65  int i;
66 
67  conf_T cargs = CONF_INITIALIZER;
68 
70 
71  ze_logLevel = 7;
72 
73  /*
74  ** 1. Read configuration parameters
75  */
76  {
77  const char *args = "ht:dvw:n:l:c:";
78  int c;
79 
80  while ((c = getopt(argc, argv, args)) != -1) {
81  switch (c) {
82  /*
83  * help
84  */
85  case 'h':
86  usage(argv[0]);
87  exit(0);
88  break;
89 
90  /*
91  * time constants
92  */
93  case 't':
94  tconst = optarg;
95  break;
96 
97  /*
98  * verbose
99  */
100  case 'v':
101  ze_logLevel++;
102  break;
103 
104  /*
105  * group
106  */
107  case 'l':
108  ze_logLevel = atoi(optarg);
109  break;
110 
111  case 'c':
112  cargs.count = atoi(optarg);
113  break;
114 
115  /*
116  * Work directory
117  */
118  case 'w':
119  workdir = optarg;
120  break;
121 
122  case 'n':
123  ntuple = optarg;
124  break;
125 
126  default:
127  usage(argv[0]);
128  printf("Error ... \n");
129  exit(0);
130  }
131  }
132 
133  }
134 
135  configure("ze-client-test", conf_file, FALSE);
136 
137  {
138  client_T client = CLIENT_INITIALIZER;
139  char buf[1024];
140  int i;
141  int fd;
142  char fname[512];
143  int tfd;
144  FILE *fin;
145 
146  printf("* Connect \n");
147  if (!client_connect(&client, "inet:2012@127.0.0.1", 10))
148  goto fin;
149 
150  printf("* Receive \n");
151  memset(buf, 0, sizeof (buf));
152  if (!client_recv(&client, buf, sizeof (buf)));
153 
154  printf("* Send \n");
155  strlcpy(buf, "download pending\r\n", sizeof (buf));
156  if (!client_send(&client, buf, strlen(buf)));
157 
158  printf("* Receive \n");
159  strlcpy(fname, "pending.XXXXXX", sizeof (fname));
160  fd = mkstemp(fname);
161  for (;;) {
162  memset(buf, 0, sizeof (buf));
163  if (!client_recv(&client, buf, sizeof (buf)))
164  break;
165  printf("%s", buf);
166  write(fd, buf, strlen(buf));
167  }
168  close(fd);
169 
170  printf("* Send \n");
171  strlcpy(buf, "download valid\r\n", sizeof (buf));
172  if (!client_send(&client, buf, strlen(buf)));
173 
174  printf("* Receive \n");
175  strlcpy(fname, "valid.XXXXXX", sizeof (fname));
176  fd = mkstemp(fname);
177  for (;;) {
178  memset(buf, 0, sizeof (buf));
179 #if 1
180  if (!client_readln(&client, buf, sizeof (buf)))
181  break;
182  printf("%s\n", buf);
183  write(fd, buf, strlen(buf));
184  write(fd, "\n", 1);
185 #else
186  if (!client_recv(&client, buf, sizeof (buf)))
187  break;
188  printf("%s", buf);
189  write(fd, buf, strlen(buf));
190 #endif
191  }
192  close(fd);
193 
194  printf("* Send \n");
195  strlcpy(buf, "download white\r\n", sizeof (buf));
196  if (!client_send(&client, buf, strlen(buf)));
197 
198  printf("* Receive \n");
199  strlcpy(fname, "white.XXXXXX", sizeof (fname));
200  fd = mkstemp(fname);
201  for (;;) {
202  memset(buf, 0, sizeof (buf));
203  if (!client_recv(&client, buf, sizeof (buf)))
204  break;
205  printf("%s", buf);
206  write(fd, buf, strlen(buf));
207  }
208  close(fd);
209 
210  printf("* Send \n");
211  strlcpy(buf, "quit\r\n", sizeof (buf));
212  if (!client_send(&client, buf, strlen(buf)));
213 
214  fin:
215  client_disconnect(&client, FALSE);
216  }
217 
218  return 0;
219 }
220 
221 
222 
223 /* ****************************************************************************
224  * *
225  * *
226  ******************************************************************************/
227 static int
228 do_test(conf_T * cargs)
229 {
230  char *ip = "", *from = "", *to = "", *hostname = "";
231  int n = 0;
232  char buf[256];
233 
234  time_t ti, tf;
235 
236  ip = "1.1.1.1";
237  from = "nobody@foss.jose-marcio.org";
238  hostname = "nowhere.foss.jose-marcio.org";
239  to = "grey@foss.jose-marcio.org";
240 
241  ti = zeTime_ms();
242  for (n = 0; n < cargs->count; n++) {
243  int r = GREY_OK;
244  char *s = NULL;
245  bool new = FALSE;
246  bool can_validate = TRUE;
247 
248  snprintf(buf, sizeof (buf), "grey-%d@foss.jose-marcio.org", n);
249  to = buf;
250 
251  r = remote_grey_check(ip, from, to, hostname);
252  switch (r) {
253  case GREY_OK:
254  s = "OK";
255  break;
256  case GREY_WAIT:
257  s = "WAIT";
258  break;
259  case GREY_ERROR:
260  s = "ERROR";
261  break;
262  case GREY_REJECT:
263  s = "REJECT";
264  break;
265  }
266  }
267  tf = zeTime_ms();
268 
269  ZE_MessageInfo(1, "Entries checked : %5d\n", n);
270  ZE_MessageInfo(1, "Time elapsed : %5d ms", tf - ti);
271 
272  return 0;
273 }
274 
275 
276 
277 /* ****************************************************************************
278  * *
279  * *
280  ******************************************************************************/
281 void
282 usage(arg)
283  char *arg;
284 {
285  printf("Usage : %s options\n"
286  " %s\n" " Compiled on %s\n", arg, PACKAGE, __DATE__ " " __TIME__);
287 }
#define CONF_INITIALIZER
#define CLIENT_INITIALIZER
Definition: ze-client.h:53
int remote_grey_check(char *ip, char *from, char *to, char *hostname)
bool client_send(client_T *client, char *buf, size_t size)
Definition: ze-client.c:263
uint64_t zeTime_ms()
Definition: zeTime.c:34
int ze_logLevel
Definition: zeSyslog.c:34
void zeLog_SetOutput(bool, bool)
Definition: zeSyslog.c:490
#define FALSE
Definition: macros.h:160
#define strlcpy
Definition: zeString.h:32
#define GREY_ERROR
Definition: ze-grey.h:38
#define GREY_REJECT
Definition: ze-grey.h:37
int main(int argc, char **argv)
#define GREY_OK
Definition: ze-grey.h:35
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
#define TRUE
Definition: macros.h:157
char * times
char * conf_file
Definition: ze-cf.c:38
#define GREY_WAIT
Definition: ze-grey.h:36
bool client_disconnect(client_T *client, bool incerr)
Definition: ze-client.c:231
int configure(char *, char *, bool)
Definition: ze-cf.c:1203
int client_connect(client_T *client, char *spec, int to)
Definition: ze-client.c:126
void usage(char *arg)
bool client_recv(client_T *client, char *buf, size_t size)
Definition: ze-client.c:355
char * hostname
#define PACKAGE
Definition: version.h:28
char * workdir
bool client_readln(client_T *client, char *buf, size_t size)
Definition: ze-client.c:447
char * tuples