xref: /freebsd/usr.sbin/ppp/physical.c (revision b6dec9f07f112fcc0ffbfaf32cfb0b817239f331)
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  *
19b6dec9f0SBrian Somers  *  $Id: physical.c,v 1.1.2.7 1998/02/10 03:22:02 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>
3063b73463SBrian Somers #include <unistd.h>
3163b73463SBrian Somers 
3263b73463SBrian Somers 
3363b73463SBrian Somers /* XXX Name space pollution from vars.h */
3463b73463SBrian Somers #include <netinet/in.h>
3563b73463SBrian Somers #include <alias.h>
3663b73463SBrian Somers #include "defs.h"
3763b73463SBrian Somers #include "command.h"
3863b73463SBrian Somers #include "loadalias.h"
3963b73463SBrian Somers 
4063b73463SBrian Somers /* XXX Name space pollution from hdlc.h */
4163b73463SBrian Somers #include "mbuf.h"
4263b73463SBrian Somers 
4363b73463SBrian Somers /* Name space pollution for physical.h */
4463b73463SBrian Somers #include "hdlc.h"
4563b73463SBrian Somers #include "timer.h"
4663b73463SBrian Somers #include "throughput.h"
476140ba11SBrian Somers #include "fsm.h"
486140ba11SBrian Somers #include "lcp.h"
496140ba11SBrian Somers #include "async.h"
508c07a7b2SBrian Somers #include "link.h"
5163b73463SBrian Somers 
5242d4d396SBrian Somers #include "descriptor.h"
5363b73463SBrian Somers #include "physical.h"
5463b73463SBrian Somers 
5563b73463SBrian Somers #include "vars.h"
562289f246SBrian Somers #include "bundle.h"
5742d4d396SBrian Somers #include "log.h"
5863b73463SBrian Somers 
5963b73463SBrian Somers /* External calls - should possibly be moved inline */
6063b73463SBrian Somers extern int IntToSpeed(int);
6163b73463SBrian Somers 
6263b73463SBrian Somers 
6363b73463SBrian Somers int
6463b73463SBrian Somers Physical_GetFD(struct physical *phys) {
6563b73463SBrian Somers    return phys->fd;
6663b73463SBrian Somers }
6763b73463SBrian Somers 
6863b73463SBrian Somers int
6963b73463SBrian Somers Physical_IsATTY(struct physical *phys) {
7063b73463SBrian Somers    return isatty(phys->fd);
7163b73463SBrian Somers }
7263b73463SBrian Somers 
7363b73463SBrian Somers int
7463b73463SBrian Somers Physical_IsSync(struct physical *phys) {
7563b73463SBrian Somers    return phys->speed == 0;
7663b73463SBrian Somers }
7763b73463SBrian Somers 
7863b73463SBrian Somers int
7963b73463SBrian Somers Physical_FD_ISSET(struct physical *phys, fd_set *set) {
8063b73463SBrian Somers    return phys->fd >= 0 && FD_ISSET(phys->fd, set);
8163b73463SBrian Somers }
8263b73463SBrian Somers 
8363b73463SBrian Somers void
8463b73463SBrian Somers Physical_FD_SET(struct physical *phys, fd_set *set) {
8563b73463SBrian Somers    assert(phys->fd >= 0);
8663b73463SBrian Somers    FD_SET(phys->fd, set);
8763b73463SBrian Somers }
8863b73463SBrian Somers 
8963b73463SBrian Somers 
9063b73463SBrian Somers /* XXX-ML - must be moved into the physical struct  */
9163b73463SBrian Somers const char *Physical_GetDevice(struct physical *phys) {
9263b73463SBrian Somers    return VarDevice;
9363b73463SBrian Somers }
9463b73463SBrian Somers 
9563b73463SBrian Somers /* XXX-ML - must be moved into the physical struct  */
9663b73463SBrian Somers void
9763b73463SBrian Somers Physical_SetDevice(struct physical *phys, const char *new_device_list) {
9863b73463SBrian Somers    strncpy(VarDeviceList, new_device_list, sizeof VarDeviceList - 1);
9963b73463SBrian Somers    VarDeviceList[sizeof VarDeviceList - 1] = '\0';
10063b73463SBrian Somers }
10163b73463SBrian Somers 
10263b73463SBrian Somers 
10363b73463SBrian Somers int
10463b73463SBrian Somers Physical_SetSpeed(struct physical *phys, int speed) {
10563b73463SBrian Somers    if (IntToSpeed(speed) != B0) {
10663b73463SBrian Somers       phys->speed = speed;
10763b73463SBrian Somers       return 1;
10863b73463SBrian Somers    } else {
10963b73463SBrian Somers 	  return 0;
11063b73463SBrian Somers    }
11163b73463SBrian Somers }
11263b73463SBrian Somers 
11363b73463SBrian Somers void
11463b73463SBrian Somers Physical_SetSync(struct physical *phys) {
11563b73463SBrian Somers    phys->speed = 0;
11663b73463SBrian Somers }
11763b73463SBrian Somers 
11863b73463SBrian Somers 
11963b73463SBrian Somers int
12063b73463SBrian Somers Physical_SetRtsCts(struct physical *phys, int enable) {
12163b73463SBrian Somers    assert(enable == 0 || enable == 1);
12263b73463SBrian Somers 
12363b73463SBrian Somers    phys->rts_cts = enable;
12463b73463SBrian Somers    return 1;
12563b73463SBrian Somers }
12663b73463SBrian Somers 
12763b73463SBrian Somers void
12863b73463SBrian Somers Physical_SetDedicated(struct physical *phys, int enable) {
12963b73463SBrian Somers    assert(enable == 0 || enable == 1);
13063b73463SBrian Somers 
13163b73463SBrian Somers    phys->is_dedicated = enable;
13263b73463SBrian Somers }
13363b73463SBrian Somers 
13463b73463SBrian Somers void
13563b73463SBrian Somers Physical_SetDirect(struct physical *phys, int enable) {
13663b73463SBrian Somers    assert(enable == 0 || enable == 1);
13763b73463SBrian Somers 
13863b73463SBrian Somers    phys->is_direct = enable;
13963b73463SBrian Somers }
14063b73463SBrian Somers 
14163b73463SBrian Somers int
14263b73463SBrian Somers Physical_IsDirect(struct physical *phys) {
14363b73463SBrian Somers    return phys->is_direct;
14463b73463SBrian Somers }
14563b73463SBrian Somers 
14663b73463SBrian Somers int
14763b73463SBrian Somers Physical_IsDedicated(struct physical *phys) {
14863b73463SBrian Somers    return phys->is_dedicated;
14963b73463SBrian Somers }
15063b73463SBrian Somers 
15163b73463SBrian Somers 
15263b73463SBrian Somers void
15363b73463SBrian Somers Physical_DupAndClose(struct physical *phys) {
15463b73463SBrian Somers    int nmodem;
15563b73463SBrian Somers 
15663b73463SBrian Somers    nmodem = dup(phys->fd);
15763b73463SBrian Somers    close(phys->fd);
15863b73463SBrian Somers    phys->fd = nmodem;
15963b73463SBrian Somers }
16063b73463SBrian Somers 
16163b73463SBrian Somers /* Encapsulation for a read on the FD.  Avoids some exposure, and
16263b73463SBrian Somers    concentrates control. */
16363b73463SBrian Somers ssize_t
16463b73463SBrian Somers Physical_Read(struct physical *phys, void *buf, size_t nbytes) {
16563b73463SBrian Somers    return read(phys->fd, buf, nbytes);
16663b73463SBrian Somers }
16763b73463SBrian Somers 
16863b73463SBrian Somers ssize_t
16963b73463SBrian Somers Physical_Write(struct physical *phys, const void *buf, size_t nbytes) {
17063b73463SBrian Somers    return write(phys->fd, buf, nbytes);
17163b73463SBrian Somers }
172ecd5172aSBrian Somers 
173ecd5172aSBrian Somers int
174ecd5172aSBrian Somers Physical_ReportProtocolStatus(struct cmdargs const *arg)
175ecd5172aSBrian Somers {
1762289f246SBrian Somers   link_ReportProtocolStatus(&arg->bundle->physical->link);
177ecd5172aSBrian Somers   return 0;
178ecd5172aSBrian Somers }
17942d4d396SBrian Somers 
18042d4d396SBrian Somers int
18142d4d396SBrian Somers Physical_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
182b6dec9f0SBrian Somers                    int *n, int force)
18342d4d396SBrian Somers {
18442d4d396SBrian Somers   struct physical *p = descriptor2physical(d);
18542d4d396SBrian Somers   int sets;
18642d4d396SBrian Somers 
18742d4d396SBrian Somers   LogPrintf(LogDEBUG, "descriptor2physical; %p -> %p\n", d, p);
18842d4d396SBrian Somers 
18942d4d396SBrian Somers   sets = 0;
190b6dec9f0SBrian Somers   if (p->fd >= 0) {
191b6dec9f0SBrian Somers     if (r) {
192b6dec9f0SBrian Somers       FD_SET(p->fd, r);
193b6dec9f0SBrian Somers       sets++;
194b6dec9f0SBrian Somers     }
195b6dec9f0SBrian Somers     if (e) {
196b6dec9f0SBrian Somers       FD_SET(p->fd, e);
197b6dec9f0SBrian Somers       sets++;
198b6dec9f0SBrian Somers     }
199b6dec9f0SBrian Somers     if (w && (force || link_QueueLen(&p->link))) {
200b6dec9f0SBrian Somers       FD_SET(p->fd, w);
201b6dec9f0SBrian Somers       sets++;
202b6dec9f0SBrian Somers     }
203b6dec9f0SBrian Somers     if (sets && *n < p->fd + 1)
204b6dec9f0SBrian Somers       *n = p->fd + 1;
205b6dec9f0SBrian Somers   }
20642d4d396SBrian Somers 
20742d4d396SBrian Somers   return sets;
20842d4d396SBrian Somers }
20942d4d396SBrian Somers 
21042d4d396SBrian Somers int
21142d4d396SBrian Somers Physical_IsSet(struct descriptor *d, fd_set *fdset)
21242d4d396SBrian Somers {
21342d4d396SBrian Somers   struct physical *p = descriptor2physical(d);
21442d4d396SBrian Somers 
21542d4d396SBrian Somers   LogPrintf(LogDEBUG, "descriptor2physical; %p -> %p\n", d, p);
21642d4d396SBrian Somers   return p->fd >= 0 && FD_ISSET(p->fd, fdset);
21742d4d396SBrian Somers }
21842d4d396SBrian Somers 
21942d4d396SBrian Somers void
220b77776a7SBrian Somers Physical_DescriptorWrite(struct descriptor *d, const fd_set *fdset)
22142d4d396SBrian Somers {
22242d4d396SBrian Somers   struct physical *p = descriptor2physical(d);
22342d4d396SBrian Somers 
22442d4d396SBrian Somers   LogPrintf(LogDEBUG, "descriptor2physical; %p -> %p\n", d, p);
22542d4d396SBrian Somers   link_StartOutput(&p->link);
22642d4d396SBrian Somers }
227