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>
36 #include <sys/tty.h> /* TIOCOUTQ */
41 #if defined(__OpenBSD__) || defined(__NetBSD__)
42 #include <sys/ioctl.h>
65 #include "throughput.h"
69 #include "slcompress.h"
73 #include "descriptor.h"
106 #define PPPOTCPLINE "ppp"
108 static int physical_DescriptorWrite(struct fdescriptor *, struct bundle *,
112 physical_DeviceSize(void)
114 return sizeof(struct device);
118 struct device *(*create)(struct physical *);
119 struct device *(*iov2device)(int, struct physical *, struct iovec *,
120 int *, int, int *, int *);
121 int (*DeviceSize)(void);
125 * This must come before ``tty'' so that the probe routine is
126 * able to identify it as a more specific type of terminal device.
128 { i4b_Create, i4b_iov2device, i4b_DeviceSize },
130 { tty_Create, tty_iov2device, tty_DeviceSize },
133 * This must come before ``udp'' so that the probe routine is
134 * able to identify it as a more specific type of SOCK_DGRAM.
136 { ether_Create, ether_iov2device, ether_DeviceSize },
137 #ifdef EXPERIMENTAL_NETGRAPH
138 { ng_Create, ng_iov2device, ng_DeviceSize },
142 /* Ditto for ATM devices */
143 { atm_Create, atm_iov2device, atm_DeviceSize },
145 { tcp_Create, tcp_iov2device, tcp_DeviceSize },
146 { udp_Create, udp_iov2device, udp_DeviceSize },
147 { exec_Create, exec_iov2device, exec_DeviceSize }
150 #define NDEVICES (sizeof devices / sizeof devices[0])
153 physical_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
156 return physical_doUpdateSet(d, r, w, e, n, 0);
160 physical_SetDescriptor(struct physical *p)
162 p->desc.type = PHYSICAL_DESCRIPTOR;
163 p->desc.UpdateSet = physical_UpdateSet;
164 p->desc.IsSet = physical_IsSet;
165 p->desc.Read = physical_DescriptorRead;
166 p->desc.Write = physical_DescriptorWrite;
170 physical_Create(struct datalink *dl, int type)
174 p = (struct physical *)malloc(sizeof(struct physical));
178 p->link.type = PHYSICAL_LINK;
179 p->link.name = dl->name;
180 p->link.len = sizeof *p;
182 /* The sample period is fixed - see physical2iov() & iov2physical() */
183 throughput_init(&p->link.stats.total, SAMPLE_PERIOD);
184 p->link.stats.parent = dl->bundle->ncp.mp.active ?
185 &dl->bundle->ncp.mp.link.stats.total : NULL;
186 p->link.stats.gather = 1;
188 memset(p->link.Queue, '\0', sizeof p->link.Queue);
189 memset(p->link.proto_in, '\0', sizeof p->link.proto_in);
190 memset(p->link.proto_out, '\0', sizeof p->link.proto_out);
191 link_EmptyStack(&p->link);
194 physical_SetDescriptor(p);
197 hdlc_Init(&p->hdlc, &p->link.lcp);
198 async_Init(&p->async);
202 p->connect_count = 0;
205 *p->name.full = '\0';
206 p->name.base = p->name.full;
209 p->session_owner = (pid_t)-1;
211 p->cfg.rts_cts = MODEM_CTSRTS;
212 p->cfg.speed = MODEM_SPEED;
214 memcpy(p->cfg.devlist, MODEM_LIST, sizeof MODEM_LIST);
215 p->cfg.ndev = NMODEMS;
216 p->cfg.cd.necessity = CD_DEFAULT;
217 p->cfg.cd.delay = 0; /* reconfigured or device specific default */
219 lcp_Init(&p->link.lcp, dl->bundle, &p->link, &dl->fsmp);
220 ccp_Init(&p->link.ccp, dl->bundle, &p->link, &dl->fsmp);
225 static const struct parity {
230 { "even", "P_EVEN", CS7 | PARENB },
231 { "odd", "P_ODD", CS7 | PARENB | PARODD },
232 { "none", "P_ZERO", CS8 },
237 GetParityValue(const char *str)
239 const struct parity *pp;
241 for (pp = validparity; pp->name; pp++) {
242 if (strcasecmp(pp->name, str) == 0 ||
243 strcasecmp(pp->name1, str) == 0) {
251 physical_SetParity(struct physical *p, const char *str)
253 struct termios rstio;
256 val = GetParityValue(str);
260 tcgetattr(p->fd, &rstio);
261 rstio.c_cflag &= ~(CSIZE | PARODD | PARENB);
262 rstio.c_cflag |= val;
263 tcsetattr(p->fd, TCSADRAIN, &rstio);
267 log_Printf(LogWARN, "%s: %s: Invalid parity\n", p->link.name, str);
272 physical_GetSpeed(struct physical *p)
274 if (p->handler && p->handler->speed)
275 return (*p->handler->speed)(p);
281 physical_SetSpeed(struct physical *p, int speed)
283 if (IntToSpeed(speed) != B0) {
284 p->cfg.speed = speed;
292 physical_Raw(struct physical *p)
294 if (p->handler && p->handler->raw)
295 return (*p->handler->raw)(p);
301 physical_Offline(struct physical *p)
303 if (p->handler && p->handler->offline)
304 (*p->handler->offline)(p);
305 log_Printf(LogPHASE, "%s: Disconnected!\n", p->link.name);
309 physical_Lock(struct physical *p)
313 if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
314 (res = ID0uu_lock(p->name.base)) != UU_LOCK_OK) {
315 if (res == UU_LOCK_INUSE)
316 log_Printf(LogPHASE, "%s: %s is in use\n", p->link.name, p->name.full);
318 log_Printf(LogPHASE, "%s: %s is in use: uu_lock: %s\n",
319 p->link.name, p->name.full, uu_lockerr(res));
327 physical_Unlock(struct physical *p)
329 if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
330 ID0uu_unlock(p->name.base) == -1)
331 log_Printf(LogALERT, "%s: Can't uu_unlock %s\n", p->link.name,
336 physical_Close(struct physical *p)
344 log_Printf(LogDEBUG, "%s: Close\n", p->link.name);
346 if (p->handler && p->handler->cooked)
347 (*p->handler->cooked)(p);
349 physical_StopDeviceTimer(p);
351 if (p->handler && (p->handler->type == TCP_DEVICE ||
352 p->handler->type == UDP_DEVICE))
353 /* Careful - we logged in on line ``ppp'' with IP as our host */
354 ID0logout(PPPOTCPLINE, 1);
356 ID0logout(p->name.base, 0);
359 newsid = tcgetpgrp(p->fd) == getpgrp();
362 log_SetTtyCommandMode(p->dl);
364 throughput_stop(&p->link.stats.total);
365 throughput_log(&p->link.stats.total, LogPHASE, p->link.name);
367 if (p->session_owner != (pid_t)-1) {
368 log_Printf(LogPHASE, "%s: HUPing %ld\n", p->link.name,
369 (long)p->session_owner);
370 ID0kill(p->session_owner, SIGHUP);
371 p->session_owner = (pid_t)-1;
375 bundle_setsid(p->dl->bundle, 0);
377 if (*p->name.full == '/') {
378 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
379 #ifndef RELEASE_CRUNCH
380 if (ID0unlink(fn) == -1)
381 log_Printf(LogALERT, "%s: Can't remove %s: %s\n",
382 p->link.name, fn, strerror(errno));
388 if (p->handler && p->handler->destroy)
389 (*p->handler->destroy)(p);
391 p->name.base = p->name.full;
392 *p->name.full = '\0';
396 physical_Destroy(struct physical *p)
399 throughput_destroy(&p->link.stats.total);
404 physical_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle,
407 struct physical *p = descriptor2physical(d);
411 p->out = link_Dequeue(&p->link);
414 nw = physical_Write(p, MBUF_CTOP(p->out), p->out->m_len);
415 log_Printf(LogDEBUG, "%s: DescriptorWrite: wrote %d(%lu) to %d\n",
416 p->link.name, nw, (unsigned long)p->out->m_len, p->fd);
419 p->out->m_offset += nw;
420 if (p->out->m_len == 0)
421 p->out = m_free(p->out);
426 else if (errno != ENOBUFS) {
427 log_Printf(LogPHASE, "%s: write (%d): %s\n", p->link.name,
428 p->fd, strerror(errno));
429 datalink_Down(p->dl, CLOSE_NORMAL);
432 /* else we shouldn't really have been called ! select() is broken ! */
439 physical_ShowStatus(struct cmdargs const *arg)
441 struct physical *p = arg->cx->physical;
446 prompt_Printf(arg->prompt, "Name: %s\n", p->link.name);
447 prompt_Printf(arg->prompt, " State: ");
449 prompt_Printf(arg->prompt, "closed\n");
450 else if (p->handler && p->handler->openinfo)
451 prompt_Printf(arg->prompt, "open (%s)\n", (*p->handler->openinfo)(p));
453 prompt_Printf(arg->prompt, "open\n");
455 prompt_Printf(arg->prompt, " Device: %s",
456 *p->name.full ? p->name.full :
457 p->type == PHYS_DIRECT ? "unknown" : "N/A");
458 if (p->session_owner != (pid_t)-1)
459 prompt_Printf(arg->prompt, " (session owner: %ld)", (long)p->session_owner);
461 prompt_Printf(arg->prompt, "\n Link Type: %s\n", mode2Nam(p->type));
462 prompt_Printf(arg->prompt, " Connect Count: %d\n", p->connect_count);
464 if (p->fd >= 0 && ioctl(p->fd, TIOCOUTQ, &n) >= 0)
465 prompt_Printf(arg->prompt, " Physical outq: %d\n", n);
468 prompt_Printf(arg->prompt, " Queued Packets: %lu\n",
469 (u_long)link_QueueLen(&p->link));
470 prompt_Printf(arg->prompt, " Phone Number: %s\n", arg->cx->phone.chosen);
472 prompt_Printf(arg->prompt, "\nDefaults:\n");
474 prompt_Printf(arg->prompt, " Device List: ");
475 dev = p->cfg.devlist;
476 for (n = 0; n < p->cfg.ndev; n++) {
478 prompt_Printf(arg->prompt, ", ");
479 prompt_Printf(arg->prompt, "\"%s\"", dev);
480 dev += strlen(dev) + 1;
483 prompt_Printf(arg->prompt, "\n Characteristics: ");
484 if (physical_IsSync(arg->cx->physical))
485 prompt_Printf(arg->prompt, "sync");
487 prompt_Printf(arg->prompt, "%dbps", p->cfg.speed);
489 switch (p->cfg.parity & CSIZE) {
491 prompt_Printf(arg->prompt, ", cs7");
494 prompt_Printf(arg->prompt, ", cs8");
497 if (p->cfg.parity & PARENB) {
498 if (p->cfg.parity & PARODD)
499 prompt_Printf(arg->prompt, ", odd parity");
501 prompt_Printf(arg->prompt, ", even parity");
503 prompt_Printf(arg->prompt, ", no parity");
505 prompt_Printf(arg->prompt, ", CTS/RTS %s\n", (p->cfg.rts_cts ? "on" : "off"));
507 prompt_Printf(arg->prompt, " CD check delay: ");
508 cd = p->handler ? &p->handler->cd : &p->cfg.cd;
509 if (cd->necessity == CD_NOTREQUIRED)
510 prompt_Printf(arg->prompt, "no cd");
511 else if (p->cfg.cd.necessity == CD_DEFAULT) {
512 prompt_Printf(arg->prompt, "device specific");
514 prompt_Printf(arg->prompt, "%d second%s", p->cfg.cd.delay,
515 p->cfg.cd.delay == 1 ? "" : "s");
516 if (p->cfg.cd.necessity == CD_REQUIRED)
517 prompt_Printf(arg->prompt, " (required!)");
519 prompt_Printf(arg->prompt, "\n\n");
521 throughput_disp(&p->link.stats.total, arg->prompt);
527 physical_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
530 struct physical *p = descriptor2physical(d);
534 rbuff = p->input.buf + p->input.sz;
536 /* something to read */
537 n = physical_Read(p, rbuff, sizeof p->input.buf - p->input.sz);
538 log_Printf(LogDEBUG, "%s: DescriptorRead: read %d/%d from %d\n",
539 p->link.name, n, (int)(sizeof p->input.buf - p->input.sz), p->fd);
542 log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd,
545 log_Printf(LogPHASE, "%s: read (%d): Got zero bytes\n",
546 p->link.name, p->fd);
547 datalink_Down(p->dl, CLOSE_NORMAL);
551 rbuff -= p->input.sz;
554 if (p->link.lcp.fsm.state <= ST_CLOSED) {
555 if (p->type != PHYS_DEDICATED) {
556 found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p));
557 if (rbuff != p->input.buf)
558 log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf),
560 p->input.sz = n - (rbuff - p->input.buf);
563 /* LCP packet is detected. Turn ourselves into packet mode */
564 log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n",
566 log_SetTtyCommandMode(p->dl);
567 datalink_Up(p->dl, 0, 1);
568 link_PullPacket(&p->link, rbuff, p->input.sz, bundle);
571 bcopy(rbuff, p->input.buf, p->input.sz);
573 /* In -dedicated mode, we just discard input until LCP is started */
576 link_PullPacket(&p->link, rbuff, n, bundle);
580 iov2physical(struct datalink *dl, struct iovec *iov, int *niov, int maxiov,
581 int fd, int *auxfd, int *nauxfd)
586 p = (struct physical *)iov[(*niov)++].iov_base;
587 p->link.name = dl->name;
588 memset(p->link.Queue, '\0', sizeof p->link.Queue);
590 p->desc.UpdateSet = physical_UpdateSet;
591 p->desc.IsSet = physical_IsSet;
592 p->desc.Read = physical_DescriptorRead;
593 p->desc.Write = physical_DescriptorWrite;
594 p->type = PHYS_DIRECT;
596 len = strlen(_PATH_DEV);
598 p->connect_count = 1;
600 physical_SetDevice(p, p->name.full);
602 p->link.lcp.fsm.bundle = dl->bundle;
603 p->link.lcp.fsm.link = &p->link;
604 memset(&p->link.lcp.fsm.FsmTimer, '\0', sizeof p->link.lcp.fsm.FsmTimer);
605 memset(&p->link.lcp.fsm.OpenTimer, '\0', sizeof p->link.lcp.fsm.OpenTimer);
606 memset(&p->link.lcp.fsm.StoppedTimer, '\0',
607 sizeof p->link.lcp.fsm.StoppedTimer);
608 p->link.lcp.fsm.parent = &dl->fsmp;
609 lcp_SetupCallbacks(&p->link.lcp);
611 p->link.ccp.fsm.bundle = dl->bundle;
612 p->link.ccp.fsm.link = &p->link;
613 /* Our in.state & out.state are NULL (no link-level ccp yet) */
614 memset(&p->link.ccp.fsm.FsmTimer, '\0', sizeof p->link.ccp.fsm.FsmTimer);
615 memset(&p->link.ccp.fsm.OpenTimer, '\0', sizeof p->link.ccp.fsm.OpenTimer);
616 memset(&p->link.ccp.fsm.StoppedTimer, '\0',
617 sizeof p->link.ccp.fsm.StoppedTimer);
618 p->link.ccp.fsm.parent = &dl->fsmp;
619 ccp_SetupCallbacks(&p->link.ccp);
621 p->hdlc.lqm.owner = &p->link.lcp;
622 p->hdlc.ReportTimer.state = TIMER_STOPPED;
623 p->hdlc.lqm.timer.state = TIMER_STOPPED;
626 p->link.stats.total.in.SampleOctets = (long long *)iov[(*niov)++].iov_base;
627 p->link.stats.total.out.SampleOctets = (long long *)iov[(*niov)++].iov_base;
628 p->link.stats.parent = dl->bundle->ncp.mp.active ?
629 &dl->bundle->ncp.mp.link.stats.total : NULL;
630 p->link.stats.gather = 1;
632 type = (long)p->handler;
634 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
635 p->handler = (*devices[h].iov2device)(type, p, iov, niov, maxiov,
637 if (p->handler == NULL) {
638 log_Printf(LogPHASE, "%s: Unknown link type\n", p->link.name);
639 free(iov[(*niov)++].iov_base);
640 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
642 log_Printf(LogPHASE, "%s: Device %s, link type is %s\n",
643 p->link.name, p->name.full, p->handler->name);
645 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
646 lqr_reStart(&p->link.lcp);
647 hdlc_StartTimer(&p->hdlc);
649 throughput_restart(&p->link.stats.total, "physical throughput",
650 Enabled(dl->bundle, OPT_THROUGHPUT));
656 physical_MaxDeviceSize()
660 biggest = sizeof(struct device);
661 for (sz = n = 0; n < NDEVICES; n++)
662 if (devices[n].DeviceSize) {
663 sz = (*devices[n].DeviceSize)();
672 physical2iov(struct physical *p, struct iovec *iov, int *niov, int maxiov,
673 int *auxfd, int *nauxfd)
680 hdlc_StopTimer(&p->hdlc);
682 timer_Stop(&p->link.lcp.fsm.FsmTimer);
683 timer_Stop(&p->link.ccp.fsm.FsmTimer);
684 timer_Stop(&p->link.lcp.fsm.OpenTimer);
685 timer_Stop(&p->link.ccp.fsm.OpenTimer);
686 timer_Stop(&p->link.lcp.fsm.StoppedTimer);
687 timer_Stop(&p->link.ccp.fsm.StoppedTimer);
690 p->handler = (struct device *)(long)p->handler->type;
693 if (Enabled(p->dl->bundle, OPT_KEEPSESSION) ||
694 tcgetpgrp(p->fd) == getpgrp())
695 p->session_owner = getpid(); /* So I'll eventually get HUP'd */
697 p->session_owner = (pid_t)-1;
698 timer_Stop(&p->link.stats.total.Timer);
701 if (*niov + 2 >= maxiov) {
702 log_Printf(LogERROR, "physical2iov: No room for physical + throughput"
709 iov[*niov].iov_base = (void *)p;
710 iov[*niov].iov_len = sizeof *p;
713 iov[*niov].iov_base = p ? (void *)p->link.stats.total.in.SampleOctets : NULL;
714 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
716 iov[*niov].iov_base = p ? (void *)p->link.stats.total.out.SampleOctets : NULL;
717 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
720 sz = physical_MaxDeviceSize();
722 if (h && h->device2iov)
723 (*h->device2iov)(h, iov, niov, maxiov, auxfd, nauxfd);
725 iov[*niov].iov_base = malloc(sz);
727 memcpy(iov[*niov].iov_base, h, sizeof *h);
728 iov[*niov].iov_len = sz;
732 iov[*niov].iov_base = NULL;
733 iov[*niov].iov_len = sz;
737 return p ? p->fd : 0;
741 physical_LockedDevice(struct physical *p)
743 if (p->fd >= 0 && *p->name.full == '/' && p->type != PHYS_DIRECT)
750 physical_ChangedPid(struct physical *p, pid_t newpid)
752 if (physical_LockedDevice(p)) {
755 if ((res = ID0uu_lock_txfr(p->name.base, newpid)) != UU_LOCK_OK)
756 log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res));
761 physical_IsSync(struct physical *p)
763 return p->cfg.speed == 0;
767 physical_DeviceMTU(struct physical *p)
769 return p->handler ? p->handler->mtu : 0;
772 const char *physical_GetDevice(struct physical *p)
778 physical_SetDeviceList(struct physical *p, int argc, const char *const *argv)
782 p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0';
783 for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) {
785 p->cfg.devlist[pos++] = '\0';
786 strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1);
787 pos += strlen(p->cfg.devlist + pos);
793 physical_SetSync(struct physical *p)
799 physical_SetRtsCts(struct physical *p, int enable)
801 p->cfg.rts_cts = enable ? 1 : 0;
806 physical_Read(struct physical *p, void *buf, size_t nbytes)
810 if (p->handler && p->handler->read)
811 ret = (*p->handler->read)(p, buf, nbytes);
813 ret = read(p->fd, buf, nbytes);
815 log_DumpBuff(LogPHYSICAL, "read", buf, ret);
821 physical_Write(struct physical *p, const void *buf, size_t nbytes)
823 log_DumpBuff(LogPHYSICAL, "write", buf, nbytes);
825 if (p->handler && p->handler->write)
826 return (*p->handler->write)(p, buf, nbytes);
828 return write(p->fd, buf, nbytes);
832 physical_doUpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
835 struct physical *p = descriptor2physical(d);
842 log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd);
847 log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd);
850 if (w && (force || link_QueueLen(&p->link) || p->out)) {
852 log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd);
855 if (sets && *n < p->fd + 1)
863 physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
865 if (p->handler && p->handler->removefromset)
866 return (*p->handler->removefromset)(p, r, w, e);
872 if (r && FD_ISSET(p->fd, r)) {
874 log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd);
877 if (e && FD_ISSET(p->fd, e)) {
879 log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd);
882 if (w && FD_ISSET(p->fd, w)) {
884 log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd);
894 physical_IsSet(struct fdescriptor *d, const fd_set *fdset)
896 struct physical *p = descriptor2physical(d);
897 return p->fd >= 0 && FD_ISSET(p->fd, fdset);
901 physical_Login(struct physical *p, const char *name)
903 if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) {
908 memset(&ut, 0, sizeof ut);
910 strncpy(ut.ut_name, name, sizeof ut.ut_name);
911 if (p->handler && (p->handler->type == TCP_DEVICE ||
912 p->handler->type == UDP_DEVICE)) {
913 strncpy(ut.ut_line, PPPOTCPLINE, sizeof ut.ut_line);
914 strncpy(ut.ut_host, p->name.base, sizeof ut.ut_host);
915 colon = memchr(ut.ut_host, ':', sizeof ut.ut_host);
919 strncpy(ut.ut_line, p->name.base, sizeof ut.ut_line);
920 if ((connstr = getenv("CONNECT")))
921 /* mgetty sets this to the connection speed */
922 strncpy(ut.ut_host, connstr, sizeof ut.ut_host);
924 p->Utmp = ut.ut_time;
929 physical_SetMode(struct physical *p, int mode)
931 if ((p->type & (PHYS_DIRECT|PHYS_DEDICATED) ||
932 mode & (PHYS_DIRECT|PHYS_DEDICATED)) &&
933 (!(p->type & PHYS_DIRECT) || !(mode & PHYS_BACKGROUND))) {
934 /* Note: The -direct -> -background is for callback ! */
935 log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name,
936 mode2Nam(p->type), mode2Nam(mode));
944 physical_DeleteQueue(struct physical *p)
950 link_DeleteQueue(&p->link);
954 physical_SetDevice(struct physical *p, const char *name)
956 int len = strlen(_PATH_DEV);
958 if (name != p->name.full) {
959 strncpy(p->name.full, name, sizeof p->name.full - 1);
960 p->name.full[sizeof p->name.full - 1] = '\0';
962 p->name.base = *p->name.full == '!' ? p->name.full + 1 :
963 strncmp(p->name.full, _PATH_DEV, len) ?
964 p->name.full : p->name.full + len;
968 physical_Found(struct physical *p)
973 if (*p->name.full == '/') {
974 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
975 lockfile = ID0fopen(fn, "w");
976 if (lockfile != NULL) {
977 fprintf(lockfile, "%s%d\n", TUN_NAME, p->dl->bundle->unit);
980 #ifndef RELEASE_CRUNCH
982 log_Printf(LogALERT, "%s: Can't create %s: %s\n",
983 p->link.name, fn, strerror(errno));
987 throughput_start(&p->link.stats.total, "physical throughput",
988 Enabled(p->dl->bundle, OPT_THROUGHPUT));
992 log_Printf(LogPHASE, "%s: Connected!\n", p->link.name);
996 physical_Open(struct physical *p, struct bundle *bundle)
998 int devno, h, wasfd, err;
1002 log_Printf(LogDEBUG, "%s: Open: Modem is already open!\n", p->link.name);
1003 /* We're going back into "term" mode */
1004 else if (p->type == PHYS_DIRECT) {
1005 physical_SetDevice(p, "");
1006 p->fd = STDIN_FILENO;
1007 for (h = 0; h < NDEVICES && p->handler == NULL && p->fd >= 0; h++)
1008 p->handler = (*devices[h].create)(p);
1010 if (p->handler == NULL) {
1011 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
1012 log_Printf(LogDEBUG, "%s: stdin is unidentified\n", p->link.name);
1017 dev = p->cfg.devlist;
1019 while (devno < p->cfg.ndev && p->fd < 0) {
1020 physical_SetDevice(p, dev);
1021 if (physical_Lock(p)) {
1024 if (*p->name.full == '/') {
1025 p->fd = ID0open(p->name.full, O_RDWR | O_NONBLOCK);
1031 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
1032 if ((p->handler = (*devices[h].create)(p)) == NULL && wasfd != p->fd)
1036 if (h == NDEVICES) {
1038 log_Printf(LogWARN, "%s: %s: %s\n", p->link.name, p->name.full,
1041 log_Printf(LogWARN, "%s: Device (%s) must begin with a '/',"
1042 " a '!' or contain at least one ':'\n", p->link.name,
1049 dev += strlen(dev) + 1;
1058 physical_SetupStack(struct physical *p, const char *who, int how)
1060 link_EmptyStack(&p->link);
1061 if (how == PHYSICAL_FORCE_SYNC || how == PHYSICAL_FORCE_SYNCNOACF ||
1062 (how == PHYSICAL_NOFORCE && physical_IsSync(p)))
1063 link_Stack(&p->link, &synclayer);
1065 link_Stack(&p->link, &asynclayer);
1066 link_Stack(&p->link, &hdlclayer);
1068 if (how != PHYSICAL_FORCE_SYNCNOACF)
1069 link_Stack(&p->link, &acflayer);
1070 link_Stack(&p->link, &protolayer);
1071 link_Stack(&p->link, &lqrlayer);
1072 link_Stack(&p->link, &ccplayer);
1073 link_Stack(&p->link, &vjlayer);
1074 link_Stack(&p->link, &tcpmsslayer);
1076 link_Stack(&p->link, &natlayer);
1078 if (how == PHYSICAL_FORCE_ASYNC && physical_IsSync(p)) {
1079 log_Printf(LogWARN, "Sync device setting ignored for ``%s'' device\n", who);
1080 p->cfg.speed = MODEM_SPEED;
1081 } else if (how == PHYSICAL_FORCE_SYNC && !physical_IsSync(p)) {
1082 log_Printf(LogWARN, "Async device setting ignored for ``%s'' device\n",
1084 physical_SetSync(p);
1089 physical_StopDeviceTimer(struct physical *p)
1091 if (p->handler && p->handler->stoptimer)
1092 (*p->handler->stoptimer)(p);
1096 physical_AwaitCarrier(struct physical *p)
1098 if (p->handler && p->handler->awaitcarrier)
1099 return (*p->handler->awaitcarrier)(p);
1106 physical_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
1108 if (p->handler && p->handler->setasyncparams)
1109 return (*p->handler->setasyncparams)(p, mymap, hismap);
1111 async_SetLinkParams(&p->async, mymap, hismap);
1115 physical_Slot(struct physical *p)
1117 if (p->handler && p->handler->slot)
1118 return (*p->handler->slot)(p);