I'm a n00b, and I've spent some time trying to research this without finding a 
good answer. I'm posting my notes in case they are helpful for someone...

In C one would use 
[getaddrinfo](https://archive.fo/eFztO#selection-589.810-589.883). This is much 
easier in Python, which has a high-level [getsocketinfo 
wrapper](https://hg.python.org/cpython/file/3.5/Modules/socketmodule.c):
    
    
    #!/usr/bin/python3
    
    import socket
    
    host = 'www.kame.net'
    port = 80
    family = socket.AF_INET6
    type = socket.SOCK_STREAM
    addrInfo = socket.getaddrinfo(host, port, family, type)
    print(addrInfo[0][4][0])
    
    
    Run

Nim doesn't seem to have an equivalent portable high-level getaddrinfo.
    
    
    import net, osproc, posix, strutils, nativesockets
    
    proc getIpv6_execCmd(hostname: string): string =
      ## Would only work on UNIX with https://linux.die.net/man/1/host
      const
        lookupCmd = "host -t AAAA "
        expectStr = " has IPv6 address "
      let (output, err) = osproc.execCmdEx(lookupCmd & hostname)
      let expectPos = output.find(expectStr)
      if expectPos < 0: return ""
      let startPos = expectPos + expectStr.len
      let finalPos = output.len - 2
      if finalPos <= startPos: return ""
      return output[startPos..finalPos]
    
    proc getIpv6_dial(hostname: string): string =
      ## Don't know why this doesn't work...  {{{Error: unhandled exception:
      ## Additional info: "Servname not supported for ai_socktype" [OSError]}}}
      let s = net.dial(hostname, 80.Port, Protocol.IPPROTO_IPV6)
      return s.getLocalAddr[0]
    
    proc getIpv6_socket(hostname: string): string =
      ## Also doesn't work... {{{Protocol not supported}}}
      let sock = newSocket(Domain.AF_INET6, SockType.SOCK_STREAM,
          Protocol.IPPROTO_IPV6)
      return sock.getLocalAddr[0]
    
    proc getIpv6_getaddressinfo(hostname: string): string =
      ## Don't have time to figure this out tonight...
      const fam = Domain.AF_INET6
      var addrInfo = getAddrInfo(hostname, 80.Port, fam)
      echo addrInfo.ai_addr.sa_data.addr
      result = ""
      while true:
        var outBuf = newString(64)
        let dataAddr = addr addrInfo.ai_addr.sa_data
        let r = inet_ntop(fam.cint, dataAddr, addr outBuf[0], 65'i32)
        result &= outBuf
        if addrInfo.ai_next.isNil: break
        addrInfo = addrInfo.ai_next
      freeAddrInfo addrInfo
    
    echo getIpv6_getaddressinfo("www.kame.net")
    
    
    
    Run

Reply via email to