tcpstream.c 9.84 KB
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 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 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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include <mailutils/sys/tcpstream.h>
#include <mailutils/error.h>

/* On solaris inet_addr() return -1.  */
#ifndef INADDR_NONE
# define INADDR_NONE (unsigned long)-1
#endif

static void
_tcp_cleanup (void *arg)
{
  struct _tcp_instance *tcp = arg;
  monitor_unlock (tcp->lock);
}

static int
_tcp_add_ref (stream_t stream)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  int status;
  monitor_lock (tcp->lock);
  status = ++tcp->ref;
  monitor_unlock (tcp->lock);
  return status;
}

static int
_tcp_destroy (stream_t stream)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  if (tcp->host)
    free (tcp->host);
  if (tcp->fd != -1)
    close (tcp->fd);
  monitor_destroy (tcp->lock);
  free (tcp);
  return 0;
}

static int
_tcp_release (stream_t stream)
{
  int status;
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  monitor_lock (tcp->lock);
  status = --tcp->ref;
  if (status <= 0)
    {
      monitor_unlock (tcp->lock);
      _tcp_destroy (stream);
      return 0;
    }
  monitor_unlock (tcp->lock);
  return status;
}

static int
_tcp_close0 (stream_t stream)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  if (tcp->fd != -1)
    close (tcp->fd);
  tcp->fd = -1;
  tcp->state = TCP_STATE_INIT;
  return 0;
}

static int
_tcp_close (stream_t stream)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;

  monitor_lock (tcp->lock);
  monitor_cleanup_push (_tcp_cleanup, tcp);
  _tcp_close0 (stream);
  monitor_unlock (tcp->lock);
  monitor_cleanup_pop (0);
  return 0;
}

static int
_tcp_open0 (stream_t stream, const char *host, int port, int flags)
{
  struct _tcp_instance 	*tcp = (struct _tcp_instance *)stream;
  int flgs, ret;
  size_t namelen;
  struct sockaddr_in peer_addr;
  struct hostent *phe;
  struct sockaddr_in soc_addr;

  if (tcp->state == TCP_STATE_INIT)
    {
      tcp->port = port;
      tcp->host = strdup (host);
      if (tcp->host == NULL)
	return MU_ERROR_NO_MEMORY;
      tcp->flags = flags;
    }

  switch (tcp->state)
    {
    case TCP_STATE_INIT:
      if (tcp->fd == -1)
	{
	  tcp->fd = socket (AF_INET, SOCK_STREAM, 0);
	  if (tcp->fd == -1)
	    return errno;
	}
      if (tcp->flags & MU_STREAM_NONBLOCK)
	{
	  flgs = fcntl (tcp->fd, F_GETFL);
	  flgs |= O_NONBLOCK;
	  fcntl (tcp->fd, F_SETFL, flgs);
	}
      tcp->state = TCP_STATE_RESOLVING;

    case TCP_STATE_RESOLVING:
      if (tcp->host == NULL || tcp->port == -1)
	return MU_ERROR_INVALID_PARAMETER;
      tcp->address = inet_addr (tcp->host);
      if (tcp->address == INADDR_NONE)
	{
	  phe = gethostbyname (tcp->host);
	  if (!phe)
	    {
	      _tcp_close0 (stream);
	      return MU_ERROR_INVALID_PARAMETER;
	    }
	  tcp->address = *(((unsigned long **)phe->h_addr_list)[0]);
	}
      tcp->state = TCP_STATE_RESOLVE;

    case TCP_STATE_RESOLVE:
      memset (&soc_addr, 0, sizeof (soc_addr));
      soc_addr.sin_family = AF_INET;
      soc_addr.sin_port = htons (tcp->port);
      soc_addr.sin_addr.s_addr = tcp->address;

      if ((connect (tcp->fd, (struct sockaddr *)&soc_addr, sizeof (soc_addr))) == -1)
	{
	  ret = errno;
	  if (ret == EINPROGRESS || ret == EAGAIN)
	    {
	      tcp->state = TCP_STATE_CONNECTING;
	      ret = MU_ERROR_TRY_AGAIN;
	    }
	  else
	    _tcp_close0 (stream);
	  return ret;
	}
      tcp->state = TCP_STATE_CONNECTING;

    case TCP_STATE_CONNECTING:
      namelen = sizeof (peer_addr);
      if (getpeername (tcp->fd, (struct sockaddr *)&peer_addr, &namelen) == 0)
	tcp->state = TCP_STATE_CONNECTED;
      else
	{
	  ret = errno;
	  _tcp_close0 (stream);
	  return ret;
	}
      break;
    }
  return 0;
}

static int
_tcp_open (stream_t stream, const char *host, int port, int flags)
{
  int status;
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  monitor_lock (tcp->lock);
  monitor_cleanup_push (_tcp_cleanup, tcp);
  status = _tcp_open0 (stream, host, port, flags);
  monitor_unlock (tcp->lock);
  monitor_cleanup_pop (0);
  return status;
}

static int
_tcp_get_fd (stream_t stream, int *fd)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;

  if (fd == NULL || tcp->fd == -1)
    return MU_ERROR_INVALID_PARAMETER;

  *fd = tcp->fd;
  return 0;
}

static int
_tcp_read (stream_t stream, void *buf, size_t buf_size, size_t *br)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  int bytes;

  if (br == NULL)
    return MU_ERROR_INVALID_PARAMETER;
  *br = 0;
  bytes = recv (tcp->fd, buf, buf_size, 0);
  if (bytes == -1)
    {
      *br = 0;
      return errno;
    }
  *br = bytes;
  return 0;
}

static int
_tcp_readline (stream_t stream, char *buf, size_t buf_size, size_t *br)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  int status = 0;
  size_t n;
  int nr = 0;
  char c;

  /* Grossly inefficient hopefully they override this */
  for (n = 1; n < buf_size; n++)
    {
      nr = recv (tcp->fd, &c, 1, 0);
      if (nr == -1) /* Error.  */
	{
	  status = errno;
	  break;
	}
      else if (nr == 1)
	{
	  *buf++ = c;
	  if (c == '\n') /* Newline is stored like fgets().  */
	    break;
	}
      else if (nr == 0)
	{
	  if (n == 1) /* EOF, no data read.  */
	    n = 0;
	  break; /* EOF, some data was read.  */
	}
    }
  *buf = '\0';
  if (br)
    *br = (n == buf_size) ? n - 1: n;
  return status;
}

static int
_tcp_write (stream_t stream, const void *buf, size_t buf_size, size_t *bw)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  int 	bytes;

  if (bw == NULL)
    return MU_ERROR_INVALID_PARAMETER;
  *bw = 0;
  bytes = send (tcp->fd, buf, buf_size, 0);
  if (bytes == -1)
    {
      *bw = 0;
      return errno;
    }
  *bw = bytes;
  return 0;
}

static int
_tcp_seek (stream_t stream, off_t off, enum stream_whence whence)
{
  (void)stream; (void)off; (void)whence;
  return MU_ERROR_NOT_SUPPORTED;
}

static int
_tcp_tell (stream_t stream, off_t *off)
{
  (void)stream; (void)off;
  return MU_ERROR_NOT_SUPPORTED;
}

static int
_tcp_get_size (stream_t stream, off_t *off)
{
  (void)stream; (void)off;
  return MU_ERROR_NOT_SUPPORTED;
}

static int
_tcp_truncate (stream_t stream, off_t off)
{
  (void)stream; (void)off;
  return MU_ERROR_NOT_SUPPORTED;
}

static int
_tcp_flush (stream_t stream)
{
  (void)stream;
  return 0;
}

static int
_tcp_get_flags (stream_t stream, int *flags)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  if (flags == NULL)
    return MU_ERROR_INVALID_PARAMETER;
  *flags = tcp->flags;
  return 0;
}

static int
_tcp_get_state (stream_t stream, enum stream_state *state)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  if (state == NULL)
    return MU_ERROR_INVALID_PARAMETER;
  *state = tcp->state;
  return 0;
}

static int
_tcp_is_readready (stream_t stream, int timeout)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  int ready;
  struct timeval tv;
  fd_set fds;

  FD_ZERO (&fds);
  FD_SET (tcp->fd, &fds);

  tv.tv_sec  = timeout / 100;
  tv.tv_usec = (timeout % 1000) * 1000;

  ready = select (tcp->fd + 1, &fds, NULL, NULL, (timeout == -1) ? NULL: &tv);
  return (ready == -1) ? 0 : 1;
}

static int
_tcp_is_writeready (stream_t stream, int timeout)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  int ready;
  struct timeval tv;
  fd_set fds;

  FD_ZERO (&fds);
  FD_SET (tcp->fd, &fds);

  tv.tv_sec  = timeout / 100;
  tv.tv_usec = (timeout % 1000) * 1000;

  ready = select (tcp->fd + 1, NULL, &fds, NULL, (timeout == -1) ? NULL: &tv);
  return (ready == -1) ? 0 : 1;
}

static int
_tcp_is_exceptionpending (stream_t stream, int timeout)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  int ready;
  struct timeval tv;
  fd_set fds;

  FD_ZERO (&fds);
  FD_SET  (tcp->fd, &fds);

  tv.tv_sec  = timeout / 100;
  tv.tv_usec = (timeout % 1000) * 1000;

  ready = select (tcp->fd + 1, NULL, NULL, &fds, (timeout == -1) ? NULL: &tv);
  return (ready == -1) ? 0 : 1;
}

static int
_tcp_is_open (stream_t stream)
{
  struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
  return tcp->fd >= 0;
}

static struct _stream_vtable _tcp_vtable =
{
  _tcp_add_ref,
  _tcp_release,
  _tcp_destroy,

  _tcp_open,
  _tcp_close,

  _tcp_read,
  _tcp_readline,
  _tcp_write,

  _tcp_seek,
  _tcp_tell,

  _tcp_get_size,
  _tcp_truncate,
  _tcp_flush,

  _tcp_get_fd,
  _tcp_get_flags,
  _tcp_get_state,

  _tcp_is_readready,
  _tcp_is_writeready,
  _tcp_is_exceptionpending,

  _tcp_is_open
};

int
stream_tcp_create (stream_t *pstream)
{
  struct _tcp_instance *tcp;

  if (pstream == NULL)
    return MU_ERROR_INVALID_PARAMETER;

  tcp = calloc (1, sizeof *tcp);
  if (tcp == NULL)
    return MU_ERROR_NO_MEMORY;

  tcp->base.vtable = &_tcp_vtable;
  tcp->ref = 1;
  tcp->fd = -1;
  tcp->host = NULL;
  tcp->port = -1;
  tcp->state = TCP_STATE_INIT;
  monitor_create (&(tcp->lock));
  *pstream = &tcp->base;
  return 0;
}