|
1 | 1 | {-# LANGUAGE CPP #-} |
2 | 2 | {-# LANGUAGE RecordWildCards #-} |
3 | | -{-# OPTIONS_GHC -fno-warn-orphans #-} |
4 | 3 |
|
5 | 4 | #include "HsNet.h" |
6 | 5 | ##include "HsNetDef.h" |
@@ -447,68 +446,6 @@ unpackBits ((k,v):xs) r |
447 | 446 | | r .&. v /= 0 = k : unpackBits xs (r .&. complement v) |
448 | 447 | | otherwise = unpackBits xs r |
449 | 448 |
|
450 | | ------------------------------------------------------------------------------ |
451 | | --- SockAddr |
452 | | - |
453 | | --- | |
454 | | --- |
455 | | --- >>> SockAddrInet6 80 0 (0,0,0xffff,0x01020304) 0 |
456 | | --- [::ffff:1.2.3.4]:80 |
457 | | -instance Show SockAddr where |
458 | | - showsPrec _ (SockAddrUnix str) = showString str |
459 | | - showsPrec _ (SockAddrInet port ha) |
460 | | - = showHostAddress ha |
461 | | - . showString ":" |
462 | | - . shows port |
463 | | - showsPrec _ (SockAddrInet6 port _ ha6 _) |
464 | | - = showChar '[' |
465 | | - . showHostAddress6 ha6 |
466 | | - . showString "]:" |
467 | | - . shows port |
468 | | - |
469 | | - |
470 | | --- Taken from on the implementation of showIPv4 in Data.IP.Addr |
471 | | -showHostAddress :: HostAddress -> ShowS |
472 | | -showHostAddress ip = |
473 | | - let (u3, u2, u1, u0) = hostAddressToTuple ip in |
474 | | - foldr1 (.) . intersperse (showChar '.') $ map showInt [u3, u2, u1, u0] |
475 | | - |
476 | | -showHostAddress' :: HostAddress -> ShowS |
477 | | -showHostAddress' ip = |
478 | | - let (u3, u2, u1, u0) = hostAddressToTuple' ip in |
479 | | - foldr1 (.) . intersperse (showChar '.') $ map showInt [u3, u2, u1, u0] |
480 | | - |
481 | | --- Taken from showIPv6 in Data.IP.Addr. |
482 | | - |
483 | | --- | Show an IPv6 address in the most appropriate notation, based on recommended |
484 | | --- representation proposed by <http://tools.ietf.org/html/rfc5952 RFC 5952>. |
485 | | --- |
486 | | --- /The implementation is completely compatible with the current implementation |
487 | | --- of the `inet_ntop` function in glibc./ |
488 | | -showHostAddress6 :: HostAddress6 -> ShowS |
489 | | -showHostAddress6 ha6@(a1, a2, a3, a4) |
490 | | - -- IPv4-Mapped IPv6 Address |
491 | | - | a1 == 0 && a2 == 0 && a3 == 0xffff = |
492 | | - showString "::ffff:" . showHostAddress' a4 |
493 | | - -- IPv4-Compatible IPv6 Address (exclude IPRange ::/112) |
494 | | - | a1 == 0 && a2 == 0 && a3 == 0 && a4 >= 0x10000 = |
495 | | - showString "::" . showHostAddress' a4 |
496 | | - -- length of longest run > 1, replace it with "::" |
497 | | - | end - begin > 1 = |
498 | | - showFields prefix . showString "::" . showFields suffix |
499 | | - | otherwise = |
500 | | - showFields fields |
501 | | - where |
502 | | - fields = |
503 | | - let (u7, u6, u5, u4, u3, u2, u1, u0) = hostAddress6ToTuple ha6 in |
504 | | - [u7, u6, u5, u4, u3, u2, u1, u0] |
505 | | - showFields = foldr (.) id . intersperse (showChar ':') . map showHex |
506 | | - prefix = take begin fields -- fields before "::" |
507 | | - suffix = drop end fields -- fields after "::" |
508 | | - begin = end + diff -- the longest run of zeros |
509 | | - (diff, end) = minimum $ |
510 | | - scanl (\c i -> if i == 0 then c - 1 else 0) 0 fields `zip` [0..] |
511 | | - |
512 | 449 | ----------------------------------------------------------------------------- |
513 | 450 |
|
514 | 451 | -- | A utility function to open a socket with `AddrInfo`. |
|
0 commit comments