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 * 19fc1141b2SBrian Somers * $Id: physical.c,v 1.1.2.13 1998/03/06 00:34:46 brian Exp $ 2063b73463SBrian Somers * 2163b73463SBrian Somers */ 2263b73463SBrian Somers 2363b73463SBrian Somers #include <sys/param.h> 2463b73463SBrian Somers #include <sys/tty.h> 2563b73463SBrian Somers #include <sys/uio.h> 2663b73463SBrian Somers 2763b73463SBrian Somers #include <assert.h> 2863b73463SBrian Somers #include <stdio.h> 2963b73463SBrian Somers #include <string.h> 30fc1141b2SBrian Somers #include <time.h> 3163b73463SBrian Somers #include <unistd.h> 32fc1141b2SBrian Somers #include <utmp.h> 3363b73463SBrian Somers 3463b73463SBrian Somers 3563b73463SBrian Somers /* XXX Name space pollution from vars.h */ 3663b73463SBrian Somers #include <netinet/in.h> 3763b73463SBrian Somers #include <alias.h> 38fc1141b2SBrian Somers 3963b73463SBrian Somers #include "defs.h" 4063b73463SBrian Somers #include "command.h" 4163b73463SBrian Somers #include "loadalias.h" 4263b73463SBrian Somers 4363b73463SBrian Somers /* XXX Name space pollution from hdlc.h */ 4463b73463SBrian Somers #include "mbuf.h" 4563b73463SBrian Somers 4663b73463SBrian Somers /* Name space pollution for physical.h */ 4763b73463SBrian Somers #include "timer.h" 4863258dccSBrian Somers #include "hdlc.h" 4963b73463SBrian Somers #include "throughput.h" 506140ba11SBrian Somers #include "fsm.h" 516140ba11SBrian Somers #include "lcp.h" 526140ba11SBrian Somers #include "async.h" 538c07a7b2SBrian Somers #include "link.h" 5463b73463SBrian Somers 5542d4d396SBrian Somers #include "descriptor.h" 5663b73463SBrian Somers #include "physical.h" 5763b73463SBrian Somers 5863b73463SBrian Somers #include "vars.h" 592289f246SBrian Somers #include "bundle.h" 6042d4d396SBrian Somers #include "log.h" 61fc1141b2SBrian Somers #include "id.h" 6263b73463SBrian Somers 6363b73463SBrian Somers /* External calls - should possibly be moved inline */ 6463b73463SBrian Somers extern int IntToSpeed(int); 6563b73463SBrian Somers 6663b73463SBrian Somers 6763b73463SBrian Somers int 6863b73463SBrian Somers Physical_GetFD(struct physical *phys) { 6963b73463SBrian Somers return phys->fd; 7063b73463SBrian Somers } 7163b73463SBrian Somers 7263b73463SBrian Somers int 7363b73463SBrian Somers Physical_IsATTY(struct physical *phys) { 7463b73463SBrian Somers return isatty(phys->fd); 7563b73463SBrian Somers } 7663b73463SBrian Somers 7763b73463SBrian Somers int 7863b73463SBrian Somers Physical_IsSync(struct physical *phys) { 79d585f8f5SBrian Somers return phys->cfg.speed == 0; 8063b73463SBrian Somers } 8163b73463SBrian Somers 8263b73463SBrian Somers int 8363b73463SBrian Somers Physical_FD_ISSET(struct physical *phys, fd_set *set) { 8463b73463SBrian Somers return phys->fd >= 0 && FD_ISSET(phys->fd, set); 8563b73463SBrian Somers } 8663b73463SBrian Somers 8763b73463SBrian Somers void 8863b73463SBrian Somers Physical_FD_SET(struct physical *phys, fd_set *set) { 8963b73463SBrian Somers assert(phys->fd >= 0); 9063b73463SBrian Somers FD_SET(phys->fd, set); 9163b73463SBrian Somers } 9263b73463SBrian Somers 9363b73463SBrian Somers 943bf710a4SBrian Somers const char *Physical_GetDevice(struct physical *phys) 953bf710a4SBrian Somers { 963bf710a4SBrian Somers return phys->name.full; 9763b73463SBrian Somers } 9863b73463SBrian Somers 9963b73463SBrian Somers /* XXX-ML - must be moved into the physical struct */ 10063b73463SBrian Somers void 1013bf710a4SBrian Somers Physical_SetDeviceList(struct physical *phys, const char *new_device_list) { 1023bf710a4SBrian Somers strncpy(phys->cfg.devlist, new_device_list, sizeof phys->cfg.devlist - 1); 1033bf710a4SBrian Somers phys->cfg.devlist[sizeof phys->cfg.devlist - 1] = '\0'; 10463b73463SBrian Somers } 10563b73463SBrian Somers 10663b73463SBrian Somers 10763b73463SBrian Somers int 10863b73463SBrian Somers Physical_SetSpeed(struct physical *phys, int speed) { 10963b73463SBrian Somers if (IntToSpeed(speed) != B0) { 110d585f8f5SBrian Somers phys->cfg.speed = speed; 11163b73463SBrian Somers return 1; 11263b73463SBrian Somers } else { 11363b73463SBrian Somers return 0; 11463b73463SBrian Somers } 11563b73463SBrian Somers } 11663b73463SBrian Somers 11763b73463SBrian Somers void 11863b73463SBrian Somers Physical_SetSync(struct physical *phys) { 119d585f8f5SBrian Somers phys->cfg.speed = 0; 12063b73463SBrian Somers } 12163b73463SBrian Somers 12263b73463SBrian Somers 12363b73463SBrian Somers int 12463b73463SBrian Somers Physical_SetRtsCts(struct physical *phys, int enable) { 12563b73463SBrian Somers assert(enable == 0 || enable == 1); 12663b73463SBrian Somers 127d585f8f5SBrian Somers phys->cfg.rts_cts = enable; 12863b73463SBrian Somers return 1; 12963b73463SBrian Somers } 13063b73463SBrian Somers 13163b73463SBrian Somers void 13263b73463SBrian Somers Physical_SetDedicated(struct physical *phys, int enable) { 13363b73463SBrian Somers assert(enable == 0 || enable == 1); 13463b73463SBrian Somers 135d585f8f5SBrian Somers phys->cfg.is_dedicated = enable; 13663b73463SBrian Somers } 13763b73463SBrian Somers 13863b73463SBrian Somers void 13963b73463SBrian Somers Physical_SetDirect(struct physical *phys, int enable) { 14063b73463SBrian Somers assert(enable == 0 || enable == 1); 14163b73463SBrian Somers 142d585f8f5SBrian Somers phys->cfg.is_direct = enable; 14363b73463SBrian Somers } 14463b73463SBrian Somers 14563b73463SBrian Somers int 14663b73463SBrian Somers Physical_IsDirect(struct physical *phys) { 147d585f8f5SBrian Somers return phys->cfg.is_direct; 14863b73463SBrian Somers } 14963b73463SBrian Somers 15063b73463SBrian Somers int 15163b73463SBrian Somers Physical_IsDedicated(struct physical *phys) { 152d585f8f5SBrian Somers return phys->cfg.is_dedicated; 15363b73463SBrian Somers } 15463b73463SBrian Somers 15563b73463SBrian Somers 15663b73463SBrian Somers void 15763b73463SBrian Somers Physical_DupAndClose(struct physical *phys) { 15863b73463SBrian Somers int nmodem; 15963b73463SBrian Somers 16063b73463SBrian Somers nmodem = dup(phys->fd); 16163b73463SBrian Somers close(phys->fd); 16263b73463SBrian Somers phys->fd = nmodem; 16363b73463SBrian Somers } 16463b73463SBrian Somers 16563b73463SBrian Somers /* Encapsulation for a read on the FD. Avoids some exposure, and 16663b73463SBrian Somers concentrates control. */ 16763b73463SBrian Somers ssize_t 16863b73463SBrian Somers Physical_Read(struct physical *phys, void *buf, size_t nbytes) { 16963b73463SBrian Somers return read(phys->fd, buf, nbytes); 17063b73463SBrian Somers } 17163b73463SBrian Somers 17263b73463SBrian Somers ssize_t 17363b73463SBrian Somers Physical_Write(struct physical *phys, const void *buf, size_t nbytes) { 17463b73463SBrian Somers return write(phys->fd, buf, nbytes); 17563b73463SBrian Somers } 176ecd5172aSBrian Somers 177ecd5172aSBrian Somers int 178ecd5172aSBrian Somers Physical_ReportProtocolStatus(struct cmdargs const *arg) 179ecd5172aSBrian Somers { 1803006ec67SBrian Somers link_ReportProtocolStatus(bundle2link(arg->bundle, NULL)); 181ecd5172aSBrian Somers return 0; 182ecd5172aSBrian Somers } 18342d4d396SBrian Somers 18442d4d396SBrian Somers int 18542d4d396SBrian Somers Physical_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, 186b6dec9f0SBrian Somers int *n, int force) 18742d4d396SBrian Somers { 18842d4d396SBrian Somers struct physical *p = descriptor2physical(d); 18942d4d396SBrian Somers int sets; 19042d4d396SBrian Somers 19142d4d396SBrian Somers LogPrintf(LogDEBUG, "descriptor2physical; %p -> %p\n", d, p); 19242d4d396SBrian Somers 19342d4d396SBrian Somers sets = 0; 194b6dec9f0SBrian Somers if (p->fd >= 0) { 195b6dec9f0SBrian Somers if (r) { 196b6dec9f0SBrian Somers FD_SET(p->fd, r); 197b6dec9f0SBrian Somers sets++; 198b6dec9f0SBrian Somers } 199b6dec9f0SBrian Somers if (e) { 200b6dec9f0SBrian Somers FD_SET(p->fd, e); 201b6dec9f0SBrian Somers sets++; 202b6dec9f0SBrian Somers } 203b6dec9f0SBrian Somers if (w && (force || link_QueueLen(&p->link))) { 204b6dec9f0SBrian Somers FD_SET(p->fd, w); 205b6dec9f0SBrian Somers sets++; 206b6dec9f0SBrian Somers } 207b6dec9f0SBrian Somers if (sets && *n < p->fd + 1) 208b6dec9f0SBrian Somers *n = p->fd + 1; 209b6dec9f0SBrian Somers } 21042d4d396SBrian Somers 21142d4d396SBrian Somers return sets; 21242d4d396SBrian Somers } 21342d4d396SBrian Somers 21442d4d396SBrian Somers int 21542d4d396SBrian Somers Physical_IsSet(struct descriptor *d, fd_set *fdset) 21642d4d396SBrian Somers { 21742d4d396SBrian Somers struct physical *p = descriptor2physical(d); 21842d4d396SBrian Somers 21942d4d396SBrian Somers LogPrintf(LogDEBUG, "descriptor2physical; %p -> %p\n", d, p); 22042d4d396SBrian Somers return p->fd >= 0 && FD_ISSET(p->fd, fdset); 22142d4d396SBrian Somers } 22242d4d396SBrian Somers 22342d4d396SBrian Somers void 224f4768038SBrian Somers Physical_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 225f4768038SBrian Somers const fd_set *fdset) 22642d4d396SBrian Somers { 22742d4d396SBrian Somers struct physical *p = descriptor2physical(d); 22842d4d396SBrian Somers 22942d4d396SBrian Somers LogPrintf(LogDEBUG, "descriptor2physical; %p -> %p\n", d, p); 230f4768038SBrian Somers link_StartOutput(&p->link, bundle); 23142d4d396SBrian Somers } 232fc1141b2SBrian Somers 233fc1141b2SBrian Somers void 234fc1141b2SBrian Somers Physical_Login(struct physical *phys, const char *name) 235fc1141b2SBrian Somers { 236fc1141b2SBrian Somers if ((mode & MODE_DIRECT) && Physical_IsATTY(phys) && Enabled(ConfUtmp)) 237fc1141b2SBrian Somers if (phys->Utmp) 238fc1141b2SBrian Somers LogPrintf(LogERROR, "Oops, already logged in on %s\n", phys->name.base); 239fc1141b2SBrian Somers else { 240fc1141b2SBrian Somers struct utmp ut; 241fc1141b2SBrian Somers 242fc1141b2SBrian Somers memset(&ut, 0, sizeof ut); 243fc1141b2SBrian Somers time(&ut.ut_time); 244fc1141b2SBrian Somers strncpy(ut.ut_name, name, sizeof ut.ut_name); 245fc1141b2SBrian Somers strncpy(ut.ut_line, phys->name.base, sizeof ut.ut_line - 1); 246fc1141b2SBrian Somers ID0login(&ut); 247fc1141b2SBrian Somers phys->Utmp = 1; 248fc1141b2SBrian Somers } 249fc1141b2SBrian Somers } 250fc1141b2SBrian Somers 251fc1141b2SBrian Somers void 252fc1141b2SBrian Somers Physical_Logout(struct physical *phys) 253fc1141b2SBrian Somers { 254fc1141b2SBrian Somers if (phys->Utmp) { 255fc1141b2SBrian Somers ID0logout(phys->name.base); 256fc1141b2SBrian Somers phys->Utmp = 0; 257fc1141b2SBrian Somers } 258fc1141b2SBrian Somers } 259