File: slip.c

package info (click to toggle)
ipip 1.1.9
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 224 kB
  • ctags: 143
  • sloc: ansic: 1,451; sh: 137; makefile: 20
file content (365 lines) | stat: -rw-r--r-- 8,220 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* slip.c		SLIP interface unit
 *
 * $Id: slip.c,v 1.11 1995/03/19 17:21:06 bdale Exp $
 *
 * Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.
 * This software may be freely used, distributed, or modified, providing
 * this header is not removed.
 */

#include "ipip.h"

#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <fcntl.h>
#include <memory.h>
#include <stdio.h>
#include <string.h>
#ifdef __bsdi__
# include <stdlib.h>
#else
# include <malloc.h>
#endif
#include <errno.h>
#include <syslog.h>

#ifdef __bsdi__
#define USE_TERMIOS
#endif

#ifndef USE_TERMIOS
#ifndef USE_TERMIO
#define USE_SGTTY
#endif
#endif

#ifdef USE_TERMIOS
# include <termios.h>
# include <unistd.h>
#endif

#ifdef USE_TERMIO
#include <sys/termio.h>
#endif

#ifdef USE_SGTTY
#include <sgtty.h>
#endif

#ifndef FNDELAY
#define FNDELAY O_NDELAY
#endif

extern int errno;

#define IF_NAME "slip"		/* for use with the error checking macros */

/*
 * Define the special characters used by SLIP
 */

#define FEND  0xc0
#define FESC  0xdb
#define TFEND 0xdc
#define TFESC 0xdd

/*
 * This is the routine that assembles a slip packet, and the private
 * data structure we need to keep track of where we are.
 */

static int assemble_slip();
struct slippy {
	unsigned char buffer[MAX_SIZE];		/* buffer from the serial line */
	int bcount;				/* number of total chars in buffer */
	int bnext;				/* next character to process */
	unsigned char ipacket[MAX_SIZE];	/* the packet we are assembling */
	int ifcount;				/* total size of assembled packet */
	int iescaped;				/* flag set if we are escaped */
};

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * open and initialize the IO interface.  Return -1 for error.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */
int slip_open(ifp)
struct interface *ifp;
{
	int baudrate;
#ifdef USE_TERMIOS
	struct termios nterm;
#endif
#ifdef USE_TERMIO
	struct termio  nterm;
#endif
#ifdef USE_SGTTY
	struct sgttyb  nterm;
#endif

	CK_IFNULL(ifp);
	CK_IFTYPE(ifp,IF_TYPE_SLIP);

	if((ifp->status & IF_STAT_OPEN)) return 1;

#ifdef __bsdi__
	/* BSDI cares about the modem control lines, so this won't ever 
	   come back unless we do the open non-blocking, then set CLOCAL. */
	ifp->fd = open(ifp->devname, O_RDWR | O_NONBLOCK);
#else
	ifp->fd = open(ifp->devname, O_RDWR);
#endif
	if (ifp->fd<0) {
		PERR(ifp->devname);
		return -1;
	}

	if (fcntl(ifp->fd, F_SETFL, FNDELAY) < 0) {
		PERR("setting non-blocking I/O on tty device");
		return -1;
	}

#ifdef USE_TERMIOS
#if defined(__bsdi__) || defined(linux)
	if(tcgetattr(ifp->fd, &nterm)<0){
#else
	if(ioctl(ifp->fd, TCGETS, &nterm)<0){
#endif /* __bsdi__ */
#endif
#ifdef USE_TERMIO
	if(ioctl(ifp->fd, TCGETA, &nterm)<0){
#endif
#ifdef USE_SGTTY
	if(ioctl(ifp->fd, TIOCGETP, &nterm)<0){
#endif
		PERR("fetching tty device parameters");
		return -1;
	}

	if(ifp->unit==50)baudrate=B50;
	else if(ifp->unit==50)baudrate=B50;
	else if(ifp->unit==75)baudrate=B75;
	else if(ifp->unit==110)baudrate=B110;
	else if(ifp->unit==134)baudrate=B134;
	else if(ifp->unit==150)baudrate=B150;
	else if(ifp->unit==200)baudrate=B200;
	else if(ifp->unit==300)baudrate=B300;
	else if(ifp->unit==600)baudrate=B600;
	else if(ifp->unit==1200)baudrate=B1200;
	else if(ifp->unit==1800)baudrate=B1800;
	else if(ifp->unit==2400)baudrate=B2400;
	else if(ifp->unit==4800)baudrate=B4800;
	else if(ifp->unit==9600)baudrate=B9600;
#ifdef B19200
	else if(ifp->unit==19200)baudrate=B19200;
#else
#ifdef EXTA
	else if(ifp->unit==19200)baudrate=EXTA;
#endif
#endif
#ifdef B38400
	else if(ifp->unit==38400)baudrate=B38400;
#else
#ifdef EXTB
	else if(ifp->unit==38400)baudrate=EXTB;
#endif
#endif
	else baudrate = B9600;

#ifdef USE_SGTTY
	nterm.sg_flags = (RAW | ANYP);
	nterm.sg_ispeed = baudrate;
	nterm.sg_ospeed = baudrate;
#else
        nterm.c_iflag = 0;
        nterm.c_oflag = 0;
	nterm.c_cflag = baudrate | CS8 | CREAD | CLOCAL;
	nterm.c_lflag = 0;
        nterm.c_cc[VMIN] = 0;
        nterm.c_cc[VTIME] = 0;
#endif

#ifdef USE_TERMIOS
#if defined(__bsdi__) || defined(linux)
	if(tcsetattr(ifp->fd, TCSANOW, &nterm)<0){
#else
	if(ioctl(ifp->fd, TCSETS, &nterm)<0){
#endif /* __bsdi__ */
#endif
#ifdef USE_TERMIO
	if(ioctl(ifp->fd, TCSETA, &nterm)<0){
#endif
#ifdef USE_SGTTY
	if(ioctl(ifp->fd, TIOCSETP, &nterm)<0){
#endif
		PERR("setting tty device parameters");
		return -1;
	}

	if(ifp->private==NULL)ifp->private = malloc(sizeof(struct slippy));
	if(ifp->private==NULL){
		syslog(LOG_ERR,"cannot allocate private data structure (slip)");
		return -1;
	}

	(void)memset(ifp->private, 0, sizeof(struct slippy));

	ifp->status = IF_STAT_OPEN;
	return 1;
}

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Read data from the specified interface. Return a complete IP datagram.
 * If the datagram is not complete, then don't return anything.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */
int slip_read(ifp, m)
struct interface *ifp;
struct message *m;
{
	int n;
	struct slippy *s;

	CK_IFNULL(ifp);
	CK_IFTYPE(ifp,IF_TYPE_SLIP);
	CK_IFOPEN(ifp);
	CK_MNULL(m);

	s = (struct slippy *)ifp->private;
	ifp->status &= ~IF_STAT_CALL_AGAIN;
	m->length = 0;

	for(;;){
		if(s->bnext >= s->bcount){	/* we need more data! */
			s->bnext = 0;
			s->bcount = 0;
			n = read(ifp->fd, (char *)s->buffer, MAX_SIZE);
			if(n==0)return 0;	/* got nothing */
			if(n<0){
				if(errno==EINTR)return 0;	/* SIGHUP! */
				if(errno==EWOULDBLOCK)return 0; /* got nothing */
				PERR("read from tty device");
				return -1;
			}
			s->bcount = n;
		}

		n = assemble_slip(s, s->buffer[s->bnext]);
		s->bnext++;

		if(n > 0){
			(void)memcpy((char *)m->msg, (char *)s->ipacket, n);
			m->length = n;
			if(s->bnext < s->bcount)
				ifp->status |= IF_STAT_CALL_AGAIN;
			return n;
		}
	}
}

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * write data from to the specified interface. Return as soon as possible.
 * The buffer provided will be a complete IP datagram.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

#define SLIPEMIT(x) if(ofcount<MAX_SIZE*2){*ofptr=(x);ofptr++;ofcount++;}

int slip_send(ifp, m)
struct interface *ifp;
struct message *m;
{
	int n, i, ofcount;
	unsigned char opacket[MAX_SIZE*2], *ofptr, *mptr;

	CK_IFNULL(ifp);
	CK_IFTYPE(ifp,IF_TYPE_SLIP);
	CK_IFOPEN(ifp);
	CK_MNULL(m);

	if(m->length<=0)return 0;

	ofptr = opacket;
	ofcount = 0;
	mptr = m->msg;

	SLIPEMIT(FEND);

	for(i=0;i<m->length;i++,mptr++){
		if(*mptr==FEND){
			SLIPEMIT(FESC);
			SLIPEMIT(TFEND);
		}else if (*mptr==FESC){
			SLIPEMIT(FESC);
			SLIPEMIT(TFESC);
		}else {
			SLIPEMIT(*mptr);
		}
	}

	SLIPEMIT(FEND);

/*
 * WARNING!  It is possible for this write to actually write less data
 * than expected if the system has, for example, no buffer space left.
 * We ignore the error (just increment the write overrun counter),
 * drop the current packet, and expect that the higher level protocols
 * will recover.
 *
 * If we got an "interrupted system call" error we print the diagnostic
 * and continue.  (the packet gets dropped as above).
 */
	n = write(ifp->fd, (char *)opacket, ofcount);
	if(n<0){
		syslog(LOG_ERR,"slip_send(): %s",strerror(errno));
		if((errno==EINTR) || (errno==EAGAIN)) return 0;
		return -1;
	}
	if(n < ofcount)ifp->out_overruns++;
	return n;
}

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * take a character and assemble it into the buffer.  Return the length
 * of the completed packet, 0 if not yet completed.  Packet can be found
 * in the private buffer...
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */
static int
assemble_slip(s, c)
struct slippy *s;
unsigned char c;
{
	int n;

	if(c==FEND){
		n = s->ifcount;
		s->ifcount = 0;
		s->iescaped = 0;
		return n;
	}

	if(c==FESC){
		s->iescaped=1;
		return 0;
	}

	if(s->iescaped){
		if(c==TFEND)c = FEND;
		if(c==TFESC)c = FESC;
		s->iescaped = 0;
	}
	if(s->ifcount < MAX_SIZE){
		s->ipacket[s->ifcount] = c;
		s->ifcount++;
	}
	return 0;
}