@@ -4,205 +4,14 @@
4||| Modified (C) The Idris Community, 2015, 2016
4||| Modified (C) The Idris Community, 2015, 2016
5module Network.Socket
5module Network.Socket
7+import public Network.Socket.Types
8+import Network.Socket.Raw
7%include C "idris_net.h"
10%include C "idris_net.h"
8%include C "sys/types.h"
11%include C "sys/types.h"
9%include C "sys/socket.h"
12%include C "sys/socket.h"
10%include C "netdb.h"
13%include C "netdb.h"
12−-- ------------------------------------------------------------ [ Type Aliases ]
24−||| Generally good enough to just set it to 0.
29−||| SocketError: Error thrown by a socket operation
34−||| SocketDescriptor: Native C Socket Descriptor
36−SocketDescriptor : Type
37−SocketDescriptor = Int
43−-- --------------------------------------------------------------- [ Constants ]
45−||| Backlog used within listen() call -- number of incoming calls
50−-- FIXME: This *must* be pulled in from C
55−-- -------------------------------------------------------------- [ Interfaces ]
58−interface ToCode a where
61−-- --------------------------------------------------------- [ Socket Families ]
65−||| The ones that people might actually use. We're not going to need US
66−||| Government proprietary ones.
68−data SocketFamily : Type where
70− AF_UNSPEC : SocketFamily
72− ||| IP / UDP etc. IPv4
73− AF_INET : SocketFamily
75− ||| IP / UDP etc. IPv6
76− AF_INET6 : SocketFamily
79−Show SocketFamily where
80− show AF_UNSPEC = "AF_UNSPEC"
81− show AF_INET = "AF_INET"
82− show AF_INET6 = "AF_INET6"
85−ToCode SocketFamily where
90−getSocketFamily : Int -> Maybe SocketFamily
92− Prelude.List.lookup i [ (0, AF_UNSPEC)
97−-- ------------------------------------------------------------ [ Socket Types ]
100−data SocketType : Type where
101− ||| Not a socket, used in certain operations
102− NotASocket : SocketType
108− Datagram : SocketType
111− RawSocket : SocketType
114−Show SocketType where
115− show NotASocket = "Not a socket"
116− show Stream = "Stream"
117− show Datagram = "Datagram"
118− show RawSocket = "Raw"
121−ToCode SocketType where
122− toCode NotASocket = 0
125− toCode RawSocket = 3
127−-- ---------------------------------------------------------------- [ Pointers ]
129−data RecvStructPtr = RSPtr Ptr
130−data RecvfromStructPtr = RFPtr Ptr
133−data BufPtr = BPtr Ptr
136−data SockaddrPtr = SAPtr Ptr
138−-- --------------------------------------------------------------- [ Addresses ]
140−||| Network Addresses
142−data SocketAddress : Type where
143− IPv4Addr : Int -> Int -> Int -> Int -> SocketAddress
145− ||| Not implemented (yet)
146− IPv6Addr : SocketAddress
148− Hostname : String -> SocketAddress
150− ||| Used when there's a parse error
151− InvalidAddress : SocketAddress
154−Show SocketAddress where
155− show (IPv4Addr i1 i2 i3 i4) = concat $ Prelude.List.intersperse "." (map show [i1, i2, i3, i4])
156− show IPv6Addr = "NOT IMPLEMENTED YET"
157− show (Hostname host) = host
158− show InvalidAddress = "Invalid"
160−-- --------------------------------------------------------- [ UDP Information ]
162−-- TODO: Expand to non-string payloads
164−record UDPRecvData where
165− constructor MkUDPRecvData
166− remote_addr : SocketAddress
172−record UDPAddrInfo where
173− constructor MkUDPAddrInfo
174− remote_addr : SocketAddress
177−-- ---------------------------------------------------------- [ Socket Utilies ]
179−||| Frees a given pointer
181−sock_free : BufPtr -> IO ()
182−sock_free (BPtr ptr) = foreign FFI_C "idrnet_free" (Ptr -> IO ()) ptr
185−sockaddr_free : SockaddrPtr -> IO ()
186−sockaddr_free (SAPtr ptr) = foreign FFI_C "idrnet_free" (Ptr -> IO ()) ptr
188−||| Allocates an amount of memory given by the ByteLength parameter.
190−||| Used to allocate a mutable pointer to be given to the Recv functions.
192−sock_alloc : ByteLength -> IO BufPtr
193−sock_alloc bl = map BPtr $ foreign FFI_C "idrnet_malloc" (Int -> IO Ptr) bl
195−-- ----------------------------------------------------------------- [ Sockets ]
196−||| The metadata about a socket
199− constructor MkSocket
200− descriptor : SocketDescriptor
201− family : SocketFamily
202− socketType : SocketType
203− protocolNumber : ProtocolNumber
206-- ----------------------------------------------------- [ Network Socket API. ]
15-- ----------------------------------------------------- [ Network Socket API. ]
208||| Creates a UNIX socket with the given family, socket type and protocol
17||| Creates a UNIX socket with the given family, socket type and protocol