Ho trovato un breve codice, scritto in C, ma - lanciando il codice C compilato - sembra non funzionare proprio la parte finale dell'invio del ping.
Ho comunque tradotto in Gambas (un lavoraccio !) la parte iniziale, quella che funziona:
Private Const PORT_NO As Integer = 0
Library "libc:6"
Public Struct sockaddr_in
sin_ As Short
sin_port As Short
sin_addr As Integer
sin_zero[8] As Byte
End Struct
Public Struct hostent
h_name As Pointer
h_aliases As Pointer
h_addrtype As Integer
h_length As Integer
h_addr_list As Pointer
End Struct
Public Struct sockaddr
sa_ As Short
sa_data[14] As Byte
End Struct
Private Const IPPROTO_ICMP As Integer = 1
Private Const AF_INET As Integer = 2
Private Const SIGINT As Integer = 2
Private Const SOCK_RAW As Integer = 3
Private Const NI_MAXHOST As Integer = 1025
Private Const NI_NAMEREQD As Integer = 8
' int socket (int __domain, int __type, int __protocol)
' Create a new socket of type TYPE in domain DOMAIN, using protocol PROTOCOL.
Private Extern socket(__domain As Integer, __type As Integer, __protocol As Integer) As Integer
' __sighandler_t signal (int __sig, __sighandler_t __handler)
' Set the handler for the signal SIG to HANDLER.
Private Extern signal(__sig As Integer, __handler As Pointer) As Pointer
' struct hostent *gethostbyname (const char *__name)
' Return entry from host data base for host with NAME.
Private Extern gethostbyname(__name As String) As Hostent
' char *inet_ntoa (struct in_addr __in)
' Convert Internet number in IN to ASCII representation.
Private Extern inet_ntoa(__in As Integer) As Pointer
' in_addr_t inet_addr (const char *__cp)
' Convert Internet host address from numbers-and-dots notation in CP into binary data in network byte order.
Private Extern inet_addr(__cp As Pointer) As Integer
' int getnameinfo (const struct sockaddr *__restrict __sa, socklen_t __salen, char *__restrict __host,
' socklen_t __hostlen, char *__restrict __serv, socklen_t __servlen, int __flags)
' Translate a socket address to a location and service name.
Private Extern getnameinfo(__sa As Sockaddr_in, __salen As Integer, __host As Pointer, __hostlen As Integer, __serv As Pointer, __servlen As Integer, __flags As Integer) As Integer
Public Sub Main()
Dim sockfd, addrlen As Integer
Dim indirizzo As String
Dim ip_addr, intHandler As Pointer
Dim addr_con As New Sockaddr_in
Dim rev As Byte[]
indirizzo = "162.241.217.63"
addrlen = Object.SizeOf(addr_con)
ip_addr = Ricerca_DNS(indirizzo, addr_con)
If ip_addr == 0 Then
Print "Ricerca fallita: impossibile risolvere l'hostname !"
Quit
Endif
rev = Risoluzione_DNS_inversa(ip_addr)
Print "\nTentativo di connessione a '"; indirizzo; "' IP: "; String@(ip_addr)
Print "\nRisoluzione DNS inversa: "; rev.ToString(0, rev.Find(0))
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)
If sockfd < 0 Then
Print "\nFile-descriptor del Socket non ricevuto !"
Quit
Endif
Print "\nFile-descriptor del Socket"; sockfd; " ricevuto !"
signal(SIGINT, intHandler)
Invia_ping(sockfd, addr_con, rev, ip_addr, indirizzo)
End
Private Function Ricerca_DNS(ind As String, con As Sockaddr_in) As Pointer ' Esegue una ricerca DNS
Dim host_entity As Hostent
Dim ip As Pointer
Print "\nRisoluzione DNS..."
host_entity = gethostbyname(ind)
ip = inet_ntoa(Int@(Pointer@(host_entity.h_addr_list))) ' inet_ntoa(*(struct in_addr *) host_entity->h_addr)
con.sin_ = host_entity.h_addrtype
con.sin_port = PORT_NO
con.sin_addr = Long@(Pointer@(host_entity.h_addr_list))
Return ip
End
Private Function Risoluzione_DNS_inversa(ip_addr As Pointer) As Byte[]
Dim temp_addr As New Sockaddr_in
Dim socklen, err As Integer
Dim buf As Byte[]
temp_addr.sin_ = AF_INET
temp_addr.sin_addr = inet_addr(ip_addr)
socklen = Object.sizeof(temp_addr)
buf = New Byte[NI_MAXHOST]
err = getnameinfo(temp_addr, socklen, buf.Data, buf.Count, 0, 0, NI_NAMEREQD)
If err < 0 Then
Print "Impossibile la risoluzione DNS inversa dell'hostname\n"
Return 0
Endif
Return buf
End
Private Procedure Invia_ping()
End