test_su.c 11.8 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@ingroup su_root_ex
26
 * @CFILE test_su.c
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29 30 31 32
 *
 * @brief Test program for su wait objects, root, timers, clones, and messages.
 *
 * @internal
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
33
 *
Pekka Pessi's avatar
Pekka Pessi committed
34 35 36 37 38 39 40 41 42 43
 * @date Created: Thu Mar 18 19:40:51 1999 pessi
 */

#include "config.h"

struct pinger;
#define SU_ROOT_MAGIC_T struct pinger
#define SU_INTERNAL_P   su_root_t *
#define SU_MSG_ARG_T    su_sockaddr_t

44 45 46
#include "sofia-sip/su.h"
#include "sofia-sip/su_wait.h"
#include "sofia-sip/su_log.h"
47 48
#include "sofia-sip/su_debug.h"

49 50 51 52 53 54
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <assert.h>

55
char const name[] = "test_su";
56 57 58 59 60 61 62 63 64 65 66

#if HAVE_FUNC
#define enter (void)SU_DEBUG_9(("%s: %s: entering\n", name, __func__))
#define nh_enter (void)SU_DEBUG_9(("%s %s(%p): entering\n", name, __func__, nh))
#elif HAVE_FUNCTION
#define enter (void)SU_DEBUG_9(("%s: %s: entering\n", name, __FUNCTION__))
#define nh_enter (void)SU_DEBUG_9(("%s %s(%p): entering\n", name, __FUNCTION__, nh))
#define __func__ __FUNCTION__
#else
#define enter ((void)0)
#define nh_enter ((void)0)
67
#define __func__ "test_su"
68
#endif
69

Pekka Pessi's avatar
Pekka Pessi committed
70 71 72 73 74 75 76 77 78 79 80 81
struct pinger {
  enum { PINGER = 1, PONGER = 2 } const sort;
  char const *  name;
  unsigned      running : 1;
  unsigned      : 0;
  su_root_t    *root;
  su_socket_t   s;
  su_timer_t   *t;
  int           id;
  int           rindex;
  su_time_t     when;
  su_sockaddr_t addr;
82 83
  double        rtt_total;
  int           rtt_n;
Pekka Pessi's avatar
Pekka Pessi committed
84 85 86 87 88 89
};

short opt_family = AF_INET;
short opt_verbatim = 0;
short opt_singlethread = 0;

90
static su_socket_t udpsocket(void)
Pekka Pessi's avatar
Pekka Pessi committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
{
  su_socket_t s;
  su_sockaddr_t su = { 0 };
  socklen_t sulen = sizeof(su);
  char nbuf[64];

  su.su_family = opt_family;

  su_getlocalip(&su);

  s = su_socket(su.su_family, SOCK_DGRAM, 0);
  if (s == INVALID_SOCKET) {
    su_perror("udpsocket: socket");
    exit(1);
  }

  if (bind(s, &su.su_sa, su_sockaddr_size(&su)) == SOCKET_ERROR) {
    su_perror("udpsocket: bind");
    exit(1);
  }

  if (getsockname(s, &su.su_sa, &sulen) == SOCKET_ERROR) {
    su_perror("udpsocket: getsockname");
114
    exit(1);
Pekka Pessi's avatar
Pekka Pessi committed
115 116 117 118
  }

  if (opt_verbatim)
    printf("udpsocket: using address [%s]:%u\n",
119
	   su_inet_ntop(su.su_family, SU_ADDR(&su), nbuf, sizeof(nbuf)),
Pekka Pessi's avatar
Pekka Pessi committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133
	   ntohs(su.su_sin.sin_port));

  return s;
}

static char *snow(su_time_t now)
{
  static char buf[24];

  su_time_print(buf, sizeof(buf), &now);

  return buf;
}

134
void
Pekka Pessi's avatar
Pekka Pessi committed
135 136 137 138 139 140 141 142 143 144
do_ping(struct pinger *p, su_timer_t *t, void *p0)
{
  char buf[1024];

  assert(p == su_root_magic(su_timer_root(t)));
  assert(p->sort == PINGER);

  p->when = su_now();

  snprintf(buf, sizeof(buf), "Ping %d at %s", p->id++, snow(p->when));
145
  if (su_sendto(p->s, buf, strlen(buf), 0,
146
		&p->addr, su_sockaddr_size(&p->addr)) == -1) {
Pekka Pessi's avatar
Pekka Pessi committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    su_perror("do_ping: send");
  }

  if (opt_verbatim) {
    puts(buf);
    fflush(stdout);
  }
}

int
do_rtt(struct pinger *p, su_wait_t *w, void *p0)
{
  su_sockaddr_t su;
  struct sockaddr * const susa = &su.su_sa;
  socklen_t susize[] = { sizeof(su)};
  char buf[1024];
  char nbuf[1024];
  int n;
  su_time_t now = su_now();
166
  double rtt;
Pekka Pessi's avatar
Pekka Pessi committed
167 168 169 170

  assert(p0 == p);
  assert(p->sort == PINGER);

171 172 173
  rtt = su_time_diff(now, p->when);

  p->rtt_total += rtt, p->rtt_n++;
Pekka Pessi's avatar
Pekka Pessi committed
174 175 176 177 178 179 180 181 182 183 184

  su_wait_events(w, p->s);

  n = recvfrom(p->s, buf, sizeof(buf) - 1, 0, susa, susize);
  if (n < 0) {
	  su_perror("do_rtt: recvfrom");
	  return 0;
  }
  buf[n] = 0;

  if (opt_verbatim)
185
    printf("do_rtt: %d bytes from [%s]:%u: \"%s\", rtt = %lg ms\n",
186
	   n, su_inet_ntop(su.su_family, SU_ADDR(&su), nbuf, sizeof(nbuf)),
187 188 189
	   ntohs(su.su_sin.sin_port), buf, rtt / 1000);

  do_ping(p, p->t, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

  return 0;
}

void
do_pong(struct pinger *p, su_timer_t *t, void *p0)
{
  char buf[1024];

  assert(p == su_root_magic(su_timer_root(t)));
  assert(p->sort == PONGER);

  p->id = 0;

  snprintf(buf, sizeof(buf), "Pong at %s", snow(su_now()));
205
  if (su_sendto(p->s, buf, strlen(buf), 0,
206
		&p->addr, su_sockaddr_size(&p->addr)) == -1) {
Pekka Pessi's avatar
Pekka Pessi committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    su_perror("do_pong: send");
  }

  if (opt_verbatim) {
    puts(buf);
    fflush(stdout);
  }
}

int
do_recv(struct pinger *p, su_wait_t *w, void *p0)
{
  su_sockaddr_t su;
  socklen_t susize[] = { sizeof(su)};
  char buf[1024];
  char nbuf[1024];
  int n;
  su_time_t now = su_now();

  assert(p0 == p);
  assert(p->sort == PONGER);

  su_wait_events(w, p->s);

  n = recvfrom(p->s, buf, sizeof(buf) - 1, 0, &su.su_sa, susize);
  if (n < 0) {
	  su_perror("do_recv: recvfrom");
	  return 0;
  }
  buf[n] = 0;

  if (opt_verbatim)
    printf("do_recv: %d bytes from [%s]:%u: \"%s\" at %s\n",
240
	   n, su_inet_ntop(su.su_family, SU_ADDR(&su), nbuf, sizeof(nbuf)),
Pekka Pessi's avatar
Pekka Pessi committed
241 242 243 244
	   ntohs(su.su_sin.sin_port), buf, snow(now));

  fflush(stdout);

245 246 247 248
#if 0
  if (p->id)
    puts("do_recv: already a pending reply");

Pekka Pessi's avatar
Pekka Pessi committed
249 250 251 252 253 254
  if (su_timer_set(p->t, do_pong, p) < 0) {
    fprintf(stderr, "do_recv: su_timer_set() error\n");
    return 0;
  }

  p->id = 1;
255 256 257
#else
  do_pong(p, p->t, NULL);
#endif
Pekka Pessi's avatar
Pekka Pessi committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

  return 0;
}

void
do_exit(struct pinger *x, su_timer_t *t, void *x0)
{
  if (opt_verbatim)
    printf("do_exit at %s\n", snow(su_now()));
  su_root_break(su_timer_root(t));
}

int
do_init(su_root_t *root, struct pinger *p)
{
273
  su_wait_t w;
Pekka Pessi's avatar
Pekka Pessi committed
274 275 276 277
  su_socket_t s;
  long interval;
  su_timer_t *t;
  su_wakeup_f f;
278 279 280 281 282
  int index;
#if nomore
  su_wait_t w0;
  int index0
#endif
Pekka Pessi's avatar
Pekka Pessi committed
283

284 285
  enter;

Pekka Pessi's avatar
Pekka Pessi committed
286 287 288 289 290 291 292 293 294 295 296 297 298
  switch (p->sort) {
  case PINGER: f = do_rtt;  interval = 200; break;
  case PONGER: f = do_recv; interval = 40;  break;
  default:
    return SU_FAILURE;
  }

  p->t = t = su_timer_create(su_root_task(root), interval);
  if (t == NULL) {
    su_perror("su_timer_create");
    return SU_FAILURE;
  }

299 300
  /* Create a sockets,  */
  p->s = s = udpsocket();
301 302

#if nomore
303
  if (su_wait_create(&w0, s, SU_WAIT_IN) == SOCKET_ERROR) {
304
    su_perror("su_wait_create");
305 306 307 308
    return SU_FAILURE;
  }

  index0 = su_root_register(root, &w0, f, p, 0);
Pekka Pessi's avatar
Pekka Pessi committed
309 310 311 312
  if (index0 == SOCKET_ERROR) {
    su_perror("su_root_register");
    return SU_FAILURE;
  }
313
#endif
Pekka Pessi's avatar
Pekka Pessi committed
314

315
  if (su_wait_create(&w, s, SU_WAIT_IN) == SOCKET_ERROR) {
316
    su_perror("su_wait_create");
317 318 319
    return SU_FAILURE;
  }

Pekka Pessi's avatar
Pekka Pessi committed
320 321 322 323 324 325
  index = su_root_register(root, &w, f, p, 0);
  if (index == SOCKET_ERROR) {
    su_perror("su_root_register");
    return SU_FAILURE;
  }

326
#if nomore
Pekka Pessi's avatar
Pekka Pessi committed
327
  su_root_deregister(root, index0);
328
#endif
Pekka Pessi's avatar
Pekka Pessi committed
329 330 331 332 333 334 335 336
  p->rindex = index;

  return 0;
}

void
do_destroy(su_root_t *root, struct pinger *p)
{
337 338
  enter;

Pekka Pessi's avatar
Pekka Pessi committed
339 340 341 342 343 344 345 346 347 348
  if (opt_verbatim)
    printf("do_destroy %s at %s\n", p->name, snow(su_now()));
  su_root_deregister(root, p->rindex);
  su_timer_destroy(p->t), p->t = NULL;
  p->running = 0;
}

void
start_ping(struct pinger *p, su_msg_r msg, su_sockaddr_t *arg)
{
349 350
  enter;

Pekka Pessi's avatar
Pekka Pessi committed
351 352 353 354 355 356 357 358
  if (!p->running)
    return;

  if (opt_verbatim)
    printf("start_ping: %s\n", p->name);

  p->addr = *arg;
  p->id = 1;
359 360

  su_timer_set_interval(p->t, do_ping, p, 0);
Pekka Pessi's avatar
Pekka Pessi committed
361 362 363 364 365 366 367
}

void
start_pong(struct pinger *p, su_msg_r msg, su_sockaddr_t *arg)
{
  su_msg_r reply;

368 369
  enter;

Pekka Pessi's avatar
Pekka Pessi committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  if (!p->running)
    return;

  if (opt_verbatim)
    printf("start_pong: %s\n", p->name);

  p->addr = *arg;

  if (su_msg_reply(reply, msg, start_ping, sizeof(p->addr)) == 0) {
    socklen_t sinsize[1] = { sizeof(p->addr) };
    if (getsockname(p->s, (struct sockaddr*)su_msg_data(reply), sinsize)
	== SOCKET_ERROR)
      su_perror("start_pong: getsockname()"), exit(1);
    su_msg_send(reply);
  }
  else {
386
    fprintf(stderr, "su_msg_reply failed!\n");
Pekka Pessi's avatar
Pekka Pessi committed
387 388 389 390 391 392 393 394
  }
}

void
init_ping(struct pinger *p, su_msg_r msg, su_sockaddr_t *arg)
{
  su_msg_r reply;

395 396
  enter;

Pekka Pessi's avatar
Pekka Pessi committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  if (opt_verbatim)
    printf("init_ping: %s\n", p->name);

  if (su_msg_reply(reply, msg, start_pong, sizeof(p->addr)) == 0) {
    socklen_t sinsize[1] = { sizeof(p->addr) };
    if (getsockname(p->s, (struct sockaddr*)su_msg_data(reply), sinsize)
	== SOCKET_ERROR)
      su_perror("start_pong: getsockname()"), exit(1);
    su_msg_send(reply);
  }
  else {
    fprintf(stderr, "su_msg_reply failed!\n");
  }
}

void
time_test(void)
{
  su_time_t now = su_now(), then = now;
  su_duration_t t1, t2;
  su_duration_t us;

419 420
  enter;

Pekka Pessi's avatar
Pekka Pessi committed
421 422 423 424 425 426 427 428 429 430 431 432 433
  for (us = 0; us < 1000000; us += 300) {
    then.tv_sec = now.tv_sec;
    if ((then.tv_usec = now.tv_usec + us) >= 1000000)
      then.tv_usec -= 1000000, then.tv_sec++;
    t1 = su_duration(now, then);
    t2 = su_duration(then, now);
    assert(t1 == -t2);
  }

  if (opt_verbatim)
    printf("time_test: passed\n");
}

434
#if HAVE_SIGNAL
435 436 437
#include <unistd.h>
#include <signal.h>

438 439
#if HAVE_ALARM

440 441
static RETSIGTYPE sig_alarm(int s)
{
442 443
  enter;

444 445 446 447 448 449 450 451
  fprintf(stderr, "%s: FAIL! test timeout!\n", name);
  exit(1);
}
static char const no_alarm[] = " [--no-alarm]";
#else
static char const no_alarm[] = "";
#endif

452 453 454 455 456 457 458
static
RETSIGTYPE term(int n)
{
  enter;

  exit(1);
}
Martti Mela's avatar
Martti Mela committed
459 460
#else
static char const no_alarm[] = "";
461 462 463
#endif


Pekka Pessi's avatar
Pekka Pessi committed
464
void
465
usage(int exitcode)
Pekka Pessi's avatar
Pekka Pessi committed
466
{
467 468
  fprintf(stderr, "usage: %s [-6vs]%s\n", name, no_alarm);

469
  exit(exitcode);
Pekka Pessi's avatar
Pekka Pessi committed
470 471 472 473 474
}

/*
 * test su_wait functionality:
 *
475
 * Create a ponger, waking up do_recv() when data arrives,
Pekka Pessi's avatar
Pekka Pessi committed
476 477 478 479 480 481 482 483 484 485 486
 *                  then scheduling do_pong() by timer
 *
 * Create a pinger, executed from timer, scheduling do_ping(),
 *                  waking up do_rtt() when data arrives
 *
 * Create a timer, executing do_exit() after 10 seconds
 */
int main(int argc, char *argv[])
{
  su_root_t *root;
  su_clone_r ping = SU_CLONE_R_INIT, pong = SU_CLONE_R_INIT;
487
  su_msg_r start_msg = SU_MSG_R_INIT;
Pekka Pessi's avatar
Pekka Pessi committed
488
  su_timer_t *t;
489
#if HAVE_ALARM
490
  int opt_alarm = 1;
491
#endif
492

493 494
  struct pinger
    pinger = { PINGER, "ping", 1 },
Pekka Pessi's avatar
Pekka Pessi committed
495 496 497 498
    ponger = { PONGER, "pong", 1 };

  char *argv0 = argv[0];

499 500
  enter;

Pekka Pessi's avatar
Pekka Pessi committed
501
  while (argv[1]) {
502 503
    if (strcmp(argv[1], "-v") == 0) {
      opt_verbatim = 1;
Pekka Pessi's avatar
Pekka Pessi committed
504 505
      argv++;
    }
506 507 508
#if SU_HAVE_IN6
    else if (strcmp(argv[1], "-6") == 0) {
      opt_family = AF_INET6;
Pekka Pessi's avatar
Pekka Pessi committed
509 510
      argv++;
    }
511
#endif
Pekka Pessi's avatar
Pekka Pessi committed
512 513 514 515
    else if (strcmp(argv[1], "-s") == 0) {
      opt_singlethread = 1;
      argv++;
    }
516 517 518
#if HAVE_ALARM
    else if (strcmp(argv[1], "--no-alarm") == 0) {
      opt_alarm = 0;
519 520
      argv++;
    }
521
#endif
Pekka Pessi's avatar
Pekka Pessi committed
522
    else {
523
      usage(1);
Pekka Pessi's avatar
Pekka Pessi committed
524 525 526 527 528
    }
  }

  su_init(); atexit(su_deinit);

529 530
#if HAVE_SIGNAL
  signal(SIGTERM, term);
Pekka Pessi's avatar
Pekka Pessi committed
531

532 533 534 535 536 537 538
#if HAVE_ALARM
  if (opt_alarm) {
    alarm(60);
    signal(SIGALRM, sig_alarm);
  }
#endif

539 540 541 542
#endif

  time_test();

543 544
  root = su_root_create(NULL);
  if (!root) perror("su_root_create"), exit(1);
545 546 547

  fprintf(stdout, "test_su: testing %s port implementation\n",
	  su_root_name(root));
548

549
  su_root_threading(root, !opt_singlethread);
Pekka Pessi's avatar
Pekka Pessi committed
550 551 552 553

  if (su_clone_start(root, ping, &pinger, do_init, do_destroy) != 0)
    perror("su_clone_start"), exit(1);
  if (su_clone_start(root, pong, &ponger, do_init, do_destroy) != 0)
554
    perror("su_clone_start"), exit(1);
Pekka Pessi's avatar
Pekka Pessi committed
555

556 557
  /* Test timer, exiting after 200 milliseconds */
  t = su_timer_create(su_root_task(root), 200L);
Pekka Pessi's avatar
Pekka Pessi committed
558 559 560 561
  if (t == NULL)
    su_perror("su_timer_create"), exit(1);
  su_timer_set(t, (su_timer_f)do_exit, NULL);

562
  su_msg_create(start_msg, su_clone_task(ping), su_clone_task(pong),
Pekka Pessi's avatar
Pekka Pessi committed
563 564 565 566 567 568 569 570 571 572
		init_ping, 0);
  su_msg_send(start_msg);

  su_root_run(root);

  su_clone_wait(root, ping);
  su_clone_wait(root, pong);

  su_timer_destroy(t);

573
  if (pinger.rtt_n) {
574
    printf("%s executed %u pings in %g, mean rtt=%g sec\n", name,
575
	   pinger.rtt_n, pinger.rtt_total, pinger.rtt_total / pinger.rtt_n);
576
  }
Pekka Pessi's avatar
Pekka Pessi committed
577 578 579
  su_root_destroy(root);

  if (opt_verbatim)
580
    printf("%s exiting\n", argv0);
Pekka Pessi's avatar
Pekka Pessi committed
581 582 583

  return 0;
}