summaryrefslogtreecommitdiff
path: root/appl/lib/msgio.b
blob: 035b178397027dbf449773b5f113de5ce18fef4f (plain)
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
implement Msgio;

# probably need Authio or Auth instead, to include Authinfo, Certificate and signing operations?
# eliminates certificates and sigalgs from Keyring
# might be better just to have mp.m and sec.m?
# general signature module?
# Keyring->dhparams is is only needed by createsignerkey (and others creating Authinfo)
# should also improve pkcs

include "sys.m";
	sys: Sys;

include "keyring.m";

include "msgio.m";

init()
{
	sys = load Sys Sys->PATH;
}

seterr(r: int)
{
	if(r > 0)
		sys->werrstr("input or format error");
	else if(r == 0)
		sys->werrstr("hungup");
}

#
# i/o on a channel that might or might not retain record boundaries
#
getmsg(fd: ref Sys->FD): array of byte
{
	num := array[5] of byte;
	r := sys->readn(fd, num, len num);
	if(r != len num) {
		seterr(r);
		return nil;
	}
	h := string num;
	if(h[0] == '!')
		m := int h[1:];
	else
		m = int h;
	if(m < 0 || m > Maxmsg) {
		seterr(1);
		return nil;
	}
	buf := array[m] of byte;
	r = sys->readn(fd, buf, m);
	if(r != m){
		seterr(r);
		return nil;
	}
	if(h[0] == '!'){
		sys->werrstr(string buf);
		return nil;
	}
	return buf;
}

sendmsg(fd: ref Sys->FD, buf: array of byte, n: int): int
{
	if(sys->fprint(fd, "%4.4d\n", n) < 0)
		return -1;
	return sys->write(fd, buf, n);
}

senderrmsg(fd: ref Sys->FD, s: string): int
{
	buf := array of byte s;
	if(sys->fprint(fd, "!%3.3d\n", len buf) < 0)
		return -1;
	if(sys->write(fd, buf, len buf) <= 0)
		return -1;
	return 0;
}

#
# i/o on a delimited channel
#
getbuf(fd: ref Sys->FD, buf: array of byte, n: int): (int, string)
{
	n = sys->read(fd, buf, n);
	if(n <= 0){
		seterr(n);
		return (-1, sys->sprint("%r"));
	}
	if(buf[0] == byte 0)
		return (n, nil);
	if(buf[0] == byte 16rFF){
		# garbled, possibly the wrong encryption
		return (-1, "failure");
	}
	# error string
	if(--n < 1)
		return (-1, "unknown");
	return (-1, string buf[1:]);
}

getbytearray(fd: ref Sys->FD): (array of byte, string)
{
	buf := array[Maxmsg] of byte;
	(n, err) := getbuf(fd, buf, len buf);
	if(n < 0)
		return (nil, err);
	return (buf[1: n], nil);
}

getstring(fd: ref Sys->FD): (string, string)
{
	(a, err) := getbytearray(fd);
	if(a != nil)
		return (string a, err);
	return (nil, err);
}

putbuf(fd: ref Sys->FD, data: array of byte, n: int): int
{
	buf := array[Maxmsg] of byte;
	if(n < 0) {
		buf[0] = byte 16rFF;
		n = -n;
	}else
		buf[0] = byte 0;
	if(n >= Maxmsg)
		n = Maxmsg-1;
	buf[1:] = data;
	return sys->write(fd, buf, n+1);
}

putstring(fd: ref Sys->FD, s: string): int
{
	a := array of byte s;
	return putbuf(fd, a, len a);
}

putbytearray(fd: ref Sys->FD, a: array of byte, n: int): int
{
	if(n > len a)
		n = len a;
	return putbuf(fd, a, n);
}

puterror(fd: ref Sys->FD, s: string): int
{
	if(s == nil)
		s = "unknown";
	a := array of byte s;
	return putbuf(fd, a, -len a);
}