tport_type_tcp.c 6.2 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
/**@CFILE tport_type_tcp.c TCP Transport
26 27 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"

#include "tport_internal.h"

39 40 41 42 43
/* avoid Open C complaints */
#if !HAVE_OPEN_C
#include <netinet/tcp.h>
#endif

44 45 46 47 48 49 50 51 52 53
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif

#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>

54 55 56 57
#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
Kai Vehmanen's avatar
Kai Vehmanen committed
58
static char const __func__[] = "tport_type_tcp";
59 60
#endif

61 62 63
/* ---------------------------------------------------------------------- */
/* TCP */

64
tport_vtable_t const tport_tcp_vtable =
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
{
  "tcp", tport_type_local,
  sizeof (tport_primary_t),
  tport_tcp_init_primary,
  NULL,
  tport_accept,
  NULL,
  sizeof (tport_t),
  tport_tcp_init_secondary,
  NULL,
  NULL,
  NULL,
  NULL,
  tport_recv_stream,
  tport_send_stream,
};

82
tport_vtable_t const tport_tcp_client_vtable =
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
{
  "tcp", tport_type_client,
  sizeof (tport_primary_t),
  tport_tcp_init_client,
  NULL,
  tport_accept,
  NULL,
  sizeof (tport_t),
  tport_tcp_init_secondary,
  NULL,
  NULL,
  NULL,
  NULL,
  tport_recv_stream,
  tport_send_stream,
};

100 101
static int tport_tcp_setsndbuf(int socket, int atleast);

102
int tport_tcp_init_primary(tport_primary_t *pri, 
103
			   tp_name_t tpn[1],
104 105 106 107
			   su_addrinfo_t *ai,
			   tagi_t const *tags,
			   char const **return_culprit)
{
108
  int socket;
109

110
  socket = su_socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
111

112
  if (socket == INVALID_SOCKET)
113 114
    return *return_culprit = "socket", -1;

115 116
  tport_tcp_setsndbuf(socket, 64 * 1024);

117 118 119 120 121 122 123 124 125 126 127
  return tport_stream_init_primary(pri, socket, tpn, ai, tags, return_culprit);
}

int tport_stream_init_primary(tport_primary_t *pri, 
			      su_socket_t socket,
			      tp_name_t tpn[1],
			      su_addrinfo_t *ai,
			      tagi_t const *tags,
			      char const **return_culprit)
{
  pri->pri_primary->tp_socket = socket;
128

129 130 131
  /* Set IP TOS if set */
  tport_set_tos(socket, ai, pri->pri_params->tpp_tos);

132 133 134
#if defined(__linux__)
  /* Linux does not allow reusing TCP port while this one is open,
     so we can safely call su_setreuseaddr() before bind(). */
135
  su_setreuseaddr(socket, 1);
136 137
#endif

138
  if (tport_bind_socket(socket, ai, return_culprit) == -1)
139
    return -1;
140

141
  if (listen(socket, pri->pri_params->tpp_qsize) == SOCKET_ERROR)
142
    return *return_culprit = "listen", -1;
143 144 145 146 147 148 149

#if !defined(__linux__)
  /* Allow reusing TCP sockets
   *
   * On Solaris & BSD, call setreuseaddr() after bind in order to avoid
   * binding to a port owned by an existing server.
   */
150
  su_setreuseaddr(socket, 1);
151 152 153 154 155 156 157 158 159
#endif

  pri->pri_primary->tp_events = SU_WAIT_ACCEPT;
  pri->pri_primary->tp_conn_orient = 1;

  return 0;
}

int tport_tcp_init_client(tport_primary_t *pri, 
160
			  tp_name_t tpn[1],
161 162 163 164 165 166 167 168 169
			  su_addrinfo_t *ai,
			  tagi_t const *tags,
			  char const **return_culprit)
{
  pri->pri_primary->tp_conn_orient = 1;

  return 0;
}

170 171
int tport_tcp_init_secondary(tport_t *self, int socket, int accepted,
			     char const **return_reason)
172 173 174
{
  int one = 1;

175
  self->tp_has_connection = 1;
176 177

  if (setsockopt(socket, SOL_TCP, TCP_NODELAY, (void *)&one, sizeof one) == -1)
178
    return *return_reason = "TCP_NODELAY", -1;
179

180 181 182
  if (!accepted)
    tport_tcp_setsndbuf(socket, 64 * 1024);

183 184 185
  return 0;
}

186 187 188 189 190 191 192
static int tport_tcp_setsndbuf(int socket, int atleast)
{
#if SU_HAVE_WINSOCK2
  /* Set send buffer size to something reasonable on windows */
  int size = 0;
  socklen_t sizelen = sizeof size;

193
  if (getsockopt(socket, SOL_SOCKET, SO_SNDBUF, (void *)&size, &sizelen) < 0)
194 195 196 197 198 199 200 201
    return -1;

  if (sizelen != sizeof size)
    return su_seterrno(EINVAL);

  if (size >= atleast)
    return 0;			/* OK */

202 203
  return setsockopt(socket, SOL_SOCKET, SO_SNDBUF,
		    (void *)&atleast, sizeof atleast);
204 205 206 207 208
#else
  return 0;
#endif
}

209 210 211 212 213 214 215 216 217 218 219
/** Receive from stream.
 *
 * @retval -1 error
 * @retval 0  end-of-stream  
 * @retval 1  normal receive
 * @retval 2  incomplete recv, recv again
 * 
 */
int tport_recv_stream(tport_t *self)
{
  msg_t *msg;
220 221
  ssize_t n, N, veclen;
  int err;
222 223 224 225 226 227 228 229 230 231
  msg_iovec_t iovec[msg_n_fragments] = {{ 0 }};

  N = su_getmsgsize(self->tp_socket);
  if (N == 0) {
    if (self->tp_msg)
      msg_recv_commit(self->tp_msg, 0, 1);
    return 0;    /* End of stream */
  }
  if (N == -1) {
    err = su_errno();
232
    SU_DEBUG_1(("%s(%p): su_getmsgsize(): %s (%d)\n", __func__, (void *)self,
233 234 235 236 237
		su_strerror(err), err));
    return -1;
  }

  veclen = tport_recv_iovec(self, &self->tp_msg, iovec, N, 0);
238
  if (veclen == -1)
239 240 241 242
    return -1;

  msg = self->tp_msg;

243
  msg_set_address(msg, self->tp_addr, (socklen_t)(self->tp_addrlen));
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

  n = su_vrecv(self->tp_socket, iovec, veclen, 0, NULL, NULL);
  if (n == SOCKET_ERROR)
    return tport_recv_error_report(self);

  assert(n <= N);

  /* Write the received data to the message dump file */
  if (self->tp_master->mr_dump_file)
    tport_dump_iovec(self, msg, n, iovec, veclen, "recv", "from");

  /* Mark buffer as used */
  msg_recv_commit(msg, n, 0);

  return 1;
}

261 262 263
ssize_t tport_send_stream(tport_t const *self, msg_t *msg, 
			  msg_iovec_t iov[], 
			  size_t iovused)
264
{
265
#if __sun__			/* XXX - there must be a better way... */
266 267 268 269 270
  if (iovused > 16)
    iovused = 16;
#endif
  return su_vsend(self->tp_socket, iov, iovused, MSG_NOSIGNAL, NULL, 0);
}