Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

1.0 KB
Raw Blame
{-
©AngelaMos | 2026
IP.hs
-}
{-# LANGUAGE OverloadedStrings #-}

module Aenebris.Net.IP
  ( sockAddrToIPBytes
  ) where

import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS8
import Data.List (intercalate)
import Network.Socket
  ( HostAddress6
  , SockAddr(..)
  , hostAddress6ToTuple
  , hostAddressToTuple
  )
import Numeric (showHex)
import Text.Printf (printf)

ipv4Format :: String
ipv4Format = "%d.%d.%d.%d"

ipv6Separator :: String
ipv6Separator = ":"

unixSocketPrefix :: String
unixSocketPrefix = "unix:"

sockAddrToIPBytes :: SockAddr -> ByteString
sockAddrToIPBytes (SockAddrInet _ ha) =
  let (a, b, c, d) = hostAddressToTuple ha
  in BS8.pack (printf ipv4Format a b c d)
sockAddrToIPBytes (SockAddrInet6 _ _ ha6 _) = renderIPv6 ha6
sockAddrToIPBytes (SockAddrUnix p) = BS8.pack (unixSocketPrefix <> p)

renderIPv6 :: HostAddress6 -> ByteString
renderIPv6 ha =
  let (a, b, c, d, e, f, g, h) = hostAddress6ToTuple ha
      parts = [a, b, c, d, e, f, g, h]
  in BS8.pack (intercalate ipv6Separator (map (`showHex` "") parts))