Annotation of gforth/unix/socket.fs, revision 1.36
1.1 pazsan 1: \ socket interface
2:
1.32 anton 3: \ Copyright (C) 1998,2000,2003,2005,2006,2007,2008,2009 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.33 pazsan 35: c-function recvfrom recvfrom n a n n a a -- n ( socket buffer count flags srcaddr addrlen -- size )
1.34 pazsan 36: c-function sendto sendto n a n n a n -- n ( socket buffer count flags srcaddr addrlen -- size )
1.25 pazsan 37: c-function listen() listen n n -- n ( socket backlog -- err )
1.28 anton 38: c-function bind bind n a n -- n ( socket sockaddr socklen --- err )
1.18 pazsan 39: c-function accept() accept n a a -- n ( socket sockaddr addrlen -- fd )
1.16 anton 40: \c #include <stdio.h>
41: c-function fdopen fdopen n a -- a ( fd fileattr -- file )
1.17 pazsan 42: \c #include <fcntl.h>
43: c-function fcntl fcntl n n n -- n ( fd n1 n2 -- ior )
1.16 anton 44: \c #include <arpa/inet.h>
45: c-function htonl htonl n -- n ( x -- x' )
46: c-function htons htons n -- n ( x -- x' )
47: c-function ntohl ntohl n -- n ( x -- x' )
1.23 pazsan 48: \c #define fileno1(file) fileno((FILE*)(file))
49: c-function fileno fileno1 a -- n ( file* -- fd )
1.35 pazsan 50: \c #include <poll.h>
51: c-function poll poll a n n -- n ( fds nfds timeout -- r )
1.36 ! pazsan 52: \c #include <netdb.h>
! 53: c-function getaddrinfo getaddrinfo a a a a -- n ( node service hints res -- r )
! 54: c-function freeaddrinfo freeaddrinfo a -- void ( res -- )
! 55: c-function gai_strerror gai_strerror n -- a ( errcode -- addr )
1.20 anton 56: end-c-library
1.4 pazsan 57:
58: 4 4 2Constant int%
1.8 pazsan 59: 2 2 2Constant short%
1.36 ! pazsan 60: cell dup 2Constant size_t%
1.1 pazsan 61:
62: struct
63: cell% field h_name
64: cell% field h_aliases
1.4 pazsan 65: int% field h_addrtype
66: int% field h_length
1.1 pazsan 67: cell% field h_addr_list
68: end-struct hostent
69:
70: struct
1.8 pazsan 71: short% field family
72: short% field port
1.4 pazsan 73: int% field sin_addr
1.1 pazsan 74: cell% 2* field padding
75: end-struct sockaddr_in
76:
1.35 pazsan 77: struct
78: int% field fd
79: short% field events
80: short% field revents
81: end-struct pollfd
82:
1.36 ! pazsan 83: struct
! 84: int% field ai_flags
! 85: int% field ai_family
! 86: int% field ai_socktype
! 87: int% field ai_protocol
! 88: size_t% field ai_addrlen
! 89: cell% field ai_addr
! 90: cell% field ai_canonname
! 91: cell% field ai_next
! 92: end-struct addrinfo
! 93:
1.12 anton 94: ' family alias family+port \ 0.6.2 32-bit field; used by itools
95:
1.1 pazsan 96: Create sockaddr-tmp
97: sockaddr-tmp sockaddr_in %size dup allot erase
98:
99: : c-string ( addr u -- addr' )
100: tuck pad swap move pad + 0 swap c! pad ;
101:
102: : host>addr ( addr u -- x )
103: \G converts a internet name into a IPv4 address
104: \G the resulting address is in network byte order
105: c-string gethostbyname dup 0= abort" address not found"
1.26 pazsan 106: [ s" os-type" environment? drop s" cygwin" str= ] [IF]
1.27 pazsan 107: &12 +
1.26 pazsan 108: [ELSE]
1.27 pazsan 109: h_addr_list
1.26 pazsan 110: [THEN]
1.31 pazsan 111: @ @ l@ ntohl ;
1.1 pazsan 112:
1.17 pazsan 113: 2 Constant PF_INET
114: 1 Constant SOCK_STREAM
1.33 pazsan 115: 2 Constant SOCK_DGRAM
116: 1 Constant IPPROTO_ICMP
1.17 pazsan 117: 6 Constant IPPROTO_TCP
1.33 pazsan 118: 17 Constant IPPROTO_UDP
1.17 pazsan 119: 4 Constant F_SETFL
120: 11 Constant EWOULDBLOCK
121: $100 Constant MSG_WAITALL
122: $802 Constant O_NONBLOCK|O_RDWR
1.35 pazsan 123: $001 Constant POLLIN
124: $002 Constant POLLPRI
125: $004 Constant POLLOUT
126:
1.30 anton 127: 2variable socket-timeout-d 2000. socket-timeout-d 2!
1.1 pazsan 128:
1.8 pazsan 129: : new-socket ( -- socket )
1.33 pazsan 130: PF_INET SOCK_STREAM 0 socket
131: dup 0<= abort" no free socket" ;
132:
133: : new-udp-socket ( -- socket )
134: PF_INET SOCK_DGRAM 0 socket
1.8 pazsan 135: dup 0<= abort" no free socket" ;
136:
137: : >inetaddr ( ip port sockaddr -- ) >r
138: r@ sockaddr_in %size erase
139: PF_INET r@ family w!
140: htons r@ port w!
141: htonl r> sin_addr l! ;
142:
1.1 pazsan 143: : open-socket ( addr u port -- fid )
1.10 pazsan 144: -rot host>addr
145: swap sockaddr-tmp >inetaddr
1.8 pazsan 146: new-socket >r
147: r@ sockaddr-tmp sockaddr_in %size connect 0< abort" can't connect"
1.1 pazsan 148: r> s" w+" c-string fdopen ;
1.17 pazsan 149:
1.33 pazsan 150: : open-udp-socket ( addr u port -- fid )
151: -rot host>addr
152: swap sockaddr-tmp >inetaddr
153: new-udp-socket >r
154: r@ sockaddr-tmp sockaddr_in %size connect 0< abort" can't connect"
155: r> s" w+" c-string fdopen ;
156:
1.18 pazsan 157: : create-server ( port# -- lsocket )
1.33 pazsan 158: sockaddr-tmp sockaddr_in %size erase
159: PF_INET sockaddr-tmp family w!
160: htons sockaddr-tmp port w!
161: new-socket
162: dup 0< abort" no free socket" >r
163: r@ sockaddr-tmp 16 bind 0= IF r> exit ENDIF
164: r> drop true abort" bind :: failed" ;
165:
166: : create-udp-server ( port# -- lsocket )
167: sockaddr-tmp sockaddr_in %size erase
168: PF_INET sockaddr-tmp family w!
169: htons sockaddr-tmp port w!
170: new-udp-socket
1.18 pazsan 171: dup 0< abort" no free socket" >r
172: r@ sockaddr-tmp 16 bind 0= IF r> exit ENDIF
173: r> drop true abort" bind :: failed" ;
174:
1.17 pazsan 175: \ from itools.frt
176:
1.18 pazsan 177: ' open-socket Alias open-service
178:
1.17 pazsan 179: : $put ( c-addr1 u1 c-addr2 -- ) swap cmove ;
180:
181: : $+ ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
182: { c-addr1 u1 c-addr2 u2 }
183: u1 u2 + allocate throw
184: c-addr1 u1 2 pick $put
185: c-addr2 u2 2 pick u1 + $put
186: u1 u2 + ;
187:
1.19 pazsan 188: Create hostname$ 0 c, 255 chars allot
1.18 pazsan 189: Create alen 16 ,
190: Create crlf 2 c, 13 c, 10 c,
191:
192: : listen ( lsocket /queue -- )
193: listen() 0< abort" listen :: failed" ;
1.17 pazsan 194:
1.18 pazsan 195: \ This call blocks the server until a client appears. The client uses socket to
196: \ converse with the server.
197: : accept-socket ( lsocket -- socket )
198: 16 alen !
199: sockaddr-tmp alen accept()
200: dup 0< IF errno cr ." accept() :: error #" .
201: abort" accept :: failed"
202: ENDIF s" w+" c-string fdopen ;
1.17 pazsan 203:
204: : +cr ( c-addr1 u1 -- c-addr2 u2 ) crlf count $+ ;
205:
206: : blocking-mode ( socket flag -- ) >r fileno
207: f_setfl r> IF 0
208: ELSE o_nonblock|o_rdwr
209: THEN
210: fcntl 0< abort" blocking-mode failed" ;
211:
1.19 pazsan 212: : hostname ( -- c-addr u )
213: hostname$ c@ 0= IF
214: hostname$ 1+ 255 gethostname drop
215: hostname$ 1+ 255 0 scan nip 255 swap - hostname$ c!
216: THEN
217: hostname$ count ;
1.30 anton 218: : set-socket-timeout ( u -- ) 200 + s>d socket-timeout-d 2! ;
219: : get-socket-timeout ( -- u ) socket-timeout-d 2@ drop 200 - ;
1.17 pazsan 220: : write-socket ( c-addr size socket -- ) fileno -rot 0 send 0< throw ;
221: : close-socket ( socket -- ) fileno closesocket drop ;
222:
223: : (rs) ( socket c-addr maxlen -- c-addr size )
224: 2 pick >r r@ false blocking-mode rot fileno -rot
225: over >r msg_waitall recv
226: dup 0< IF 0 max
227: errno dup 0<> swap ewouldblock <> and abort" (rs) :: socket read error"
228: THEN
229: r> swap
230: r> true blocking-mode ;
231:
232: : read-socket ( socket c-addr maxlen -- c-addr u )
1.30 anton 233: utime socket-timeout-d 2@ d+ { socket c-addr maxlen d: tmax -- c-addr size }
1.17 pazsan 234: BEGIN
235: socket c-addr maxlen (rs) dup 0=
1.30 anton 236: utime tmax d< and
1.17 pazsan 237: WHILE
238: 2drop
239: REPEAT ;
1.33 pazsan 240:
241: : (rs-from) ( socket c-addr maxlen -- c-addr size )
242: 2 pick >r r@ false blocking-mode rot fileno -rot
243: over >r msg_waitall sockaddr-tmp alen recvfrom
244: dup 0< IF 0 max
245: errno dup 0<> swap ewouldblock <> and abort" (rs) :: socket read error"
246: THEN
247: r> swap
248: r> true blocking-mode ;
249:
250: : read-socket-from ( socket c-addr maxlen -- c-addr u )
251: utime socket-timeout-d 2@ d+ { socket c-addr maxlen d: tmax -- c-addr size }
252: BEGIN
253: socket c-addr maxlen (rs-from) dup 0=
254: utime tmax d< and
255: WHILE
256: 2drop
257: REPEAT ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>