defs.c revision 31921
131921Sbrian/*-
231921Sbrian * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
331921Sbrian * All rights reserved.
431921Sbrian *
531921Sbrian * Redistribution and use in source and binary forms, with or without
631921Sbrian * modification, are permitted provided that the following conditions
731921Sbrian * are met:
831921Sbrian * 1. Redistributions of source code must retain the above copyright
931921Sbrian *    notice, this list of conditions and the following disclaimer.
1031921Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1131921Sbrian *    notice, this list of conditions and the following disclaimer in the
1231921Sbrian *    documentation and/or other materials provided with the distribution.
1331921Sbrian *
1431921Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1531921Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1631921Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1731921Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1831921Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1931921Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2031921Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2131921Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2231921Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2331921Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2431921Sbrian * SUCH DAMAGE.
2531921Sbrian *
2631921Sbrian *	$Id$
2730715Sbrian */
2830715Sbrian
2931196Sbrian#include <sys/param.h>
3031196Sbrian#include <netinet/in.h>
3131196Sbrian
3231196Sbrian#include <errno.h>
3331196Sbrian#include <stdio.h>
3430715Sbrian#include <stdlib.h>
3531121Sbrian#include <string.h>
3631343Sbrian#include <time.h>
3731196Sbrian#include <unistd.h>
3830715Sbrian
3930715Sbrian#include "defs.h"
4031343Sbrian#include "command.h"
4131196Sbrian#include "mbuf.h"
4231196Sbrian#include "log.h"
4331196Sbrian#include "loadalias.h"
4431196Sbrian#include "vars.h"
4530715Sbrian
4630715Sbrianint mode = MODE_INTER;
4730715Sbrianint BGFiledes[2] = { -1, -1 };
4830715Sbrianint modem = -1;
4930715Sbrianint tun_in = -1;
5030715Sbrianint tun_out = -1;
5130715Sbrianint netfd = -1;
5230715Sbrian
5331121Sbrianstatic char dstsystem[50];
5431121Sbrian
5530715Sbrianvoid
5631121SbrianSetLabel(const char *label)
5731121Sbrian{
5831121Sbrian  if (label)
5931121Sbrian    strncpy(dstsystem, label, sizeof dstsystem);
6031121Sbrian  else
6131121Sbrian    *dstsystem = '\0';
6231121Sbrian}
6331121Sbrian
6431121Sbrianconst char *
6531121SbrianGetLabel()
6631121Sbrian{
6731121Sbrian  return *dstsystem ? dstsystem : NULL;
6831121Sbrian}
6931121Sbrian
7031121Sbrianvoid
7130715Sbrianrandinit()
7230715Sbrian{
7331343Sbrian#ifdef __FreeBSD__
7430715Sbrian  static int initdone;
7530715Sbrian
7630715Sbrian  if (!initdone) {
7730715Sbrian    initdone = 1;
7830715Sbrian    srandomdev();
7930715Sbrian  }
8031343Sbrian#else
8131343Sbrian  srandom(time(NULL)^getpid());
8231343Sbrian#endif
8330715Sbrian}
8431196Sbrian
8531196Sbrian
8631196Sbrianint
8731196SbrianGetShortHost()
8831196Sbrian{
8931196Sbrian  char *p;
9031196Sbrian
9131196Sbrian  if (gethostname(VarShortHost, sizeof(VarShortHost))) {
9231196Sbrian    LogPrintf(LogERROR, "GetShortHost: gethostbyname: %s\n", strerror(errno));
9331196Sbrian    return 0;
9431196Sbrian  }
9531196Sbrian
9631196Sbrian  if ((p = strchr(VarShortHost, '.')))
9731196Sbrian    *p = '\0';
9831196Sbrian
9931196Sbrian  return 1;
10031196Sbrian}
10131203Sbrian
10231203Sbrianvoid
10331203SbrianDropClient()
10431203Sbrian{
10531203Sbrian  FILE *oVarTerm;
10631203Sbrian
10731203Sbrian  if (VarTerm && !(mode & MODE_INTER)) {
10831203Sbrian    oVarTerm = VarTerm;
10931203Sbrian    VarTerm = 0;
11031203Sbrian    if (oVarTerm)
11131203Sbrian      fclose(oVarTerm);
11231203Sbrian    close(netfd);
11331203Sbrian    netfd = -1;
11431203Sbrian    LogPrintf(LogPHASE, "Client connection closed.\n");
11531203Sbrian  }
11631203Sbrian}
117