unix.ffi, windows.winsock: wraps for servent functions
							parent
							
								
									32b07016b0
								
							
						
					
					
						commit
						7c179f876c
					
				| 
						 | 
				
			
			@ -169,3 +169,5 @@ os unix? [
 | 
			
		|||
! Binding to all interfaces should work
 | 
			
		||||
[ ] [ f 0 <inet4> <datagram> dispose ] unit-test
 | 
			
		||||
[ ] [ f 0 <inet6> <datagram> dispose ] unit-test
 | 
			
		||||
 | 
			
		||||
[ 80 ] [ "http" protocol-port ] unit-test
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -464,6 +464,9 @@ M: invalid-local-address summary
 | 
			
		|||
        [ invalid-local-address ] if
 | 
			
		||||
    ] dip with-variable ; inline
 | 
			
		||||
 | 
			
		||||
: protocol-port ( protocol -- port )
 | 
			
		||||
    f getservbyname [ port>> htons ] [ f ] if* ;
 | 
			
		||||
 | 
			
		||||
{
 | 
			
		||||
    { [ os unix? ] [ "io.sockets.unix" require ] }
 | 
			
		||||
    { [ os windows? ] [ "io.sockets.windows" require ] }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
IN: unix.ffi.tests
 | 
			
		||||
USING: accessors alien.c-types tools.test unix.ffi ;
 | 
			
		||||
 | 
			
		||||
[ 80 ] [ "http" f getservbyname port>> ntohs ] unit-test
 | 
			
		||||
 | 
			
		||||
[ "www" ] [
 | 
			
		||||
    0 "http" f getservbyname aliases>> c-string alien-element
 | 
			
		||||
] unit-test
 | 
			
		||||
 | 
			
		||||
[ "http" ] [ 80 htons f getservbyport name>> ] unit-test
 | 
			
		||||
| 
						 | 
				
			
			@ -62,6 +62,12 @@ STRUCT: iovec
 | 
			
		|||
    { iov_base void* }
 | 
			
		||||
    { iov_len size_t } ;
 | 
			
		||||
 | 
			
		||||
STRUCT: servent
 | 
			
		||||
    { name c-string }
 | 
			
		||||
    { aliases void* }
 | 
			
		||||
    { port int }
 | 
			
		||||
    { proto c-string } ;
 | 
			
		||||
 | 
			
		||||
CONSTANT: F_OK 0 ! test for existence of file
 | 
			
		||||
CONSTANT: X_OK 1 ! test for execute or search permission
 | 
			
		||||
CONSTANT: W_OK 2 ! test for write permission
 | 
			
		||||
| 
						 | 
				
			
			@ -121,6 +127,8 @@ FUNCTION: int gethostname ( c-string name, int len ) ;
 | 
			
		|||
FUNCTION: int getsockname ( int socket, sockaddr* address, socklen_t* address_len ) ;
 | 
			
		||||
FUNCTION: int getpeername ( int socket, sockaddr* address, socklen_t* address_len ) ;
 | 
			
		||||
FUNCTION: protoent* getprotobyname ( c-string name ) ;
 | 
			
		||||
FUNCTION: servent* getservbyname ( c-string name, c-string prot ) ;
 | 
			
		||||
FUNCTION: servent* getservbyport ( int port, c-string prot ) ;
 | 
			
		||||
FUNCTION: uid_t getuid ;
 | 
			
		||||
FUNCTION: uint htonl ( uint n ) ;
 | 
			
		||||
FUNCTION: ushort htons ( ushort n ) ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -87,14 +87,6 @@ M: string >url
 | 
			
		|||
 | 
			
		||||
M: pathname >url string>> >url ;
 | 
			
		||||
 | 
			
		||||
: protocol-port ( protocol -- port )
 | 
			
		||||
    {
 | 
			
		||||
        { "http" [ 80 ] }
 | 
			
		||||
        { "https" [ 443 ] }
 | 
			
		||||
        { "ftp" [ 21 ] }
 | 
			
		||||
        [ drop f ]
 | 
			
		||||
    } case ;
 | 
			
		||||
 | 
			
		||||
: relative-url ( url -- url' )
 | 
			
		||||
    clone
 | 
			
		||||
        f >>protocol
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -146,6 +146,10 @@ STRUCT: timeval
 | 
			
		|||
    { sec long }
 | 
			
		||||
    { usec long } ;
 | 
			
		||||
 | 
			
		||||
STRUCT: servent
 | 
			
		||||
    { name c-string }
 | 
			
		||||
    { proto c-string } ;
 | 
			
		||||
 | 
			
		||||
GENERIC: sockaddr>ip ( sockaddr -- string )
 | 
			
		||||
 | 
			
		||||
M: sockaddr-in sockaddr>ip ( sockaddr -- string )
 | 
			
		||||
| 
						 | 
				
			
			@ -187,6 +191,9 @@ FUNCTION: int getpeername ( SOCKET s, sockaddr-in* address, int* addrlen ) ;
 | 
			
		|||
 | 
			
		||||
FUNCTION: protoent* getprotobyname ( c-string name ) ;
 | 
			
		||||
 | 
			
		||||
FUNCTION: servent* getservbyname ( c-string name, c-string prot ) ;
 | 
			
		||||
FUNCTION: servent* getservbyport ( c-string name, c-string prot ) ;
 | 
			
		||||
 | 
			
		||||
TYPEDEF: uint SERVICETYPE
 | 
			
		||||
TYPEDEF: OVERLAPPED WSAOVERLAPPED
 | 
			
		||||
TYPEDEF: WSAOVERLAPPED* LPWSAOVERLAPPED
 | 
			
		||||
| 
						 | 
				
			
			@ -430,7 +437,7 @@ ERROR: winsock-exception n string ;
 | 
			
		|||
 | 
			
		||||
: throw-winsock-error ( -- * )
 | 
			
		||||
    WSAGetLastError (throw-winsock-error) ;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
: winsock-error=0/f ( n/f -- )
 | 
			
		||||
    { 0 f } member? [ throw-winsock-error ] when ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue