]> gitweb.factorcode.org Git - factor.git/commitdiff
unix.ffi, windows.winsock: wraps for servent functions
authorBjörn Lindqvist <bjourne@gmail.com>
Mon, 16 Sep 2013 12:45:11 +0000 (14:45 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 17 Sep 2013 00:31:37 +0000 (17:31 -0700)
basis/io/sockets/sockets-tests.factor
basis/io/sockets/sockets.factor
basis/unix/ffi/ffi-tests.factor [new file with mode: 0644]
basis/unix/ffi/ffi.factor
basis/urls/urls.factor
basis/windows/winsock/winsock.factor

index 685d5a649ad4d4ff64b29a152cbb9458c2c60277..64b6d88a63de1d4e1c4258986a8c2f2663db6d1c 100644 (file)
@@ -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
index 596b6bfc2201c029ecfbdd9ba648954042be92a8..8afb4388e4c25042e62a626b9bd001c92721e2ac 100644 (file)
@@ -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 (file)
index 0000000..88164ae
--- /dev/null
@@ -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
index 512a913b41995e8d3fd058072a09f6dc9fd23324..d5faa8f6d5eaf823ec21c59ddc79fa3b10bde5da 100644 (file)
@@ -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 ) ;
index c36b8b08296a43995fe6bfc87b67d7f6a946cd16..66e6ee01eb4ec703af0619412d3363aa431fa126 100644 (file)
@@ -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
index d198bf8019aeb42a383a82766ca2a5be8f35e1ae..5049d13de4f20f3a408717ede136e57b61a1ca36 100644 (file)
@@ -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 ;