File:  [gforth] / gforth / unix / socket.fs
Revision 1.2: download - view: text, annotated - select for diffs
Sun Mar 9 15:17:08 2003 UTC (18 years, 6 months ago) by anton
Branches: MAIN
CVS tags: v0-6-1, v0-6-0, HEAD
updated copyright years

    1: \ socket interface
    2: 
    3: \ Copyright (C) 1998,2000 Free Software Foundation, Inc.
    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: 
   24: 1 (int) libc gethostbyname gethostbyname ( name -- hostent )
   25: 3 (int) libc socket socket ( class type proto -- fd )
   26: 3 (int) libc connect connect ( fd sock size -- err )
   27: 2 (int) libc fdopen fdopen ( fd fileattr -- file )
   28: 1 (int) libc htonl htonl ( x -- x' )
   29: 
   30: struct
   31:     cell% field h_name
   32:     cell% field h_aliases
   33:     cell% field h_addrtype
   34:     cell% field h_length
   35:     cell% field h_addr_list
   36: end-struct hostent
   37: 
   38: struct
   39:     cell% field family+port
   40:     cell% field sin_addr
   41:     cell% 2* field padding
   42: end-struct sockaddr_in
   43: 
   44: Create sockaddr-tmp
   45: sockaddr-tmp sockaddr_in %size dup allot erase
   46: 
   47: : c-string ( addr u -- addr' )
   48:     tuck pad swap move pad + 0 swap c! pad ;
   49: 
   50: : host>addr ( addr u -- x )
   51:     \G converts a internet name into a IPv4 address
   52:     \G the resulting address is in network byte order
   53:     c-string gethostbyname dup 0= abort" address not found"
   54:     h_addr_list @ @ @ ;
   55: 
   56: 2 Constant PF_INET
   57: 1 Constant SOCK_STREAM
   58: 6 Constant IPPROTO_TCP
   59: 
   60: : open-socket ( addr u port -- fid )
   61:     htonl PF_INET [ base c@ 0= ] [IF] $10 lshift [THEN]
   62:     or sockaddr-tmp family+port !
   63:     host>addr sockaddr-tmp sin_addr !
   64:     PF_INET SOCK_STREAM IPPROTO_TCP socket
   65:     dup 0<= abort" no free socket" >r
   66:     r@ sockaddr-tmp $10 connect 0< abort" can't connect"
   67:     r> s" w+" c-string fdopen ;

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