summaryrefslogtreecommitdiffstats
path: root/call/yapp.c
blob: 3d7bf452f20c6292c57c15d2d2ea59822aedc788 (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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/* yapp.c
 *
 * Copyright (C) 1994 by Jonathan Naylor
 *
 * This module implements the YAPP file transfer protocol as defined by Jeff
 * Jacobsen WA7MBL in the files yappxfer.doc and yappxfer.pas.
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the license, or (at your option) any later version.
 */

/*
 * Yapp C and Resume support added by S N Henson.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <stdio.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>
#include <linux/ax25.h>
#include <sys/stat.h>

#include "call.h"

#define	TIMEOUT		300	/* 5 Minutes */

#define	NUL		0x00
#define	SOH		0x01
#define	STX		0x02
#define	ETX		0x03
#define	EOT		0x04
#define	ENQ		0x05
#define	ACK		0x06
#define	DLE		0x10
#define	NAK		0x15
#define	CAN		0x18

#define	STATE_S		0
#define	STATE_SH	1
#define	STATE_SD	2
#define	STATE_SE	3
#define	STATE_ST	4
#define	STATE_R		5
#define	STATE_RH	6
#define	STATE_RD	7

static int state;
static int total = 0;

static int readlen = 0;
static int outlen = 0;
static int outbufptr = 0;
static unsigned char outbuffer[512];
static char yappc;		/* Nonzero if using YAPP C */

static void Write_Status(char *s)
{
	fprintf(stdout, "State: %-60s\r", s);
	fflush(stdout);
}

static void Send_RR(void)
{
	char buffer[2];

	buffer[0] = ACK;
	buffer[1] = 0x01;

	write(fd, buffer, 2);
}

static void Send_RF(void)
{
	char buffer[2];

	buffer[0] = ACK;
	buffer[1] = 0x02;

	write(fd, buffer, 2);
}

static void Send_RT(void)
{
	char buffer[2];

	buffer[0] = ACK;
	buffer[1] = ACK;

	write(fd, buffer, 2);
}

static void Send_AF(void)
{
	char buffer[2];

	buffer[0] = ACK;
	buffer[1] = 0x03;

	write(fd, buffer, 2);
}

static void Send_AT(void)
{
	char buffer[2];

	buffer[0] = ACK;
	buffer[1] = 0x04;

	write(fd, buffer, 2);
}

static void Send_NR(char *reason)
{
	char buffer[257];
	int length;

	if ((length = strlen(reason)) > 255)
		length = 255;

	buffer[0] = NAK;
	buffer[1] = length;
	memcpy(buffer + 2, reason, length);

	write(fd, buffer, length + 2);
}

/* Send a Resume Sequence */

static void Send_RS(int length)
{
	char buffer[256];
	int len;

	buffer[0] = NAK;
	buffer[2] = 'R';
	buffer[3] = 0;

	len = sprintf(buffer + 4, "%d", length) + 5;

	buffer[len] = 'C';
	buffer[len + 1] = 0;
	buffer[1] = len;

	write(fd, buffer, len + 2);
}

static void Send_SI(void)
{
	char buffer[2];

	buffer[0] = ENQ;
	buffer[1] = 0x01;

	write(fd, buffer, 2);
}

static void Send_CN(char *reason)
{
	char buffer[257];
	int length;

	if ((length = strlen(reason)) > 255)
		length = 255;

	buffer[0] = CAN;
	buffer[1] = length;
	memcpy(buffer + 2, reason, length);

	write(fd, buffer, length + 2);
}

static void Send_HD(char *filename, long length)
{
	char buffer[257];
	char size_buffer[10];
	int len_filename;
	int len_size;
	int len;
	struct stat sb;

	sprintf(size_buffer, "%ld", length);

	len_filename = strlen(filename) + 1;	/* Include the NUL */
	len_size = strlen(size_buffer) + 1;	/* Include the NUL */

	len = len_filename + len_size;

	if (!stat(filename, &sb)) {
		unix2yapp(sb.st_mtime, buffer + len + 2);
		len += 9;
	}

	buffer[0] = SOH;
	buffer[1] = len;

	memcpy(buffer + 2, filename, len_filename);
	memcpy(buffer + len_filename + 2, size_buffer, len_size);

	write(fd, buffer, len + 2);
}

static void Send_ET(void)
{
	char buffer[2];

	buffer[0] = EOT;
	buffer[1] = 0x01;

	write(fd, buffer, 2);
}

static void Send_DT(int length)
{
	char buffer[2];

	if (length > 255)
		length = 0;

	buffer[0] = STX;
	buffer[1] = length;

	write(fd, buffer, 2);
}

static void Send_EF(void)
{
	char buffer[2];

	buffer[0] = ETX;
	buffer[1] = 0x01;

	write(fd, buffer, 2);
}

static unsigned char checksum(unsigned char *buf, int len)
{
	int i;
	unsigned char sum = 0;

	for (i = 0; i < len; i++)
		sum += buf[i];

	return sum;
}

static int yapp_download_data(int *filefd, unsigned char *buffer)
{
	int length, file_time;
	char Message[50];

	if (buffer[0] == CAN || buffer[0] == NAK) {
		Write_Status("RcdABORT");
		return FALSE;
	}

	switch (state) {
	case STATE_R:
		if (buffer[0] == ENQ && buffer[1] == 0x01) {
			Send_RR();
			Write_Status("RcvHeader");
			state = STATE_RH;
			break;
		}

		Send_CN("Unknown code");
		Write_Status("SndABORT");
		return FALSE;

	case STATE_RH:
		if (buffer[0] == SOH) {
			/* Parse header: 3 fields == YAPP C */
			char *hptr, *hfield[3];
			if ((length = buffer[1]) == 0)
				length = 256;
			hptr = buffer + 2;
			while (length > 0) {
				int hlen;
				hlen = strlen(hptr) + 1;
				hfield[(int) yappc++] = hptr;
				hptr += hlen;
				length -= hlen;
			}

			if (yappc < 3) {
				yappc = 0;
			} else {
				file_time = yapp2unix(hfield[2]);
				yappc = 1;
			}

			if (*filefd == -1) {
				if ((*filefd =
				     open(hfield[0],
					  O_RDWR | O_APPEND | O_CREAT,
					  0666)) == -1) {
					printf("\n[Unable to open %s]\n",
					       hfield[0]);
					Send_NR("Invalid filename");
					return FALSE;
				}
			}

			printf("Receiving %s %s %s", hfield[0], hfield[1],
			       yappc ? ctime((time_t *) & file_time) :
			       " ");

			if (yappc) {
				struct stat sb;

				if (!fstat(*filefd, &sb) && sb.st_size)
					Send_RS(sb.st_size);
				else
					Send_RT();
			} else {
				Send_RF();
			}

			state = STATE_RD;
			break;
		}

		if (buffer[0] == ENQ && buffer[1] == 0x01) {
			break;
		}

		if (buffer[0] == EOT && buffer[1] == 0x01) {
			Send_AT();
			Write_Status("RcvEOT");
			return FALSE;
		}

		Send_CN("Unknown code");
		Write_Status("SndABORT");
		return FALSE;

	case STATE_RD:
		if (buffer[0] == STX) {
			if ((length = buffer[1]) == 0)
				length = 256;
			total += length;
			sprintf(Message, "RcvData  %5d bytes received",
				total);
			Write_Status(Message);

			if (yappc) {
				int i;
				unsigned char checksum = 0;

				for (i = 0; i < length; i++)
					checksum += buffer[i + 2];

				if (checksum != buffer[length + 2]) {
					Send_CN("Bad Checksum");
					Write_Status
					    ("SndABORT: Bad Checksum");
					return FALSE;
				}
			}

			write(*filefd, buffer + 2, length);
			break;
		}

		if (buffer[0] == ETX && buffer[1] == 0x01) {
			Send_AF();
			Write_Status("RcvEof");
			state = STATE_RH;
			close(*filefd);
			*filefd = -1;
			break;
		}

		Send_CN("Unknown code");
		Write_Status("SndABORT");
		return FALSE;
	}

	return TRUE;
}

static void yapp_download(int filefd)
{
	struct timeval timeval;
	fd_set sock_read;
	int n;
	int buflen = 0;
	int length;
	int used;
	unsigned char buffer[1024];

	Write_Status("RcvWait");

	state = STATE_R;
	total = 0;
	yappc = 0;

	while (TRUE) {
		FD_ZERO(&sock_read);
		FD_SET(STDIN_FILENO, &sock_read);
		FD_SET(fd, &sock_read);

		timeval.tv_usec = 0;
		timeval.tv_sec = TIMEOUT;

		n = select(fd + 1, &sock_read, NULL, NULL, &timeval);

		if (n == -1) {
			if (!interrupted && errno == EAGAIN)
				continue;
			if (!interrupted)
				perror("select");
			Send_CN("Internal error");
			Write_Status("SndABORT");
			return;
		}

		if (n == 0) {	/* Timeout */
			Send_CN("Timeout");
			Write_Status("SndABORT");
			return;
		}

		if (FD_ISSET(STDIN_FILENO, &sock_read)) {
			Send_CN("Cancelled by user");
			Write_Status("SndABORT");
			return;
		}

		if (FD_ISSET(fd, &sock_read)) {
			if ((length = read(fd, buffer + buflen, 511)) > 0) {
				buflen += length;

				do {
					used = FALSE;

					switch (buffer[0]) {
					case ACK:
					case ENQ:
					case ETX:
					case EOT:
						if (buflen >= 2) {
							if (!yapp_download_data(&filefd, buffer))
								return;
							buflen -= 2;
							memcpy(buffer,
							       buffer + 2,
							       buflen);
							used = TRUE;
						}
						break;
					default:
						if ((length =
						     buffer[1]) == 0)
							length = 256;
						if (buffer[0] == STX)
							length += yappc;
						if (buflen >= (length + 2)) {
							if (!yapp_download_data(&filefd, buffer))
								return;
							buflen -=
							    length + 2;
							memcpy(buffer,
							       buffer +
							       length + 2,
							       buflen);
							used = TRUE;
						}
						break;
					}
				}
				while (used);
			}
		}
	}
}

static int yapp_upload_data(int filefd, char *filename, int filelength,
			    unsigned char *buffer)
{
	char Message[80];

	if (buffer[0] == CAN || buffer[0] == NAK) {
		Write_Status("RcvABORT");
		return FALSE;
	}

	switch (state) {
	case STATE_S:
		if (buffer[0] == ACK && buffer[1] == 0x01) {
			Write_Status("SendHeader");
			Send_HD(filename, filelength);
			state = STATE_SH;
			break;
		}

		if (buffer[0] == ACK && buffer[1] == 0x02) {
			sprintf(Message, "SendData  %5d bytes transmitted",
				total);
			Write_Status(Message);
			outlen = read(filefd, outbuffer, readlen);
			outbufptr = 0;

			if (outlen)
				Send_DT(outlen);

			if (yappc) {
				outbuffer[outlen] =
				    checksum(outbuffer, outlen);
				outlen++;
			}

			state = STATE_SD;
			break;
		}

		Send_CN("Unknown code");
		Write_Status("SndABORT");
		return FALSE;

	case STATE_SH:
		/* Could get three replies here:
		 * ACK 02 : normal acknowledge.
		 * ACK ACK: yappc acknowledge.
		 * NAK ...: resume request.
		 */
		if (buffer[0] == NAK && buffer[2] == 'R') {
			int len;
			off_t rpos;

			len = buffer[1];
			if (buffer[len] == 'C')
				yappc = 1;
			rpos = atol(buffer + 4);
			lseek(filefd, rpos, SEEK_SET);
			buffer[0] = ACK;
			buffer[1] = yappc ? ACK : 0x02;
		}

		if (buffer[0] == ACK &&
		    (buffer[1] == 0x02 || buffer[1] == ACK)) {
			if (buffer[1] == ACK)
				yappc = 1;

			sprintf(Message, "SendData  %5d bytes transmitted",
				total);
			Write_Status(Message);
			outlen = read(filefd, outbuffer, readlen);
			outbufptr = 0;
			if (outlen)
				Send_DT(outlen);
			state = STATE_SD;

			if (yappc) {
				outbuffer[outlen] =
				    checksum(outbuffer, outlen);
				outlen++;
			}
			break;
		}

		Send_CN("Unknown code");
		Write_Status("SndABORT");
		return FALSE;

	case STATE_SD:
		Send_CN("Unknown code");
		Write_Status("SndABORT");
		return FALSE;

	case STATE_SE:
		if (buffer[0] == ACK && buffer[1] == 0x03) {
			Write_Status("SendEOT");
			Send_ET();
			state = STATE_ST;
			break;
		}

		Send_CN("Unknown code");
		Write_Status("SndABORT");
		return FALSE;

	case STATE_ST:
		if (buffer[0] == ACK && buffer[1] == 0x04) {
			return FALSE;
		}

		Send_CN("Unknown code");
		Write_Status("SndABORT");
		return FALSE;
	}

	return TRUE;
}

static void yapp_upload(int filefd, char *filename, long filelength)
{
	struct timeval timeval;
	fd_set sock_read;
	fd_set sock_write;
	int n;
	unsigned char buffer[1024];
	int buflen = 0;
	int length;
	int used;
	char Message[80];

	Write_Status("SendInit");

	readlen = (paclen - 2 > 253) ? 253 : paclen - 2;
	state = STATE_S;
	total = 0;
	yappc = 0;

	Send_SI();

	while (TRUE) {
		FD_ZERO(&sock_read);
		FD_ZERO(&sock_write);
		FD_SET(STDIN_FILENO, &sock_read);
		FD_SET(fd, &sock_read);

		if (state == STATE_SD) {
			FD_SET(fd, &sock_write);

			n = select(fd + 1, &sock_read, &sock_write, NULL,
				   NULL);
		} else {
			timeval.tv_usec = 0;
			timeval.tv_sec = TIMEOUT;

			n = select(fd + 1, &sock_read, NULL, NULL,
				   &timeval);
		}

		if (n == -1) {
			if (!interrupted && errno == EAGAIN)
				continue;
			if (!interrupted)
				perror("select");
			Write_Status("SndABORT");
			Send_CN("Internal error");
			return;
		}

		if (n == 0) {	/* Timeout, not STATE_SD */
			Write_Status("SndABORT");
			Send_CN("Timeout");
			return;
		}

		if (FD_ISSET(STDIN_FILENO, &sock_read)) {
			Write_Status("SndABORT");
			Send_CN("Cancelled by user");
			return;
		}

		if (FD_ISSET(fd, &sock_write)) {	/* Writable, only STATE_SD */
			if (outlen > 0) {
				if ((n =
				     write(fd, outbuffer + outbufptr,
					   outlen)) > 0) {
					outbufptr += n;
					outlen -= n;
					total += n;
				}
			}

			if (outlen == 0) {
				total -= yappc;
				if ((outlen =
				     read(filefd, outbuffer,
					  readlen)) > 0) {
					sprintf(Message,
						"SendData  %5d bytes transmitted",
						total);
					Write_Status(Message);

					outbufptr = 0;
					Send_DT(outlen);

					if (yappc) {
						outbuffer[outlen] =
						    checksum(outbuffer,
							     outlen);
						outlen++;
					}
				} else {
					Write_Status("SendEof");
					state = STATE_SE;
					Send_EF();
				}
			}
		}

		if (FD_ISSET(fd, &sock_read)) {
			if ((length = read(fd, buffer + buflen, 511)) > 0) {
				buflen += length;

				do {
					used = FALSE;

					switch (buffer[0]) {
					case ACK:
					case ENQ:
					case ETX:
					case EOT:
						if (buflen >= 2) {
							if (!yapp_upload_data(filefd, filename, filelength, buffer))
								return;
							buflen -= 2;
							memcpy(buffer,
							       buffer + 2,
							       buflen);
							used = TRUE;
						}
						break;
					default:
						if ((length =
						     buffer[1]) == 0)
							length = 256;
						if (buflen >= (length + 2)) {
							if (!yapp_upload_data(filefd, filename, filelength, buffer))
								return;
							buflen -=
							    length + 2;
							memcpy(buffer,
							       buffer +
							       length + 2,
							       buflen);
							used = TRUE;
						}
						break;
					}
				}
				while (used);
			}
		}
	}
}

void cmd_yapp(char *buf, int bytes)
{
	int filefd;
	long size = 0L;
	char *t;

	if (bytes == 0)
		return;

	switch (buf[0]) {
	case 'U':
	case 'u':
		if ((t = strchr(buf, '\n')) != NULL)
			*t = '\0';
		t = buf + 2;
		while (*t != '\0' && isspace(*t))
			t++;
		if (*t == '\0') {
			printf
			    ("\n[YAPP Upload requires a filename - eg ~yu hello.txt]\n");
			Send_NR("No filename");
			return;
		}
		if ((filefd = open(t, O_RDONLY)) == -1) {
			printf("\n[Unable to open upload file]\n");
			Send_NR("Invalid filename");
			return;
		}
		if (lseek(filefd, 0L, SEEK_END) != -1)
			size = lseek(filefd, 0L, SEEK_CUR);
		lseek(filefd, 0L, SEEK_SET);
		if (size != -1)
			printf
			    ("\n[Uploading %ld bytes from %s using YAPP]\n",
			     size, t);
		else
			printf("\n[Uploading from %s using YAPP]\n", t);
		yapp_upload(filefd, t, size);
		close(filefd);
		printf("[Finished YAPP Upload, %d bytes Transmitted]\n",
		       total);
		break;

	case 'D':
	case 'd':
		if ((t = strchr(buf, '\n')) != NULL)
			*t = '\0';
		t = buf + 2;
		while (*t != '\0' && isspace(*t))
			t++;
		if (*t == '\0')
			filefd = -1;
		else if ((filefd =
			  open(t, O_RDWR | O_APPEND | O_CREAT,
			       0666)) == -1) {
			printf("\n[Unable to open %s]\n", buf + 2);
			Send_NR("Invalid filename");
			return;
		}
		printf("\n[Downloading using YAPP]\n");
		yapp_download(filefd);
		close(filefd);
		printf("[Finished YAPP Download, %d bytes received]\n",
		       total);
		break;

	default:
		printf("\nUnknown '~y' escape. Type ~h for a list.\n");
		break;
	}
}