mirror of
https://github.com/vishapoberon/compiler.git
synced 2026-04-06 02:52:24 +00:00
now test server can listen on any ipv4 address.
This commit is contained in:
parent
e36e862c8c
commit
2178fa7b18
2 changed files with 68 additions and 62 deletions
|
|
@ -4,7 +4,7 @@ IMPORT sockets, types, Out := Console, SYSTEM, Platform, Strings;
|
||||||
|
|
||||||
|
|
||||||
PROCEDURE DoSmth(sock: Platform.FileHandle);
|
PROCEDURE DoSmth(sock: Platform.FileHandle);
|
||||||
VAR
|
VAR
|
||||||
str, aff: ARRAY 256 OF CHAR;
|
str, aff: ARRAY 256 OF CHAR;
|
||||||
n: LONGINT;
|
n: LONGINT;
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|
@ -15,7 +15,7 @@ BEGIN
|
||||||
ELSE
|
ELSE
|
||||||
str[n] := 0X; (* Make sure that received message is zero terminated *)
|
str[n] := 0X; (* Make sure that received message is zero terminated *)
|
||||||
Out.String("received message is "); Out.String(str); Out.Ln;
|
Out.String("received message is "); Out.String(str); Out.Ln;
|
||||||
|
|
||||||
IF Platform.Write(sock, SYSTEM.ADR(aff), Strings.Length(aff)) # 0 THEN
|
IF Platform.Write(sock, SYSTEM.ADR(aff), Strings.Length(aff)) # 0 THEN
|
||||||
Out.String("error writing to socket"); Out.Ln
|
Out.String("error writing to socket"); Out.Ln
|
||||||
END;
|
END;
|
||||||
|
|
@ -29,16 +29,17 @@ PROCEDURE -fork(): LONGINT "(LONGINT)fork()";
|
||||||
|
|
||||||
|
|
||||||
PROCEDURE serve;
|
PROCEDURE serve;
|
||||||
CONST
|
CONST
|
||||||
Port = 2055;
|
Port = 2055;
|
||||||
MaxQueue = 5;
|
MaxQueue = 5;
|
||||||
VAR
|
VAR
|
||||||
sockfd: LONGINT;
|
sockfd: LONGINT;
|
||||||
newsockfd: LONGINT;
|
newsockfd: LONGINT;
|
||||||
ServAddr: sockets.SockAddrIn;
|
ServAddr: sockets.SockAddrIn;
|
||||||
pid: LONGINT;
|
pid: LONGINT;
|
||||||
res: Platform.ErrorCode;
|
res: Platform.ErrorCode;
|
||||||
sockaddrlen: LONGINT;
|
sockaddrlen: LONGINT;
|
||||||
|
ipAddr: LONGINT; ip: ARRAY 16 OF CHAR;
|
||||||
BEGIN
|
BEGIN
|
||||||
sockfd := sockets.Socket(sockets.AfInet, sockets.SockStream, 0);
|
sockfd := sockets.Socket(sockets.AfInet, sockets.SockStream, 0);
|
||||||
IF sockfd < 0 THEN
|
IF sockfd < 0 THEN
|
||||||
|
|
@ -47,15 +48,17 @@ BEGIN
|
||||||
Out.String("socket created.")
|
Out.String("socket created.")
|
||||||
END;
|
END;
|
||||||
Out.Ln;
|
Out.Ln;
|
||||||
|
COPY("127.0.0.1", ip);
|
||||||
|
ipAddr := sockets.inetaddr(ip);
|
||||||
|
|
||||||
sockets.SetSockAddrIn(sockets.AfInet, Port, 0, ServAddr);
|
sockets.SetSockAddrIn(sockets.AfInet, Port, ipAddr, ServAddr);
|
||||||
IF sockets.Bind(sockfd, SYSTEM.VAL(sockets.SockAddr, ServAddr), SIZE(sockets.SockAddr)) < 0 THEN
|
IF sockets.Bind(sockfd, SYSTEM.VAL(sockets.SockAddr, ServAddr), SIZE(sockets.SockAddr)) < 0 THEN
|
||||||
Out.String("error on binding")
|
Out.String("error on binding")
|
||||||
ELSE
|
ELSE
|
||||||
Out.String("binding completed.")
|
Out.String("binding completed.")
|
||||||
END;
|
END;
|
||||||
Out.Ln;
|
Out.Ln;
|
||||||
|
|
||||||
IF sockets.Listen(sockfd, MaxQueue) # 0 THEN
|
IF sockets.Listen(sockfd, MaxQueue) # 0 THEN
|
||||||
Out.String("listen() failed");
|
Out.String("listen() failed");
|
||||||
ELSE
|
ELSE
|
||||||
|
|
|
||||||
|
|
@ -2,60 +2,60 @@ MODULE sockets;
|
||||||
|
|
||||||
IMPORT SYSTEM, oocC;
|
IMPORT SYSTEM, oocC;
|
||||||
|
|
||||||
CONST
|
CONST
|
||||||
SockStream* = 1;
|
SockStream* = 1;
|
||||||
SockDgram* = 2;
|
SockDgram* = 2;
|
||||||
SockRaw* = 3;
|
SockRaw* = 3;
|
||||||
SockRdm* = 4;
|
SockRdm* = 4;
|
||||||
SockSeqpacket* = 5;
|
SockSeqpacket* = 5;
|
||||||
SockDccp* = 6;
|
SockDccp* = 6;
|
||||||
SockPacket* = 10;
|
SockPacket* = 10;
|
||||||
|
|
||||||
AfUnscec* = 0; (* Unspecified. *)
|
AfUnscec* = 0; (* Unspecified. *)
|
||||||
AfLocal* = 1; (* Local to host (pipes and file-domain). *)
|
AfLocal* = 1; (* Local to host (pipes and file-domain). *)
|
||||||
AfUnix* = 1; (* POSIX name for PF_LOCAL. *)
|
AfUnix* = 1; (* POSIX name for PF_LOCAL. *)
|
||||||
AfFile* = 1; (* Another non-standard name for PF_LOCAL. *)
|
AfFile* = 1; (* Another non-standard name for PF_LOCAL. *)
|
||||||
AfInet* = 2; (* IP protocol family. *)
|
AfInet* = 2; (* IP protocol family. *)
|
||||||
AfAx25* = 3; (* Amateur Radio AX.25. *)
|
AfAx25* = 3; (* Amateur Radio AX.25. *)
|
||||||
AfIpx* = 4; (* Novell Internet Protocol. *)
|
AfIpx* = 4; (* Novell Internet Protocol. *)
|
||||||
AfAppletalk* = 5; (* Appletalk DDP. *)
|
AfAppletalk* = 5; (* Appletalk DDP. *)
|
||||||
AfNetrom* = 6; (* Amateur radio NetROM. *)
|
AfNetrom* = 6; (* Amateur radio NetROM. *)
|
||||||
AfBridge* = 7; (* Multiprotocol bridge. *)
|
AfBridge* = 7; (* Multiprotocol bridge. *)
|
||||||
AfAtmpvc* = 8; (* ATM PVCs. *)
|
AfAtmpvc* = 8; (* ATM PVCs. *)
|
||||||
AfX25* = 9; (* Reserved for X.25 project. *)
|
AfX25* = 9; (* Reserved for X.25 project. *)
|
||||||
AfInet6* = 10; (* IP version 6. *)
|
AfInet6* = 10; (* IP version 6. *)
|
||||||
AfRose* = 11; (* Amateur Radio X.25 PLP. *)
|
AfRose* = 11; (* Amateur Radio X.25 PLP. *)
|
||||||
AfDecnet* = 12; (* Reserved for DECnet project. *)
|
AfDecnet* = 12; (* Reserved for DECnet project. *)
|
||||||
AfNetbeui* = 13; (* Reserved for 802.2LLC project. *)
|
AfNetbeui* = 13; (* Reserved for 802.2LLC project. *)
|
||||||
AfSecurity* = 14; (* Security callback pseudo AF. *)
|
AfSecurity* = 14; (* Security callback pseudo AF. *)
|
||||||
AfKey* = 15; (* PF_KEY key management API. *)
|
AfKey* = 15; (* PF_KEY key management API. *)
|
||||||
AfNetlink* = 16;
|
AfNetlink* = 16;
|
||||||
AfRoute* = 16; (* Alias to emulate 4.4BSD. *)
|
AfRoute* = 16; (* Alias to emulate 4.4BSD. *)
|
||||||
AfPacket = 17; (* Packet family. *)
|
AfPacket = 17; (* Packet family. *)
|
||||||
AfAsh = 18; (* Ash. *)
|
AfAsh = 18; (* Ash. *)
|
||||||
AfEconet* = 19; (* Acorn Econet. *)
|
AfEconet* = 19; (* Acorn Econet. *)
|
||||||
AfAtmsvc* = 20; (* ATM SVCs. *)
|
AfAtmsvc* = 20; (* ATM SVCs. *)
|
||||||
AfRds* = 21; (* RDS sockets. *)
|
AfRds* = 21; (* RDS sockets. *)
|
||||||
AfSna = 22; (* Linux SNA Project *)
|
AfSna = 22; (* Linux SNA Project *)
|
||||||
AfIrda* = 23; (* IRDA sockets. *)
|
AfIrda* = 23; (* IRDA sockets. *)
|
||||||
AfPppox = 24; (* PPPoX sockets. *)
|
AfPppox = 24; (* PPPoX sockets. *)
|
||||||
AfWanpipe* = 25; (* Wanpipe API sockets. *)
|
AfWanpipe* = 25; (* Wanpipe API sockets. *)
|
||||||
AfLlc* = 26; (* Linux LLC. *)
|
AfLlc* = 26; (* Linux LLC. *)
|
||||||
AfCan* = 29; (* Controller Area Network. *)
|
AfCan* = 29; (* Controller Area Network. *)
|
||||||
AfTipc* = 30; (* TIPC sockets. *)
|
AfTipc* = 30; (* TIPC sockets. *)
|
||||||
AfBluetooth* = 31; (* Bluetooth sockets. *)
|
AfBluetooth* = 31; (* Bluetooth sockets. *)
|
||||||
AfIucv* = 32; (* IUCV sockets. *)
|
AfIucv* = 32; (* IUCV sockets. *)
|
||||||
AfRxrpc* = 33; (* RxRPC sockets. *)
|
AfRxrpc* = 33; (* RxRPC sockets. *)
|
||||||
AfIsdn* = 34; (* mISDN sockets. *)
|
AfIsdn* = 34; (* mISDN sockets. *)
|
||||||
AfPhonet* = 35; (* Phonet sockets. *)
|
AfPhonet* = 35; (* Phonet sockets. *)
|
||||||
AfIeee802154* = 36; (* IEEE 802.15.4 sockets. *)
|
AfIeee802154* = 36; (* IEEE 802.15.4 sockets. *)
|
||||||
AfCaif* = 37; (* CAIF sockets. *)
|
AfCaif* = 37; (* CAIF sockets. *)
|
||||||
AfAlg* = 38; (* Algorithm sockets. *)
|
AfAlg* = 38; (* Algorithm sockets. *)
|
||||||
AfNfc* = 39; (* NFC sockets. *)
|
AfNfc* = 39; (* NFC sockets. *)
|
||||||
AfVsock* = 40; (* vSockets. *)
|
AfVsock* = 40; (* vSockets. *)
|
||||||
AfMax* = 41; (* For now.. *)
|
AfMax* = 41; (* For now.. *)
|
||||||
|
|
||||||
InAddrAny* = 0;
|
InAddrAny* = 0;
|
||||||
|
|
||||||
TYPE
|
TYPE
|
||||||
(* /usr/include/netinet/in.h *)
|
(* /usr/include/netinet/in.h *)
|
||||||
|
|
@ -70,13 +70,16 @@ TYPE
|
||||||
SinZero*: ARRAY 8 OF CHAR;
|
SinZero*: ARRAY 8 OF CHAR;
|
||||||
END;
|
END;
|
||||||
(* /usr/include/sys/socket.h *)
|
(* /usr/include/sys/socket.h *)
|
||||||
|
|
||||||
SockAddr* = RECORD
|
SockAddr* = RECORD
|
||||||
SaFamily*: oocC.shortint;
|
SaFamily*: oocC.shortint;
|
||||||
SaData*: ARRAY 14 OF CHAR
|
SaData*: ARRAY 14 OF CHAR
|
||||||
END;
|
END;
|
||||||
|
|
||||||
PROCEDURE -includesocket "#include <sys/socket.h>";
|
PROCEDURE -includesocket "#include <sys/socket.h>";
|
||||||
|
PROCEDURE -includeInet "#include <arpa/inet.h>";
|
||||||
|
|
||||||
|
PROCEDURE -inetaddr*(s: ARRAY OF CHAR): LONGINT "(LONGINT)inet_addr((char*)s)";
|
||||||
|
|
||||||
PROCEDURE -SetCShort(i: INTEGER; VAR si: oocC.shortint)
|
PROCEDURE -SetCShort(i: INTEGER; VAR si: oocC.shortint)
|
||||||
"*(short*)si = i";
|
"*(short*)si = i";
|
||||||
|
|
@ -84,7 +87,7 @@ TYPE
|
||||||
PROCEDURE -SetCShortSwapped(i: INTEGER; VAR si: oocC.shortint)
|
PROCEDURE -SetCShortSwapped(i: INTEGER; VAR si: oocC.shortint)
|
||||||
"*(short*)si = ((i >> 8) & 0x00ff) | ((i << 8) & 0xff00)";
|
"*(short*)si = ((i >> 8) & 0x00ff) | ((i << 8) & 0xff00)";
|
||||||
|
|
||||||
PROCEDURE SetSockAddrIn*(family, port, inaddr: INTEGER; VAR sai: SockAddrIn);
|
PROCEDURE SetSockAddrIn*(family, port: INTEGER; inaddr: LONGINT; VAR sai: SockAddrIn);
|
||||||
VAR i: INTEGER;
|
VAR i: INTEGER;
|
||||||
BEGIN
|
BEGIN
|
||||||
SetCShort(family, sai.SinFamily);
|
SetCShort(family, sai.SinFamily);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue