aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/auth/factotum/xio.c
blob: 116fccaf58e13110bda2bb376a4535efe590d560 (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
153
154
155
156
157
158
159
160
161
162
163
164
#include "std.h"
#include "dat.h"

static Ioproc *cache[5];
static int ncache;

static Ioproc*
xioproc(void)
{
	Ioproc *c;
	int i;

	for(i=0; i<ncache; i++){
		if(c = cache[i]){
			cache[i] = nil;
			return c;
		}
	}

	return ioproc();
}

static void
closexioproc(Ioproc *io)
{
	int i;

	for(i=0; i<ncache; i++)
		if(cache[i] == nil){
			cache[i] = io;
			return;
		}

	closeioproc(io);
}

int
xiodial(char *ds, char *local, char *dir, int *cfdp)
{
	int fd;
	Ioproc *io;

	if((io = xioproc()) == nil)
		return -1;
	fd = iodial(io, ds, local, dir, cfdp);
	closexioproc(io);
	return fd;
}

void
xioclose(int fd)
{
	Ioproc *io;

	if((io = xioproc()) == nil){
		close(fd);
		return;
	}

	ioclose(io, fd);
	closexioproc(io);
}

int
xiowrite(int fd, void *v, int n)
{
	int m;
	Ioproc *io;

	if((io = xioproc()) == nil)
		return -1;
	m = iowrite(io, fd, v, n);
	closexioproc(io);
	if(m != n)
		return -1;
	return n;
}

static long
_ioauthdial(va_list *arg)
{
	char *net;
	char *dom;
	int fd;

	net = va_arg(*arg, char*);
	dom = va_arg(*arg, char*);
	fd = _authdial(net, dom);
	if(fd < 0)
		fprint(2, "authdial: %r\n");
	return fd;
}

int
xioauthdial(char *net, char *dom)
{
	int fd;
	Ioproc *io;

	if((io = xioproc()) == nil)
		return -1;
	fd = iocall(io, _ioauthdial, net, dom);
	closexioproc(io);
	return fd;
}

static long
_ioasrdresp(va_list *arg)
{
	int fd;
	void *a;
	int n;

	fd = va_arg(*arg, int);
	a = va_arg(*arg, void*);
	n = va_arg(*arg, int);

	return _asrdresp(fd, a, n);
}

int
xioasrdresp(int fd, void *a, int n)
{
	Ioproc *io;

	if((io = xioproc()) == nil)
		return -1;

	n = iocall(io, _ioasrdresp, fd, a, n);
	closexioproc(io);
	return n;
}

static long
_ioasgetticket(va_list *arg)
{
	int asfd;
	char *trbuf;
	char *tbuf;

	asfd = va_arg(*arg, int);
	trbuf = va_arg(*arg, char*);
	tbuf = va_arg(*arg, char*);

	return _asgetticket(asfd, trbuf, tbuf);
}

int
xioasgetticket(int fd, char *trbuf, char *tbuf)
{
	int n;
	Ioproc *io;

	if((io = xioproc()) == nil)
		return -1;

	n = iocall(io, _ioasgetticket, fd, trbuf, tbuf);
	closexioproc(io);
	if(n != 2*TICKETLEN)
		n = -1;
	else
		n = 0;
	return n;
}