Annotation of gforth/unix/socket.fs, revision 1.21
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>
26: c-function 'errno __errno_location -- a ( -- addr )
1.16 anton 27: \c #include <sys/types.h>
28: \c #include <sys/socket.h>
29: c-function socket socket n n n -- n ( class type proto -- fd )
1.17 pazsan 30: c-function closesocket close n -- n ( fd -- ior )
1.16 anton 31: c-function connect connect n a n -- n ( fd sock size -- err )
1.17 pazsan 32: c-function send send n a n n -- n ( socket buffer count flags -- size )
33: c-function recv recv n a n n -- n ( socket buffer count flags -- size )
1.18 pazsan 34: c-function listen() n n -- n ( socket backlog -- err )
35: c-function bind n a a -- n ( socket sockaddr socklen --- err )
36: c-function accept() accept n a a -- n ( socket sockaddr addrlen -- fd )
1.16 anton 37: \c #include <stdio.h>
38: c-function fdopen fdopen n a -- a ( fd fileattr -- file )
1.17 pazsan 39: \c #include <fcntl.h>
40: c-function fcntl fcntl n n n -- n ( fd n1 n2 -- ior )
1.16 anton 41: \c #include <arpa/inet.h>
42: c-function htonl htonl n -- n ( x -- x' )
43: c-function htons htons n -- n ( x -- x' )
44: c-function ntohl ntohl n -- n ( x -- x' )
1.17 pazsan 45: c-function fileno fileno a -- n ( file* -- fd )
1.20 anton 46: end-c-library
1.4 pazsan 47:
48: 4 4 2Constant int%
1.8 pazsan 49: 2 2 2Constant short%
1.1 pazsan 50:
51: struct
52: cell% field h_name
53: cell% field h_aliases
1.4 pazsan 54: int% field h_addrtype
55: int% field h_length
1.1 pazsan 56: cell% field h_addr_list
57: end-struct hostent
58:
59: struct
1.8 pazsan 60: short% field family
61: short% field port
1.4 pazsan 62: int% field sin_addr
1.1 pazsan 63: cell% 2* field padding
64: end-struct sockaddr_in
65:
1.12 anton 66: ' family alias family+port \ 0.6.2 32-bit field; used by itools
67:
1.1 pazsan 68: Create sockaddr-tmp
69: sockaddr-tmp sockaddr_in %size dup allot erase
70:
71: : c-string ( addr u -- addr' )
72: tuck pad swap move pad + 0 swap c! pad ;
73:
74: : host>addr ( addr u -- x )
75: \G converts a internet name into a IPv4 address
76: \G the resulting address is in network byte order
77: c-string gethostbyname dup 0= abort" address not found"
1.10 pazsan 78: h_addr_list @ @ @ ntohl ;
1.1 pazsan 79:
1.17 pazsan 80: 2 Constant PF_INET
81: 1 Constant SOCK_STREAM
82: 6 Constant IPPROTO_TCP
83: 4 Constant F_SETFL
84: 11 Constant EWOULDBLOCK
85: $100 Constant MSG_WAITALL
86: $802 Constant O_NONBLOCK|O_RDWR
87: 2000 Value SOCKET-TIMEOUT
1.1 pazsan 88:
1.8 pazsan 89: : new-socket ( -- socket )
90: PF_INET SOCK_STREAM IPPROTO_TCP socket
91: dup 0<= abort" no free socket" ;
92:
93: : >inetaddr ( ip port sockaddr -- ) >r
94: r@ sockaddr_in %size erase
95: PF_INET r@ family w!
96: htons r@ port w!
97: htonl r> sin_addr l! ;
98:
1.1 pazsan 99: : open-socket ( addr u port -- fid )
1.10 pazsan 100: -rot host>addr
101: swap sockaddr-tmp >inetaddr
1.8 pazsan 102: new-socket >r
103: r@ sockaddr-tmp sockaddr_in %size connect 0< abort" can't connect"
1.1 pazsan 104: r> s" w+" c-string fdopen ;
1.17 pazsan 105:
1.18 pazsan 106: : create-server ( port# -- lsocket )
107: sockaddr-tmp 4 CELLS ERASE
108: htonl PF_INET OR sockaddr-tmp !
109: PF_INET SOCK_STREAM 0 socket
110: dup 0< abort" no free socket" >r
111: r@ sockaddr-tmp 16 bind 0= IF r> exit ENDIF
112: r> drop true abort" bind :: failed" ;
113:
1.17 pazsan 114: \ from itools.frt
115:
1.18 pazsan 116: ' open-socket Alias open-service
117:
1.17 pazsan 118: : ms@ utime 1000 um/mod nip ; ( -- u )
119:
120: : $put ( c-addr1 u1 c-addr2 -- ) swap cmove ;
121:
122: : $+ ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
123: { c-addr1 u1 c-addr2 u2 }
124: u1 u2 + allocate throw
125: c-addr1 u1 2 pick $put
126: c-addr2 u2 2 pick u1 + $put
127: u1 u2 + ;
128:
1.19 pazsan 129: Create hostname$ 0 c, 255 chars allot
1.18 pazsan 130: Create alen 16 ,
131: Create crlf 2 c, 13 c, 10 c,
132:
133: : errno ( -- #error ) 'errno @ ;
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>