Annotation of gforth/unix/socket.fs, revision 1.25
1.1 pazsan 1: \ socket interface
2:
1.21 anton 3: \ Copyright (C) 1998,2000,2003,2005,2006,2007,2008 Free Software Foundation, Inc.
1.1 pazsan 4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
1.15 anton 9: \ as published by the Free Software Foundation, either version 3
1.1 pazsan 10: \ of the License, or (at your option) any later version.
11:
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16:
17: \ You should have received a copy of the GNU General Public License
1.15 anton 18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1 pazsan 19:
1.20 anton 20: c-library socket
1.16 anton 21: \c #include <netdb.h>
22: c-function gethostbyname gethostbyname a -- a ( name -- hostent )
1.17 pazsan 23: \c #include <unistd.h>
24: c-function gethostname gethostname a n -- n ( c-addr u -- ior )
25: \c #include <errno.h>
1.24 pazsan 26: \c #define get_errno() errno
27: c-function errno get_errno -- n ( -- value )
1.16 anton 28: \c #include <sys/types.h>
29: \c #include <sys/socket.h>
30: c-function socket socket n n n -- n ( class type proto -- fd )
1.17 pazsan 31: c-function closesocket close n -- n ( fd -- ior )
1.16 anton 32: c-function connect connect n a n -- n ( fd sock size -- err )
1.17 pazsan 33: c-function send send n a n n -- n ( socket buffer count flags -- size )
34: c-function recv recv n a n n -- n ( socket buffer count flags -- size )
1.25 ! pazsan 35: c-function listen() listen n n -- n ( socket backlog -- err )
! 36: c-function bind bind n a a -- n ( socket sockaddr socklen --- err )
1.18 pazsan 37: c-function accept() accept n a a -- n ( socket sockaddr addrlen -- fd )
1.16 anton 38: \c #include <stdio.h>
39: c-function fdopen fdopen n a -- a ( fd fileattr -- file )
1.17 pazsan 40: \c #include <fcntl.h>
41: c-function fcntl fcntl n n n -- n ( fd n1 n2 -- ior )
1.16 anton 42: \c #include <arpa/inet.h>
43: c-function htonl htonl n -- n ( x -- x' )
44: c-function htons htons n -- n ( x -- x' )
45: c-function ntohl ntohl n -- n ( x -- x' )
1.23 pazsan 46: \c #define fileno1(file) fileno((FILE*)(file))
47: c-function fileno fileno1 a -- n ( file* -- fd )
1.20 anton 48: end-c-library
1.4 pazsan 49:
50: 4 4 2Constant int%
1.8 pazsan 51: 2 2 2Constant short%
1.1 pazsan 52:
53: struct
54: cell% field h_name
55: cell% field h_aliases
1.4 pazsan 56: int% field h_addrtype
57: int% field h_length
1.1 pazsan 58: cell% field h_addr_list
59: end-struct hostent
60:
61: struct
1.8 pazsan 62: short% field family
63: short% field port
1.4 pazsan 64: int% field sin_addr
1.1 pazsan 65: cell% 2* field padding
66: end-struct sockaddr_in
67:
1.12 anton 68: ' family alias family+port \ 0.6.2 32-bit field; used by itools
69:
1.1 pazsan 70: Create sockaddr-tmp
71: sockaddr-tmp sockaddr_in %size dup allot erase
72:
73: : c-string ( addr u -- addr' )
74: tuck pad swap move pad + 0 swap c! pad ;
75:
76: : host>addr ( addr u -- x )
77: \G converts a internet name into a IPv4 address
78: \G the resulting address is in network byte order
79: c-string gethostbyname dup 0= abort" address not found"
1.10 pazsan 80: h_addr_list @ @ @ ntohl ;
1.1 pazsan 81:
1.17 pazsan 82: 2 Constant PF_INET
83: 1 Constant SOCK_STREAM
84: 6 Constant IPPROTO_TCP
85: 4 Constant F_SETFL
86: 11 Constant EWOULDBLOCK
87: $100 Constant MSG_WAITALL
88: $802 Constant O_NONBLOCK|O_RDWR
89: 2000 Value SOCKET-TIMEOUT
1.1 pazsan 90:
1.8 pazsan 91: : new-socket ( -- socket )
92: PF_INET SOCK_STREAM IPPROTO_TCP socket
93: dup 0<= abort" no free socket" ;
94:
95: : >inetaddr ( ip port sockaddr -- ) >r
96: r@ sockaddr_in %size erase
97: PF_INET r@ family w!
98: htons r@ port w!
99: htonl r> sin_addr l! ;
100:
1.1 pazsan 101: : open-socket ( addr u port -- fid )
1.10 pazsan 102: -rot host>addr
103: swap sockaddr-tmp >inetaddr
1.8 pazsan 104: new-socket >r
105: r@ sockaddr-tmp sockaddr_in %size connect 0< abort" can't connect"
1.1 pazsan 106: r> s" w+" c-string fdopen ;
1.17 pazsan 107:
1.18 pazsan 108: : create-server ( port# -- lsocket )
109: sockaddr-tmp 4 CELLS ERASE
110: htonl PF_INET OR sockaddr-tmp !
111: PF_INET SOCK_STREAM 0 socket
112: dup 0< abort" no free socket" >r
113: r@ sockaddr-tmp 16 bind 0= IF r> exit ENDIF
114: r> drop true abort" bind :: failed" ;
115:
1.17 pazsan 116: \ from itools.frt
117:
1.18 pazsan 118: ' open-socket Alias open-service
119:
1.17 pazsan 120: : ms@ utime 1000 um/mod nip ; ( -- u )
121:
122: : $put ( c-addr1 u1 c-addr2 -- ) swap cmove ;
123:
124: : $+ ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
125: { c-addr1 u1 c-addr2 u2 }
126: u1 u2 + allocate throw
127: c-addr1 u1 2 pick $put
128: c-addr2 u2 2 pick u1 + $put
129: u1 u2 + ;
130:
1.19 pazsan 131: Create hostname$ 0 c, 255 chars allot
1.18 pazsan 132: Create alen 16 ,
133: Create crlf 2 c, 13 c, 10 c,
134:
135: : listen ( lsocket /queue -- )
136: listen() 0< abort" listen :: failed" ;
1.17 pazsan 137:
1.18 pazsan 138: \ This call blocks the server until a client appears. The client uses socket to
139: \ converse with the server.
140: : accept-socket ( lsocket -- socket )
141: 16 alen !
142: sockaddr-tmp alen accept()
143: dup 0< IF errno cr ." accept() :: error #" .
144: abort" accept :: failed"
145: ENDIF s" w+" c-string fdopen ;
1.17 pazsan 146:
147: : +cr ( c-addr1 u1 -- c-addr2 u2 ) crlf count $+ ;
148:
149: : blocking-mode ( socket flag -- ) >r fileno
150: f_setfl r> IF 0
151: ELSE o_nonblock|o_rdwr
152: THEN
153: fcntl 0< abort" blocking-mode failed" ;
154:
1.19 pazsan 155: : hostname ( -- c-addr u )
156: hostname$ c@ 0= IF
157: hostname$ 1+ 255 gethostname drop
158: hostname$ 1+ 255 0 scan nip 255 swap - hostname$ c!
159: THEN
160: hostname$ count ;
1.17 pazsan 161: : set-socket-timeout ( u -- ) 200 + to socket-timeout ;
162: : get-socket-timeout ( -- u ) socket-timeout 200 - ;
163: : write-socket ( c-addr size socket -- ) fileno -rot 0 send 0< throw ;
164: : close-socket ( socket -- ) fileno closesocket drop ;
165:
166: : (rs) ( socket c-addr maxlen -- c-addr size )
167: 2 pick >r r@ false blocking-mode rot fileno -rot
168: over >r msg_waitall recv
169: dup 0< IF 0 max
170: errno dup 0<> swap ewouldblock <> and abort" (rs) :: socket read error"
171: THEN
172: r> swap
173: r> true blocking-mode ;
174:
175: : read-socket ( socket c-addr maxlen -- c-addr u )
176: ms@ socket-timeout + { socket c-addr maxlen tmax -- c-addr size }
177: BEGIN
178: socket c-addr maxlen (rs) dup 0=
179: ms@ tmax u< and
180: WHILE
181: 2drop
182: REPEAT ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>