From: Björn Lindqvist Date: Mon, 16 Sep 2013 12:45:11 +0000 (+0200) Subject: unix.ffi, windows.winsock: wraps for servent functions X-Git-Tag: 0.97~1115 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=7c179f876c169fdc404428d340d2f4e2aec70501 unix.ffi, windows.winsock: wraps for servent functions --- diff --git a/basis/io/sockets/sockets-tests.factor b/basis/io/sockets/sockets-tests.factor index 685d5a649a..64b6d88a63 100644 --- a/basis/io/sockets/sockets-tests.factor +++ b/basis/io/sockets/sockets-tests.factor @@ -169,3 +169,5 @@ os unix? [ ! Binding to all interfaces should work [ ] [ f 0 dispose ] unit-test [ ] [ f 0 dispose ] unit-test + +[ 80 ] [ "http" protocol-port ] unit-test diff --git a/basis/io/sockets/sockets.factor b/basis/io/sockets/sockets.factor index 596b6bfc22..8afb4388e4 100644 --- a/basis/io/sockets/sockets.factor +++ b/basis/io/sockets/sockets.factor @@ -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 ] } diff --git a/basis/unix/ffi/ffi-tests.factor b/basis/unix/ffi/ffi-tests.factor new file mode 100644 index 0000000000..88164aed6c --- /dev/null +++ b/basis/unix/ffi/ffi-tests.factor @@ -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 diff --git a/basis/unix/ffi/ffi.factor b/basis/unix/ffi/ffi.factor index 512a913b41..d5faa8f6d5 100644 --- a/basis/unix/ffi/ffi.factor +++ b/basis/unix/ffi/ffi.factor @@ -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 ) ; diff --git a/basis/urls/urls.factor b/basis/urls/urls.factor index c36b8b0829..66e6ee01eb 100644 --- a/basis/urls/urls.factor +++ b/basis/urls/urls.factor @@ -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 diff --git a/basis/windows/winsock/winsock.factor b/basis/windows/winsock/winsock.factor index d198bf8019..5049d13de4 100644 --- a/basis/windows/winsock/winsock.factor +++ b/basis/windows/winsock/winsock.factor @@ -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 ;