summaryrefslogtreecommitdiff
path: root/vxi11_svc.c
blob: e5c4b38cd01d2d0afd43e608868b4beebb828638 (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
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
#define  _GNU_SOURCE

#include "vxi11.h"
#include "vxi11_server.h"
#include <stdio.h>
#include <stdlib.h>
#include <rpc/pmap_clnt.h>
#include <string.h>
#include <memory.h>
#include <poll.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define	VXI11_DEFAULT_TIMEOUT	1000

#ifndef SIG_PF
#define SIG_PF void(*)(int)
#endif

static void device_async_1(struct svc_req *rqstp, register SVCXPRT *transp) {
	union {
		Device_Link device_abort_1_arg;
	} argument;
	char *result;
	xdrproc_t _xdr_argument, _xdr_result;
	char *(*local)(char *, struct svc_req *);

	switch (rqstp->rq_proc) {
		case NULLPROC:
			(void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *) NULL);
			return;

		case device_abort:
			_xdr_argument = (xdrproc_t) xdr_Device_Link;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) device_abort_1_svc;
			break;

		default:
			svcerr_noproc(transp);
			return;
	}
	memset((char *) &argument, 0, sizeof(argument));
	if (!svc_getargs(transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		svcerr_decode(transp);
		return;
	}
	result = (*local)((char *) &argument, rqstp);
	if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
		svcerr_systemerr(transp);
	}
	if (!svc_freeargs(transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		fprintf(stderr, "%s", "unable to free arguments");
		exit(1);
	}
	return;
}

static void device_core_1(struct svc_req *rqstp, register SVCXPRT *transp) {
	union {
		Create_LinkParms create_link_1_arg;
		Device_WriteParms device_write_1_arg;
		Device_ReadParms device_read_1_arg;
		Device_GenericParms device_readstb_1_arg;
		Device_GenericParms device_trigger_1_arg;
		Device_GenericParms device_clear_1_arg;
		Device_GenericParms device_remote_1_arg;
		Device_GenericParms device_local_1_arg;
		Device_LockParms device_lock_1_arg;
		Device_Link device_unlock_1_arg;
		Device_EnableSrqParms device_enable_srq_1_arg;
		Device_DocmdParms device_docmd_1_arg;
		Device_Link destroy_link_1_arg;
		Device_RemoteFunc create_intr_chan_1_arg;
	} argument;
	char *result;
	xdrproc_t _xdr_argument, _xdr_result;
	char *(*local)(char *, struct svc_req *);

	switch (rqstp->rq_proc) {
		case NULLPROC:
			(void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *) NULL);
			return;

		case create_link:
			_xdr_argument = (xdrproc_t) xdr_Create_LinkParms;
			_xdr_result = (xdrproc_t) xdr_Create_LinkResp;
			local = (char *(*)(char *, struct svc_req *)) create_link_1_svc;
			break;

		case device_write:
			_xdr_argument = (xdrproc_t) xdr_Device_WriteParms;
			_xdr_result = (xdrproc_t) xdr_Device_WriteResp;
			local = (char *(*)(char *, struct svc_req *)) device_write_1_svc;
			break;

		case device_read:
			_xdr_argument = (xdrproc_t) xdr_Device_ReadParms;
			_xdr_result = (xdrproc_t) xdr_Device_ReadResp;
			local = (char *(*)(char *, struct svc_req *)) device_read_1_svc;
			break;

		case device_readstb:
			_xdr_argument = (xdrproc_t) xdr_Device_GenericParms;
			_xdr_result = (xdrproc_t) xdr_Device_ReadStbResp;
			local = (char *(*)(char *, struct svc_req *)) device_readstb_1_svc;
			break;

		case device_trigger:
			_xdr_argument = (xdrproc_t) xdr_Device_GenericParms;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) device_trigger_1_svc;
			break;

		case device_clear:
			_xdr_argument = (xdrproc_t) xdr_Device_GenericParms;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) device_clear_1_svc;
			break;

		case device_remote:
			_xdr_argument = (xdrproc_t) xdr_Device_GenericParms;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) device_remote_1_svc;
			break;

		case device_local:
			_xdr_argument = (xdrproc_t) xdr_Device_GenericParms;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) device_local_1_svc;
			break;

		case device_lock:
			_xdr_argument = (xdrproc_t) xdr_Device_LockParms;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) device_lock_1_svc;
			break;

		case device_unlock:
			_xdr_argument = (xdrproc_t) xdr_Device_Link;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) device_unlock_1_svc;
			break;

		case device_enable_srq:
			_xdr_argument = (xdrproc_t) xdr_Device_EnableSrqParms;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) device_enable_srq_1_svc;
			break;

		case device_docmd:
			_xdr_argument = (xdrproc_t) xdr_Device_DocmdParms;
			_xdr_result = (xdrproc_t) xdr_Device_DocmdResp;
			local = (char *(*)(char *, struct svc_req *)) device_docmd_1_svc;
			break;

		case destroy_link:
			_xdr_argument = (xdrproc_t) xdr_Device_Link;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) destroy_link_1_svc;
			break;

		case create_intr_chan:
			_xdr_argument = (xdrproc_t) xdr_Device_RemoteFunc;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) create_intr_chan_1_svc;
			break;

		case destroy_intr_chan:
			_xdr_argument = (xdrproc_t) xdr_void;
			_xdr_result = (xdrproc_t) xdr_Device_Error;
			local = (char *(*)(char *, struct svc_req *)) destroy_intr_chan_1_svc;
			break;

		default:
			svcerr_noproc(transp);
			return;
	}
	memset((char *) &argument, 0, sizeof(argument));
	if (!svc_getargs(transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		svcerr_decode(transp);
		return;
	}
	result = (*local)((char *) &argument, rqstp);
	if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
		svcerr_systemerr(transp);
	}
	if (!svc_freeargs(transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		fprintf(stderr, "%s", "unable to free arguments");
		exit(1);
	}
	return;
}

void vxi_main() {

	printf("VXI service started\n");

	register SVCXPRT *transp;

	pmap_unset(DEVICE_ASYNC, DEVICE_ASYNC_VERSION);
	pmap_unset(DEVICE_CORE, DEVICE_CORE_VERSION);

	transp = svcudp_create(RPC_ANYSOCK);
	if (transp == NULL) {
		fprintf(stderr, "%s", "cannot create udp service.");
		exit(1);
	}
	if (!svc_register(transp, DEVICE_ASYNC, DEVICE_ASYNC_VERSION, device_async_1, IPPROTO_UDP)) {
		fprintf(stderr, "%s", "unable to register (DEVICE_ASYNC, DEVICE_ASYNC_VERSION, udp).");
		exit(1);
	}
	if (!svc_register(transp, DEVICE_CORE, DEVICE_CORE_VERSION, device_core_1, IPPROTO_UDP)) {
		fprintf(stderr, "%s", "unable to register (DEVICE_CORE, DEVICE_CORE_VERSION, udp).");
		exit(1);
	}

	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
	if (transp == NULL) {
		fprintf(stderr, "%s", "cannot create tcp service.");
		exit(1);
	}
	if (!svc_register(transp, DEVICE_ASYNC, DEVICE_ASYNC_VERSION, device_async_1, IPPROTO_TCP)) {
		fprintf(stderr, "%s", "unable to register (DEVICE_ASYNC, DEVICE_ASYNC_VERSION, tcp).");
		exit(1);
	}
	if (!svc_register(transp, DEVICE_CORE, DEVICE_CORE_VERSION, device_core_1, IPPROTO_TCP)) {
		fprintf(stderr, "%s", "unable to register (DEVICE_CORE, DEVICE_CORE_VERSION, tcp).");
		exit(1);
	}

	int i, j;
	struct pollfd *my_pollfd = NULL;
	int last_max_pollfd = 0;

	while (1) {
		int max_pollfd = svc_max_pollfd;
		if (max_pollfd == 0 && svc_pollfd == NULL)
			break;

		if (last_max_pollfd != max_pollfd) {
			struct pollfd *new_pollfd = realloc(my_pollfd, sizeof(struct pollfd) * max_pollfd);

			if (new_pollfd == NULL) {
				break;
			}

			my_pollfd = new_pollfd;
			last_max_pollfd = max_pollfd;
		}

		for (i = 0; i < max_pollfd; ++i) {
			my_pollfd[i].fd = svc_pollfd[i].fd;
			my_pollfd[i].events = svc_pollfd[i].events | POLLRDHUP; // add the "socket is dead" event
			my_pollfd[i].revents = 0;
		}

		j = poll(my_pollfd, max_pollfd, VXI11_DEFAULT_TIMEOUT);

		for (i = 0; i < max_pollfd; i++) {
			if (my_pollfd[i].revents & POLLRDHUP)
				vxi11_deadsocketcallback(my_pollfd[i].fd);
		}

		switch (j) {
			case -1:
				break;
			case 0:
				continue;
			default:
				svc_getreq_poll(my_pollfd, i);
				continue;
		}
		break;
	}

	//svc_run();
	//fprintf(stderr, "%s", "svc_run returned");
}