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