-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpunter.c
468 lines (406 loc) · 11.5 KB
/
punter.c
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
// Punter Transfer Protocol
//
// Copyright (c) 2003,2016, Per Olofsson. All rights reserved.
// This file is licensed under the BSD 2-Clause License.
//
// https://github.com/MagerValp/CGTerm
//
// Modified by Michael Steil to match the protocol variant in CCGMS.
// Search for "#ifdef CCGMS" for code additions.
#define CCGMS
#include <stdio.h>
#include <string.h>
#include <unistd.h>
extern int _inbyte(unsigned short timeout);
extern void _outbyte(int c);
extern int xfer_save_data(unsigned char *data, int length);
unsigned char xfer_buffer[4096];
int xfer_cancel = 0;
void xfer_send_byte(unsigned char c) {
_outbyte(c);
}
signed int xfer_recv_byte(int timeout) {
return _inbyte(timeout);
}
signed int xfer_recv_byte_error(int timeout, int errorcnt) {
return _inbyte(timeout);
}
#define xfer_saved_bytes 0
void menu_update_xfer_progress(char *message, int a, int b) {
fprintf(stderr, "menu_update_xfer_progress: %s\n", message);
}
#define gfx_vbl(...)
#define MIN(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
void punter_fail(char *message) {
fprintf(stderr, "punter_fail: %s\n", message);
menu_update_xfer_progress(message, xfer_saved_bytes, 0);
gfx_vbl();
}
void punter_retry(char *message) {
fprintf(stderr, "punter_retry: %s\n", message);
menu_update_xfer_progress(message, xfer_saved_bytes, 0);
gfx_vbl();
}
void punter_send_string(char *s) {
fprintf(stderr, "punter_send_string: %s\n", s);
while (*s) {
xfer_send_byte(*s++);
}
}
int punter_recv_string(char *sendstring, char *recvstring) {
int c, bytecnt;
int errorcnt;
fprintf(stderr, "punter_recv_string: sending \"%s\"\n", sendstring);
memset(recvstring, 'a', 4);
if (sendstring) {
punter_send_string(sendstring);
}
bytecnt = 0;
errorcnt = 10;
while (bytecnt != 3) {
while ((c = xfer_recv_byte(1000)) < 0 && errorcnt-- && (xfer_cancel == 0)) {
fprintf(stderr, "punter_recv_string: timeout %d\n", errorcnt);
punter_send_string(sendstring);
fprintf(stderr, "punter_recv_string: sending \"%s\"\n", sendstring);
bytecnt = 0;
}
if (errorcnt && (xfer_cancel == 0)) {
recvstring[bytecnt++] = c;
} else {
return(0);
}
}
recvstring[3] = 0;
fprintf(stderr, "punter_recv_string: received \"%s\"\n", recvstring);
return(3);
}
int punter_handshake(char *sendstring, char *waitstring) {
char p[4];
int l = 0;
int errorcnt = 10;
fprintf(stderr, "punter_handshake: %s -> %s\n", sendstring, waitstring);
while (errorcnt--) {
l = punter_recv_string(sendstring, p);
if (l == 3) {
if (strcmp(waitstring, p) == 0) {
fprintf(stderr, "punter_handshake: got \"%s\", done\n", waitstring);
return(1);
} else if (strcmp(sendstring, p) == 0) {
fprintf(stderr, "punter_handshake: got echoed \"%s\", failed\n", sendstring);
return(0);
} else {
if (strcmp("ACK", waitstring) == 0) {
if (strcmp("CKA", p) == 0) {
xfer_recv_byte(100);
xfer_recv_byte(100);
return(1);
}
if (strcmp("KAC", p) == 0) {
xfer_recv_byte(100);
return(1);
}
}
}
}
}
fprintf(stderr, "punter_handshake: failed\n");
return(0);
}
void punter_checksum(int len, unsigned short *c1, unsigned short *c2) {
unsigned short cksum = 0;
unsigned short clc = 0;
unsigned char *data = xfer_buffer + 4;
len -= 4;
while (len--) {
cksum += *data;
clc ^= *data++;
clc = (clc<<1) | (clc>>15);
}
*c1 = cksum;
*c2 = clc;
}
int punter_checksum_verify(int len) {
unsigned short cksum;
unsigned short clc;
punter_checksum(len, &cksum, &clc);
if (cksum == (xfer_buffer[0] | (xfer_buffer[1]<<8))) {
if (clc == (xfer_buffer[2] | (xfer_buffer[3]<<8))) {
return(1);
}
}
fprintf(stderr, "punter_checksum_verify: cksum = %04x (%04x)\n", cksum, xfer_buffer[0] | (xfer_buffer[1]<<8));
fprintf(stderr, "punter_checksum_verify: clc = %04x (%04x)\n", clc, xfer_buffer[2] | (xfer_buffer[3]<<8));
return(0);
}
void punter_checksum_create(int len) {
unsigned short cksum;
unsigned short clc;
punter_checksum(len, &cksum, &clc);
xfer_buffer[0] = cksum & 0xff;
xfer_buffer[1] = cksum >> 8;
xfer_buffer[2] = clc & 0xff;
xfer_buffer[3] = clc >> 8;
}
unsigned short punter_next_blocknum(void) {
return(xfer_buffer[5] | (xfer_buffer[6]<<8));
}
signed int punter_recv_block(int len) {
signed int c;
int bytecnt;
int errorcnt = 10;
restart:
fprintf(stderr, "punter_recv_block: receiving %d byte block\n", len);
punter_send_string("S/B");
bytecnt = 0;
while (bytecnt < len) {
if ((c = xfer_recv_byte_error(500, 10)) < 0) {
if (bytecnt == 3) {
if (strncmp("S/B", (char *)xfer_buffer, 3) == 0) {
menu_update_xfer_progress("Transfer canceled by remote", xfer_saved_bytes, 0);
gfx_vbl();
return(-1);
}
}
menu_update_xfer_progress("Block timed out, retrying", xfer_saved_bytes, 0);
gfx_vbl();
if (punter_handshake("BAD", "ACK")) {
if (errorcnt--) {
goto restart;
} else {
menu_update_xfer_progress("Block timed out", xfer_saved_bytes, 0);
gfx_vbl();
return(-1);
}
} else {
menu_update_xfer_progress("Handshake timed out", xfer_saved_bytes, 0);
gfx_vbl();
fprintf(stderr, "punter_recv_block: bad handshake timeout\n");
return(-1);
}
}
fprintf(stderr, "punter_recv_block: received byte %3d: %02x '%c'\n", bytecnt, c, c >= 0x20 && c < 0x7f ? c : '.');
xfer_buffer[bytecnt++] = c;
if (bytecnt == 4) {
if (strncmp("ACK", (char *)xfer_buffer, 3) == 0) {
menu_update_xfer_progress("Lost sync, retrying...", xfer_saved_bytes, 0);
gfx_vbl();
if (xfer_buffer[3] == 'A') {
goto restart;
} else {
fprintf(stderr, "punter_recv_block: skipping late ack\n");
xfer_buffer[0] = xfer_buffer[3];
bytecnt = 1;
}
}
}
if (bytecnt == 8) {
if (strncmp("ACKACK", (char *)xfer_buffer + 2, 6) == 0) {
menu_update_xfer_progress("Lost sync, retrying...", xfer_saved_bytes, 0);
gfx_vbl();
fprintf(stderr, "punter_recv_block: lost sync, restarting block\n");
goto restart;
}
if (strncmp("CKACKA", (char *)xfer_buffer + 2, 6) == 0) {
menu_update_xfer_progress("Lost sync, retrying...", xfer_saved_bytes, 0);
gfx_vbl();
fprintf(stderr, "punter_recv_block: lost sync, restarting block\n");
goto restart;
}
if (strncmp("KACKAC", (char *)xfer_buffer + 2, 6) == 0) {
menu_update_xfer_progress("Lost sync, retrying...", xfer_saved_bytes, 0);
gfx_vbl();
fprintf(stderr, "punter_recv_block: lost sync, restarting block\n");
goto restart;
}
}
}
if (punter_checksum_verify(bytecnt)) {
if (punter_handshake("GOO", "ACK") == 0) {
menu_update_xfer_progress("Handshake timed out", xfer_saved_bytes, 0);
gfx_vbl();
fprintf(stderr, "punter_recv_block: goo handshake timeout\n");
return(-1);
}
menu_update_xfer_progress("Downloading...", xfer_saved_bytes, 0);
gfx_vbl();
if (len <= 8) {
fprintf(stderr, "punter_recv_block: short block, returning %d\n", xfer_buffer[4]);
return(xfer_buffer[4]);
}
if (xfer_save_data(xfer_buffer + 7, len - 7)) {
fprintf(stderr, "punter_recv_block: returning %d\n", xfer_buffer[4]);
return(xfer_buffer[4]);
} else {
punter_fail("Write error!");
gfx_vbl();
return(-1);
}
} else {
menu_update_xfer_progress("Checksum failed, retrying", xfer_saved_bytes, 0);
gfx_vbl();
fprintf(stderr, "punter_recv_block: checksum failed\n");
if (punter_handshake("BAD", "ACK")) {
if (errorcnt--) {
goto restart;
} else {
menu_update_xfer_progress("Checksum failed", xfer_saved_bytes, 0);
gfx_vbl();
return(-1);
}
} else {
menu_update_xfer_progress("Handshake timed out", xfer_saved_bytes, 0);
gfx_vbl();
fprintf(stderr, "punter_recv_block: bad handshake timeout\n");
return(-1);
}
}
}
void punter_countdown(int num) {
char s[24];
sprintf(s, "Starting in %d", num);
menu_update_xfer_progress(s, xfer_saved_bytes, 0);
gfx_vbl();
}
int punter_recv(void) {
signed int nextblocksize;
menu_update_xfer_progress("Starting...", xfer_saved_bytes, 0);
gfx_vbl();
#ifdef CCGMS
char p[4];
int l = punter_recv_string(NULL, p);
(void)l;
#endif
if (punter_handshake("GOO", "ACK")) {
punter_countdown(5);
} else {
punter_fail("Timed out");
return(0);
}
nextblocksize = punter_recv_block(8);
if (nextblocksize < 0) {
punter_fail("Timed out on filetype block");
return(0);
}
if (punter_handshake("GOO", "ACK")) {
punter_countdown(4);
} else {
punter_fail("Handshake timeout");
return(0);
}
if (punter_handshake("S/B", "SYN")) {
punter_countdown(3);
} else {
punter_fail("Handshake timeout");
return(0);
}
if (punter_handshake("SYN", "S/B")) {
punter_countdown(2);
} else {
punter_fail("Handshake timeout");
return(0);
}
#ifdef CCGMS
l = punter_recv_string(NULL, p);
(void)l;
l = punter_recv_string(NULL, p);
(void)l;
fprintf(stderr, "sleeping...\n");
sleep(2);
fprintf(stderr, "...done\n");
#endif
if (punter_handshake("GOO", "ACK")) {
punter_countdown(1);
} else {
punter_fail("Handshake timeout");
return(0);
}
nextblocksize = punter_recv_block(7);
if (nextblocksize < 0) {
// error
punter_fail("file start timeout");
return(0);
}
while (punter_next_blocknum() < 0xff00 && nextblocksize >= 7) {
nextblocksize = punter_recv_block(nextblocksize);
}
if (nextblocksize < 0) {
//punter_fail("Block timeout");
return(0);
}
menu_update_xfer_progress("Finishing...", xfer_saved_bytes, 0);
gfx_vbl();
if (punter_handshake("S/B", "SYN")) {
punter_handshake("SYN", "S/B");
menu_update_xfer_progress("Finished", xfer_saved_bytes, 0);
gfx_vbl();
} else {
menu_update_xfer_progress("Done, but handshake timed out", xfer_saved_bytes, 0);
gfx_vbl();
}
return(1);
}
void xmit_block(int len) {
for (int i = 0; i < len; i++) {
xfer_send_byte(xfer_buffer[i]);
}
}
// very simple Punter transmit implementation with zero error handling
int punter_xmit(void *data, int data_len) {
// A1
fprintf(stderr, "punter_send: A1\n");
punter_handshake("GOO", "GOO");
// A2
fprintf(stderr, "punter_send: A2\n");
punter_handshake("ACK", "S/B");
xfer_buffer[5] = 0xff;
xfer_buffer[6] = 0xff;
xfer_buffer[7] = 1; // SEQ
punter_checksum_create(8);
xmit_block(8);
punter_handshake(NULL, "GOO");
// A3
fprintf(stderr, "punter_send: A3\n");
punter_handshake("ACK", "S/B");
punter_handshake("SYN", "SYN");
punter_send_string("S/B");
// B1
fprintf(stderr, "punter_send: B1\n");
punter_handshake(NULL, "GOO");
// B2
int max_block = 255;
int index = 0;
int data_ptr = 0;
int len = 7;
int next_len;
int is_last_block = 0;
do {
int next_data_ptr = data_ptr + len - 7;
next_len = MIN(data_len - next_data_ptr, max_block - 7) + 7;
if (len < max_block && index) {
index = 0xffff;
is_last_block = 1;
}
fprintf(stderr, "punter_send: B2\n");
punter_handshake("ACK", "S/B");
xfer_buffer[5] = index & 0xff;
xfer_buffer[6] = index >> 8;
xfer_buffer[4] = next_len;
memcpy(&xfer_buffer[7], &data[data_ptr], len - 7);
punter_checksum_create(len);
xmit_block(len);
punter_handshake(NULL, "GOO");
index++;
data_ptr = next_data_ptr;
len = next_len;
fprintf(stderr, "punter_send: len = %d\n", len);
} while(!is_last_block);
// B3
fprintf(stderr, "punter_send: B3\n");
punter_handshake("ACK", "S/B");
punter_handshake("SYN", "SYN");
punter_send_string("S/B");
return(1);
}