Question

My python script uses getaddrinfo() to parse an address before it can 'bind()' to it.

Snippet of the script:

def fetch_ipv6_address(addr="::1"):
    # try to detect whether IPv6 is supported at the present system and
    # fetch the IPv6 address of localhost.
    if not socket.has_ipv6:
        raise Exception("the local machine has no IPv6 support enabled")

    addrs = socket.getaddrinfo(addr, 0, socket.AF_INET6, socket.SOCK_RAW, 0x73, socket.AI_PASSIVE)
    ....
    ....

sockaddr = fetch_ipv6_address("::1")
RX = socket.socket(socket.AF_INET6, socket.SOCK_RAW, 0x73)
RX.bind(sockaddr)

The script throws an error when executed:

    # ./ip6_l2tp_ip.py
Traceback (most recent call last):
  File "./ip6_l2tp_ip.py", line 36, in <module>
    sockaddr = fetch_ipv6_address("::1")
  File "./ip6_l2tp_ip.py", line 26, in fetch_ipv6_address
    addrs = socket.getaddrinfo(addr, 0, socket.AF_INET6, socket.SOCK_RAW, 0x73, socket.AI_PASSIVE)
socket.gaierror: [Errno -8] Servname not supported for ai_socktype

Any idea on what is wrong with the getaddrinfo() args?

Thanks!

Was it helpful?

Solution

The 0 as 2nd argument is converted to a string if it is a long or an int, so that it fits to the format the underlying API call supports for the ai_servname field.

OTOH, the docs write that

o   For internet address families, if you specify servname while you set
    ai_socktype to SOCK_RAW, getaddrinfo() will raise an error, because
    service names are not defined for the internet SOCK_RAW space.

If you replace that 0 with None, it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top