isc_tcp.c 7.63 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1 2 3
/*
** isc_tcp.c                          Routines to handle TCP ISC sessions
**
Per Cederqvist's avatar
Per Cederqvist committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
** Copyright (C) 1992, 1998-1999 by Peter Eriksson and
** Per Cederqvist of the Lysator Academic Computer Association.
**
**
** This library 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 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
** Library General Public License for more details.
**
** You should have received a copy of the GNU Library General Public
** License along with this library; if not, write to the Free
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Per Cederqvist's avatar
Per Cederqvist committed
21 22 23
**
** history:
** 920209 pen      code extracted from isc_session.c
Per Cederqvist's avatar
Per Cederqvist committed
24
** (See ChangeLog for recent history)
Per Cederqvist's avatar
Per Cederqvist committed
25 26 27
*/

#include <errno.h>
28 29 30
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
31 32 33
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif
34 35 36
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
37 38 39 40 41 42
#include <ctype.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/file.h>
#include <sys/ioctl.h>
43
#include <sys/socket.h>
Per Cederqvist's avatar
Per Cederqvist committed
44
#include <netdb.h>
45 46 47
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
48
#include <fcntl.h>
49 50 51
#ifndef NULL
#  include <stdio.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
52 53 54
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
55
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
56 57 58

#include "isc.h"
#include "intern.h"
Per Cederqvist's avatar
Per Cederqvist committed
59
#include "unused.h"
Per Cederqvist's avatar
Per Cederqvist committed
60 61


Per Cederqvist's avatar
Per Cederqvist committed
62 63
static IscSession *
isc_tcp_accept_fn(IscHandlerList *UNUSED(hl),
Per Cederqvist's avatar
Per Cederqvist committed
64
		  IscSession *scb,
Per Cederqvist's avatar
Per Cederqvist committed
65
		  IscMessage *UNUSED(msg))
Per Cederqvist's avatar
Per Cederqvist committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
{
  IscSession *new_scb;
  int fd;
  

  if ((fd = accept(scb->fd,
		   (struct sockaddr *) NULL,
		   (int *) NULL)) < 0)
    return NULL;
  
  new_scb = isc_createtcp(scb->cfg, fd);
  if (!new_scb)
    return NULL;
  
  /* Fill in the session info structure */
  new_scb->state = ISC_STATE_RUNNING;
  new_scb->mode  = O_RDWR;
83
  /* FIXME: isc_getraddress may return NULL.  Should we fail then?  */
Per Cederqvist's avatar
Per Cederqvist committed
84 85 86 87 88 89 90 91 92 93
  new_scb->info.tcp.raddr = isc_getraddress(new_scb);
  new_scb->info.tcp.laddr = isc_getladdress(new_scb);
  
  return new_scb;
}





Per Cederqvist's avatar
Per Cederqvist committed
94 95
static void
isc_tcp_destroy_fn(IscHandlerList *UNUSED(hl),
Per Cederqvist's avatar
Per Cederqvist committed
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
		   IscSession *scb)
{
  if (scb->info.tcp.raddr)
  {
    isc_freeaddress(scb->info.tcp.raddr);
    scb->info.tcp.raddr = NULL;
  }
  
  if (scb->info.tcp.laddr)
  {
    isc_freeaddress(scb->info.tcp.laddr);
    scb->info.tcp.laddr = NULL;
  }
}



/*
** TCP I/O functions structure
*/
static IscHandler isc_tcp_funs =
{
  &isc_default_read_fn,
  &isc_default_write_fn,
  &isc_default_close_fn,
  NULL,
  &isc_tcp_accept_fn,
  &isc_tcp_destroy_fn,
  NULL
};



/*
** Create a TCP Session Address
*/
Per Cederqvist's avatar
Per Cederqvist committed
132
static IscAddress *
Per Cederqvist's avatar
Per Cederqvist committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
isc_mktcpaddress(const char *address,
		 const char *service)
{
  struct sockaddr_in addr;
  struct hostent *hp;
  struct servent *sp;


  memset(&addr, 0, sizeof(addr));
  
  /* Any local address? */
  if (address == NULL)
  {
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
  }
Per Cederqvist's avatar
Per Cederqvist committed
149
  else if (isdigit((int)(unsigned char)address[0]))
Per Cederqvist's avatar
Per Cederqvist committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163
  {
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(address);
    if (addr.sin_addr.s_addr == -1)
      return NULL;
  }
  else if ((hp = gethostbyname(address)) == NULL)
    return NULL;
  else
  {
    addr.sin_family = hp->h_addrtype;
    memcpy(&addr.sin_addr, hp->h_addr, sizeof(addr.sin_addr));
  }
  
Per Cederqvist's avatar
Per Cederqvist committed
164
  if (isdigit((int)(unsigned char)service[0]))
Per Cederqvist's avatar
Per Cederqvist committed
165 166 167 168 169 170 171 172 173 174 175 176
    addr.sin_port = htons(atoi(service));
  else if ((sp = getservbyname(service, "tcp")) == NULL)
    return NULL;
  else
    addr.sin_port = sp->s_port;

  return isc_mkipaddress((struct sockaddr *)&addr);
}


		  
/*
177 178
** Create a TCP session.
** Will close fd and return NULL if an error occurs.
Per Cederqvist's avatar
Per Cederqvist committed
179 180 181 182 183 184
*/
IscSession *
isc_createtcp(IscSessionConfig *cfg, int fd)
{
  IscSession *scb;
  int res;
185 186
  int flag;
  struct linger ling;
Per Cederqvist's avatar
Per Cederqvist committed
187 188 189 190 191
  

  if (fd == -1)
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
      return NULL;
192 193

  fd = isc_relocate_fd(fd, cfg->fd_relocate);
Per Cederqvist's avatar
Per Cederqvist committed
194 195 196 197 198 199 200 201
  
  /* Set non blocking write mode */
  if ((res = fcntl(fd, F_GETFL, 0)) == -1)
  {
    close(fd);
    return NULL;
  }
    
Per Cederqvist's avatar
Per Cederqvist committed
202 203 204 205
  /* If compilation fails on the next line, please report it as a bug
     to ceder@lysator.liu.se.  I'd like to talk to you so that you can
     test an autoconf solution to this problem.  As a workaround, you
     can change "O_NONBLOCK" to "FNDELAY". */
206
  if (fcntl(fd, F_SETFL, res | O_NONBLOCK) == -1)
Per Cederqvist's avatar
Per Cederqvist committed
207 208 209 210
  {
    close(fd);
    return NULL;
  }
211

212 213 214
#if 0
  /* The setsockopt calls below used to look like this, but this style is
     apparently now obsolete.  It doesn't work on some Linux boxes. */
Per Cederqvist's avatar
Per Cederqvist committed
215 216
  setsockopt(fd, SOL_SOCKET, SO_DONTLINGER, 0, 0); 
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 0, 0);
Per Cederqvist's avatar
Per Cederqvist committed
217 218
#else
  /* This is the modern way to turn off linger and turn on reuseaddr. */
219 220 221 222 223
  ling.l_onoff = 0;
  ling.l_linger = 0;
  setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)); 
  flag = 1;
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
Per Cederqvist's avatar
Per Cederqvist committed
224
#endif
Per Cederqvist's avatar
Per Cederqvist committed
225 226 227
  
  scb = isc_create(cfg, &isc_tcp_funs);
  if (!scb)
228 229
  {
    close(fd);
Per Cederqvist's avatar
Per Cederqvist committed
230
    return NULL;
231
  }
Per Cederqvist's avatar
Per Cederqvist committed
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

  scb->type = ISC_TYPE_TCP;
  scb->fd = fd;
  
  scb->info.tcp.raddr = NULL;
  scb->info.tcp.laddr = NULL;
    
  return scb;
}



/*
** Bind a TCP session to a local port and address
*/
int
isc_bindtcp(IscSession *scb,
	    const char *address,
	    const char *service)
{
  IscAddress *ia;


  if (scb->type != ISC_TYPE_TCP)
    return -1;
  
  ia = isc_mktcpaddress(address, service);
			
  if (!ia)
    return -1;
  
  if (bind(scb->fd, &ia->ip.saddr, sizeof(ia->ip.saddr)) < 0)
    return -1;

  scb->info.tcp.laddr = ia;
    
  return 0;
}


  
/*
Per Cederqvist's avatar
Per Cederqvist committed
274
** Connect a TCP session to a remote service.
Per Cederqvist's avatar
Per Cederqvist committed
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
*/
int
isc_connecttcp(IscSession *scb,
	       const char *address,
	       const char *service)
{
  IscAddress *ia;
  int res;

  
  if (scb->type != ISC_TYPE_TCP)
    return -1;
  
  /* Local connection? */
  if (address == NULL)
    address = "localhost";

  ia = isc_mktcpaddress(address, service);
  if (!ia)
    return -1;
  
  res = connect(scb->fd, &ia->ip.saddr, sizeof(ia->ip.saddr));
  if (res < 0 && errno != EINPROGRESS)
    return -1;

  scb->state = (res < 0 ? ISC_STATE_CONNECTING : ISC_STATE_RUNNING);
  
  scb->info.tcp.raddr = ia;
  if (scb->info.tcp.laddr == NULL)
    scb->info.tcp.laddr = isc_getladdress(scb);
  
  return 0;
}



/*
** Establish a session with a remote TCP service, then insert it
** into a master control structure-
*/
IscSession *
isc_opentcp(IscMaster *mcb,
	    const char *address,
	    const char *service)
{
  IscSession *scb;

  
  scb = isc_createtcp(&mcb->scfg, -1);
  if (!scb)
    return NULL;

  if (isc_connecttcp(scb, address, service) < 0)
  {
    isc_destroy(NULL, scb);
    return NULL;
  }

  (void) isc_insert(mcb, scb);

  return scb;
}



/*
** Establish a port to listen at for new TCP connections
*/
IscSession *
isc_listentcp(IscMaster  *mcb,
	      const char *address,
	      const char *service)
{
  IscSession *scb;
  int retries;
  int errcode;
  

  scb = isc_createtcp(&mcb->scfg, -1);
  if (!scb)
    return NULL;

  for (retries = 0; retries < scb->cfg->max.openretries; sleep(1), retries++)
358 359
  {
    errno = 0;
Per Cederqvist's avatar
Per Cederqvist committed
360 361
    if (isc_bindtcp(scb, address, service) >= 0 || errno != EADDRINUSE)
      break;
362
  }
Per Cederqvist's avatar
Per Cederqvist committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

  if (retries >= scb->cfg->max.openretries || errno != 0)
  {
    errcode = errno;
    isc_destroy(NULL, scb);
    errno = errcode;
    return NULL;
  }

  if (listen(scb->fd, scb->cfg->max.backlog) < 0)
  {
    errcode = errno;
    isc_destroy(NULL, scb);
    errno = errcode;
    return NULL;
  }

  scb->state = ISC_STATE_LISTENING;

  (void) isc_insert(mcb, scb);
  
  return scb;
}