File: io.c

package info (click to toggle)
ax25-utils 2.1.42a-6
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,172 kB
  • ctags: 2,417
  • sloc: ansic: 30,184; sh: 1,068; makefile: 908
file content (431 lines) | stat: -rw-r--r-- 8,423 bytes parent folder | download | duplicates (2)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * This is an I/O library for file descriptors (or sockets). It is
 * sort of like stdio but a few differences. All reads and writes
 * are buffered with user defined buffer sizes (ie. reading and
 * writing to SOCK_SEQPACKET sockets is safe). Also this library
 * provides user selectable end-of-line conversions and very limited
 * telnet option negotiation (mostly just answering no to all proposals).
 *
 * I'm not at all sure this is a good idea and parts of the lib (like
 * the telnet stuff) are a mess but I needed something for my AX.25
 * projects. So here it is.
 *
 * After you get a file descriptor from open() or connect() you call
 * init_io(). Then you can use usputc(), usprintf() etc. to write to and
 * usgetc, usgets() etc. to read from the file descriptor. usflush()
 * causes any pending output to be sent even if the buffer isn't full
 * and end_io() flushes the file descriptor and frees all buffers.
 *
 * Tomi Manninen, OH2BNS, <tomi.manninen@hut.fi>
 */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#include "io.h"

#define StdoutFd	STDIN_FILENO
#define BUFLEN		1500

/* Telnet command characters */
#define IAC			255	/* Interpret as command */
#define WILL			251
#define WONT			252
#define DO			253
#define DONT			254
#define SB			250	/* Subnegotiation begin */
#define SE			240	/* Subnegotiation end   */

/* Telnet options */
#define TN_TRANSMIT_BINARY	0
#define TN_ECHO			1
#define TN_SUPPRESS_GA		3
#define TN_STATUS		5
#define TN_TIMING_MARK		6

/* Linemode options */
#define TN_LINEMODE		34
#define TN_LINEMODE_MODE	1
#define TN_LINEMODE_MODE_EDIT	1

struct io {
	int fd;			/* socket index				*/
	char *eol;		/* end-of-line sequence			*/
	int eolmode;		/* end-of-line translation on/off	*/
	int telnetmode;		/* telnet option negotiation on/off	*/
	int tn_echo;		/* will/wont echo			*/
	int tn_linemode;	/* will/wont linemode			*/
	int size;		/* size of the packet in input buffer	*/
	int paclen;		/* paclen				*/
	unsigned char *ibuf;	/* input buffer				*/
	unsigned char *obuf;	/* output buffer			*/
	int ipointer;		/* input pointer			*/
	int opointer;		/* output pointer			*/

	struct io *next;
};

static struct io *Iolist = NULL;

static struct io *itop(int fd)
{
	struct io *iop;

	for (iop = Iolist; iop != NULL && iop->fd != fd; iop = iop->next)
		;
	if (iop == NULL)
		errno = 10000; /* "unknown error 10000" */
	return iop;
}

int init_io(int fd, int paclen, char *eol)
{
	struct io *new;

	if (itop(fd) != NULL)
		return -1;
	if ((new = malloc(sizeof(struct io))) == NULL)
		return -1;
	new->ibuf	= malloc(BUFLEN);
	new->obuf	= malloc(paclen);
	new->fd		= fd;
	new->eol	= eol;
	new->eolmode	= EOLMODE_TEXT;
	new->telnetmode = 0;
	new->tn_echo	= 0;
	new->tn_linemode = 0;
	new->size	= 0;
	new->paclen	= paclen;
	new->ipointer	= 0;
	new->opointer	= 0;
	if (new->ibuf == NULL || new->obuf == NULL) {
		free(new->ibuf);
		free(new->obuf);
		free(new);
		return -1;
	}
	new->next = Iolist;
	Iolist = new;
	return 0;
}

int set_paclen(int fd, int paclen)
{
	struct io *iop;
	unsigned char *old_obuf;

	usflush(fd);
	if ((iop = itop(fd)) == NULL)
		return -1;
	if (paclen == iop->paclen)
		return paclen;
	old_obuf = iop->obuf;
	if ((iop->obuf = malloc(paclen)) == NULL) {
		iop->obuf = old_obuf;
		return -1;
	}
	free(old_obuf);
	iop->paclen = paclen;
	return paclen;
}

int set_eolmode(int fd, int newmode)
{
	struct io *iop;
	int oldmode;

	if ((iop = itop(fd)) == NULL)
		return -1;
	oldmode = iop->eolmode;
	iop->eolmode = newmode;
	return oldmode;
}

int set_telnetmode(int fd, int newmode)
{
	struct io *iop;
	int oldmode;

	if ((iop = itop(fd)) == NULL)
		return -1;
	oldmode = iop->telnetmode;
	iop->telnetmode = newmode;
	return oldmode;
}

void end_io(int fd)
{
	struct io *iop;

	usflush(fd);
	if ((iop = itop(fd)) == NULL)
		return;
	free(iop->ibuf);
	free(iop->obuf);
	iop->fd = -1;
	return;
}

int usflush(int fd)
{
	struct io *iop;
	int ret;

	if ((iop = itop(fd)) == NULL)
		return -1;
	if (iop->opointer == 0)
		return 0;
	ret = write(iop->fd, iop->obuf, iop->opointer);
	if (ret < 0)
		return -1;
	if (ret < iop->opointer) {
		memmove(iop->obuf, &iop->obuf[ret], iop->opointer - ret);
		iop->opointer = ret;
	} else
		iop->opointer = 0;
	return 0;
}

static int rsendchar(unsigned char c, struct io *iop)
{
	iop->obuf[iop->opointer++] = c;
	if (iop->opointer >= iop->paclen)
		return usflush(iop->fd);
	return 1;
}

static int rrecvchar(struct io *iop)
{
	if (iop->ipointer >= iop->size) {
		iop->size = read(iop->fd, iop->ibuf, BUFLEN);
		if (iop->size == -1)
			return -1;
		if (iop->size == 0) {
			errno = 0;
			return -1;
		}
		iop->ipointer = 0;
	}
	return iop->ibuf[iop->ipointer++];
}

int usputc(unsigned char c, int fd)
{
	struct io *iop;
	char *cp;

	if ((iop = itop(fd)) == NULL)
		return -1;
	if (iop->telnetmode && c == IAC) {
		if (rsendchar(IAC, iop) == -1)
			return -1;
		return rsendchar(IAC, iop);
	}
	if (iop->eolmode == EOLMODE_TEXT && c == '\n') {
		for (cp = iop->eol; *cp; cp++) {
			if (rsendchar(*cp, iop) == -1)
				return -1;
		}
		return 1;
	}
	return rsendchar(c, iop);
}

int usgetc(int fd)
{
	struct io *iop;
	int c, opt;
	char *cp;

	opt = 0; /* silence warning */
	if ((iop = itop(fd)) == NULL)
		return -1;
	if ((c = rrecvchar(iop)) == -1)
		return -1;
	if (iop->telnetmode && c == IAC) {
		if ((c = rrecvchar(iop)) == -1)
			return -1;
		if (c > 249 && c < 255 && (opt = rrecvchar(iop)) == -1)
			return -1;
		switch(c) {
		case SB:
			if ((c = rrecvchar(iop)) == -1)
				return -1;
			if ((opt = rrecvchar(iop)) == -1)
				return -1;
			while (!(c == IAC && opt == SE)) {
				c = opt;
				if ((opt = rrecvchar(iop)) == -1)
					return -1;
			}
			break;
		case WILL:
			if (opt == TN_LINEMODE && iop->tn_linemode) {
				rsendchar(IAC, iop);
				rsendchar(SB,  iop);
				rsendchar(TN_LINEMODE, iop);
				rsendchar(TN_LINEMODE_MODE, iop);
				rsendchar(TN_LINEMODE_MODE_EDIT, iop);
				rsendchar(IAC, iop);
				rsendchar(SE,  iop);
			} else {
				rsendchar(IAC,  iop);
				rsendchar(DONT, iop);
				rsendchar(opt,  iop);
			}
			usflush(fd);
			break;
		case DO:
			if (!(opt == TN_ECHO && iop->tn_echo)) {
				rsendchar(IAC,  iop);
				rsendchar(WONT, iop);
				rsendchar(opt,  iop);
				usflush(fd);
			}
			break;
		case IAC:	/* Escaped IAC */
			return IAC;
			break;
		case WONT:
		case DONT:
		default:
			break;
		}
		return usgetc(fd);
	}
	if (iop->eolmode == EOLMODE_TEXT && c == iop->eol[0]) {
		for (cp = iop->eol + 1; *cp; cp++) {
			if (rrecvchar(iop) == -1)
				return -1;
		}
		c = '\n';
	}
	return c;
}

char *usgets(char *buf, int buflen, int fd)
{
	int c, len = 0;

	while (len < (buflen - 1)) {
		c = usgetc(fd);
		if (c == -1) {
			if (len > 0) {
				buf[len] = 0;
				return buf;
			} else
				return NULL;
		}
		/* NUL also interpreted as EOL */
		if (c == '\n' || c == '\r' || c == 0) {
			buf[len] = 0;
			return buf;
		}
		buf[len++] = c;
	}
	buf[buflen - 1] = 0;
	return buf;
}

char *readline(int fd)
{
	static char buf[BUFLEN];

	return usgets(buf, BUFLEN, fd);
}

static int usvprintf(int fd, const char *fmt, va_list args)
{
	static char buf[BUFLEN];
	int len, i;

	len = vsprintf(buf, fmt, args);
	for (i = 0; i < len; i++)
		if (usputc(buf[i], fd) == -1)
			return -1;
	return len;
}

int usprintf(int fd, const char *fmt, ...)
{
	va_list args;
	int len;

	va_start(args, fmt);
	len = usvprintf(fd, fmt, args);
	va_end(args);
	return len;
}

int tprintf(const char *fmt, ...)
{
	va_list args;
	int len;

	va_start(args, fmt);
	len = usvprintf(StdoutFd, fmt, args);
	va_end(args);
	return len;
}

int usputs(const char *s, int fd)
{
	while (*s) {
		if (usputc(*s, fd) == -1)
			return -1;
		s++;
	}
	return 0;
}

int tputs(const char *s)
{
	while (*s) {
		if (usputc(*s, StdoutFd) == -1)
			return -1;
		s++;
	}
	return 0;
}

void tn_do_linemode(int fd)
{
	struct io *iop;

	if ((iop = itop(fd)) == NULL)
		return;
	rsendchar(IAC, iop);
	rsendchar(DO, iop);
	rsendchar(TN_LINEMODE, iop);
	iop->tn_linemode = 1;
}

void tn_will_echo(int fd)
{
	struct io *iop;

	if ((iop = itop(fd)) == NULL)
		return;
	rsendchar(IAC, iop);
	rsendchar(WILL, iop);
	rsendchar(TN_ECHO, iop);
	iop->tn_echo = 1;
}

void tn_wont_echo(int fd)
{
	struct io *iop;

	if ((iop = itop(fd)) == NULL)
		return;
	rsendchar(IAC, iop);
	rsendchar(WONT, iop);
	rsendchar(TN_ECHO, iop);
	iop->tn_echo = 0;
}