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>
47 #if defined(__OpenBSD__) || defined(__NetBSD__)
48 #include <sys/ioctl.h>
71 #include "throughput.h"
75 #include "slcompress.h"
79 #include "descriptor.h"
103 #include "netgraph.h"
107 static int physical_DescriptorWrite(struct fdescriptor *, struct bundle *,
111 physical_DeviceSize(void)
113 return sizeof(struct device);
117 struct device *(*create)(struct physical *);
118 struct device *(*iov2device)(int, struct physical *, struct iovec *,
119 int *, int, int *, int *);
120 unsigned (*DeviceSize)(void);
122 { tty_Create, tty_iov2device, tty_DeviceSize },
125 * This must come before ``udp'' so that the probe routine is
126 * able to identify it as a more specific type of SOCK_DGRAM.
128 { ether_Create, ether_iov2device, ether_DeviceSize },
129 #ifdef EXPERIMENTAL_NETGRAPH
130 { ng_Create, ng_iov2device, ng_DeviceSize },
133 { tcp_Create, tcp_iov2device, tcp_DeviceSize },
134 { udp_Create, udp_iov2device, udp_DeviceSize },
135 { exec_Create, exec_iov2device, exec_DeviceSize }
138 #define NDEVICES (sizeof devices / sizeof devices[0])
141 physical_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
144 return physical_doUpdateSet(d, r, w, e, n, 0);
148 physical_SetDescriptor(struct physical *p)
150 p->desc.type = PHYSICAL_DESCRIPTOR;
151 p->desc.UpdateSet = physical_UpdateSet;
152 p->desc.IsSet = physical_IsSet;
153 p->desc.Read = physical_DescriptorRead;
154 p->desc.Write = physical_DescriptorWrite;
158 physical_Create(struct datalink *dl, int type)
162 p = (struct physical *)malloc(sizeof(struct physical));
166 p->link.type = PHYSICAL_LINK;
167 p->link.name = dl->name;
168 p->link.len = sizeof *p;
170 /* The sample period is fixed - see physical2iov() & iov2physical() */
171 throughput_init(&p->link.stats.total, SAMPLE_PERIOD);
172 p->link.stats.parent = dl->bundle->ncp.mp.active ?
173 &dl->bundle->ncp.mp.link.stats.total : NULL;
174 p->link.stats.gather = 1;
176 memset(p->link.Queue, '\0', sizeof p->link.Queue);
177 memset(p->link.proto_in, '\0', sizeof p->link.proto_in);
178 memset(p->link.proto_out, '\0', sizeof p->link.proto_out);
179 link_EmptyStack(&p->link);
182 physical_SetDescriptor(p);
185 hdlc_Init(&p->hdlc, &p->link.lcp);
186 async_Init(&p->async);
190 p->connect_count = 0;
193 *p->name.full = '\0';
194 p->name.base = p->name.full;
197 p->session_owner = (pid_t)-1;
199 p->cfg.rts_cts = MODEM_CTSRTS;
200 p->cfg.speed = MODEM_SPEED;
202 memcpy(p->cfg.devlist, MODEM_LIST, sizeof MODEM_LIST);
203 p->cfg.ndev = NMODEMS;
204 p->cfg.cd.necessity = CD_DEFAULT;
205 p->cfg.cd.delay = 0; /* reconfigured or device specific default */
207 lcp_Init(&p->link.lcp, dl->bundle, &p->link, &dl->fsmp);
208 ccp_Init(&p->link.ccp, dl->bundle, &p->link, &dl->fsmp);
213 static const struct parity {
218 { "even", "P_EVEN", CS7 | PARENB },
219 { "odd", "P_ODD", CS7 | PARENB | PARODD },
220 { "none", "P_ZERO", CS8 },
225 GetParityValue(const char *str)
227 const struct parity *pp;
229 for (pp = validparity; pp->name; pp++) {
230 if (strcasecmp(pp->name, str) == 0 ||
231 strcasecmp(pp->name1, str) == 0) {
239 physical_SetParity(struct physical *p, const char *str)
241 struct termios rstio;
244 val = GetParityValue(str);
248 tcgetattr(p->fd, &rstio);
249 rstio.c_cflag &= ~(CSIZE | PARODD | PARENB);
250 rstio.c_cflag |= val;
251 tcsetattr(p->fd, TCSADRAIN, &rstio);
255 log_Printf(LogWARN, "%s: %s: Invalid parity\n", p->link.name, str);
260 physical_GetSpeed(struct physical *p)
262 if (p->handler && p->handler->speed)
263 return (*p->handler->speed)(p);
269 physical_SetSpeed(struct physical *p, unsigned speed)
271 if (UnsignedToSpeed(speed) != B0) {
272 p->cfg.speed = speed;
280 physical_Raw(struct physical *p)
282 if (p->handler && p->handler->raw)
283 return (*p->handler->raw)(p);
289 physical_Offline(struct physical *p)
291 if (p->handler && p->handler->offline)
292 (*p->handler->offline)(p);
293 log_Printf(LogPHASE, "%s: Disconnected!\n", p->link.name);
297 physical_Lock(struct physical *p)
301 if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
302 (res = ID0uu_lock(p->name.base)) != UU_LOCK_OK) {
303 if (res == UU_LOCK_INUSE)
304 log_Printf(LogPHASE, "%s: %s is in use\n", p->link.name, p->name.full);
306 log_Printf(LogPHASE, "%s: %s is in use: uu_lock: %s\n",
307 p->link.name, p->name.full, uu_lockerr(res));
315 physical_Unlock(struct physical *p)
317 if (*p->name.full == '/' && p->type != PHYS_DIRECT &&
318 ID0uu_unlock(p->name.base) == -1)
319 log_Printf(LogALERT, "%s: Can't uu_unlock %s\n", p->link.name,
324 physical_Close(struct physical *p)
333 log_Printf(LogDEBUG, "%s: Close\n", p->link.name);
335 if (p->handler && p->handler->cooked)
336 (*p->handler->cooked)(p);
338 physical_StopDeviceTimer(p);
340 memset(&ut, 0, sizeof ut);
341 ut.ut_type = DEAD_PROCESS;
342 gettimeofday(&ut.ut_tv, NULL);
343 snprintf(ut.ut_id, sizeof ut.ut_id, "%xppp", (int)getpid());
347 newsid = tcgetpgrp(p->fd) == getpgrp();
350 log_SetTtyCommandMode(p->dl);
352 throughput_stop(&p->link.stats.total);
353 throughput_log(&p->link.stats.total, LogPHASE, p->link.name);
355 if (p->session_owner != (pid_t)-1) {
356 log_Printf(LogPHASE, "%s: HUPing %ld\n", p->link.name,
357 (long)p->session_owner);
358 ID0kill(p->session_owner, SIGHUP);
359 p->session_owner = (pid_t)-1;
363 bundle_setsid(p->dl->bundle, 0);
365 if (*p->name.full == '/') {
366 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
367 #ifndef RELEASE_CRUNCH
368 if (ID0unlink(fn) == -1)
369 log_Printf(LogALERT, "%s: Can't remove %s: %s\n",
370 p->link.name, fn, strerror(errno));
376 if (p->handler && p->handler->destroy)
377 (*p->handler->destroy)(p);
379 p->name.base = p->name.full;
380 *p->name.full = '\0';
384 physical_Destroy(struct physical *p)
387 throughput_destroy(&p->link.stats.total);
392 physical_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle __unused,
393 const fd_set *fdset __unused)
395 struct physical *p = descriptor2physical(d);
399 p->out = link_Dequeue(&p->link);
402 nw = physical_Write(p, MBUF_CTOP(p->out), p->out->m_len);
403 log_Printf(LogDEBUG, "%s: DescriptorWrite: wrote %d(%lu) to %d\n",
404 p->link.name, nw, (unsigned long)p->out->m_len, p->fd);
407 p->out->m_offset += nw;
408 if (p->out->m_len == 0)
409 p->out = m_free(p->out);
414 else if (errno != ENOBUFS) {
415 log_Printf(LogPHASE, "%s: write (fd %d, len %zd): %s\n", p->link.name,
416 p->fd, p->out->m_len, strerror(errno));
417 datalink_Down(p->dl, CLOSE_NORMAL);
420 /* else we shouldn't really have been called ! select() is broken ! */
427 physical_ShowStatus(struct cmdargs const *arg)
429 struct physical *p = arg->cx->physical;
434 prompt_Printf(arg->prompt, "Name: %s\n", p->link.name);
435 prompt_Printf(arg->prompt, " State: ");
437 prompt_Printf(arg->prompt, "closed\n");
439 slot = physical_Slot(p);
440 if (p->handler && p->handler->openinfo) {
442 prompt_Printf(arg->prompt, "open (%s)\n", (*p->handler->openinfo)(p));
444 prompt_Printf(arg->prompt, "open (%s, port %d)\n",
445 (*p->handler->openinfo)(p), slot);
446 } else if (slot == -1)
447 prompt_Printf(arg->prompt, "open\n");
449 prompt_Printf(arg->prompt, "open (port %d)\n", slot);
452 prompt_Printf(arg->prompt, " Device: %s",
453 *p->name.full ? p->name.full :
454 p->type == PHYS_DIRECT ? "unknown" : "N/A");
455 if (p->session_owner != (pid_t)-1)
456 prompt_Printf(arg->prompt, " (session owner: %ld)", (long)p->session_owner);
458 prompt_Printf(arg->prompt, "\n Link Type: %s\n", mode2Nam(p->type));
459 prompt_Printf(arg->prompt, " Connect Count: %d\n", p->connect_count);
461 if (p->fd >= 0 && ioctl(p->fd, TIOCOUTQ, &n) >= 0)
462 prompt_Printf(arg->prompt, " Physical outq: %d\n", n);
465 prompt_Printf(arg->prompt, " Queued Packets: %lu\n",
466 (u_long)link_QueueLen(&p->link));
467 prompt_Printf(arg->prompt, " Phone Number: %s\n", arg->cx->phone.chosen);
469 prompt_Printf(arg->prompt, "\nDefaults:\n");
471 prompt_Printf(arg->prompt, " Device List: ");
472 dev = p->cfg.devlist;
473 for (n = 0; n < p->cfg.ndev; n++) {
475 prompt_Printf(arg->prompt, ", ");
476 prompt_Printf(arg->prompt, "\"%s\"", dev);
477 dev += strlen(dev) + 1;
480 prompt_Printf(arg->prompt, "\n Characteristics: ");
481 if (physical_IsSync(arg->cx->physical))
482 prompt_Printf(arg->prompt, "sync");
484 prompt_Printf(arg->prompt, "%dbps", p->cfg.speed);
486 switch (p->cfg.parity & CSIZE) {
488 prompt_Printf(arg->prompt, ", cs7");
491 prompt_Printf(arg->prompt, ", cs8");
494 if (p->cfg.parity & PARENB) {
495 if (p->cfg.parity & PARODD)
496 prompt_Printf(arg->prompt, ", odd parity");
498 prompt_Printf(arg->prompt, ", even parity");
500 prompt_Printf(arg->prompt, ", no parity");
502 prompt_Printf(arg->prompt, ", CTS/RTS %s\n", (p->cfg.rts_cts ? "on" : "off"));
504 prompt_Printf(arg->prompt, " CD check delay: ");
505 cd = p->handler ? &p->handler->cd : &p->cfg.cd;
506 if (cd->necessity == CD_NOTREQUIRED)
507 prompt_Printf(arg->prompt, "no cd");
508 else if (p->cfg.cd.necessity == CD_DEFAULT) {
509 prompt_Printf(arg->prompt, "device specific");
511 prompt_Printf(arg->prompt, "%d second%s", p->cfg.cd.delay,
512 p->cfg.cd.delay == 1 ? "" : "s");
513 if (p->cfg.cd.necessity == CD_REQUIRED)
514 prompt_Printf(arg->prompt, " (required!)");
516 prompt_Printf(arg->prompt, "\n\n");
518 throughput_disp(&p->link.stats.total, arg->prompt);
524 physical_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
525 const fd_set *fdset __unused)
527 struct physical *p = descriptor2physical(d);
531 rbuff = p->input.buf + p->input.sz;
533 /* something to read */
534 n = physical_Read(p, rbuff, sizeof p->input.buf - p->input.sz);
535 log_Printf(LogDEBUG, "%s: DescriptorRead: read %d/%d from %d\n",
536 p->link.name, n, (int)(sizeof p->input.buf - p->input.sz), p->fd);
539 log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd,
542 log_Printf(LogPHASE, "%s: read (%d): Got zero bytes\n",
543 p->link.name, p->fd);
544 datalink_Down(p->dl, CLOSE_NORMAL);
548 rbuff -= p->input.sz;
551 if (p->link.lcp.fsm.state <= ST_CLOSED) {
552 if (p->type != PHYS_DEDICATED) {
553 found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p));
554 if (rbuff != p->input.buf)
555 log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf),
557 p->input.sz = n - (rbuff - p->input.buf);
560 /* LCP packet is detected. Turn ourselves into packet mode */
561 log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n",
563 log_SetTtyCommandMode(p->dl);
564 datalink_Up(p->dl, 0, 1);
565 link_PullPacket(&p->link, rbuff, p->input.sz, bundle);
568 bcopy(rbuff, p->input.buf, p->input.sz);
570 /* In -dedicated mode, we just discard input until LCP is started */
573 link_PullPacket(&p->link, rbuff, n, bundle);
577 iov2physical(struct datalink *dl, struct iovec *iov, int *niov, int maxiov,
578 int fd, int *auxfd, int *nauxfd)
584 p = (struct physical *)iov[(*niov)++].iov_base;
585 p->link.name = dl->name;
586 memset(p->link.Queue, '\0', sizeof p->link.Queue);
588 p->desc.UpdateSet = physical_UpdateSet;
589 p->desc.IsSet = physical_IsSet;
590 p->desc.Read = physical_DescriptorRead;
591 p->desc.Write = physical_DescriptorWrite;
592 p->type = PHYS_DIRECT;
595 p->connect_count = 1;
597 physical_SetDevice(p, p->name.full);
599 p->link.lcp.fsm.bundle = dl->bundle;
600 p->link.lcp.fsm.link = &p->link;
601 memset(&p->link.lcp.fsm.FsmTimer, '\0', sizeof p->link.lcp.fsm.FsmTimer);
602 memset(&p->link.lcp.fsm.OpenTimer, '\0', sizeof p->link.lcp.fsm.OpenTimer);
603 memset(&p->link.lcp.fsm.StoppedTimer, '\0',
604 sizeof p->link.lcp.fsm.StoppedTimer);
605 p->link.lcp.fsm.parent = &dl->fsmp;
606 lcp_SetupCallbacks(&p->link.lcp);
608 p->link.ccp.fsm.bundle = dl->bundle;
609 p->link.ccp.fsm.link = &p->link;
610 /* Our in.state & out.state are NULL (no link-level ccp yet) */
611 memset(&p->link.ccp.fsm.FsmTimer, '\0', sizeof p->link.ccp.fsm.FsmTimer);
612 memset(&p->link.ccp.fsm.OpenTimer, '\0', sizeof p->link.ccp.fsm.OpenTimer);
613 memset(&p->link.ccp.fsm.StoppedTimer, '\0',
614 sizeof p->link.ccp.fsm.StoppedTimer);
615 p->link.ccp.fsm.parent = &dl->fsmp;
616 ccp_SetupCallbacks(&p->link.ccp);
618 p->hdlc.lqm.owner = &p->link.lcp;
619 p->hdlc.ReportTimer.state = TIMER_STOPPED;
620 p->hdlc.lqm.timer.state = TIMER_STOPPED;
623 p->link.stats.total.in.SampleOctets = (long long *)iov[(*niov)++].iov_base;
624 p->link.stats.total.out.SampleOctets = (long long *)iov[(*niov)++].iov_base;
625 p->link.stats.parent = dl->bundle->ncp.mp.active ?
626 &dl->bundle->ncp.mp.link.stats.total : NULL;
627 p->link.stats.gather = 1;
629 type = (long)p->handler;
631 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
632 p->handler = (*devices[h].iov2device)(type, p, iov, niov, maxiov,
634 if (p->handler == NULL) {
635 log_Printf(LogPHASE, "%s: Unknown link type\n", p->link.name);
636 free(iov[(*niov)++].iov_base);
637 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
639 log_Printf(LogPHASE, "%s: Device %s, link type is %s\n",
640 p->link.name, p->name.full, p->handler->name);
642 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
643 lqr_reStart(&p->link.lcp);
644 hdlc_StartTimer(&p->hdlc);
646 throughput_restart(&p->link.stats.total, "physical throughput",
647 Enabled(dl->bundle, OPT_THROUGHPUT));
653 physical_MaxDeviceSize()
655 unsigned biggest, sz, n;
657 biggest = sizeof(struct device);
658 for (n = 0; n < NDEVICES; n++)
659 if (devices[n].DeviceSize) {
660 sz = (*devices[n].DeviceSize)();
669 physical2iov(struct physical *p, struct iovec *iov, int *niov, int maxiov,
670 int *auxfd, int *nauxfd)
677 hdlc_StopTimer(&p->hdlc);
679 timer_Stop(&p->link.lcp.fsm.FsmTimer);
680 timer_Stop(&p->link.ccp.fsm.FsmTimer);
681 timer_Stop(&p->link.lcp.fsm.OpenTimer);
682 timer_Stop(&p->link.ccp.fsm.OpenTimer);
683 timer_Stop(&p->link.lcp.fsm.StoppedTimer);
684 timer_Stop(&p->link.ccp.fsm.StoppedTimer);
687 p->handler = (struct device *)(long)p->handler->type;
690 if (Enabled(p->dl->bundle, OPT_KEEPSESSION) ||
691 tcgetpgrp(p->fd) == getpgrp())
692 p->session_owner = getpid(); /* So I'll eventually get HUP'd */
694 p->session_owner = (pid_t)-1;
695 timer_Stop(&p->link.stats.total.Timer);
698 if (*niov + 2 >= maxiov) {
699 log_Printf(LogERROR, "physical2iov: No room for physical + throughput"
706 iov[*niov].iov_base = (void *)p;
707 iov[*niov].iov_len = sizeof *p;
710 iov[*niov].iov_base = p ? (void *)p->link.stats.total.in.SampleOctets : NULL;
711 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
713 iov[*niov].iov_base = p ? (void *)p->link.stats.total.out.SampleOctets : NULL;
714 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
717 sz = physical_MaxDeviceSize();
719 if (h && h->device2iov)
720 (*h->device2iov)(h, iov, niov, maxiov, auxfd, nauxfd);
722 if ((iov[*niov].iov_base = malloc(sz)) == NULL) {
723 log_Printf(LogALERT, "physical2iov: Out of memory (%d bytes)\n", sz);
724 AbortProgram(EX_OSERR);
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)
783 p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0';
784 for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) {
786 p->cfg.devlist[pos++] = '\0';
787 strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1);
788 pos += strlen(p->cfg.devlist + pos);
794 physical_SetSync(struct physical *p)
800 physical_SetRtsCts(struct physical *p, int enable)
802 p->cfg.rts_cts = enable ? 1 : 0;
807 physical_Read(struct physical *p, void *buf, size_t nbytes)
811 if (p->handler && p->handler->read)
812 ret = (*p->handler->read)(p, buf, nbytes);
814 ret = read(p->fd, buf, nbytes);
816 log_DumpBuff(LogPHYSICAL, "read", buf, ret);
822 physical_Write(struct physical *p, const void *buf, size_t nbytes)
824 log_DumpBuff(LogPHYSICAL, "write", buf, nbytes);
826 if (p->handler && p->handler->write)
827 return (*p->handler->write)(p, buf, nbytes);
829 return write(p->fd, buf, nbytes);
833 physical_doUpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
836 struct physical *p = descriptor2physical(d);
843 log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd);
848 log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd);
851 if (w && (force || link_QueueLen(&p->link) || p->out)) {
853 log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd);
856 if (sets && *n < p->fd + 1)
864 physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
866 if (p->handler && p->handler->removefromset)
867 return (*p->handler->removefromset)(p, r, w, e);
873 if (r && FD_ISSET(p->fd, r)) {
875 log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd);
878 if (e && FD_ISSET(p->fd, e)) {
880 log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd);
883 if (w && FD_ISSET(p->fd, w)) {
885 log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd);
895 physical_IsSet(struct fdescriptor *d, const fd_set *fdset)
897 struct physical *p = descriptor2physical(d);
898 return p->fd >= 0 && FD_ISSET(p->fd, fdset);
902 physical_Login(struct physical *p, const char *name)
904 if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) {
909 memset(&ut, 0, sizeof ut);
910 ut.ut_type = USER_PROCESS;
911 gettimeofday(&ut.ut_tv, NULL);
912 snprintf(ut.ut_id, sizeof ut.ut_id, "%xppp", (int)getpid());
913 strncpy(ut.ut_user, name, sizeof ut.ut_user);
914 if (p->handler && (p->handler->type == TCP_DEVICE ||
915 p->handler->type == UDP_DEVICE)) {
916 strncpy(ut.ut_host, p->name.base, sizeof ut.ut_host);
917 colon = memchr(ut.ut_host, ':', sizeof ut.ut_host);
921 strncpy(ut.ut_line, p->name.base, sizeof ut.ut_line);
922 if ((connstr = getenv("CONNECT")))
923 /* mgetty sets this to the connection speed */
924 strncpy(ut.ut_host, connstr, sizeof ut.ut_host);
931 physical_SetMode(struct physical *p, int mode)
933 if ((p->type & (PHYS_DIRECT|PHYS_DEDICATED) ||
934 mode & (PHYS_DIRECT|PHYS_DEDICATED)) &&
935 (!(p->type & PHYS_DIRECT) || !(mode & PHYS_BACKGROUND))) {
936 /* Note: The -direct -> -background is for callback ! */
937 log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name,
938 mode2Nam(p->type), mode2Nam(mode));
946 physical_DeleteQueue(struct physical *p)
952 link_DeleteQueue(&p->link);
956 physical_SetDevice(struct physical *p, const char *name)
958 int len = strlen(_PATH_DEV);
960 if (name != p->name.full) {
961 strncpy(p->name.full, name, sizeof p->name.full - 1);
962 p->name.full[sizeof p->name.full - 1] = '\0';
964 p->name.base = *p->name.full == '!' ? p->name.full + 1 :
965 strncmp(p->name.full, _PATH_DEV, len) ?
966 p->name.full : p->name.full + len;
970 physical_Found(struct physical *p)
975 if (*p->name.full == '/') {
976 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
977 lockfile = ID0fopen(fn, "w");
978 if (lockfile != NULL) {
979 fprintf(lockfile, "%s%d\n", TUN_NAME, p->dl->bundle->unit);
982 #ifndef RELEASE_CRUNCH
984 log_Printf(LogALERT, "%s: Can't create %s: %s\n",
985 p->link.name, fn, strerror(errno));
989 throughput_start(&p->link.stats.total, "physical throughput",
990 Enabled(p->dl->bundle, OPT_THROUGHPUT));
994 log_Printf(LogPHASE, "%s: Connected!\n", p->link.name);
998 physical_Open(struct physical *p)
1001 int devno, wasfd, err;
1005 log_Printf(LogDEBUG, "%s: Open: Modem is already open!\n", p->link.name);
1006 /* We're going back into "term" mode */
1007 else if (p->type == PHYS_DIRECT) {
1008 physical_SetDevice(p, "");
1009 p->fd = STDIN_FILENO;
1010 for (h = 0; h < NDEVICES && p->handler == NULL && p->fd >= 0; h++)
1011 p->handler = (*devices[h].create)(p);
1012 close(STDOUT_FILENO);
1014 if (p->handler == NULL) {
1015 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
1016 log_Printf(LogDEBUG, "%s: stdin is unidentified\n", p->link.name);
1021 dev = p->cfg.devlist;
1023 while (devno < p->cfg.ndev && p->fd < 0) {
1024 physical_SetDevice(p, dev);
1025 if (physical_Lock(p)) {
1028 if (*p->name.full == '/') {
1029 p->fd = ID0open(p->name.full, O_RDWR | O_NONBLOCK);
1035 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
1036 if ((p->handler = (*devices[h].create)(p)) == NULL && wasfd != p->fd)
1040 if (h == NDEVICES) {
1042 log_Printf(LogWARN, "%s: %s: %s\n", p->link.name, p->name.full,
1045 log_Printf(LogWARN, "%s: Device (%s) must begin with a '/',"
1046 " a '!' or contain at least one ':'\n", p->link.name,
1053 dev += strlen(dev) + 1;
1062 physical_SetupStack(struct physical *p, const char *who, int how)
1064 link_EmptyStack(&p->link);
1065 if (how == PHYSICAL_FORCE_SYNC || how == PHYSICAL_FORCE_SYNCNOACF ||
1066 (how == PHYSICAL_NOFORCE && physical_IsSync(p)))
1067 link_Stack(&p->link, &synclayer);
1069 link_Stack(&p->link, &asynclayer);
1070 link_Stack(&p->link, &hdlclayer);
1072 if (how != PHYSICAL_FORCE_SYNCNOACF)
1073 link_Stack(&p->link, &acflayer);
1074 link_Stack(&p->link, &protolayer);
1075 link_Stack(&p->link, &lqrlayer);
1076 link_Stack(&p->link, &ccplayer);
1077 link_Stack(&p->link, &vjlayer);
1078 link_Stack(&p->link, &tcpmsslayer);
1080 link_Stack(&p->link, &natlayer);
1082 if (how == PHYSICAL_FORCE_ASYNC && physical_IsSync(p)) {
1083 log_Printf(LogWARN, "Sync device setting ignored for ``%s'' device\n", who);
1084 p->cfg.speed = MODEM_SPEED;
1085 } else if (how == PHYSICAL_FORCE_SYNC && !physical_IsSync(p)) {
1086 log_Printf(LogWARN, "Async device setting ignored for ``%s'' device\n",
1088 physical_SetSync(p);
1093 physical_StopDeviceTimer(struct physical *p)
1095 if (p->handler && p->handler->stoptimer)
1096 (*p->handler->stoptimer)(p);
1100 physical_AwaitCarrier(struct physical *p)
1102 if (p->handler && p->handler->awaitcarrier)
1103 return (*p->handler->awaitcarrier)(p);
1110 physical_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
1112 if (p->handler && p->handler->setasyncparams)
1113 return (*p->handler->setasyncparams)(p, mymap, hismap);
1115 async_SetLinkParams(&p->async, mymap, hismap);
1119 physical_Slot(struct physical *p)
1121 if (p->handler && p->handler->slot)
1122 return (*p->handler->slot)(p);
1128 physical_SetPPPoEnonstandard(struct physical *p, int enable)
1130 p->cfg.nonstandard_pppoe = enable ? 1 : 0;
1131 p->cfg.pppoe_configured = 1;