UCSPI library in basic working state sans documentation
Getty Ritter
9 years ago
1 | Copyright (c) 2015, Getty Ritter | |
2 | ||
3 | All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions are met: | |
7 | ||
8 | * Redistributions of source code must retain the above copyright | |
9 | notice, this list of conditions and the following disclaimer. | |
10 | ||
11 | * Redistributions in binary form must reproduce the above | |
12 | copyright notice, this list of conditions and the following | |
13 | disclaimer in the documentation and/or other materials provided | |
14 | with the distribution. | |
15 | ||
16 | * Neither the name of Getty Ritter nor the names of other | |
17 | contributors may be used to endorse or promote products derived | |
18 | from this software without specific prior written permission. | |
19 | ||
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
1 | module Network.UCSPI where | |
2 | ||
3 | import Control.Arrow (first) | |
4 | import Data.List (stripPrefix) | |
5 | import System.IO (Handle, hPutStrLn, stderr) | |
6 | import System.Posix.IO (fdToHandle) | |
7 | import System.Posix.Types (Fd(..)) | |
8 | import System.Environment (getEnvironment) | |
9 | ||
10 | data UCSPIOptions = UCSPIOptions | |
11 | { ucspiProtocol :: String | |
12 | , ucspiLocalVars :: [(String,String)] | |
13 | , ucspiRemoteVars :: [(String,String)] | |
14 | } deriving (Eq, Show) | |
15 | ||
16 | gatherOptions :: [(String,String)] -> Maybe UCSPIOptions | |
17 | gatherOptions envs = do | |
18 | proto <- lookup "PROTO" envs | |
19 | let locals = [ (k, v) | let pr = proto ++ "LOCAL" | |
20 | , (Just k, v) <- map (first (stripPrefix pr)) envs | |
21 | ] | |
22 | let remotes = [ (k, v) | let pr = proto ++ "REMOTE" | |
23 | , (Just k, v) <- map (first (stripPrefix pr)) envs | |
24 | ] | |
25 | return $ UCSPIOptions | |
26 | { ucspiProtocol = proto | |
27 | , ucspiLocalVars = locals | |
28 | , ucspiRemoteVars = remotes | |
29 | } | |
30 | ||
31 | ||
32 | ucspiClient :: (UCSPIOptions -> Handle -> Handle -> IO ()) -> IO () | |
33 | ucspiClient client = do | |
34 | opts <- gatherOptions `fmap` getEnvironment | |
35 | case opts of | |
36 | Nothing -> hPutStrLn stderr "Non-conformant UCSPI client: no PROTO supplied" | |
37 | Just opts' -> do | |
38 | rdH <- fdToHandle (Fd 6) | |
39 | wrH <- fdToHandle (Fd 7) | |
40 | client opts' rdH wrH | |
41 | ||
42 | ucspiServer :: (UCSPIOptions -> IO ()) -> IO () | |
43 | ucspiServer server = do | |
44 | opts <- gatherOptions `fmap` getEnvironment | |
45 | case opts of | |
46 | Nothing -> hPutStrLn stderr "Non-conformant UCSPI server: no PROTO supplied" | |
47 | Just opts' -> server opts' |
1 | name: ucspi-hs | |
2 | version: 0.1.0.0 | |
3 | synopsis: A small Haskell interface to writing @ucspi@-conformant applications | |
4 | -- description: | |
5 | license: BSD3 | |
6 | license-file: LICENSE | |
7 | author: Getty Ritter | |
8 | maintainer: gettylefou@gmail.com | |
9 | copyright: © 2015 Getty Ritter | |
10 | category: Network | |
11 | build-type: Simple | |
12 | cabal-version: >=1.10 | |
13 | ||
14 | library | |
15 | exposed-modules: Network.UCSPI | |
16 | build-depends: base >=4.7 && <4.8, unix >=2.7 && <2.8 | |
17 | default-language: Haskell2010 |