@@ -0,0 +1,172 @@
1+||| Low-Level C Sockets bindings for Idris. Used by higher-level, cleverer things.
2+||| Types used by Network.Socket.Raw and Network.Socket.
4+||| Original (C) SimonJF, MIT Licensed, 2014
5+||| Modified (C) The Idris Community, 2015, 2016
6+module Network.Socket.Data
10+-- ------------------------------------------------------------ [ Type Aliases ]
20+||| Generally good enough to just set it to 0.
24+||| SocketError: Error thrown by a socket operation
28+||| SocketDescriptor: Native C Socket Descriptor
29+SocketDescriptor : Type
30+SocketDescriptor = Int
35+-- --------------------------------------------------------------- [ Constants ]
37+||| Backlog used within listen() call -- number of incoming calls
45+ unsafePerformIO $ foreign FFI_C "idrnet_geteagain" (() -> IO Int) ()
47+-- -------------------------------------------------------------- [ Interfaces ]
49+interface ToCode a where
52+-- --------------------------------------------------------- [ Socket Families ]
56+||| The ones that people might actually use. We're not going to need US
57+||| Government proprietary ones.
58+data SocketFamily : Type where
60+ AF_UNSPEC : SocketFamily
62+ ||| IP / UDP etc. IPv4
63+ AF_INET : SocketFamily
65+ ||| IP / UDP etc. IPv6
66+ AF_INET6 : SocketFamily
68+Show SocketFamily where
69+ show AF_UNSPEC = "AF_UNSPEC"
70+ show AF_INET = "AF_INET"
71+ show AF_INET6 = "AF_INET6"
73+ToCode SocketFamily where
78+getSocketFamily : Int -> Maybe SocketFamily
80+ Prelude.List.lookup i [ (0, AF_UNSPEC)
85+-- ------------------------------------------------------------ [ Socket Types ]
88+data SocketType : Type where
89+ ||| Not a socket, used in certain operations
90+ NotASocket : SocketType
96+ Datagram : SocketType
99+ RawSocket : SocketType
101+Show SocketType where
102+ show NotASocket = "Not a socket"
103+ show Stream = "Stream"
104+ show Datagram = "Datagram"
105+ show RawSocket = "Raw"
107+ToCode SocketType where
108+ toCode NotASocket = 0
111+ toCode RawSocket = 3
113+-- --------------------------------------------------------------- [ Addresses ]
115+||| Network Addresses
116+data SocketAddress : Type where
117+ IPv4Addr : Int -> Int -> Int -> Int -> SocketAddress
119+ ||| Not implemented (yet)
120+ IPv6Addr : SocketAddress
122+ Hostname : String -> SocketAddress
124+ ||| Used when there's a parse error
125+ InvalidAddress : SocketAddress
127+Show SocketAddress where
128+ show (IPv4Addr i1 i2 i3 i4) = concat $ Prelude.List.intersperse "." (map show [i1, i2, i3, i4])
129+ show IPv6Addr = "NOT IMPLEMENTED YET"
130+ show (Hostname host) = host
131+ show InvalidAddress = "Invalid"
133+||| Parses a textual representation of an IPv4 address into a SocketAddress
134+parseIPv4 : String -> SocketAddress
137+ (i1 :: i2 :: i3 :: i4 :: _) => IPv4Addr i1 i2 i3 i4
138+ otherwise => InvalidAddress
140+ toInt' : String -> Integer
143+ toInt : String -> Int
144+ toInt s = fromInteger $ toInt' s
147+ splitted = map toInt (Prelude.Strings.split (\c => c == '.') str)
149+-- --------------------------------------------------------- [ UDP Information ]
151+-- TODO: Expand to non-string payloads
152+record UDPRecvData where
153+ constructor MkUDPRecvData
154+ remote_addr : SocketAddress
159+record UDPAddrInfo where
160+ constructor MkUDPAddrInfo
161+ remote_addr : SocketAddress
164+-- ----------------------------------------------------------------- [ Sockets ]
165+||| The metadata about a socket
167+ constructor MkSocket
168+ descriptor : SocketDescriptor
169+ family : SocketFamily
170+ socketType : SocketType
171+ protocolNumber : ProtocolNumber