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");
451 slot = physical_Slot(p);
452 if (p->handler && p->handler->openinfo) {
454 prompt_Printf(arg->prompt, "open (%s)\n", (*p->handler->openinfo)(p));
456 prompt_Printf(arg->prompt, "open (%s, port %d)\n",
457 (*p->handler->openinfo)(p), slot);
458 } else if (slot == -1)
459 prompt_Printf(arg->prompt, "open\n");
461 prompt_Printf(arg->prompt, "open (port %d)\n", slot);
464 prompt_Printf(arg->prompt, " Device: %s",
465 *p->name.full ? p->name.full :
466 p->type == PHYS_DIRECT ? "unknown" : "N/A");
467 if (p->session_owner != (pid_t)-1)
468 prompt_Printf(arg->prompt, " (session owner: %ld)", (long)p->session_owner);
470 prompt_Printf(arg->prompt, "\n Link Type: %s\n", mode2Nam(p->type));
471 prompt_Printf(arg->prompt, " Connect Count: %d\n", p->connect_count);
473 if (p->fd >= 0 && ioctl(p->fd, TIOCOUTQ, &n) >= 0)
474 prompt_Printf(arg->prompt, " Physical outq: %d\n", n);
477 prompt_Printf(arg->prompt, " Queued Packets: %lu\n",
478 (u_long)link_QueueLen(&p->link));
479 prompt_Printf(arg->prompt, " Phone Number: %s\n", arg->cx->phone.chosen);
481 prompt_Printf(arg->prompt, "\nDefaults:\n");
483 prompt_Printf(arg->prompt, " Device List: ");
484 dev = p->cfg.devlist;
485 for (n = 0; n < p->cfg.ndev; n++) {
487 prompt_Printf(arg->prompt, ", ");
488 prompt_Printf(arg->prompt, "\"%s\"", dev);
489 dev += strlen(dev) + 1;
492 prompt_Printf(arg->prompt, "\n Characteristics: ");
493 if (physical_IsSync(arg->cx->physical))
494 prompt_Printf(arg->prompt, "sync");
496 prompt_Printf(arg->prompt, "%dbps", p->cfg.speed);
498 switch (p->cfg.parity & CSIZE) {
500 prompt_Printf(arg->prompt, ", cs7");
503 prompt_Printf(arg->prompt, ", cs8");
506 if (p->cfg.parity & PARENB) {
507 if (p->cfg.parity & PARODD)
508 prompt_Printf(arg->prompt, ", odd parity");
510 prompt_Printf(arg->prompt, ", even parity");
512 prompt_Printf(arg->prompt, ", no parity");
514 prompt_Printf(arg->prompt, ", CTS/RTS %s\n", (p->cfg.rts_cts ? "on" : "off"));
516 prompt_Printf(arg->prompt, " CD check delay: ");
517 cd = p->handler ? &p->handler->cd : &p->cfg.cd;
518 if (cd->necessity == CD_NOTREQUIRED)
519 prompt_Printf(arg->prompt, "no cd");
520 else if (p->cfg.cd.necessity == CD_DEFAULT) {
521 prompt_Printf(arg->prompt, "device specific");
523 prompt_Printf(arg->prompt, "%d second%s", p->cfg.cd.delay,
524 p->cfg.cd.delay == 1 ? "" : "s");
525 if (p->cfg.cd.necessity == CD_REQUIRED)
526 prompt_Printf(arg->prompt, " (required!)");
528 prompt_Printf(arg->prompt, "\n\n");
530 throughput_disp(&p->link.stats.total, arg->prompt);
536 physical_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
539 struct physical *p = descriptor2physical(d);
543 rbuff = p->input.buf + p->input.sz;
545 /* something to read */
546 n = physical_Read(p, rbuff, sizeof p->input.buf - p->input.sz);
547 log_Printf(LogDEBUG, "%s: DescriptorRead: read %d/%d from %d\n",
548 p->link.name, n, (int)(sizeof p->input.buf - p->input.sz), p->fd);
551 log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd,
554 log_Printf(LogPHASE, "%s: read (%d): Got zero bytes\n",
555 p->link.name, p->fd);
556 datalink_Down(p->dl, CLOSE_NORMAL);
560 rbuff -= p->input.sz;
563 if (p->link.lcp.fsm.state <= ST_CLOSED) {
564 if (p->type != PHYS_DEDICATED) {
565 found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p));
566 if (rbuff != p->input.buf)
567 log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf),
569 p->input.sz = n - (rbuff - p->input.buf);
572 /* LCP packet is detected. Turn ourselves into packet mode */
573 log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n",
575 log_SetTtyCommandMode(p->dl);
576 datalink_Up(p->dl, 0, 1);
577 link_PullPacket(&p->link, rbuff, p->input.sz, bundle);
580 bcopy(rbuff, p->input.buf, p->input.sz);
582 /* In -dedicated mode, we just discard input until LCP is started */
585 link_PullPacket(&p->link, rbuff, n, bundle);
589 iov2physical(struct datalink *dl, struct iovec *iov, int *niov, int maxiov,
590 int fd, int *auxfd, int *nauxfd)
595 p = (struct physical *)iov[(*niov)++].iov_base;
596 p->link.name = dl->name;
597 memset(p->link.Queue, '\0', sizeof p->link.Queue);
599 p->desc.UpdateSet = physical_UpdateSet;
600 p->desc.IsSet = physical_IsSet;
601 p->desc.Read = physical_DescriptorRead;
602 p->desc.Write = physical_DescriptorWrite;
603 p->type = PHYS_DIRECT;
605 len = strlen(_PATH_DEV);
607 p->connect_count = 1;
609 physical_SetDevice(p, p->name.full);
611 p->link.lcp.fsm.bundle = dl->bundle;
612 p->link.lcp.fsm.link = &p->link;
613 memset(&p->link.lcp.fsm.FsmTimer, '\0', sizeof p->link.lcp.fsm.FsmTimer);
614 memset(&p->link.lcp.fsm.OpenTimer, '\0', sizeof p->link.lcp.fsm.OpenTimer);
615 memset(&p->link.lcp.fsm.StoppedTimer, '\0',
616 sizeof p->link.lcp.fsm.StoppedTimer);
617 p->link.lcp.fsm.parent = &dl->fsmp;
618 lcp_SetupCallbacks(&p->link.lcp);
620 p->link.ccp.fsm.bundle = dl->bundle;
621 p->link.ccp.fsm.link = &p->link;
622 /* Our in.state & out.state are NULL (no link-level ccp yet) */
623 memset(&p->link.ccp.fsm.FsmTimer, '\0', sizeof p->link.ccp.fsm.FsmTimer);
624 memset(&p->link.ccp.fsm.OpenTimer, '\0', sizeof p->link.ccp.fsm.OpenTimer);
625 memset(&p->link.ccp.fsm.StoppedTimer, '\0',
626 sizeof p->link.ccp.fsm.StoppedTimer);
627 p->link.ccp.fsm.parent = &dl->fsmp;
628 ccp_SetupCallbacks(&p->link.ccp);
630 p->hdlc.lqm.owner = &p->link.lcp;
631 p->hdlc.ReportTimer.state = TIMER_STOPPED;
632 p->hdlc.lqm.timer.state = TIMER_STOPPED;
635 p->link.stats.total.in.SampleOctets = (long long *)iov[(*niov)++].iov_base;
636 p->link.stats.total.out.SampleOctets = (long long *)iov[(*niov)++].iov_base;
637 p->link.stats.parent = dl->bundle->ncp.mp.active ?
638 &dl->bundle->ncp.mp.link.stats.total : NULL;
639 p->link.stats.gather = 1;
641 type = (long)p->handler;
643 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
644 p->handler = (*devices[h].iov2device)(type, p, iov, niov, maxiov,
646 if (p->handler == NULL) {
647 log_Printf(LogPHASE, "%s: Unknown link type\n", p->link.name);
648 free(iov[(*niov)++].iov_base);
649 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
651 log_Printf(LogPHASE, "%s: Device %s, link type is %s\n",
652 p->link.name, p->name.full, p->handler->name);
654 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
655 lqr_reStart(&p->link.lcp);
656 hdlc_StartTimer(&p->hdlc);
658 throughput_restart(&p->link.stats.total, "physical throughput",
659 Enabled(dl->bundle, OPT_THROUGHPUT));
665 physical_MaxDeviceSize()
669 biggest = sizeof(struct device);
670 for (sz = n = 0; n < NDEVICES; n++)
671 if (devices[n].DeviceSize) {
672 sz = (*devices[n].DeviceSize)();
681 physical2iov(struct physical *p, struct iovec *iov, int *niov, int maxiov,
682 int *auxfd, int *nauxfd)
689 hdlc_StopTimer(&p->hdlc);
691 timer_Stop(&p->link.lcp.fsm.FsmTimer);
692 timer_Stop(&p->link.ccp.fsm.FsmTimer);
693 timer_Stop(&p->link.lcp.fsm.OpenTimer);
694 timer_Stop(&p->link.ccp.fsm.OpenTimer);
695 timer_Stop(&p->link.lcp.fsm.StoppedTimer);
696 timer_Stop(&p->link.ccp.fsm.StoppedTimer);
699 p->handler = (struct device *)(long)p->handler->type;
702 if (Enabled(p->dl->bundle, OPT_KEEPSESSION) ||
703 tcgetpgrp(p->fd) == getpgrp())
704 p->session_owner = getpid(); /* So I'll eventually get HUP'd */
706 p->session_owner = (pid_t)-1;
707 timer_Stop(&p->link.stats.total.Timer);
710 if (*niov + 2 >= maxiov) {
711 log_Printf(LogERROR, "physical2iov: No room for physical + throughput"
718 iov[*niov].iov_base = (void *)p;
719 iov[*niov].iov_len = sizeof *p;
722 iov[*niov].iov_base = p ? (void *)p->link.stats.total.in.SampleOctets : NULL;
723 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
725 iov[*niov].iov_base = p ? (void *)p->link.stats.total.out.SampleOctets : NULL;
726 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
729 sz = physical_MaxDeviceSize();
731 if (h && h->device2iov)
732 (*h->device2iov)(h, iov, niov, maxiov, auxfd, nauxfd);
734 iov[*niov].iov_base = malloc(sz);
736 memcpy(iov[*niov].iov_base, h, sizeof *h);
737 iov[*niov].iov_len = sz;
741 iov[*niov].iov_base = NULL;
742 iov[*niov].iov_len = sz;
746 return p ? p->fd : 0;
750 physical_LockedDevice(struct physical *p)
752 if (p->fd >= 0 && *p->name.full == '/' && p->type != PHYS_DIRECT)
759 physical_ChangedPid(struct physical *p, pid_t newpid)
761 if (physical_LockedDevice(p)) {
764 if ((res = ID0uu_lock_txfr(p->name.base, newpid)) != UU_LOCK_OK)
765 log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res));
770 physical_IsSync(struct physical *p)
772 return p->cfg.speed == 0;
776 physical_DeviceMTU(struct physical *p)
778 return p->handler ? p->handler->mtu : 0;
781 const char *physical_GetDevice(struct physical *p)
787 physical_SetDeviceList(struct physical *p, int argc, const char *const *argv)
791 p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0';
792 for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) {
794 p->cfg.devlist[pos++] = '\0';
795 strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1);
796 pos += strlen(p->cfg.devlist + pos);
802 physical_SetSync(struct physical *p)
808 physical_SetRtsCts(struct physical *p, int enable)
810 p->cfg.rts_cts = enable ? 1 : 0;
815 physical_Read(struct physical *p, void *buf, size_t nbytes)
819 if (p->handler && p->handler->read)
820 ret = (*p->handler->read)(p, buf, nbytes);
822 ret = read(p->fd, buf, nbytes);
824 log_DumpBuff(LogPHYSICAL, "read", buf, ret);
830 physical_Write(struct physical *p, const void *buf, size_t nbytes)
832 log_DumpBuff(LogPHYSICAL, "write", buf, nbytes);
834 if (p->handler && p->handler->write)
835 return (*p->handler->write)(p, buf, nbytes);
837 return write(p->fd, buf, nbytes);
841 physical_doUpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
844 struct physical *p = descriptor2physical(d);
851 log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd);
856 log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd);
859 if (w && (force || link_QueueLen(&p->link) || p->out)) {
861 log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd);
864 if (sets && *n < p->fd + 1)
872 physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
874 if (p->handler && p->handler->removefromset)
875 return (*p->handler->removefromset)(p, r, w, e);
881 if (r && FD_ISSET(p->fd, r)) {
883 log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd);
886 if (e && FD_ISSET(p->fd, e)) {
888 log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd);
891 if (w && FD_ISSET(p->fd, w)) {
893 log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd);
903 physical_IsSet(struct fdescriptor *d, const fd_set *fdset)
905 struct physical *p = descriptor2physical(d);
906 return p->fd >= 0 && FD_ISSET(p->fd, fdset);
910 physical_Login(struct physical *p, const char *name)
912 if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) {
917 memset(&ut, 0, sizeof ut);
919 strncpy(ut.ut_name, name, sizeof ut.ut_name);
920 if (p->handler && (p->handler->type == TCP_DEVICE ||
921 p->handler->type == UDP_DEVICE)) {
922 strncpy(ut.ut_line, PPPOTCPLINE, sizeof ut.ut_line);
923 strncpy(ut.ut_host, p->name.base, sizeof ut.ut_host);
924 colon = memchr(ut.ut_host, ':', sizeof ut.ut_host);
928 strncpy(ut.ut_line, p->name.base, sizeof ut.ut_line);
929 if ((connstr = getenv("CONNECT")))
930 /* mgetty sets this to the connection speed */
931 strncpy(ut.ut_host, connstr, sizeof ut.ut_host);
933 p->Utmp = ut.ut_time;
938 physical_SetMode(struct physical *p, int mode)
940 if ((p->type & (PHYS_DIRECT|PHYS_DEDICATED) ||
941 mode & (PHYS_DIRECT|PHYS_DEDICATED)) &&
942 (!(p->type & PHYS_DIRECT) || !(mode & PHYS_BACKGROUND))) {
943 /* Note: The -direct -> -background is for callback ! */
944 log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name,
945 mode2Nam(p->type), mode2Nam(mode));
953 physical_DeleteQueue(struct physical *p)
959 link_DeleteQueue(&p->link);
963 physical_SetDevice(struct physical *p, const char *name)
965 int len = strlen(_PATH_DEV);
967 if (name != p->name.full) {
968 strncpy(p->name.full, name, sizeof p->name.full - 1);
969 p->name.full[sizeof p->name.full - 1] = '\0';
971 p->name.base = *p->name.full == '!' ? p->name.full + 1 :
972 strncmp(p->name.full, _PATH_DEV, len) ?
973 p->name.full : p->name.full + len;
977 physical_Found(struct physical *p)
982 if (*p->name.full == '/') {
983 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
984 lockfile = ID0fopen(fn, "w");
985 if (lockfile != NULL) {
986 fprintf(lockfile, "%s%d\n", TUN_NAME, p->dl->bundle->unit);
989 #ifndef RELEASE_CRUNCH
991 log_Printf(LogALERT, "%s: Can't create %s: %s\n",
992 p->link.name, fn, strerror(errno));
996 throughput_start(&p->link.stats.total, "physical throughput",
997 Enabled(p->dl->bundle, OPT_THROUGHPUT));
1001 log_Printf(LogPHASE, "%s: Connected!\n", p->link.name);
1005 physical_Open(struct physical *p, struct bundle *bundle)
1007 int devno, h, wasfd, err;
1011 log_Printf(LogDEBUG, "%s: Open: Modem is already open!\n", p->link.name);
1012 /* We're going back into "term" mode */
1013 else if (p->type == PHYS_DIRECT) {
1014 physical_SetDevice(p, "");
1015 p->fd = STDIN_FILENO;
1016 for (h = 0; h < NDEVICES && p->handler == NULL && p->fd >= 0; h++)
1017 p->handler = (*devices[h].create)(p);
1019 if (p->handler == NULL) {
1020 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
1021 log_Printf(LogDEBUG, "%s: stdin is unidentified\n", p->link.name);
1026 dev = p->cfg.devlist;
1028 while (devno < p->cfg.ndev && p->fd < 0) {
1029 physical_SetDevice(p, dev);
1030 if (physical_Lock(p)) {
1033 if (*p->name.full == '/') {
1034 p->fd = ID0open(p->name.full, O_RDWR | O_NONBLOCK);
1040 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
1041 if ((p->handler = (*devices[h].create)(p)) == NULL && wasfd != p->fd)
1045 if (h == NDEVICES) {
1047 log_Printf(LogWARN, "%s: %s: %s\n", p->link.name, p->name.full,
1050 log_Printf(LogWARN, "%s: Device (%s) must begin with a '/',"
1051 " a '!' or contain at least one ':'\n", p->link.name,
1058 dev += strlen(dev) + 1;
1067 physical_SetupStack(struct physical *p, const char *who, int how)
1069 link_EmptyStack(&p->link);
1070 if (how == PHYSICAL_FORCE_SYNC || how == PHYSICAL_FORCE_SYNCNOACF ||
1071 (how == PHYSICAL_NOFORCE && physical_IsSync(p)))
1072 link_Stack(&p->link, &synclayer);
1074 link_Stack(&p->link, &asynclayer);
1075 link_Stack(&p->link, &hdlclayer);
1077 if (how != PHYSICAL_FORCE_SYNCNOACF)
1078 link_Stack(&p->link, &acflayer);
1079 link_Stack(&p->link, &protolayer);
1080 link_Stack(&p->link, &lqrlayer);
1081 link_Stack(&p->link, &ccplayer);
1082 link_Stack(&p->link, &vjlayer);
1083 link_Stack(&p->link, &tcpmsslayer);
1085 link_Stack(&p->link, &natlayer);
1087 if (how == PHYSICAL_FORCE_ASYNC && physical_IsSync(p)) {
1088 log_Printf(LogWARN, "Sync device setting ignored for ``%s'' device\n", who);
1089 p->cfg.speed = MODEM_SPEED;
1090 } else if (how == PHYSICAL_FORCE_SYNC && !physical_IsSync(p)) {
1091 log_Printf(LogWARN, "Async device setting ignored for ``%s'' device\n",
1093 physical_SetSync(p);
1098 physical_StopDeviceTimer(struct physical *p)
1100 if (p->handler && p->handler->stoptimer)
1101 (*p->handler->stoptimer)(p);
1105 physical_AwaitCarrier(struct physical *p)
1107 if (p->handler && p->handler->awaitcarrier)
1108 return (*p->handler->awaitcarrier)(p);
1115 physical_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
1117 if (p->handler && p->handler->setasyncparams)
1118 return (*p->handler->setasyncparams)(p, mymap, hismap);
1120 async_SetLinkParams(&p->async, mymap, hismap);
1124 physical_Slot(struct physical *p)
1126 if (p->handler && p->handler->slot)
1127 return (*p->handler->slot)(p);