moved Internet files to its subdirectory

This commit is contained in:
Norayr Chilingarian 2020-05-07 19:26:14 +04:00
parent 12b8ca56c5
commit 6757ca74b5
4 changed files with 0 additions and 0 deletions

102
Internet/Internet.Mod Normal file
View file

@ -0,0 +1,102 @@
MODULE Internet; (*noch 14.4.2017 / 14.4.2017*)
IMPORT sockets, netdb, types, Strings, Out, Platform, SYSTEM;
TYPE
Socket* = LONGINT; (* INT32 *)
Int16 = types.Int16;
Int32 = types.Int32;
Int64 = types.Int64;
PROCEDURE Write*(fd: Socket; buf: ARRAY OF CHAR): BOOLEAN;
VAR
l: SYSTEM.ADDRESS;
r: INTEGER;
len: LONGINT;
BEGIN
l := SYSTEM.ADR(buf[0]);
len := Strings.Length(buf)-1;
r := Platform.Write(fd, l, len);
IF r = -1 THEN
(*Out.String("write() failed."); Out.Ln;*)
RETURN FALSE
ELSE
(*Out.String("write() success."); Out.Ln;*)
RETURN TRUE
END;
END Write;
PROCEDURE Read*(fd: Socket; VAR buf: ARRAY OF CHAR): BOOLEAN;
VAR
p: SYSTEM.ADDRESS;
l, f: LONGINT;
r: INTEGER;
BEGIN
p := SYSTEM.ADR(buf[0]);
l := LEN(buf)-1;
f := 0;
r := Platform.Read(fd, p, l, f);
IF r >= 0 THEN RETURN TRUE ELSE RETURN FALSE END
END Read;
PROCEDURE Connect*(host, port: ARRAY OF CHAR; VAR conn: Socket): BOOLEAN;
VAR
hints, res : netdb.addrInfo;
pres, pres2, phints: netdb.PaddrInfo;
tmpaddr : SYSTEM.ADDRESS;
tmp32 : netdb.Int32;
(*conn : netdb.Int32;*)
BEGIN
hints.aiFamily := sockets.AfUnspec;
hints.aiSockType := sockets.SockStream;
hints.aiFlags := 0;
hints.aiProtocol := netdb.ipprotoTCP;
hints.aiAddrLen := 0;
hints.aiAddr := 0; hints.aiCanonName := 0; hints.aiNext := 0;
phints := SYSTEM.VAL(netdb.PaddrInfo, SYSTEM.ADR(hints));
pres := SYSTEM.VAL(netdb.PaddrInfo, SYSTEM.ADR(res));
pres2 := SYSTEM.VAL(netdb.PaddrInfo, SYSTEM.ADR(pres));
tmp32 := netdb.getAddrInfo(host, port, phints, pres2);
IF tmp32 # 0 THEN
Out.String("getaddrinfo() failed"); Out.Ln;
HALT(1);
ELSE
Out.String("getaddrinfo() returned 0, success"); Out.Ln;
END;
conn := sockets.Socket(pres^.aiFamily, pres^.aiSockType, pres^.aiProtocol);
IF conn = -1 THEN
Out.String("socket() returned -1, error"); Out.Ln;
HALT(1);
ELSE
Out.String("socket() succeeded."); Out.Ln;
END;
tmpaddr := SYSTEM.ADR(pres^.aiAddr);
tmp32 := sockets.Connect(conn, pres^.aiAddr, pres^.aiAddrLen);
netdb.freeAddrInfo(pres);
IF tmp32 = 0 THEN
Out.String("connect() succeeded."); Out.Ln;
RETURN TRUE
ELSE
Out.String("connect() failed."); Out.Ln;
RETURN FALSE
END;
END Connect;
PROCEDURE Disconnect*(VAR fd: Socket);
VAR
i : INTEGER;
BEGIN
i := Platform.Close(fd);
END Disconnect;
END Internet.

79
Internet/netdb.Mod Normal file
View file

@ -0,0 +1,79 @@
MODULE netdb; (*noch 23.2.2017 / 14.4.2017*)
IMPORT SYSTEM;
CONST
ipprotoIP* = 0;
ipprotoICMP* = 1;
ipprotoIGMP* = 2;
ipprotoIPIP* = 4;
ipprotoTCP* = 6;
ipprotoEGP* = 8;
ipprotoPUP* = 12;
ipprotoUDP* = 17;
ipprotoIDP* = 22;
ipprotoTP* = 29;
ipprotoDCCP* = 33;
ipprotoIPV6* = 41;
ipprotoRSVP* = 46;
ipprotoGRE* = 47;
ipprotoESP* = 50;
ipprotoAH* = 51;
ipprotoMTP* = 92;
ipprotoBEETPH* = 94;
ipprotoENCAP* = 98;
ipprotoPIM* = 103;
ipprotoCOMP* = 108;
ipprotoSCTP* = 132;
ipprotoUDPLITE* = 136;
ipprotoMPLS* = 137;
ipprotoRAW* = 255;
TYPE
Int32* = LONGINT;
Int64* = HUGEINT;
TYPE
PaddrInfo* = POINTER [1] TO addrInfo;
addrInfo* = RECORD
aiFlags*: Int32;
aiFamily*:Int32;
aiSockType*: Int32;
aiProtocol*: Int32;
aiAddrLen*: Int32;
aiAddr*, aiCanonName*, aiNext*: SYSTEM.ADDRESS; (* pointers *)
END;
PROCEDURE -getAddrInfo*(VAR node, service: ARRAY OF CHAR; hints: PaddrInfo; res: PaddrInfo): Int32
"getaddrinfo (node, service, hints, res)";
PROCEDURE -freeAddrInfo*( res: PaddrInfo)
"freeaddrinfo(res)";
END netdb.

156
Internet/sockets.Mod Normal file
View file

@ -0,0 +1,156 @@
MODULE sockets; (*noch 23.2.2017 / 14.4.2017*)
IMPORT types, SYS := SYSTEM;
TYPE
Int16* = types.Int16; (* INTEGER on 32 bit platform *)
Int32* = types.Int32;
Int64* = types.Int64;
CONST
SockStream* = 1;
SockDgram* = 2;
SockRaw* = 3;
SockRdm* = 4;
SockSeqpacket* = 5;
SockDccp* = 6;
SockPacket* = 10;
AfUnspec* = 0; (* Unspecified. *)
AfLocal* = 1; (* Local to host (pipes and file-domain). *)
AfUnix* = 1; (* POSIX name for PF_LOCAL. *)
AfFile* = 1; (* Another non-standard name for PF_LOCAL. *)
AfInet* = 2; (* IP protocol family. *)
AfAx25* = 3; (* Amateur Radio AX.25. *)
AfIpx* = 4; (* Novell Internet Protocol. *)
AfAppletalk* = 5; (* Appletalk DDP. *)
AfNetrom* = 6; (* Amateur radio NetROM. *)
AfBridge* = 7; (* Multiprotocol bridge. *)
AfAtmpvc* = 8; (* ATM PVCs. *)
AfX25* = 9; (* Reserved for X.25 project. *)
AfInet6* = 10; (* IP version 6. *)
AfRose* = 11; (* Amateur Radio X.25 PLP. *)
AfDecnet* = 12; (* Reserved for DECnet project. *)
AfNetbeui*= 13; (* Reserved for 802.2LLC project. *)
AfSecurity*=14; (* Security callback pseudo AF. *)
AfKey* = 15; (* PF_KEY key management API. *)
AfNetlink*= 16;
AfRoute* = 16; (* Alias to emulate 4.4BSD. *)
AfPacket = 17; (* Packet family. *)
AfAsh = 18; (* Ash. *)
AfEconet* = 19; (* Acorn Econet. *)
AfAtmsvc* = 20; (* ATM SVCs. *)
AfRds* = 21; (* RDS sockets. *)
AfSna = 22; (* Linux SNA Project *)
AfIrda* = 23; (* IRDA sockets. *)
AfPppox = 24; (* PPPoX sockets. *)
AfWanpipe*= 25; (* Wanpipe API sockets. *)
AfLlc* = 26; (* Linux LLC. *)
AfCan* = 29; (* Controller Area Network. *)
AfTipc* = 30; (* TIPC sockets. *)
AfBluetooth* = 31; (* Bluetooth sockets. *)
AfIucv* = 32; (* IUCV sockets. *)
AfRxrpc* = 33; (* RxRPC sockets. *)
AfIsdn* = 34; (* mISDN sockets. *)
AfPhonet* = 35; (* Phonet sockets. *)
AfIeee802154* = 36; (* IEEE 802.15.4 sockets. *)
AfCaif* = 37; (* CAIF sockets. *)
AfAlg* = 38; (* Algorithm sockets. *)
AfNfc* = 39; (* NFC sockets. *)
AfVsock* = 40; (* vSockets. *)
AfMax* = 41; (* For now.. *)
InAddrAny* = 0;
TYPE
(*
(* /usr/include/netinet/in.h *)
InAddr* = RECORD
SAddr* : Int32;
END;
SockAddrIn* = RECORD
SinFamily* : Int16;
SinPort* : Int16;
SinAddr* : InAddr;
SinZero* : ARRAY 8 OF CHAR;
END;
*)
(* /usr/include/sys/socket.h *)
SockAddr* = RECORD
SaFamily* : Int16;
SaData* : ARRAY 14 OF CHAR
END;
(*
PROCEDURE -includeSockets
"#include <sys/socket.h>";
*)
PROCEDURE -socket(domain, type, protocol: Int32): Int32
"(int)socket(domain, type, protocol)";
PROCEDURE Socket*(domain, type, protocol: Int32): Int32;
BEGIN
RETURN socket(domain, type, protocol)
END Socket;
PROCEDURE -bind(sockfd: Int32; VAR addr: SockAddr; addrlen: Int32): Int32
"(int)bind(sockfd, addr, addrlen)";
PROCEDURE Bind*(sockfd: Int32; VAR addr: SockAddr; addrlen: Int32): Int32;
BEGIN
RETURN bind(sockfd, addr, addrlen)
END Bind;
PROCEDURE -listen(sockfd, backlog: Int32): Int32
"(int)listen(sockfd, backlog)";
PROCEDURE Listen*(sockfd, backlog: Int32): Int32;
BEGIN
RETURN listen(sockfd, backlog)
END Listen;
PROCEDURE -accept(sockfd: Int32; VAR addr: SockAddr; VAR addrlen: Int32): Int32
"(int)accept(sockfd, addr, addrlen)";
PROCEDURE Accept*(sockfd: Int32; VAR addr: SockAddr; VAR addrlen: Int32): Int32;
BEGIN
RETURN accept(sockfd, addr, addrlen)
END Accept;
(* int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen); *)
PROCEDURE -connect(sockfd: Int32; addr: SYS.ADDRESS; addrlen: Int32): Int32
"(INTEGER)(connect(sockfd, addr, addrlen))";
PROCEDURE Connect*(sockfd: Int32; sockaddr: SYS.ADDRESS; addrlen: Int32): Int32;
BEGIN
RETURN connect(sockfd, sockaddr, addrlen)
END Connect;
(* ssize_t recv(int sockfd, void *buf, size_t len, int flags); *)
PROCEDURE -recv(sockfd: Int32; buf: SYS.ADDRESS; len: Int64; flags: Int32):Int64
"(HUGEINT)recv(sockfd, buf, len, flags)";
(*
PROCEDURE -SizeofSockaddr(): INTEGER
"sizeof(sockaddr)";
PROCEDURE -Error(msg: ARRAY OF CHAR; len: INTEGER)
"write(1/*stdout*/, msg, len); char ch = 0xa; write(1, &ch, 1)";
PROCEDURE sockaddrCheck; (* check for inconsistent usage of sigjmp_buf; better avoid Unix_JmpBuf *)
VAR x, y: LONGINT;
BEGIN
x := SizeofSockaddr();
y := SIZE(SockAddr);
IF x # y THEN
Error("sockets.sockaddrCheck: inconsistent usage of sockaddr", 52);
HALT(1);
END
END sockaddrCheck;
*)
BEGIN
END sockets.

41
Internet/types.Mod Normal file
View file

@ -0,0 +1,41 @@
MODULE types; (*noch 23.2.2017 / 13.4.2017*)
IMPORT SYS := SYSTEM;
TYPE
intarr64 = ARRAY 8 OF SYS.BYTE; (* to emulate int16 on x86_64; -- noch *)
intarr32 = ARRAY 4 OF SYS.BYTE;
intarr16 = ARRAY 2 OF SYS.BYTE;
Int16* = INTEGER; (* INTEGER on 32 bit platform *)
Int32* = LONGINT;
Int64* = HUGEINT;
String* = ARRAY 256 OF CHAR;
PROCEDURE HugeintToInt16*(in: HUGEINT; VAR out: Int16);
VAR
int64 : intarr64;
int16 : intarr16;
BEGIN
int64 := SYS.VAL(intarr64, in);
int16[0] := int64[0];
int16[1] := int64[1];
out := SYS.VAL(Int16, int16)
END HugeintToInt16;
PROCEDURE LongintToInt16*(int: LONGINT; VAR int16: Int16);
BEGIN
int16 := SYS.VAL(Int16, int)
END LongintToInt16;
PROCEDURE htons*(in: Int16; VAR out : Int16);
VAR
tmpin, tmpout : intarr16;
BEGIN
tmpin := SYS.VAL(intarr16, in);
tmpout := SYS.VAL(intarr16, out);
tmpout[0] := tmpin[1];
tmpout[1] := tmpin[0];
out := SYS.VAL(Int16, tmpout)
END htons;
END types.