|
|
Riga 1: |
Riga 1: |
− | 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.
| + | #REDIRECT [[Ottenere_gli_indirizzi_IP_di_ogni_interfaccia_presente_sulla_medesima_macchina]] |
− | | |
− | 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
| |
− |
| |
− | <FONT Color=gray>' ''int socket (int __domain, int __type, int __protocol)''
| |
− | ' ''Create a new socket of type TYPE in domain DOMAIN, using protocol PROTOCOL.''</font>
| |
− | Private Extern socket(domain As Integer, type As Integer, protocol As Integer) As Integer
| |
− |
| |
− | <FONT Color=gray>' ''int ioctl (int __fd, unsigned long int __request, ...)''
| |
− | ' ''Perform the I/O control operation specified by REQUEST on FD.''</font>
| |
− | Private Extern ioctl(__fd As Integer, request As Long, argum As Pointer) As Integer
| |
− |
| |
− | <FONT Color=gray>' ''int Close (int __fd)''
| |
− | ' ''Close the file descriptor FD.''</font>
| |
− | Private Extern close_C(__fd As Integer) As Integer Exec "close"
| |
− |
| |
− |
| |
− | Library "/tmp/libso"
| |
− |
| |
− | <FONT Color=gray>' ''int Inetntoa(po As Pointer)''</font>
| |
− | 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)
| |
− |
| |
− | <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>
| |
− | bb.Write(st, 0, bb.Count)
| |
− | <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'''
| |
− |
| |
− |
| |
− | '''Private''' Procedure Creaso()
| |
− |
| |
− | <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>
| |
− | Shell "gcc -o /tmp/libso.so /tmp/libso.c -shared -fPIC" Wait
| |
− |
| |
− | '''End'''
| |