tport_sigcomp.c 21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2006 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * 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
 *
 */

25 26 27
/**@CFILE tport_sigcomp.c Transport using SigComp.
 *
 * Incomplete.
28 29 30 31 32 33 34 35 36 37 38
 *
 * See tport.docs for more detailed description of tport interface.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Martti Mela <Martti.Mela@nokia.com>
 *
 * @date Created: Fri Mar 24 08:45:49 EET 2006 ppessi
 */

#include "config.h"

39
#include "tport.h"
40 41 42 43 44 45 46 47 48 49 50 51 52

#include <sofia-sip/string0.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>

#include <sigcomp.h>

/* ---------------------------------------------------------------------- */
/* SigComp */

53
typedef struct tport_sigcomp_handler tport_sigcomp_handler_t;
54

55
/** @internal Per end-point SigComp data */
56 57 58 59 60
struct tport_compressor {
  struct sigcomp_state_handler *msc_state_handler;
  struct sigcomp_compartment *msc_compartment;
};

61
/** @internal Per-socket SigComp data */
62
struct tport_comp {
63 64 65 66
  struct sigcomp_udvm          *sc_udvm;
  struct sigcomp_compartment   *sc_cc;
  struct sigcomp_compressor    *sc_compressor;
  struct sigcomp_buffer        *sc_output;
67
  unsigned                      sc_compressed;
68 69 70

  struct sigcomp_buffer        *sc_input;
  unsigned                      sc_copied;
71

72 73 74 75 76 77 78 79
  enum {
    format_is_unknown,
    format_is_sigcomp,
    format_is_noncomp
  } sc_infmt, sc_outfmt;
};


80 81
tport_compressor_t *vsc_master_init_sigcomp(tport_t *mr,
					     char const *algorithm)
82
{
83 84 85
  tport_compressor_t *retval =  NLL;
  stuct sigcomp_state_handler *sh = NULL;
  struct sigcomp_algorithm *a = NULL;
86
  struct sigcomp_compartment *cc = NULL;
87

88
  if (algorithm == NULL)
89 90 91 92 93
    algorithm = getenv("SIGCOMP_ALGORITHM");

  retval = su_zalloc((su_home_t *)mr, sizeof *retval);
  if (retval == NULL)
    return retval;
94

95
  sh = sigcomp_state_handler_create();
96

97
  if (sh == NULL) {
98
    SU_DEBUG_1(("tport: initializing SigComp state handler: %s\n",
99 100 101 102 103 104 105 106 107 108
		strerror(errno)));
    vsc_master_deinit_sigcomp(mr, retval), retval = NULL;
    return retval;
  }
  retval->msc_state_handler = sh;

  a = sigcomp_algorithm_by_name(algorithm);
  cc = sigcomp_compartment_create(a, sh, 0, "", 0, NULL, 0);

  if (cc == NULL) {
109
    SU_DEBUG_1(("tport: initializing SigComp master compartment: %s\n",
110 111 112 113 114 115 116 117 118
		strerror(errno)));
    vsc_master_deinit_sigcomp(mr, retval), retval = NULL;
    return retval;
  }

  self->msc_compartment = cc;

  return retval;
}
119

120 121 122 123 124
  if (self->sa_compartment) {
    agent_sigcomp_options(self, self->sa_compartment);
    sigcomp_compartment_option(self->sa_compartment, "stateless");
  }
  else
125 126 127 128

  if (mr->mr_compartment)
    sigcomp_compartment_unref(mt->mr_compartment);
  mr->mr_compartment = sigcomp_compartment_ref(cc);
129

130 131 132 133 134 135 136 137 138 139 140
  return cc;
}


void vsc_master_deinit_sigcomp(tport_master_t *mr)
{
  tport_sigcomp_vtable_t const *vsc = tport_sigcomp_vtable;

  if (mr->mr_compartment)
    sigcomp_compartment_unref(mr->mr_compartment), mr->mr_compartment = NULL;

141 142
}

143 144 145 146 147 148
char const *vsc_comp_name(tport_sigcomp_t const *master_sc,
			  char const *proposed_name,
			  tagi_t const *tags)
{
  if (master_sc == NULL ||
      master_sc->sc_cc == NULL ||
149
      compression == NULL ||
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      strcasecmp(compression, tport_sigcomp_name))
    return NULL;

  return tport_sigcomp_name;
}

/** Check if transport can receive compressed messages */
int vsc_can_recv_sigcomp(tport_sigcomp_t const *sc)
{
  return sc && sc->sc_infmt != format_is_noncomp;
}

/** Check if transport can send compressed messages */
int vsc_can_send_sigcomp(tport_sigcomp_t const *sc)
{
  return sc && sc->sc_outfmt != format_is_noncomp;
}

/** Set/reset compression */
169
int vsc_set_compression(tport_t *self,
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
			tport_sigcomp_t *sc,
			char const *comp)
{
  if (self == NULL)
    ;
  else if (comp == NULL) {
    if (sc == NULL || sc->sc_outfmt != format_is_sigcomp) {
      self->tp_name->tpn_comp = NULL;
      return 0;
    }
  }
  else {
    comp = tport_canonize_comp(comp);

    if (comp && sc && sc->sc_outfmt != format_is_noncomp) {
      self->tp_name->tpn_comp = comp;
      return 0;
    }
  }
189

190 191 192 193 194 195 196
  return -1;
}


/** Assign a SigComp compartment (to a possibly connected tport). */
int tport_sigcomp_assign(tport_t *self, struct sigcomp_compartment *cc)
{
197
  if (tport_is_connection_oriented(self) &&
198
      tport_is_secondary(self) &&
199
      self->tp_socket != INVALID_SOCKET) {
200 201 202 203 204 205 206 207

    if (self->tp_sigcomp->sc_cc) {
      if (cc == self->tp_sigcomp->sc_cc)
	return 0;

      /* Remove old assignment */
      sigcomp_compartment_unref(self->tp_sigcomp->sc_cc);
    }
208

209 210 211 212 213 214 215 216 217 218
    self->tp_sigcomp->sc_cc = sigcomp_compartment_ref(cc);

    return 0;
  }

  return su_seterrno(EINVAL);
}


void vsc_sigcomp_shutdown(tport_t *self, int how)
219 220 221 222 223 224 225
{
  if (self->tp_socket != -1)
    shutdown(self->tp_socket, how - 1);

  if (how >= 2 && self->tp_sigcomp->sc_cc) {
    sigcomp_compartment_unref(self->tp_sigcomp->sc_cc);
    self->tp_sigcomp->sc_cc = NULL;
226
  }
227 228
}

229 230
static int vsc_recv_sigcomp_r(tport_t*, msg_t**, struct sigcomp_udvm*, int);
static struct sigcomp_compartment *vsc_primary_compartment(tport_master_t *);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339


static int tport_sigcomp_init_secondary()
{
  if (accept) {
    if (!pri->pri_primary->tp_name->tpn_comp)
      self->tp_sigcomp->sc_infmt = format_is_noncomp;
  }
  else {
    /* XXX - no tpn here */
    if (tpn->tpn_comp == pri->pri_primary->tp_name->tpn_comp)
      self->tp_name->tpn_comp = pri->pri_primary->tp_name->tpn_comp;
    if (!pri->pri_primary->tp_name->tpn_comp)
      self->tp_sigcomp->sc_infmt = format_is_noncomp;
  }
}

static int tport_sigcomp_deinit_secondary(tport_t *self)
{
  if (self->tp_sigcomp) {
    tport_sigcomp_t *sc = self->tp_sigcomp;

    if (sc->sc_udvm)
      sigcomp_udvm_free(sc->sc_udvm), sc->sc_udvm = NULL;
    if (sc->sc_compressor)
      sigcomp_compressor_free(sc->sc_compressor), sc->sc_compressor = NULL;
  }
}

#if 0
#if HAVE_SIGCOMP
  if (tport_can_recv_sigcomp(self))
    return tport_recv_sigcomp_stream(self);
#endif
#endif

/** Try to receive stream data using SigComp. */
static int tport_recv_sigcomp_stream(tport_t *self)
{
  struct sigcomp_udvm *udvm;
  int retval;

  SU_DEBUG_7(("%s(%p)\n", __func__, self));

  /* Peek for first byte in stream,
     determine if this is a compressed stream or not */
  if (self->tp_sigcomp->sc_infmt == format_is_unknown) {
    unsigned char sample;
    int n;

    n = recv(self->tp_socket, &sample, 1, MSG_PEEK);
    if (n < 0)
      return n;

    if (n == 0) {
      recv(self->tp_socket, &sample, 1, 0);
      return 0;			/* E-o-S from first message */
    }

    if ((sample & 0xf8) != 0xf8) {
      /* Not SigComp, receive as usual */
      if (tport_is_primary(self)) {
	SU_DEBUG_1(("%s(%p): receive semantics not implemented\n",
		    __func__, self));
	su_seterrno(EINVAL);		/* Internal error */
	return -1;
      }

      /* Do not try to receive with sigcomp from this socket */
      self->tp_sigcomp->sc_infmt = format_is_noncomp;

      return tport_recv_stream(self);
    }

    /* SigComp, receive using UDVM */
    self->tp_sigcomp->sc_infmt = format_is_sigcomp;

    /* Initialize UDVM */
    self->tp_sigcomp->sc_udvm = tport_init_udvm(self);
    if (!self->tp_sigcomp->sc_udvm) {
      int save = su_errno();
      recv(self->tp_socket, &sample, 1, 0); /* remove msg from socket */
      return su_seterrno(save);
    }
  }

  udvm = self->tp_sigcomp->sc_udvm; assert(udvm);

  retval = tport_recv_sigcomp_r(self, &self->tp_msg, udvm, N);

  if (retval < 0)
    sigcomp_udvm_reject(udvm);

  return retval;
}

/** Receive data available on the socket.
 *
 * @retval -1 error
 * @retval 0  end-of-stream
 * @retval 1  normal receive
 * @retval 2  incomplete recv, recv again
 */
static int tport_recv_sigcomp_r(tport_t *self,
				msg_t **mmsg,
				struct sigcomp_udvm *udvm,
				int N)
{
  msg_t *msg;
340 341 342
  size_t n, m, i;
  int eos, complete;
  ssize_t veclen;
343 344 345 346 347
  msg_iovec_t iovec[msg_n_fragments] = {{ 0 }};
  su_sockaddr_t su[1];
  socklen_t su_size = sizeof(su);
  struct sigcomp_buffer *input, *output;
  void *data;
348
  size_t dlen;
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

  SU_DEBUG_7(("%s(%p)\n", __func__, self));

  assert(udvm);

  if (sigcomp_udvm_has_input(udvm)) {
    input = sigcomp_udvm_input_buffer(udvm, n = N = 0); assert(input);
  }
  else {
    if (N == 0) {
      assert(self->tp_addrinfo->ai_socktype != SOCK_DGRAM);
      if (self->tp_addrinfo->ai_socktype == SOCK_DGRAM) {
	recv(self->tp_socket, (void *)su, 1, 0);
	return 1;
      }
    }

    input = sigcomp_udvm_input_buffer(udvm, N); assert(input);
    if (input == NULL)
      return su_seterrno(EIO);

    data = input->b_data + input->b_avail;
    dlen = input->b_size - input->b_avail;
372

373 374
    if (tport_is_stream(self)) {
      n = recv(self->tp_socket, data, dlen, 0);
375
    }
376 377 378
    else if (dlen >= N) {
      n = recvfrom(self->tp_socket, data, dlen, 0, &su->su_sa, &su_size);
      SU_CANONIZE_SOCKADDR(su);
379
    }
380 381 382 383 384 385
    else {
      recvfrom(self->tp_socket, data, dlen, 0, &su->su_sa, &su_size);
      SU_CANONIZE_SOCKADDR(su);
      su_seterrno(EMSGSIZE);		/* Protocol error */
      return -1;
    }
386

387 388 389
    if (n == (unsigned)-1) {
      char const *pn = self->tp_protoname;
      int err = su_errno();
390

391
      if (su_is_blocking(err)) {
392 393 394
	SU_DEBUG_7(("%s(%p): recv from %s: EAGAIN\n", __func__, self, pn));
	return 1;
      }
395

396 397 398 399
      SU_DEBUG_1(("%s(%p): recv from %s: %s (%d)\n", __func__, self, pn,
		  su_strerror(err), err));
      return -1;
    }
400

401
    /* XXX - in case of stream, use message buffers for output? */
402

403 404 405 406 407 408
    input->b_avail += n;
    input->b_complete = (n == 0) || !tport_is_stream(self);
  }

  for (complete = eos = 0; !complete;) {
    output = sigcomp_udvm_output_buffer(udvm, 16384);
409

410 411
    if (sigcomp_udvm_decompress(udvm, output, input) < 0) {
      int error = sigcomp_udvm_errno(udvm);
412

413 414
      SU_DEBUG_3(("%s: UDVM error %d: %s\n", __func__,
		  error, sigcomp_udvm_strerror(udvm)));
415

416 417 418 419 420 421 422 423 424 425 426
      su_seterrno(EREMOTEIO);

      return -1;
    }

    data = output->b_data + output->b_used;
    dlen = output->b_avail - output->b_used;
    complete = output->b_complete;
    eos = complete && input->b_complete;

    veclen = tport_recv_iovec(self, mmsg, iovec, dlen, eos);
427

428 429 430 431 432 433 434 435 436 437 438 439
    if (dlen ? veclen <= 0 : veclen < 0) {
      return -1;
    }

    for (i = 0, n = 0; i < veclen; i++) {
      m = iovec[i].mv_len; assert(dlen >= n + m);
      memcpy(iovec[i].mv_base, data + n, m);
      n += m;
    }
    assert(dlen == n);

    msg = *mmsg;
440

441 442
    if (msg) {
      /* Message address */
443 444 445 446
      if (self->tp_addrinfo->ai_socktype == SOCK_STREAM)
	msg_set_address(msg, self->tp_addr, self->tp_addrlen);
      else
	msg_set_address(msg, su, su_size);
447 448

      SU_DEBUG_5(("%s(%p): sigcomp recv = %u => %u %s\n", __func__, self,
449 450 451
		  N, dlen, eos ? " (complete)" : ""));

      msg_mark_as_compressed(msg);
452

453 454 455
      /* Write the received data to the message dump file */
      if (self->tp_master->mr_dump_file && !self->tp_pri->pri_threadpool)
	tport_dump_iovec(self, msg, n, iovec, veclen, "recv", "from");
456

457 458 459
      msg_recv_commit(msg, dlen, eos);    /* Mark buffer as used */
    }
    else {
460
      SU_DEBUG_5(("%s(%p): sigcomp recv = %u => %u %s\n", __func__, self,
461 462 463 464 465 466 467 468 469
		  N, dlen, eos ? " (complete)" : ""));
      if (complete || !tport_is_stream(self)) {
	sigcomp_udvm_reject(udvm); /* Reject empty message */
      }
    }

    if (self->tp_addrinfo->ai_socktype == SOCK_STREAM) {
      if (eos)
	return 0;
470

471 472 473 474 475 476 477 478 479 480 481 482
      if (output->b_complete)
	return n < N || sigcomp_udvm_has_pending_data(udvm) ? 2 : 1;

      if (!sigcomp_udvm_has_input(udvm))
	return 1;
    }
  }

  return eos ? 0 : 2;
}

static
483
int vsc_send_sigcomp(tport_t const *self,
484 485
		     msg_t *msg,
		     msg_iovec_t iov[],
486 487 488
		     int iovused,
		     struct sigcomp_compartment *cc,
		     tport_sigcomp_t *sc)
489 490 491 492 493 494 495 496 497 498 499 500 501 502
{
  struct sigcomp_compressor *c = sc->sc_compressor;
  struct sigcomp_buffer *input = sc->sc_input;
  struct sigcomp_buffer *output = sc->sc_output;
  msg_iovec_t ciov[1];

  int i, n, m, k, stream = tport_is_stream(self);
  char const *ccname;
  int ccnamelen;

  su_addrinfo_t *ai = msg_addrinfo(msg);

  int compress = (cc || (cc = sc->sc_cc)) && ai->ai_flags & TP_AI_COMPRESSED;

503
  if (!compress) {
504 505 506
    if (stream)
      sc->sc_outfmt = format_is_noncomp;
    ai->ai_flags &= ~TP_AI_COMPRESSED;
507
    return self->tp_pri->pri_vtable->vtp_send(self, msg, iov, iovused, NULL);
508 509
  }

510 511 512
  if (stream)
    sc->sc_outfmt = format_is_sigcomp;

513
  assert(cc);
514

515 516
  if (c == NULL) {
    assert(input == NULL);
517
    if (stream)
518 519 520 521
      c = sigcomp_compressor_create_for_stream(cc);
    else
      c = sigcomp_compressor_create(cc);
    sc->sc_compressor = c;
522
  }
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

  ccname = sigcomp_compartment_name(cc, &ccnamelen);

  if (sc->sc_compressed != 0) {
    input = NONE;
  }
  else if (input == NULL) {
    int input_size = -1;

    if (tport_is_udp(self)) {
      input_size = 0;

      for (i = 0; i < iovused; i++)
	input_size += iov[i].siv_len;
    }

    sc->sc_input = input = sigcomp_compressor_input_buffer(c, input_size);

    assert(input->b_avail == 0 && input->b_used == 0);
542
  }
543 544 545 546 547 548 549 550 551 552 553 554
  else if (!input->b_complete) {
    int input_size = 0;

    for (i = 0; i < iovused; i++)
      input_size += iov[i].siv_len;

    if (input_size > input->b_size - input->b_avail)
      sigcomp_buffer_align_available(input, 0);
  }

  if (output == NULL)
    sc->sc_output = output = sigcomp_compressor_output_buffer(c, NULL);
555

556
  if (!c || !input || !output) {
557
    SU_DEBUG_3(("%s(%p): %s (%u)%s%s%s\n",
558 559 560 561 562
		__func__, self, strerror(errno), errno,
		c ? "" : " (comp)",
		input ? "" : " (input)",
		output ? "" : " (output)"));
    sigcomp_compressor_free(c);
563
    sc->sc_compressor = NULL;
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    sc->sc_output = NULL; sc->sc_input = NULL;
    sc->sc_compressed = 0; sc->sc_copied = 0;
    return -1;
  }

  if (sc->sc_compressed == 0) {
    k = sc->sc_copied;

    if (!input->b_complete) {
      int m = sc->sc_copied;

      for (i = 0, n = 0; i < iovused; i++) {
	char *b = iov[i].siv_base;
	int l = iov[i].siv_len;

	if (m >= l) {
	  m -= l;
	  continue;
	}

	b += m; l -= m;

	if (input->b_size == input->b_avail)
	  break;

	if (l > input->b_size - input->b_avail)
	  l = input->b_size - input->b_avail;

	memcpy(input->b_data + input->b_avail, b, l);
	input->b_avail += l; n += l; sc->sc_copied += l;

	if (l != iov[i].siv_len)
	  break;
      }
      input->b_complete = i == iovused;
      assert(stream || input->b_complete); (void)stream;
    }
601

602 603 604
    m = output->b_avail - output->b_used;

    n = sigcomp_compressor_compress(c, output, input);
605

606 607
    if (n < 0) {
      SU_DEBUG_3(("%s(%p): %s (%u)\n", __func__, self,
608
		  sigcomp_compressor_strerror(c),
609 610
		  sigcomp_compressor_errno(c)));
      sigcomp_compressor_free(c);
611
      sc->sc_compressor = NULL;
612 613 614 615 616 617 618
      sc->sc_output = NULL; sc->sc_input = NULL;
      sc->sc_compressed = 0;
      return -1;
    }

    assert(input->b_complete || sc->sc_copied - k > 0);

619 620 621
    SU_DEBUG_5(("%s: input %u (%u new) compressed %u to %u with '%.*s'\n",
		__func__, sc->sc_copied, k, n,
		(output->b_avail - output->b_used) - m,
622 623 624 625 626
		ccnamelen, ccname));

    sc->sc_compressed = n;

    assert(stream || output->b_complete);
627
  }
628 629 630 631 632 633 634 635 636 637 638
  else {
    assert(tport_is_connection_oriented(self));
    n = sc->sc_compressed;
  }

  assert(input && cc && c && output);

  ciov->siv_base = output->b_data + output->b_used;
  ciov->siv_len = output->b_avail - output->b_used;

  m = self->tp_pri->pri_vtable->vtp_send(self, msg, ciov, 1);
639

640 641 642
  if (m == -1) {
    int error = su_errno();

643
    if (su_is_blocking(error)) {
644 645 646 647 648 649 650 651 652
      sigcomp_compressor_free(c);
      sc->sc_compressor = NULL;
      sc->sc_output = NULL; sc->sc_input = NULL;
      sc->sc_compressed = 0; sc->sc_copied = 0;
      su_seterrno(error);
    }

    return -1;
  }
653

654
  output->b_used += m;
655

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
  if (output->b_used < output->b_avail)
    return 0;

  if (output->b_complete) {
    sigcomp_compressor_accept(c, cc), sc->sc_output = output = NULL;
  }

  if (input != NONE && input->b_avail == input->b_used && input->b_complete)
    sigcomp_buffer_reset(input, -1), sc->sc_input = input = NULL;

  if (!input && !output) {
    sigcomp_compressor_free(c);
    sc->sc_compressor = NULL;
  }

671
  assert(sc->sc_compressed >= n); assert(sc->sc_copied >= n);
672 673 674 675 676 677 678 679

  sc->sc_compressed -= n;
  sc->sc_copied -= n;

  return n;
}

/** Initialize UDVM */
680
static
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
struct sigcomp_udvm *tport_init_udvm(tport_t *self)
{
  struct sigcomp_compartment *cc;
  struct sigcomp_udvm *udvm;

  if (self->tp_sigcomp->sc_udvm)
    return self->tp_sigcomp->sc_udvm;

  cc = tport_primary_compartment(self->tp_master);

  if (!cc)
    return NULL;

  if (self->tp_addrinfo->ai_socktype == SOCK_STREAM)
    udvm = sigcomp_udvm_create_for_stream(cc);
  else
    udvm = sigcomp_udvm_create_for_compartment(cc);

  return udvm;
}


/** Get primary compartment */
704
static
705 706 707 708 709 710 711
struct sigcomp_compartment *
tport_primary_compartment(tport_master_t *mr)
{
  return mr->mr_compartment;
}

/** Test if tport has a SigComp compartment is assigned to it. */
712
int vsc_has_sigcomp_assigned(tport_sigcomp_t const *sc)
713
{
714
  return sc && sc->sc_udvm != NULL;
715 716
}

717 718
static
void vsc_try_accept_sigcomp(tport_t *self, msg_t *msg)
719 720 721 722 723
{
  struct sigcomp_udvm *udvm;

  udvm = self->tp_sigcomp->sc_udvm;
  if (udvm && sigcomp_udvm_is_complete(udvm)) {
724
    if (self->tp_master->mr_tpac->tpac_sigcomp_accept &&
725 726 727 728 729 730
	self->tp_sigcomp->sc_cc == NULL) {
      tport_t *ref;
      struct tport_delivery *d;

      d = self->tp_master->mr_delivery;

731
      d->d_tport = self;
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
      d->d_msg = msg;
      d->d_udvm = &self->tp_sigcomp->sc_udvm;
      *d->d_from = *self->tp_name;

      ref = tport_incref(self);
      STACK_SIGCOMP_ACCEPT(self, msg);
      /* Reject by default */
      if (self->tp_sigcomp->sc_udvm)
	sigcomp_udvm_accept(self->tp_sigcomp->sc_udvm, NULL);
      tport_decref(&ref);

      d->d_msg = NULL;
    }
    else {
      if (tport_log->log_level >= 5) {
747
	char const *name;
748
	int namelen;
749

750
	name = sigcomp_compartment_name(self->tp_sigcomp->sc_cc, &namelen);
751
	SU_DEBUG_5(("tport(%p): msg %p SigComp implicit accept '%.*s'\n",
752 753 754 755 756 757 758 759 760
		    self, msg, namelen, name));
      }
      sigcomp_udvm_accept(udvm, self->tp_sigcomp->sc_cc);
    }
  }
}


/** Accept a SigComp message */
761 762 763
int
tport_sigcomp_accept(tport_t *self,
		     struct sigcomp_compartment *cc,
764 765 766 767 768 769 770 771 772 773 774 775 776 777
		     msg_t *msg)
{
  struct sigcomp_udvm *udvm;

  if (self == NULL || msg != self->tp_master->mr_delivery->d_msg)
    return su_seterrno(EINVAL);

  if (!self->tp_master->mr_delivery->d_udvm || cc == NONE)
    return 0;

  udvm = *self->tp_master->mr_delivery->d_udvm;

  if (udvm) {
    if (tport_log->log_level >= 5) {
778
      char const *name;
779
      int namelen;
780

781 782
      if (cc) {
	name = sigcomp_compartment_name(cc, &namelen);
783
	SU_DEBUG_5(("tport(%p): msg %p SigComp accept '%.*s'\n",
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
		    self, msg, namelen, name));
      }
      else {
	SU_DEBUG_5(("tport(%p): msg %p SigComp reject\n", self, msg));
      }
    }
    sigcomp_udvm_accept(udvm, cc);
  }

  self->tp_master->mr_delivery->d_udvm = NULL;

  return 0;
}


/** Pass message to the protocol stack */
void
tport_sigcomp_deliver(tport_t *self, msg_t *msg, su_time_t now)
{
  /* XXX - no d */

  STACK_RECV(self, msg, now);

  if (d->d_udvm && *d->d_udvm)
    sigcomp_udvm_accept(*d->d_udvm, NULL); /* reject */
}


#if HAVE_SIGCOMP && 0

814
su_inline
815 816
int msg_is_compressed(msg_t *msg)
{
817
  return msg &&
818 819 820
    (msg_addrinfo(msg)->ai_flags & TP_AI_COMPRESSED) == TP_AI_COMPRESSED;
}

821
su_inline
822 823 824 825 826 827 828
void msg_mark_as_compressed(msg_t *msg)
{
  if (msg)
    msg_addrinfo(msg)->ai_flags |= TP_AI_COMPRESSED;
}


829
struct sigcomp_udvm **tport_get_udvm_slot(tport_t *self)
830
{
831
  tport_sigcomp_vtable_t const *vsc = tport_sigcomp_vtable;
832

833
  if (vsc)
834

835 836 837 838 839
#if HAVE_SIGCOMP
  return &self->tp_sigcomp->sc_udvm;
#else
  return NULL;
#endif
840 841
}

842 843 844
struct sigcomp_compartment *
tport_sigcomp_assign_if_needed(tport_t *self,
			       struct sigcomp_compartment *cc)
845
{
846 847 848 849 850 851 852 853
  if (self->tp_name->tpn_comp) {
    if (cc)
      tport_sigcomp_assign(self, cc);
    else if (self->tp_sigcomp->sc_cc)
      cc = self->tp_sigcomp->sc_cc;
    else
      /* Use default compartment */
      cc = self->tp_master->mr_compartment;
854
  }
855
  else
856
    cc = NULL;
857

858
  return cc;
859
}
860

861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
/** Receive data from datagram using SigComp. */
int tport_recv_sigcomp_dgram(tport_t *self, int N)
{
  char dummy[1];
  int error = EBADMSG;
#if HAVE_SIGCOMP
  struct sigcomp_udvm *udvm;

  if (self->tp_sigcomp->sc_udvm == 0)
    self->tp_sigcomp->sc_udvm = tport_init_udvm(self);

  udvm = self->tp_sigcomp->sc_udvm;

  if (udvm) {
    retval = tport_recv_sigcomp_r(self, &self->tp_msg, udvm, N);
    if (retval < 0)
      sigcomp_udvm_reject(udvm);
    return retval;
879
  }
880
  error = su_errno();
881
#endif
882 883
  recv(self->tp_socket, dummy, 1, 0); /* remove msg from socket */
  /* XXX - send NACK ? */
884
  return su_seterrno(error);
885
}