aboutsummaryrefslogtreecommitdiff
path: root/contrib/ngatm/sscop/sscop_main.c
blob: a35d7a6c2da5c22d862d318e0cd65f63f8ccf0af (plain) (blame)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * Copyright (c) 2001-2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 * 	All rights reserved.
 *
 * Author: Harti Brandt <harti@freebsd.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Begemot: libunimsg/sscop/sscop_main.c,v 1.5 2005/05/23 11:46:17 brandt_h Exp $
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <assert.h>
#include <err.h>

#include <netnatm/unimsg.h>
#include <netnatm/saal/sscop.h>
#include "common.h"

static int sigusr1;		/* got SIGUSR1 */
static int unidir;		/* write only user */
static int end_at_eof = 1;	/* send RELEASE_request at user EOF */

static volatile int ready;	/* flag if connection is established */
static volatile int finished;	/* got release confirm or indication */

static const char usgtxt[] = "\
SSCOP transport protocol\n\
Usage: sscop [-h] [-Fbefirwx3] [-ap=v] [-lN] [-tt=m] [-v X] [-V X] [-W N]\n\
Options:\n\
  -F	  use framing for sscop also\n\
  -V X	  set verbose flags to hex X\n\
  -W N	  set initial window to N\n\
  -a p=v  set parameter 'p' to 'v'\n\
  -b	  enable robustness enhancement\n\
  -e	  don't RELEASE_request on user EOF\n\
  -f	  use begemot frame functions for user fd\n\
  -h	  print this info\n\
  -i	  use user fd only for output\n\
  -lN	  loose every nth message\n\
  -r	  reverse user and sscop file descriptors\n\
  -t t=m  set timer 't' to 'm' milliseconds\n\
  -v X	  set sscop verbose flags to hex X\n\
  -w	  don't start conversation\n\
  -x	  enable POLL after retransmission\n\
  -3	  redirect output to fd 3\n\
Timers are cc, poll, ka, nr or idle; parameters are j, k, cc, pd or stat.\n";

static void sscop_send_manage(struct sscop *, void *,
	enum sscop_maasig, struct uni_msg *, u_int, u_int);
static void sscop_send_upper(struct sscop *, void *, enum sscop_aasig,
	struct SSCOP_MBUF_T *, u_int);
static void sscop_send_lower(struct sscop *, void *, struct SSCOP_MBUF_T *);

static const struct sscop_funcs sscop_funcs = {
	sscop_send_manage,
	sscop_send_upper,
	sscop_send_lower,
	sscop_verbose,
	sscop_start_timer,
	sscop_stop_timer
};

/*
 * SSCOP file descriptor is ready. Allocate and read one message
 * and dispatch a signal.
 */
#ifdef USE_LIBBEGEMOT
static void
proto_infunc(int fd, int mask __unused, void *uap)
#else
static void
proto_infunc(evContext ctx __unused, void *uap, int fd, int mask __unused)
#endif
{
	struct uni_msg *m;

	if ((m = proto_msgin(fd)) != NULL)
		sscop_input((struct sscop *)uap, m);
}

/*
 * User input. Allocate and read message and dispatch signal.
 */
#ifdef USE_LIBBEGEMOT
static void
user_infunc(int fd, int mask __unused, void *uap)
#else
static void
user_infunc(evContext ctx __unused, void *uap, int fd, int mask __unused)
#endif
{
	struct uni_msg *m;

	if ((m = user_msgin(fd)) != NULL)
		sscop_aasig((struct sscop *)uap, SSCOP_DATA_request, m, 0);

	else if (end_at_eof)
		sscop_aasig((struct sscop *)uap, SSCOP_RELEASE_request, 0, 0);
}

static void
onusr1(int s __unused)
{
	sigusr1++;
}

int
main(int argc, char *argv[])
{
	int opt;
	struct sscop *sscop;
	struct sscop_param param;
	struct sigaction sa;
	int wait = 0;
	u_int mask;
#ifndef USE_LIBBEGEMOT
	evEvent ev;
#endif

	/*
	 * Default is to have the USER on stdin and SSCOP on stdout
	 */
	sscop_fd = 0;
	user_fd = 1;
	user_out_fd = -1;

	memset(&param, 0, sizeof(param));
	param.maxk = MAXUSRMSG;
	param.maxj = 0;
	param.maxcc = 4;
	mask = SSCOP_SET_MAXK | SSCOP_SET_MAXJ | SSCOP_SET_MAXCC;

	while((opt = getopt(argc, argv, "3a:befFhil:rt:v:V:wW:x")) != -1)
		switch(opt) {

		  case '3':
			user_out_fd = 3;
			break;

		  case 'e':
			end_at_eof = 0;
			break;

		  case 'f':
			useframe = 1;
			break;

		  case 'F':
			sscopframe = 1;
			break;

		  case 'h':
			fprintf(stderr, usgtxt);
			exit(0);

		  case 'i':
			unidir++;
			break;

		  case 'l':
			loose = strtoul(optarg, NULL, 0);
			break;

		  case 'r':
			sscop_fd = 1;
			user_fd = 0;
			break;

		  case 'v':
			sscop_vflag = strtoul(optarg, NULL, 16);
			break;

		  case 'V':
			verbose = strtoul(optarg, NULL, 16);
			break;

		  case 'w':
			wait = 1;
			break;

		  case 'a':
		  case 't':
		  case 'b':
		  case 'x':
		  case 'W':
			parse_param(&param, &mask, opt, optarg);
			break;
		}

	if(user_out_fd < 0)
		user_out_fd = user_fd;

#ifndef USE_LIBBEGEMOT
	if (evCreate(&evctx))
		err(1, "evCreate");
#endif

	/*
	 * Catch USR1
	 */
	sa.sa_handler = onusr1;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
	if(sigaction(SIGUSR1, &sa, NULL))
		err(1, "sigaction(SIGUSR1)");

	/*
	 * Allocate and initialize SSCOP
	 */
	if ((sscop = sscop_create(NULL, &sscop_funcs)) == NULL)
		err(1, NULL);
	sscop_setdebug(sscop, sscop_vflag);
	if ((errno = sscop_setparam(sscop, &param, &mask)) != 0)
		err(1, "can't set sscop parameters %#x", mask);

	/*
	 * Register sscop fd
	 */
#ifdef USE_LIBBEGEMOT
	if ((sscop_h = poll_register(sscop_fd, proto_infunc,
	    sscop, POLL_IN)) == -1)
		err(1, "can't select on sscop fd");
#else
	if (evSelectFD(evctx, sscop_fd, EV_READ, proto_infunc, sscop, &sscop_h))
		err(1, "can't select on sscop fd");
#endif

	/*
	 * if we are active - send establish request
	 */
	if(!wait)
		sscop_aasig(sscop, SSCOP_ESTABLISH_request, NULL, 1);

	/*
	 * Run protocol until it get's ready
	 */
	while (sscop_fd >= 0 && !ready) {
#ifdef USE_LIBBEGEMOT
		poll_dispatch(1);
#else
		if (evGetNext(evctx, &ev, EV_WAIT) == 0) {
			if (evDispatch(evctx, ev))
				err(1, "dispatch event");
		} else if (errno != EINTR)
			err(1, "get event");
#endif
	}

	/*
	 * If this led to a closed file - exit.
	 */
	if (sscop_fd < 0) {
		VERBOSE(("SSCOP file descriptor closed - exiting"));
		sscop_destroy(sscop);
		return 0;
	}

	VERBOSE(("READY - starting data transfer"));

	if (!unidir &&
#ifdef USE_LIBBEGEMOT
	    ((user_h = poll_register(user_fd, user_infunc, sscop, POLL_IN)) == -1))
#else
	    evSelectFD(evctx, user_fd, EV_READ, user_infunc, sscop, &user_h))
#endif
		err(1, "can't select on sscop fd");

	while (!sigusr1 && sscop_fd >= 0) {
#ifdef USE_LIBBEGEMOT
		poll_dispatch(1);
#else
		if (evGetNext(evctx, &ev, EV_WAIT) == 0) {
			if (evDispatch(evctx, ev))
				err(1, "dispatch event");
		} else if (errno != EINTR)
			err(1, "get event");
#endif
	}

	if (sigusr1 && sscop_fd >= 0) {
		/*
		 * Release if we still have the connection
		 */
		sscop_aasig(sscop, SSCOP_RELEASE_request, NULL, 0);
		while (!finished && sscop_fd >= 0) {
#ifdef USE_LIBBEGEMOT
			poll_dispatch(1);
#else
			if (evGetNext(evctx, &ev, EV_WAIT) == 0) {
				if (evDispatch(evctx, ev))
					err(1, "dispatch event");
			} else if (errno != EINTR)
				err(1, "get event");
#endif
		}
	}

	VERBOSE(("SSCOP file descriptor closed - exiting"));
	sscop_destroy(sscop);

	return (0);
}



/*
 * AAL OUTPUT
 */
static void
sscop_send_lower(struct sscop *sscop __unused, void *arg __unused,
    struct SSCOP_MBUF_T *m)
{
	proto_msgout(m);
}


/*
 * Write the message to the user and move the window
 */
static void
uoutput(struct sscop *sscop, struct uni_msg *m)
{
	user_msgout(m);
	sscop_window(sscop, +1);
}

/*
 * SSCOP AA-SIGNALS
 */
static void
sscop_send_upper(struct sscop *sscop, void *arg __unused, enum sscop_aasig sig,
    struct SSCOP_MBUF_T *m, u_int p __unused)
{
	VERBOSE(("--> got aa %d(%s)", sig, sscop_signame(sig)));

	switch (sig) {

	  case SSCOP_RELEASE_indication:
		if (end_at_eof) {
			VERBOSE((" ... exiting"));
#ifdef USE_LIBBEGEMOT
			poll_unregister(sscop_h);
#else
			evDeselectFD(evctx, sscop_h);
#endif
			(void)close(sscop_fd);
			sscop_fd = -1;
		}
		finished++;
		if (m)
			uni_msg_destroy(m);
		break;

	  case SSCOP_RELEASE_confirm:
		if (end_at_eof) {
			VERBOSE((" ... exiting"));
#ifdef USE_LIBBEGEMOT
			poll_unregister(sscop_h);
#else
			evDeselectFD(evctx, sscop_h);
#endif
			(void)close(sscop_fd);
			sscop_fd = -1;
		}
		finished++;
		break;

	  case SSCOP_ESTABLISH_indication:
		sscop_aasig(sscop, SSCOP_ESTABLISH_response, NULL, 1);
		ready++;
		if (m)
			uni_msg_destroy(m);
		break;

	  case SSCOP_ESTABLISH_confirm:
		ready++;
		if (m)
			uni_msg_destroy(m);
		break;

	  case SSCOP_DATA_indication:
		assert(m != NULL);
		uoutput(sscop, m);
		break;

	  case SSCOP_UDATA_indication:
		assert(m != NULL);
		VERBOSE(("UDATA.indication ignored"));
		uni_msg_destroy(m);
		break;

	  case SSCOP_RECOVER_indication:
		sscop_aasig(sscop, SSCOP_RECOVER_response, NULL, 0);
		break;

	  case SSCOP_RESYNC_indication:
		sscop_aasig(sscop, SSCOP_RESYNC_response, NULL, 0);
		if (m)
			uni_msg_destroy(m);
		break;

	  case SSCOP_RESYNC_confirm:
		break;

	  case SSCOP_RETRIEVE_indication:
	  case SSCOP_RETRIEVE_COMPL_indication:
		warnx("Ooops. A retrieve indication");
		abort();

	  case SSCOP_ESTABLISH_request:
	  case SSCOP_RELEASE_request:
	  case SSCOP_ESTABLISH_response:
	  case SSCOP_DATA_request:
	  case SSCOP_UDATA_request:
	  case SSCOP_RECOVER_response:
	  case SSCOP_RESYNC_request:
	  case SSCOP_RESYNC_response:
	  case SSCOP_RETRIEVE_request:
		warnx("bad signal for this direction");
		abort();
	}
}

/*
 * This get's called for MAAL
 */
static void
sscop_send_manage(struct sscop *sscop __unused, void *arg __unused,
    enum sscop_maasig sig, struct uni_msg *m, u_int error, u_int cnt)
{
	VERBOSE(("--> got maa %d(%s)", sig, sscop_msigname(sig)));

	switch (sig) {

	  case SSCOP_MDATA_indication:
		VERBOSE(("MDATA.indication ignored"));
		uni_msg_destroy(m);
		break;

	  case SSCOP_MERROR_indication:
		VERBOSE(("MAAL-ERROR.indication '%c' %u", error, cnt));
		break;

	  case SSCOP_MDATA_request:
		warnx("bad signal for this direction");
		abort();
	}
}