« Risposta #3 il: 23 Novembre 2015, 16:41:48 »
...forse con questo codice può andare:
Private Const AF_INET As Integer = 2
Private Const SOCK_DGRAM As Integer = 2
Private Const SIOCGIFADDR As Integer = 35093
Library "libc:6"
' 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
' int ioctl (int __fd, unsigned long int __request, ...)
' Perform the I/O control operation specified by REQUEST on FD.
Private Extern ioctl(__fd As Integer, request As Long, argum As Pointer) As Integer
' int Close (int __fd)
' Close the file descriptor FD.
Private Extern close_C(__fd As Integer) As Integer Exec "close"
Library "/tmp/libso"
' int Inetntoa(struct ifreq ifr)
Private Extern Inetntoa(po As Pointer, i As Integer) As String
Public Sub Main()
Dim fd As Integer
Dim bb As Byte[] = [101, 116, 104, 48] ' eth0
Dim ifreq As Pointer
Dim s As String
Dim st As Stream
fd = socket(AF_INET, SOCK_DGRAM, 0)
' Crea un apposita libreria dinamica condivisa .so esterna per la gestione sicura di alcune Strutture e Funzioni:
Creaso()
ifreq = Alloc(48)
st = Memory ifreq For Write
' Unisce l'indirizzo IP a "eth0":
bb.Write(st, 0, bb.Count)
' Ottiene un indirizzo IP IPv4:
Seek #st, 16
Write #st, AF_INET As Integer
st.Close
ioctl(fd, SIOCGIFADDR, ifreq)
s = Inetntoa(ifreq, fd)
Print s
close_C(fd)
Free(ifreq)
End
Private Procedure Creaso()
' Salva il codice sorgente in C dell'apposita libreria esterna .so:
File.Save("/tmp/libso.c", "#include <arpa/inet.h>\n" &
"#include <net/if.h>\n\n" &
"void Scrive_sa_family(struct ifreq * p, int afnet) {\n" &
" p->ifr_addr.sa_family = AF_INET;\n}" &
"\n\n" &
"char * Inetntoa(struct ifreq * p, int fd) {\n" &
" return inet_ntoa(((struct sockaddr_in *) &p->ifr_addr)->sin_addr);" &
"\n}")
' Genera l'apposita libreria esterna .so:
Shell "gcc -o /tmp/libso.so /tmp/libso.c -shared -fPIC" Wait
End
« Ultima modifica: 24 Novembre 2015, 11:54:19 da vuott »
Registrato
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »