I am trying to send an icmpv6 ping packet across running as root (python 2.7 on Linux)
I understand that sendto uses two tuple struct in case of ipv4 (and it works) and know that ipv6 uses a 4 tuple struct. Still i can't get it to work.
It either results in an "invalid argument" or "socket.gaierror: [Errno -2] Name or service not known"
Following is a bare minimum example showing what i am attempting. I am even fine if i can get it to work with local host in case of ipv6 i.e. ::1
import socket
def main(dest_name):
#dest_addr = socket.gethostbyname(dest_name)
addrs = socket.getaddrinfo(dest_name, 0, socket.AF_INET6, 0, socket.SOL_IP)
print addrs
dest = addrs[2]
port = 33434 # just some random number because of icmp
icmp = socket.getprotobyname('ipv6-icmp')
#print icmp
send_socket = socket.socket(socket.AF_INET6, socket.SOCK_RAW, icmp)
print "sent to " + str(dest[4])
send_socket.sendto('', (str(dest[4]), port))
send_socket.close()
if __name__ == '__main__':
main('ipv6.google.com')
I actually tried each tuple from the addr list, but the result is same.
Update:
Also tried alternates with sendto's params, but it results in invalid arguments whether i use local host or google ipv6 address
send_socket.sendto('', dest[4])
Update 2:
For reference, working ipv4 code follows (as asked in comments)
def main(dest_name):
dest_addr = socket.gethostbyname(dest_name)
icmp = socket.getprotobyname('icmp')
send_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
print "sent to " + dest_name#str(dest[4])
send_socket.sendto('', (dest_addr, 0))
send_socket.close()
if __name__ == '__main__':
main('www.google.com')
Update 3:
When i run the v6 version with dest[4] as the only parameter (no string, just the tuple and NO port), following is output on my machine (Mint 15) which includes printing interfaces
sudo python test_v6.py
[(10, 1, 6, '', ('::1', 0, 0, 0)), (10, 2, 17, '', ('::1', 0, 0, 0)), (10, 3, 0, '', ('::1', 0, 0, 0))]
sent to ('::1', 0, 0, 0)
Traceback (most recent call last):
File "test_v6.py", line 18, in <module>
main('::1')
File "test_v6.py", line 14, in main
send_socket.sendto('', dest[4])
socket.error: [Errno 22] Invalid argument
I am not sure why it still produces invalid argument