Ottenere il nome dell'host corrispondente ad un indirizzo IP e viceversa

Da Gambas-it.org - Wikipedia.
Versione del 20 gen 2024 alle 09:19 di Vuott (Discussione | contributi) (Creata pagina con "=Ottenere il nome (se esistente) dell'host corrispondente ad un indirizzo IP= Library "libc:6" Public Struct in_addr s_addr As Integer End Struct Public Struct host...")

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

Ottenere il nome (se esistente) dell'host corrispondente ad un indirizzo IP

Library "libc:6"

Public Struct in_addr
  s_addr As Integer
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

Private Const AF_INET As Integer = 2

' int inet_aton(const char *cp, struct in_addr *inp)
' Converts the Internet host address cp from the IPv4 numbers-and-dots notation into binary form.
Private Extern inet_aton(cp As String, inp As In_addr) As Integer

' struct hostent *gethostbyaddr (const void *__addr, __socklen_t __len, int __type)
' Returns a structure of type hostent for the given host address addr of length len and address type type.
Private Extern gethostbyaddr(__addr As In_addr, __len As Integer, __type As Integer) As Hostent


Public Sub Main()

 Dim ipstr As String
 Dim ip As New In_addr
 Dim hp As Hostent

' Esempio pratico:
 ipstr = "127.0.0.1"

 If inet_aton(ipstr, ip) < 1 Then Error.Raise("Errore !")

 hp = gethostbyaddr(ip, SizeOf(TypeOf(ip.s_addr)), AF_INET)
 If IsNull(hp) Then Error.Raise("Errore !")
 
 Print "Nome associato all'IP: "; ipstr; " = \e[31m"; String@(hp.h_name)
 
End