Initial revision
This commit is contained in:
39
lib/socket/Makefile
Normal file
39
lib/socket/Makefile
Normal file
@@ -0,0 +1,39 @@
|
||||
|
||||
CFLAGS = -O -D_MINIX -D_POSIX_SOURCE -I../../servers
|
||||
CC1 = $(CC) $(CFLAGS) -c
|
||||
|
||||
LIBRARY = ../libsocket.a
|
||||
all: $(LIBRARY)
|
||||
|
||||
OBJECTS = \
|
||||
$(LIBRARY)(socket.o) \
|
||||
$(LIBRARY)(listen.o) \
|
||||
$(LIBRARY)(connect.o) \
|
||||
$(LIBRARY)(shutdown.o) \
|
||||
$(LIBRARY)(extra.o) \
|
||||
$(LIBRARY)(bind.o)
|
||||
|
||||
$(LIBRARY): $(OBJECTS)
|
||||
aal cr $@ *.o
|
||||
rm *.o
|
||||
|
||||
$(LIBRARY)(socket.o): socket.c
|
||||
$(CC1) socket.c
|
||||
|
||||
$(LIBRARY)(connect.o): connect.c
|
||||
$(CC1) connect.c
|
||||
|
||||
$(LIBRARY)(listen.o): listen.c
|
||||
$(CC1) listen.c
|
||||
|
||||
$(LIBRARY)(shutdown.o): shutdown.c
|
||||
$(CC1) shutdown.c
|
||||
|
||||
$(LIBRARY)(extra.o): extra.c
|
||||
$(CC1) extra.c
|
||||
|
||||
$(LIBRARY)(bind.o): bind.c
|
||||
$(CC1) bind.c
|
||||
|
||||
clean:
|
||||
-rm *.o $(LIBRARY)
|
||||
34
lib/socket/bind.c
Normal file
34
lib/socket/bind.c
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
/* bsd-socket(2)-lookalike */
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <net/ioctl.h>
|
||||
#include <net/gen/socket.h>
|
||||
#include <net/gen/emu.h>
|
||||
#include <net/gen/tcp.h>
|
||||
#include <net/gen/in.h>
|
||||
#include <net/gen/tcp_hdr.h>
|
||||
#include <net/gen/tcp_io.h>
|
||||
|
||||
int
|
||||
bind(int s, struct sockaddr *addr, socklen_t len)
|
||||
{
|
||||
nwio_tcpconf_t tcpconf;
|
||||
struct sockaddr_in *in_local;
|
||||
|
||||
in_local = (struct sockaddr_in *) addr;
|
||||
|
||||
memset(&tcpconf, 0, sizeof(tcpconf));
|
||||
tcpconf.nwtc_flags = NWTC_EXCL | NWTC_LP_SET | NWTC_UNSET_RA | NWTC_UNSET_RP;
|
||||
tcpconf.nwtc_locport = in_local->sin_port;
|
||||
|
||||
if(ioctl(s, NWIOSTCPCONF, &tcpconf) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
46
lib/socket/connect.c
Normal file
46
lib/socket/connect.c
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
/* bsd-socket(2)-lookalike */
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <net/ioctl.h>
|
||||
#include <net/gen/in.h>
|
||||
#include <net/gen/socket.h>
|
||||
#include <net/gen/emu.h>
|
||||
#include <net/gen/tcp.h>
|
||||
#include <net/gen/tcp_hdr.h>
|
||||
#include <net/gen/tcp_io.h>
|
||||
|
||||
int
|
||||
connect(int s, struct sockaddr *peer, socklen_t len)
|
||||
{
|
||||
nwio_tcpcl_t tcpopt;
|
||||
nwio_tcpconf_t tcpconf;
|
||||
struct sockaddr_in *in_peer;
|
||||
|
||||
if(!peer || peer->sa_family != AF_INET ||
|
||||
len != sizeof(struct sockaddr_in)) {
|
||||
errno = EAFNOSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
in_peer = (struct sockaddr_in *) peer;
|
||||
|
||||
memset(&tcpconf, 0, sizeof(tcpconf));
|
||||
tcpconf.nwtc_flags = NWTC_EXCL | NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
|
||||
tcpconf.nwtc_remaddr = in_peer->sin_addr.s_addr;
|
||||
tcpconf.nwtc_remport = in_peer->sin_port;
|
||||
|
||||
if(ioctl(s, NWIOSTCPCONF, &tcpconf) < 0)
|
||||
return -1;
|
||||
|
||||
tcpopt.nwtcl_flags = 0;
|
||||
tcpopt.nwtcl_ttl = 0;
|
||||
|
||||
if(ioctl(s, NWIOTCPCONN, &tcpopt) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
26
lib/socket/extra.c
Normal file
26
lib/socket/extra.c
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
/* bsd-socket(2)-lookalike */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <net/gen/socket.h>
|
||||
#include <net/gen/emu.h>
|
||||
#include <net/gen/tcp.h>
|
||||
#include <net/gen/in.h>
|
||||
#include <net/gen/tcp_hdr.h>
|
||||
#include <net/gen/tcp_io.h>
|
||||
|
||||
ssize_t
|
||||
recv(int s, void *buf, size_t len, int flags)
|
||||
{
|
||||
return read(s, buf, len);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
send(int s, void *buf, size_t len, int flags)
|
||||
{
|
||||
return write(s, buf, len);
|
||||
}
|
||||
|
||||
26
lib/socket/listen.c
Normal file
26
lib/socket/listen.c
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
/* bsd-socket(2)-lookalike */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <net/gen/socket.h>
|
||||
#include <net/gen/in.h>
|
||||
#include <net/ioctl.h>
|
||||
#include <net/gen/emu.h>
|
||||
#include <net/gen/tcp.h>
|
||||
#include <net/gen/tcp_hdr.h>
|
||||
#include <net/gen/tcp_io.h>
|
||||
|
||||
int
|
||||
listen(int s, int backlog)
|
||||
{
|
||||
nwio_tcpcl_t tcpopt;
|
||||
struct sockaddr_in *in_peer;
|
||||
|
||||
tcpopt.nwtcl_flags = tcpopt.nwtcl_ttl = 0;
|
||||
|
||||
if(ioctl(s, NWIOTCPLISTEN, &tcpopt) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
26
lib/socket/shutdown.c
Normal file
26
lib/socket/shutdown.c
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
/* bsd-socket(2)-lookalike */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/gen/socket.h>
|
||||
#include <net/gen/emu.h>
|
||||
#include <net/gen/tcp.h>
|
||||
#include <net/gen/in.h>
|
||||
#include <net/gen/tcp_hdr.h>
|
||||
#include <net/gen/tcp_io.h>
|
||||
|
||||
int
|
||||
shutdown(int s, int how)
|
||||
{
|
||||
nwio_tcpcl_t tcpopt;
|
||||
|
||||
if(ioctl(s, NWIOTCPSHUTDOWN, NULL) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
56
lib/socket/socket.c
Normal file
56
lib/socket/socket.c
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
/* bsd-socket(2)-lookalike */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <net/gen/socket.h>
|
||||
#include <net/gen/emu.h>
|
||||
#include <net/gen/tcp.h>
|
||||
#include <net/gen/in.h>
|
||||
#include <net/gen/tcp_hdr.h>
|
||||
#include <net/gen/tcp_io.h>
|
||||
|
||||
int
|
||||
socket(int domain, int type, int protocol)
|
||||
{
|
||||
int s;
|
||||
char *tcpname;
|
||||
|
||||
/* only domain is AF_INET */
|
||||
if(domain != AF_INET) {
|
||||
errno = EAFNOSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* only type is SOCK_STREAM */
|
||||
if(type != SOCK_STREAM) {
|
||||
errno = EPROTONOSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* default protocol type is TCP */
|
||||
if(!protocol)
|
||||
protocol = IPPROTO_TCP;
|
||||
|
||||
/* only protocol type is TCP */
|
||||
if(protocol != IPPROTO_TCP) {
|
||||
errno = EPROTONOSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* tcp device name */
|
||||
if(!tcpname)
|
||||
tcpname = getenv("TCP_DEVICE");
|
||||
if(!tcpname || !*tcpname)
|
||||
tcpname = "/dev/tcp";
|
||||
|
||||
if((s = open(tcpname, O_RDWR)) < 0) {
|
||||
perror(tcpname);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user