diff --git a/Makefile b/Makefile index af20791..fa51fa1 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ VPATH=str:byte:fmt:scan:uint:open:stralloc:unix:socket:buffer CC=egcc #CFLAGS=-I. -pipe -Wall -Os -march=pentiumpro -fomit-frame-pointer -fschedule-insns2 -Wall -CFLAGS=-I. -I../dietlibc/include -pipe -Wall -g #-Os -march=athlon -mcpu=athlon -fomit-frame-pointer -fschedule-insns2 +CFLAGS=-I. -I../dietlibc/include -pipe -Wall -Os -march=athlon -mcpu=athlon -malign-functions=2 -fomit-frame-pointer -fschedule-insns2 #CFLAGS=-I../dietlibc/include -I. -pipe -Wall -Os -march=pentiumpro -mcpu=athlon -fomit-frame-pointer -fschedule-insns2 -Wall #CFLAGS=-I../dietlibc/include -pipe -Os -march=pentiumpro -mcpu=pentiumpro -fomit-frame-pointer -fschedule-insns2 -Wall diff --git a/buffer/buffer_putsflush.c b/buffer/buffer_putsflush.c index 28c6fec..f13e639 100644 --- a/buffer/buffer_putsflush.c +++ b/buffer/buffer_putsflush.c @@ -2,5 +2,5 @@ #include "buffer.h" int buffer_puts(buffer* b,const char* x) { - buffer_putflush(b,x,str_len(x)); + return buffer_putflush(b,x,str_len(x)); } diff --git a/socket/socket_accept4.3 b/socket/socket_accept4.3 new file mode 100644 index 0000000..5ce4132 --- /dev/null +++ b/socket/socket_accept4.3 @@ -0,0 +1,33 @@ +.TH socket_accept4 3 +.SH NAME +socket_accept4 \- accept an IPv4 TCP connection on a socket +.SH SYNTAX +.B #include + +int \fBsocket_accept4\fP(int \fIs\fR,char \fIip\fR[4],uint16 *\fIport\fR); +.SH DESCRIPTION +When a TCP connection arrives on a listening TCP socket \fIs\fR, the +socket becomes readable. + +socket_accept4 accepts the connection. It sets \fIip\fR and \fIport\fR +to the client IP address and client TCP port. It creates a new socket +for the connection, and returns a file descriptor pointing to the new +socket; you can use the read and write system calls to transmit data +through that file descriptor. + +If something goes wrong, socket_accept4 returns -1, setting errno +appropriately, without creating a new socket. + +.SH EXAMPLE + #include + + int \fIs\fR; + char \fIip\fR[4]; + uint16 \fIp\fR; + + \fIs\fR = socket_tcp(); + socket_bind4(s,ip,p); + socket_connect4(s,ip,p); + +.SH "SEE ALSO" +socket_accept6(3), socket_connected(3) diff --git a/socket/socket_accept6.3 b/socket/socket_accept6.3 new file mode 100644 index 0000000..4143817 --- /dev/null +++ b/socket/socket_accept6.3 @@ -0,0 +1,36 @@ +.TH socket_accept6 3 +.SH NAME +socket_accept6 \- accept an IPv6 TCP connection on a socket +.SH SYNTAX +.B #include + +int \fBsocket_accept6\fP(int \fIs\fR,char \fIip\fR[16],uint16 *\fIport\fR,uint32 *\fIscope_id\fR); +.SH DESCRIPTION +When a TCP connection arrives on a listening TCP socket \fIs\fR, the +socket becomes readable. + +socket_accept6 accepts the connection. It sets \fIip\fR and \fIport\fR +to the client IP address and client TCP port and \fIscope_id\fR to the +IPv6 scope ID. It creates a new socket for the connection, and returns +a file descriptor pointing to the new socket; you can use the read and +write system calls to transmit data through that file descriptor. + +If \fIscope_id\fR is the null pointer, socket_accept6 will discard the +scope ID. + +If something goes wrong, socket_accept6 returns -1, setting errno +appropriately, without creating a new socket. + +.SH EXAMPLE + #include + + int \fIs\fR; + char \fIip\fR[16]; + uint16 \fIp\fR; + + \fIs\fR = socket_tcp6(); + socket_bind6(s,ip,p); + socket_connect6(s,ip,p); + +.SH "SEE ALSO" +socket_accept4(3), socket_connected(3) diff --git a/socket/socket_bind4.3 b/socket/socket_bind4.3 new file mode 100644 index 0000000..66f2195 --- /dev/null +++ b/socket/socket_bind4.3 @@ -0,0 +1,30 @@ +.TH socket_bind4 3 +.SH NAME +socket_bind4 \- set the local IP address and port of a socket +.SH SYNTAX +.B #include + +int \fBsocket_bind4\fP(int \fIs\fR,char \fIip\fR[4],uint16 \fIport\fR); +.SH DESCRIPTION +socket_bind4 sets the local IP address and TCP/UDP port of a TCP/UDP +socket \fIs\fR to \fIip\fR and \fIport\fR respectively. + +If the IP address is 0.0.0.0, the operating system chooses a local IP +address. If \fIport\fR is 0, the operating system chooses a port. + +Normally socket_bind4 returns 0. If anything goes wrong, socket_bind4 +returns -1, setting errno appropriately. + +.SH EXAMPLE + #include + + int \fIs\fR; + char \fIip\fR[4]; + uint16 \fIp\fR; + + \fIs\fR = socket_tcp(); + socket_bind4(s,ip,p); + socket_connect4(s,ip,p); + +.SH "SEE ALSO" +socket_bind6(3) diff --git a/socket/socket_bind4_reuse.c b/socket/socket_bind4_reuse.c new file mode 100644 index 0000000..927a635 --- /dev/null +++ b/socket/socket_bind4_reuse.c @@ -0,0 +1,8 @@ +#include +#include "socket.h" + +int socket_bind4_reuse(int s,const char *ip,uint16 port) { + int one=1; + setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&one,sizeof one); + return socket_bind4(s,ip,port); +} diff --git a/socket/socket_bind6.3 b/socket/socket_bind6.3 new file mode 100644 index 0000000..da66749 --- /dev/null +++ b/socket/socket_bind6.3 @@ -0,0 +1,30 @@ +.TH socket_bind6 3 +.SH NAME +socket_bind6 \- set the local IP address and port of a socket +.SH SYNTAX +.B #include + +int \fBsocket_bind6\fP(int \fIs\fR,char \fIip\fR[16],uint16 \fIport\fR); +.SH DESCRIPTION +socket_bind6 sets the local IP address and TCP/UDP port of a TCP/UDP +socket \fIs\fR to \fIip\fR and \fIport\fR respectively. + +If the IP address is ::, the operating system chooses a local IP +address. If \fIport\fR is 0, the operating system chooses a port. + +Normally socket_bind6 returns 0. If anything goes wrong, socket_bind6 +returns -1, setting errno appropriately. + +.SH EXAMPLE + #include + + int \fIs\fR; + char \fIip\fR[16]; + uint16 \fIp\fR; + + \fIs\fR = socket_tcp6(); + socket_bind6(s,ip,p); + socket_connect6(s,ip,p); + +.SH "SEE ALSO" +socket_bind4(3) diff --git a/socket/socket_bind6_reuse.c b/socket/socket_bind6_reuse.c new file mode 100644 index 0000000..61fc6e6 --- /dev/null +++ b/socket/socket_bind6_reuse.c @@ -0,0 +1,8 @@ +#include +#include "socket.h" + +int socket_bind6_reuse(int s,const char *ip,uint16 port,uint32 scope_id) { + int one=1; + setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&one,sizeof one); + return socket_bind6(s,ip,port,scope_id); +} diff --git a/socket/socket_broadcast.3 b/socket/socket_broadcast.3 new file mode 100644 index 0000000..9eaae87 --- /dev/null +++ b/socket/socket_broadcast.3 @@ -0,0 +1,29 @@ +.TH socket_broadcast 3 +.SH NAME +socket_broadcast \- set UDP socket to broadcast mode +.SH SYNTAX +.B #include + +int \fBsocket_broadcast\fP(int \fIs\fR); +.SH DESCRIPTION +socket_broadcast sets UDP socket \fIs\fR to broadcast mode. +socket_send4 and socket_send6 will fail to send packets to the broadcast +address unless socket_broadcast is called before. + +Normally socket_broadcast returns 0. + +If anything goes wrong, socket_broadcast returns -1, setting errno +appropriately. + +.SH EXAMPLE + #include + + int \fIs\fR; + + if ((\fIs\fR=socket_udp6())==-1) + strerr_die2sys(111,FATAL,"unable to create UDP socket: "); + if (socket_broadcast(\fIs\fR) == -1) + strerr_die2sys(111,FATAL,"unable to set broadcast mode: "); + +.SH "SEE ALSO" +socket_send4(3), socket_send6(3) diff --git a/socket/socket_connect4.3 b/socket/socket_connect4.3 new file mode 100644 index 0000000..c4cea3f --- /dev/null +++ b/socket/socket_connect4.3 @@ -0,0 +1,48 @@ +.TH socket_connect4 3 +.SH NAME +socket_connect4 \- attempt to make a TCP connection +.SH SYNTAX +.B #include + +int \fBsocket_connect4\fP(int \fIs\fR,char \fIip\fR[4],uint16 \fIport\fR); +.SH DESCRIPTION +socket_connect4 attempts to make a connection from TCP socket \fIs\fR to +TCP port \fIport\fR on IP address \fIip\fR. + +socket_connect4 may return +.sp 1 +.IP \(bu +0, to indicate that the connection succeeded (and succeeded immediately, +if the socket is non-blocking) +.IP \(bu +-1, setting errno to error_inprogress or error_wouldblock, to indicate +that the socket is non-blocking +.IP \(bu +-1, setting errno to something else, to indicate that the connection +failed (and failed immediately, if the socket is non-blocking). +.PP + +When a background connection succeeds or fails, \fIs\fR becomes +writable; you can use socket_connected to see whether the connection +succeeded. If the connection failed, socket_connected returns 0, +setting errno appropriately. + +Once a TCP socket is connected, you can use the read and write +system calls to transmit data. + +You can call socket_connect4 without calling socket_bind4. This has the +effect as first calling socket_bind4 with IP address 0.0.0.0 and port 0. + +.SH EXAMPLE + #include + + int \fIs\fR; + char \fIip\fR[4]; + uint16 \fIp\fR; + + \fIs\fR = socket_tcp(); + socket_bind4(s,ip,p); + socket_connect4(s,ip,p); + +.SH "SEE ALSO" +socket_connect6(3) diff --git a/socket/socket_connect6.3 b/socket/socket_connect6.3 new file mode 100644 index 0000000..c375875 --- /dev/null +++ b/socket/socket_connect6.3 @@ -0,0 +1,53 @@ +.TH socket_connect6 3 +.SH NAME +socket_connect6 \- attempt to make a TCP connection +.SH SYNTAX +.B #include + +int \fBsocket_connect6\fP(int \fIs\fR,char \fIip\fR[16],uint16 \fIport\fR,uint32 \fIscope_id\fR); +.SH DESCRIPTION +socket_connect6 attempts to make a connection from TCP socket \fIs\fR to +TCP port \fIport\fR on IP address \fIip\fR. + +The meaning of \fIscope_id\fR is dependent on the implementation and +IPv6 IP. On link-local IPv6 addresses it specifies the outgoing +interface index. The name (e.g. "eth0") for a given interface index can +be queried with getifname. \fIscope_id\fR should normally be set to 0. + +socket_connect6 may return +.sp 1 +.IP \(bu +0, to indicate that the connection succeeded (and succeeded immediately, +if the socket is non-blocking) +.IP \(bu +-1, setting errno to error_inprogress or error_wouldblock, to indicate +that the socket is non-blocking +.IP \(bu +-1, setting errno to something else, to indicate that the connection +failed (and failed immediately, if the socket is non-blocking). +.PP + +When a background connection succeeds or fails, \fIs\fR becomes +writable; you can use socket_connected to see whether the connection +succeeded. If the connection failed, socket_connected returns 0, +setting errno appropriately. + +Once a TCP socket is connected, you can use the read and write +system calls to transmit data. + +You can call socket_connect6 without calling socket_bind6. This has the +effect as first calling socket_bind6 with IP address :: and port 0. + +.SH EXAMPLE + #include + + int \fIs\fR; + char \fIip\fR[16]; + uint16 \fIp\fR; + + \fIs\fR = socket_tcp6(); + socket_bind6(s,ip,p); + socket_connect6(s,ip,p,0); + +.SH "SEE ALSO" +socket_connect4(3), socket_getifname(3) diff --git a/socket/socket_listen.3 b/socket/socket_listen.3 new file mode 100644 index 0000000..545e944 --- /dev/null +++ b/socket/socket_listen.3 @@ -0,0 +1,32 @@ +.TH socket_listen 3 +.SH NAME +socket_listen \- attempt to make a TCP connection +.SH SYNTAX +.B #include + +int \fBsocket_listen\fP(int \fIs\fR,int \fIn\fR); +.SH DESCRIPTION +socket_listen prepares TCP socket \fIs\fR to accept TCP connections. It +allows a backlog of approximately \fIn\fR TCP SYNs. (On systems +supporting SYN cookies, the backlog is irrelevant.) Normally +socket_listen returns 0. + +If anything goes wrong, socket_listen returns -1, setting errno +appropriately. + +.SH EXAMPLE + #include + + int \fIs\fR; + char \fIip\fR[16]; + uint16 \fIp\fR; + + if ((\fIs\fR=socket_tcp6())==-1) + strerr_die2sys(111,FATAL,"unable to create TCP socket: "); + if (socket_bind6_reuse(\fIs\fR,(char *)V6any,8002,0) == -1) + strerr_die2sys(111,FATAL,"unable to bind: "); + if (socket_listen(\fIs\fR,1) == -1) + strerr_die2sys(111,FATAL,"unable to listen: "); + +.SH "SEE ALSO" +socket_connect4(3), socket_connect6(3) diff --git a/socket/socket_local4.3 b/socket/socket_local4.3 new file mode 100644 index 0000000..119f81f --- /dev/null +++ b/socket/socket_local4.3 @@ -0,0 +1,16 @@ +.TH socket_local4 3 +.SH NAME +socket_local4 \- get local IP address of socket +.SH SYNTAX +.B #include + +int \fBsocket_local4\fP(int \fIs\fR,char \fIip\fR[4],uint16 *\fIport\fR); +.SH DESCRIPTION +socket_local4 returns the local IP address and port for the UDP or TCP +socket \fIs\fR and writes the IP address to \fIip\fR and the port to +\fIport\fR. + +If something goes wrong, socket_local4 returns -1, setting errno +appropriately. +.SH "SEE ALSO" +socket_accept4(3), socket_remote4(3) diff --git a/socket/socket_local6.3 b/socket/socket_local6.3 new file mode 100644 index 0000000..4ee5bd6 --- /dev/null +++ b/socket/socket_local6.3 @@ -0,0 +1,19 @@ +.TH socket_local6 3 +.SH NAME +socket_local6 \- get local IP address of socket +.SH SYNTAX +.B #include + +int \fBsocket_local6\fP(int \fIs\fR,char \fIip\fR[16],uint16 *\fIport\fR,uint32 *\fIscope_id\fR); +.SH DESCRIPTION +socket_local6 returns the local IPv6 address, port and scope ID for the +UDP or TCP socket \fIs\fR and writes the IPv6 address to \fIip\fR, the +port to \fIport\fR and the scope ID to \fIscope_id\fR. + +If \fIscope_id\fR is the null pointer, socket_local6 will discard the +scope ID. + +If something goes wrong, socket_local6 returns -1, setting errno +appropriately. +.SH "SEE ALSO" +socket_accept6(3), socket_remote6(3) diff --git a/socket/socket_mcjoin4.3 b/socket/socket_mcjoin4.3 new file mode 100644 index 0000000..1055426 --- /dev/null +++ b/socket/socket_mcjoin4.3 @@ -0,0 +1,19 @@ +.TH socket_mcjoin4 3 +.SH NAME +socket_mcjoin4 \- join a multicast group +.SH SYNTAX +.B #include + +int \fBsocket_mcjoin4\fP(int \fIs\fR,const char \fIgroupip\fR[4],const char \fIinterface\fR[4]); +.SH DESCRIPTION +socket_mcjoin4 joins the IPv4 multicast group \fIgroupip\fR on \fIinterface\fR. + +TODO + +socket \fIs\fR and writes the IP address to \fIip\fR and the port to +\fIport\fR. + +If something goes wrong, socket_local4 returns -1, setting errno +appropriately. +.SH "SEE ALSO" +socket_accept4(3), socket_remote4(3) diff --git a/socket/socket_remote4.3 b/socket/socket_remote4.3 new file mode 100644 index 0000000..d5c3765 --- /dev/null +++ b/socket/socket_remote4.3 @@ -0,0 +1,16 @@ +.TH socket_remote4 3 +.SH NAME +socket_remote4 \- get remote IP address of socket +.SH SYNTAX +.B #include + +int \fBsocket_remote4\fP(int \fIs\fR,char \fIip\fR[4],uint16 *\fIport\fR); +.SH DESCRIPTION +socket_remote4 returns the remote IP address and port for the UDP or TCP +socket \fIs\fR and writes the IP address to \fIip\fR and the port to +\fIport\fR. + +If something goes wrong, socket_remote4 returns -1, setting errno +appropriately. +.SH "SEE ALSO" +socket_accept4(3), socket_local4(3) diff --git a/socket/socket_remote6.3 b/socket/socket_remote6.3 new file mode 100644 index 0000000..2a02a06 --- /dev/null +++ b/socket/socket_remote6.3 @@ -0,0 +1,19 @@ +.TH socket_remote6 3 +.SH NAME +socket_remote6 \- get remote IP address of socket +.SH SYNTAX +.B #include + +int \fBsocket_remote6\fP(int \fIs\fR,char \fIip\fR[16],uint16 *\fIport\fR,uint32 *\fIscope_id\fR); +.SH DESCRIPTION +socket_remote6 returns the remote IPv6 address, port and scope ID for the +UDP or TCP socket \fIs\fR and writes the IPv6 address to \fIip\fR, the +port to \fIport\fR and the scope ID to \fIscope_id\fR. + +If \fIscope_id\fR is the null pointer, socket_remote6 will discard the +scope ID. + +If something goes wrong, socket_remote6 returns -1, setting errno +appropriately. +.SH "SEE ALSO" +socket_accept6(3), socket_local6(3) diff --git a/socket/socket_tcp.3 b/socket/socket_tcp.3 new file mode 100644 index 0000000..fa72b38 --- /dev/null +++ b/socket/socket_tcp.3 @@ -0,0 +1,29 @@ +.TH socket_tcp 3 +.SH NAME +socket_tcp \- create a non-blocking TCP/IP stream socket +.SH SYNTAX +.B #include + +int \fBsocket_tcp\fP(); +.SH DESCRIPTION +socket_tcp creates a non-blocking TCP/IP stream socket and returns a +file descriptor pointing to that socket. If something goes wrong, +socket_tcp returns -1, setting errno appropriately, without allocating +any resources. + +.SH EXAMPLE + #include + + int \fIs\fR; + char \fIip\fR[4]; + uint16 \fIp\fR; + + \fIs\fR = socket_tcp(); + socket_bind4(s,ip,p); + socket_connect4(s,ip,p); + +.SH BUGS +It is called socket_tcp instead of socket_tcp4 to be compatible with DJ +Bernstein's original implementation. +.SH "SEE ALSO" +socket_bind4(3), socket_bind6(3) diff --git a/socket/socket_tcp6.3 b/socket/socket_tcp6.3 new file mode 100644 index 0000000..9f143df --- /dev/null +++ b/socket/socket_tcp6.3 @@ -0,0 +1,26 @@ +.TH socket_tcp6 3 +.SH NAME +socket_tcp \- create a non-blocking IPv6 TCP/IP stream socket +.SH SYNTAX +.B #include + +int \fBsocket_tcp6\fP(); +.SH DESCRIPTION +socket_tcp creates a non-blocking IPv6 TCP/IP stream socket and returns a +file descriptor pointing to that socket. If something goes wrong, +socket_tcp returns -1, setting errno appropriately, without allocating +any resources. + +.SH EXAMPLE + #include + + int \fIs\fR; + char \fIip\fR[16]; + uint16 \fIp\fR; + + \fIs\fR = socket_tcp6(); + socket_bind6(s,ip,p); + socket_connect6(s,ip,p,0); + +.SH "SEE ALSO" +socket_bind4(3), socket_bind6(3)