forked from KolibriOS/kolibrios
- Added functions for working with sockets in libck tcc
git-svn-id: svn://kolibrios.org@8315 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
parent
bc2c4a193e
commit
818c477d6f
Binary file not shown.
@ -3,7 +3,7 @@ LIBSFORBUILD = math
|
|||||||
LIBNAME = libck.a
|
LIBNAME = libck.a
|
||||||
CC = ../bin/kos32-tcc
|
CC = ../bin/kos32-tcc
|
||||||
CFLAGS = -I$(INCLUDE) -m32 -nostdinc -nostdlib -DGNUC
|
CFLAGS = -I$(INCLUDE) -m32 -nostdinc -nostdlib -DGNUC
|
||||||
DIRS := stdio memory kolibrisys string stdlib math dlfcn libgen fs
|
DIRS := stdio memory kolibrisys string stdlib math dlfcn libgen fs net
|
||||||
|
|
||||||
##############################################################
|
##############################################################
|
||||||
#files := $(foreach dir,$(DIRS),$(dir)/$(wildcard $(dir)/*))
|
#files := $(foreach dir,$(DIRS),$(dir)/$(wildcard $(dir)/*))
|
||||||
@ -19,7 +19,7 @@ doClean = rm $(cfiles) $(asmfiles)
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
all: $(cfiles) $(asmfiles)
|
all: $(cfiles) $(asmfiles)
|
||||||
ar -ru $(LIBNAME) $^
|
ar -rcs $(LIBNAME) $^
|
||||||
|
|
||||||
$(cfiles): $(INCLUDE)/*.h
|
$(cfiles): $(INCLUDE)/*.h
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ set CC=kos32-tcc
|
|||||||
set CFLAGS=-c -nostdinc -DGNUC -I"%cd%\%INCLUDE%" -Wall
|
set CFLAGS=-c -nostdinc -DGNUC -I"%cd%\%INCLUDE%" -Wall
|
||||||
set AR=kos32-ar
|
set AR=kos32-ar
|
||||||
set ASM=fasm
|
set ASM=fasm
|
||||||
set dirs=stdio memory kolibrisys string stdlib math dlfcn libgen fs
|
set dirs=stdio memory kolibrisys string stdlib math dlfcn libgen fs net
|
||||||
rem #### END OF CONFIG SECTION ####
|
rem #### END OF CONFIG SECTION ####
|
||||||
|
|
||||||
set objs=
|
set objs=
|
||||||
|
107
programs/develop/ktcc/trunk/libc/include/net/socket.h
Normal file
107
programs/develop/ktcc/trunk/libc/include/net/socket.h
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
#ifndef __SOCKET_H
|
||||||
|
#define __SOCKET_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// 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
|
100
programs/develop/ktcc/trunk/libc/net/socket.c
Normal file
100
programs/develop/ktcc/trunk/libc/net/socket.c
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
#include <net/socket.h>
|
||||||
|
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
}
|
@ -1,19 +1,21 @@
|
|||||||
all:
|
all:
|
||||||
kos32-tcc asm_ex.c -lck -o asm_ex.kex -I ../libc/include
|
../bin/kos32-tcc asm_ex.c -lck -o asm_ex.kex -I ../libc/include
|
||||||
kos32-tcc consoleio.c -lck -o consoleio.kex -I ../libc/include
|
../bin/kos32-tcc consoleio.c -lck -o consoleio.kex -I ../libc/include
|
||||||
kos32-tcc files.c -lck -o files.kex -I ../libc/include
|
../bin/kos32-tcc files.c -lck -o files.kex -I ../libc/include
|
||||||
kos32-tcc winbasics.c -lck -o winbasics.kex -I ../libc/include
|
../bin/kos32-tcc winbasics.c -lck -o winbasics.kex -I ../libc/include
|
||||||
kos32-tcc dynamic.c -lck -lhttp -linputbox -o dynamic.kex -I ../libc/include
|
../bin/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
|
../bin/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
|
../bin/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
|
../bin/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
|
../bin/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
|
../bin/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
|
../bin/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
|
../bin/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 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:
|
clean:
|
||||||
rm *.kex
|
rm *.kex
|
||||||
rm clayer/*.kex
|
rm clayer/*.kex
|
||||||
rm console/*.kex
|
rm console/*.kex
|
||||||
|
rm net/*.kex
|
||||||
|
@ -12,4 +12,5 @@
|
|||||||
../tcc clayer/libimg.c -lck -limg -o /tmp0/1/libimg_ex
|
../tcc clayer/libimg.c -lck -limg -o /tmp0/1/libimg_ex
|
||||||
../tcc console/console.c -lck -limg -o /tmp0/1/console
|
../tcc console/console.c -lck -limg -o /tmp0/1/console
|
||||||
../tcc dir_example.c -lck -o /tmp0/1/dir_example
|
../tcc dir_example.c -lck -o /tmp0/1/dir_example
|
||||||
|
../tcc net/tcpsrv_demo.c -lck -o /tmp0/1/tcpsrv_demo
|
||||||
exit
|
exit
|
||||||
|
36
programs/develop/ktcc/trunk/samples/net/tcpsrv_demo.c
Normal file
36
programs/develop/ktcc/trunk/samples/net/tcpsrv_demo.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include <net/socket.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user