Commit 2610c869 authored by Mart Lubbers's avatar Mart Lubbers

Sockets: Remove flags

parent 8960881c
Pipeline #21489 failed with stage
in 3 minutes
......@@ -14,7 +14,7 @@ Start w
(Ok sockfd, w)
#! (merr, sockfd) = connect {sin_port=8124,sin_addr=Just (fromString "127.0.0.1")} sockfd
| isError merr = (liftError merr, w)
#! (merr, sockfd) = recv 128 0 sockfd
#! (merr, sockfd) = recv 128 [] sockfd
| isError merr = (merr, w)
# (Ok msg) = merr
# (merr, w) = close sockfd w
......
......@@ -20,7 +20,7 @@ Start w
= case accept sockfd of
(Error e, sockfd) = (Error e, w)
(Ok (sock, addr), sockfd)
# (merr, sock) = send "Hello world!" 0 sock
# (merr, sock) = send "Hello world!" [] sock
| isError merr = (liftError merr, w)
# (merr, w) = close sock w
| isError merr = (merr, w)
......
......@@ -24,6 +24,12 @@ int main(void)
printf("SOCK_STREAM :== %lu\n", SOCK_STREAM);
printf("SOCK_DGRAM :== %lu\n", SOCK_DGRAM);
printf("MSG_DONTROUTE :== %lu\n", MSG_DONTROUTE);
printf("MSG_OOB :== %lu\n", MSG_OOB);
printf("MSG_PEEK :== %lu\n", MSG_PEEK);
printf("MSG_WAITALL :== %lu\n", MSG_WAITALL);
printf("\nsockaddr_in offsets:\n");
printf("sin_family: %lu\n", offsetof(struct sockaddr_in, sin_family));
printf("sin_port: %lu\n", offsetof(struct sockaddr_in, sin_port));
......@@ -31,9 +37,11 @@ int main(void)
printf("in_addr offsets:\n");
printf("s_addr: %lu\n", offsetof(struct in_addr, s_addr));
#ifdef linux
printf("\nsockaddr_un offsets:\n");
printf("sun_family: %lu\n", offsetof(struct sockaddr_un, sun_family));
printf("sun_path: %lu\n", offsetof(struct sockaddr_un, sun_path));
#endif
printf("\nsockaddr_in6 offsets:\n");
printf("sin6_family: %lu\n",
......@@ -47,5 +55,9 @@ int main(void)
printf("in6_addr offsets:\n");
printf("s6_addr: %lu\n", offsetof(struct in6_addr, s6_addr));
#ifdef _WIN32
printf("sizeof(WSADATA): %lu\n", sizeof(WSADATA));
#endif
return 0;
}
......@@ -7,7 +7,9 @@ from System._Socket import :: Socket
from System.OSError import :: MaybeOSError, :: OSError, :: OSErrorMessage, :: OSErrorCode
:: SocketType = ST_Stream | ST_DGram
instance toInt SocketType
:: SendFlag = SendFlagOob | SendFlagDontRoute
:: RecvFlag = RecvFlagOob | RecvFlagWaitAll | RecvFlagPeek
instance toInt SocketType, SendFlag, RecvFlag
class SocketAddress sa where
sa_length :: !sa -> Int
......@@ -16,16 +18,16 @@ class SocketAddress sa where
sa_domain :: !sa -> Int
sa_null :: sa
/*
* Register a socket with the given type
*
* @param Socket type
* @param Socket protocol
* @param environment
* @return socket
* @return new environment
*/
socket :: !SocketType !Int !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
socket :: !SocketType !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
/*
* Bind a socket to an address
......@@ -84,7 +86,7 @@ connect :: !sa !*(Socket sa) -> *(!MaybeOSError (), !*Socket sa) | SocketAddress
* @return error if something went wrong or the number of bytes sent otherwise
* @return new socket
*/
send :: !String !Int !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
send :: !String ![SendFlag] !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
/*
* Receive data from a socket
......@@ -94,7 +96,7 @@ send :: !String !Int !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
* @return error if something went wrong or the data received otherwise
* @return new socket
*/
recv :: !Int !Int !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
recv :: !Int ![RecvFlag] !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
ntohs :: !Int -> Int
htons :: !Int -> Int
......@@ -8,8 +8,17 @@ instance toInt SocketType where
toInt ST_Stream = SOCK_STREAM
toInt ST_DGram = SOCK_DGRAM
socket :: !SocketType !Int !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
socket a b c = 'System._Socket'.socket a b c
instance toInt SendFlag where
toInt SendFlagOob = MSG_OOB
toInt SendFlagDontRoute = MSG_DONTROUTE
instance toInt RecvFlag where
toInt RecvFlagOob = MSG_OOB
toInt RecvFlagWaitAll = MSG_WAITALL
toInt RecvFlagPeek = MSG_PEEK
socket :: !SocketType !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
socket a b = 'System._Socket'.socket a b
bind :: !sa !*(Socket sa) -> *(!MaybeOSError (), !*Socket sa) | SocketAddress sa
bind a b = 'System._Socket'.bind a b
......@@ -26,10 +35,10 @@ close a b = 'System._Socket'.close a b
connect :: !sa !*(Socket sa) -> *(!MaybeOSError (), !*Socket sa) | SocketAddress sa
connect a b = 'System._Socket'.connect a b
send :: !String !Int !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
send :: !String ![SendFlag] !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
send a b c = 'System._Socket'.send a b c
recv :: !Int !Int !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
recv :: !Int ![RecvFlag] !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
recv a b c = 'System._Socket'.recv a b c
ntohs :: !Int -> Int
......
......@@ -2,7 +2,7 @@ definition module System._Socket
from Data.Error import :: MaybeError
from System.OSError import :: MaybeOSError, :: OSError, :: OSErrorMessage, :: OSErrorCode
from System.Socket import :: SocketType, class SocketAddress
from System.Socket import :: SocketType, class SocketAddress, :: SendFlag, :: RecvFlag
:: *Socket a
......@@ -16,7 +16,12 @@ AF_IRDA :== 23
SOCK_STREAM :== 1
SOCK_DGRAM :== 2
socket :: !SocketType !Int !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
MSG_DONTROUTE :== 4
MSG_OOB :== 1
MSG_PEEK :== 2
MSG_WAITALL :== 256
socket :: !SocketType !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
bind :: !sa !*(Socket sa) -> *(!MaybeOSError (), !*Socket sa) | SocketAddress sa
listen :: !Int !*(Socket sa) -> *(!MaybeOSError (), !*Socket sa) | SocketAddress sa
accept :: !*(Socket sa) -> *(!MaybeOSError (!*Socket sa, !sa), !*Socket sa) | SocketAddress sa
......@@ -24,8 +29,8 @@ close :: !*(Socket sa) !*env -> *(!MaybeOSError (), !*env) | SocketAddress sa
connect :: !sa !*(Socket sa) -> *(!MaybeOSError (), !*Socket sa) | SocketAddress sa
send :: !String !Int !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
recv :: !Int !Int !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
send :: !String ![SendFlag] !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
recv :: !Int ![RecvFlag] !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
ntohs :: !Int -> Int
htons :: !Int -> Int
......@@ -10,8 +10,8 @@ import System.Socket => qualified socket, bind, listen, accept, close, connect,
:: *Socket a :== Int
socket :: !SocketType !Int !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
socket type protocol w
# (sockfd, w) = socket` (sa_domain msa) (toInt type) protocol w
socket type w
# (sockfd, w) = socket` (sa_domain msa) (toInt type) 0 w
# (fd, sockfd) = getFd sockfd
| fd == -1 = getLastOSError w
= (Ok (coerce sockfd msa), w)
......@@ -95,8 +95,9 @@ where
ccall connect "IpI:I:A"
}
send :: !String !Int !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
send :: !String ![SendFlag] !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
send data flags sockfd
# flags = foldr (bitor) 0 (map toInt flags)
# (fd, sockfd) = getFd sockfd
# (r, sockfd) = send` fd (packString data) (size data) flags sockfd
| r == -1 = getLastOSError sockfd
......@@ -107,8 +108,9 @@ where
ccall send "IsII:I:A"
}
recv :: !Int !Int !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
recv :: !Int ![RecvFlag] !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
recv length flags sockfd
# flags = foldr (bitor) 0 (map toInt flags)
# (p, sockfd) = mallocSt length sockfd
| p == 0 = getLastOSError sockfd
# (fd, sockfd) = getFd sockfd
......
......@@ -2,7 +2,7 @@ definition module System._Socket
from Data.Error import :: MaybeError
from System.OSError import :: MaybeOSError, :: OSError, :: OSErrorMessage, :: OSErrorCode
from System.Socket import :: SocketType, class SocketAddress
from System.Socket import :: SocketType, class SocketAddress, :: SendFlag, :: RecvFlag
:: *Socket a
......@@ -20,7 +20,12 @@ SOCK_RAW :== 3
SOCK_RDM :== 4
SOCK_SEQPACKET :== 5
socket :: !SocketType !Int !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
MSG_WAITALL :== 8
MSG_DONTROUTE :== 4
MSG_PEEK :== 2
MSG_OOB :== 1
socket :: !SocketType !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
bind :: !sa !*(Socket sa) -> *(!MaybeOSError (), !*Socket sa) | SocketAddress sa
listen :: !Int !*(Socket sa) -> *(!MaybeOSError (), !*Socket sa) | SocketAddress sa
accept :: !*(Socket sa) -> *(!MaybeOSError (!*Socket sa, !sa), !*Socket sa) | SocketAddress sa
......@@ -28,8 +33,8 @@ close :: !*(Socket sa) !*env -> *(!MaybeOSError (), !*env) | SocketAddress sa
connect :: !sa !*(Socket sa) -> *(!MaybeOSError (), !*Socket sa) | SocketAddress sa
send :: !String !Int !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
recv :: !Int !Int !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
send :: !String ![SendFlag] !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
recv :: !Int ![RecvFlag] !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
ntohs :: !Int -> Int
htons :: !Int -> Int
......@@ -118,14 +118,14 @@ where
ccall WSAGetLastError@0 "P:I:A"
}
socket :: !SocketType !Int !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
socket type protocol w
#! (p, w) = mallocSt 2048 w
socket :: !SocketType !*env -> *(!MaybeOSError *(Socket sa), !*env) | SocketAddress sa
socket type w
#! (p, w) = mallocSt 400 w
| p == 0 = getLastOSError w
#! (r, w) = WSAStartup (2 * 256 + 2) p w
#! w = freeSt p w
| r <> 0 = getLastWSAError "WSAStartup" w
#! (sockfd, w) = socket` (sa_domain msa) (toInt type) protocol w
#! (sockfd, w) = socket` (sa_domain msa) (toInt type) 0 w
#! (fd, sockfd) = getFd sockfd
| fd == -1 = getLastWSAError "socket" w
= (Ok (coerce sockfd msa), w)
......@@ -225,8 +225,9 @@ where
ccall connect@12 "PIpI:I:A"
}
send :: !String !Int !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
send :: !String ![SendFlag] !*(Socket sa) -> *(!MaybeOSError Int, !*Socket sa)
send data flags sockfd
#! flags = foldr (bitor) 0 (map toInt flags)
#! (fd, sockfd) = getFd sockfd
#! (r, sockfd) = send` fd (packString data) (size data) flags sockfd
| r == -1 = getLastWSAError "send" sockfd
......@@ -237,8 +238,9 @@ where
ccall send@16 "PIsII:I:A"
}
recv :: !Int !Int !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
recv :: !Int ![RecvFlag] !*(Socket sa) -> *(!MaybeOSError String, !*Socket sa)
recv length flags sockfd
# flags = foldr (bitor) 0 (map toInt flags)
#! (p, sockfd) = mallocSt length sockfd
| p == 0 = getLastOSError sockfd
#! (fd, sockfd) = getFd sockfd
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment