146686Sbrian/*-
246686Sbrian * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
346686Sbrian * All rights reserved.
446686Sbrian *
546686Sbrian * Redistribution and use in source and binary forms, with or without
646686Sbrian * modification, are permitted provided that the following conditions
746686Sbrian * are met:
846686Sbrian * 1. Redistributions of source code must retain the above copyright
946686Sbrian *    notice, this list of conditions and the following disclaimer.
1046686Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1146686Sbrian *    notice, this list of conditions and the following disclaimer in the
1246686Sbrian *    documentation and/or other materials provided with the distribution.
1346686Sbrian *
1446686Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1546686Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1646686Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1746686Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1846686Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1946686Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2046686Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2146686Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2246686Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2346686Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2446686Sbrian * SUCH DAMAGE.
2546686Sbrian *
2650479Speter * $FreeBSD$
2746686Sbrian */
2846686Sbrian
2946686Sbrian#include <sys/types.h>
3046686Sbrian#include <sys/socket.h>
3146686Sbrian#include <netinet/in.h>
3246686Sbrian#include <arpa/inet.h>
3346686Sbrian#include <netdb.h>
3446686Sbrian
3546686Sbrian#include <errno.h>
3646686Sbrian#include <stdio.h>
3746686Sbrian#include <stdlib.h>
3846686Sbrian#include <string.h>
3971006Sbrian#include <sys/stat.h>
4047769Sbrian#include <sys/uio.h>
4146686Sbrian#include <termios.h>
4246686Sbrian#include <unistd.h>
4346686Sbrian
4446686Sbrian#include "layer.h"
4546686Sbrian#include "defs.h"
4646686Sbrian#include "mbuf.h"
4746686Sbrian#include "log.h"
4846686Sbrian#include "timer.h"
4946686Sbrian#include "lqr.h"
5046686Sbrian#include "hdlc.h"
5146686Sbrian#include "throughput.h"
5246686Sbrian#include "fsm.h"
5346686Sbrian#include "lcp.h"
5446686Sbrian#include "ccp.h"
5546686Sbrian#include "link.h"
5646686Sbrian#include "async.h"
5746686Sbrian#include "descriptor.h"
5846686Sbrian#include "physical.h"
5946686Sbrian#include "tcp.h"
6046686Sbrian
6146686Sbrianstatic int
6247061Sbriantcp_OpenConnection(const char *name, char *host, char *port)
6346686Sbrian{
6446686Sbrian  struct sockaddr_in dest;
6546686Sbrian  int sock;
6646686Sbrian  struct servent *sp;
6746686Sbrian
6846686Sbrian  dest.sin_family = AF_INET;
6946686Sbrian  dest.sin_addr = GetIpAddr(host);
7046686Sbrian  if (dest.sin_addr.s_addr == INADDR_NONE) {
7146686Sbrian    log_Printf(LogWARN, "%s: %s: unknown host\n", name, host);
7260779Sbrian    return -2;
7346686Sbrian  }
7446686Sbrian  dest.sin_port = htons(atoi(port));
7546686Sbrian  if (dest.sin_port == 0) {
7646686Sbrian    sp = getservbyname(port, "tcp");
7747061Sbrian    if (sp)
7846686Sbrian      dest.sin_port = sp->s_port;
7947061Sbrian    else {
8046686Sbrian      log_Printf(LogWARN, "%s: %s: unknown service\n", name, port);
8160779Sbrian      return -2;
8246686Sbrian    }
8346686Sbrian  }
8447061Sbrian  log_Printf(LogPHASE, "%s: Connecting to %s:%s/tcp\n", name, host, port);
8546686Sbrian
8689422Sbrian  sock = socket(PF_INET, SOCK_STREAM, 0);
8747061Sbrian  if (sock < 0)
8860779Sbrian    return -2;
8947061Sbrian
9046686Sbrian  if (connect(sock, (struct sockaddr *)&dest, sizeof dest) < 0) {
9146686Sbrian    log_Printf(LogWARN, "%s: connect: %s\n", name, strerror(errno));
9246686Sbrian    close(sock);
9360779Sbrian    return -2;
9446686Sbrian  }
9546686Sbrian
9647061Sbrian  return sock;
9746686Sbrian}
9846686Sbrian
9947061Sbrianstatic struct device tcpdevice = {
10047061Sbrian  TCP_DEVICE,
10146686Sbrian  "tcp",
10278410Sbrian  0,
10353733Sbrian  { CD_NOTREQUIRED, 0 },
10446686Sbrian  NULL,
10546686Sbrian  NULL,
10646686Sbrian  NULL,
10746686Sbrian  NULL,
10846686Sbrian  NULL,
10946686Sbrian  NULL,
11047061Sbrian  NULL,
11147061Sbrian  NULL,
11247061Sbrian  NULL,
11349472Sbrian  NULL,
11452942Sbrian  NULL,
11593418Sbrian  NULL,
11696582Sbrian  NULL,
11746686Sbrian  NULL
11846686Sbrian};
11947061Sbrian
12047061Sbrianstruct device *
12147061Sbriantcp_iov2device(int type, struct physical *p, struct iovec *iov,
122134789Sbrian               int *niov, int maxiov __unused, int *auxfd __unused,
123134789Sbrian	       int *nauxfd __unused)
12447061Sbrian{
12547461Sbrian  if (type == TCP_DEVICE) {
12647769Sbrian    free(iov[(*niov)++].iov_base);
12747461Sbrian    physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC);
12847061Sbrian    return &tcpdevice;
12947461Sbrian  }
13047061Sbrian
13147061Sbrian  return NULL;
13247061Sbrian}
13347061Sbrian
13447061Sbrianstruct device *
13547061Sbriantcp_Create(struct physical *p)
13647061Sbrian{
13747061Sbrian  char *cp, *host, *port, *svc;
13847061Sbrian
13947061Sbrian  if (p->fd < 0) {
14052942Sbrian    if ((cp = strchr(p->name.full, ':')) != NULL && !strchr(cp + 1, ':')) {
14147061Sbrian      *cp = '\0';
14247061Sbrian      host = p->name.full;
14347061Sbrian      port = cp + 1;
14447061Sbrian      svc = strchr(port, '/');
14547061Sbrian      if (svc && strcasecmp(svc, "/tcp")) {
14647061Sbrian        *cp = ':';
14747061Sbrian        return 0;
14847061Sbrian      }
14952942Sbrian      if (svc) {
15052942Sbrian        p->fd--;     /* We own the device but maybe can't use it - change fd */
15147061Sbrian        *svc = '\0';
15252942Sbrian      }
15347061Sbrian      if (*host && *port) {
15447061Sbrian        p->fd = tcp_OpenConnection(p->link.name, host, port);
15547061Sbrian        *cp = ':';
15647061Sbrian        if (svc)
15747061Sbrian          *svc = '/';
15847061Sbrian        if (p->fd >= 0)
15947061Sbrian          log_Printf(LogDEBUG, "%s: Opened tcp socket %s\n", p->link.name,
16047061Sbrian                     p->name.full);
16147061Sbrian      } else {
16247061Sbrian        if (svc)
16347061Sbrian          *svc = '/';
16447061Sbrian        *cp = ':';
16547061Sbrian      }
16647061Sbrian    }
16747061Sbrian  }
16847061Sbrian
16947061Sbrian  if (p->fd >= 0) {
17047061Sbrian    /* See if we're a tcp socket */
17171006Sbrian    struct stat st;
17247061Sbrian
17371006Sbrian    if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
17471006Sbrian      int type, sz;
17547061Sbrian
17671006Sbrian      sz = sizeof type;
17771006Sbrian      if (getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz) == -1) {
17871006Sbrian        log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
17971006Sbrian        close(p->fd);
18071006Sbrian        p->fd = -1;
18171006Sbrian        return NULL;
18271006Sbrian      }
18347061Sbrian
18471006Sbrian      if (sz == sizeof type && type == SOCK_STREAM) {
18571006Sbrian        struct sockaddr_in sock;
18671006Sbrian        struct sockaddr *sockp = (struct sockaddr *)&sock;
18747061Sbrian
18871006Sbrian        if (*p->name.full == '\0') {
18971006Sbrian          sz = sizeof sock;
19071006Sbrian          if (getpeername(p->fd, sockp, &sz) != 0 ||
19171006Sbrian              sz != sizeof(struct sockaddr_in) || sock.sin_family != AF_INET) {
19271006Sbrian            log_Printf(LogDEBUG, "%s: Link is SOCK_STREAM, but not inet\n",
19371006Sbrian                       p->link.name);
19471006Sbrian            return NULL;
19571006Sbrian          }
19671006Sbrian
19771006Sbrian          log_Printf(LogPHASE, "%s: Link is a tcp socket\n", p->link.name);
19871006Sbrian
19971006Sbrian          snprintf(p->name.full, sizeof p->name.full, "%s:%d/tcp",
20071006Sbrian                   inet_ntoa(sock.sin_addr), ntohs(sock.sin_port));
20171006Sbrian          p->name.base = p->name.full;
20271006Sbrian        }
20371006Sbrian        physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC);
20471006Sbrian        if (p->cfg.cd.necessity != CD_DEFAULT)
20571006Sbrian          log_Printf(LogWARN, "Carrier settings ignored\n");
20671006Sbrian        return &tcpdevice;
20747061Sbrian      }
20847061Sbrian    }
20947061Sbrian  }
21047061Sbrian
21147061Sbrian  return NULL;
21247061Sbrian}
213