From 818c477d6f8e6cdc2efe8ac9e3eff2e131ff546e Mon Sep 17 00:00:00 2001 From: superturbocat2001 Date: Thu, 3 Dec 2020 17:27:05 +0000 Subject: [PATCH] - Added functions for working with sockets in libck tcc git-svn-id: svn://kolibrios.org@8315 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/develop/ktcc/trunk/bin/lib/libck.a | Bin 131644 -> 133124 bytes programs/develop/ktcc/trunk/libc/Makefile | 4 +- programs/develop/ktcc/trunk/libc/build.bat | 2 +- .../ktcc/trunk/libc/include/net/socket.h | 107 ++++++++++++++++++ programs/develop/ktcc/trunk/libc/net/socket.c | 100 ++++++++++++++++ programs/develop/ktcc/trunk/samples/Makefile | 28 ++--- .../develop/ktcc/trunk/samples/build_all.sh | 1 + .../ktcc/trunk/samples/net/tcpsrv_demo.c | 36 ++++++ 8 files changed, 262 insertions(+), 16 deletions(-) create mode 100644 programs/develop/ktcc/trunk/libc/include/net/socket.h create mode 100644 programs/develop/ktcc/trunk/libc/net/socket.c create mode 100644 programs/develop/ktcc/trunk/samples/net/tcpsrv_demo.c diff --git a/programs/develop/ktcc/trunk/bin/lib/libck.a b/programs/develop/ktcc/trunk/bin/lib/libck.a index b06062aad8bff8a610dccd942fcff620021c0a58..270747e86899a5052a4fa5ac929eb71041964339 100644 GIT binary patch delta 2397 zcmcJQe{2(F7{|ZQt<|CnDKIQ%h;1$@YZALI!g>X01X z0DQu2N2ddy^5kx$o%@~`06O&qUEC*`ZXWbZkK}9`XsRZ5Q0o}{(ncF1y zSA()Ro|z7s^%E$M^>e%l3OO$^K=WHA3!R`Fd14o+GzKbjj}KohXjL_6^)ol~;&Y&z zFh)v_k*oEAHUjk80#N-1P{a?~pk(&(?g;kx4r9a}I-uaT&^!nde7rrCBOZ{TwL2L;|6?7J zeDK$Vbu9oj7zk*pjuon^ zt<@3MxGAKm>)6vprywU+C0to&?TVTZke_chx^m3}F8jt#*WpeVg)Z8M+qX{2q=#&U zMa2cCd)jQ}c$zshH`7>J(ef04BMcfAI~tayH@xKh!drizNIbv%oHgpL>ehPC2`mowrss7hURg{9|j zY+{2(m&@Xg>*9FTlzoz-2`tKydp@G-deU6dJjGmiFw+nlF$oG>Y{iquOa@_wGbunh$Jpnbo;YdoSl=X5;=@wA#EdFWX*_22`3? NJlSt}ZDw0r#y>G`NH_oh delta 1353 zcmcK3O-NKx6u|NS!3wnkk)k0b3d%whr!X{eA*dzcA|j+k3n__$h$v|lJh2G`Ata{| zE1F6SiAqxJgCScOS_vA8S`9gg z02kbF(F<3;&*agVb+Zp@>!B_S?rQOR9Z*B2(G-M7+UC_>nssoqwP_>Jy7ge$ zdP + +// Socket Types +#define SOCK_STREAM 1 +#define SOCK_DGRAM 2 +#define SOCK_RAW 3 + +// IP protocols +#define IPPROTO_IP 0 +#define IPPROTO_ICMP 1 +#define IPPROTO_TCP 6 +#define IPPROTO_UDP 17 +#define IPPROTO_RAW 255 + +// IP options +#define IP_TTL 2 + +// Address families +#define AF_UNSPEC 0 +#define AF_LOCAL 1 +#define AF_INET4 2 // IPv4 +#define AF_INET6 10 // IPv6 + +#define PF_UNSPEC AF_UNSPEC +#define PF_LOCAL AF_LOCAL +#define PF_INET4 AF_INET4 +#define PF_INET6 AF_INET6 + +// internal definition +#define AI_SUPPORTED 0x40F + +// for system function 76 +#define API_ETH (0<<16) +#define API_IPv4 (1<<16) +#define API_ICMP (2<<16) +#define API_UDP (3<<16) +#define API_TCP (4<<16) +#define API_ARP (5<<16) +#define API_PPPOE (6<<16) + +// Socket flags for user calls +#define MSG_NOFLAG 0 +#define MSG_PEEK 0x02 +#define MSG_DONTWAIT 0x40 + +// Socket levels +#define SOL_SOCKET 0xffff + +//Socket options +#define SO_BINDTODEVICE (1<<9) +#define SO_NONBLOCK (1<<31) + +// Error Codes +#define ENOBUFS 1 +#define EINPROGRESS 2 +#define EOPNOTSUPP 4 +#define EWOULDBLOCK 6 +#define ENOTCONN 9 +#define EALREADY 10 +#define EINVALUE 11 +#define EMSGSIZE 12 +#define ENOMEM 18 +#define EADDRINUSE 20 +#define ECONNREFUSED 61 +#define ECONNRESET 52 +#define EISCONN 56 +#define ETIMEDOUT 60 +#define ECONNABORTED 53 + + +#define PORT(X) (X<<8) +extern int err_code; + +#pragma pack(push,1) +typedef struct{ + unsigned short sin_family; + unsigned short sin_port; + unsigned int sin_addr; + unsigned long long sin_zero; +}sockaddr; +#pragma pack(pop) + +#pragma pack(push, 1) +typedef struct{ + unsigned int level; + unsigned int optionname; + unsigned int optlenght; + unsigned char options; +}optstruct; +#pragma pack(pop) + +int socket(int domain, int type, int protocol); +int close(int socket); +int bind(int socket, const sockaddr *addres, int addres_len); +int listen(int socket, int backlog); +int connect(int socket, const sockaddr* address, int socket_len); +int accept(int socket, const sockaddr* address, int address_len); +int send(int socket, const void *message, size_t msg_len, int flag); +int recv(int socket, void *buffer, size_t buff_len, int flag); +int setsockopt(int socket,const optstruct* opt); +int getsockopt(int socket, optstruct* opt); +int socketpair(); + +#endif diff --git a/programs/develop/ktcc/trunk/libc/net/socket.c b/programs/develop/ktcc/trunk/libc/net/socket.c new file mode 100644 index 0000000000..1fde2dc152 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/net/socket.c @@ -0,0 +1,100 @@ +#include + +int err_code=0; + +int socket(int domain, int type, int protocol) +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + :"a"(75), "b"(0), "c"(domain), "d"(type), "S"(protocol) + ); +} + +int close(int socket) +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + :"a"(75), "b"(1), "c"(socket) + ); +} +int bind(int socket, const sockaddr *addres, int addres_len) +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + :"a"(75), "b"(2), "c"(socket), "d"(addres), "S"(addres_len) + ); +} + +int listen(int socket, int backlog) +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + :"a"(75), "b"(3), "c"(socket), "d"(backlog) + ); +} + +int connect(int socket,const sockaddr* address, int socket_len) +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + :"a"(75), "b"(4), "c"(socket), "d"(address), "S"(socket_len) + ); +} + +int accept(int socket, const sockaddr *address, int address_len) +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + :"a"(75), "b"(5), "c"(socket), "d"(address), "S"(address_len) + ); +} + +int send(int socket, const void *message, size_t msg_len, int flag) +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + :"a"(75), "b"(6), "c"(socket), "d"(message), "S"(msg_len), "D"(flag) + ); +} + +int recv(int socket, void *buffer, size_t buff_len, int flag) +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + :"a"(75), "b"(7), "c"(socket), "d"(buffer), "S"(buff_len), "D"(flag) + ); +} + +int setsockopt(int socket,const optstruct* opt) +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + :"a"(75), "b"(8), "c"(socket),"d"(opt) + ); +} + +int getsockopt(int socket, optstruct* opt) +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + :"a"(75), "b"(9), "c"(socket),"d"(opt) + ); +} + +int socketpair() +{ + asm volatile( + "int $0x40" + :"=b"(err_code) + ); +} diff --git a/programs/develop/ktcc/trunk/samples/Makefile b/programs/develop/ktcc/trunk/samples/Makefile index f39c13990e..92e8f7ba6c 100755 --- a/programs/develop/ktcc/trunk/samples/Makefile +++ b/programs/develop/ktcc/trunk/samples/Makefile @@ -1,19 +1,21 @@ all: - kos32-tcc asm_ex.c -lck -o asm_ex.kex -I ../libc/include - kos32-tcc consoleio.c -lck -o consoleio.kex -I ../libc/include - kos32-tcc files.c -lck -o files.kex -I ../libc/include - kos32-tcc winbasics.c -lck -o winbasics.kex -I ../libc/include - kos32-tcc dynamic.c -lck -lhttp -linputbox -o dynamic.kex -I ../libc/include - kos32-tcc load_coff.c -o load_coff.kex -lck -I ../libc/include - kos32-tcc clayer/msgbox.c -lck -lmsgbox -o clayer/msgbox.kex -I ../libc/include - kos32-tcc graphics.c -lck -lgb -o graphics.kex -I ../libc/include - kos32-tcc clayer/rasterworks.c -lck -lrasterworks -o clayer/rasterworks.kex -I ../libc/include - kos32-tcc clayer/boxlib.c -lck -lbox -o clayer/boxlib.kex -I ../libc/include - kos32-tcc clayer/libimg.c -lck -limg -o clayer/libimg.kex -I ../libc/include - kos32-tcc console/console.c -lck -limg -o console/console.kex -I ../libc/include - kos32-tcc dir_example.c -lck -o dir_example.kex -I ../libc/include + ../bin/kos32-tcc asm_ex.c -lck -o asm_ex.kex -I ../libc/include + ../bin/kos32-tcc consoleio.c -lck -o consoleio.kex -I ../libc/include + ../bin/kos32-tcc files.c -lck -o files.kex -I ../libc/include + ../bin/kos32-tcc winbasics.c -lck -o winbasics.kex -I ../libc/include + ../bin/kos32-tcc dynamic.c -lck -lhttp -linputbox -o dynamic.kex -I ../libc/include + ../bin/kos32-tcc load_coff.c -o load_coff.kex -lck -I ../libc/include + ../bin/kos32-tcc clayer/msgbox.c -lck -lmsgbox -o clayer/msgbox.kex -I ../libc/include + ../bin/kos32-tcc graphics.c -lck -lgb -o graphics.kex -I ../libc/include + ../bin/kos32-tcc clayer/rasterworks.c -lck -lrasterworks -o clayer/rasterworks.kex -I ../libc/include + ../bin/kos32-tcc clayer/boxlib.c -lck -lbox -o clayer/boxlib.kex -I ../libc/include + ../bin/kos32-tcc clayer/libimg.c -lck -limg -o clayer/libimg.kex -I ../libc/include + ../bin/kos32-tcc console/console.c -lck -limg -o console/console.kex -I ../libc/include + ../bin/kos32-tcc dir_example.c -lck -o dir_example.kex -I ../libc/include + ../bin/kos32-tcc net/tcpsrv_demo.c -lck -o net/tcpsrv_demo.kex -I ../libc/include clean: rm *.kex rm clayer/*.kex rm console/*.kex + rm net/*.kex diff --git a/programs/develop/ktcc/trunk/samples/build_all.sh b/programs/develop/ktcc/trunk/samples/build_all.sh index 8ef72d69ff..7aa7919dda 100644 --- a/programs/develop/ktcc/trunk/samples/build_all.sh +++ b/programs/develop/ktcc/trunk/samples/build_all.sh @@ -12,4 +12,5 @@ ../tcc clayer/libimg.c -lck -limg -o /tmp0/1/libimg_ex ../tcc console/console.c -lck -limg -o /tmp0/1/console ../tcc dir_example.c -lck -o /tmp0/1/dir_example +../tcc net/tcpsrv_demo.c -lck -o /tmp0/1/tcpsrv_demo exit diff --git a/programs/develop/ktcc/trunk/samples/net/tcpsrv_demo.c b/programs/develop/ktcc/trunk/samples/net/tcpsrv_demo.c new file mode 100644 index 0000000000..51c7ffe384 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/net/tcpsrv_demo.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +const char msg1[]="Hello!"; +char msg2='\0'; + +int main() +{ + sockaddr addr={AF_INET4, PORT(23) , 0, 0}; + + int sk1=socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP); + printf("Open socket: %d. Error: %d\n",sk1, err_code); + + bind(sk1, &addr,sizeof(addr)); + printf("Socket binding. Error: %d\n", err_code); + + listen(sk1, 1); + printf("Listening to a socket. Error: %d\n", err_code); + int sk2 = accept(sk1, &addr, sizeof(addr)); + printf("Accept done. Error: %d\n", err_code); + + send(sk2, msg1, strlen(msg1),MSG_NOFLAG); + printf("Send message: '%s' Error: %d\n", msg1, err_code); + puts("Received data:"); + while(msg2!='!') + { + recv(sk2, &msg2, 1, MSG_NOFLAG); + printf("%c",msg2); + } + close(sk1); + close(sk2); + puts("\nGood bye!"); + exit(0); +}