secureUDP-0.1.1.3: Setups secure (unsorted) UDP packet transfer.

Copyright(c) Francisco Casas Barrientos, 2017
LicenseMIT
Maintainerfranciscojacb@gmail.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

SecureUDP

Contents

Description

The main module contains all the operations needed to create a channel and send or receive packages.

import SecureUDP

import qualified Network.Socket as So
import qualified System.IO as SI

main = do
    -- This is needed so lazyness doens't mess with inner thread communication.
    SI.hSetBuffering SI.stdout SI.NoBuffering
    -- Create the socket:
    sock <- So.socket So.AF_INET So.Datagram So.defaultProtocol
    -- Bind the socket to the address 0.0.0.0 so it can receive packages from everywhere.
    let port = 7272
    let address = So.tupleToHostAddress (0,0,0,0)
    So.bind sock (So.SockAddrInet port address)
    -- Create the configuration for a new channel,
    -- this values should be OK for most purposes:
    let chcfg = ChannelConfig {
            socket = sock,
            resendTimeout = 280000000000, -- 0.28 seconds.
            maxResends = 8,
            allowed = (_ -> return (True)), -- Allow any incomming address.
            maxPacketSize = 500,
            recvRetention = 10
        }
    -- Start the channel with the given configuration:
    channel <- startChannel chcfg

    -- ...

Then you can use the channel to send and receive messages from and to any address, using sendMessages and getReceived.

You can also use getLoss to check which messages that you sent weren't ACKed.

Make sure that you call getReceived and getLoss once in a while, even if you don't use them, so the messages won't accumulate in memory.

Synopsis

Channel configuration

data ChannelConfig Source

Holds the configuration of a channel.

Constructors

ChannelConfig 

Fields

socket :: Socket

The UDP Socket from Network.Socket that the channel will use to send and receive messages.

resendTimeout :: Integer

Picoseconds after a package is re-send if no ACK for it is received.

maxResends :: Int

Times that the same package can be re-sended without ACK after considerating it lost.

allowed :: SockAddr -> IO Bool

Function used to determinate if accept or not incomming packages from the given address.

maxPacketSize :: Int

Max bytes that can be sent on this channel packages, larger packages will throw and exception.

recvRetention :: Integer

Time that a received and delivired package will remain on memory in order to avoid duplicated receptions. The packages will be stored recvRetention *resendTimeout * maxResends picoseconds after reception and after that, will be freed on the next getReceived call.

Channel control

startChannel :: ChannelConfig -> IO ChannelSt Source

Starts a sending and a receiving threads for the protocol, returns a channel that can be used to insert and extract messages.

closeChannel :: ChannelSt -> IO () Source

Terminates a channel, ending its threads and making it unable to send or receive messages.

checkClosed :: ChannelSt -> IO Bool Source

Check if the given channel has been closed.

channelConf :: ChannelSt -> ChannelConfig Source

The configuration that was used to start the given channel.

Channel manipulation

getReceived :: ChannelSt -> IO [(SockAddr, ByteString)] Source

Get the received messages and their sender addresses, then erases them.

It's important that your program calls this once in a while or the packages will remain in memory.

getLoss :: ChannelSt -> IO [(SockAddr, ByteString)] Source

Get the messages that weren't ACKed from the target recipent host and erases them. Useful to detect missing connections.

It's also important that your program calls this once in a while or the packages will remain in memory.

sendMessages :: ChannelSt -> [(SockAddr, ByteString)] -> IO Bool Source

Trought the given channel, send packages to the given addresses and message bytestrings.

Returns False if the channel has being closed, True otherwise.

type ChannelSt = (ChannelConfig, MVar ChannelStatus) Source

Represents the status of a channel, also holds, internally, information about the threads that it uses. After a channel is created, it is used as an argument for the main functions.