/*
 * ----------------------------------------------------------------
 * Night Light IRC Proxy - Listen Functions
 * ----------------------------------------------------------------
 * Copyright (C) 1997-2007 Jonas Kvinge <jonas@night-light.net>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, 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 General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Last modified by:
 * Jonas Kvinge (24.11.2007)
 *
 */

#define LISTEN_C

#define NEED_SYS_TYPES_H 1		/* Extra types */
#define NEED_SYS_PARAM_H 1		/* Some systems need this */
#define NEED_LIMITS_H 0			/* Kernel limits */
#define NEED_STDARG_H 1			/* va_list, etc */
#define NEED_ERRNO_H 1			/* errno */
#define NEED_CTYPE_H 1			/* isdigit(), etc */
#define NEED_NETINET_IN_H 1		/* in_addr, sockaddr_in, etc */
#define NEED_ARPA_INET_H 1		/* inet_ntoa(), inet_aton(), etc */
#define NEED_STDIO_H 1			/* Standard C UNIX functions */
#define NEED_STDLIB_H 1			/* malloc(), exit(), atoi(), etc */
#define NEED_TIME_H 1			/* time(), etc */
#define NEED_SYSCTL_H 0			/* sysctl(), etc */
#define NEED_SYS_STAT_H 0		/* chmod(), mkdir(), etc */
#define NEED_SYS_UIO_H 0		/* iovec, etc */
#define NEED_FCNTL_H 1			/* open(), creat(), fcntl(), etc */
#define NEED_SYS_IOCTL_H 0		/* ioctl(), etc */
#define NEED_SYS_FILIO_H 0		/* Solaris need this for ioctl(), etc */
#define NEED_UNISTD_H 1			/* Unix standard functions */
#define NEED_STRING_H 1			/* C string functions */
#define NEED_SIGNAL_H 0			/* Signal functions */
#define NEED_SYS_SOCKET_H 0		/* Socket functions */
#define NEED_NETDB_H 1			/* Network database functions */
#define NEED_ARPA_NAMESER_H 0		/* Nameserver definitions */
#define NEED_GETUSERPW_HEADERS 0 	/* Functions to retrive system passwords */
#define NEED_ARES 0			/* Functions needed for ares */
#define NEED_SSL 1			/* Needed for SSL support */

#include "includes.h"

#include "listen.h"
#include "client.h"

/* VARIABLES - JONAS (06.10.2000) */

struct Listen_Struct *Listen_Head = NULL;
struct Listen_Struct *Listen_Tail = NULL;
unsigned long int G_Listens = 0;

extern struct Client_Struct *Client_Head;

/* LISTEN_ADD FUNCTION - JONAS (22.07.2001) */

struct Listen_Struct *listen_add(const char *const HostPT, const unsigned long int Port) {

  struct Listen_Struct *ListenS = NULL;
  struct Listen_Struct *Listen_NEW = NULL;

  assert(HostPT != NULL);

  ListenS = listen_get(HostPT, Port);
  if (ListenS != NULL) {
    aerrno = AEEXISTS;
    return(ListenS);
  }

  Listen_NEW = malloc(sizeof(struct Listen_Struct));
  if (Listen_NEW == NULL) {
    aerrno = AEMALLOC;
    return(NULL);
  }

  memset(Listen_NEW, 0, sizeof(struct Listen_Struct));
  listen_init(Listen_NEW);

  Listen_NEW->Host = strdup(HostPT);
  if (Listen_NEW->Host == NULL) {
    free(Listen_NEW);
    aerrno = AEMALLOC;
    return(NULL);
  }

  Listen_NEW->PortH = Port;
  Listen_NEW->PortN = htons(Port);

  if (Listen_Head == NULL) {
    Listen_Head = Listen_NEW;
    Listen_Tail = Listen_NEW;
  }
  else {
    ListenS = Listen_Tail;
    ListenS->Next = Listen_NEW;
    Listen_NEW->Prev = ListenS;
    Listen_Tail = Listen_NEW;
  }

  G_Listens++;

  aerrno = AESUCCESS;
  return(Listen_NEW);

}

/* LISTEN_REM FUNCTION - JONAS (01.08.2001) */

void listen_rem(struct Listen_Struct *ListenS) {

  struct Client_Struct *ClientS = NULL;

  assert(ListenS != NULL);

  if (ListenS->Prev == NULL) { Listen_Head = ListenS->Next; }
  else { ListenS->Prev->Next = ListenS->Next; }

  if (ListenS->Next == NULL) { Listen_Tail = ListenS->Prev; }
  else { ListenS->Next->Prev = ListenS->Prev; }

  FREE(ListenS->Host);
  FREE(ListenS->HostIPS);

  free(ListenS);

  G_Listens--;

  for (ClientS = Client_Head ; ClientS != NULL ; ClientS = ClientS->Next) {
    if (ClientS->ListenS != ListenS) { continue; }
    ClientS->ListenS = NULL;
  }

}

/* LISTEN_INIT FUNCTION - JONAS (01.08.2001) */

void listen_init(struct Listen_Struct *ListenS) {

  assert(ListenS != NULL);

  ListenS->Flags = 0;
  FREE(ListenS->HostIPS);
  ListenS->PortH = 0;
  ListenS->PortN = 0;
  ListenS->FD = FD_NONE;

}

/* LISTEN_GET - JONAS (06.10.2000) */

struct Listen_Struct *listen_get(const char *const HostPT, const unsigned long int Port) {

  struct Listen_Struct *ListenS = NULL;

  assert(HostPT != NULL);

  for (ListenS = Listen_Head ; ListenS != NULL ; ListenS = ListenS->Next) {
    if ((strcasecmp(ListenS->Host, HostPT) == FALSE) && (ListenS->PortH == Port)) {
      aerrno = AESUCCESS;
      return(ListenS);
    }
  }
  aerrno = AENOMATCH;
  return(NULL);

}


syntax highlighted by Code2HTML, v. 0.9.1