xref: /freebsd/usr.sbin/ppp/physical.c (revision 9950fb2a0035e3d4439c9ad3e24b48dfb6548ecc)
163b73463SBrian Somers /*
263b73463SBrian Somers  * Written by Eivind Eklund <eivind@yes.no>
363b73463SBrian Somers  *    for Yes Interactive
463b73463SBrian Somers  *
563b73463SBrian Somers  * Copyright (C) 1998, Yes Interactive.  All rights reserved.
663b73463SBrian Somers  *
763b73463SBrian Somers  * Redistribution and use in any form is permitted.  Redistribution in
863b73463SBrian Somers  * source form should include the above copyright and this set of
963b73463SBrian Somers  * conditions, because large sections american law seems to have been
1063b73463SBrian Somers  * created by a bunch of jerks on drugs that are now illegal, forcing
1163b73463SBrian Somers  * me to include this copyright-stuff instead of placing this in the
1263b73463SBrian Somers  * public domain.  The name of of 'Yes Interactive' or 'Eivind Eklund'
1363b73463SBrian Somers  * may not be used to endorse or promote products derived from this
1463b73463SBrian Somers  * software without specific prior written permission.
1563b73463SBrian Somers  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1663b73463SBrian Somers  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1763b73463SBrian Somers  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1863b73463SBrian Somers  *
199950fb2aSBrian Somers  *  $Id: physical.c,v 1.12 1999/05/13 19:29:40 brian Exp $
2063b73463SBrian Somers  *
2163b73463SBrian Somers  */
2263b73463SBrian Somers 
235d9e6103SBrian Somers #include <sys/param.h>
245d9e6103SBrian Somers #include <sys/socket.h>
255d9e6103SBrian Somers #include <netinet/in.h>
265d9e6103SBrian Somers #include <arpa/inet.h>
275d9e6103SBrian Somers #include <netdb.h>
285d9e6103SBrian Somers #include <netinet/in_systm.h>
295d9e6103SBrian Somers #include <netinet/ip.h>
305d9e6103SBrian Somers #include <sys/un.h>
315d9e6103SBrian Somers 
325d9e6103SBrian Somers #include <errno.h>
335d9e6103SBrian Somers #include <fcntl.h>
345d9e6103SBrian Somers #include <paths.h>
3563b73463SBrian Somers #include <stdio.h>
366f384573SBrian Somers #include <stdlib.h>
3763b73463SBrian Somers #include <string.h>
385d9e6103SBrian Somers #include <sys/tty.h>	/* TIOCOUTQ */
395d9e6103SBrian Somers #include <sys/uio.h>
405d9e6103SBrian Somers #include <sys/wait.h>
41fc1141b2SBrian Somers #include <time.h>
4263b73463SBrian Somers #include <unistd.h>
43fc1141b2SBrian Somers #include <utmp.h>
445d9e6103SBrian Somers #if defined(__OpenBSD__) || defined(__NetBSD__)
455d9e6103SBrian Somers #include <sys/ioctl.h>
465d9e6103SBrian Somers #include <util.h>
475d9e6103SBrian Somers #else
485d9e6103SBrian Somers #include <libutil.h>
495d9e6103SBrian Somers #endif
5063b73463SBrian Somers 
515d9e6103SBrian Somers #include "layer.h"
525d9e6103SBrian Somers #ifndef NOALIAS
535d9e6103SBrian Somers #include "alias_cmd.h"
545d9e6103SBrian Somers #endif
555d9e6103SBrian Somers #include "proto.h"
565d9e6103SBrian Somers #include "acf.h"
575d9e6103SBrian Somers #include "vjcomp.h"
5863b73463SBrian Somers #include "defs.h"
595d9e6103SBrian Somers #include "command.h"
6063b73463SBrian Somers #include "mbuf.h"
6142d4d396SBrian Somers #include "log.h"
62fc1141b2SBrian Somers #include "id.h"
635d9e6103SBrian Somers #include "timer.h"
645d9e6103SBrian Somers #include "fsm.h"
655d9e6103SBrian Somers #include "lqr.h"
665d9e6103SBrian Somers #include "hdlc.h"
675d9e6103SBrian Somers #include "lcp.h"
685d9e6103SBrian Somers #include "throughput.h"
695d9e6103SBrian Somers #include "sync.h"
705d9e6103SBrian Somers #include "async.h"
715d9e6103SBrian Somers #include "iplist.h"
725d9e6103SBrian Somers #include "slcompress.h"
735d9e6103SBrian Somers #include "ipcp.h"
745d9e6103SBrian Somers #include "filter.h"
755d9e6103SBrian Somers #include "descriptor.h"
765d9e6103SBrian Somers #include "ccp.h"
775d9e6103SBrian Somers #include "link.h"
785d9e6103SBrian Somers #include "physical.h"
795d9e6103SBrian Somers #include "mp.h"
805d9e6103SBrian Somers #ifndef NORADIUS
815d9e6103SBrian Somers #include "radius.h"
825d9e6103SBrian Somers #endif
835d9e6103SBrian Somers #include "bundle.h"
845d9e6103SBrian Somers #include "prompt.h"
855d9e6103SBrian Somers #include "chat.h"
865d9e6103SBrian Somers #include "auth.h"
875d9e6103SBrian Somers #include "chap.h"
885d9e6103SBrian Somers #include "cbcp.h"
895d9e6103SBrian Somers #include "datalink.h"
905d9e6103SBrian Somers #include "tcp.h"
916815097bSBrian Somers #include "udp.h"
925d9e6103SBrian Somers #include "exec.h"
935d9e6103SBrian Somers #include "tty.h"
9463b73463SBrian Somers 
9563b73463SBrian Somers 
965d9e6103SBrian Somers static int physical_DescriptorWrite(struct descriptor *, struct bundle *,
975d9e6103SBrian Somers                                     const fd_set *);
985d9e6103SBrian Somers static void physical_DescriptorRead(struct descriptor *, struct bundle *,
995d9e6103SBrian Somers                                     const fd_set *);
10063b73463SBrian Somers 
1016815097bSBrian Somers struct {
1026815097bSBrian Somers   struct device *(*create)(struct physical *);
1036815097bSBrian Somers   struct device *(*iov2device)(int, struct physical *, struct iovec *iov,
1046815097bSBrian Somers                                int *niov, int maxiov);
1056815097bSBrian Somers } devices[] = {
1066815097bSBrian Somers   { tty_Create, tty_iov2device },
1076815097bSBrian Somers   { tcp_Create, tcp_iov2device },
1086815097bSBrian Somers   { udp_Create, udp_iov2device },
1096815097bSBrian Somers   { exec_Create, exec_iov2device }
1105d9e6103SBrian Somers };
11163b73463SBrian Somers 
1126815097bSBrian Somers #define NDEVICES (sizeof devices / sizeof devices[0])
1135d9e6103SBrian Somers 
1145d9e6103SBrian Somers static int
1155d9e6103SBrian Somers physical_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
1165d9e6103SBrian Somers                    int *n)
1173bf710a4SBrian Somers {
1185d9e6103SBrian Somers   return physical_doUpdateSet(d, r, w, e, n, 0);
1195d9e6103SBrian Somers }
1205d9e6103SBrian Somers 
1215d9e6103SBrian Somers struct physical *
1225d9e6103SBrian Somers physical_Create(struct datalink *dl, int type)
1235d9e6103SBrian Somers {
1245d9e6103SBrian Somers   struct physical *p;
1255d9e6103SBrian Somers 
1265d9e6103SBrian Somers   p = (struct physical *)malloc(sizeof(struct physical));
1275d9e6103SBrian Somers   if (!p)
1285d9e6103SBrian Somers     return NULL;
1295d9e6103SBrian Somers 
1305d9e6103SBrian Somers   p->link.type = PHYSICAL_LINK;
1315d9e6103SBrian Somers   p->link.name = dl->name;
1325d9e6103SBrian Somers   p->link.len = sizeof *p;
1335d9e6103SBrian Somers   throughput_init(&p->link.throughput);
1345d9e6103SBrian Somers 
1355d9e6103SBrian Somers   memset(p->link.Queue, '\0', sizeof p->link.Queue);
1365d9e6103SBrian Somers   memset(p->link.proto_in, '\0', sizeof p->link.proto_in);
1375d9e6103SBrian Somers   memset(p->link.proto_out, '\0', sizeof p->link.proto_out);
1385d9e6103SBrian Somers   link_EmptyStack(&p->link);
1395d9e6103SBrian Somers 
1405d9e6103SBrian Somers   p->handler = NULL;
1415d9e6103SBrian Somers   p->desc.type = PHYSICAL_DESCRIPTOR;
1425d9e6103SBrian Somers   p->desc.UpdateSet = physical_UpdateSet;
1435d9e6103SBrian Somers   p->desc.IsSet = physical_IsSet;
1445d9e6103SBrian Somers   p->desc.Read = physical_DescriptorRead;
1455d9e6103SBrian Somers   p->desc.Write = physical_DescriptorWrite;
1465d9e6103SBrian Somers   p->type = type;
1475d9e6103SBrian Somers 
1485d9e6103SBrian Somers   hdlc_Init(&p->hdlc, &p->link.lcp);
1495d9e6103SBrian Somers   async_Init(&p->async);
1505d9e6103SBrian Somers 
1515d9e6103SBrian Somers   p->fd = -1;
1525d9e6103SBrian Somers   p->out = NULL;
1535d9e6103SBrian Somers   p->connect_count = 0;
1545d9e6103SBrian Somers   p->dl = dl;
1555d9e6103SBrian Somers   p->input.sz = 0;
1565d9e6103SBrian Somers   *p->name.full = '\0';
1575d9e6103SBrian Somers   p->name.base = p->name.full;
1585d9e6103SBrian Somers 
1595d9e6103SBrian Somers   p->Utmp = 0;
1605d9e6103SBrian Somers   p->session_owner = (pid_t)-1;
1615d9e6103SBrian Somers 
1625d9e6103SBrian Somers   p->cfg.rts_cts = MODEM_CTSRTS;
1635d9e6103SBrian Somers   p->cfg.speed = MODEM_SPEED;
1645d9e6103SBrian Somers   p->cfg.parity = CS8;
1655d9e6103SBrian Somers   memcpy(p->cfg.devlist, MODEM_LIST, sizeof MODEM_LIST);
1665d9e6103SBrian Somers   p->cfg.ndev = NMODEMS;
1675d9e6103SBrian Somers   p->cfg.cd.required = 0;
1685d9e6103SBrian Somers   p->cfg.cd.delay = DEF_CDDELAY;
1695d9e6103SBrian Somers 
1705d9e6103SBrian Somers   lcp_Init(&p->link.lcp, dl->bundle, &p->link, &dl->fsmp);
1715d9e6103SBrian Somers   ccp_Init(&p->link.ccp, dl->bundle, &p->link, &dl->fsmp);
1725d9e6103SBrian Somers 
1735d9e6103SBrian Somers   return p;
1745d9e6103SBrian Somers }
1755d9e6103SBrian Somers 
1765d9e6103SBrian Somers static const struct parity {
1775d9e6103SBrian Somers   const char *name;
1785d9e6103SBrian Somers   const char *name1;
1795d9e6103SBrian Somers   int set;
1805d9e6103SBrian Somers } validparity[] = {
1815d9e6103SBrian Somers   { "even", "P_EVEN", CS7 | PARENB },
1825d9e6103SBrian Somers   { "odd", "P_ODD", CS7 | PARENB | PARODD },
1835d9e6103SBrian Somers   { "none", "P_ZERO", CS8 },
1845d9e6103SBrian Somers   { NULL, 0 },
1855d9e6103SBrian Somers };
1865d9e6103SBrian Somers 
1875d9e6103SBrian Somers static int
1885d9e6103SBrian Somers GetParityValue(const char *str)
1895d9e6103SBrian Somers {
1905d9e6103SBrian Somers   const struct parity *pp;
1915d9e6103SBrian Somers 
1925d9e6103SBrian Somers   for (pp = validparity; pp->name; pp++) {
1935d9e6103SBrian Somers     if (strcasecmp(pp->name, str) == 0 ||
1945d9e6103SBrian Somers 	strcasecmp(pp->name1, str) == 0) {
1955d9e6103SBrian Somers       return pp->set;
1965d9e6103SBrian Somers     }
1975d9e6103SBrian Somers   }
1985d9e6103SBrian Somers   return (-1);
1995d9e6103SBrian Somers }
2005d9e6103SBrian Somers 
2015d9e6103SBrian Somers int
2025d9e6103SBrian Somers physical_SetParity(struct physical *p, const char *str)
2035d9e6103SBrian Somers {
2045d9e6103SBrian Somers   struct termios rstio;
2055d9e6103SBrian Somers   int val;
2065d9e6103SBrian Somers 
2075d9e6103SBrian Somers   val = GetParityValue(str);
2085d9e6103SBrian Somers   if (val > 0) {
2095d9e6103SBrian Somers     p->cfg.parity = val;
2105d9e6103SBrian Somers     if (p->fd >= 0) {
2115d9e6103SBrian Somers       tcgetattr(p->fd, &rstio);
2125d9e6103SBrian Somers       rstio.c_cflag &= ~(CSIZE | PARODD | PARENB);
2135d9e6103SBrian Somers       rstio.c_cflag |= val;
2145d9e6103SBrian Somers       tcsetattr(p->fd, TCSADRAIN, &rstio);
2155d9e6103SBrian Somers     }
2165d9e6103SBrian Somers     return 0;
2175d9e6103SBrian Somers   }
2185d9e6103SBrian Somers   log_Printf(LogWARN, "%s: %s: Invalid parity\n", p->link.name, str);
2195d9e6103SBrian Somers   return -1;
2205d9e6103SBrian Somers }
2215d9e6103SBrian Somers 
2225d9e6103SBrian Somers int
2235d9e6103SBrian Somers physical_GetSpeed(struct physical *p)
2245d9e6103SBrian Somers {
2255d9e6103SBrian Somers   if (p->handler && p->handler->speed)
2265d9e6103SBrian Somers     return (*p->handler->speed)(p);
2275d9e6103SBrian Somers 
2285d9e6103SBrian Somers   return 115200;
2295d9e6103SBrian Somers }
2305d9e6103SBrian Somers 
2315d9e6103SBrian Somers int
2325d9e6103SBrian Somers physical_SetSpeed(struct physical *p, int speed)
2335d9e6103SBrian Somers {
2345d9e6103SBrian Somers   if (IntToSpeed(speed) != B0) {
2355d9e6103SBrian Somers       p->cfg.speed = speed;
2365d9e6103SBrian Somers       return 1;
2375d9e6103SBrian Somers   }
2385d9e6103SBrian Somers 
2395d9e6103SBrian Somers   return 0;
2405d9e6103SBrian Somers }
2415d9e6103SBrian Somers 
2425d9e6103SBrian Somers int
2435d9e6103SBrian Somers physical_Raw(struct physical *p)
2445d9e6103SBrian Somers {
2455d9e6103SBrian Somers   if (p->handler && p->handler->raw)
2465d9e6103SBrian Somers     return (*p->handler->raw)(p);
2475d9e6103SBrian Somers 
2485d9e6103SBrian Somers   return 1;
2495d9e6103SBrian Somers }
2505d9e6103SBrian Somers 
2515d9e6103SBrian Somers void
2525d9e6103SBrian Somers physical_Offline(struct physical *p)
2535d9e6103SBrian Somers {
2545d9e6103SBrian Somers   if (p->handler && p->handler->offline)
2555d9e6103SBrian Somers     (*p->handler->offline)(p);
2565d9e6103SBrian Somers   log_Printf(LogPHASE, "%s: Disconnected!\n", p->link.name);
2575d9e6103SBrian Somers }
2585d9e6103SBrian Somers 
2599950fb2aSBrian Somers static int
2609950fb2aSBrian Somers physical_Lock(struct physical *p)
2619950fb2aSBrian Somers {
2629950fb2aSBrian Somers   int res;
2639950fb2aSBrian Somers 
2649950fb2aSBrian Somers   if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
2659950fb2aSBrian Somers       (res = ID0uu_lock(p->name.base)) != UU_LOCK_OK) {
2669950fb2aSBrian Somers     if (res == UU_LOCK_INUSE)
2679950fb2aSBrian Somers       log_Printf(LogPHASE, "%s: %s is in use\n", p->link.name, p->name.full);
2689950fb2aSBrian Somers     else
2699950fb2aSBrian Somers       log_Printf(LogPHASE, "%s: %s is in use: uu_lock: %s\n",
2709950fb2aSBrian Somers                  p->link.name, p->name.full, uu_lockerr(res));
2719950fb2aSBrian Somers     return 0;
2729950fb2aSBrian Somers   }
2739950fb2aSBrian Somers 
2749950fb2aSBrian Somers   return 1;
2759950fb2aSBrian Somers }
2769950fb2aSBrian Somers 
2775d9e6103SBrian Somers static void
2789950fb2aSBrian Somers physical_Unlock(struct physical *p)
2799950fb2aSBrian Somers {
2809950fb2aSBrian Somers   char fn[MAXPATHLEN];
2819950fb2aSBrian Somers   if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
2829950fb2aSBrian Somers       ID0uu_unlock(p->name.base) == -1)
2839950fb2aSBrian Somers     log_Printf(LogALERT, "%s: Can't uu_unlock %s\n", p->link.name, fn);
2849950fb2aSBrian Somers }
2859950fb2aSBrian Somers 
2869950fb2aSBrian Somers void
2879950fb2aSBrian Somers physical_Close(struct physical *p)
2885d9e6103SBrian Somers {
2895d9e6103SBrian Somers   int newsid;
2909950fb2aSBrian Somers   char fn[MAXPATHLEN];
2915d9e6103SBrian Somers 
2929950fb2aSBrian Somers   if (p->fd < 0)
2939950fb2aSBrian Somers     return;
2949950fb2aSBrian Somers 
2959950fb2aSBrian Somers   log_Printf(LogDEBUG, "%s: Close\n", p->link.name);
2969950fb2aSBrian Somers 
2979950fb2aSBrian Somers   if (p->handler && p->handler->cooked)
2989950fb2aSBrian Somers     (*p->handler->cooked)(p);
2999950fb2aSBrian Somers 
3006815097bSBrian Somers   physical_StopDeviceTimer(p);
3015d9e6103SBrian Somers   if (p->Utmp) {
3025d9e6103SBrian Somers     ID0logout(p->name.base);
3035d9e6103SBrian Somers     p->Utmp = 0;
3045d9e6103SBrian Somers   }
3055d9e6103SBrian Somers   newsid = tcgetpgrp(p->fd) == getpgrp();
3065d9e6103SBrian Somers   close(p->fd);
3075d9e6103SBrian Somers   p->fd = -1;
3085d9e6103SBrian Somers   log_SetTtyCommandMode(p->dl);
3099950fb2aSBrian Somers 
3105d9e6103SBrian Somers   throughput_stop(&p->link.throughput);
3115d9e6103SBrian Somers   throughput_log(&p->link.throughput, LogPHASE, p->link.name);
3129950fb2aSBrian Somers 
3135d9e6103SBrian Somers   if (p->session_owner != (pid_t)-1) {
3145d9e6103SBrian Somers     ID0kill(p->session_owner, SIGHUP);
3155d9e6103SBrian Somers     p->session_owner = (pid_t)-1;
3165d9e6103SBrian Somers   }
3179950fb2aSBrian Somers 
3185d9e6103SBrian Somers   if (newsid)
3195d9e6103SBrian Somers     bundle_setsid(p->dl->bundle, 0);
3209950fb2aSBrian Somers 
3219950fb2aSBrian Somers   if (*p->name.full == '/') {
3229950fb2aSBrian Somers     snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
3239950fb2aSBrian Somers #ifndef RELEASE_CRUNCH
3249950fb2aSBrian Somers     if (ID0unlink(fn) == -1)
3259950fb2aSBrian Somers       log_Printf(LogALERT, "%s: Can't remove %s: %s\n",
3269950fb2aSBrian Somers                  p->link.name, fn, strerror(errno));
3279950fb2aSBrian Somers #else
3289950fb2aSBrian Somers     ID0unlink(fn);
3299950fb2aSBrian Somers #endif
3309950fb2aSBrian Somers   }
3319950fb2aSBrian Somers   physical_Unlock(p);
3326815097bSBrian Somers   if (p->handler && p->handler->destroy)
3336815097bSBrian Somers     (*p->handler->destroy)(p);
3345d9e6103SBrian Somers   p->handler = NULL;
3355d9e6103SBrian Somers   p->name.base = p->name.full;
3369950fb2aSBrian Somers   *p->name.full = '\0';
3375d9e6103SBrian Somers }
3385d9e6103SBrian Somers 
3395d9e6103SBrian Somers void
3405d9e6103SBrian Somers physical_Destroy(struct physical *p)
3415d9e6103SBrian Somers {
3425d9e6103SBrian Somers   physical_Close(p);
3435d9e6103SBrian Somers   free(p);
3445d9e6103SBrian Somers }
3455d9e6103SBrian Somers 
3465d9e6103SBrian Somers static int
3475d9e6103SBrian Somers physical_DescriptorWrite(struct descriptor *d, struct bundle *bundle,
3485d9e6103SBrian Somers                          const fd_set *fdset)
3495d9e6103SBrian Somers {
3505d9e6103SBrian Somers   struct physical *p = descriptor2physical(d);
3515d9e6103SBrian Somers   int nw, result = 0;
3525d9e6103SBrian Somers 
3535d9e6103SBrian Somers   if (p->out == NULL)
3545d9e6103SBrian Somers     p->out = link_Dequeue(&p->link);
3555d9e6103SBrian Somers 
3565d9e6103SBrian Somers   if (p->out) {
3575d9e6103SBrian Somers     nw = physical_Write(p, MBUF_CTOP(p->out), p->out->cnt);
3585d9e6103SBrian Somers     log_Printf(LogDEBUG, "%s: DescriptorWrite: wrote %d(%d) to %d\n",
3595d9e6103SBrian Somers                p->link.name, nw, p->out->cnt, p->fd);
3605d9e6103SBrian Somers     if (nw > 0) {
3615d9e6103SBrian Somers       p->out->cnt -= nw;
3625d9e6103SBrian Somers       p->out->offset += nw;
3635d9e6103SBrian Somers       if (p->out->cnt == 0)
3645d9e6103SBrian Somers 	p->out = mbuf_FreeSeg(p->out);
3655d9e6103SBrian Somers       result = 1;
3665d9e6103SBrian Somers     } else if (nw < 0) {
3675d9e6103SBrian Somers       if (errno != EAGAIN) {
3685d9e6103SBrian Somers 	log_Printf(LogPHASE, "%s: write (%d): %s\n", p->link.name,
3695d9e6103SBrian Somers                    p->fd, strerror(errno));
3705d9e6103SBrian Somers         datalink_Down(p->dl, CLOSE_NORMAL);
3715d9e6103SBrian Somers       }
3725d9e6103SBrian Somers       result = 1;
3735d9e6103SBrian Somers     }
3745d9e6103SBrian Somers     /* else we shouldn't really have been called !  select() is broken ! */
3755d9e6103SBrian Somers   }
3765d9e6103SBrian Somers 
3775d9e6103SBrian Somers   return result;
3785d9e6103SBrian Somers }
3795d9e6103SBrian Somers 
3805d9e6103SBrian Somers int
3815d9e6103SBrian Somers physical_ShowStatus(struct cmdargs const *arg)
3825d9e6103SBrian Somers {
3835d9e6103SBrian Somers   struct physical *p = arg->cx->physical;
3845d9e6103SBrian Somers   const char *dev;
3855d9e6103SBrian Somers   int n;
3865d9e6103SBrian Somers 
3875d9e6103SBrian Somers   prompt_Printf(arg->prompt, "Name: %s\n", p->link.name);
3885d9e6103SBrian Somers   prompt_Printf(arg->prompt, " State:           ");
3895d9e6103SBrian Somers   if (p->fd < 0)
3905d9e6103SBrian Somers     prompt_Printf(arg->prompt, "closed\n");
3915d9e6103SBrian Somers   else if (p->handler && p->handler->openinfo)
3925d9e6103SBrian Somers     prompt_Printf(arg->prompt, "open (%s)\n", (*p->handler->openinfo)(p));
3935d9e6103SBrian Somers   else
3945d9e6103SBrian Somers     prompt_Printf(arg->prompt, "open\n");
3955d9e6103SBrian Somers 
3965d9e6103SBrian Somers   prompt_Printf(arg->prompt, " Device:          %s",
3975d9e6103SBrian Somers                 *p->name.full ?  p->name.full :
3985d9e6103SBrian Somers                 p->type == PHYS_DIRECT ? "unknown" : "N/A");
3995d9e6103SBrian Somers   if (p->session_owner != (pid_t)-1)
4005d9e6103SBrian Somers     prompt_Printf(arg->prompt, " (session owner: %d)", (int)p->session_owner);
4015d9e6103SBrian Somers 
4025d9e6103SBrian Somers   prompt_Printf(arg->prompt, "\n Link Type:       %s\n", mode2Nam(p->type));
4035d9e6103SBrian Somers   prompt_Printf(arg->prompt, " Connect Count:   %d\n", p->connect_count);
4045d9e6103SBrian Somers #ifdef TIOCOUTQ
4055d9e6103SBrian Somers   if (p->fd >= 0 && ioctl(p->fd, TIOCOUTQ, &n) >= 0)
4065d9e6103SBrian Somers       prompt_Printf(arg->prompt, " Physical outq:   %d\n", n);
4075d9e6103SBrian Somers #endif
4085d9e6103SBrian Somers 
4095d9e6103SBrian Somers   prompt_Printf(arg->prompt, " Queued Packets:  %d\n",
4105d9e6103SBrian Somers                 link_QueueLen(&p->link));
4115d9e6103SBrian Somers   prompt_Printf(arg->prompt, " Phone Number:    %s\n", arg->cx->phone.chosen);
4125d9e6103SBrian Somers 
4135d9e6103SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
4145d9e6103SBrian Somers 
4155d9e6103SBrian Somers   prompt_Printf(arg->prompt, " Device List:     ");
4165d9e6103SBrian Somers   dev = p->cfg.devlist;
4175d9e6103SBrian Somers   for (n = 0; n < p->cfg.ndev; n++) {
4185d9e6103SBrian Somers     if (n)
4195d9e6103SBrian Somers       prompt_Printf(arg->prompt, ", ");
4205d9e6103SBrian Somers     prompt_Printf(arg->prompt, "\"%s\"", dev);
4215d9e6103SBrian Somers     dev += strlen(dev) + 1;
4225d9e6103SBrian Somers   }
4235d9e6103SBrian Somers 
4245d9e6103SBrian Somers   prompt_Printf(arg->prompt, "\n Characteristics: ");
4255d9e6103SBrian Somers   if (physical_IsSync(arg->cx->physical))
4265d9e6103SBrian Somers     prompt_Printf(arg->prompt, "sync");
4275d9e6103SBrian Somers   else
4285d9e6103SBrian Somers     prompt_Printf(arg->prompt, "%dbps", p->cfg.speed);
4295d9e6103SBrian Somers 
4305d9e6103SBrian Somers   switch (p->cfg.parity & CSIZE) {
4315d9e6103SBrian Somers   case CS7:
4325d9e6103SBrian Somers     prompt_Printf(arg->prompt, ", cs7");
4335d9e6103SBrian Somers     break;
4345d9e6103SBrian Somers   case CS8:
4355d9e6103SBrian Somers     prompt_Printf(arg->prompt, ", cs8");
4365d9e6103SBrian Somers     break;
4375d9e6103SBrian Somers   }
4385d9e6103SBrian Somers   if (p->cfg.parity & PARENB) {
4395d9e6103SBrian Somers     if (p->cfg.parity & PARODD)
4405d9e6103SBrian Somers       prompt_Printf(arg->prompt, ", odd parity");
4415d9e6103SBrian Somers     else
4425d9e6103SBrian Somers       prompt_Printf(arg->prompt, ", even parity");
4435d9e6103SBrian Somers   } else
4445d9e6103SBrian Somers     prompt_Printf(arg->prompt, ", no parity");
4455d9e6103SBrian Somers 
4465d9e6103SBrian Somers   prompt_Printf(arg->prompt, ", CTS/RTS %s\n", (p->cfg.rts_cts ? "on" : "off"));
4475d9e6103SBrian Somers 
4485d9e6103SBrian Somers   prompt_Printf(arg->prompt, " CD check delay:  %d second%s",
4495d9e6103SBrian Somers                 p->cfg.cd.delay, p->cfg.cd.delay == 1 ? "" : "s");
4505d9e6103SBrian Somers   if (p->cfg.cd.required)
4515d9e6103SBrian Somers     prompt_Printf(arg->prompt, " (required!)\n\n");
4525d9e6103SBrian Somers   else
4535d9e6103SBrian Somers     prompt_Printf(arg->prompt, "\n\n");
4545d9e6103SBrian Somers 
4555d9e6103SBrian Somers   throughput_disp(&p->link.throughput, arg->prompt);
4565d9e6103SBrian Somers 
4575d9e6103SBrian Somers   return 0;
4585d9e6103SBrian Somers }
4595d9e6103SBrian Somers 
4605d9e6103SBrian Somers static void
4615d9e6103SBrian Somers physical_DescriptorRead(struct descriptor *d, struct bundle *bundle,
4625d9e6103SBrian Somers                      const fd_set *fdset)
4635d9e6103SBrian Somers {
4645d9e6103SBrian Somers   struct physical *p = descriptor2physical(d);
4655d9e6103SBrian Somers   u_char *rbuff;
4665d9e6103SBrian Somers   int n, found;
4675d9e6103SBrian Somers 
4685d9e6103SBrian Somers   rbuff = p->input.buf + p->input.sz;
4695d9e6103SBrian Somers 
4705d9e6103SBrian Somers   /* something to read */
4715d9e6103SBrian Somers   n = physical_Read(p, rbuff, sizeof p->input.buf - p->input.sz);
4725d9e6103SBrian Somers   log_Printf(LogDEBUG, "%s: DescriptorRead: read %d/%d from %d\n",
4735d9e6103SBrian Somers              p->link.name, n, (int)(sizeof p->input.buf - p->input.sz), p->fd);
4745d9e6103SBrian Somers   if (n <= 0) {
4755d9e6103SBrian Somers     if (n < 0)
4765d9e6103SBrian Somers       log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd,
4775d9e6103SBrian Somers                  strerror(errno));
4785d9e6103SBrian Somers     else
4795d9e6103SBrian Somers       log_Printf(LogPHASE, "%s: read (%d): Got zero bytes\n",
4805d9e6103SBrian Somers                  p->link.name, p->fd);
4815d9e6103SBrian Somers     datalink_Down(p->dl, CLOSE_NORMAL);
4825d9e6103SBrian Somers     return;
4835d9e6103SBrian Somers   }
4845d9e6103SBrian Somers 
4855d9e6103SBrian Somers   rbuff -= p->input.sz;
4865d9e6103SBrian Somers   n += p->input.sz;
4875d9e6103SBrian Somers 
4885d9e6103SBrian Somers   if (p->link.lcp.fsm.state <= ST_CLOSED) {
4895d9e6103SBrian Somers     if (p->type != PHYS_DEDICATED) {
4905d9e6103SBrian Somers       found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p));
4915d9e6103SBrian Somers       if (rbuff != p->input.buf)
4925d9e6103SBrian Somers         log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf),
4935d9e6103SBrian Somers                          p->input.buf);
4945d9e6103SBrian Somers       p->input.sz = n - (rbuff - p->input.buf);
4955d9e6103SBrian Somers 
4965d9e6103SBrian Somers       if (found) {
4975d9e6103SBrian Somers         /* LCP packet is detected. Turn ourselves into packet mode */
4985d9e6103SBrian Somers         log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n",
4995d9e6103SBrian Somers                    p->link.name);
5005d9e6103SBrian Somers         log_SetTtyCommandMode(p->dl);
5015d9e6103SBrian Somers         datalink_Up(p->dl, 0, 1);
5025d9e6103SBrian Somers         link_PullPacket(&p->link, rbuff, p->input.sz, bundle);
5035d9e6103SBrian Somers         p->input.sz = 0;
5045d9e6103SBrian Somers       } else
5055d9e6103SBrian Somers         bcopy(rbuff, p->input.buf, p->input.sz);
5065d9e6103SBrian Somers     } else
5075d9e6103SBrian Somers       /* In -dedicated mode, we just discard input until LCP is started */
5085d9e6103SBrian Somers       p->input.sz = 0;
5095d9e6103SBrian Somers   } else if (n > 0)
5105d9e6103SBrian Somers     link_PullPacket(&p->link, rbuff, n, bundle);
5115d9e6103SBrian Somers }
5125d9e6103SBrian Somers 
5135d9e6103SBrian Somers struct physical *
5145d9e6103SBrian Somers iov2physical(struct datalink *dl, struct iovec *iov, int *niov, int maxiov,
5155d9e6103SBrian Somers              int fd)
5165d9e6103SBrian Somers {
5175d9e6103SBrian Somers   struct physical *p;
5186815097bSBrian Somers   int len, h, type;
5195d9e6103SBrian Somers 
5205d9e6103SBrian Somers   p = (struct physical *)iov[(*niov)++].iov_base;
5215d9e6103SBrian Somers   p->link.name = dl->name;
5225d9e6103SBrian Somers   throughput_init(&p->link.throughput);
5235d9e6103SBrian Somers   memset(p->link.Queue, '\0', sizeof p->link.Queue);
5245d9e6103SBrian Somers 
5255d9e6103SBrian Somers   p->desc.UpdateSet = physical_UpdateSet;
5265d9e6103SBrian Somers   p->desc.IsSet = physical_IsSet;
5275d9e6103SBrian Somers   p->desc.Read = physical_DescriptorRead;
5285d9e6103SBrian Somers   p->desc.Write = physical_DescriptorWrite;
5295d9e6103SBrian Somers   p->type = PHYS_DIRECT;
5305d9e6103SBrian Somers   p->dl = dl;
5315d9e6103SBrian Somers   len = strlen(_PATH_DEV);
5325d9e6103SBrian Somers   p->name.base = strncmp(p->name.full, _PATH_DEV, len) ?
5335d9e6103SBrian Somers                         p->name.full : p->name.full + len;
5345d9e6103SBrian Somers   p->out = NULL;
5355d9e6103SBrian Somers   p->connect_count = 1;
5365d9e6103SBrian Somers 
5375d9e6103SBrian Somers   p->link.lcp.fsm.bundle = dl->bundle;
5385d9e6103SBrian Somers   p->link.lcp.fsm.link = &p->link;
5395d9e6103SBrian Somers   memset(&p->link.lcp.fsm.FsmTimer, '\0', sizeof p->link.lcp.fsm.FsmTimer);
5405d9e6103SBrian Somers   memset(&p->link.lcp.fsm.OpenTimer, '\0', sizeof p->link.lcp.fsm.OpenTimer);
5415d9e6103SBrian Somers   memset(&p->link.lcp.fsm.StoppedTimer, '\0',
5425d9e6103SBrian Somers          sizeof p->link.lcp.fsm.StoppedTimer);
5435d9e6103SBrian Somers   p->link.lcp.fsm.parent = &dl->fsmp;
5445d9e6103SBrian Somers   lcp_SetupCallbacks(&p->link.lcp);
5455d9e6103SBrian Somers 
5465d9e6103SBrian Somers   p->link.ccp.fsm.bundle = dl->bundle;
5475d9e6103SBrian Somers   p->link.ccp.fsm.link = &p->link;
5485d9e6103SBrian Somers   /* Our in.state & out.state are NULL (no link-level ccp yet) */
5495d9e6103SBrian Somers   memset(&p->link.ccp.fsm.FsmTimer, '\0', sizeof p->link.ccp.fsm.FsmTimer);
5505d9e6103SBrian Somers   memset(&p->link.ccp.fsm.OpenTimer, '\0', sizeof p->link.ccp.fsm.OpenTimer);
5515d9e6103SBrian Somers   memset(&p->link.ccp.fsm.StoppedTimer, '\0',
5525d9e6103SBrian Somers          sizeof p->link.ccp.fsm.StoppedTimer);
5535d9e6103SBrian Somers   p->link.ccp.fsm.parent = &dl->fsmp;
5545d9e6103SBrian Somers   ccp_SetupCallbacks(&p->link.ccp);
5555d9e6103SBrian Somers 
5565d9e6103SBrian Somers   p->hdlc.lqm.owner = &p->link.lcp;
5575d9e6103SBrian Somers   p->hdlc.ReportTimer.state = TIMER_STOPPED;
5585d9e6103SBrian Somers   p->hdlc.lqm.timer.state = TIMER_STOPPED;
5595d9e6103SBrian Somers 
5605d9e6103SBrian Somers   p->fd = fd;
5615d9e6103SBrian Somers 
5625d9e6103SBrian Somers   if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
5635d9e6103SBrian Somers     lqr_reStart(&p->link.lcp);
5645d9e6103SBrian Somers   hdlc_StartTimer(&p->hdlc);
5655d9e6103SBrian Somers 
5665d9e6103SBrian Somers   throughput_start(&p->link.throughput, "physical throughput",
5675d9e6103SBrian Somers                    Enabled(dl->bundle, OPT_THROUGHPUT));
5686815097bSBrian Somers 
5696815097bSBrian Somers   type = (long)p->handler;
5706815097bSBrian Somers   for (h = 0; h < NDEVICES && p->handler == NULL; h++)
5716815097bSBrian Somers     p->handler = (*devices[h].iov2device)(type, p, iov, niov, maxiov);
5726815097bSBrian Somers 
5736815097bSBrian Somers   if (p->handler == NULL)
5746815097bSBrian Somers     physical_SetupStack(p, PHYSICAL_NOFORCE);
5755d9e6103SBrian Somers 
5765d9e6103SBrian Somers   return p;
5775d9e6103SBrian Somers }
5785d9e6103SBrian Somers 
5795d9e6103SBrian Somers int
5805d9e6103SBrian Somers physical2iov(struct physical *p, struct iovec *iov, int *niov, int maxiov,
5815d9e6103SBrian Somers              pid_t newpid)
5825d9e6103SBrian Somers {
5835d9e6103SBrian Somers   if (p) {
5845d9e6103SBrian Somers     hdlc_StopTimer(&p->hdlc);
5855d9e6103SBrian Somers     lqr_StopTimer(p);
5865d9e6103SBrian Somers     timer_Stop(&p->link.lcp.fsm.FsmTimer);
5875d9e6103SBrian Somers     timer_Stop(&p->link.ccp.fsm.FsmTimer);
5885d9e6103SBrian Somers     timer_Stop(&p->link.lcp.fsm.OpenTimer);
5895d9e6103SBrian Somers     timer_Stop(&p->link.ccp.fsm.OpenTimer);
5905d9e6103SBrian Somers     timer_Stop(&p->link.lcp.fsm.StoppedTimer);
5915d9e6103SBrian Somers     timer_Stop(&p->link.ccp.fsm.StoppedTimer);
5926815097bSBrian Somers     if (p->handler) {
5936815097bSBrian Somers       if (p->handler->device2iov)
5946815097bSBrian Somers         (*p->handler->device2iov)(p, iov, niov, maxiov, newpid);
5956815097bSBrian Somers       p->handler = (struct device *)(long)p->handler->type;
5965d9e6103SBrian Somers     }
5976815097bSBrian Somers 
5985d9e6103SBrian Somers     if (tcgetpgrp(p->fd) == getpgrp())
5995d9e6103SBrian Somers       p->session_owner = getpid();      /* So I'll eventually get HUP'd */
6005d9e6103SBrian Somers     timer_Stop(&p->link.throughput.Timer);
6015d9e6103SBrian Somers     physical_ChangedPid(p, newpid);
6025d9e6103SBrian Somers   }
6035d9e6103SBrian Somers 
6045d9e6103SBrian Somers   if (*niov >= maxiov) {
6055d9e6103SBrian Somers     log_Printf(LogERROR, "physical2iov: No room for physical !\n");
6065d9e6103SBrian Somers     if (p)
6075d9e6103SBrian Somers       free(p);
6085d9e6103SBrian Somers     return -1;
6095d9e6103SBrian Somers   }
6105d9e6103SBrian Somers 
6115d9e6103SBrian Somers   iov[*niov].iov_base = p ? p : malloc(sizeof *p);
6125d9e6103SBrian Somers   iov[*niov].iov_len = sizeof *p;
6135d9e6103SBrian Somers   (*niov)++;
6145d9e6103SBrian Somers 
6155d9e6103SBrian Somers   return p ? p->fd : 0;
6165d9e6103SBrian Somers }
6175d9e6103SBrian Somers 
6185d9e6103SBrian Somers void
6195d9e6103SBrian Somers physical_ChangedPid(struct physical *p, pid_t newpid)
6205d9e6103SBrian Somers {
6215d9e6103SBrian Somers   if (p->fd >= 0 && p->type != PHYS_DIRECT) {
6225d9e6103SBrian Somers     int res;
6235d9e6103SBrian Somers 
6245d9e6103SBrian Somers     if ((res = ID0uu_lock_txfr(p->name.base, newpid)) != UU_LOCK_OK)
6255d9e6103SBrian Somers       log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res));
6265d9e6103SBrian Somers   }
6275d9e6103SBrian Somers }
6285d9e6103SBrian Somers 
6295d9e6103SBrian Somers int
6305d9e6103SBrian Somers physical_IsSync(struct physical *p)
6315d9e6103SBrian Somers {
6325d9e6103SBrian Somers    return p->cfg.speed == 0;
6335d9e6103SBrian Somers }
6345d9e6103SBrian Somers 
6355d9e6103SBrian Somers const char *physical_GetDevice(struct physical *p)
6365d9e6103SBrian Somers {
6375d9e6103SBrian Somers    return p->name.full;
63863b73463SBrian Somers }
63963b73463SBrian Somers 
64063b73463SBrian Somers void
641dd7e2610SBrian Somers physical_SetDeviceList(struct physical *p, int argc, const char *const *argv)
64299294c8bSBrian Somers {
64399294c8bSBrian Somers   int f, pos;
64499294c8bSBrian Somers 
64599294c8bSBrian Somers   p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0';
64699294c8bSBrian Somers   for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) {
64799294c8bSBrian Somers     if (pos)
648a8d7acdcSBrian Somers       p->cfg.devlist[pos++] = '\0';
64999294c8bSBrian Somers     strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1);
65099294c8bSBrian Somers     pos += strlen(p->cfg.devlist + pos);
65199294c8bSBrian Somers   }
652a8d7acdcSBrian Somers   p->cfg.ndev = f;
65363b73463SBrian Somers }
65463b73463SBrian Somers 
65563b73463SBrian Somers void
6565d9e6103SBrian Somers physical_SetSync(struct physical *p)
6575d9e6103SBrian Somers {
6585d9e6103SBrian Somers    p->cfg.speed = 0;
65963b73463SBrian Somers }
66063b73463SBrian Somers 
66163b73463SBrian Somers int
6625d9e6103SBrian Somers physical_SetRtsCts(struct physical *p, int enable)
6635d9e6103SBrian Somers {
6645d9e6103SBrian Somers    p->cfg.rts_cts = enable ? 1 : 0;
66563b73463SBrian Somers    return 1;
66663b73463SBrian Somers }
66763b73463SBrian Somers 
66863b73463SBrian Somers ssize_t
6695d9e6103SBrian Somers physical_Read(struct physical *p, void *buf, size_t nbytes)
6705d9e6103SBrian Somers {
6716815097bSBrian Somers   ssize_t ret;
6726815097bSBrian Somers 
6736815097bSBrian Somers   if (p->handler && p->handler->read)
6746815097bSBrian Somers     ret = (*p->handler->read)(p, buf, nbytes);
6756815097bSBrian Somers   else
6766815097bSBrian Somers     ret = read(p->fd, buf, nbytes);
6776815097bSBrian Somers 
6786815097bSBrian Somers   log_DumpBuff(LogPHYSICAL, "read", buf, ret);
6796815097bSBrian Somers 
6806815097bSBrian Somers   return ret;
68163b73463SBrian Somers }
68263b73463SBrian Somers 
68363b73463SBrian Somers ssize_t
6845d9e6103SBrian Somers physical_Write(struct physical *p, const void *buf, size_t nbytes)
6855d9e6103SBrian Somers {
6866815097bSBrian Somers   log_DumpBuff(LogPHYSICAL, "write", buf, nbytes);
6876815097bSBrian Somers 
6886815097bSBrian Somers   if (p->handler && p->handler->write)
6896815097bSBrian Somers     return (*p->handler->write)(p, buf, nbytes);
6906815097bSBrian Somers 
6915d9e6103SBrian Somers   return write(p->fd, buf, nbytes);
69263b73463SBrian Somers }
693ecd5172aSBrian Somers 
694ecd5172aSBrian Somers int
6955d9e6103SBrian Somers physical_doUpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
696b6dec9f0SBrian Somers                      int *n, int force)
69742d4d396SBrian Somers {
69842d4d396SBrian Somers   struct physical *p = descriptor2physical(d);
69942d4d396SBrian Somers   int sets;
70042d4d396SBrian Somers 
70142d4d396SBrian Somers   sets = 0;
702b6dec9f0SBrian Somers   if (p->fd >= 0) {
703b6dec9f0SBrian Somers     if (r) {
704b6dec9f0SBrian Somers       FD_SET(p->fd, r);
70524989c68SBrian Somers       log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd);
706b6dec9f0SBrian Somers       sets++;
707b6dec9f0SBrian Somers     }
708b6dec9f0SBrian Somers     if (e) {
709b6dec9f0SBrian Somers       FD_SET(p->fd, e);
71024989c68SBrian Somers       log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd);
711b6dec9f0SBrian Somers       sets++;
712b6dec9f0SBrian Somers     }
7130f2f3eb3SBrian Somers     if (w && (force || link_QueueLen(&p->link) || p->out)) {
714b6dec9f0SBrian Somers       FD_SET(p->fd, w);
71524989c68SBrian Somers       log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd);
716b6dec9f0SBrian Somers       sets++;
717b6dec9f0SBrian Somers     }
718b6dec9f0SBrian Somers     if (sets && *n < p->fd + 1)
719b6dec9f0SBrian Somers       *n = p->fd + 1;
720b6dec9f0SBrian Somers   }
72142d4d396SBrian Somers 
72242d4d396SBrian Somers   return sets;
72342d4d396SBrian Somers }
72442d4d396SBrian Somers 
72542d4d396SBrian Somers int
726ea722969SBrian Somers physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
727ea722969SBrian Somers {
728ea722969SBrian Somers   int sets;
729ea722969SBrian Somers 
730ea722969SBrian Somers   sets = 0;
731ea722969SBrian Somers   if (p->fd >= 0) {
732ea722969SBrian Somers     if (r && FD_ISSET(p->fd, r)) {
733ea722969SBrian Somers       FD_CLR(p->fd, r);
734ea722969SBrian Somers       log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd);
735ea722969SBrian Somers       sets++;
736ea722969SBrian Somers     }
737ea722969SBrian Somers     if (e && FD_ISSET(p->fd, e)) {
738ea722969SBrian Somers       FD_CLR(p->fd, e);
739ea722969SBrian Somers       log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd);
740ea722969SBrian Somers       sets++;
741ea722969SBrian Somers     }
742ea722969SBrian Somers     if (w && FD_ISSET(p->fd, w)) {
743ea722969SBrian Somers       FD_CLR(p->fd, w);
744ea722969SBrian Somers       log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd);
745ea722969SBrian Somers       sets++;
746ea722969SBrian Somers     }
747ea722969SBrian Somers   }
748ea722969SBrian Somers 
749ea722969SBrian Somers   return sets;
750ea722969SBrian Somers }
751ea722969SBrian Somers 
752ea722969SBrian Somers int
753dd7e2610SBrian Somers physical_IsSet(struct descriptor *d, const fd_set *fdset)
75442d4d396SBrian Somers {
75542d4d396SBrian Somers   struct physical *p = descriptor2physical(d);
75642d4d396SBrian Somers   return p->fd >= 0 && FD_ISSET(p->fd, fdset);
75742d4d396SBrian Somers }
75842d4d396SBrian Somers 
75942d4d396SBrian Somers void
7605d9e6103SBrian Somers physical_Login(struct physical *p, const char *name)
761fc1141b2SBrian Somers {
762229c7625SBrian Somers   if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) {
763fc1141b2SBrian Somers     struct utmp ut;
7641fa75dc1SBrian Somers     const char *connstr;
765fc1141b2SBrian Somers 
766fc1141b2SBrian Somers     memset(&ut, 0, sizeof ut);
767fc1141b2SBrian Somers     time(&ut.ut_time);
768fc1141b2SBrian Somers     strncpy(ut.ut_name, name, sizeof ut.ut_name);
7695d9e6103SBrian Somers     strncpy(ut.ut_line, p->name.base, sizeof ut.ut_line);
7701fa75dc1SBrian Somers     if ((connstr = getenv("CONNECT")))
7711fa75dc1SBrian Somers       /* mgetty sets this to the connection speed */
7721fa75dc1SBrian Somers       strncpy(ut.ut_host, connstr, sizeof ut.ut_host);
773fc1141b2SBrian Somers     ID0login(&ut);
7745d9e6103SBrian Somers     p->Utmp = 1;
775fc1141b2SBrian Somers   }
776fc1141b2SBrian Somers }
777dd0645c5SBrian Somers 
778dd0645c5SBrian Somers int
779dd0645c5SBrian Somers physical_SetMode(struct physical *p, int mode)
780dd0645c5SBrian Somers {
78192b09558SBrian Somers   if ((p->type & (PHYS_DIRECT|PHYS_DEDICATED) ||
78292b09558SBrian Somers        mode & (PHYS_DIRECT|PHYS_DEDICATED)) &&
78392b09558SBrian Somers       (!(p->type & PHYS_DIRECT) || !(mode & PHYS_BACKGROUND))) {
784dd0645c5SBrian Somers     log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name,
785dd0645c5SBrian Somers                mode2Nam(p->type), mode2Nam(mode));
786dd0645c5SBrian Somers     return 0;
787dd0645c5SBrian Somers   }
788dd0645c5SBrian Somers   p->type = mode;
789dd0645c5SBrian Somers   return 1;
790dd0645c5SBrian Somers }
7916f8e9f0aSBrian Somers 
7926f8e9f0aSBrian Somers void
7936f8e9f0aSBrian Somers physical_DeleteQueue(struct physical *p)
7946f8e9f0aSBrian Somers {
7956f8e9f0aSBrian Somers   if (p->out) {
7966f8e9f0aSBrian Somers     mbuf_Free(p->out);
7976f8e9f0aSBrian Somers     p->out = NULL;
7986f8e9f0aSBrian Somers   }
7996f8e9f0aSBrian Somers   link_DeleteQueue(&p->link);
8006f8e9f0aSBrian Somers }
8015d9e6103SBrian Somers 
8025d9e6103SBrian Somers void
8035d9e6103SBrian Somers physical_SetDevice(struct physical *p, const char *name)
8045d9e6103SBrian Somers {
8055d9e6103SBrian Somers   int len = strlen(_PATH_DEV);
8065d9e6103SBrian Somers 
8075d9e6103SBrian Somers   strncpy(p->name.full, name, sizeof p->name.full - 1);
8085d9e6103SBrian Somers   p->name.full[sizeof p->name.full - 1] = '\0';
8095d9e6103SBrian Somers   p->name.base = *p->name.full == '!' ?  p->name.full + 1 :
8105d9e6103SBrian Somers                  strncmp(p->name.full, _PATH_DEV, len) ?
8115d9e6103SBrian Somers                  p->name.full : p->name.full + len;
8125d9e6103SBrian Somers }
8135d9e6103SBrian Somers 
8145d9e6103SBrian Somers static void
8155d9e6103SBrian Somers physical_Found(struct physical *p)
8165d9e6103SBrian Somers {
8179950fb2aSBrian Somers   FILE *lockfile;
8189950fb2aSBrian Somers   char fn[MAXPATHLEN];
8199950fb2aSBrian Somers 
8209950fb2aSBrian Somers   if (*p->name.full == '/') {
8219950fb2aSBrian Somers     snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
8229950fb2aSBrian Somers     lockfile = ID0fopen(fn, "w");
8239950fb2aSBrian Somers     if (lockfile != NULL) {
8249950fb2aSBrian Somers       fprintf(lockfile, "%s%d\n", TUN_NAME, p->dl->bundle->unit);
8259950fb2aSBrian Somers       fclose(lockfile);
8269950fb2aSBrian Somers     }
8279950fb2aSBrian Somers #ifndef RELEASE_CRUNCH
8289950fb2aSBrian Somers     else
8299950fb2aSBrian Somers       log_Printf(LogALERT, "%s: Can't create %s: %s\n",
8309950fb2aSBrian Somers                  p->link.name, fn, strerror(errno));
8319950fb2aSBrian Somers #endif
8329950fb2aSBrian Somers   }
8339950fb2aSBrian Somers 
8345d9e6103SBrian Somers   throughput_start(&p->link.throughput, "physical throughput",
8355d9e6103SBrian Somers                    Enabled(p->dl->bundle, OPT_THROUGHPUT));
8365d9e6103SBrian Somers   p->connect_count++;
8375d9e6103SBrian Somers   p->input.sz = 0;
8385d9e6103SBrian Somers 
8395d9e6103SBrian Somers   log_Printf(LogPHASE, "%s: Connected!\n", p->link.name);
8405d9e6103SBrian Somers }
8415d9e6103SBrian Somers 
8425d9e6103SBrian Somers int
8435d9e6103SBrian Somers physical_Open(struct physical *p, struct bundle *bundle)
8445d9e6103SBrian Somers {
8459950fb2aSBrian Somers   int devno, h, wasopen;
8465d9e6103SBrian Somers   char *dev;
8475d9e6103SBrian Somers 
8485d9e6103SBrian Somers   if (p->fd >= 0)
8495d9e6103SBrian Somers     log_Printf(LogDEBUG, "%s: Open: Modem is already open!\n", p->link.name);
8505d9e6103SBrian Somers     /* We're going back into "term" mode */
8515d9e6103SBrian Somers   else if (p->type == PHYS_DIRECT) {
8525d9e6103SBrian Somers     physical_SetDevice(p, "");
8536815097bSBrian Somers     p->fd = STDIN_FILENO;
8546815097bSBrian Somers     for (h = 0; h < NDEVICES && p->handler == NULL && p->fd >= 0; h++)
8556815097bSBrian Somers         p->handler = (*devices[h].create)(p);
8566815097bSBrian Somers     if (p->fd >= 0) {
85727be3ac6SBrian Somers       if (p->handler == NULL) {
8586815097bSBrian Somers         physical_SetupStack(p, PHYSICAL_NOFORCE);
85927be3ac6SBrian Somers         log_Printf(LogDEBUG, "%s: stdin is unidentified\n", p->link.name);
86027be3ac6SBrian Somers       }
8615d9e6103SBrian Somers       physical_Found(p);
8625d9e6103SBrian Somers     }
8635d9e6103SBrian Somers   } else {
8645d9e6103SBrian Somers     dev = p->cfg.devlist;
8655d9e6103SBrian Somers     devno = 0;
8665d9e6103SBrian Somers     while (devno < p->cfg.ndev && p->fd < 0) {
8675d9e6103SBrian Somers       physical_SetDevice(p, dev);
8689950fb2aSBrian Somers       if (physical_Lock(p)) {
8696815097bSBrian Somers         if (*p->name.full == '/')
8706815097bSBrian Somers           p->fd = ID0open(p->name.full, O_RDWR | O_NONBLOCK);
8716815097bSBrian Somers 
8729950fb2aSBrian Somers         wasopen = p->fd >= 0;
8736815097bSBrian Somers         for (h = 0; h < NDEVICES && p->handler == NULL; h++)
8749950fb2aSBrian Somers           if ((p->handler = (*devices[h].create)(p)) == NULL &&
8759950fb2aSBrian Somers               wasopen && p->fd == -1)
8769950fb2aSBrian Somers             break;
8775d9e6103SBrian Somers 
8789950fb2aSBrian Somers         if (p->fd < 0) {
8799950fb2aSBrian Somers           if (h == NDEVICES)
8805d9e6103SBrian Somers 	    log_Printf(LogWARN, "%s: Device (%s) must begin with a '/',"
8815d9e6103SBrian Somers                        " a '!' or be a host:port pair\n", p->link.name,
8825d9e6103SBrian Somers                        p->name.full);
8839950fb2aSBrian Somers           physical_Unlock(p);
8849950fb2aSBrian Somers         } else
8856815097bSBrian Somers           physical_Found(p);
8869950fb2aSBrian Somers       }
8875d9e6103SBrian Somers       dev += strlen(dev) + 1;
8885d9e6103SBrian Somers       devno++;
8895d9e6103SBrian Somers     }
8905d9e6103SBrian Somers   }
8915d9e6103SBrian Somers 
8925d9e6103SBrian Somers   return p->fd;
8935d9e6103SBrian Somers }
8945d9e6103SBrian Somers 
8955d9e6103SBrian Somers void
8966815097bSBrian Somers physical_SetupStack(struct physical *p, int how)
8975d9e6103SBrian Somers {
8985d9e6103SBrian Somers   link_EmptyStack(&p->link);
8996815097bSBrian Somers   if (how == PHYSICAL_FORCE_SYNC ||
9006815097bSBrian Somers       (how == PHYSICAL_NOFORCE && physical_IsSync(p)))
9015d9e6103SBrian Somers     link_Stack(&p->link, &synclayer);
9025d9e6103SBrian Somers   else {
9035d9e6103SBrian Somers     link_Stack(&p->link, &asynclayer);
9045d9e6103SBrian Somers     link_Stack(&p->link, &hdlclayer);
9055d9e6103SBrian Somers   }
9065d9e6103SBrian Somers   link_Stack(&p->link, &acflayer);
9075d9e6103SBrian Somers   link_Stack(&p->link, &protolayer);
9085d9e6103SBrian Somers   link_Stack(&p->link, &lqrlayer);
9095d9e6103SBrian Somers   link_Stack(&p->link, &ccplayer);
9105d9e6103SBrian Somers   link_Stack(&p->link, &vjlayer);
9115d9e6103SBrian Somers #ifndef NOALIAS
9125d9e6103SBrian Somers   link_Stack(&p->link, &aliaslayer);
9135d9e6103SBrian Somers #endif
9146815097bSBrian Somers   if (how == PHYSICAL_FORCE_ASYNC && physical_IsSync(p)) {
9155d9e6103SBrian Somers     log_Printf(LogWARN, "Sync device setting ignored for ``%s'' device\n",
9165d9e6103SBrian Somers                p->handler ? p->handler->name : "unknown");
9176815097bSBrian Somers     p->cfg.speed = MODEM_SPEED;
9186815097bSBrian Somers   } else if (how == PHYSICAL_FORCE_SYNC && !physical_IsSync(p)) {
9196815097bSBrian Somers     log_Printf(LogWARN, "Async device setting ignored for ``%s'' device\n",
9206815097bSBrian Somers                p->handler ? p->handler->name : "unknown");
9216815097bSBrian Somers     physical_SetSync(p);
9226815097bSBrian Somers   }
9236815097bSBrian Somers }
9246815097bSBrian Somers 
9256815097bSBrian Somers void
9266815097bSBrian Somers physical_StopDeviceTimer(struct physical *p)
9276815097bSBrian Somers {
9286815097bSBrian Somers   if (p->handler && p->handler->stoptimer)
9296815097bSBrian Somers     (*p->handler->stoptimer)(p);
9305d9e6103SBrian Somers }
931