Kannel: Open Source WAP and SMS gateway  svn-r5335
test_cimd2.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /* test_cimd2.c - fake cimd2 smsc
58  *
59  * This program pretends to be an CIMD 2 SMS center, accessible via IP.
60  * It is used to test the Kannel smsc_cimd2 code.
61  *
62  * Richard Braakman
63  */
64 
65 /* Note: The CIMD2 parsing code was written as a prototype, and currently
66  * its main use is to exercise the *real* CIMD2 code in gw/smsc_cimd2.c.
67  * Please don't use this code for anything real.
68  * Richard Braakman */
69 
70 /*
71  * TODO: If log level is high and activity level is low, there will be
72  * "SND" log entries for packets that are not sent, which is confusing
73  * and should be fixed.
74  */
75 
76 #include <stdlib.h>
77 #include <stdio.h>
78 #include <string.h>
79 #include <errno.h>
80 #include <ctype.h>
81 #include <stdarg.h>
82 #include <limits.h>
83 
84 #include <unistd.h>
85 #include <time.h>
86 #include <sys/types.h>
87 #include <sys/time.h>
88 #include <sys/socket.h>
89 #include <netinet/in.h>
90 #include <arpa/inet.h>
91 
92 #include "gwlib/gwlib.h"
93 
94 enum { TIMESTAMP_MAXLEN = 13 };
95 
96 unsigned char *progname;
97 
98 /* Set up a fake account for Kannel to use */
99 unsigned char *username = "foo";
100 unsigned char *password = "bar";
101 
102 int port = 6789;
103 
104 /* This can be useful to get past protocol-ID checks when testing spew. */
105 unsigned char *intro = "";
106 
107 enum ACT {
112 };
113 
114 enum SPEW {
119 };
120 
121 enum LOG {
123  LOG_data = 1,
126 };
127 
128 enum CHK {
131  CHK_sums = 2,
134 };
135 
140 
141 int max_deliveries = -1;
142 int deliveries = 0;
143 time_t start_time = 0;
144 
145 int sockfd = -1;
146 
149 /* Maximum reasonable outbuffer size. It can go above this, but we don't
150  * deliberately add data when it's already more than this. */
151 enum { OUTBUFFER_LIMIT = 65536 };
152 
153 /* Test dependencies on neatly-sized read and write chunks, by using
154  * a deliberately evil buffer size. 1021 is the largest prime smaller
155  * than 1024. */
156 enum { EVIL_BUFSIZE = 1021 };
157 
158 
159 enum CHARS {
160  STX = 2,
161  ETX = 3,
162  TAB = 9,
163  LF = 10,
164  CR = 13
165 };
166 
167 static void usage(FILE *out) {
168  fprintf(out, "Usage: %s [options...]\n"
169 " --help Print this message\n"
170 " --user USER Allow clients to log in with username USER (default %s)\n"
171 " --password PASS Allow clients to log in with password PASS (default %s)\n"
172 " --intro INTRO Send INTRO string before anything else (default nothing)\n"
173 " --port PORT TCP port to listen on (default %d)\n"
174 " --activity ACT Activity level of test server (default %d)\n"
175 " ACT = 0 send nothing, just listen\n"
176 " ACT = 1 send valid replies, do not initiate any transactions\n"
177 " ACT = 2 attempt to deliver a random SMS every few seconds (NI)\n"
178 " ACT = 3 deliver many random SMSes, measure throughput (NI)\n"
179 " --spew SPEW Flood client, overrides --activity (default %d)\n"
180 " SPEW = 0 don't spew, use --activity instead\n"
181 " SPEW = 1 spew random binary gunk at client\n"
182 " SPEW = 2 spew random data of the right character set at client (NI)\n"
183 " SPEW = 3 spew valid packets with random contents at client (NI)\n"
184 " --logging LOG Log level of test server (default %d)\n"
185 " LOG = 0 log nothing\n"
186 " LOG = 1 log all data\n"
187 " LOG = 2 log summaries of valid packets\n"
188 " LOG = 3 log successfully sent and received SMSes (NI)\n"
189 " --checking CHK Check level of test server (default %d)\n"
190 " CHK = 0 check nothing\n"
191 " CHK = 1 signal invalid packets (NI)\n"
192 " CHK = 2 signal checksum errors (NI)\n"
193 " CHK = 3 signal protocol errors (NI)\n"
194 " CHK = 4 signal invalid SMS contents (NI)\n"
195 " --max MAX With high activity values, stop after MAX deliveries\n"
196 " NI means Not Implemented\n"
199 }
200 
201 static void pretty_print(unsigned char *data, size_t length) {
202  size_t i;
203  int c;
204 
205  for (i = 0; i < length; i++) {
206  c = data[i];
207  switch(c) {
208  default:
209  if (isprint(c))
210  putchar(c);
211  else
212  printf("<%d>", c);
213  break;
214  case TAB: fputs("<TAB>", stdout); break;
215  case LF: fputs("<LF>\n", stdout); break;
216  case CR: fputs("<CR>", stdout); break;
217  case STX: fputs("<STX>", stdout); break;
218  case ETX: fputs("<ETX>\n", stdout); break;
219  }
220  }
221  fflush(stdout);
222 }
223 
224 static void read_data(Octstr *in, int fd) {
225  unsigned char buf[EVIL_BUFSIZE];
226  int ret;
227 
228  ret = read(fd, buf, sizeof(buf));
229  if (ret > 0) {
230  octstr_append_data(in, buf, ret);
231  if (logging == LOG_data)
232  pretty_print(buf, ret);
233  } else if (ret == 0) {
234  fprintf(stderr, "Client closed socket\n");
235  exit(0);
236  } else {
237  if (errno == EINTR || errno == EAGAIN)
238  return;
239  error(errno, "read_data");
240  exit(1);
241  }
242 }
243 
244 static void write_data(Octstr *out, int fd) {
245  unsigned char buf[EVIL_BUFSIZE];
246  int len;
247  ssize_t ret;
248 
249  len = sizeof(buf);
250  if (len > octstr_len(out))
251  len = octstr_len(out);
252  if (len == 0)
253  return;
254  octstr_get_many_chars(buf, out, 0, len);
255  ret = write(fd, buf, len);
256  if (ret > 0) {
257  if (logging == LOG_data)
258  pretty_print(buf, ret);
259  octstr_delete(out, 0, ret);
260  } else if (ret == 0) {
261  warning(0, "empty write");
262  } else {
263  if (errno == EINTR || errno == EAGAIN)
264  return;
265  error(errno, "write_data");
266  exit(1);
267  }
268 }
269 
270 static void gen_message(Octstr *out);
271 
272 /* Return the minimum interval (in microseconds) after which we will
273  * want to be called again. This value is only used if we _don't_
274  * generate data this time through. */
275 static long gen_data(Octstr *out) {
276  unsigned char buf[EVIL_BUFSIZE];
277  size_t i;
278  long interval = -1;
279  static int last_sms; /* Used by ACT_deliver */
280  time_t now;
281 
283  switch (activity) {
284  case ACT_deliver:
285  now = time(NULL);
286  if (last_sms == 0)
287  last_sms = now;
288  while (last_sms < now) {
289  if (random() % 7 == 1) {
290  gen_message(out);
291  last_sms = now;
292  } else
293  last_sms++;
294  }
295  interval = 1000000;
296  break;
297  case ACT_flood:
298  gen_message(out);
299  break;
300  }
301  }
302 
303  switch (spew) {
304  case SPEW_binary:
305  for (i = 0; i < sizeof(buf); i++) {
306  buf[i] = random() % 256;
307  }
308  octstr_append_data(out, buf, sizeof(buf));
309  break;
310  }
311 
312  return interval;
313 }
314 
315 /******************************* CIMD 2 specific code ************************/
316 
318 
319 /* buf must be at least TIMESTAMP_MAXLEN bytes long. */
320 static void make_timestamp(unsigned char *buf, time_t fortime) {
321  /* Is there a thread-safe version of gmtime? */
322  struct tm tm = gw_gmtime(fortime);
323 
324  sprintf(buf, "%02d%02d%02d%02d%02d%02d",
325  tm.tm_year % 100, tm.tm_mon + 1, tm.tm_mday,
326  tm.tm_hour, tm.tm_min, tm.tm_sec);
327 }
328 
329 
330 static void send_packet(Octstr *out, int opcode, int sequence, ...) {
331  va_list ap;
332  int parm;
333  unsigned char *value;
334  int checksum;
335  int old_len, new_len;
336 
337  if (activity == ACT_listen)
338  return;
339 
340  old_len = octstr_len(out);
341 
342  octstr_format_append(out, "%c%02d:%03d%c", STX, opcode, sequence, TAB);
343 
344  va_start(ap, sequence);
345  for (parm = va_arg(ap, int); parm != 0; parm = va_arg(ap, int)) {
346  value = va_arg(ap, unsigned char *);
347  octstr_format_append(out, "%03d:%s\11", parm, value);
348  }
349  va_end(ap);
350 
351  /* Calculate checksum */
352  checksum = 0;
353  for (new_len = octstr_len(out); old_len < new_len; old_len++) {
354  checksum = (checksum + octstr_get_char(out, old_len)) & 0xff;
355  }
356 
357  octstr_format_append(out, "%02X%c", checksum, ETX);
358 }
359 
360 static void send_error(Octstr *out, int opcode, int sequence,
361  unsigned char *errorcode, unsigned char *errortext) {
362  if (logging == LOG_packets)
363  printf("SND: ERROR, %s\n", errortext);
364 
365  send_packet(out, opcode, sequence, 900, errorcode, 901, errortext, 0);
366 }
367 
368 static int eat_char(Octstr *packet, int ch) {
369  if (octstr_get_char(packet, 0) == ch) {
370  octstr_delete(packet, 0, 1);
371  return 0;
372  }
373  return -1;
374 }
375 
377  long start, datastart;
378  long tab;
379  Octstr *result;
380  Octstr *parmheader;
381 
382  parmheader = octstr_format("%c%03d:", TAB, parm);
383  start = octstr_search(packet, parmheader, 0);
384  if (start < 0) {
385  octstr_destroy(parmheader);
386  return NULL;
387  }
388  datastart = start + octstr_len(parmheader);
389 
390  tab = octstr_search_char(packet, TAB, datastart + 1);
391  if (tab < 0) {
392  tab = octstr_len(packet);
393  }
394 
395  result = octstr_copy(packet, datastart, tab - datastart);
396  octstr_delete(packet, start, tab - start);
397  octstr_destroy(parmheader);
398  return result;
399 }
400 
401 static long eat_number(Octstr *ostr) {
402  long result;
403  long pos;
404 
405  pos = octstr_parse_long(&result, ostr, 0, 10);
406  if (pos < 0)
407  return INT_MIN;
408 
409  octstr_delete(ostr, 0, pos);
410  return result;
411 }
412 
413 static long eat_int_parm(Octstr *packet, int parm, int maxlen) {
414  Octstr *value;
415  long result;
416 
417  value = eat_string_parm(packet, parm, maxlen);
418  if (!value)
419  return INT_MIN;
420 
421  result = eat_number(value);
422  if (octstr_len(value) > 0)
423  result = INT_MIN;
424 
425  octstr_destroy(value);
426  return result;
427 }
428 
429 static void eat_checksum(Octstr *packet) {
430  int len;
431  int ch1, ch2, ch3;
432 
433  len = octstr_len(packet);
434 
435  if (len < 3)
436  return;
437 
438  ch1 = octstr_get_char(packet, len - 3);
439  ch2 = octstr_get_char(packet, len - 2);
440  ch3 = octstr_get_char(packet, len - 1);
441 
442  if (isxdigit(ch3) && isxdigit(ch2) && ch1 == TAB)
443  octstr_delete(packet, len - 3, 3);
444 }
445 
446 static void handle_login(Octstr *packet, Octstr *out, int sequence) {
447  Octstr *user = eat_string_parm(packet, 10, 32);
448  Octstr *pass = eat_string_parm(packet, 11, 32);
449 
450  if (user == NULL)
451  user = octstr_create("");
452  if (pass == NULL)
453  pass = octstr_create("");
454 
455  if (logging == LOG_packets)
456  printf("RCV: Login user '%s', password '%s'\n",
457  octstr_get_cstr(user), octstr_get_cstr(pass));
458 
459  if (octstr_str_compare(user, username) == 0 &&
460  octstr_str_compare(pass, password) == 0) {
461  if (logging == LOG_packets)
462  printf("SND: Login OK\n");
463  send_packet(out, 51, sequence, 0);
464  } else {
465  send_error(out, 51, sequence, "100", "invalid login");
466  }
467 
468  octstr_destroy(user);
469  octstr_destroy(pass);
470 }
471 
472 static void handle_logout(Octstr *packet, Octstr *out, int sequence) {
473  if (logging == LOG_packets)
474  printf("RCV: Logout\n");
475  if (logging == LOG_packets)
476  printf("SND: Logout OK\n");
477  send_packet(out, 52, sequence, 0);
478 }
479 
480 static void handle_submit(Octstr *packet, Octstr *out, int sequence) {
481  Octstr *dest_addr = eat_string_parm(packet, 21, 20);
482  Octstr *orig_addr = eat_string_parm(packet, 23, 20);
483  long DCS = eat_int_parm(packet, 30, 3);
484  Octstr *UDH = eat_string_parm(packet, 32, 280);
485  Octstr *text = eat_string_parm(packet, 33, 480);
486  Octstr *textb = eat_string_parm(packet, 34, 280);
487  long valid_rel = eat_int_parm(packet, 50, 3);
488  Octstr *valid_abs = eat_string_parm(packet, 51, 12);
489  long proto_id = eat_int_parm(packet, 52, 3);
490  long delivery_rel = eat_int_parm(packet, 53, 3);
491  Octstr *delivery_abs = eat_string_parm(packet, 54, 12);
492  long reply_path = eat_int_parm(packet, 55, 1);
493  long SRR = eat_int_parm(packet, 56, 2);
494  long cancel = eat_int_parm(packet, 58, 1);
495  long tariff_class = eat_int_parm(packet, 64, 2);
496  long service_desc = eat_int_parm(packet, 65, 1);
497  long priority = eat_int_parm(packet, 67, 1);
498  List *other_dests = gwlist_create();
499  Octstr *tmp;
500 
501  while ((tmp = eat_string_parm(packet, 21, 20)))
502  gwlist_append(other_dests, tmp);
503 
504  if (logging == LOG_packets) {
505  int i;
506  printf("RCV: Submit to %s", octstr_get_cstr(dest_addr));
507  for (i = 0; i < gwlist_len(other_dests); i++) {
508  printf(", %s",
509  octstr_get_cstr(gwlist_get(other_dests, i)));
510  }
511  printf("\n");
512 
513  if (orig_addr)
514  printf(" From: %s\n", octstr_get_cstr(orig_addr));
515  if (DCS > INT_MIN)
516  printf(" Data coding: %ld\n", DCS);
517  if (UDH)
518  printf(" User data header: %s\n",
519  octstr_get_cstr(UDH));
520  if (text)
521  printf(" Text: %s\n", octstr_get_cstr(text));
522  if (textb)
523  printf(" Text (binary): %s\n",
524  octstr_get_cstr(textb));
525  if (valid_rel > INT_MIN)
526  printf(" Validity period: %ld (relative)\n",
527  valid_rel);
528  if (valid_abs)
529  printf(" Validity period: %s (absolute)\n",
530  octstr_get_cstr(valid_abs));
531  if (proto_id > INT_MIN)
532  printf(" Protocol ID: %ld\n", proto_id);
533  if (delivery_rel > INT_MIN)
534  printf(" First delivery: %ld (relative)\n",
535  delivery_rel);
536  if (delivery_abs)
537  printf(" First delivery: %s (absolute)\n",
538  octstr_get_cstr(delivery_abs));
539  if (reply_path == 0)
540  printf(" Reply path disabled\n");
541  else if (reply_path == 1)
542  printf(" Reply path enabled\n");
543  else if (reply_path > INT_MAX)
544  printf(" Reply path: %ld\n", reply_path);
545  if (SRR > INT_MAX)
546  printf(" Status report flags: %ld\n", SRR);
547  if (cancel == 0)
548  printf(" Cancel disabled\n");
549  else if (cancel == 1)
550  printf(" Cancel enabled\n");
551  else if (cancel > INT_MAX)
552  printf(" Cancel enabled: %ld\n", cancel);
553  if (tariff_class > INT_MAX)
554  printf(" Tariff class: %ld\n", tariff_class);
555  if (service_desc > INT_MAX)
556  printf(" Service description: %ld\n", service_desc);
557  if (priority > INT_MAX)
558  printf(" Priority: %ld\n", priority);
559  }
560 
561  if (!dest_addr) {
562  send_error(out, 53, sequence, "300", "no destination");
563  } else if (gwlist_len(other_dests) > 0) {
564  send_error(out, 53, sequence, "301", "too many destinations");
565  /* TODO: Report many other possible errors here */
566  } else {
567  unsigned char buf[TIMESTAMP_MAXLEN];
568 
569  make_timestamp(buf, time(NULL));
570  if (logging == LOG_packets)
571  printf("SND: Submit OK\n");
572  send_packet(out, 53, sequence,
573  21, octstr_get_cstr(dest_addr),
574  60, buf,
575  0);
576  }
577 
578  octstr_destroy(dest_addr);
579  octstr_destroy(orig_addr);
580  octstr_destroy(UDH);
582  octstr_destroy(textb);
583  octstr_destroy(valid_abs);
584  octstr_destroy(delivery_abs);
585  gwlist_destroy(other_dests, octstr_destroy_item);
586 }
587 
588 static void handle_enquire(Octstr *packet, Octstr *out, int sequence) {
589  Octstr *dest_addr = eat_string_parm(packet, 21, 20);
590  Octstr *timestamp = eat_string_parm(packet, 60, 12);
591 
592  if (logging == LOG_packets)
593  printf("RCV: Enquire status, dest='%s', time='%s'\n",
594  dest_addr ? octstr_get_cstr(dest_addr) : "",
595  timestamp ? octstr_get_cstr(timestamp) : "");
596 
597  if (!dest_addr) {
598  send_error(out, 54, sequence, "400", "no destination");
599  } else if (!timestamp) {
600  send_error(out, 54, sequence, "401", "no timestamp");
601  } else {
602  if (logging == LOG_packets)
603  printf("SND: Respond: status unknown\n");
604  send_packet(out, 54, sequence,
605  21, octstr_get_cstr(dest_addr),
606  60, octstr_get_cstr(timestamp),
607  61, "0",
608  0);
609  }
610  octstr_destroy(dest_addr);
611  octstr_destroy(timestamp);
612 }
613 
614 static void handle_delivery_request(Octstr *packet, Octstr *out, int sequence) {
615  long mode = eat_int_parm(packet, 68, 1);
616 
617  if (logging == LOG_packets) {
618  switch (mode) {
619  case 0: printf("RCV: Delivery request, messages waiting?\n");
620  break;
621  case 1: printf("RCV: Delivery request, one message\n");
622  break;
623  case 2: printf("RCV: Delivery request, all messages\n");
624  break;
625  case INT_MIN:
626  printf("RCV: Delivery request, no mode\n");
627  break;
628  default:
629  printf("RCV: Delivery request, mode %ld\n", mode);
630  }
631  }
632 
633  if (mode == INT_MIN)
634  mode = 1;
635 
636  switch (mode) {
637  case 0:
638  if (logging == LOG_packets)
639  printf("SND: Respond: 0 messages\n");
640  send_packet(out, 55, sequence,
641  66, "0",
642  0);
643  break;
644 
645  case 1:
646  send_error(out, 55, sequence, "500", "no messages available");
647  break;
648 
649  case 2:
650  send_error(out, 55, sequence, "500", "no messages available");
651  break;
652 
653  default:
654  send_error(out, 55, sequence, "501", "bad mode");
655  break;
656  }
657 }
658 
659 static void handle_cancel(Octstr *packet, Octstr *out, int sequence) {
660  long mode = eat_int_parm(packet, 59, 1);
661  Octstr *timestamp = eat_string_parm(packet, 60, 12);
662  Octstr *destination = eat_string_parm(packet, 21, 20);
663 
664  if (logging == LOG_packets) {
665  printf("RCV: Cancel");
666  if (mode != INT_MIN)
667  printf(", mode %ld", mode);
668  if (destination)
669  printf(", dest '%s'", octstr_get_cstr(destination));
670  if (timestamp)
671  printf(", time '%s'", octstr_get_cstr(timestamp));
672  printf("\n");
673  }
674 
675  if (mode < 0 || mode > 2)
676  send_error(out, 56, sequence, "602", "bad mode");
677  else {
678  if (logging == LOG_packets)
679  printf("SND: OK\n");
680  send_packet(out, 56, sequence, 0);
681  }
682 }
683 
684 
685 static void handle_set(Octstr *packet, Octstr *out, int sequence) {
686  Octstr *pass = eat_string_parm(packet, 11, 32);
687 
688  if (pass) {
689  if (logging == LOG_packets)
690  printf("RCV: Set password to '%s'\n",
691  octstr_get_cstr(pass));
692  send_error(out, 58, sequence,
693  "801", "changing password not allowed");
694  } else {
695  if (logging == LOG_packets)
696  printf("RCV: Set, unknown parameters\n");
697  send_error(out, 58, sequence, "3", "cannot set");
698  }
699 }
700 
701 
702 static void handle_get(Octstr *packet, Octstr *out, int sequence) {
703  long number = eat_int_parm(packet, 500, 3);
704 
705  if (logging == LOG_packets)
706  printf("RCV: Get parameter #%ld\n", number);
707 
708  if (number == INT_MIN) {
709  send_error(out, 59, sequence, "900", "missing parameter");
710  } else if (number == 501) {
711  unsigned char buf[TIMESTAMP_MAXLEN];
712  make_timestamp(buf, time(NULL));
713  if (logging == LOG_packets)
714  printf("SND: OK, SMSC timestamp is '%s'\n", buf);
715  send_packet(out, 59, sequence,
716  501, buf,
717  0);
718  } else {
719  send_error(out, 59, sequence, "900", "unknown parameter");
720  }
721 }
722 
723 static void handle_alive(Octstr *packet, Octstr *out, int sequence) {
724  if (logging == LOG_packets)
725  printf("RCV: Alive?\n");
726  if (logging == LOG_packets)
727  printf("SND: Alive.\n");
728  send_packet(out, 90, sequence, 0);
729 }
730 
731 static void handle_deliver_response(Octstr *packet, Octstr *out, int sequence) {
732  awaiting_response = 0;
733  if (logging == LOG_packets)
734  printf("RCV: Deliver response\n");
735  deliveries++;
736  if (max_deliveries > 0 && deliveries == max_deliveries) {
737  time_t elapsed = time(NULL) - start_time;
738  printf("LOG: %ld deliveries in %ld seconds\n",
739  (long) max_deliveries, (long) elapsed);
740  }
741  /* No need to respond to a response */
742 }
743 
744 static void handle_deliver_status_report_response(Octstr *packet, Octstr *out, int sequence) {
745  awaiting_response = 0;
746  if (logging == LOG_packets)
747  printf("RCV: Deliver status report response\n");
748  /* No need to respond to a response */
749 }
750 
751 static void handle_alive_response(Octstr *packet, Octstr *out, int sequence) {
752  awaiting_response = 0;
753  if (logging == LOG_packets)
754  printf("RCV: Alive.\n");
755  /* No need to respond to a response */
756 }
757 
758 static void handle_nack(Octstr *packet, Octstr *out, int sequence) {
759  awaiting_response = 0;
760  if (logging == LOG_packets)
761  printf("RCV: NACK\n");
762  /* TODO: We should retransmit if we get a nack, but there's
763  * no record of what request we sent. */
764 }
765 
766 typedef void (*packet_handler)(Octstr *, Octstr *, int);
767 
768 struct {
769  int opcode;
771 } handlers[] = {
772  { 1, handle_login },
773  { 2, handle_logout },
774  { 3, handle_submit },
775  { 4, handle_enquire },
777  { 6, handle_cancel },
778  { 8, handle_set },
779  { 9, handle_get },
780  { 40, handle_alive },
781  { 70, handle_deliver_response },
783  { 90, handle_alive_response },
784  { 99, handle_nack },
785  { -1, NULL },
786 };
787 
788 static void parse_packet(Octstr *packet, Octstr *out) {
789  int opcode, sequence;
790  int i;
791 
793 
795  if (opcode < 0 || eat_char(packet, ':') < 0)
796  return;
797  sequence = eat_number(packet);
798  if (sequence < 0)
799  return;
800 
801  for (i = 0; handlers[i].opcode >= 0; i++) {
802  if (handlers[i].opcode == opcode) {
803  (handlers[i].handler)(packet, out, sequence);
804  break;
805  }
806  }
807 
808  if (handlers[i].opcode < 0) { /* Loop failed */
809  if (logging == LOG_packets)
810  printf("RCV: unknown operation %ld\n",
811  (long) handlers[i].opcode);
812  send_error(out, 98, sequence, "1", "unexpected operation");
813  }
814 }
815 
816 /* Parse the data stream for packets, and send out replies. */
817 static void parse_data(Octstr *in, Octstr *out) {
818  int stx, etx;
819  Octstr *packet;
820 
821  for (;;) {
822  /* Look for start of packet. Delete everything up to the start
823  * marker. (CIMD2 section 3.1 says we can ignore any data
824  * transmitted between packets.) */
825  stx = octstr_search_char(in, STX, 0);
826  if (stx < 0)
827  octstr_delete(in, 0, octstr_len(in));
828  else if (stx > 0)
829  octstr_delete(in, 0, stx);
830 
831  etx = octstr_search_char(in, ETX, 0);
832  if (etx < 0)
833  return; /* Incomplete packet; wait for more data. */
834 
835  /* Copy the data between stx and etx */
836  packet = octstr_copy(in, 1, etx - 1);
837  /* Then cut the packet (including stx and etx) from inbuffer */
838  octstr_delete(in, 0, etx + 1);
839 
840  parse_packet(packet, out);
841 
843  }
844 }
845 
846 static void random_address(unsigned char *buf, int size) {
847  int len = random() % size;
848 
849  while (len--) {
850  *buf++ = '0' + random() % 10;
851  }
852 
853  *buf++ = '\0';
854 }
855 
856 static void random_message(unsigned char *buf, int size) {
857  int len = random() % size;
858 
859  while (len--) {
860  do {
861  *buf = random() % 256;
862  } while (*buf == STX || *buf == ETX || *buf == TAB);
863  buf++;
864  }
865 
866  *buf++ = '\0';
867 }
868 
869 static void random_hex(unsigned char *buf, int size) {
870  int len = random() % size;
871 
872  /* Make even */
873  len -= (len % 2);
874 
875  while (len--) {
876  int c = random() % 16;
877  if (c < 10)
878  *buf++ = c + '0';
879  else
880  *buf++ = c - 10 + 'a';
881  }
882 
883  *buf++ = '\0';
884 }
885 
886 static void gen_message(Octstr *out) {
887  static int send_seq = 0;
888  unsigned char dest[21];
889  unsigned char orig[21];
890  unsigned char scts[TIMESTAMP_MAXLEN];
891  unsigned char message[481];
892  unsigned char udh[281];
893 
894  if (awaiting_response == 1)
895  return;
896 
897  random_address(dest, sizeof(dest));
898  random_address(orig, sizeof(orig));
899  make_timestamp(scts, time(NULL));
900  random_message(message, sizeof(message));
901  if (random() % 2 == 0)
902  random_hex(udh, sizeof(udh));
903  else
904  *udh = 0;
905 
906  if (logging == LOG_packets)
907  printf("SND: Deliver message (random)\n");
908 
909  if (*udh) {
910  send_packet(out, 20, send_seq,
911  21, dest,
912  23, orig,
913  60, scts,
914  32, udh,
915  33, message,
916  0);
917  } else {
918  send_packet(out, 20, send_seq,
919  21, dest,
920  23, orig,
921  60, scts,
922  33, message,
923  0);
924  }
925 
926  send_seq += 2;
927  if (send_seq > 255)
928  send_seq = 0;
929 
930  awaiting_response = 1;
931 }
932 
933 /************************** CIMD 2 specific code ends ************************/
934 
935 static void main_loop(void) {
936  fd_set readfds, writefds;
937  int n;
938  static int reported_outfull = 0;
939  int interval = -1;
940 
941  inbuffer = octstr_create("");
943  start_time = time(NULL);
944 
945  for (;;) {
948  } else if (!reported_outfull) {
949  warning(0, "outbuffer getting full; waiting...");
950  reported_outfull = 1;
951  }
952 
953  FD_ZERO(&readfds);
954  FD_SET(sockfd, &readfds);
955 
956  if (octstr_len(outbuffer) > 0) {
957  FD_ZERO(&writefds);
958  FD_SET(sockfd, &writefds);
959  n = select(sockfd+1, &readfds, &writefds, NULL, NULL);
960  } else {
961  struct timeval tv;
962  struct timeval *tvp;
963 
964  if (interval >= 0) {
965  tv.tv_sec = 0;
966  tv.tv_usec = interval;
967  tvp = &tv;
968  } else {
969  tvp = NULL;
970  }
971  n = select(sockfd+1, &readfds, NULL, NULL, tvp);
972  }
973 
974  if (n < 0) {
975  if (errno == EINTR) {
976  warning(errno, "main loop, select");
977  continue;
978  }
979  error(errno, "main loop, select");
980  sleep(1);
981  continue;
982  }
983  if (n > 0) {
984  if (FD_ISSET(sockfd, &readfds)) {
987  }
988  if (octstr_len(outbuffer) > 0 &&
989  FD_ISSET(sockfd, &writefds)) {
991  }
993  reported_outfull = 0;
994  }
995  }
996  }
997 }
998 
999 static struct {
1000  unsigned char *option;
1001  void *location;
1002  int number;
1003 } options[] = {
1004  { "--user", &username, 0 },
1005  { "--password", &password, 0 },
1006  { "--port", &port, 1 },
1007  { "--intro", &intro, 0 },
1008  { "--activity", &activity, 1 },
1009  { "--spew", &spew, 1 },
1010  { "--logging", &logging, 1 },
1011  { "--checking", &checking, 1 },
1012  { "--max", &max_deliveries, 1 },
1013  { NULL, NULL, 0 },
1014 };
1015 
1016 static int wait_for_client(int port) {
1017  struct sockaddr_in sin;
1018  socklen_t addrlen;
1019  int listenfd;
1020  int clientfd;
1021  Octstr *addr;
1022 
1023  listenfd = make_server_socket(port, NULL);
1024  if (listenfd < 0) {
1025  fprintf(stderr, "%s: failed to open socket at port %d\n",
1026  progname, port);
1027  exit(1);
1028  }
1029 
1030  do {
1031  addrlen = sizeof(sin);
1032  clientfd = accept(listenfd, (struct sockaddr *)&sin, &addrlen);
1033  if (clientfd < 0) {
1034  error(errno, "failed to accept new connection");
1035  }
1036  } while (clientfd < 0);
1037 
1038  if (socket_set_blocking(clientfd, 0) < 0) {
1039  panic(0, "failed to make client socket nonblocking");
1040  }
1041 
1042  addr = gw_netaddr_to_octstr(AF_INET, &sin.sin_addr);
1043  info(0, "Accepted client from %s:%d",
1044  octstr_get_cstr(addr), ntohs(sin.sin_port));
1045  octstr_destroy(addr);
1046 
1047  close(listenfd);
1048 
1049  return clientfd;
1050 }
1051 
1052 
1053 int main(int argc, char *argv[]) {
1054  int i;
1055  int opt;
1056 
1057  gwlib_init();
1058 
1059  progname = argv[0];
1060  srandom(0); /* Make "random" data reproducible */
1061 
1062  for (i = 1; i < argc; i++) {
1063  for (opt = 0; options[opt].option; opt++) {
1064  if (strcmp(argv[i], options[opt].option) == 0) {
1065  if (i + 1 >= argc) {
1066  fprintf(stderr, "%s: missing argument to %s",
1067  progname, argv[i]);
1068  exit(2);
1069  }
1070  if (options[opt].number) {
1071  * (int *) options[opt].location = atoi(argv[i+1]);
1072  } else {
1073  * (char **) options[opt].location = argv[i+1];
1074  }
1075  i++;
1076  break;
1077  }
1078  }
1079  if (options[opt].option)
1080  continue;
1081  if (strcmp(argv[i], "--help") == 0) {
1082  usage(stdout);
1083  exit(0);
1084  }
1085  if (argv[i][0] == '-') {
1086  fprintf(stderr, "%s: unknown option %s\n",
1087  progname, argv[i]);
1088  usage(stderr);
1089  exit(2);
1090  }
1091  }
1092 
1094  main_loop();
1095  return 0;
1096 }
void error(int err, const char *fmt,...)
Definition: log.c:648
void info(int err, const char *fmt,...)
Definition: log.c:672
static long eat_int_parm(Octstr *packet, int parm, int maxlen)
Definition: test_cimd2.c:413
int socket_set_blocking(int fd, int blocking)
Definition: socket.c:368
void(* packet_handler)(Octstr *, Octstr *, int)
Definition: test_cimd2.c:766
Definition: http.c:2014
int size
Definition: wsasm.c:84
static long gen_data(Octstr *out)
Definition: test_cimd2.c:275
void octstr_append_data(Octstr *ostr, const char *data, long len)
Definition: octstr.c:1497
static void handle_deliver_status_report_response(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:744
void gwlist_append(List *list, void *item)
Definition: list.c:179
struct tm gw_gmtime(time_t t)
Definition: protected.c:137
CHARS
Definition: test_cimd2.c:159
Octstr * inbuffer
Definition: test_cimd2.c:147
long gwlist_len(List *list)
Definition: list.c:166
int port
Definition: test_cimd2.c:102
void * gwlist_get(List *list, long pos)
Definition: list.c:292
static long eat_number(Octstr *ostr)
Definition: test_cimd2.c:401
static void send_packet(Octstr *out, int opcode, int sequence,...)
Definition: test_cimd2.c:330
long octstr_search(const Octstr *haystack, const Octstr *needle, long pos)
Definition: octstr.c:1070
static void handle_delivery_request(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:614
CHK
Definition: test_cimd2.c:128
static void main_loop(void)
Definition: test_cimd2.c:935
Definition: test_cimd2.c:164
static void handle_submit(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:480
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1012
static int eat_char(Octstr *packet, int ch)
Definition: test_cimd2.c:368
static void parse_packet(Octstr *packet, Octstr *out)
Definition: test_cimd2.c:788
int awaiting_response
Definition: test_cimd2.c:317
unsigned char * username
Definition: test_cimd2.c:99
static void handle_logout(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:472
int number
Definition: test_cimd2.c:1002
static void random_hex(unsigned char *buf, int size)
Definition: test_cimd2.c:869
unsigned char * password
Definition: test_cimd2.c:100
void * location
Definition: test_cimd2.c:1001
SPEW
Definition: test_cimd2.c:114
int deliveries
Definition: test_cimd2.c:142
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1527
static void handle_nack(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:758
char * text
Definition: smsc_cimd2.c:921
static void handle_login(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:446
Definition: test_cimd2.c:163
Octstr * outbuffer
Definition: test_cimd2.c:148
static void eat_checksum(Octstr *packet)
Definition: test_cimd2.c:429
unsigned char * intro
Definition: test_cimd2.c:105
Octstr * gw_netaddr_to_octstr(int af, void *src)
Definition: socket.c:677
static void read_data(Octstr *in, int fd)
Definition: test_cimd2.c:224
static void handle_alive(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:723
int main(int argc, char *argv[])
Definition: test_cimd2.c:1053
int make_server_socket(int port, const char *interface_name)
Definition: socket.c:93
void warning(int err, const char *fmt,...)
Definition: log.c:660
Definition: seewbmp.c:154
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
struct @77 handlers[]
#define octstr_create(cstr)
Definition: octstr.h:125
void octstr_destroy_item(void *os)
Definition: octstr.c:336
static void pretty_print(unsigned char *data, size_t length)
Definition: test_cimd2.c:201
ACT
Definition: test_cimd2.c:107
static void handle_set(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:685
static void parse_data(Octstr *in, Octstr *out)
Definition: test_cimd2.c:817
double interval
Definition: fakewap.c:234
unsigned char * option
Definition: test_cimd2.c:1000
static void write_data(Octstr *out, int fd)
Definition: test_cimd2.c:244
static void random_address(unsigned char *buf, int size)
Definition: test_cimd2.c:846
int sockfd
Definition: test_cimd2.c:145
int maxlen
Definition: smsc_cimd2.c:214
static void gen_message(Octstr *out)
Definition: test_cimd2.c:886
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
static void make_timestamp(unsigned char *buf, time_t fortime)
Definition: test_cimd2.c:320
static void random_message(unsigned char *buf, int size)
Definition: test_cimd2.c:856
Definition: octstr.c:118
static void handle_alive_response(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:751
static struct @78 options[]
static Octstr * eat_string_parm(Octstr *packet, int parm, int maxlen)
Definition: test_cimd2.c:376
static void handle_get(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:702
static void handle_deliver_response(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:731
#define panic
Definition: log.h:87
int octstr_str_compare(const Octstr *ostr, const char *str)
Definition: octstr.c:973
int socklen_t
Definition: socket.h:73
long octstr_parse_long(long *nump, Octstr *ostr, long pos, int base)
Definition: octstr.c:749
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2507
int activity
Definition: test_cimd2.c:136
#define gwlist_create()
Definition: list.h:136
int max_deliveries
Definition: test_cimd2.c:141
packet_handler handler
Definition: test_cimd2.c:770
void gwlib_init(void)
Definition: gwlib.c:78
static void send_error(Octstr *out, int opcode, int sequence, unsigned char *errorcode, unsigned char *errortext)
Definition: test_cimd2.c:360
static void handle_cancel(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:659
int logging
Definition: test_cimd2.c:138
static void handle_enquire(Octstr *packet, Octstr *out, int sequence)
Definition: test_cimd2.c:588
time_t start_time
Definition: test_cimd2.c:143
static int wait_for_client(int port)
Definition: test_cimd2.c:1016
void octstr_get_many_chars(char *buf, Octstr *ostr, long pos, long len)
Definition: octstr.c:425
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:406
static void usage(FILE *out)
Definition: test_cimd2.c:167
int spew
Definition: test_cimd2.c:137
unsigned char * progname
Definition: test_cimd2.c:96
Definition: list.c:102
static int start
int checking
Definition: test_cimd2.c:139
int opcode
Definition: test_cimd2.c:769
LOG
Definition: test_cimd2.c:121
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.