2 * Written by Eivind Eklund <eivind@yes.no>
5 * Copyright (C) 1998, Yes Interactive. All rights reserved.
7 * Redistribution and use in any form is permitted. Redistribution in
8 * source form should include the above copyright and this set of
9 * conditions, because large sections american law seems to have been
10 * created by a bunch of jerks on drugs that are now illegal, forcing
11 * me to include this copyright-stuff instead of placing this in the
12 * public domain. The name of of 'Yes Interactive' or 'Eivind Eklund'
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 #include <sys/param.h>
24 #include <netinet/in.h>
25 #include <netinet/in_systm.h>
26 #include <netinet/ip.h>
27 #include <sys/socket.h>
40 #include <sys/tty.h> /* TIOCOUTQ */
46 #if defined(__OpenBSD__) || defined(__NetBSD__)
47 #include <sys/ioctl.h>
70 #include "throughput.h"
74 #include "slcompress.h"
78 #include "descriptor.h"
105 #include "netgraph.h"
112 #define PPPOTCPLINE "ppp"
114 static int physical_DescriptorWrite(struct fdescriptor *, struct bundle *,
118 physical_DeviceSize(void)
120 return sizeof(struct device);
124 struct device *(*create)(struct physical *);
125 struct device *(*iov2device)(int, struct physical *, struct iovec *,
126 int *, int, int *, int *);
127 unsigned (*DeviceSize)(void);
131 * This must come before ``tty'' so that the probe routine is
132 * able to identify it as a more specific type of terminal device.
134 { i4b_Create, i4b_iov2device, i4b_DeviceSize },
136 { tty_Create, tty_iov2device, tty_DeviceSize },
139 * This must come before ``udp'' so that the probe routine is
140 * able to identify it as a more specific type of SOCK_DGRAM.
142 { ether_Create, ether_iov2device, ether_DeviceSize },
143 #ifdef EXPERIMENTAL_NETGRAPH
144 { ng_Create, ng_iov2device, ng_DeviceSize },
148 /* Ditto for ATM devices */
149 { atm_Create, atm_iov2device, atm_DeviceSize },
151 { tcp_Create, tcp_iov2device, tcp_DeviceSize },
152 { udp_Create, udp_iov2device, udp_DeviceSize },
153 { exec_Create, exec_iov2device, exec_DeviceSize }
156 #define NDEVICES (sizeof devices / sizeof devices[0])
159 physical_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
162 return physical_doUpdateSet(d, r, w, e, n, 0);
166 physical_SetDescriptor(struct physical *p)
168 p->desc.type = PHYSICAL_DESCRIPTOR;
169 p->desc.UpdateSet = physical_UpdateSet;
170 p->desc.IsSet = physical_IsSet;
171 p->desc.Read = physical_DescriptorRead;
172 p->desc.Write = physical_DescriptorWrite;
176 physical_Create(struct datalink *dl, int type)
180 p = (struct physical *)malloc(sizeof(struct physical));
184 p->link.type = PHYSICAL_LINK;
185 p->link.name = dl->name;
186 p->link.len = sizeof *p;
188 /* The sample period is fixed - see physical2iov() & iov2physical() */
189 throughput_init(&p->link.stats.total, SAMPLE_PERIOD);
190 p->link.stats.parent = dl->bundle->ncp.mp.active ?
191 &dl->bundle->ncp.mp.link.stats.total : NULL;
192 p->link.stats.gather = 1;
194 memset(p->link.Queue, '\0', sizeof p->link.Queue);
195 memset(p->link.proto_in, '\0', sizeof p->link.proto_in);
196 memset(p->link.proto_out, '\0', sizeof p->link.proto_out);
197 link_EmptyStack(&p->link);
200 physical_SetDescriptor(p);
203 hdlc_Init(&p->hdlc, &p->link.lcp);
204 async_Init(&p->async);
208 p->connect_count = 0;
211 *p->name.full = '\0';
212 p->name.base = p->name.full;
215 p->session_owner = (pid_t)-1;
217 p->cfg.rts_cts = MODEM_CTSRTS;
218 p->cfg.speed = MODEM_SPEED;
220 memcpy(p->cfg.devlist, MODEM_LIST, sizeof MODEM_LIST);
221 p->cfg.ndev = NMODEMS;
222 p->cfg.cd.necessity = CD_DEFAULT;
223 p->cfg.cd.delay = 0; /* reconfigured or device specific default */
225 lcp_Init(&p->link.lcp, dl->bundle, &p->link, &dl->fsmp);
226 ccp_Init(&p->link.ccp, dl->bundle, &p->link, &dl->fsmp);
231 static const struct parity {
236 { "even", "P_EVEN", CS7 | PARENB },
237 { "odd", "P_ODD", CS7 | PARENB | PARODD },
238 { "none", "P_ZERO", CS8 },
243 GetParityValue(const char *str)
245 const struct parity *pp;
247 for (pp = validparity; pp->name; pp++) {
248 if (strcasecmp(pp->name, str) == 0 ||
249 strcasecmp(pp->name1, str) == 0) {
257 physical_SetParity(struct physical *p, const char *str)
259 struct termios rstio;
262 val = GetParityValue(str);
266 tcgetattr(p->fd, &rstio);
267 rstio.c_cflag &= ~(CSIZE | PARODD | PARENB);
268 rstio.c_cflag |= val;
269 tcsetattr(p->fd, TCSADRAIN, &rstio);
273 log_Printf(LogWARN, "%s: %s: Invalid parity\n", p->link.name, str);
278 physical_GetSpeed(struct physical *p)
280 if (p->handler && p->handler->speed)
281 return (*p->handler->speed)(p);
287 physical_SetSpeed(struct physical *p, unsigned speed)
289 if (UnsignedToSpeed(speed) != B0) {
290 p->cfg.speed = speed;
298 physical_Raw(struct physical *p)
300 if (p->handler && p->handler->raw)
301 return (*p->handler->raw)(p);
307 physical_Offline(struct physical *p)
309 if (p->handler && p->handler->offline)
310 (*p->handler->offline)(p);
311 log_Printf(LogPHASE, "%s: Disconnected!\n", p->link.name);
315 physical_Lock(struct physical *p)
319 if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
320 (res = ID0uu_lock(p->name.base)) != UU_LOCK_OK) {
321 if (res == UU_LOCK_INUSE)
322 log_Printf(LogPHASE, "%s: %s is in use\n", p->link.name, p->name.full);
324 log_Printf(LogPHASE, "%s: %s is in use: uu_lock: %s\n",
325 p->link.name, p->name.full, uu_lockerr(res));
333 physical_Unlock(struct physical *p)
335 if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
336 ID0uu_unlock(p->name.base) == -1)
337 log_Printf(LogALERT, "%s: Can't uu_unlock %s\n", p->link.name,
342 physical_Close(struct physical *p)
350 log_Printf(LogDEBUG, "%s: Close\n", p->link.name);
352 if (p->handler && p->handler->cooked)
353 (*p->handler->cooked)(p);
355 physical_StopDeviceTimer(p);
357 if (p->handler && (p->handler->type == TCP_DEVICE ||
358 p->handler->type == UDP_DEVICE))
359 /* Careful - we logged in on line ``ppp'' with IP as our host */
360 ID0logout(PPPOTCPLINE, 1);
362 ID0logout(p->name.base, 0);
365 newsid = tcgetpgrp(p->fd) == getpgrp();
368 log_SetTtyCommandMode(p->dl);
370 throughput_stop(&p->link.stats.total);
371 throughput_log(&p->link.stats.total, LogPHASE, p->link.name);
373 if (p->session_owner != (pid_t)-1) {
374 log_Printf(LogPHASE, "%s: HUPing %ld\n", p->link.name,
375 (long)p->session_owner);
376 ID0kill(p->session_owner, SIGHUP);
377 p->session_owner = (pid_t)-1;
381 bundle_setsid(p->dl->bundle, 0);
383 if (*p->name.full == '/') {
384 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
385 #ifndef RELEASE_CRUNCH
386 if (ID0unlink(fn) == -1)
387 log_Printf(LogALERT, "%s: Can't remove %s: %s\n",
388 p->link.name, fn, strerror(errno));
394 if (p->handler && p->handler->destroy)
395 (*p->handler->destroy)(p);
397 p->name.base = p->name.full;
398 *p->name.full = '\0';
402 physical_Destroy(struct physical *p)
405 throughput_destroy(&p->link.stats.total);
410 physical_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle __unused,
411 const fd_set *fdset __unused)
413 struct physical *p = descriptor2physical(d);
417 p->out = link_Dequeue(&p->link);
420 nw = physical_Write(p, MBUF_CTOP(p->out), p->out->m_len);
421 log_Printf(LogDEBUG, "%s: DescriptorWrite: wrote %d(%lu) to %d\n",
422 p->link.name, nw, (unsigned long)p->out->m_len, p->fd);
425 p->out->m_offset += nw;
426 if (p->out->m_len == 0)
427 p->out = m_free(p->out);
432 else if (errno != ENOBUFS) {
433 log_Printf(LogPHASE, "%s: write (fd %d, len %zd): %s\n", p->link.name,
434 p->fd, p->out->m_len, strerror(errno));
435 datalink_Down(p->dl, CLOSE_NORMAL);
438 /* else we shouldn't really have been called ! select() is broken ! */
445 physical_ShowStatus(struct cmdargs const *arg)
447 struct physical *p = arg->cx->physical;
452 prompt_Printf(arg->prompt, "Name: %s\n", p->link.name);
453 prompt_Printf(arg->prompt, " State: ");
455 prompt_Printf(arg->prompt, "closed\n");
457 slot = physical_Slot(p);
458 if (p->handler && p->handler->openinfo) {
460 prompt_Printf(arg->prompt, "open (%s)\n", (*p->handler->openinfo)(p));
462 prompt_Printf(arg->prompt, "open (%s, port %d)\n",
463 (*p->handler->openinfo)(p), slot);
464 } else if (slot == -1)
465 prompt_Printf(arg->prompt, "open\n");
467 prompt_Printf(arg->prompt, "open (port %d)\n", slot);
470 prompt_Printf(arg->prompt, " Device: %s",
471 *p->name.full ? p->name.full :
472 p->type == PHYS_DIRECT ? "unknown" : "N/A");
473 if (p->session_owner != (pid_t)-1)
474 prompt_Printf(arg->prompt, " (session owner: %ld)", (long)p->session_owner);
476 prompt_Printf(arg->prompt, "\n Link Type: %s\n", mode2Nam(p->type));
477 prompt_Printf(arg->prompt, " Connect Count: %d\n", p->connect_count);
479 if (p->fd >= 0 && ioctl(p->fd, TIOCOUTQ, &n) >= 0)
480 prompt_Printf(arg->prompt, " Physical outq: %d\n", n);
483 prompt_Printf(arg->prompt, " Queued Packets: %lu\n",
484 (u_long)link_QueueLen(&p->link));
485 prompt_Printf(arg->prompt, " Phone Number: %s\n", arg->cx->phone.chosen);
487 prompt_Printf(arg->prompt, "\nDefaults:\n");
489 prompt_Printf(arg->prompt, " Device List: ");
490 dev = p->cfg.devlist;
491 for (n = 0; n < p->cfg.ndev; n++) {
493 prompt_Printf(arg->prompt, ", ");
494 prompt_Printf(arg->prompt, "\"%s\"", dev);
495 dev += strlen(dev) + 1;
498 prompt_Printf(arg->prompt, "\n Characteristics: ");
499 if (physical_IsSync(arg->cx->physical))
500 prompt_Printf(arg->prompt, "sync");
502 prompt_Printf(arg->prompt, "%dbps", p->cfg.speed);
504 switch (p->cfg.parity & CSIZE) {
506 prompt_Printf(arg->prompt, ", cs7");
509 prompt_Printf(arg->prompt, ", cs8");
512 if (p->cfg.parity & PARENB) {
513 if (p->cfg.parity & PARODD)
514 prompt_Printf(arg->prompt, ", odd parity");
516 prompt_Printf(arg->prompt, ", even parity");
518 prompt_Printf(arg->prompt, ", no parity");
520 prompt_Printf(arg->prompt, ", CTS/RTS %s\n", (p->cfg.rts_cts ? "on" : "off"));
522 prompt_Printf(arg->prompt, " CD check delay: ");
523 cd = p->handler ? &p->handler->cd : &p->cfg.cd;
524 if (cd->necessity == CD_NOTREQUIRED)
525 prompt_Printf(arg->prompt, "no cd");
526 else if (p->cfg.cd.necessity == CD_DEFAULT) {
527 prompt_Printf(arg->prompt, "device specific");
529 prompt_Printf(arg->prompt, "%d second%s", p->cfg.cd.delay,
530 p->cfg.cd.delay == 1 ? "" : "s");
531 if (p->cfg.cd.necessity == CD_REQUIRED)
532 prompt_Printf(arg->prompt, " (required!)");
534 prompt_Printf(arg->prompt, "\n\n");
536 throughput_disp(&p->link.stats.total, arg->prompt);
542 physical_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
543 const fd_set *fdset __unused)
545 struct physical *p = descriptor2physical(d);
549 rbuff = p->input.buf + p->input.sz;
551 /* something to read */
552 n = physical_Read(p, rbuff, sizeof p->input.buf - p->input.sz);
553 log_Printf(LogDEBUG, "%s: DescriptorRead: read %d/%d from %d\n",
554 p->link.name, n, (int)(sizeof p->input.buf - p->input.sz), p->fd);
557 log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd,
560 log_Printf(LogPHASE, "%s: read (%d): Got zero bytes\n",
561 p->link.name, p->fd);
562 datalink_Down(p->dl, CLOSE_NORMAL);
566 rbuff -= p->input.sz;
569 if (p->link.lcp.fsm.state <= ST_CLOSED) {
570 if (p->type != PHYS_DEDICATED) {
571 found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p));
572 if (rbuff != p->input.buf)
573 log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf),
575 p->input.sz = n - (rbuff - p->input.buf);
578 /* LCP packet is detected. Turn ourselves into packet mode */
579 log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n",
581 log_SetTtyCommandMode(p->dl);
582 datalink_Up(p->dl, 0, 1);
583 link_PullPacket(&p->link, rbuff, p->input.sz, bundle);
586 bcopy(rbuff, p->input.buf, p->input.sz);
588 /* In -dedicated mode, we just discard input until LCP is started */
591 link_PullPacket(&p->link, rbuff, n, bundle);
595 iov2physical(struct datalink *dl, struct iovec *iov, int *niov, int maxiov,
596 int fd, int *auxfd, int *nauxfd)
602 p = (struct physical *)iov[(*niov)++].iov_base;
603 p->link.name = dl->name;
604 memset(p->link.Queue, '\0', sizeof p->link.Queue);
606 p->desc.UpdateSet = physical_UpdateSet;
607 p->desc.IsSet = physical_IsSet;
608 p->desc.Read = physical_DescriptorRead;
609 p->desc.Write = physical_DescriptorWrite;
610 p->type = PHYS_DIRECT;
612 len = strlen(_PATH_DEV);
614 p->connect_count = 1;
616 physical_SetDevice(p, p->name.full);
618 p->link.lcp.fsm.bundle = dl->bundle;
619 p->link.lcp.fsm.link = &p->link;
620 memset(&p->link.lcp.fsm.FsmTimer, '\0', sizeof p->link.lcp.fsm.FsmTimer);
621 memset(&p->link.lcp.fsm.OpenTimer, '\0', sizeof p->link.lcp.fsm.OpenTimer);
622 memset(&p->link.lcp.fsm.StoppedTimer, '\0',
623 sizeof p->link.lcp.fsm.StoppedTimer);
624 p->link.lcp.fsm.parent = &dl->fsmp;
625 lcp_SetupCallbacks(&p->link.lcp);
627 p->link.ccp.fsm.bundle = dl->bundle;
628 p->link.ccp.fsm.link = &p->link;
629 /* Our in.state & out.state are NULL (no link-level ccp yet) */
630 memset(&p->link.ccp.fsm.FsmTimer, '\0', sizeof p->link.ccp.fsm.FsmTimer);
631 memset(&p->link.ccp.fsm.OpenTimer, '\0', sizeof p->link.ccp.fsm.OpenTimer);
632 memset(&p->link.ccp.fsm.StoppedTimer, '\0',
633 sizeof p->link.ccp.fsm.StoppedTimer);
634 p->link.ccp.fsm.parent = &dl->fsmp;
635 ccp_SetupCallbacks(&p->link.ccp);
637 p->hdlc.lqm.owner = &p->link.lcp;
638 p->hdlc.ReportTimer.state = TIMER_STOPPED;
639 p->hdlc.lqm.timer.state = TIMER_STOPPED;
642 p->link.stats.total.in.SampleOctets = (long long *)iov[(*niov)++].iov_base;
643 p->link.stats.total.out.SampleOctets = (long long *)iov[(*niov)++].iov_base;
644 p->link.stats.parent = dl->bundle->ncp.mp.active ?
645 &dl->bundle->ncp.mp.link.stats.total : NULL;
646 p->link.stats.gather = 1;
648 type = (long)p->handler;
650 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
651 p->handler = (*devices[h].iov2device)(type, p, iov, niov, maxiov,
653 if (p->handler == NULL) {
654 log_Printf(LogPHASE, "%s: Unknown link type\n", p->link.name);
655 free(iov[(*niov)++].iov_base);
656 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
658 log_Printf(LogPHASE, "%s: Device %s, link type is %s\n",
659 p->link.name, p->name.full, p->handler->name);
661 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
662 lqr_reStart(&p->link.lcp);
663 hdlc_StartTimer(&p->hdlc);
665 throughput_restart(&p->link.stats.total, "physical throughput",
666 Enabled(dl->bundle, OPT_THROUGHPUT));
672 physical_MaxDeviceSize()
674 unsigned biggest, sz, n;
676 biggest = sizeof(struct device);
677 for (n = 0; n < NDEVICES; n++)
678 if (devices[n].DeviceSize) {
679 sz = (*devices[n].DeviceSize)();
688 physical2iov(struct physical *p, struct iovec *iov, int *niov, int maxiov,
689 int *auxfd, int *nauxfd)
696 hdlc_StopTimer(&p->hdlc);
698 timer_Stop(&p->link.lcp.fsm.FsmTimer);
699 timer_Stop(&p->link.ccp.fsm.FsmTimer);
700 timer_Stop(&p->link.lcp.fsm.OpenTimer);
701 timer_Stop(&p->link.ccp.fsm.OpenTimer);
702 timer_Stop(&p->link.lcp.fsm.StoppedTimer);
703 timer_Stop(&p->link.ccp.fsm.StoppedTimer);
706 p->handler = (struct device *)(long)p->handler->type;
709 if (Enabled(p->dl->bundle, OPT_KEEPSESSION) ||
710 tcgetpgrp(p->fd) == getpgrp())
711 p->session_owner = getpid(); /* So I'll eventually get HUP'd */
713 p->session_owner = (pid_t)-1;
714 timer_Stop(&p->link.stats.total.Timer);
717 if (*niov + 2 >= maxiov) {
718 log_Printf(LogERROR, "physical2iov: No room for physical + throughput"
725 iov[*niov].iov_base = (void *)p;
726 iov[*niov].iov_len = sizeof *p;
729 iov[*niov].iov_base = p ? (void *)p->link.stats.total.in.SampleOctets : NULL;
730 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
732 iov[*niov].iov_base = p ? (void *)p->link.stats.total.out.SampleOctets : NULL;
733 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
736 sz = physical_MaxDeviceSize();
738 if (h && h->device2iov)
739 (*h->device2iov)(h, iov, niov, maxiov, auxfd, nauxfd);
741 if ((iov[*niov].iov_base = malloc(sz)) == NULL) {
742 log_Printf(LogALERT, "physical2iov: Out of memory (%d bytes)\n", sz);
743 AbortProgram(EX_OSERR);
746 memcpy(iov[*niov].iov_base, h, sizeof *h);
747 iov[*niov].iov_len = sz;
751 iov[*niov].iov_base = NULL;
752 iov[*niov].iov_len = sz;
756 return p ? p->fd : 0;
760 physical_LockedDevice(struct physical *p)
762 if (p->fd >= 0 && *p->name.full == '/' && p->type != PHYS_DIRECT)
769 physical_ChangedPid(struct physical *p, pid_t newpid)
771 if (physical_LockedDevice(p)) {
774 if ((res = ID0uu_lock_txfr(p->name.base, newpid)) != UU_LOCK_OK)
775 log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res));
780 physical_IsSync(struct physical *p)
782 return p->cfg.speed == 0;
786 physical_DeviceMTU(struct physical *p)
788 return p->handler ? p->handler->mtu : 0;
791 const char *physical_GetDevice(struct physical *p)
797 physical_SetDeviceList(struct physical *p, int argc, const char *const *argv)
802 p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0';
803 for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) {
805 p->cfg.devlist[pos++] = '\0';
806 strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1);
807 pos += strlen(p->cfg.devlist + pos);
813 physical_SetSync(struct physical *p)
819 physical_SetRtsCts(struct physical *p, int enable)
821 p->cfg.rts_cts = enable ? 1 : 0;
826 physical_Read(struct physical *p, void *buf, size_t nbytes)
830 if (p->handler && p->handler->read)
831 ret = (*p->handler->read)(p, buf, nbytes);
833 ret = read(p->fd, buf, nbytes);
835 log_DumpBuff(LogPHYSICAL, "read", buf, ret);
841 physical_Write(struct physical *p, const void *buf, size_t nbytes)
843 log_DumpBuff(LogPHYSICAL, "write", buf, nbytes);
845 if (p->handler && p->handler->write)
846 return (*p->handler->write)(p, buf, nbytes);
848 return write(p->fd, buf, nbytes);
852 physical_doUpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
855 struct physical *p = descriptor2physical(d);
862 log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd);
867 log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd);
870 if (w && (force || link_QueueLen(&p->link) || p->out)) {
872 log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd);
875 if (sets && *n < p->fd + 1)
883 physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
885 if (p->handler && p->handler->removefromset)
886 return (*p->handler->removefromset)(p, r, w, e);
892 if (r && FD_ISSET(p->fd, r)) {
894 log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd);
897 if (e && FD_ISSET(p->fd, e)) {
899 log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd);
902 if (w && FD_ISSET(p->fd, w)) {
904 log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd);
914 physical_IsSet(struct fdescriptor *d, const fd_set *fdset)
916 struct physical *p = descriptor2physical(d);
917 return p->fd >= 0 && FD_ISSET(p->fd, fdset);
921 physical_Login(struct physical *p, const char *name)
923 if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) {
928 memset(&ut, 0, sizeof ut);
929 ut.ut_time = time(NULL);
930 strncpy(ut.ut_name, name, sizeof ut.ut_name);
931 if (p->handler && (p->handler->type == TCP_DEVICE ||
932 p->handler->type == UDP_DEVICE)) {
933 strncpy(ut.ut_line, PPPOTCPLINE, sizeof ut.ut_line);
934 strncpy(ut.ut_host, p->name.base, sizeof ut.ut_host);
935 colon = memchr(ut.ut_host, ':', sizeof ut.ut_host);
939 strncpy(ut.ut_line, p->name.base, sizeof ut.ut_line);
940 if ((connstr = getenv("CONNECT")))
941 /* mgetty sets this to the connection speed */
942 strncpy(ut.ut_host, connstr, sizeof ut.ut_host);
944 p->Utmp = ut.ut_time;
949 physical_SetMode(struct physical *p, int mode)
951 if ((p->type & (PHYS_DIRECT|PHYS_DEDICATED) ||
952 mode & (PHYS_DIRECT|PHYS_DEDICATED)) &&
953 (!(p->type & PHYS_DIRECT) || !(mode & PHYS_BACKGROUND))) {
954 /* Note: The -direct -> -background is for callback ! */
955 log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name,
956 mode2Nam(p->type), mode2Nam(mode));
964 physical_DeleteQueue(struct physical *p)
970 link_DeleteQueue(&p->link);
974 physical_SetDevice(struct physical *p, const char *name)
976 int len = strlen(_PATH_DEV);
978 if (name != p->name.full) {
979 strncpy(p->name.full, name, sizeof p->name.full - 1);
980 p->name.full[sizeof p->name.full - 1] = '\0';
982 p->name.base = *p->name.full == '!' ? p->name.full + 1 :
983 strncmp(p->name.full, _PATH_DEV, len) ?
984 p->name.full : p->name.full + len;
988 physical_Found(struct physical *p)
993 if (*p->name.full == '/') {
994 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
995 lockfile = ID0fopen(fn, "w");
996 if (lockfile != NULL) {
997 fprintf(lockfile, "%s%d\n", TUN_NAME, p->dl->bundle->unit);
1000 #ifndef RELEASE_CRUNCH
1002 log_Printf(LogALERT, "%s: Can't create %s: %s\n",
1003 p->link.name, fn, strerror(errno));
1007 throughput_start(&p->link.stats.total, "physical throughput",
1008 Enabled(p->dl->bundle, OPT_THROUGHPUT));
1012 log_Printf(LogPHASE, "%s: Connected!\n", p->link.name);
1016 physical_Open(struct physical *p)
1019 int devno, wasfd, err;
1023 log_Printf(LogDEBUG, "%s: Open: Modem is already open!\n", p->link.name);
1024 /* We're going back into "term" mode */
1025 else if (p->type == PHYS_DIRECT) {
1026 physical_SetDevice(p, "");
1027 p->fd = STDIN_FILENO;
1028 for (h = 0; h < NDEVICES && p->handler == NULL && p->fd >= 0; h++)
1029 p->handler = (*devices[h].create)(p);
1031 if (p->handler == NULL) {
1032 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
1033 log_Printf(LogDEBUG, "%s: stdin is unidentified\n", p->link.name);
1038 dev = p->cfg.devlist;
1040 while (devno < p->cfg.ndev && p->fd < 0) {
1041 physical_SetDevice(p, dev);
1042 if (physical_Lock(p)) {
1045 if (*p->name.full == '/') {
1046 p->fd = ID0open(p->name.full, O_RDWR | O_NONBLOCK);
1052 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
1053 if ((p->handler = (*devices[h].create)(p)) == NULL && wasfd != p->fd)
1057 if (h == NDEVICES) {
1059 log_Printf(LogWARN, "%s: %s: %s\n", p->link.name, p->name.full,
1062 log_Printf(LogWARN, "%s: Device (%s) must begin with a '/',"
1063 " a '!' or contain at least one ':'\n", p->link.name,
1070 dev += strlen(dev) + 1;
1079 physical_SetupStack(struct physical *p, const char *who, int how)
1081 link_EmptyStack(&p->link);
1082 if (how == PHYSICAL_FORCE_SYNC || how == PHYSICAL_FORCE_SYNCNOACF ||
1083 (how == PHYSICAL_NOFORCE && physical_IsSync(p)))
1084 link_Stack(&p->link, &synclayer);
1086 link_Stack(&p->link, &asynclayer);
1087 link_Stack(&p->link, &hdlclayer);
1089 if (how != PHYSICAL_FORCE_SYNCNOACF)
1090 link_Stack(&p->link, &acflayer);
1091 link_Stack(&p->link, &protolayer);
1092 link_Stack(&p->link, &lqrlayer);
1093 link_Stack(&p->link, &ccplayer);
1094 link_Stack(&p->link, &vjlayer);
1095 link_Stack(&p->link, &tcpmsslayer);
1097 link_Stack(&p->link, &natlayer);
1099 if (how == PHYSICAL_FORCE_ASYNC && physical_IsSync(p)) {
1100 log_Printf(LogWARN, "Sync device setting ignored for ``%s'' device\n", who);
1101 p->cfg.speed = MODEM_SPEED;
1102 } else if (how == PHYSICAL_FORCE_SYNC && !physical_IsSync(p)) {
1103 log_Printf(LogWARN, "Async device setting ignored for ``%s'' device\n",
1105 physical_SetSync(p);
1110 physical_StopDeviceTimer(struct physical *p)
1112 if (p->handler && p->handler->stoptimer)
1113 (*p->handler->stoptimer)(p);
1117 physical_AwaitCarrier(struct physical *p)
1119 if (p->handler && p->handler->awaitcarrier)
1120 return (*p->handler->awaitcarrier)(p);
1127 physical_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
1129 if (p->handler && p->handler->setasyncparams)
1130 return (*p->handler->setasyncparams)(p, mymap, hismap);
1132 async_SetLinkParams(&p->async, mymap, hismap);
1136 physical_Slot(struct physical *p)
1138 if (p->handler && p->handler->slot)
1139 return (*p->handler->slot)(p);
1145 physical_SetPPPoEnonstandard(struct physical *p, int enable)
1147 p->cfg.nonstandard_pppoe = enable ? 1 : 0;
1148 p->cfg.pppoe_configured = 1;