163b73463SBrian Somers /* 263b73463SBrian Somers * Written by Eivind Eklund <eivind@yes.no> 363b73463SBrian Somers * for Yes Interactive 463b73463SBrian Somers * 563b73463SBrian Somers * Copyright (C) 1998, Yes Interactive. All rights reserved. 663b73463SBrian Somers * 763b73463SBrian Somers * Redistribution and use in any form is permitted. Redistribution in 863b73463SBrian Somers * source form should include the above copyright and this set of 963b73463SBrian Somers * conditions, because large sections american law seems to have been 1063b73463SBrian Somers * created by a bunch of jerks on drugs that are now illegal, forcing 1163b73463SBrian Somers * me to include this copyright-stuff instead of placing this in the 1263b73463SBrian Somers * public domain. The name of of 'Yes Interactive' or 'Eivind Eklund' 1363b73463SBrian Somers * may not be used to endorse or promote products derived from this 1463b73463SBrian Somers * software without specific prior written permission. 1563b73463SBrian Somers * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1663b73463SBrian Somers * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1763b73463SBrian Somers * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1863b73463SBrian Somers * 196f384573SBrian Somers * $Id: physical.c,v 1.1.2.27 1998/04/28 01:25:37 brian Exp $ 2063b73463SBrian Somers * 2163b73463SBrian Somers */ 2263b73463SBrian Somers 232764b86aSBrian Somers #include <sys/types.h> 2463b73463SBrian Somers 2563b73463SBrian Somers #include <assert.h> 2663b73463SBrian Somers #include <stdio.h> 276f384573SBrian Somers #include <stdlib.h> 2863b73463SBrian Somers #include <string.h> 29fc1141b2SBrian Somers #include <time.h> 3063b73463SBrian Somers #include <unistd.h> 31fc1141b2SBrian Somers #include <utmp.h> 326f384573SBrian Somers #include <sys/tty.h> 3363b73463SBrian Somers 3463b73463SBrian Somers #include "defs.h" 3563b73463SBrian Somers #include "mbuf.h" 3663b73463SBrian Somers #include "timer.h" 37879ed6faSBrian Somers #include "lqr.h" 3863258dccSBrian Somers #include "hdlc.h" 3963b73463SBrian Somers #include "throughput.h" 406140ba11SBrian Somers #include "fsm.h" 416140ba11SBrian Somers #include "lcp.h" 426140ba11SBrian Somers #include "async.h" 433b0f8d2eSBrian Somers #include "ccp.h" 448c07a7b2SBrian Somers #include "link.h" 4542d4d396SBrian Somers #include "descriptor.h" 4663b73463SBrian Somers #include "physical.h" 4742d4d396SBrian Somers #include "log.h" 48fc1141b2SBrian Somers #include "id.h" 4963b73463SBrian Somers 5063b73463SBrian Somers /* External calls - should possibly be moved inline */ 5163b73463SBrian Somers extern int IntToSpeed(int); 5263b73463SBrian Somers 5363b73463SBrian Somers 5463b73463SBrian Somers int 5563b73463SBrian Somers Physical_GetFD(struct physical *phys) { 5663b73463SBrian Somers return phys->fd; 5763b73463SBrian Somers } 5863b73463SBrian Somers 5963b73463SBrian Somers int 6063b73463SBrian Somers Physical_IsATTY(struct physical *phys) { 6163b73463SBrian Somers return isatty(phys->fd); 6263b73463SBrian Somers } 6363b73463SBrian Somers 6463b73463SBrian Somers int 6563b73463SBrian Somers Physical_IsSync(struct physical *phys) { 66d585f8f5SBrian Somers return phys->cfg.speed == 0; 6763b73463SBrian Somers } 6863b73463SBrian Somers 693bf710a4SBrian Somers const char *Physical_GetDevice(struct physical *phys) 703bf710a4SBrian Somers { 713bf710a4SBrian Somers return phys->name.full; 7263b73463SBrian Somers } 7363b73463SBrian Somers 7463b73463SBrian Somers void 7599294c8bSBrian Somers Physical_SetDeviceList(struct physical *p, int argc, const char *const *argv) 7699294c8bSBrian Somers { 7799294c8bSBrian Somers int f, pos; 7899294c8bSBrian Somers 7999294c8bSBrian Somers p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0'; 8099294c8bSBrian Somers for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) { 8199294c8bSBrian Somers if (pos) 8299294c8bSBrian Somers p->cfg.devlist[pos++] = ' '; 8399294c8bSBrian Somers strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1); 8499294c8bSBrian Somers pos += strlen(p->cfg.devlist + pos); 8599294c8bSBrian Somers } 8663b73463SBrian Somers } 8763b73463SBrian Somers 8863b73463SBrian Somers 8963b73463SBrian Somers int 9063b73463SBrian Somers Physical_SetSpeed(struct physical *phys, int speed) { 9163b73463SBrian Somers if (IntToSpeed(speed) != B0) { 92d585f8f5SBrian Somers phys->cfg.speed = speed; 9363b73463SBrian Somers return 1; 9463b73463SBrian Somers } else { 9563b73463SBrian Somers return 0; 9663b73463SBrian Somers } 9763b73463SBrian Somers } 9863b73463SBrian Somers 9963b73463SBrian Somers void 10063b73463SBrian Somers Physical_SetSync(struct physical *phys) { 101d585f8f5SBrian Somers phys->cfg.speed = 0; 10263b73463SBrian Somers } 10363b73463SBrian Somers 10463b73463SBrian Somers 10563b73463SBrian Somers int 10663b73463SBrian Somers Physical_SetRtsCts(struct physical *phys, int enable) { 10763b73463SBrian Somers assert(enable == 0 || enable == 1); 10863b73463SBrian Somers 109d585f8f5SBrian Somers phys->cfg.rts_cts = enable; 11063b73463SBrian Somers return 1; 11163b73463SBrian Somers } 11263b73463SBrian Somers 11363b73463SBrian Somers void 11463b73463SBrian Somers Physical_DupAndClose(struct physical *phys) { 11563b73463SBrian Somers int nmodem; 11663b73463SBrian Somers 11763b73463SBrian Somers nmodem = dup(phys->fd); 11863b73463SBrian Somers close(phys->fd); 11963b73463SBrian Somers phys->fd = nmodem; 12063b73463SBrian Somers } 12163b73463SBrian Somers 12263b73463SBrian Somers /* Encapsulation for a read on the FD. Avoids some exposure, and 12363b73463SBrian Somers concentrates control. */ 12463b73463SBrian Somers ssize_t 12563b73463SBrian Somers Physical_Read(struct physical *phys, void *buf, size_t nbytes) { 12663b73463SBrian Somers return read(phys->fd, buf, nbytes); 12763b73463SBrian Somers } 12863b73463SBrian Somers 12963b73463SBrian Somers ssize_t 13063b73463SBrian Somers Physical_Write(struct physical *phys, const void *buf, size_t nbytes) { 13163b73463SBrian Somers return write(phys->fd, buf, nbytes); 13263b73463SBrian Somers } 133ecd5172aSBrian Somers 134ecd5172aSBrian Somers int 13542d4d396SBrian Somers Physical_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, 136b6dec9f0SBrian Somers int *n, int force) 13742d4d396SBrian Somers { 13842d4d396SBrian Somers struct physical *p = descriptor2physical(d); 13942d4d396SBrian Somers int sets; 14042d4d396SBrian Somers 14142d4d396SBrian Somers sets = 0; 142b6dec9f0SBrian Somers if (p->fd >= 0) { 143b6dec9f0SBrian Somers if (r) { 144b6dec9f0SBrian Somers FD_SET(p->fd, r); 145b6dec9f0SBrian Somers sets++; 146b6dec9f0SBrian Somers } 147b6dec9f0SBrian Somers if (e) { 148b6dec9f0SBrian Somers FD_SET(p->fd, e); 149b6dec9f0SBrian Somers sets++; 150b6dec9f0SBrian Somers } 151b6dec9f0SBrian Somers if (w && (force || link_QueueLen(&p->link))) { 152b6dec9f0SBrian Somers FD_SET(p->fd, w); 153b6dec9f0SBrian Somers sets++; 154b6dec9f0SBrian Somers } 155b6dec9f0SBrian Somers if (sets && *n < p->fd + 1) 156b6dec9f0SBrian Somers *n = p->fd + 1; 157b6dec9f0SBrian Somers } 15842d4d396SBrian Somers 15942d4d396SBrian Somers return sets; 16042d4d396SBrian Somers } 16142d4d396SBrian Somers 16242d4d396SBrian Somers int 1632f786681SBrian Somers Physical_IsSet(struct descriptor *d, const fd_set *fdset) 16442d4d396SBrian Somers { 16542d4d396SBrian Somers struct physical *p = descriptor2physical(d); 16642d4d396SBrian Somers return p->fd >= 0 && FD_ISSET(p->fd, fdset); 16742d4d396SBrian Somers } 16842d4d396SBrian Somers 16942d4d396SBrian Somers void 170fc1141b2SBrian Somers Physical_Login(struct physical *phys, const char *name) 171fc1141b2SBrian Somers { 1726f384573SBrian Somers if (phys->type == PHYS_DIRECT && Physical_IsATTY(phys)) { 173fc1141b2SBrian Somers if (phys->Utmp) 174fc1141b2SBrian Somers LogPrintf(LogERROR, "Oops, already logged in on %s\n", phys->name.base); 175fc1141b2SBrian Somers else { 176fc1141b2SBrian Somers struct utmp ut; 177fc1141b2SBrian Somers 178fc1141b2SBrian Somers memset(&ut, 0, sizeof ut); 179fc1141b2SBrian Somers time(&ut.ut_time); 180fc1141b2SBrian Somers strncpy(ut.ut_name, name, sizeof ut.ut_name); 181fc1141b2SBrian Somers strncpy(ut.ut_line, phys->name.base, sizeof ut.ut_line - 1); 182fc1141b2SBrian Somers ID0login(&ut); 183fc1141b2SBrian Somers phys->Utmp = 1; 184fc1141b2SBrian Somers } 185fc1141b2SBrian Somers } 186e43ebac1SBrian Somers } 187fc1141b2SBrian Somers 188fc1141b2SBrian Somers void 189fc1141b2SBrian Somers Physical_Logout(struct physical *phys) 190fc1141b2SBrian Somers { 191fc1141b2SBrian Somers if (phys->Utmp) { 192fc1141b2SBrian Somers ID0logout(phys->name.base); 193fc1141b2SBrian Somers phys->Utmp = 0; 194fc1141b2SBrian Somers } 195fc1141b2SBrian Somers } 196