physical.c revision 47061
1/* 2 * Written by Eivind Eklund <eivind@yes.no> 3 * for Yes Interactive 4 * 5 * Copyright (C) 1998, Yes Interactive. All rights reserved. 6 * 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. 18 * 19 * $Id: physical.c,v 1.10 1999/05/09 20:13:51 brian Exp $ 20 * 21 */ 22 23#include <sys/param.h> 24#include <sys/socket.h> 25#include <netinet/in.h> 26#include <arpa/inet.h> 27#include <netdb.h> 28#include <netinet/in_systm.h> 29#include <netinet/ip.h> 30#include <sys/un.h> 31 32#include <errno.h> 33#include <fcntl.h> 34#include <paths.h> 35#include <stdio.h> 36#include <stdlib.h> 37#include <string.h> 38#include <sys/tty.h> /* TIOCOUTQ */ 39#include <sys/uio.h> 40#include <sys/wait.h> 41#include <time.h> 42#include <unistd.h> 43#include <utmp.h> 44#if defined(__OpenBSD__) || defined(__NetBSD__) 45#include <sys/ioctl.h> 46#include <util.h> 47#else 48#include <libutil.h> 49#endif 50 51#include "layer.h" 52#ifndef NOALIAS 53#include "alias_cmd.h" 54#endif 55#include "proto.h" 56#include "acf.h" 57#include "vjcomp.h" 58#include "defs.h" 59#include "command.h" 60#include "mbuf.h" 61#include "log.h" 62#include "id.h" 63#include "timer.h" 64#include "fsm.h" 65#include "lqr.h" 66#include "hdlc.h" 67#include "lcp.h" 68#include "throughput.h" 69#include "sync.h" 70#include "async.h" 71#include "iplist.h" 72#include "slcompress.h" 73#include "ipcp.h" 74#include "filter.h" 75#include "descriptor.h" 76#include "ccp.h" 77#include "link.h" 78#include "physical.h" 79#include "mp.h" 80#ifndef NORADIUS 81#include "radius.h" 82#endif 83#include "bundle.h" 84#include "prompt.h" 85#include "chat.h" 86#include "auth.h" 87#include "chap.h" 88#include "cbcp.h" 89#include "datalink.h" 90#include "tcp.h" 91#include "udp.h" 92#include "exec.h" 93#include "tty.h" 94 95 96static int physical_DescriptorWrite(struct descriptor *, struct bundle *, 97 const fd_set *); 98static void physical_DescriptorRead(struct descriptor *, struct bundle *, 99 const fd_set *); 100 101struct { 102 struct device *(*create)(struct physical *); 103 struct device *(*iov2device)(int, struct physical *, struct iovec *iov, 104 int *niov, int maxiov); 105} devices[] = { 106 { tty_Create, tty_iov2device }, 107 { tcp_Create, tcp_iov2device }, 108 { udp_Create, udp_iov2device }, 109 { exec_Create, exec_iov2device } 110}; 111 112#define NDEVICES (sizeof devices / sizeof devices[0]) 113 114static int 115physical_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, 116 int *n) 117{ 118 return physical_doUpdateSet(d, r, w, e, n, 0); 119} 120 121struct physical * 122physical_Create(struct datalink *dl, int type) 123{ 124 struct physical *p; 125 126 p = (struct physical *)malloc(sizeof(struct physical)); 127 if (!p) 128 return NULL; 129 130 p->link.type = PHYSICAL_LINK; 131 p->link.name = dl->name; 132 p->link.len = sizeof *p; 133 throughput_init(&p->link.throughput); 134 135 memset(p->link.Queue, '\0', sizeof p->link.Queue); 136 memset(p->link.proto_in, '\0', sizeof p->link.proto_in); 137 memset(p->link.proto_out, '\0', sizeof p->link.proto_out); 138 link_EmptyStack(&p->link); 139 140 p->handler = NULL; 141 p->desc.type = PHYSICAL_DESCRIPTOR; 142 p->desc.UpdateSet = physical_UpdateSet; 143 p->desc.IsSet = physical_IsSet; 144 p->desc.Read = physical_DescriptorRead; 145 p->desc.Write = physical_DescriptorWrite; 146 p->type = type; 147 148 hdlc_Init(&p->hdlc, &p->link.lcp); 149 async_Init(&p->async); 150 151 p->fd = -1; 152 p->out = NULL; 153 p->connect_count = 0; 154 p->dl = dl; 155 p->input.sz = 0; 156 *p->name.full = '\0'; 157 p->name.base = p->name.full; 158 159 p->Utmp = 0; 160 p->session_owner = (pid_t)-1; 161 162 p->cfg.rts_cts = MODEM_CTSRTS; 163 p->cfg.speed = MODEM_SPEED; 164 p->cfg.parity = CS8; 165 memcpy(p->cfg.devlist, MODEM_LIST, sizeof MODEM_LIST); 166 p->cfg.ndev = NMODEMS; 167 p->cfg.cd.required = 0; 168 p->cfg.cd.delay = DEF_CDDELAY; 169 170 lcp_Init(&p->link.lcp, dl->bundle, &p->link, &dl->fsmp); 171 ccp_Init(&p->link.ccp, dl->bundle, &p->link, &dl->fsmp); 172 173 return p; 174} 175 176static const struct parity { 177 const char *name; 178 const char *name1; 179 int set; 180} validparity[] = { 181 { "even", "P_EVEN", CS7 | PARENB }, 182 { "odd", "P_ODD", CS7 | PARENB | PARODD }, 183 { "none", "P_ZERO", CS8 }, 184 { NULL, 0 }, 185}; 186 187static int 188GetParityValue(const char *str) 189{ 190 const struct parity *pp; 191 192 for (pp = validparity; pp->name; pp++) { 193 if (strcasecmp(pp->name, str) == 0 || 194 strcasecmp(pp->name1, str) == 0) { 195 return pp->set; 196 } 197 } 198 return (-1); 199} 200 201int 202physical_SetParity(struct physical *p, const char *str) 203{ 204 struct termios rstio; 205 int val; 206 207 val = GetParityValue(str); 208 if (val > 0) { 209 p->cfg.parity = val; 210 if (p->fd >= 0) { 211 tcgetattr(p->fd, &rstio); 212 rstio.c_cflag &= ~(CSIZE | PARODD | PARENB); 213 rstio.c_cflag |= val; 214 tcsetattr(p->fd, TCSADRAIN, &rstio); 215 } 216 return 0; 217 } 218 log_Printf(LogWARN, "%s: %s: Invalid parity\n", p->link.name, str); 219 return -1; 220} 221 222int 223physical_GetSpeed(struct physical *p) 224{ 225 if (p->handler && p->handler->speed) 226 return (*p->handler->speed)(p); 227 228 return 115200; 229} 230 231int 232physical_SetSpeed(struct physical *p, int speed) 233{ 234 if (IntToSpeed(speed) != B0) { 235 p->cfg.speed = speed; 236 return 1; 237 } 238 239 return 0; 240} 241 242int 243physical_Raw(struct physical *p) 244{ 245 if (p->handler && p->handler->raw) 246 return (*p->handler->raw)(p); 247 248 return 1; 249} 250 251void 252physical_Offline(struct physical *p) 253{ 254 if (p->handler && p->handler->offline) 255 (*p->handler->offline)(p); 256 log_Printf(LogPHASE, "%s: Disconnected!\n", p->link.name); 257} 258 259static void 260physical_ReallyClose(struct physical *p) 261{ 262 int newsid; 263 264 log_Printf(LogDEBUG, "%s: Really close %d\n", p->link.name, p->fd); 265 if (p->fd >= 0) { 266 physical_StopDeviceTimer(p); 267 if (p->Utmp) { 268 ID0logout(p->name.base); 269 p->Utmp = 0; 270 } 271 newsid = tcgetpgrp(p->fd) == getpgrp(); 272 close(p->fd); 273 p->fd = -1; 274 log_SetTtyCommandMode(p->dl); 275 throughput_stop(&p->link.throughput); 276 throughput_log(&p->link.throughput, LogPHASE, p->link.name); 277 if (p->session_owner != (pid_t)-1) { 278 ID0kill(p->session_owner, SIGHUP); 279 p->session_owner = (pid_t)-1; 280 } 281 if (newsid) 282 bundle_setsid(p->dl->bundle, 0); 283 if (p->handler && p->handler->destroy) 284 (*p->handler->destroy)(p); 285 p->handler = NULL; 286 } 287 *p->name.full = '\0'; 288 p->name.base = p->name.full; 289} 290 291void 292physical_Close(struct physical *p) 293{ 294 if (p->fd < 0) 295 return; 296 297 log_Printf(LogDEBUG, "%s: Close\n", p->link.name); 298 299 if (p->handler && p->handler->cooked) 300 (*p->handler->cooked)(p); 301 302 physical_ReallyClose(p); 303} 304 305void 306physical_Destroy(struct physical *p) 307{ 308 physical_Close(p); 309 free(p); 310} 311 312static int 313physical_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 314 const fd_set *fdset) 315{ 316 struct physical *p = descriptor2physical(d); 317 int nw, result = 0; 318 319 if (p->out == NULL) 320 p->out = link_Dequeue(&p->link); 321 322 if (p->out) { 323 nw = physical_Write(p, MBUF_CTOP(p->out), p->out->cnt); 324 log_Printf(LogDEBUG, "%s: DescriptorWrite: wrote %d(%d) to %d\n", 325 p->link.name, nw, p->out->cnt, p->fd); 326 if (nw > 0) { 327 p->out->cnt -= nw; 328 p->out->offset += nw; 329 if (p->out->cnt == 0) 330 p->out = mbuf_FreeSeg(p->out); 331 result = 1; 332 } else if (nw < 0) { 333 if (errno != EAGAIN) { 334 log_Printf(LogPHASE, "%s: write (%d): %s\n", p->link.name, 335 p->fd, strerror(errno)); 336 datalink_Down(p->dl, CLOSE_NORMAL); 337 } 338 result = 1; 339 } 340 /* else we shouldn't really have been called ! select() is broken ! */ 341 } 342 343 return result; 344} 345 346int 347physical_ShowStatus(struct cmdargs const *arg) 348{ 349 struct physical *p = arg->cx->physical; 350 const char *dev; 351 int n; 352 353 prompt_Printf(arg->prompt, "Name: %s\n", p->link.name); 354 prompt_Printf(arg->prompt, " State: "); 355 if (p->fd < 0) 356 prompt_Printf(arg->prompt, "closed\n"); 357 else if (p->handler && p->handler->openinfo) 358 prompt_Printf(arg->prompt, "open (%s)\n", (*p->handler->openinfo)(p)); 359 else 360 prompt_Printf(arg->prompt, "open\n"); 361 362 prompt_Printf(arg->prompt, " Device: %s", 363 *p->name.full ? p->name.full : 364 p->type == PHYS_DIRECT ? "unknown" : "N/A"); 365 if (p->session_owner != (pid_t)-1) 366 prompt_Printf(arg->prompt, " (session owner: %d)", (int)p->session_owner); 367 368 prompt_Printf(arg->prompt, "\n Link Type: %s\n", mode2Nam(p->type)); 369 prompt_Printf(arg->prompt, " Connect Count: %d\n", p->connect_count); 370#ifdef TIOCOUTQ 371 if (p->fd >= 0 && ioctl(p->fd, TIOCOUTQ, &n) >= 0) 372 prompt_Printf(arg->prompt, " Physical outq: %d\n", n); 373#endif 374 375 prompt_Printf(arg->prompt, " Queued Packets: %d\n", 376 link_QueueLen(&p->link)); 377 prompt_Printf(arg->prompt, " Phone Number: %s\n", arg->cx->phone.chosen); 378 379 prompt_Printf(arg->prompt, "\nDefaults:\n"); 380 381 prompt_Printf(arg->prompt, " Device List: "); 382 dev = p->cfg.devlist; 383 for (n = 0; n < p->cfg.ndev; n++) { 384 if (n) 385 prompt_Printf(arg->prompt, ", "); 386 prompt_Printf(arg->prompt, "\"%s\"", dev); 387 dev += strlen(dev) + 1; 388 } 389 390 prompt_Printf(arg->prompt, "\n Characteristics: "); 391 if (physical_IsSync(arg->cx->physical)) 392 prompt_Printf(arg->prompt, "sync"); 393 else 394 prompt_Printf(arg->prompt, "%dbps", p->cfg.speed); 395 396 switch (p->cfg.parity & CSIZE) { 397 case CS7: 398 prompt_Printf(arg->prompt, ", cs7"); 399 break; 400 case CS8: 401 prompt_Printf(arg->prompt, ", cs8"); 402 break; 403 } 404 if (p->cfg.parity & PARENB) { 405 if (p->cfg.parity & PARODD) 406 prompt_Printf(arg->prompt, ", odd parity"); 407 else 408 prompt_Printf(arg->prompt, ", even parity"); 409 } else 410 prompt_Printf(arg->prompt, ", no parity"); 411 412 prompt_Printf(arg->prompt, ", CTS/RTS %s\n", (p->cfg.rts_cts ? "on" : "off")); 413 414 prompt_Printf(arg->prompt, " CD check delay: %d second%s", 415 p->cfg.cd.delay, p->cfg.cd.delay == 1 ? "" : "s"); 416 if (p->cfg.cd.required) 417 prompt_Printf(arg->prompt, " (required!)\n\n"); 418 else 419 prompt_Printf(arg->prompt, "\n\n"); 420 421 throughput_disp(&p->link.throughput, arg->prompt); 422 423 return 0; 424} 425 426static void 427physical_DescriptorRead(struct descriptor *d, struct bundle *bundle, 428 const fd_set *fdset) 429{ 430 struct physical *p = descriptor2physical(d); 431 u_char *rbuff; 432 int n, found; 433 434 rbuff = p->input.buf + p->input.sz; 435 436 /* something to read */ 437 n = physical_Read(p, rbuff, sizeof p->input.buf - p->input.sz); 438 log_Printf(LogDEBUG, "%s: DescriptorRead: read %d/%d from %d\n", 439 p->link.name, n, (int)(sizeof p->input.buf - p->input.sz), p->fd); 440 if (n <= 0) { 441 if (n < 0) 442 log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd, 443 strerror(errno)); 444 else 445 log_Printf(LogPHASE, "%s: read (%d): Got zero bytes\n", 446 p->link.name, p->fd); 447 datalink_Down(p->dl, CLOSE_NORMAL); 448 return; 449 } 450 451 rbuff -= p->input.sz; 452 n += p->input.sz; 453 454 if (p->link.lcp.fsm.state <= ST_CLOSED) { 455 if (p->type != PHYS_DEDICATED) { 456 found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p)); 457 if (rbuff != p->input.buf) 458 log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf), 459 p->input.buf); 460 p->input.sz = n - (rbuff - p->input.buf); 461 462 if (found) { 463 /* LCP packet is detected. Turn ourselves into packet mode */ 464 log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n", 465 p->link.name); 466 log_SetTtyCommandMode(p->dl); 467 datalink_Up(p->dl, 0, 1); 468 link_PullPacket(&p->link, rbuff, p->input.sz, bundle); 469 p->input.sz = 0; 470 } else 471 bcopy(rbuff, p->input.buf, p->input.sz); 472 } else 473 /* In -dedicated mode, we just discard input until LCP is started */ 474 p->input.sz = 0; 475 } else if (n > 0) 476 link_PullPacket(&p->link, rbuff, n, bundle); 477} 478 479struct physical * 480iov2physical(struct datalink *dl, struct iovec *iov, int *niov, int maxiov, 481 int fd) 482{ 483 struct physical *p; 484 int len, h, type; 485 486 p = (struct physical *)iov[(*niov)++].iov_base; 487 p->link.name = dl->name; 488 throughput_init(&p->link.throughput); 489 memset(p->link.Queue, '\0', sizeof p->link.Queue); 490 491 p->desc.UpdateSet = physical_UpdateSet; 492 p->desc.IsSet = physical_IsSet; 493 p->desc.Read = physical_DescriptorRead; 494 p->desc.Write = physical_DescriptorWrite; 495 p->type = PHYS_DIRECT; 496 p->dl = dl; 497 len = strlen(_PATH_DEV); 498 p->name.base = strncmp(p->name.full, _PATH_DEV, len) ? 499 p->name.full : p->name.full + len; 500 p->out = NULL; 501 p->connect_count = 1; 502 503 p->link.lcp.fsm.bundle = dl->bundle; 504 p->link.lcp.fsm.link = &p->link; 505 memset(&p->link.lcp.fsm.FsmTimer, '\0', sizeof p->link.lcp.fsm.FsmTimer); 506 memset(&p->link.lcp.fsm.OpenTimer, '\0', sizeof p->link.lcp.fsm.OpenTimer); 507 memset(&p->link.lcp.fsm.StoppedTimer, '\0', 508 sizeof p->link.lcp.fsm.StoppedTimer); 509 p->link.lcp.fsm.parent = &dl->fsmp; 510 lcp_SetupCallbacks(&p->link.lcp); 511 512 p->link.ccp.fsm.bundle = dl->bundle; 513 p->link.ccp.fsm.link = &p->link; 514 /* Our in.state & out.state are NULL (no link-level ccp yet) */ 515 memset(&p->link.ccp.fsm.FsmTimer, '\0', sizeof p->link.ccp.fsm.FsmTimer); 516 memset(&p->link.ccp.fsm.OpenTimer, '\0', sizeof p->link.ccp.fsm.OpenTimer); 517 memset(&p->link.ccp.fsm.StoppedTimer, '\0', 518 sizeof p->link.ccp.fsm.StoppedTimer); 519 p->link.ccp.fsm.parent = &dl->fsmp; 520 ccp_SetupCallbacks(&p->link.ccp); 521 522 p->hdlc.lqm.owner = &p->link.lcp; 523 p->hdlc.ReportTimer.state = TIMER_STOPPED; 524 p->hdlc.lqm.timer.state = TIMER_STOPPED; 525 526 p->fd = fd; 527 528 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load) 529 lqr_reStart(&p->link.lcp); 530 hdlc_StartTimer(&p->hdlc); 531 532 throughput_start(&p->link.throughput, "physical throughput", 533 Enabled(dl->bundle, OPT_THROUGHPUT)); 534 535 type = (long)p->handler; 536 for (h = 0; h < NDEVICES && p->handler == NULL; h++) 537 p->handler = (*devices[h].iov2device)(type, p, iov, niov, maxiov); 538 539 if (p->handler == NULL) 540 physical_SetupStack(p, PHYSICAL_NOFORCE); 541 542 return p; 543} 544 545int 546physical2iov(struct physical *p, struct iovec *iov, int *niov, int maxiov, 547 pid_t newpid) 548{ 549 if (p) { 550 hdlc_StopTimer(&p->hdlc); 551 lqr_StopTimer(p); 552 timer_Stop(&p->link.lcp.fsm.FsmTimer); 553 timer_Stop(&p->link.ccp.fsm.FsmTimer); 554 timer_Stop(&p->link.lcp.fsm.OpenTimer); 555 timer_Stop(&p->link.ccp.fsm.OpenTimer); 556 timer_Stop(&p->link.lcp.fsm.StoppedTimer); 557 timer_Stop(&p->link.ccp.fsm.StoppedTimer); 558 if (p->handler) { 559 if (p->handler->device2iov) 560 (*p->handler->device2iov)(p, iov, niov, maxiov, newpid); 561 p->handler = (struct device *)(long)p->handler->type; 562 } 563 564 if (tcgetpgrp(p->fd) == getpgrp()) 565 p->session_owner = getpid(); /* So I'll eventually get HUP'd */ 566 timer_Stop(&p->link.throughput.Timer); 567 physical_ChangedPid(p, newpid); 568 } 569 570 if (*niov >= maxiov) { 571 log_Printf(LogERROR, "physical2iov: No room for physical !\n"); 572 if (p) 573 free(p); 574 return -1; 575 } 576 577 iov[*niov].iov_base = p ? p : malloc(sizeof *p); 578 iov[*niov].iov_len = sizeof *p; 579 (*niov)++; 580 581 return p ? p->fd : 0; 582} 583 584void 585physical_ChangedPid(struct physical *p, pid_t newpid) 586{ 587 if (p->fd >= 0 && p->type != PHYS_DIRECT) { 588 int res; 589 590 if ((res = ID0uu_lock_txfr(p->name.base, newpid)) != UU_LOCK_OK) 591 log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res)); 592 } 593} 594 595int 596physical_IsSync(struct physical *p) 597{ 598 return p->cfg.speed == 0; 599} 600 601const char *physical_GetDevice(struct physical *p) 602{ 603 return p->name.full; 604} 605 606void 607physical_SetDeviceList(struct physical *p, int argc, const char *const *argv) 608{ 609 int f, pos; 610 611 p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0'; 612 for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) { 613 if (pos) 614 p->cfg.devlist[pos++] = '\0'; 615 strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1); 616 pos += strlen(p->cfg.devlist + pos); 617 } 618 p->cfg.ndev = f; 619} 620 621void 622physical_SetSync(struct physical *p) 623{ 624 p->cfg.speed = 0; 625} 626 627int 628physical_SetRtsCts(struct physical *p, int enable) 629{ 630 p->cfg.rts_cts = enable ? 1 : 0; 631 return 1; 632} 633 634ssize_t 635physical_Read(struct physical *p, void *buf, size_t nbytes) 636{ 637 ssize_t ret; 638 639 if (p->handler && p->handler->read) 640 ret = (*p->handler->read)(p, buf, nbytes); 641 else 642 ret = read(p->fd, buf, nbytes); 643 644 log_DumpBuff(LogPHYSICAL, "read", buf, ret); 645 646 return ret; 647} 648 649ssize_t 650physical_Write(struct physical *p, const void *buf, size_t nbytes) 651{ 652 log_DumpBuff(LogPHYSICAL, "write", buf, nbytes); 653 654 if (p->handler && p->handler->write) 655 return (*p->handler->write)(p, buf, nbytes); 656 657 return write(p->fd, buf, nbytes); 658} 659 660int 661physical_doUpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, 662 int *n, int force) 663{ 664 struct physical *p = descriptor2physical(d); 665 int sets; 666 667 sets = 0; 668 if (p->fd >= 0) { 669 if (r) { 670 FD_SET(p->fd, r); 671 log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd); 672 sets++; 673 } 674 if (e) { 675 FD_SET(p->fd, e); 676 log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd); 677 sets++; 678 } 679 if (w && (force || link_QueueLen(&p->link) || p->out)) { 680 FD_SET(p->fd, w); 681 log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd); 682 sets++; 683 } 684 if (sets && *n < p->fd + 1) 685 *n = p->fd + 1; 686 } 687 688 return sets; 689} 690 691int 692physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e) 693{ 694 int sets; 695 696 sets = 0; 697 if (p->fd >= 0) { 698 if (r && FD_ISSET(p->fd, r)) { 699 FD_CLR(p->fd, r); 700 log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd); 701 sets++; 702 } 703 if (e && FD_ISSET(p->fd, e)) { 704 FD_CLR(p->fd, e); 705 log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd); 706 sets++; 707 } 708 if (w && FD_ISSET(p->fd, w)) { 709 FD_CLR(p->fd, w); 710 log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd); 711 sets++; 712 } 713 } 714 715 return sets; 716} 717 718int 719physical_IsSet(struct descriptor *d, const fd_set *fdset) 720{ 721 struct physical *p = descriptor2physical(d); 722 return p->fd >= 0 && FD_ISSET(p->fd, fdset); 723} 724 725void 726physical_Login(struct physical *p, const char *name) 727{ 728 if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) { 729 struct utmp ut; 730 const char *connstr; 731 732 memset(&ut, 0, sizeof ut); 733 time(&ut.ut_time); 734 strncpy(ut.ut_name, name, sizeof ut.ut_name); 735 strncpy(ut.ut_line, p->name.base, sizeof ut.ut_line); 736 if ((connstr = getenv("CONNECT"))) 737 /* mgetty sets this to the connection speed */ 738 strncpy(ut.ut_host, connstr, sizeof ut.ut_host); 739 ID0login(&ut); 740 p->Utmp = 1; 741 } 742} 743 744int 745physical_SetMode(struct physical *p, int mode) 746{ 747 if ((p->type & (PHYS_DIRECT|PHYS_DEDICATED) || 748 mode & (PHYS_DIRECT|PHYS_DEDICATED)) && 749 (!(p->type & PHYS_DIRECT) || !(mode & PHYS_BACKGROUND))) { 750 log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name, 751 mode2Nam(p->type), mode2Nam(mode)); 752 return 0; 753 } 754 p->type = mode; 755 return 1; 756} 757 758void 759physical_DeleteQueue(struct physical *p) 760{ 761 if (p->out) { 762 mbuf_Free(p->out); 763 p->out = NULL; 764 } 765 link_DeleteQueue(&p->link); 766} 767 768void 769physical_SetDevice(struct physical *p, const char *name) 770{ 771 int len = strlen(_PATH_DEV); 772 773 strncpy(p->name.full, name, sizeof p->name.full - 1); 774 p->name.full[sizeof p->name.full - 1] = '\0'; 775 p->name.base = *p->name.full == '!' ? p->name.full + 1 : 776 strncmp(p->name.full, _PATH_DEV, len) ? 777 p->name.full : p->name.full + len; 778} 779 780static void 781physical_Found(struct physical *p) 782{ 783 throughput_start(&p->link.throughput, "physical throughput", 784 Enabled(p->dl->bundle, OPT_THROUGHPUT)); 785 p->connect_count++; 786 p->input.sz = 0; 787 788 log_Printf(LogPHASE, "%s: Connected!\n", p->link.name); 789} 790 791int 792physical_Open(struct physical *p, struct bundle *bundle) 793{ 794 int devno, h; 795 char *dev; 796 797 if (p->fd >= 0) 798 log_Printf(LogDEBUG, "%s: Open: Modem is already open!\n", p->link.name); 799 /* We're going back into "term" mode */ 800 else if (p->type == PHYS_DIRECT) { 801 physical_SetDevice(p, ""); 802 p->fd = STDIN_FILENO; 803 for (h = 0; h < NDEVICES && p->handler == NULL && p->fd >= 0; h++) 804 p->handler = (*devices[h].create)(p); 805 if (p->fd >= 0) { 806 if (p->handler == NULL) 807 physical_SetupStack(p, PHYSICAL_NOFORCE); 808 physical_Found(p); 809 if (p->handler == NULL) 810 log_Printf(LogDEBUG, "%s: stdin is unidentified\n", 811 p->link.name); 812 } 813 } else { 814 dev = p->cfg.devlist; 815 devno = 0; 816 while (devno < p->cfg.ndev && p->fd < 0) { 817 physical_SetDevice(p, dev); 818 819 if (*p->name.full == '/') 820 p->fd = ID0open(p->name.full, O_RDWR | O_NONBLOCK); 821 822 for (h = 0; h < NDEVICES && p->handler == NULL; h++) 823 p->handler = (*devices[h].create)(p); 824 825 if (p->fd < 0) 826 log_Printf(LogWARN, "%s: Device (%s) must begin with a '/'," 827 " a '!' or be a host:port pair\n", p->link.name, 828 p->name.full); 829 else 830 physical_Found(p); 831 dev += strlen(dev) + 1; 832 devno++; 833 } 834 } 835 836 return p->fd; 837} 838 839void 840physical_SetupStack(struct physical *p, int how) 841{ 842 link_EmptyStack(&p->link); 843 if (how == PHYSICAL_FORCE_SYNC || 844 (how == PHYSICAL_NOFORCE && physical_IsSync(p))) 845 link_Stack(&p->link, &synclayer); 846 else { 847 link_Stack(&p->link, &asynclayer); 848 link_Stack(&p->link, &hdlclayer); 849 } 850 link_Stack(&p->link, &acflayer); 851 link_Stack(&p->link, &protolayer); 852 link_Stack(&p->link, &lqrlayer); 853 link_Stack(&p->link, &ccplayer); 854 link_Stack(&p->link, &vjlayer); 855#ifndef NOALIAS 856 link_Stack(&p->link, &aliaslayer); 857#endif 858 if (how == PHYSICAL_FORCE_ASYNC && physical_IsSync(p)) { 859 log_Printf(LogWARN, "Sync device setting ignored for ``%s'' device\n", 860 p->handler ? p->handler->name : "unknown"); 861 p->cfg.speed = MODEM_SPEED; 862 } else if (how == PHYSICAL_FORCE_SYNC && !physical_IsSync(p)) { 863 log_Printf(LogWARN, "Async device setting ignored for ``%s'' device\n", 864 p->handler ? p->handler->name : "unknown"); 865 physical_SetSync(p); 866 } 867} 868 869void 870physical_StopDeviceTimer(struct physical *p) 871{ 872 if (p->handler && p->handler->stoptimer) 873 (*p->handler->stoptimer)(p); 874} 875