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.1.2.22 1998/04/07 00:54:13 brian Exp $ 20 * 21 */ 22 23 #include <sys/types.h> 24 25 #include <sys/tty.h> 26 27 #include <assert.h> 28 #include <stdio.h> 29 #include <string.h> 30 #include <time.h> 31 #include <unistd.h> 32 #include <utmp.h> 33 34 35 /* XXX Name space pollution from vars.h */ 36 37 #include "defs.h" 38 39 /* XXX Name space pollution from hdlc.h */ 40 #include "mbuf.h" 41 42 /* Name space pollution for physical.h */ 43 #include "timer.h" 44 #include "lqr.h" 45 #include "hdlc.h" 46 #include "throughput.h" 47 #include "fsm.h" 48 #include "lcp.h" 49 #include "async.h" 50 #include "ccp.h" 51 #include "link.h" 52 53 #include "descriptor.h" 54 #include "physical.h" 55 56 #include "vars.h" 57 #include "log.h" 58 #include "id.h" 59 60 /* External calls - should possibly be moved inline */ 61 extern int IntToSpeed(int); 62 63 64 int 65 Physical_GetFD(struct physical *phys) { 66 return phys->fd; 67 } 68 69 int 70 Physical_IsATTY(struct physical *phys) { 71 return isatty(phys->fd); 72 } 73 74 int 75 Physical_IsSync(struct physical *phys) { 76 return phys->cfg.speed == 0; 77 } 78 79 const char *Physical_GetDevice(struct physical *phys) 80 { 81 return phys->name.full; 82 } 83 84 /* XXX-ML - must be moved into the physical struct */ 85 void 86 Physical_SetDeviceList(struct physical *phys, const char *new_device_list) { 87 strncpy(phys->cfg.devlist, new_device_list, sizeof phys->cfg.devlist - 1); 88 phys->cfg.devlist[sizeof phys->cfg.devlist - 1] = '\0'; 89 } 90 91 92 int 93 Physical_SetSpeed(struct physical *phys, int speed) { 94 if (IntToSpeed(speed) != B0) { 95 phys->cfg.speed = speed; 96 return 1; 97 } else { 98 return 0; 99 } 100 } 101 102 void 103 Physical_SetSync(struct physical *phys) { 104 phys->cfg.speed = 0; 105 } 106 107 108 int 109 Physical_SetRtsCts(struct physical *phys, int enable) { 110 assert(enable == 0 || enable == 1); 111 112 phys->cfg.rts_cts = enable; 113 return 1; 114 } 115 116 void 117 Physical_DupAndClose(struct physical *phys) { 118 int nmodem; 119 120 nmodem = dup(phys->fd); 121 close(phys->fd); 122 phys->fd = nmodem; 123 } 124 125 /* Encapsulation for a read on the FD. Avoids some exposure, and 126 concentrates control. */ 127 ssize_t 128 Physical_Read(struct physical *phys, void *buf, size_t nbytes) { 129 return read(phys->fd, buf, nbytes); 130 } 131 132 ssize_t 133 Physical_Write(struct physical *phys, const void *buf, size_t nbytes) { 134 return write(phys->fd, buf, nbytes); 135 } 136 137 int 138 Physical_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, 139 int *n, int force) 140 { 141 struct physical *p = descriptor2physical(d); 142 int sets; 143 144 sets = 0; 145 if (p->fd >= 0) { 146 if (r) { 147 FD_SET(p->fd, r); 148 sets++; 149 } 150 if (e) { 151 FD_SET(p->fd, e); 152 sets++; 153 } 154 if (w && (force || link_QueueLen(&p->link))) { 155 FD_SET(p->fd, w); 156 sets++; 157 } 158 if (sets && *n < p->fd + 1) 159 *n = p->fd + 1; 160 } 161 162 return sets; 163 } 164 165 int 166 Physical_IsSet(struct descriptor *d, const fd_set *fdset) 167 { 168 struct physical *p = descriptor2physical(d); 169 return p->fd >= 0 && FD_ISSET(p->fd, fdset); 170 } 171 172 void 173 Physical_Login(struct physical *phys, const char *name) 174 { 175 if (phys->type == PHYS_STDIN && Physical_IsATTY(phys) && Enabled(ConfUtmp)) 176 if (phys->Utmp) 177 LogPrintf(LogERROR, "Oops, already logged in on %s\n", phys->name.base); 178 else { 179 struct utmp ut; 180 181 memset(&ut, 0, sizeof ut); 182 time(&ut.ut_time); 183 strncpy(ut.ut_name, name, sizeof ut.ut_name); 184 strncpy(ut.ut_line, phys->name.base, sizeof ut.ut_line - 1); 185 ID0login(&ut); 186 phys->Utmp = 1; 187 } 188 } 189 190 void 191 Physical_Logout(struct physical *phys) 192 { 193 if (phys->Utmp) { 194 ID0logout(phys->name.base); 195 phys->Utmp = 0; 196 } 197 } 198