-
Notifications
You must be signed in to change notification settings - Fork 25
/
6551_modem.c
784 lines (681 loc) · 16.1 KB
/
6551_modem.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
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
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** 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, version 2
** of the License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** 6551 ACIA emulation - modem back-end
*/
#ifdef __MINGW32__
// to cope with getaddrinfo's bug: http://programmingrants.blogspot.fr/2009/09/tips-on-undefined-reference-to.html
#define _WIN32_WINNT 0x0501
#endif
#ifdef _MSC_VER
// Need to link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#endif // _MSC_VER
#include "system.h"
#include "6502.h"
#include "via.h"
#include "8912.h"
#include "gui.h"
#include "disk.h"
#include "monitor.h"
#include "6551.h"
#include "machine.h"
#ifdef BACKEND_MODEM
static struct machine* oric = NULL;
#define CMD_BUF_SIZE 4
static Uint8 mdm_cmd_buf[CMD_BUF_SIZE];
// SDL_GetTicks() counts from 0 on SDL_Init, so overrun would only
// happen if you ran Oricutron for ~50 days solid
static Uint32 mdm_time_buf[CMD_BUF_SIZE];
// FIXME: BUG: drops byte(s)... kind of buffer overflow ???
#define DATA_BUF_SIZE 0x10000
static int mdm_in = 0;
static Uint8 mdm_in_buf[DATA_BUF_SIZE];
static int mdm_out = 0;
static Uint8 mdm_out_buf[DATA_BUF_SIZE];
static SDL_bool connected = SDL_FALSE;
static SDL_bool listening = SDL_FALSE;
static SDL_bool escaped = SDL_FALSE;
static int srv_sck = -1;
static int cnt_sck = -1;
/* Forward definitions */
static SDL_bool socket_init(void);
static void socket_done(void);
static int socket_create(int* sock, int domain);
static int socket_bind(int sock, int port, int domain);
static int socket_listen(int sock);
static int socket_accept(int sock, int* sck);
static int socket_connect(int sock, const char* ip, int port, int domain);
static int socket_write(int sock, const unsigned char* data, int len);
static int socket_read(int sock, unsigned char* data, int* len);
static int socket_close(int sock);
static char* trim(char* line)
{
if(line && *line)
{
int l = (int)strlen(line);
while(0 <= l && line[l-1] == ' ')
line[--l] = '\0';
while(0 <= l && line[0] == ' ')
memmove(line, line+1, l--);
}
return line;
}
static void send_str(const char* s)
{
while(s && *s)
{
mdm_out_buf[mdm_out] = *s++;
mdm_out++;
if(mdm_out == DATA_BUF_SIZE)
{
memmove(mdm_out_buf, mdm_out_buf+1, DATA_BUF_SIZE-1);
mdm_in--;
}
}
}
static void send_responce(const char* s)
{
send_str("\r\n");
if(s && *s)
{
send_str(s);
send_str("\r\n");
}
}
static void send_responce_ok(void)
{
send_responce("OK");
}
static void send_responce_error(void)
{
send_responce("ERROR");
}
static void mdm_hangup(void)
{
connected = SDL_FALSE;
escaped = SDL_FALSE;
socket_close(cnt_sck);
cnt_sck = -1;
}
static void mdm_answeroff(void)
{
if( listening )
{
listening = SDL_FALSE;
socket_close(srv_sck);
srv_sck = -1;
}
}
static void mdm_answeron(void)
{
if( !listening )
{
if(socket_create(&srv_sck, oric->aciabackendcfgdomain) &&
socket_bind(srv_sck, oric->aciabackendcfgport, oric->aciabackendcfgdomain) &&
socket_listen(srv_sck))
listening = SDL_TRUE;
}
}
static void mdm_connect(const char* s)
{
char ip[1024];
char* p = NULL;
int port = 0;
strcpy(ip, s);
trim(ip);
p = strrchr(ip, ':');
if(p)
{
port = atoi(p+1);
p[0] = '\0';
}
// default telnet port
if(port == 0) port = ACIA_TYPE_MODEM_DEFAULT_PORT;
if( socket_create(&cnt_sck, oric->aciabackendcfgdomain) )
{
if(socket_connect(cnt_sck, ip, port, oric->aciabackendcfgdomain))
{
send_responce("CONNECT");
connected = SDL_TRUE;
return;
}
else
send_responce("BUSY");
}
else
send_responce("NO DIALTINE");
mdm_hangup();
}
static void parse_command(const char* s)
{
if(!s)
return;
if( escaped )
{
if('\0' == s[0])
send_responce("");
else if(!strcasecmp("ATA", s))
escaped = SDL_FALSE;
else if(!strcasecmp("ATO", s))
escaped = SDL_FALSE;
else if(!strcasecmp("ATH1", s))
escaped = SDL_FALSE;
else if(!strcasecmp("ATH0", s))
{
mdm_hangup();
send_responce_ok();
}
else if(!strncasecmp("ATZ", s, 3))
{
mdm_hangup();
send_responce_ok();
}
else
send_responce_error();
}
else
{
if('\0' == s[0])
send_responce("");
else if(!strcasecmp("AT", s))
send_responce_ok();
else if(!strcasecmp("ATA", s))
{
mdm_answeron();
if( !listening )
send_responce_error();
else
send_responce("AUTOANSWER ON");
}
else if(!strncasecmp("ATS0=", s, 5))
{
char* p = trim(strdup(s+5));
int on = atoi(p);
free(p);
if(0<on)
{
mdm_answeron();
if( !listening )
send_responce_error();
else
send_responce("AUTOANSWER ON");
}
else
{
mdm_answeroff();
send_responce("AUTOANSWER OFF");
}
}
else if(!strcasecmp("ATS0?", s))
{
if( listening )
send_responce("AUTOANSWER ON");
else
send_responce("AUTOANSWER OFF");
}
else if(!strcasecmp("ATH0", s))
{
mdm_hangup();
send_responce_ok();
}
else if(!strcasecmp("ATH1", s))
send_responce_ok();
else if(!strncasecmp("ATZ", s, 3))
send_responce_ok();
else if(!strncasecmp("AT&F", s, 4))
send_responce_ok();
else if(!strncasecmp("ATD", s, 3))
{
s = s + 3;
switch(tolower(*s))
{
case 't':
case 'p':
s++;
break;
}
if(*s)
mdm_connect(s);
else
send_responce_error();
}
else
send_responce_error();
}
}
static void modem_done( struct acia* acia )
{
mdm_hangup();
socket_close(srv_sck);
srv_sck = -1;
socket_done();
// acia_init_none( acia );
}
static Uint8 modem_stat(Uint8 stat)
{
if( connected && !escaped )
{
int len = DATA_BUF_SIZE - mdm_out;
if(0 < len)
{
if(!socket_read(cnt_sck, mdm_out_buf + mdm_out, &len))
{
mdm_hangup();
send_responce("NO CARRIER");
}
else
mdm_out += len;
}
}
if( !connected && listening )
{
if(socket_accept(srv_sck, &cnt_sck))
{
send_responce("CONNECT");
connected = SDL_TRUE;
}
}
if( connected )
return (stat & ~(ASTF_CARRIER|ASTF_DSR));
if( listening )
return (stat & ~(ASTF_DSR));
return (stat | (ASTF_CARRIER|ASTF_DSR));
}
static SDL_bool modem_has_byte(Uint8* data)
{
if(0 < mdm_out)
{
*data = mdm_out_buf[0];
return SDL_TRUE;
}
return SDL_FALSE;
}
static SDL_bool modem_get_byte(Uint8* data)
{
if(0 < mdm_out)
{
*data = mdm_out_buf[0];
memmove(mdm_out_buf, mdm_out_buf+1, mdm_out);
mdm_out--;
return SDL_TRUE;
}
return SDL_FALSE;
}
static void mdm_escape(Uint8 data)
{
mdm_cmd_buf[0] = mdm_cmd_buf[1];
mdm_cmd_buf[1] = mdm_cmd_buf[2];
mdm_cmd_buf[2] = mdm_cmd_buf[3];
mdm_cmd_buf[3] = data;
mdm_time_buf[0] = mdm_time_buf[1];
mdm_time_buf[1] = mdm_time_buf[2];
mdm_time_buf[2] = mdm_time_buf[3];
mdm_time_buf[3] = SDL_GetTicks();
if( mdm_cmd_buf[1] == '+' && mdm_cmd_buf[2] == '+' && mdm_cmd_buf[3] == '+')
{
if(1000 < (mdm_time_buf[3] - mdm_time_buf[0]))
{
escaped = SDL_TRUE;
}
}
}
static SDL_bool modem_put_byte(Uint8 data)
{
if( connected && !escaped )
{
if(!socket_write(cnt_sck, &data, 1))
{
mdm_hangup();
send_responce("NO CARRIER");
}
mdm_escape(data);
}
else
{
mdm_in_buf[mdm_in] = data;
mdm_in++;
if(mdm_in == DATA_BUF_SIZE)
{
memmove(mdm_in_buf, mdm_in_buf+1, DATA_BUF_SIZE-1);
mdm_in--;
}
// including 0x7e as BACKSPACE:
// this is tribute to Vagelis Blathras
// for the excellent terminal program
if(data == 0x08 || data == 0x7f || data == 0x7e)
{
switch(mdm_in)
{
case 0:
break;
case 1:
mdm_in = 0;
break;
default:
mdm_in -= 2;
break;
}
}
else if(data == 0x0d)
{
mdm_in_buf[mdm_in-1] = 0x00;
parse_command((const char*)mdm_in_buf);
mdm_in = 0;
}
else
{
// local echo
if(mdm_out < DATA_BUF_SIZE)
mdm_out_buf[mdm_out++] = data;
}
}
return SDL_TRUE;
}
SDL_bool acia_init_modem( struct acia* acia )
{
oric = acia->oric;
mdm_in = 0;
mdm_out = 0;
mdm_in_buf[0] = 0x00;
mdm_out_buf[0] = 0x00;
memset(mdm_cmd_buf, 0, sizeof(Uint8)*CMD_BUF_SIZE);
memset(mdm_time_buf, 0, sizeof(Uint32)*CMD_BUF_SIZE);
acia->done = modem_done;
acia->stat = modem_stat;
acia->has_byte = modem_has_byte;
acia->get_byte = modem_get_byte;
acia->put_byte = modem_put_byte;
srv_sck = -1;
cnt_sck = -1;
connected = SDL_FALSE;
listening = SDL_FALSE;
escaped = SDL_FALSE;
if(socket_init())
{
oric->aciabackend = ACIA_TYPE_MODEM;
return SDL_TRUE;
}
// fall-back to none
acia_init_none( acia );
return SDL_FALSE;
}
// simple socket library
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#if defined(__amigaos4__) || defined(__MORPHOS__)
#include <sys/socket.h>
#include <sys/fcntl.h>
#include <netinet/in.h>
#include <netdb.h>
#define _recvdata(a, b, c) recv(a, b, c, 0)
#define _closesocket close
#if defined(__MORPHOS__)
struct Library *SocketBase;
typedef LONG socklen_t;
char *inet_ntoa(struct in_addr n)
{
static char a[sizeof "XXX.XXX.XXX.XXX"];
char *p = (char *)&n;
snprintf(a, sizeof a, "%d.%d.%d.%d", p[0]&0xff, p[1]&0xff, p[2]&0xff, p[3]&0xff);
return a;
}
#endif
#endif
#if defined(__LINUX__) || defined(__APPLE__)
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#define _recvdata(a, b, c) read(a, b, c)
#define _closesocket close
#endif
#ifdef __APPLE__
#include <sys/time.h>
#endif
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#define _recvdata(a, b, c) recv(a, b, c, 0)
#define _closesocket closesocket
#endif // WIN32
static SDL_bool socket_initialized = SDL_FALSE;
static SDL_bool socket_init(void)
{
if(!socket_initialized)
{
#ifdef WIN32
WSADATA wsadata;
if( (WSAStartup(MAKEWORD(2,2), &wsadata) == SOCKET_ERROR) ||
(WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) )
return SDL_FALSE;
#endif
#ifdef __MORPHOS__
if( !(SocketBase = OldOpenLibrary("bsdsocket.library")) )
return SDL_FALSE;
#endif
}
socket_initialized = SDL_TRUE;
return SDL_TRUE;
}
static void socket_done(void)
{
#ifdef WIN32
WSACleanup();
#endif
#ifdef __MORPHOS__
if( SocketBase )
CloseLibrary(SocketBase);
#endif
}
static int socket_create(int* sock, int domain)
{
int on = 1;
#if defined(__amigaos4__) || defined(__MORPHOS__)
domain = AF_INET;
#else
if (domain == 6)
domain = AF_INET6;
else
domain = AF_INET;
#endif
*sock = socket(domain, SOCK_STREAM, 0);
if(*sock == -1)
return 0;
if(setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1)
return 0;
#ifdef __APPLE__
if(setsockopt(*sock, SOL_SOCKET, SO_REUSEPORT, (const char*) &on, sizeof(on)) == -1)
return 0;
#endif
return 1;
}
// Just in case AOS 4 and other OSes don't have the new getaddrinfo functions
// #define NO_GETADDRINFO
static int socket_bind(int sock, int port, int domain)
{
#ifdef NO_GETADDRINFO
struct sockaddr_in srv_addr;
memset((void*)&srv_addr, 0, sizeof(srv_addr));
srv_addr.sin_family = AF_INET;
srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
srv_addr.sin_port = htons(port);
if(bind(sock, (struct sockaddr*) &srv_addr, sizeof(srv_addr)) == -1) {
perror("socket_bind, bind error: ");
return 0;
}
#else
struct addrinfo hints, *res, *ressave;
char service[NI_MAXSERV];
int n;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
if (domain == 6)
hints.ai_family = AF_INET6;
else
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
sprintf(service, "%d", port);
if ( (n = getaddrinfo(NULL, service, &hints, &res)) != 0) {
printf("socket_bind, getaddrinfo error: %s", gai_strerror(n));
return 0;
}
ressave = res;
// loop through all the addresses that we got
do {
if (bind(sock, res->ai_addr, res->ai_addrlen) == 0)
break; /* success */
} while ( (res = res->ai_next) != NULL);
freeaddrinfo(ressave);
if (res == NULL) {
perror("socket_bind, bind error: ");
return 0;
}
#endif
#if defined(__LINUX__) || defined(__APPLE__) || defined(__amigaos4__) || defined(__MORPHOS__)
fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
#endif
#ifdef WIN32
u_long imode = 1;
ioctlsocket(sock, FIONBIO, &imode);
#endif
return 1;
}
static int socket_listen(int sock)
{
if(listen(sock, 1) == -1)
return 0;
return 1;
}
static int socket_accept(int sock, int* sck)
{
#if defined(__amigaos4__) || defined(__MORPHOS__)
struct sockaddr_in cli_addr;
#else
struct sockaddr_storage cli_addr;
#endif
socklen_t cli_addr_len = sizeof(cli_addr);
*sck = accept(sock, (struct sockaddr*) &cli_addr, (socklen_t*) &cli_addr_len);
if(*sck == -1)
return 0;
else
{
#if defined(__LINUX__) || defined(__amigaos4__) || defined(__MORPHOS__)
fcntl(*sck, F_SETFL, fcntl(*sck, F_GETFL, 0) | O_NONBLOCK);
#endif
#if defined(WIN32)
u_long imode = 1;
ioctlsocket(*sck, FIONBIO, &imode);
#endif
}
return 1;
}
static int socket_write(int sock, const unsigned char* data, int len)
{
if(send(sock, (void*)data, len, 0) == -1)
return 0;
return 1;
}
static int socket_read(int sock, unsigned char* data, int* len)
{
int ret = (int)_recvdata(sock, (void*)data, *len);
*len = 0;
if(0 == ret)
return 0;
else if(0 < ret)
*len = ret;
return 1;
}
#ifdef NO_GETADDRINFO
static void hostname_to_ip(const char* host, char* ip, int len)
{
struct hostent *he = gethostbyname( (unsigned char*)host );
if( NULL != he )
{
struct in_addr **addr_list = (struct in_addr **) he->h_addr_list;
if( NULL != addr_list[0] )
{
strncpy(ip, inet_ntoa(*addr_list[0]), len);
return;
}
}
strncpy(ip, host, len);
}
#endif
static int socket_connect(int sock, const char* host, int port, int domain)
{
#ifdef NO_GETADDRINFO
char ip[64];
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
hostname_to_ip(host, ip, 64);
addr.sin_addr.s_addr = inet_addr((unsigned char*)ip);
if(connect(sock, (struct sockaddr*) &addr, sizeof(addr)) != 0) {
perror("socket_connect, connect error: ");
return 0;
}
#else
struct addrinfo hints, *res, *ressave;
char service[NI_MAXSERV];
int n;
memset(&hints, 0, sizeof(hints));
if (domain == 6)
hints.ai_family = AF_INET6;
else
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
sprintf(service, "%d", port);
if ( (n = getaddrinfo(host, service, &hints, &res)) != 0) {
printf("socket_connect, getaddrinfo error: %s", gai_strerror(n));
return 0;
}
ressave = res;
// loop through all the addresses that we got
do {
if (connect(sock, res->ai_addr, res->ai_addrlen) == 0)
break; /* success */
} while ( (res = res->ai_next) != NULL);
freeaddrinfo(ressave);
if (res == NULL) {
perror("socket_bind, connect error: ");
return 0;
}
#endif
#if defined(__LINUX__) || defined(__APPLE__) || defined(__amigaos4__) || defined(__MORPHOS__)
fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
#endif
#ifdef WIN32
u_long imode = 1;
ioctlsocket(sock, FIONBIO, &imode);
#endif
return 1;
}
int socket_close(int sock)
{
if(_closesocket(sock) == -1)
return 0;
return 1;
}
#endif /* BACKEND_MODEM */