tport_type_sctp.c 6.49 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 25 26 27 28
/*
 * 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
 *
 */

/**@CFILE tport_type_sctp.c Transport using SCTP.
 *
 * See tport.docs for more detailed description of tport interface.
 *
29 30
 * @RFC4168.
 *
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 * @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
 * @date Original Created: Thu Jul 20 12:54:32 2000 ppessi
 */

#include "config.h"

#include "tport_internal.h"

#if HAVE_NETINET_SCTP_H
#include <netinet/sctp.h>
#undef HAVE_SCTP
#define HAVE_SCTP 1
#endif

#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
53
#include <string.h>
54 55 56 57

/* ---------------------------------------------------------------------- */
/* SCTP */

58 59 60
#undef MAX_STREAMS
#define MAX_STREAMS MAX_STREAMS

Pekka Pessi's avatar
Pekka Pessi committed
61 62 63 64 65 66
/* Missing socket symbols */
#ifndef SOL_SCTP
#define SOL_SCTP IPPROTO_SCTP
#endif


67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
enum { MAX_STREAMS = 1 };
typedef struct tport_sctp_t
{
  tport_t sctp_base[1];
  
  msg_t *sctp_recv[MAX_STREAMS];
  struct sctp_send {
    msg_t *ss_msg;
    msg_iovec_t *ss_unsent;	/**< Pointer to first unsent iovec */
    unsigned     ss_unsentlen;  /**< Number of unsent iovecs */
    msg_iovec_t *ss_iov;	/**< Iovecs allocated for sending */
    unsigned     ss_iovlen;	/**< Number of allocated iovecs */
  } sctp_send[MAX_STREAMS];
} tport_sctp_t;

82
#define TP_SCTP_MSG_MAX (65536)
83

84 85
#if HAVE_SCTP

86
static int tport_sctp_init_primary(tport_primary_t *, 
87
				   tp_name_t tpn[1], 
88 89 90
				   su_addrinfo_t *, tagi_t const *,
				   char const **return_culprit);
static int tport_sctp_init_client(tport_primary_t *, 
91
				  tp_name_t tpn[1], 
92 93
				  su_addrinfo_t *, tagi_t const *,
				  char const **return_culprit);
94 95 96 97 98
static int tport_sctp_init_secondary(tport_t *self, int socket, int accepted,
				     char const **return_reason);
static int tport_sctp_init_socket(tport_primary_t *pri, 
				  int socket,
				  char const **return_reason);
99
static int tport_recv_sctp(tport_t *self);
100 101
static ssize_t tport_send_sctp(tport_t const *self, msg_t *msg,
			       msg_iovec_t iov[], size_t iovused);
102

103
tport_vtable_t const tport_sctp_client_vtable =
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
{
  "sctp", tport_type_client,
  sizeof (tport_primary_t),
  tport_sctp_init_client,
  NULL,
  tport_accept,
  NULL,
  sizeof (tport_t),
  tport_sctp_init_secondary,
  NULL,
  NULL,
  NULL,
  NULL,
  tport_recv_sctp,
  tport_send_sctp,
};

#undef NEXT_VTABLE
#define NEXT_VTABLE &tport_sctp_client_vtable

124
tport_vtable_t const tport_sctp_vtable =
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
{
  "sctp", tport_type_local,
  sizeof (tport_primary_t),
  tport_sctp_init_primary,
  NULL,
  tport_accept,
  NULL,
  sizeof (tport_t),
  tport_sctp_init_secondary,
  NULL,
  NULL,
  NULL,
  NULL,
  tport_recv_sctp,
  tport_send_sctp,
};

#undef NEXT_VTABLE
#define NEXT_VTABLE &tport_sctp_vtable

static int tport_sctp_init_primary(tport_primary_t *pri, 
146
				   tp_name_t tpn[1],
147 148 149 150
				   su_addrinfo_t *ai,
				   tagi_t const *tags,
				   char const **return_culprit)
{
151 152
  int socket;

153 154 155
  if (pri->pri_params->tpp_mtu > TP_SCTP_MSG_MAX)
    pri->pri_params->tpp_mtu = TP_SCTP_MSG_MAX;

156 157
  socket = su_socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);

158
  if (socket == INVALID_SOCKET)
159 160 161 162 163 164
    return *return_culprit = "socket", -1;

  if (tport_sctp_init_socket(pri, socket, return_culprit) < 0)
    return -1;
  
  return tport_stream_init_primary(pri, socket, tpn, ai, tags, return_culprit);
165 166 167
}

static int tport_sctp_init_client(tport_primary_t *pri, 
168 169 170 171
				  tp_name_t tpn[1],
				  su_addrinfo_t *ai,
				  tagi_t const *tags,
				  char const **return_culprit)
172 173 174 175 176 177 178
{
  if (pri->pri_params->tpp_mtu > TP_SCTP_MSG_MAX)
    pri->pri_params->tpp_mtu = TP_SCTP_MSG_MAX;

  return tport_tcp_init_client(pri, tpn, ai, tags, return_culprit);
}

179 180
static int tport_sctp_init_secondary(tport_t *self, int socket, int accepted,
				     char const **return_reason)
181
{
182
  self->tp_has_connection = 1;
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  if (accepted) {
    /* Accepted socket inherit the init information from listen socket */
    return 0;
  }
  else {
    return tport_sctp_init_socket(self->tp_pri, socket, return_reason);
  }
}

/** Initialize a SCTP socket */
static int tport_sctp_init_socket(tport_primary_t *pri, 
				  int socket,
				  char const **return_reason)
{
  struct sctp_initmsg initmsg = { 0 };

  initmsg.sinit_num_ostreams = MAX_STREAMS;
  initmsg.sinit_max_instreams = MAX_STREAMS;

  if (setsockopt(socket, SOL_SCTP, SCTP_INITMSG, &initmsg, sizeof initmsg) < 0)
    return *return_reason = "SCTP_INITMSG", -1;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

  return 0;
}

/** 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_sctp(tport_t *self)
{
  msg_t *msg;
220
  ssize_t N, veclen;
221 222 223 224 225 226 227 228
  msg_iovec_t iovec[2] = {{ 0 }};

  char sctp_buf[TP_SCTP_MSG_MAX];

  iovec[0].mv_base = sctp_buf;
  iovec[0].mv_len = sizeof(sctp_buf);

  N = su_vrecv(self->tp_socket, iovec, 1, 0, NULL, NULL);
229
  if (N == SOCKET_ERROR) {
230
    return su_is_blocking(su_errno()) ? 1 : -1;
231
  }
232

233 234 235 236 237 238 239
  if (N == 0) {
    if (self->tp_msg)
      msg_recv_commit(self->tp_msg, 0, 1);
    return 0;    /* End of stream */
  }

  veclen = tport_recv_iovec(self, &self->tp_msg, iovec, N, 0);
240 241 242 243 244 245
  if (veclen < 0)
    return -1;

  assert(veclen == 1); assert(iovec[0].mv_len == N);
  msg = self->tp_msg;

246
  msg_set_address(msg, self->tp_addr, self->tp_addrlen);
247

248
  memcpy(iovec[0].mv_base, sctp_buf, iovec[0].mv_len);
249 250

  if (self->tp_master->mr_dump_file)
251
    tport_dump_iovec(self, msg, N, iovec, veclen, "recv", "from");
252

253
  msg_recv_commit(msg, N, 0);  /* Mark buffer as used */
254

255
  return 2;
256 257
}

258 259
static ssize_t tport_send_sctp(tport_t const *self, msg_t *msg,
			       msg_iovec_t iov[], size_t iovused)
260
{
261 262 263
  

  return su_vsend(self->tp_socket, iov, iovused, MSG_NOSIGNAL, NULL, 0);
264 265 266
}

#endif