123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- module ICMPV6
- class ICMPV6Socket
- require 'socket'
- require_relative 'icmpv6_packet.rb'
- def initialize
- @icmp_socket = Socket.new(Socket::PF_INET6, Socket::SOCK_RAW, Socket::IPPROTO_ICMPV6)
- end
- def send(destination, packet)
- @icmp_socket.send(packet, 0, destination)
- response = @icmp_socket.recvfrom(9000)
- return response
- end
- # Poor implementation of the ping command.
- def ping(host, count = 1)
- def create_destination(host)
- return Addrinfo.new(Socket.pack_sockaddr_in(0, host))
- end
- destination = create_destination(host)
- packet = Packet.new(message_type: 128)
- packet.payload.identifier = EchoRequest.random_identifier
- packet.payload.payload = EchoRequest.sample_payload
- successful = 0
- count.times do
- response = send(destination, packet.to_binary_s)
- response_packet = Packet.read(response[0])
- if response[1].ip_address == destination.ip_address &&
- response_packet.payload.payload = packet.payload.payload &&
- response_packet.payload.identifier == packet.payload.identifier &&
- response_packet.payload.sequence == packet.payload.sequence
- successful += 1
- end
- packet.payload.sequence += 1
- packet.payload.sequence = 0 unless packet.payload.sequence < 2**16
- end
- return successful
- end
- def close
- @icmp_socket.close unless @icmp_socket.closed?
- end
- end
- end
|