@@ -4,8 +4,8 @@
44module IdrisNet.Socket
55
66%include C "idris_net.h"
7−%include C "sys/types.h"
8−%include C "sys/socket.h"
7+%include C "sys/types.h"
8+%include C "sys/socket.h"
99%include C "netdb.h"
1010
1111%access public
@@ -16,12 +16,17 @@ ByteLength = Int
1616class ToCode a where
1717 toCode : a -> Int
1818
19−-- Socket Families.
20−-- The ones that people might actually use. We're not going to need US Government
21−-- proprietary ones...
22−data SocketFamily = AF_UNSPEC -- Unspecified
23− | AF_INET -- IP / UDP etc. IPv4
24− | AF_INET6 -- IP / UDP etc. IPv6
19+||| Socket Families
20+|||
21+||| The ones that people might actually use. We're not going to need US
22+||| Government proprietary ones.
23+data SocketFamily =
24+ ||| Unspecified
25+ AF_UNSPEC |
26+ ||| IP / UDP etc. IPv4
27+ AF_INET |
28+ ||| IP / UDP etc. IPv6
29+ AF_INET6
2530
2631instance Show SocketFamily where
2732 show AF_UNSPEC = "AF_UNSPEC"
@@ -36,11 +41,16 @@ instance ToCode SocketFamily where
3641getSocketFamily : Int -> Maybe SocketFamily
3742getSocketFamily i = Prelude.List.lookup i [(0, AF_UNSPEC), (2, AF_INET), (10, AF_INET6)]
3843
39−-- Socket Types.
40−data SocketType = NotASocket -- Not a socket, used in certain operations
41− | Stream -- TCP
42− | Datagram -- UDP
43− | RawSocket -- Raw sockets. A guy can dream.
44+||| Socket Types.
45+data SocketType =
46+ ||| Not a socket, used in certain operations
47+ NotASocket |
48+ ||| TCP
49+ Stream |
50+ ||| UDP
51+ Datagram |
52+ ||| Raw sockets
53+ RawSocket
4454
4555instance Show SocketType where
4656 show NotASocket = "Not a socket"
@@ -60,15 +70,17 @@ data RecvfromStructPtr = RFPtr Ptr
6070data BufPtr = BPtr Ptr
6171data SockaddrPtr = SAPtr Ptr
6272
63−-- Protocol Number. Generally good enough to just set it to 0.
73+||| Protocol Number.
74+|||
75+||| Generally good enough to just set it to 0.
6476ProtocolNumber : Type
6577ProtocolNumber = Int
6678
67−-- SocketError: Error thrown by a socket operation
79+||| SocketError: Error thrown by a socket operation
6880SocketError : Type
6981SocketError = Int
7082
71−-- SocketDescriptor: Native C Socket Descriptor
83+||| SocketDescriptor: Native C Socket Descriptor
7284SocketDescriptor : Type
7385SocketDescriptor = Int
7486
@@ -86,7 +98,7 @@ instance Show SocketAddress where
8698Port : Type
8799Port = Int
88100
89−-- Backlog used within listen() call -- number of incoming calls
101+||| Backlog used within listen() call -- number of incoming calls
90102BACKLOG : Int
91103BACKLOG = 20
92104
@@ -109,7 +121,7 @@ record UDPAddrInfo : Type where
109121 (remote_port : Port) ->
110122 UDPAddrInfo
111123
112−-- Frees a given pointer
124+||| Frees a given pointer
113125public
114126sock_free : BufPtr -> IO ()
115127sock_free (BPtr ptr) = mkForeign (FFun "idrnet_free" [FPtr] FUnit) ptr
@@ -118,12 +130,14 @@ public
118130sockaddr_free : SockaddrPtr -> IO ()
119131sockaddr_free (SAPtr ptr) = mkForeign (FFun "idrnet_free" [FPtr] FUnit) ptr
120132
121−-- Allocates an amount of memory given by the ByteLength parameter.
122−-- Used to allocate a mutable pointer to be given to the Recv functions.
133+||| Allocates an amount of memory given by the ByteLength parameter.
134+|||
135+||| Used to allocate a mutable pointer to be given to the Recv functions.
123136public
124137sock_alloc : ByteLength -> IO BufPtr
125138sock_alloc bl = map BPtr $ mkForeign (FFun "idrnet_malloc" [FInt] FPtr) bl
126139
140+||| The metadata about a socket
127141record Socket : Type where
128142 MkSocket : (descriptor : SocketDescriptor) ->
129143 (family : SocketFamily) ->
@@ -131,20 +145,21 @@ record Socket : Type where
131145 (protocolNumber : ProtocolNumber) ->
132146 Socket
133147
148+||| Get the C error number
134149getErrno : IO Int
135150getErrno = mkForeign (FFun "idrnet_errno" [] FInt)
136151
137−-- Creates a UNIX socket with the given family, socket type and protocol number.
138−-- Returns either a socket or an error.
152+||| Creates a UNIX socket with the given family, socket type and protocol
153+||| number. Returns either a socket or an error.
139154socket : SocketFamily -> SocketType -> ProtocolNumber -> IO (Either SocketError Socket)
140155socket sf st pn = do
141156 socket_res <- mkForeign (FFun "socket" [FInt, FInt, FInt] FInt) (toCode sf) (toCode st) pn
142157 if socket_res == -1 then -- error
143158 map Left getErrno
144− else
159+ else
145160 return $ Right (MkSocket socket_res sf st pn)
146161
147−
162+||| Close a socket
148163close : Socket -> IO ()
149164close sock = mkForeign (FFun "close" [FInt] FUnit) (descriptor sock)
150165
@@ -153,18 +168,18 @@ saString : (Maybe SocketAddress) -> String
153168saString (Just sa) = show sa
154169saString Nothing = ""
155170
156−-- Binds a socket to the given socket address and port.
157−-- Returns 0 on success, an error code otherwise.
171+||| Binds a socket to the given socket address and port.
172+||| Returns 0 on success, an error code otherwise.
158173bind : Socket -> (Maybe SocketAddress) -> Port -> IO Int
159174bind sock addr port = do
160− bind_res <- (mkForeign (FFun "idrnet_bind" [FInt, FInt, FInt, FString, FInt] FInt)
175+ bind_res <- (mkForeign (FFun "idrnet_bind" [FInt, FInt, FInt, FString, FInt] FInt)
161176 (descriptor sock) (toCode $ family sock) (toCode $ socketType sock) (saString addr) port)
162177 if bind_res == (-1) then -- error
163178 getErrno
164179 else return 0 -- Success
165180
166−-- Connects to a given address and port.
167−-- Returns 0 on success, and an error number on error.
181+||| Connects to a given address and port.
182+||| Returns 0 on success, and an error number on error.
168183connect : Socket -> SocketAddress -> Port -> IO Int
169184connect sock addr port = do
170185 conn_res <- (mkForeign (FFun "idrnet_connect" [FInt, FInt, FInt, FString, FInt] FInt)
@@ -173,7 +188,7 @@ connect sock addr port = do
173188 getErrno
174189 else return 0
175190
176−-- Listens on a bound socket.
191+||| Listens on a bound socket.
177192listen : Socket -> IO Int
178193listen sock = do
179194 listen_res <- mkForeign (FFun "listen" [FInt, FInt] FInt) (descriptor sock) BACKLOG
@@ -181,7 +196,7 @@ listen sock = do
181196 getErrno
182197 else return 0
183198
184−-- Parses a textual representation of an IPv4 address into a SocketAddress
199+||| Parses a textual representation of an IPv4 address into a SocketAddress
185200parseIPv4 : String -> SocketAddress
186201parseIPv4 str = case splitted of
187202 (i1 :: i2 :: i3 :: i4 :: _) => IPv4Addr i1 i2 i3 i4
@@ -194,7 +209,7 @@ parseIPv4 str = case splitted of
194209 splitted = map toInt (Prelude.Strings.split (\c => c == '.') str)
195210
196211
197−-- Retrieves a socket address from a sockaddr pointer
212+||| Retrieves a socket address from a sockaddr pointer
198213getSockAddr : SockaddrPtr -> IO SocketAddress
199214getSockAddr (SAPtr ptr) = do
200215 addr_family_int <- mkForeign (FFun "idrnet_sockaddr_family" [FPtr] FInt) ptr
@@ -256,13 +271,13 @@ recv sock len = do
256271 return $ Right (payload, recv_res)
257272
258273
259−-- Sends the data in a given memory location
274+||| Sends the data in a given memory location
260275sendBuf : Socket -> BufPtr -> ByteLength -> IO (Either SocketError ByteLength)
261276sendBuf sock (BPtr ptr) len = do
262277 send_res <- mkForeign (FFun "idrnet_send_buf" [FInt, FPtr, FInt] FInt) (descriptor sock) ptr len
263278 if send_res == (-1) then
264279 map Left getErrno
265− else
280+ else
266281 return $ Right send_res
267282
268283recvBuf : Socket -> BufPtr -> ByteLength -> IO (Either SocketError ByteLength)
@@ -313,7 +328,7 @@ recvFrom sock bl = do
313328 recv_ptr <- mkForeign (FFun "idrnet_recvfrom" [FInt, FInt] FPtr)
314329 (descriptor sock) bl
315330 let recv_ptr' = RFPtr recv_ptr
316− if !(nullPtr recv_ptr) then -- ! notation = monadic bind shortcut, not "not"
331+ if !(nullPtr recv_ptr) then
317332 map Left getErrno
318333 else do
319334 result <- mkForeign (FFun "idrnet_get_recvfrom_res" [FPtr] FInt) recv_ptr
@@ -332,7 +347,7 @@ recvFromBuf : Socket -> BufPtr -> ByteLength -> IO (Either SocketError (UDPAddrI
332347recvFromBuf sock (BPtr ptr) bl = do
333348 recv_ptr <- mkForeign (FFun "idrnet_recvfrom_buf" [FInt, FPtr, FInt] FPtr) (descriptor sock) ptr bl
334349 let recv_ptr' = RFPtr recv_ptr
335− if !(nullPtr recv_ptr) then -- ! notation = monadic bind shortcut, not "not"
350+ if !(nullPtr recv_ptr) then
336351 map Left getErrno
337352 else do
338353 result <- mkForeign (FFun "idrnet_get_recvfrom_res" [FPtr] FInt) recv_ptr
@@ -345,5 +360,4 @@ recvFromBuf sock (BPtr ptr) bl = do
345360 freeRecvfromStruct recv_ptr'
346361 return $ Right (MkUDPAddrInfo addr port, result + 1)
347362
348−
349363