Differenze tra le versioni di "Ottenere il proprio indirizzo IP locale da una rete"
Da Gambas-it.org - Wikipedia.
Riga 34: | Riga 34: | ||
Dim st As Stream | Dim st As Stream | ||
Dim s As String | Dim s As String | ||
− | + | ||
− | + | fd = socket(AF_INET, SOCK_DGRAM, 0) | |
<FONT Color=gray>' ''Crea un apposita libreria dinamica condivisa .so esterna per la gestione sicura di alcune Strutture e Funzioni:''</font> | <FONT Color=gray>' ''Crea un apposita libreria dinamica condivisa .so esterna per la gestione sicura di alcune Strutture e Funzioni:''</font> | ||
− | + | Creaso() | |
− | + | ifreq = Alloc(SizeOf(gb.Byte), 40) | |
− | + | st = Memory ifreq For Write | |
<FONT Color=gray>' ''Unisce l'indirizzo IP a "eth0":''</font> | <FONT Color=gray>' ''Unisce l'indirizzo IP a "eth0":''</font> | ||
− | + | bb.Write(st, 0, bb.Count) | |
<FONT Color=gray>' ''Ottiene un indirizzo IP IPv4:''</font> | <FONT Color=gray>' ''Ottiene un indirizzo IP IPv4:''</font> | ||
− | + | Seek #st, 16 | |
− | + | Write #st, AF_INET As Integer | |
− | + | st.Close | |
− | + | ioctl(fd, SIOCGIFADDR, ifreq) | |
− | + | s = Inetntoa(ifreq, fd) | |
− | + | Print s | |
− | + | Free(ifreq) | |
− | + | close_C(fd) | |
'''End''' | '''End''' | ||
Riga 65: | Riga 65: | ||
<FONT Color=gray>' ''Salva il codice sorgente in C dell'apposita libreria esterna .so:''</font> | <FONT Color=gray>' ''Salva il codice sorgente in C dell'apposita libreria esterna .so:''</font> | ||
− | + | File.Save("/tmp/libso.c", "#include <arpa/inet.h>\n" & | |
− | + | "#include <net/if.h>\n\n" & | |
− | + | "char * Inetntoa(struct ifreq * p, int fd) {\n" & | |
− | + | " return inet_ntoa(((struct sockaddr_in *) &p->ifr_addr)->sin_addr);" & | |
− | + | "\n}") | |
<FONT Color=gray>' ''Genera l'apposita libreria esterna .so:''</font> | <FONT Color=gray>' ''Genera l'apposita libreria esterna .so:''</font> | ||
− | + | Shell "gcc -o /tmp/libso.so /tmp/libso.c -shared -fPIC" Wait | |
'''End''' | '''End''' |
Versione delle 01:10, 28 ago 2018
Per ottenere il proprio indirizzo IP locale da una rete, è possibile utilizzare il codice che segue, il quale prevede la creazione di un'apposita libreria dinamica condivisa .so, finalizzata alla gestione sicura di alcune risorse esterne.
L'area di memoria della Struttura "struct ifreq", contenuta nel file header: /usr/include/net/if.h, verrà gestita mediante un Puntatore ed i Memory Stream.
Library "libc:6" Private Const AF_INET As Integer = 2 Private Const SOCK_DGRAM As Integer = 2 Private Const SIOCGIFADDR As Integer = 35093 ' 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(po As Pointer) 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 st As Stream Dim s As String 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(SizeOf(gb.Byte), 40) 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 Free(ifreq) close_C(fd) 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" & "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