Annotation of gforth/unix/socket.fs, revision 1.34

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.20      anton      50: end-c-library
1.4       pazsan     51: 
                     52: 4 4 2Constant int%
1.8       pazsan     53: 2 2 2Constant short%
1.1       pazsan     54: 
                     55: struct
                     56:     cell% field h_name
                     57:     cell% field h_aliases
1.4       pazsan     58:     int% field h_addrtype
                     59:     int% field h_length
1.1       pazsan     60:     cell% field h_addr_list
                     61: end-struct hostent
                     62: 
                     63: struct
1.8       pazsan     64:     short% field family
                     65:     short% field port
1.4       pazsan     66:     int% field sin_addr
1.1       pazsan     67:     cell% 2* field padding
                     68: end-struct sockaddr_in
                     69: 
1.12      anton      70: ' family alias family+port \ 0.6.2 32-bit field; used by itools
                     71: 
1.1       pazsan     72: Create sockaddr-tmp
                     73: sockaddr-tmp sockaddr_in %size dup allot erase
                     74: 
                     75: : c-string ( addr u -- addr' )
                     76:     tuck pad swap move pad + 0 swap c! pad ;
                     77: 
                     78: : host>addr ( addr u -- x )
                     79:     \G converts a internet name into a IPv4 address
                     80:     \G the resulting address is in network byte order
                     81:     c-string gethostbyname dup 0= abort" address not found"
1.26      pazsan     82: [ s" os-type" environment? drop s" cygwin" str= ] [IF]
1.27      pazsan     83:     &12 +
1.26      pazsan     84: [ELSE]
1.27      pazsan     85:     h_addr_list
1.26      pazsan     86: [THEN]
1.31      pazsan     87:     @ @ l@ ntohl ;
1.1       pazsan     88: 
1.17      pazsan     89:    2 Constant PF_INET
                     90:    1 Constant SOCK_STREAM
1.33      pazsan     91:    2 Constant SOCK_DGRAM
                     92:    1 Constant IPPROTO_ICMP
1.17      pazsan     93:    6 Constant IPPROTO_TCP
1.33      pazsan     94:   17 Constant IPPROTO_UDP
1.17      pazsan     95:    4 Constant F_SETFL
                     96:   11 Constant EWOULDBLOCK
                     97: $100 Constant MSG_WAITALL
                     98: $802 Constant O_NONBLOCK|O_RDWR
1.30      anton      99: 2variable socket-timeout-d 2000. socket-timeout-d 2!
1.1       pazsan    100: 
1.8       pazsan    101: : new-socket ( -- socket )
1.33      pazsan    102:     PF_INET SOCK_STREAM 0 socket
                    103:     dup 0<= abort" no free socket" ;
                    104: 
                    105: : new-udp-socket ( -- socket )
                    106:     PF_INET SOCK_DGRAM 0 socket
1.8       pazsan    107:     dup 0<= abort" no free socket" ;
                    108: 
                    109: : >inetaddr ( ip port sockaddr -- ) >r
                    110:     r@ sockaddr_in %size erase
                    111:     PF_INET r@ family w!
                    112:     htons r@ port w!
                    113:     htonl r> sin_addr l! ;
                    114: 
1.1       pazsan    115: : open-socket ( addr u port -- fid )
1.10      pazsan    116:     -rot host>addr
                    117:     swap sockaddr-tmp >inetaddr
1.8       pazsan    118:     new-socket >r
                    119:     r@ sockaddr-tmp sockaddr_in %size connect 0< abort" can't connect"
1.1       pazsan    120:     r> s" w+" c-string fdopen ;
1.17      pazsan    121: 
1.33      pazsan    122: : open-udp-socket ( addr u port -- fid )
                    123:     -rot host>addr
                    124:     swap sockaddr-tmp >inetaddr
                    125:     new-udp-socket >r
                    126:     r@ sockaddr-tmp sockaddr_in %size connect 0< abort" can't connect"
                    127:     r> s" w+" c-string fdopen ;
                    128: 
1.18      pazsan    129: : create-server  ( port# -- lsocket )
1.33      pazsan    130:     sockaddr-tmp sockaddr_in %size erase
                    131:     PF_INET sockaddr-tmp family w!
                    132:     htons   sockaddr-tmp port w!
                    133:     new-socket
                    134:     dup 0< abort" no free socket" >r
                    135:     r@ sockaddr-tmp 16 bind 0= IF  r> exit  ENDIF
                    136:     r> drop true abort" bind :: failed" ;
                    137: 
                    138: : create-udp-server  ( port# -- lsocket )
                    139:     sockaddr-tmp sockaddr_in %size erase
                    140:     PF_INET sockaddr-tmp family w!
                    141:     htons   sockaddr-tmp port w!
                    142:     new-udp-socket
1.18      pazsan    143:     dup 0< abort" no free socket" >r
                    144:     r@ sockaddr-tmp 16 bind 0= IF  r> exit  ENDIF
                    145:     r> drop true abort" bind :: failed" ;
                    146: 
1.17      pazsan    147: \ from itools.frt
                    148: 
1.18      pazsan    149: ' open-socket Alias open-service
                    150: 
1.17      pazsan    151: : $put ( c-addr1 u1 c-addr2 -- ) swap cmove ;
                    152: 
                    153: : $+   ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
                    154:     { c-addr1 u1 c-addr2 u2 }
                    155:     u1 u2 + allocate throw 
                    156:     c-addr1 u1  2 pick       $put 
                    157:     c-addr2 u2  2 pick u1 +  $put  
                    158:     u1 u2 + ;
                    159: 
1.19      pazsan    160: Create hostname$ 0 c, 255 chars allot
1.18      pazsan    161: Create alen   16 ,
                    162: Create crlf 2 c, 13 c, 10 c,
                    163: 
                    164: : listen ( lsocket /queue -- )
                    165:     listen() 0< abort" listen :: failed" ;
1.17      pazsan    166: 
1.18      pazsan    167: \ This call blocks the server until a client appears. The client uses socket to
                    168: \ converse with the server.
                    169: : accept-socket ( lsocket -- socket )
                    170:     16 alen !
                    171:     sockaddr-tmp alen accept() 
                    172:     dup 0< IF  errno cr ." accept() :: error #" .  
                    173:        abort" accept :: failed"  
                    174:     ENDIF   s" w+" c-string fdopen ;
1.17      pazsan    175: 
                    176: : +cr  ( c-addr1 u1 -- c-addr2 u2 ) crlf count $+ ;
                    177: 
                    178: : blocking-mode ( socket flag -- ) >r fileno
                    179:     f_setfl r> IF  0  
                    180:     ELSE  o_nonblock|o_rdwr  
                    181:     THEN  
                    182:     fcntl 0< abort" blocking-mode failed" ;
                    183: 
1.19      pazsan    184: : hostname ( -- c-addr u )
                    185:     hostname$ c@ 0= IF
                    186:        hostname$ 1+ 255 gethostname drop
                    187:        hostname$ 1+ 255 0 scan nip 255 swap - hostname$ c!
                    188:     THEN
                    189:     hostname$ count ;
1.30      anton     190: : set-socket-timeout ( u -- ) 200 + s>d socket-timeout-d 2! ;
                    191: : get-socket-timeout ( -- u ) socket-timeout-d 2@ drop 200 - ;
1.17      pazsan    192: : write-socket ( c-addr size socket -- ) fileno -rot 0 send 0< throw ;
                    193: : close-socket ( socket -- ) fileno closesocket drop ;
                    194: 
                    195: : (rs)  ( socket c-addr maxlen -- c-addr size ) 
                    196:     2 pick >r r@ false blocking-mode  rot fileno -rot
                    197:     over >r msg_waitall recv
                    198:     dup 0<  IF  0 max
                    199:        errno dup 0<> swap ewouldblock <> and abort" (rs) :: socket read error"
                    200:     THEN
                    201:     r> swap
                    202:     r> true blocking-mode ;
                    203: 
                    204: : read-socket ( socket c-addr maxlen -- c-addr u )
1.30      anton     205:     utime socket-timeout-d 2@ d+ { socket c-addr maxlen d: tmax -- c-addr size }
1.17      pazsan    206:     BEGIN 
                    207:        socket c-addr maxlen (rs) dup 0=
1.30      anton     208:        utime tmax d< and 
1.17      pazsan    209:     WHILE 
                    210:            2drop
                    211:     REPEAT ;
1.33      pazsan    212: 
                    213: : (rs-from)  ( socket c-addr maxlen -- c-addr size ) 
                    214:     2 pick >r  r@ false blocking-mode  rot fileno -rot
                    215:     over >r msg_waitall sockaddr-tmp alen  recvfrom
                    216:     dup 0<  IF  0 max
                    217:        errno dup 0<> swap ewouldblock <> and abort" (rs) :: socket read error"
                    218:     THEN
                    219:     r> swap
                    220:     r> true blocking-mode ;
                    221: 
                    222: : read-socket-from ( socket c-addr maxlen -- c-addr u )
                    223:     utime socket-timeout-d 2@ d+ { socket c-addr maxlen d: tmax -- c-addr size }
                    224:     BEGIN 
                    225:        socket c-addr maxlen (rs-from) dup 0=
                    226:        utime tmax d< and 
                    227:     WHILE 
                    228:            2drop
                    229:     REPEAT ;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>