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

1.1       pazsan      1: \ socket interface
                      2: 
1.6       anton       3: \ Copyright (C) 1998,2000,2003,2005 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
                      9: \ as published by the Free Software Foundation; either version 2
                     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
                     18: \ along with this program; if not, write to the Free Software
                     19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
                     20: 
                     21: require lib.fs
                     22: [IFUNDEF] libc  library libc libc.so.6  [THEN]
                     23: 
1.4       pazsan     24: libc gethostbyname ptr (ptr) gethostbyname ( name -- hostent )
                     25: libc socket int int int (int) socket ( class type proto -- fd )
                     26: libc connect int ptr int (int) connect ( fd sock size -- err )
                     27: libc fdopen int ptr (ptr) fdopen ( fd fileattr -- file )
                     28: libc htonl int (int) htonl ( x -- x' )
                     29: 
                     30: 4 4 2Constant int%
1.1       pazsan     31: 
                     32: struct
                     33:     cell% field h_name
                     34:     cell% field h_aliases
1.4       pazsan     35:     int% field h_addrtype
                     36:     int% field h_length
1.1       pazsan     37:     cell% field h_addr_list
                     38: end-struct hostent
                     39: 
                     40: struct
1.4       pazsan     41:     int% field family+port
                     42:     int% field sin_addr
1.1       pazsan     43:     cell% 2* field padding
                     44: end-struct sockaddr_in
                     45: 
                     46: Create sockaddr-tmp
                     47: sockaddr-tmp sockaddr_in %size dup allot erase
                     48: 
                     49: : c-string ( addr u -- addr' )
                     50:     tuck pad swap move pad + 0 swap c! pad ;
                     51: 
                     52: : host>addr ( addr u -- x )
                     53:     \G converts a internet name into a IPv4 address
                     54:     \G the resulting address is in network byte order
                     55:     c-string gethostbyname dup 0= abort" address not found"
                     56:     h_addr_list @ @ @ ;
                     57: 
                     58: 2 Constant PF_INET
                     59: 1 Constant SOCK_STREAM
                     60: 6 Constant IPPROTO_TCP
                     61: 
                     62: : open-socket ( addr u port -- fid )
                     63:     htonl PF_INET [ base c@ 0= ] [IF] $10 lshift [THEN]
1.7     ! pazsan     64:     or sockaddr-tmp family+port l!
        !            65:     host>addr sockaddr-tmp sin_addr l!
1.1       pazsan     66:     PF_INET SOCK_STREAM IPPROTO_TCP socket
                     67:     dup 0<= abort" no free socket" >r
                     68:     r@ sockaddr-tmp $10 connect 0< abort" can't connect"
                     69:     r> s" w+" c-string fdopen ;

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