1 /* ttyprot.c - utility routines to deal with the rtty protocol
2  * vixie 12Sep91 [new]
3  */
4 
5 #ifndef LINT
6 static char RCSid[] = "$Id: ttyprot.c,v 1.10 2001/03/24 21:14:30 vixie Exp $";
7 #endif
8 
9 /* Copyright (c) 1996 by Internet Software Consortium.
10  *
11  * Permission to use, copy, modify, and distribute this software for any
12  * purpose with or without fee is hereby granted, provided that the above
13  * copyright notice and this permission notice appear in all copies.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
16  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
18  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
19  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
20  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
21  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22  * SOFTWARE.
23  */
24 
25 #include <sys/param.h>
26 #include <sys/uio.h>
27 
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <termios.h>
32 #include <unistd.h>
33 
34 #include "rtty.h"
35 #include "misc.h"
36 #include "ttyprot.h"
37 
38 #if DEBUG
39 extern	int		Debug;
40 #endif
41 
42 int
tp_senddata(int fd,const u_char * buf,int len,int typ)43 tp_senddata(int fd, const u_char *buf, int len, int typ) {
44 	struct iovec iov[2];
45 	ttyprot t;
46 	int n = 0;
47 
48 #if DEBUG
49 	if (Debug >= 5) {
50 		fprintf(stderr, "tp_senddata(fd=%d, buf=\"", fd);
51 		cat_v(stderr, buf, len);
52 		fprintf(stderr, "\", len=%d, typ=%d)\r\n", len, typ);
53 	}
54 #endif
55 	t.f = htons(typ);
56 	iov[0].iov_base = (caddr_t)&t;
57 	iov[0].iov_len = TP_FIXED;
58 	while (len > 0) {
59 		int i = min(len, TP_MAXVAR);
60 
61 		t.i = htons(i);
62 		iov[1].iov_base = (caddr_t)buf;
63 		iov[1].iov_len = i;
64 		buf += i;
65 		len -= i;
66 		i = writev(fd, iov, 2);
67 		if (i < 0)
68 			break;
69 		n += i;
70 	}
71 	return (n);
72 }
73 
74 int
tp_sendctl(int fd,u_int f,u_int i,u_char * c)75 tp_sendctl(int fd, u_int f, u_int i, u_char *c) {
76 	struct iovec iov[2];
77 	ttyprot t;
78 	int len = c ?min(strlen((char *)c), TP_MAXVAR) :0;
79 	int il = 0;
80 
81 #if DEBUG
82 	if (Debug >= 5) {
83 		fprintf(stderr, "tp_sendctl(fd=%d, f=%04x, i=%d, c=\"",
84 			fd, f, i);
85 		cat_v(stderr, c ?c :(u_char*)"", len);
86 		fprintf(stderr, "\")\r\n");
87 	}
88 #endif
89 	t.f = htons(f);
90 	t.i = htons(i);
91 	iov[il].iov_base = (caddr_t)&t;
92 	iov[il].iov_len = TP_FIXED;
93 	il++;
94 	if (c) {
95 		iov[il].iov_base = (caddr_t)c;
96 		iov[il].iov_len = len;
97 		il++;
98 	}
99 	return (writev(fd, iov, il));
100 }
101 
102 int
tp_getdata(int fd,ttyprot * tp)103 tp_getdata(int fd, ttyprot *tp) {
104 	int len = ntohs(tp->i);
105 	int nchars;
106 
107 	if ((nchars = read(fd, tp->c, len)) != len) {
108 		dprintf(stderr, "tp_getdata: read=%d(%d) fd%d: ",
109 			nchars, len, fd);
110 		if (nchars < 0)
111 			perror("read#2");
112 		else
113 			fputc('\n', stderr);
114 		return (0);
115 	}
116 #ifdef DEBUG
117 	if (Debug >= 5) {
118 		fprintf(stderr, "tp_getdata(fd%d, len%d): got %d bytes",
119 			fd, len, nchars);
120 		if (Debug >= 6) {
121 			fputs(": \"", stderr);
122 			cat_v(stderr, tp->c, nchars);
123 			fputs("\"", stderr);
124 		}
125 		fputc('\n', stderr);
126 	}
127 #endif
128 	return (nchars);
129 }
130