31.05.2013 Views

progettazione e realizzazione in java di una rete peer to peer ...

progettazione e realizzazione in java di una rete peer to peer ...

progettazione e realizzazione in java di una rete peer to peer ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROGETTAZIONE E REALIZZAZIONE IN JAVA DI<br />

UNA RETE PEER TO PEER ANONIMA E<br />

MULTIFUNZIONALE<br />

RELATORE: Ch.mo Prof. Enoch Peserico Stecch<strong>in</strong>i Negri De Salvi<br />

LAUREANDO: Paolo Bertasi<br />

Corso <strong>di</strong> laurea <strong>in</strong> Ingegneria Informatica<br />

A.A. 2004-2005


UNIVERSITÀ DEGLI STUDI DI PADOVA<br />

Dipartimen<strong>to</strong> <strong>di</strong> Ingegneria dell’Informazione<br />

Corso <strong>di</strong> Laurea <strong>in</strong> Ingegneria Informatica<br />

TESI DI LAUREA<br />

PROGETTAZIONE E<br />

REALIZZAZIONE IN JAVA DI UNA<br />

RETE PEER TO PEER ANONIMA E<br />

MULTIFUNZIONALE<br />

RELATORE: Prof. Enoch Peserico Stecch<strong>in</strong>i Negri De Salvi<br />

LAUREANDO: Paolo Bertasi<br />

A.A. 2004-2005


Ai miei geni<strong>to</strong>ri<br />

chi mi hanno sempre sostenu<strong>to</strong><br />

<strong>in</strong>coraggia<strong>to</strong> e aiuta<strong>to</strong>.


In<strong>di</strong>ce<br />

Sommario 1<br />

Introduzione 3<br />

1 Le caratteristiche peculiari 7<br />

1.1 Architettura a plug-<strong>in</strong> . . . . . . . . . . . . . . . . . . . . . . . . 8<br />

1.2 Serverless . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9<br />

1.3 Anonima<strong>to</strong> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10<br />

1.4 I cre<strong>di</strong>ti . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11<br />

2 Le componenti 15<br />

2.1 Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15<br />

2.1.1 S<strong>to</strong>rage . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19<br />

2.1.2 Connettività . . . . . . . . . . . . . . . . . . . . . . . . . . 21<br />

2.2 Kademlia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22<br />

2.3 Altri plug-<strong>in</strong> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28<br />

2.3.1 Web server (http) . . . . . . . . . . . . . . . . . . . . . . 28<br />

2.3.2 E-mail (smtp, pop3) . . . . . . . . . . . . . . . . . . . . . 29<br />

2.3.3 Host resolution (dns) . . . . . . . . . . . . . . . . . . . . . 30<br />

2.3.4 File shar<strong>in</strong>g (aMule) . . . . . . . . . . . . . . . . . . . . . 30<br />

3 Management 33<br />

Conclusioni 37<br />

A Documentazione del proget<strong>to</strong> 39<br />

A.1 Kademlia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39<br />

A.1.1 ADT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39<br />

A.1.2 Communication . . . . . . . . . . . . . . . . . . . . . . . . 42<br />

A.1.3 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45<br />

v


INDICE<br />

A.1.4 Datagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . 46<br />

A.2 Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48<br />

A.2.1 The core . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48<br />

A.2.2 Resource manager . . . . . . . . . . . . . . . . . . . . . . . 49<br />

A.2.3 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50<br />

Bibliografia 53<br />

Elenco delle figure 55<br />

Elenco delle tabelle 57<br />

vi


Sommario<br />

Con l’avven<strong>to</strong> delle connessioni a banda larga e la moltiplicazione dei PC<br />

posseduti dai s<strong>in</strong>goli privati è, <strong>di</strong> fat<strong>to</strong>, na<strong>to</strong> un nuovo possibile uso <strong>di</strong> <strong>in</strong>ternet.<br />

Già da qualche anno, <strong>in</strong>fatti, per sfruttare le potenzialità delle macch<strong>in</strong>e connesse<br />

alla <strong>rete</strong>, sono sorti <strong>di</strong>versi progetti per il calcolo <strong>di</strong>stribui<strong>to</strong> 1 . Parallelamente,<br />

è esploso il fenomeno del file shar<strong>in</strong>g grazie a client estremamente semplici e<br />

funzionali come eMule 2 . Entrambe queste tecnologie hanno un denom<strong>in</strong>a<strong>to</strong>re<br />

comune: sfruttano l’architettura <strong>peer</strong>-<strong>to</strong>-<strong>peer</strong> per offrire servizi che le s<strong>in</strong>gole<br />

macch<strong>in</strong>e, da sole, non potrebbero mai offrire. Da qui l’idea <strong>di</strong> realizzare <strong>una</strong><br />

<strong>rete</strong> multi-funzionale, <strong>in</strong> grado, grazie al contribu<strong>to</strong> <strong>di</strong> tutti gli utenti, <strong>di</strong> fornire<br />

tutti i servizi già oggi <strong>di</strong>sponibili su <strong>in</strong>ternet e <strong>di</strong> essere aggiornata con nuove<br />

funzionalità, qualora ce ne fosse l’esigenza.<br />

Una caratteristica, che avrà un forte peso nella <strong>realizzazione</strong> <strong>di</strong> questa <strong>in</strong>fra-<br />

struttura, sarà l’attenzione all’anonima<strong>to</strong>. In un momen<strong>to</strong> <strong>in</strong> cui, sempre più<br />

spesso, casi <strong>di</strong> censura balzano agli onori della cronaca 3 , <strong>una</strong> <strong>rete</strong> come questa<br />

permetterà la libera circolazione <strong>di</strong> idee e <strong>in</strong>formazioni.<br />

1 Citiamo qui solo Bo<strong>in</strong>c http://bo<strong>in</strong>c.berkeley.edu/.<br />

2 http://www.emule-project.net/<br />

3 Uno su tutti: C<strong>in</strong>a, anche Google accetta la censura<br />

http://www.corriere.it/Primo Piano/Scienze e Tecnologie/2006/01 Gennaio/25/<br />

google.shtml


Introduzione<br />

In ques<strong>to</strong> elabora<strong>to</strong> si illustrerà la <strong>progettazione</strong> <strong>di</strong> PariPari, <strong>una</strong> nuova <strong>rete</strong><br />

<strong>peer</strong>-<strong>to</strong>-<strong>peer</strong>.<br />

Generalmente per <strong>peer</strong>-<strong>to</strong>-<strong>peer</strong> (o P2P) si <strong>in</strong>tende <strong>una</strong> <strong>rete</strong> <strong>di</strong> compu-<br />

ter o qualsiasi <strong>rete</strong> che non possiede client o server fissi, ma un numero<br />

<strong>di</strong> no<strong>di</strong> equivalenti (<strong>peer</strong>, appun<strong>to</strong>) che fungono sia da client che da<br />

server verso altri no<strong>di</strong> della <strong>rete</strong> 4 .<br />

Gli esempi più famosi <strong>di</strong> reti <strong>di</strong> ques<strong>to</strong> tipo sono quelle de<strong>di</strong>cate allo scambio<br />

<strong>di</strong> file: <strong>in</strong> pr<strong>in</strong>cipio si trattava <strong>di</strong> file musicali (MP3), poi si è arrivati allo scambio<br />

<strong>di</strong> qualsiasi genere <strong>di</strong> file. Queste reti, nonostante la loro natura <strong>di</strong>stribuita, basa-<br />

vano il loro funzionamen<strong>to</strong> su dei server. Questi servivano a mettere <strong>in</strong> contat<strong>to</strong><br />

tra loro i vari no<strong>di</strong> della <strong>rete</strong>, senza però prendere mai parte al trasferimen<strong>to</strong> dei<br />

file. Tuttavia, è chiaro che <strong>in</strong> caso <strong>di</strong> irraggiungibilità <strong>di</strong> questi server, la <strong>rete</strong> non<br />

poteva sussistere proprio perché i vari no<strong>di</strong> non avrebbero potu<strong>to</strong> comunicare tra<br />

loro. Esempi <strong>di</strong> ques<strong>to</strong> tipo <strong>di</strong> <strong>rete</strong> sono la <strong>rete</strong> eDonkey2000 (d’ora <strong>in</strong> poi ED2K),<br />

ancor oggi usata da celebri programmi come mlDonkey, eMule, aMule e la <strong>rete</strong><br />

Bit<strong>to</strong>rrent 5 col suo famoso client Azureus.<br />

Per rendere la <strong>rete</strong> più resistente al black out <strong>di</strong> qualunque dei computer ad<br />

essa collegati sono stati sviluppati pro<strong>to</strong>colli <strong>di</strong> gestione completamente decen-<br />

tralizzati 6 . Questi pro<strong>to</strong>colli cercano <strong>di</strong> affidare a ogni nodo che concorre alla<br />

formazione della <strong>rete</strong> l’<strong>in</strong><strong>di</strong>cizzazione <strong>di</strong> alcuni file, <strong>in</strong> modo che ogni file faccia<br />

riferimen<strong>to</strong> ad un nodo attivo, e sia qu<strong>in</strong><strong>di</strong> r<strong>in</strong>tracciabile. Alcuni pro<strong>to</strong>colli <strong>di</strong><br />

ques<strong>to</strong> tipo sono Chord 7 , Pastry 8 , ma soprattut<strong>to</strong> Kademlia. Quest’ultimo, che<br />

4 [4]<br />

5 Cfr. [5]<br />

6 Pro<strong>to</strong>colli DHT: Distributed Hash Table.<br />

cfr. http://en.wikipe<strong>di</strong>a.org/wiki/Distributed hash table<br />

7 Cfr. [6]<br />

8 Cfr. [7]<br />

3


INTRODUZIONE<br />

sembra essere il più promettente, viene già sfrutta<strong>to</strong> nelle recenti versioni <strong>di</strong> quasi<br />

tutti i client per la <strong>rete</strong> ED2K.<br />

Un altro aspet<strong>to</strong> poco desiderabile delle reti P2P è il fat<strong>to</strong> che, anche <strong>in</strong><br />

<strong>una</strong> <strong>rete</strong> completamente serverless, è piut<strong>to</strong>s<strong>to</strong> semplice riuscire a scoprire quali<br />

no<strong>di</strong> (quali <strong>in</strong><strong>di</strong>rizzi IP) con<strong>di</strong>vidono quali file. La privacy degli utenti della<br />

<strong>rete</strong> non è, qu<strong>in</strong><strong>di</strong>, per nulla garantita. Ques<strong>to</strong> problema è sta<strong>to</strong> affronta<strong>to</strong> e<br />

risol<strong>to</strong> da recenti reti, come Ants e Mute, che si avvalgono <strong>di</strong> tecniche come<br />

il rout<strong>in</strong>g probabilistico 9 . Queste reti sono però, tut<strong>to</strong>ra, reti <strong>di</strong> nicchia, vista<br />

l’esigua <strong>di</strong>mensione che raggiungono, proprio perché la garanzia dell’anonima<strong>to</strong><br />

dell’utente tende a rendere la ricerca meno efficiente e, qu<strong>in</strong><strong>di</strong>, spesso, più lenta.<br />

Tuttavia il fat<strong>to</strong>re che maggiormente contribuisce a contenere la <strong>di</strong>mensione <strong>di</strong><br />

queste reti, è il cosiddet<strong>to</strong> effet<strong>to</strong> <strong>rete</strong> 10 .<br />

Accan<strong>to</strong> a queste reti, che hanno come scopo il trasferimen<strong>to</strong> file, vivono<br />

altri progetti con obiettivi <strong>di</strong>versi. Citiamo qui Freenet 11 , che fornisce servizi <strong>di</strong><br />

webhost<strong>in</strong>g anonimo e Skype 12 che gestisce un sistema <strong>di</strong> VOIP su <strong>una</strong> <strong>rete</strong> P2P.<br />

Uno dei problemi pr<strong>in</strong>cipali che hanno affronta<strong>to</strong> queste reti è sta<strong>to</strong> come<br />

<strong>in</strong>vogliare l’utente a con<strong>di</strong>videre i propri file. Per risolvere ques<strong>to</strong> problema le<br />

reti si sono spesso basate su un sistema <strong>di</strong> gestione <strong>di</strong> cre<strong>di</strong>ti virtuali. Il sistema<br />

che sembra funzionare meglio 13 è quello adotta<strong>to</strong> dalla <strong>rete</strong> ED2K. Proviamo a<br />

spiegare come funziona con un semplice esempio. Se Alice scarica un file da Bob,<br />

Alice è <strong>in</strong> debi<strong>to</strong> verso Bob e, qu<strong>in</strong><strong>di</strong>, Bob potrà saldare il cre<strong>di</strong><strong>to</strong> scaricando i<br />

file <strong>di</strong> Alice prima degli altri eventuali concorrenti. Ques<strong>to</strong> sistema ha però <strong>una</strong><br />

grossa pecca: Bob, <strong>in</strong>fatti, sarà <strong>in</strong> cre<strong>di</strong><strong>to</strong> solo con Alice e con nessun altro nodo<br />

della <strong>rete</strong>, nonostante la partecipazione alla <strong>rete</strong> e la con<strong>di</strong>visione dei file rendano<br />

un servizio (per quan<strong>to</strong> ipotetico) a tutti i no<strong>di</strong>.<br />

PariPari è <strong>una</strong> <strong>rete</strong> serverless basata su <strong>una</strong> variante <strong>di</strong> kademlia, garantisce<br />

l’anonima<strong>to</strong> dei suoi no<strong>di</strong>, fornisce un sistema <strong>di</strong> cre<strong>di</strong>ti più <strong>in</strong>telligente <strong>di</strong> reti<br />

come ED2K, e, soprattut<strong>to</strong>, è multifunzionale.<br />

È probabilmente la multifunziona-<br />

lità l’aspet<strong>to</strong> più <strong>in</strong>novativo <strong>di</strong> ques<strong>to</strong> proget<strong>to</strong> (e probabilemte la sfida più grande<br />

che ci siamo posti). Lo scopo è, <strong>in</strong>fatti, quello <strong>di</strong> <strong>di</strong>stribuire sulla <strong>rete</strong> tutti i più<br />

comuni servizi <strong>di</strong>sponibili su <strong>in</strong>ternet, mantendendoli però raggiungibili e fruibili<br />

anche da computer esterni a PariPari. Per raggiungere ques<strong>to</strong> obiettivo è sta<strong>to</strong><br />

scel<strong>to</strong> un approccio a plug-<strong>in</strong>. At<strong>to</strong>rno a un nucleo centrale (det<strong>to</strong> core) viene<br />

9Cfr. [8]<br />

10Cfr. http://en.wikipe<strong>di</strong>a.org/wiki/Network effect<br />

11http://freenetproject.org/ 12http://www.skype.com/ 13Valutandolo <strong>in</strong> base al numero <strong>di</strong> utenti<br />

4


gradualmente costruita <strong>una</strong> galassia <strong>di</strong> plug-<strong>in</strong> che saranno <strong>in</strong> grado <strong>di</strong> <strong>in</strong>teragi-<br />

re tra loro e con <strong>in</strong>ternet per fornire i più <strong>di</strong>sparati servizi. Questa architettura<br />

permette <strong>di</strong> poter aggiungere alla <strong>rete</strong> nuovi servizi, qualora ce ne sia la necessità.<br />

PariPari si propone, qu<strong>in</strong><strong>di</strong>, <strong>di</strong> creare <strong>una</strong> macch<strong>in</strong>a virtuale capace <strong>di</strong> prov-<br />

vedere a tutti i bisogni dell’utente <strong>di</strong> <strong>in</strong>ternet mantendosi <strong>di</strong>pendente solo dalla<br />

comunità dei no<strong>di</strong> da cui è formata.<br />

Figura 1: La <strong>rete</strong> e gli host esterni.<br />

Nelle prossime pag<strong>in</strong>e illustreremo approfon<strong>di</strong>tamente i tratti <strong>in</strong>novativi <strong>di</strong><br />

questa <strong>rete</strong>. Inoltre, tratteremo la <strong>progettazione</strong> della <strong>rete</strong> e la <strong>realizzazione</strong><br />

<strong>di</strong> alcune sue parti. Nel primo capi<strong>to</strong>lo passeremo <strong>in</strong> rassegna le peculiarità <strong>di</strong><br />

PariPari. Nel secondo, <strong>in</strong>vece, sposteremo l’attenzione sulla <strong>progettazione</strong> della<br />

<strong>rete</strong> e sulla <strong>realizzazione</strong> delle parti a me assegnate. Si menzionerano altresì gli<br />

altri plug-<strong>in</strong> e il loro sta<strong>to</strong> d’avanzamen<strong>to</strong>. Il terzo capi<strong>to</strong>lo verrà de<strong>di</strong>ca<strong>to</strong> alle<br />

problematiche dovute alla gestione del proget<strong>to</strong>. Inf<strong>in</strong>e, <strong>in</strong> appen<strong>di</strong>ce, si potrà<br />

trovare la documentazione, <strong>in</strong> <strong>in</strong>glese, delle parti del proget<strong>to</strong> da me realizzate.<br />

5


INTRODUZIONE<br />

6


Capi<strong>to</strong>lo 1<br />

Le caratteristiche peculiari<br />

PariPari, come det<strong>to</strong> nell’<strong>in</strong>troduzione, si contrad<strong>di</strong>st<strong>in</strong>gue per quattro aspetti<br />

pr<strong>in</strong>cipali:<br />

• la sua natura serverless,<br />

• l’architettura de<strong>di</strong>ta all’espan<strong>di</strong>bilità e alla multifunzionalità,<br />

• il sistema <strong>di</strong> gestione dei cre<strong>di</strong>ti,<br />

• la garanzia <strong>di</strong> anonima<strong>to</strong> per l’utente.<br />

Questi punti saranno, <strong>in</strong> segui<strong>to</strong>, approfon<strong>di</strong>ti adeguatamente; ora <strong>in</strong>vece sot<strong>to</strong>li-<br />

neiamo altre due caratteristiche del client e della <strong>rete</strong>.<br />

Si è deciso <strong>di</strong> scrivere il client <strong>in</strong> Java, per permettere la massima penetrazione<br />

della <strong>rete</strong>. Java, come è no<strong>to</strong>, rende possibile la portabilità del programma su<br />

tutte le pr<strong>in</strong>cipali piattaforme senza la necessità <strong>di</strong> ri-compilare il co<strong>di</strong>ce sorgente.<br />

Java, <strong>in</strong>oltre, grazie alla tecnologia Java Web Start, rende trasparente all’utente<br />

l’uso della <strong>rete</strong>. Infatti, grazie all’<strong>in</strong>tegrazione col browser, sarà possibile scaricare,<br />

<strong>in</strong>stallare, e tenere aggiorna<strong>to</strong> il software <strong>in</strong> modo pressochè au<strong>to</strong>matico.<br />

L’uso <strong>di</strong> Java, tuttavia, comporta <strong>una</strong> certa per<strong>di</strong>ta <strong>di</strong> performance. Le ope-<br />

razioni più cos<strong>to</strong>se dal pun<strong>to</strong> <strong>di</strong> vista computazionale risultano essere quelle <strong>di</strong><br />

crit<strong>to</strong>grafia. Queste, scritte <strong>in</strong> Java, risultano quattro volte più lente che se fosse-<br />

ro scritte <strong>in</strong> C++. Questa riduzione nelle prestazioni non <strong>in</strong>fluisce mol<strong>to</strong> sui tempi<br />

<strong>di</strong> risposta del programma, da<strong>to</strong> che la maggior parte delle cifrazioni avviene su<br />

stream <strong>di</strong> byte dalle <strong>di</strong>mensioni mol<strong>to</strong> contenute.<br />

Possiamo affermare qu<strong>in</strong><strong>di</strong>, che a fronte <strong>di</strong> un leggero calo nelle prestazioni,<br />

c’è, per l’utente f<strong>in</strong>ale, e per lo sviluppa<strong>to</strong>re, un significativo <strong>in</strong>cremen<strong>to</strong> nella<br />

facilità d’uso e <strong>di</strong> gestione del client.<br />

7


1. LE CARATTERISTICHE PECULIARI<br />

Un altro pun<strong>to</strong> <strong>di</strong> forza della <strong>rete</strong> è la fruibilità dei servizi anche dall’esterno.<br />

Infatti, i client che partecipano alla <strong>rete</strong> possono utilizzare le risorse con<strong>di</strong>vise da-<br />

gli altri no<strong>di</strong>; elemen<strong>to</strong> <strong>di</strong> novità è che la <strong>rete</strong> potrà essere sfruttata da computer<br />

non facenti parte <strong>di</strong> PariPari. Ad esempio, se all’<strong>in</strong>terno della <strong>rete</strong>, Alice si pro-<br />

pone come webserver e Bob le fornisce <strong>una</strong> sua pag<strong>in</strong>a, la pag<strong>in</strong>a sarà consultabile<br />

da qualsiasi computer connesso a <strong>in</strong>ternet con un normale browser.<br />

Vista la natura aperta al pubblico e agli sviluppa<strong>to</strong>ri della <strong>rete</strong> si è deciso che<br />

il proget<strong>to</strong> sarà open source e rilascia<strong>to</strong> sot<strong>to</strong> licenza GPL 1 .<br />

1.1 Architettura a plug-<strong>in</strong><br />

La <strong>rete</strong>, come det<strong>to</strong>, è concepita per permetterle <strong>di</strong> fornire praticamente qualsiasi<br />

serivizio. Ogni nodo, <strong>in</strong>fatti, concorrerà alla creazione <strong>di</strong> un’entità <strong>in</strong> grado,<br />

al momen<strong>to</strong>, <strong>di</strong> agire come un server web o un server per la posta elettronica,<br />

<strong>di</strong> trasferire file, e, <strong>in</strong> un futuro prossimo, <strong>di</strong> con<strong>di</strong>videre i cicli macch<strong>in</strong>a, <strong>di</strong><br />

permettere il VOIP e <strong>di</strong> offrire le funzionalità <strong>di</strong> un DBMS <strong>di</strong>stribui<strong>to</strong>. Per<br />

raggiungere ques<strong>to</strong> obiettivo, ogni client della <strong>rete</strong> è costitui<strong>to</strong> da un nocciolo<br />

centrale, det<strong>to</strong> core, che si occupa <strong>di</strong> fare da collante tra i vari plug-<strong>in</strong> e <strong>di</strong> gestire<br />

le comunicazioni tra questi. I plug-<strong>in</strong>, realizzati secondo <strong>una</strong> specifica <strong>in</strong>terfaccia,<br />

gestiscono le varie funzionalità della <strong>rete</strong> e sfruttano le risorse della macch<strong>in</strong>a su<br />

cui gira il client, <strong>in</strong>teragendo con gli altri plug-<strong>in</strong> tramite il core. Sempre con la<br />

struttura <strong>di</strong> semplici plug-<strong>in</strong>, si possono <strong>in</strong><strong>di</strong>viduare alcune classi che svolgono un<br />

ruolo basilare per il client: i ges<strong>to</strong>ri <strong>di</strong> risorse. Questi, scritti esattamente come<br />

dei normali plug-<strong>in</strong>, sovra<strong>in</strong>tendono all’utilizzazione delle risorse del computer<br />

locale da parte dei plug-<strong>in</strong>. Ques<strong>to</strong> approccio garantisce la modularità, non solo<br />

per quan<strong>to</strong> riguarda i servizi offerti alla <strong>rete</strong>, ma anche per quan<strong>to</strong> riguarda la<br />

gestione <strong>in</strong>terna dei plug-<strong>in</strong>. Con un esempio si chiarirà meglio questa scelta.<br />

Il plug-<strong>in</strong>, che funge da server ftp sul nodo PariPari <strong>di</strong> Alice, viene contat-<br />

ta<strong>to</strong> da un normale utente del web, Bob. Bob fa upload <strong>di</strong> un file e il server ftp<br />

lo salva. L’ftp però non ha accesso <strong>di</strong>ret<strong>to</strong> al <strong>di</strong>sco <strong>di</strong> Alice, ma <strong>in</strong>oltra la sua<br />

richiesta <strong>di</strong> salvataggio del file al ges<strong>to</strong>re dello spazio su <strong>di</strong>sco, il quale si occupa<br />

<strong>di</strong> salvare realmente il file. Ques<strong>to</strong> layer <strong>in</strong>terpos<strong>to</strong> tra il <strong>di</strong>sco e il server permette<br />

<strong>di</strong> aggiungere nuove potenzialità <strong>in</strong> maniera estremamente semplice, non mo<strong>di</strong>fi-<br />

cando cioè <strong>in</strong> alcun modo i plug-<strong>in</strong> già <strong>di</strong>sponibili e cambiando solamente qualche<br />

riga nei ges<strong>to</strong>ri <strong>di</strong> risorse. Si immag<strong>in</strong>i, <strong>in</strong>fatti, <strong>di</strong> voler aggiungere la possibilità<br />

<strong>di</strong> salvare il file non sul <strong>di</strong>sco fisso locale, ma <strong>di</strong> <strong>di</strong>stribuendolo tra <strong>di</strong>versi no<strong>di</strong><br />

1 Cfr. http://www.gnu.org/copyleft/gpl.html<br />

8


1.2 SERVERLESS<br />

della <strong>rete</strong> (ad esempio per assicurarne la sopravvivenza <strong>in</strong> caso <strong>di</strong> <strong>di</strong>sastro); il<br />

ges<strong>to</strong>re semplicemente passerà il file a un ulteriore plug-<strong>in</strong> che si occupa <strong>di</strong> ques<strong>to</strong><br />

processo e ques<strong>to</strong> plug-<strong>in</strong>, <strong>in</strong>teragendo con altri plug-<strong>in</strong> e altri moduli, <strong>di</strong>sperderà<br />

il file sulla <strong>rete</strong>. Ovviamente, su richiesta, il file potrà venir recupera<strong>to</strong> e passa<strong>to</strong><br />

nuovamente al ges<strong>to</strong>re e da ques<strong>to</strong> al server ftp.<br />

Conclu<strong>di</strong>amo <strong>di</strong>cendo che, nonostante ques<strong>to</strong> sistema garantisca <strong>una</strong> così am-<br />

pia elasticità, scrivere i plug-<strong>in</strong> risulta ancora mol<strong>to</strong> semplice. Questi, <strong>in</strong>fatti, non<br />

hanno alc<strong>una</strong> <strong>in</strong>terazione <strong>di</strong>retta tra loro, ma vengono sempre me<strong>di</strong>ati dal core<br />

e comunque usano le risorse del sistema e gli altri plug-<strong>in</strong> come se quest ultimi<br />

fossero delle semplici black box. Colui che scrive un plug-<strong>in</strong>, <strong>in</strong> ultima analisi, si<br />

deve preoccupare solo <strong>di</strong> capire con quali eventuali altri moduli <strong>in</strong>teragire e cosa<br />

questi prendono <strong>in</strong> <strong>in</strong>put e forniscono come output.<br />

1.2 Serverless<br />

Figura 1.1: La struttura del client.<br />

PariPari deve il suo successo alla possibilità <strong>di</strong> funzionare senza richiedere ai<br />

suoi utenti <strong>di</strong> permanere collegati ad essa perennemente. D’altra parte, non è<br />

consigliabile mantenere <strong>una</strong> struttura centralizzata della <strong>rete</strong>, tramite dei no<strong>di</strong><br />

pr<strong>in</strong>cipali, proprio perché l’<strong>in</strong><strong>di</strong>sponibilità <strong>di</strong> quest ultimi <strong>di</strong>sgregherebbe la <strong>rete</strong><br />

<strong>in</strong>tera.<br />

9


1. LE CARATTERISTICHE PECULIARI<br />

Per fronteggiare queste richieste, ci siamo orientati verso un pro<strong>to</strong>collo recente,<br />

ma già usa<strong>to</strong> e collauda<strong>to</strong>: Kademlia.<br />

Kademlia, idea<strong>to</strong> da Petar Maymounkov e David Mazi‘eres, è essenzialmente<br />

un sistema per <strong>in</strong><strong>di</strong>cizzare host e risorse e permettere la ricerca <strong>di</strong> entrambi, <strong>in</strong><br />

modo completamente decentralizza<strong>to</strong>. Senza aver la presunzione <strong>di</strong> affrontare il<br />

problema <strong>in</strong> modo esaustivo, possiamo <strong>di</strong>re che ques<strong>to</strong> sistema associa ad ogni<br />

risorsa e ad ogni nodo un hash univoco nello stesso spazio matematico. Con<br />

<strong>una</strong> metrica basata sull’operazione <strong>di</strong> XOR, è possibile calcolare le <strong>di</strong>stanze tra i<br />

no<strong>di</strong> e le risorse ed assegnare ad ogni nodo attivo la responsabilità delle risorse<br />

a lui più vic<strong>in</strong>e. Un nodo che cerca sulla <strong>rete</strong> qualcosa (<strong>di</strong> cui conosce l’hash)<br />

<strong>in</strong>terroga tutti i no<strong>di</strong> che già conosce che sono più vic<strong>in</strong>i alla risorsa. Questi<br />

rispondono o con la risorsa stessa o fornendo al cerca<strong>to</strong>re <strong>una</strong> lista <strong>di</strong> ulteriori<br />

no<strong>di</strong> ancora più vic<strong>in</strong>i. La <strong>rete</strong> <strong>di</strong>venta così completamente serverless e qu<strong>in</strong><strong>di</strong><br />

mol<strong>to</strong> resistente all’<strong>in</strong>evitabile <strong>in</strong><strong>di</strong>sponibilità <strong>di</strong> alcuni suoi no<strong>di</strong>. Ques<strong>to</strong> risulta<strong>to</strong><br />

permette, altresì, alla <strong>rete</strong> <strong>di</strong> essere <strong>di</strong>fficilmente sabotabile da<strong>to</strong> che i suoi no<strong>di</strong><br />

sono tutti uguali e perciò non presenta nessun pun<strong>to</strong> debole. 2<br />

1.3 Anonima<strong>to</strong><br />

In <strong>una</strong> <strong>rete</strong> P2P, e qu<strong>in</strong><strong>di</strong> anche <strong>in</strong> PariPari, le transizioni <strong>di</strong> risorse (file, cicli<br />

macch<strong>in</strong>a,...) avvengono sempre tra due no<strong>di</strong> della <strong>rete</strong>. Il nostro sistema <strong>di</strong> ano-<br />

nima<strong>to</strong> permette <strong>di</strong> effetture ques<strong>to</strong> contat<strong>to</strong> senza che sia possibile r<strong>in</strong>tracciare<br />

l’<strong>in</strong><strong>di</strong>rizzo IP del mittente nè quello del ricevente. Senza addentraci <strong>in</strong> <strong>una</strong> de-<br />

scrizione troppo approfon<strong>di</strong>ta che sarà esposta <strong>in</strong> [3], <strong>di</strong>amo <strong>una</strong> descrizione dei<br />

tratti salienti del funzionamen<strong>to</strong> <strong>di</strong> ques<strong>to</strong> sistema.<br />

Premettiamo che ogni volta che si parlerà <strong>di</strong> crit<strong>to</strong>grafia si <strong>in</strong>tenderà un pro-<br />

cesso mis<strong>to</strong> tra crit<strong>to</strong>grafia simmetrica e asimmetrica. Attualmente, <strong>in</strong>fatti, i<br />

dati sot<strong>to</strong>posti a cifratura, vengono prima passati all’algoritmo a chiave segreta<br />

AES e poi, solo la chiave viene passata all’algoritmo RSA con chiave pubblica<br />

privata a 2048 bit 3 . La crit<strong>to</strong>grafia sui byte viene affidata ad un algoritmo mol<strong>to</strong><br />

veloce, mentre a quello mol<strong>to</strong> più len<strong>to</strong> viene lasciata la crit<strong>to</strong>grafia solo della<br />

chiave simmetrica (della <strong>di</strong>mensione <strong>di</strong> pochi byte). In ques<strong>to</strong> modo si possono<br />

sfruttare i benefici derivanti dall’uso <strong>di</strong> algoritmo a chiave asimmetrica pagando<br />

solo marg<strong>in</strong>almente la sua effettiva lentezza.<br />

2 Sarebbe più corret<strong>to</strong> <strong>di</strong>re che tutti i no<strong>di</strong> sono punti con la stessa debolezza.<br />

3 La limitazione 2048 bit è imposta dalle JCE della Sun per restrizioni sull’esportazione <strong>di</strong><br />

tecnologia crit<strong>to</strong>grafica fuori dagli USA.<br />

10


<strong>di</strong>mensione array <strong>in</strong> KB ms per RSA ms per AES<br />

3 27721 141<br />

11 107865 145<br />

269 2498566 285<br />

1799 16612472 1306<br />

Tabella 1.1: Crit<strong>to</strong>grafia: RSA vs AES.<br />

1.4 I CREDITI<br />

Il sistema permette all’utente della <strong>rete</strong> <strong>di</strong> creare tra sè e il suo <strong>in</strong>terlocu<strong>to</strong>re<br />

<strong>una</strong> catena <strong>di</strong> no<strong>di</strong>, e altrettan<strong>to</strong> può fare l’<strong>in</strong>terlocu<strong>to</strong>re stesso. Il mittente, e<br />

simmetricamente il ricevente, prima <strong>di</strong> <strong>in</strong>iziare la trasmissione, sceglie <strong>una</strong> catena<br />

<strong>di</strong> no<strong>di</strong> <strong>in</strong>terme<strong>di</strong>, ognuno dei quali ignora l’esistenza e qu<strong>in</strong><strong>di</strong> l’identità degli altri<br />

no<strong>di</strong> formanti la catena. Vengono qu<strong>in</strong><strong>di</strong> passate le richieste tra i due <strong>in</strong>terlocu-<br />

<strong>to</strong>ri attraverso questi tunnel con un meccanismo <strong>di</strong> onion rout<strong>in</strong>g che permette<br />

la seg<strong>rete</strong>zza dei dati trasportati dai no<strong>di</strong> e la sicurezza della comunicazione. I<br />

dati, <strong>in</strong>fatti, sono cifrati con le chiavi pubbliche <strong>in</strong> modo che solo il nodo che <strong>in</strong><br />

quel momen<strong>to</strong> deve ricevere, ed eventualemente rispe<strong>di</strong>re, il pacchet<strong>to</strong> <strong>di</strong> comu-<br />

nicazione può leggerne il contenu<strong>to</strong>.<br />

È evidente che, quan<strong>to</strong> più le catene sono<br />

lunghe, tan<strong>to</strong> è maggiore la sicurezza offerta all’utente, d’altra parte, aumentando<br />

il numero <strong>di</strong> salti e <strong>di</strong> passaggi crit<strong>to</strong>grafici aumenta anche la latenza nei trasferi-<br />

menti e nelle ricerche. Inoltre, l’uso dell’onion rout<strong>in</strong>g, aumenta il carico <strong>di</strong> banda<br />

complessivo per la <strong>rete</strong> <strong>in</strong> modo proporzionale al numero <strong>di</strong> salti.<br />

Riportiamo solamente qui un risulta<strong>to</strong> <strong>di</strong>scusso <strong>in</strong>[3]:<br />

È sufficiente che un nodo della catena non tenti <strong>di</strong> imbrogliare il<br />

nodo che l’ha recluta<strong>to</strong> per la costruzione del tunnel per garantirne<br />

l’anonima<strong>to</strong>.<br />

Si mette qu<strong>in</strong><strong>di</strong> a <strong>di</strong>sposizione <strong>di</strong> ogni nodo <strong>una</strong> tecnologia che mira a garantire<br />

la privacy dell’utilizza<strong>to</strong>re. Inoltre, l’utente può scegliere se usare o meno ques<strong>to</strong><br />

sistema, <strong>in</strong><strong>di</strong>pendentemente dalla scelta effettuata dal suo <strong>in</strong>terlocu<strong>to</strong>re.<br />

1.4 I cre<strong>di</strong>ti<br />

La gestione dei cre<strong>di</strong>ti <strong>in</strong> <strong>una</strong> <strong>rete</strong> P2P potrebbe sembrare un argomen<strong>to</strong> <strong>di</strong> se-<br />

codaria importanza, vis<strong>to</strong> che non ne migliora le prestazioni, nè ne aumenta le<br />

caratteristiche. Tuttavia è uno dei punti cruciali <strong>di</strong> queste reti perché regola<br />

11


1. LE CARATTERISTICHE PECULIARI<br />

Alice Bob<br />

Figura 1.2: Formazione <strong>di</strong> tunnel per la comunicazione anonima.<br />

i rapporti <strong>di</strong> collaborazione dei no<strong>di</strong> e garantisce qu<strong>in</strong><strong>di</strong> l’esistenza della <strong>rete</strong>.<br />

Proviamo ad analizzare alcune d<strong>in</strong>amiche che si possono presentare.<br />

Il caso più semplice, quello <strong>in</strong> cui non servirebbe nemmeno l’emissione <strong>di</strong><br />

cre<strong>di</strong>ti, si verifica quando due utenti barattano tra loro due loro risorse simul-<br />

taneamente. Ques<strong>to</strong> scenario risulta essere però estremamente raro; è <strong>di</strong>fficile,<br />

<strong>in</strong>fatti, che i due <strong>in</strong>terlocu<strong>to</strong>ri necessit<strong>in</strong>o, nello stesso momen<strong>to</strong>, della risorsa of-<br />

ferta dall’altro.<br />

È mol<strong>to</strong> più frequente, <strong>in</strong>vece, il caso <strong>in</strong> cui ques<strong>to</strong> avvenga <strong>in</strong><br />

tempi <strong>di</strong>fferenti. Ecco <strong>una</strong> descrizone esemplificata <strong>di</strong> quan<strong>to</strong> appena descrit<strong>to</strong>.<br />

Alice cerca un file musicale che possiede Bob, ma Bob nel momen<strong>to</strong> <strong>in</strong> cui cede<br />

il file ad Alice, non ha bisogno <strong>di</strong> niente da Alice. Tuttavia, visti gli <strong>in</strong>teressi<br />

comuni dei due, è facile prevedere che, pres<strong>to</strong> o tar<strong>di</strong>, Alice sarà nella con<strong>di</strong>zione<br />

<strong>di</strong> ricambiare il favore <strong>di</strong> Bob. Basterebbe che Alice si ricordasse <strong>di</strong> avere un de-<br />

bi<strong>to</strong> verso Bob per sod<strong>di</strong>sfare la sua richiesta tempestivamente. Ques<strong>to</strong> sistema,<br />

attualmente usa<strong>to</strong> dalla <strong>rete</strong> ED2K, premia le “amicizie” tra i no<strong>di</strong> della <strong>rete</strong>, ma<br />

trascura un fat<strong>to</strong> mol<strong>to</strong> rilevante. Un nodo che mette a <strong>di</strong>sposizione della <strong>rete</strong><br />

gran<strong>di</strong> risorse, e aumenta <strong>di</strong> mol<strong>to</strong> la potenzialità della <strong>rete</strong> stessa, potrà, qu<strong>in</strong>-<br />

<strong>di</strong>, <strong>in</strong> prima approssimazione, esigere risorse solo dai no<strong>di</strong> con cui ha già avu<strong>to</strong><br />

transizioni; per tutti gli altri no<strong>di</strong> della <strong>rete</strong>, <strong>in</strong>vece, avrà le stesse credenziali <strong>di</strong><br />

un nodo appena connesso. Se poi questi no<strong>di</strong> dovessero sparire, il nodo forni<strong>to</strong>re<br />

si ritroverebbe esattamente come un nodo appena entra<strong>to</strong>, avendo, <strong>in</strong> qualche<br />

modo, spreca<strong>to</strong> le risorse che aveva con<strong>di</strong>viso coi no<strong>di</strong> fuggiaschi. Inoltre, per<br />

12


1.4 I CREDITI<br />

garantire l’accrescimen<strong>to</strong> della <strong>rete</strong>, si dovrebbe favorire i client che con<strong>di</strong>vidono<br />

le risorse.<br />

Per superare ques<strong>to</strong> problema si potrebbe pensare all’adozione <strong>di</strong> <strong>una</strong> “mone-<br />

ta” unica comune per tutta la <strong>rete</strong>, non solo per ogni coppia <strong>di</strong> no<strong>di</strong> <strong>in</strong>terlocu<strong>to</strong>ri.<br />

Ques<strong>to</strong> approccio però risulta estremamente complesso e <strong>in</strong>sicuro. In primo luogo,<br />

ci sarebbe il rischio che la moneta potesse soffrire <strong>di</strong> forti movimenti <strong>in</strong>flazioni-<br />

stici, tali da precludere l’accesso alla <strong>rete</strong> da parte <strong>di</strong> nuovi no<strong>di</strong>. In secondo<br />

luogo, sarebbe necessaria la presenza <strong>di</strong> un’ au<strong>to</strong>rità che regolamenti l’emissione<br />

<strong>di</strong> questa moneta, ma ques<strong>to</strong> entrerebbe <strong>in</strong> conflit<strong>to</strong> con la natura completamente<br />

decentrata della <strong>rete</strong>.<br />

Il sistema che noi proponiamo si basa su due pilastri fondamentali: il criterio<br />

del buon emporista e il sacrificio.<br />

Partendo dalla situazione precedentemente menzionata, assumiamo che Bob,<br />

Alice, Charlie siano tre utenti della <strong>rete</strong>. Alice e Bob spesso concludono affari tra<br />

loro così come Alice e Charlie, mentre Bob e Charlie supponiamo non abbiano<br />

mai avu<strong>to</strong> nessun contat<strong>to</strong> <strong>di</strong>ret<strong>to</strong>. Immag<strong>in</strong>iamo ora che Charlie sia <strong>in</strong>teressa<strong>to</strong><br />

ad acquisire <strong>una</strong> risorsa <strong>di</strong> Bob. Putroppo, Charlie non è <strong>in</strong> cre<strong>di</strong><strong>to</strong> con Bob e non<br />

ha modo per aggiu<strong>di</strong>carsi tale risorsa. Alice, però, è fortemente <strong>in</strong>debitata con<br />

Charlie. Charlie procederà qu<strong>in</strong><strong>di</strong> ad acquisire la risorsa grazie alla me<strong>di</strong>azione<br />

<strong>di</strong> Alice, che risulta essere <strong>in</strong> debi<strong>to</strong> con Charlie e <strong>in</strong> cre<strong>di</strong><strong>to</strong> con Bob.<br />

Il nocciolo della questione è ques<strong>to</strong>: come Alice me<strong>di</strong>a questa transizione?<br />

Alice si trova ad avere cre<strong>di</strong>ti verso <strong>di</strong>versi altri utenti che le vengono richiesti con<br />

frequenze e quantità sempre <strong>di</strong>versi. Il suo scopo sarà quello <strong>di</strong> riuscire a compiere<br />

queste me<strong>di</strong>azioni, guadagnando comqunue sulla transazione, non rimanendo mai<br />

sprovvista <strong>di</strong> quei cre<strong>di</strong>ti che potrebbero servirle <strong>in</strong> futuro. Dovrebbe, sulla base<br />

delle transazioni cui ha preso parte, grazie a dei filtri <strong>di</strong> Kalman o dei filtri<br />

bayesiani, m<strong>in</strong>imizzare la probabilità <strong>di</strong> non poter concludere gli affari che le<br />

potranno <strong>in</strong>teressare nel futuro. Quello appena illustra<strong>to</strong> è, appun<strong>to</strong>, il criterio<br />

del buon emporista che deve cercare sempre <strong>di</strong> guadagnare non rimanendo mai<br />

senza scorta <strong>di</strong> nessun prodot<strong>to</strong> che possa <strong>in</strong>teressare ai clienti.<br />

Per rendere agevole ad un nuovo nodo entrare nella <strong>rete</strong> senza aver ness<strong>una</strong><br />

risorsa da con<strong>di</strong>videre, senza rischiare che questi si approfitti della situazione, si<br />

ricorre al sacrificio. Qualora, <strong>in</strong>fatti, si permettesse ad un nodo appena entra<strong>to</strong><br />

<strong>di</strong> acquisire risorse senza cederne <strong>di</strong> sue, concedendogli <strong>di</strong> emettere dei suoi cre-<br />

<strong>di</strong>ti, si renderebbe estremamente facile la creazione <strong>di</strong> masse <strong>di</strong> no<strong>di</strong>-sanguisuga.<br />

Converrebbe, <strong>in</strong>fatti, ai no<strong>di</strong> non <strong>in</strong>teressati allo sviluppo della <strong>rete</strong>, sferrare un<br />

13


1. LE CARATTERISTICHE PECULIARI<br />

attacco Sibilla 4 verso la <strong>rete</strong> nel seguente modo:<br />

1. il nodo si crea un’identità e si connette alla <strong>rete</strong>;<br />

2. il nodo trova la risorsa che sta cercando, la acquisisce rilasciando al forni<strong>to</strong>re<br />

i suoi cre<strong>di</strong>ti;<br />

3. il nodo si sconnette e non <strong>to</strong>rna mai più con quella identità <strong>in</strong> <strong>rete</strong>;<br />

4. il nodo ri-esegue le fasi da 1 a 3 cambiando identità un numero <strong>in</strong>def<strong>in</strong>i<strong>to</strong><br />

<strong>di</strong> volte.<br />

Ques<strong>to</strong> comportamen<strong>to</strong>, assolutamente da evitare, penalizza i no<strong>di</strong> onesti della<br />

<strong>rete</strong> <strong>di</strong>lapidando le loro risorse <strong>in</strong> favore dei no<strong>di</strong>-sanguisuga. Ma, se per il nodo<br />

non fosse così semplice cambiare identità o non gli fosse possibile emettere cre<strong>di</strong>ti,<br />

il problema sarebbe risol<strong>to</strong>.<br />

La nostra soluzione propone che un nuovo nodo che entra <strong>in</strong> <strong>rete</strong> non posso<br />

<strong>in</strong>debitarsi, senza prima aver spreca<strong>to</strong> <strong>una</strong> certa quantità delle sue risorse. Un<br />

utente novello che volesse acuisire <strong>una</strong> risorsa della <strong>rete</strong>, non avendo nulla con cui<br />

scambiarla, e neppure altri cre<strong>di</strong>ti per procedere a <strong>una</strong> me<strong>di</strong>azione, sprecherebbe,<br />

per esempio, la sua banda, come pegno della sua buona volontà, per comprare la<br />

risorsa desiderata.<br />

È imme<strong>di</strong>a<strong>to</strong> realizzare che la quantità sprecata deve essere<br />

non eccessivamente grande per non precludere l’accesso alla <strong>rete</strong>, nè troppo piccola<br />

per fornire un efficace deterrente ai possibili no<strong>di</strong>-sanguisuga. Ques<strong>to</strong> spreco <strong>di</strong><br />

risorse, chiama<strong>to</strong> appun<strong>to</strong> sacrificio, permette, <strong>in</strong> f<strong>in</strong> dei conti, ad ogni nodo senza<br />

risorse richieste <strong>di</strong> partecipare alla <strong>rete</strong>, proteggendola da utenti mal<strong>in</strong>tenzionati.<br />

Questa visione “a volo d’uccello” su un ambi<strong>to</strong> così critico della nostra <strong>rete</strong>,<br />

verrà comunque ripreso, approfon<strong>di</strong><strong>to</strong> e maggiormente argomenta<strong>to</strong> nella tesi <strong>di</strong><br />

un collega.<br />

4 Cfr. John R. Doceur, The Sybil Attack<br />

14


Capi<strong>to</strong>lo 2<br />

Le componenti<br />

Come precedentamente illustra<strong>to</strong>, per garantire <strong>una</strong> sp<strong>in</strong>ta modularità al client,<br />

abbiamo opta<strong>to</strong> per un’architettura a plug-<strong>in</strong>. A livello <strong>di</strong> struttura possiamo,<br />

qu<strong>in</strong><strong>di</strong>, <strong>in</strong><strong>di</strong>viduare il core, che permette il caricamen<strong>to</strong> dei plug-<strong>in</strong> e le loro<br />

comunicazioni, e i plug-<strong>in</strong> stessi. Esam<strong>in</strong>iamo <strong>in</strong> dettaglio quan<strong>to</strong> f<strong>in</strong>ora espos<strong>to</strong>.<br />

2.1 Core<br />

Il core assolve a due funzioni pr<strong>in</strong>cipali: caricare le classi già compilate che<br />

contengono i plug-<strong>in</strong> e permettere ai plug-<strong>in</strong> <strong>di</strong> comunicare tra loro.<br />

Per concedere la più ampia libertà possibile agli sviluppa<strong>to</strong>ri dei plug-<strong>in</strong> abbia-<br />

mo cerca<strong>to</strong> <strong>di</strong> limitare al massimo le richieste dell’<strong>in</strong>terfaccia. Al momen<strong>to</strong>, non<br />

è ad<strong>di</strong>rittura possibile specificare un <strong>in</strong>terface da<strong>to</strong> che l’unica richiesta str<strong>in</strong>-<br />

gente è un v<strong>in</strong>colo sul costrut<strong>to</strong>re. Al costrut<strong>to</strong>re del plug-<strong>in</strong>, <strong>in</strong>fatti, è necessario<br />

passare come argomen<strong>to</strong> il moni<strong>to</strong>r del core. Se poi, come sembra evidente, il<br />

plug-<strong>in</strong> necessita <strong>di</strong> comunicare con altri plug-<strong>in</strong>, lo sviluppa<strong>to</strong>re portà spe<strong>di</strong>re al<br />

e ricevere dal moni<strong>to</strong>r i messaggi <strong>di</strong> cui ha bisogno.<br />

Il caricamen<strong>to</strong> d<strong>in</strong>amico delle classi già compilate è sta<strong>to</strong> affronta<strong>to</strong> ponendo<br />

delle blande restrizioni sui nomi che le classi possono assumere e usando i package<br />

<strong>di</strong> Java <strong>java</strong>.lang.ClassLoader e <strong>java</strong>.lang.reflect. Il nome che assume la<br />

classe, e qu<strong>in</strong><strong>di</strong> anche il costrut<strong>to</strong>re, è assun<strong>to</strong> come identificativo univoco del<br />

plug-<strong>in</strong> per tut<strong>to</strong> il tempo <strong>in</strong> cui rimane attivo. Viene, qu<strong>in</strong><strong>di</strong>, passata la lista dei<br />

nomi delle classi, che deve caricare, al core. Quest’ultimo carica le classi e <strong>in</strong>voca<br />

i costrut<strong>to</strong>ri passando loro come argomen<strong>to</strong> il moni<strong>to</strong>r.<br />

Il core, al momen<strong>to</strong> della creazione degli oggetti plug-<strong>in</strong>, associa ad ognuno<br />

<strong>una</strong> coda prioritaria. Ogni coda è assegnata ad un plug-<strong>in</strong> e contrad<strong>di</strong>st<strong>in</strong>ta dal<br />

nome del plug-<strong>in</strong> stesso. Il plug-<strong>in</strong>, a ques<strong>to</strong> pun<strong>to</strong>, per comunicare con un suo<br />

15


2. LE COMPONENTI<br />

Figura 2.1: La Struttura del nucleo.<br />

16


2.1 CORE<br />

pari, semplicemente <strong>in</strong>serisce la sua richiesta nella coda del dest<strong>in</strong>atario. L’even-<br />

tuale risposta gli verrà recapitata <strong>di</strong>rettamente nella propria coda. Il ricevente<br />

deve cont<strong>in</strong>uare a fare poll<strong>in</strong>g della propria coda <strong>in</strong> attesa <strong>di</strong> messaggi <strong>di</strong>retti a<br />

lui. Ques<strong>to</strong> ciclo <strong>in</strong>f<strong>in</strong>i<strong>to</strong> tenderebbe a sprecare le risorse del sistema; proprio per<br />

ques<strong>to</strong> abbiamo scel<strong>to</strong> <strong>di</strong> usare la PriorityBlock<strong>in</strong>gQueue. Questa struttura<br />

dati già presente nelle JDK dalla versione 1.5, porge due caratteristiche mol<strong>to</strong> <strong>in</strong>-<br />

teressanti. In primo luogo <strong>in</strong>corpora nella coda già un moni<strong>to</strong>r. Ques<strong>to</strong> permette<br />

al thread, che controlla la coda <strong>in</strong> attesa <strong>di</strong> nuovi messaggi, <strong>di</strong> andare <strong>in</strong> uno sta<strong>to</strong><br />

<strong>di</strong> wait <strong>in</strong> caso <strong>di</strong> coda vuota e <strong>di</strong> non sprecare risorse. In secondo luogo poi, la<br />

coda gestisce un sistema <strong>di</strong> priorità def<strong>in</strong>ibile sulla base <strong>di</strong> un compara<strong>to</strong>re che<br />

abbiamo noi stessi specifica<strong>to</strong>. In ques<strong>to</strong> modo, <strong>in</strong> caso <strong>di</strong> congestione del core, si<br />

può sperare che i messaggi con priorità più alta arriv<strong>in</strong>o comunque a dest<strong>in</strong>azione,<br />

consentendo al core stesso <strong>di</strong> mantenere le sue normali funzionalità.<br />

Da alcune prove che abbiamo esegui<strong>to</strong>, ques<strong>to</strong> sistema sembra comportarsi<br />

secondo le attese. Tuttavia possono sorgere problemi nel caso i plug-<strong>in</strong> produca-<br />

no messaggi a <strong>una</strong> velocità mol<strong>to</strong> maggiore della velocità con cui li consumano.<br />

In tale evenienza, <strong>in</strong>fatti, le code si riempiono e tendono a saturare la memoria<br />

della JVM. Ques<strong>to</strong> comporta il lancio <strong>di</strong> un’eccezione e l’arres<strong>to</strong> <strong>di</strong> tut<strong>to</strong> il core.<br />

Per fronteggiare il problema, si è provvedu<strong>to</strong> a limitare il numero <strong>di</strong> messaggi che<br />

possono essere contemporaneamente <strong>in</strong> <strong>una</strong> coda, scartando gli ulteriori even-<br />

tuali messaggi <strong>in</strong> arrivo. Ques<strong>to</strong> escamotage, che sembra funzionare secondo le<br />

previsioni, è tuttavia ampiamente migliorabile. Sarebbe auspicabile, <strong>in</strong>fatti, un<br />

controllo sulla priorità del pacchet<strong>to</strong> prima <strong>di</strong> decidere se scartarlo, o meno o<br />

anche un approccio <strong>di</strong> tipo RED[2].<br />

Questa struttura con un grosso moni<strong>to</strong>r dota<strong>to</strong> <strong>di</strong> code è sta<strong>to</strong> scel<strong>to</strong> per ov-<br />

viare ad alcune limitazioni <strong>di</strong> Java. Se i plug-<strong>in</strong> avessero comunica<strong>to</strong> <strong>di</strong>rettamente<br />

tra loro senza passare da un moni<strong>to</strong>r, ci sarebbe sta<strong>to</strong> sicuramente un <strong>in</strong>cremen<strong>to</strong><br />

delle prestazioni (ci sarebbe sta<strong>to</strong>, <strong>in</strong>fatti, un passaggio <strong>in</strong> meno). In C/C++ si<br />

sarebbero probabilmente potuti usare i punta<strong>to</strong>ri per affrontare il problema. In<br />

Java ci siamo <strong>in</strong>vece affidati all’uso <strong>di</strong> un moni<strong>to</strong>r opport<strong>una</strong>mente mo<strong>di</strong>fica<strong>to</strong>:<br />

<strong>una</strong> soluzione che permette anche <strong>di</strong> controllare i flussi <strong>di</strong> messaggi tra i plug-<strong>in</strong>.<br />

Ve<strong>di</strong>amo ora cosa sono esattamente questi messaggi. I messaggi trasmessi<br />

tra i vari plug-<strong>in</strong> sono dei pacchetti caratterizzati quasi come dei datagrammi <strong>di</strong><br />

network<strong>in</strong>g. Ognuno <strong>di</strong> questi pacchetti è forma<strong>to</strong> da <strong>di</strong>versi campi che servono<br />

ad <strong>in</strong><strong>di</strong>care la provenienza e la dest<strong>in</strong>azione del pacchet<strong>to</strong>, la priorità e il pay-<br />

lod. Questi pacchetti, che abbiamo chiama<strong>to</strong> cocoon, corrispondono, se vogliamo<br />

cont<strong>in</strong>uare il nostro parallelo col mondo delle reti, a datagrammi ip. Il payload<br />

17


2. LE COMPONENTI<br />

poi si <strong>di</strong>fferenzia a seconda della funzione che ha il messaggio e a seconda del<br />

plug-<strong>in</strong> da cui proviene o a cui è dest<strong>in</strong>a<strong>to</strong>. Se, ad esempio, si tratterà <strong>di</strong> un<br />

cocoon dest<strong>in</strong>a<strong>to</strong> a Connectivity, <strong>in</strong>capsulerà un ogget<strong>to</strong> flux che a sua volta<br />

<strong>in</strong>capsulerà un ogget<strong>to</strong> triplaDati. flux corrisponderebbe qu<strong>in</strong><strong>di</strong> al pro<strong>to</strong>collo<br />

tcp e triplaDati ad un datagramma http.<br />

Figura 2.2: L’<strong>in</strong>capsulamen<strong>to</strong> dei messaggi.<br />

Nel pacchet<strong>to</strong> core sono anche <strong>in</strong>seriti i manager per le risorse. Questi moduli<br />

hanno il compi<strong>to</strong> <strong>di</strong> assegnare e, <strong>in</strong> caso <strong>di</strong> conflit<strong>to</strong>, arbitrare le risorse. Queste<br />

operazioni però, non <strong>di</strong>pendono esclusivamente dal resource manager competen-<br />

te. L’assegnazione, <strong>in</strong>fatti, si basa su un meccanismo che co<strong>in</strong>volge il plug-<strong>in</strong><br />

richiedente e il modulo per la gestione dei cre<strong>di</strong>ti. Il plug-<strong>in</strong> che vuole usufruire,<br />

ad esempio, <strong>di</strong> <strong>una</strong> certa quantità <strong>di</strong> spazio su <strong>di</strong>sco, deve, <strong>in</strong>fatti, richiederne<br />

l’uso al ges<strong>to</strong>re locale ma deve anche “pagare” al modulo per la gestione dei cre-<br />

<strong>di</strong>ti. La transazione tende a complicarsi nel caso <strong>in</strong> cui è un altro nodo della <strong>rete</strong><br />

a <strong>in</strong>terrogare il client locale per usarne lo spazio. Infatti, il nodo remo<strong>to</strong>, deve<br />

prendere contat<strong>to</strong> col plug-<strong>in</strong> locale per controllare la <strong>di</strong>sponibilità del servizio,<br />

e, poi, deve <strong>in</strong>teragire col modulo <strong>di</strong> gestione cre<strong>di</strong>ti per accordarsi sul prezzo.<br />

In locale, il plug-<strong>in</strong> accetta <strong>di</strong> fornire il servizio al nodo remo<strong>to</strong> previa verifica<br />

<strong>di</strong> pagamen<strong>to</strong> presso il modulo gestione cre<strong>di</strong>ti. Successivamente si fa carico <strong>di</strong><br />

chiedere l’allocazione della risorsa presso il ges<strong>to</strong>re della risorsa competente. Il<br />

ges<strong>to</strong>re dovrà qu<strong>in</strong><strong>di</strong>, oltre ad accettare le richieste dei plug-<strong>in</strong>, controllare pre-<br />

18


2.1 CORE<br />

ventivamente la possibilità <strong>di</strong> assegnare risorse. Tornando all’esempio dello spazio<br />

su <strong>di</strong>sco, prima <strong>di</strong> prendere <strong>in</strong> carico i dati da salvare, il ges<strong>to</strong>re deve assicurarsi<br />

<strong>di</strong> avere abbastanza byte liberi da utilizzare.<br />

Lo scenario si arricchisce pensando a quante e quali <strong>di</strong>verse possibili risorse<br />

possono venire trattare dalla <strong>rete</strong>. Forniamo qui un elenco <strong>di</strong> risorse previste (e<br />

<strong>di</strong> loro caratteristiche).<br />

• Spazio su <strong>di</strong>sco;<br />

• cicli macch<strong>in</strong>a;<br />

• connettività:<br />

– velocità;<br />

– latenza;<br />

– assenza <strong>di</strong> jitter.<br />

Da queste risorse, che possono essere sfruttare dai vari plug-<strong>in</strong>, è possibile ricavare<br />

<strong>di</strong>versi servizi. Si pensi, ad esempio, come un servizio <strong>di</strong> web host<strong>in</strong>g faccia uso<br />

contemporaneamente <strong>di</strong> spazio su <strong>di</strong>sco e connettività. In ques<strong>to</strong> modo, all’utente,<br />

è rischies<strong>to</strong> <strong>di</strong> gestire servizi f<strong>in</strong>iti, mentre la scomposizione <strong>di</strong> questi <strong>in</strong> risorse è<br />

lasciata ai plug-<strong>in</strong> e ai ges<strong>to</strong>ri delle risorse.<br />

Passiamo ora a illustrare i ges<strong>to</strong>ri delle risorse attualmente <strong>in</strong>clusi nel core.<br />

2.1.1 S<strong>to</strong>rage<br />

Come già det<strong>to</strong> precedentemente, i ges<strong>to</strong>ri, hanno la medesima struttura dei plug-<br />

<strong>in</strong>, si <strong>di</strong>fferenziano da essi solo per la loro funzione. I ges<strong>to</strong>ri, <strong>in</strong>fatti, sono il<br />

tramite tra il core, e i suoi plug-<strong>in</strong>, e le risorse della macch<strong>in</strong>a su cui gira il client.<br />

Essi, <strong>in</strong>fatti, amm<strong>in</strong>istrano spazio su <strong>di</strong>sco, banda e, nel futuro, cicli macch<strong>in</strong>a.<br />

dataS<strong>to</strong>rage è il ges<strong>to</strong>re che sovra<strong>in</strong>tende lo spazio su <strong>di</strong>sco. Attualmente pre-<br />

senta <strong>una</strong> struttura piut<strong>to</strong>s<strong>to</strong> semplice, perché gestisce solamente qualche opera-<br />

zione su file <strong>in</strong> locale. Ques<strong>to</strong> ges<strong>to</strong>re gestisce file <strong>in</strong>teri tramite un handler <strong>di</strong><br />

Java. In ques<strong>to</strong> modo il passaggio <strong>di</strong> un file da <strong>una</strong> parte all’altra del client non<br />

comporta nessun accesso al <strong>di</strong>sco, elim<strong>in</strong>ando uno dei possibili colli <strong>di</strong> bottiglia del<br />

client. Oltre file <strong>in</strong>teri dataS<strong>to</strong>rage lavora anche su pezzi <strong>di</strong> file, chiamati chunk.<br />

dataS<strong>to</strong>rage può ricevere da un plug-<strong>in</strong> dei chunk, senza che questi seguano ne-<br />

cessariamente un ord<strong>in</strong>e, e riassemblare il file, mantendo opzionalmente un cer<strong>to</strong><br />

controllo nella ricostruzione. Ques<strong>to</strong> ges<strong>to</strong>re, <strong>in</strong>fatti, può controllare che i chunk<br />

19


2. LE COMPONENTI<br />

non <strong>di</strong>ano orig<strong>in</strong>e a deleteri fenomeni <strong>di</strong> overlapp<strong>in</strong>g. D’altra parte dataS<strong>to</strong>rage<br />

può <strong>in</strong>viare chunk su richiesta.<br />

Il vero pun<strong>to</strong> <strong>di</strong> forza <strong>di</strong> ques<strong>to</strong> ges<strong>to</strong>re è però da cercarsi non nelle sue attuali<br />

capacità, ma nei suoi sviluppi futuri. Infatti <strong>in</strong>serire dataS<strong>to</strong>rage tra il <strong>di</strong>sco loca-<br />

le e il plug-<strong>in</strong> equivale alla creazione <strong>di</strong> un nuovo layer <strong>in</strong>terme<strong>di</strong>o. Ques<strong>to</strong> layer<br />

permette ai plug-<strong>in</strong> <strong>di</strong> non dovere cambiare i loro meccanismi <strong>in</strong>terni <strong>di</strong> s<strong>to</strong>r<strong>in</strong>g,<br />

qualora cambiasse la natura del salvataggio. Quando sarà pronta l’<strong>in</strong>frastruttura<br />

per il salvataggio <strong>di</strong> dati su <strong>di</strong>versi no<strong>di</strong> della <strong>rete</strong>, il plug-<strong>in</strong> che vorrà sfruttare<br />

questa nuova feature dovrà semplicemente cambiare il valore <strong>di</strong> un parametro nel<br />

messaggio <strong>in</strong>via<strong>to</strong> a dataS<strong>to</strong>rage. D’altra parte, grazie alla <strong>progettazione</strong> modu-<br />

lare del ges<strong>to</strong>re stesso, l’aggiornamen<strong>to</strong> con la nuova caratteristica comporterà la<br />

riscrittura <strong>di</strong> solo un paio <strong>di</strong> righe <strong>di</strong> co<strong>di</strong>ce.<br />

Erasure cod<strong>in</strong>g<br />

Ques<strong>to</strong>, che è uno degli aspetti più <strong>in</strong>novativi <strong>di</strong> tut<strong>to</strong> il proget<strong>to</strong>, verrà qui solo<br />

accenna<strong>to</strong> senza alc<strong>una</strong> p<strong>rete</strong>sa <strong>di</strong> completezza. Sarà, <strong>in</strong>fatti, espos<strong>to</strong> nella tesi <strong>di</strong><br />

Federico Sogaro. Scopo dell’erasure cod<strong>in</strong>g è mo<strong>di</strong>ficare un file aggiungendo un<br />

piccolo overhead <strong>in</strong> modo tale che <strong>in</strong> caso <strong>di</strong> irrecuperabilità <strong>di</strong> alcune sue parti il<br />

file sia comunque ricostruibile. Uno schema <strong>di</strong> funzionamen<strong>to</strong> possibile, anche se<br />

descrit<strong>to</strong> <strong>in</strong> maniera volutamente mol<strong>to</strong> semplificata su un ipotetico file da 100<br />

MB, segue.<br />

1. Il file viene opport<strong>una</strong>mente mo<strong>di</strong>fica<strong>to</strong> aggiungendo un 10% <strong>di</strong> overhead<br />

arrivando ad occupare 110 MB;<br />

2. il file viene scompos<strong>to</strong> il 110 pacchetti da 1 MB l’uno;<br />

3. del file vengono recuperati 100 pacchetti qualsiasi;<br />

4. tramite opportune manipolazione si ricostruisce il file <strong>in</strong>iziale.<br />

Ques<strong>to</strong> sistema permette, con relativamente poco overhead, e con un’efficien-<br />

za mol<strong>to</strong> maggiore della semplice ridondanza, il backup <strong>di</strong> grossi file sulla <strong>rete</strong>.<br />

Esis<strong>to</strong>no, <strong>in</strong>oltre, alcune sue versioni mo<strong>di</strong>ficate, chiamate <strong>di</strong>gital fouta<strong>in</strong> la cui<br />

vocazione è la trasmissione e non lo s<strong>to</strong>rage. Le <strong>di</strong>gital fouta<strong>in</strong>, <strong>in</strong>fatti, applicano<br />

il pro<strong>to</strong>collo precedentemente descrit<strong>to</strong> per scomporre il file e poi procedono a<br />

spe<strong>di</strong>re i pacchetti su pro<strong>to</strong>colli veloci, ma <strong>in</strong>affidabili, sulla <strong>rete</strong>, senza aspettare<br />

mai ness<strong>una</strong> conferma <strong>in</strong>terme<strong>di</strong>a. I riceventi qu<strong>in</strong><strong>di</strong> recuperano <strong>una</strong> certa per-<br />

centuale <strong>di</strong> pacchetti; quando ne hanno ricevuti abbastanza per poter ricostruire<br />

20


2.1 CORE<br />

il file, mandano un segnale <strong>di</strong> s<strong>to</strong>p al mittente. Quando il mittente riceve tutti i<br />

segnali <strong>di</strong> s<strong>to</strong>p smette <strong>di</strong> erogare pacchetti. Il risulta<strong>to</strong> probabilmente più signifi-<br />

cativo è che la trasmissione tramite <strong>di</strong>gital fouta<strong>in</strong> su udp è più efficiente che la<br />

trasmissione semplice su tcp.<br />

Test eseguiti con le <strong>di</strong>gital fouta<strong>in</strong> hanno <strong>di</strong>mostra<strong>to</strong> <strong>di</strong> poter otte-<br />

nere velocità <strong>di</strong> trasmissione dati f<strong>in</strong>o a 10 volte maggiore rispet<strong>to</strong><br />

alle trasmissioni su tcp (con<strong>di</strong>zioni con latenze elevate, come tra-<br />

smissioni tra cont<strong>in</strong>enti <strong>di</strong>versi e per<strong>di</strong>ta dei pacchetti dell’1%) Gli<br />

erasure cod<strong>in</strong>g devono tuttavia usare erasure channel , cioè canali <strong>di</strong><br />

trasmissione dove i dati quando arrivano a dest<strong>in</strong>azioni sono sicura-<br />

mente corretti. Sono qu<strong>in</strong><strong>di</strong> necessari co<strong>di</strong>ci <strong>di</strong> <strong>in</strong><strong>di</strong>viduazioni degli<br />

errori per correggere ma pr<strong>in</strong>cipalmente scartare i pacchetti errati 1<br />

(udp checksum).<br />

Purtroppo molte <strong>di</strong> queste tecnologie stu<strong>di</strong>ate negli U.S.A. risultano essere<br />

già brevettate e qu<strong>in</strong><strong>di</strong> <strong>in</strong>compatibili con la licenza GPL da noi scelta. Tuttavia,<br />

almeno per ora, possiamo usare queste tecnologie <strong>in</strong> ambi<strong>to</strong> europeo grazie al<br />

vo<strong>to</strong> contrario alla brevettabilità del software del Parlamen<strong>to</strong> Europeo 2 .<br />

2.1.2 Connettività<br />

Ques<strong>to</strong> secondo ges<strong>to</strong>re presiede alla trasmissione e alla ricezione <strong>di</strong> byte. Il<br />

package permette l’<strong>in</strong>vio e la ricezione dati su pro<strong>to</strong>collo udp e tcp. Scendere a<br />

livello del pro<strong>to</strong>collo ip non è purtroppo sta<strong>to</strong> possibile da<strong>to</strong> che <strong>java</strong> non prevede<br />

nativamente questa possibilità. A onor del vero usando alcune librerie come le<br />

jpcap sarebbe sta<strong>to</strong> possibile manipolare i pacchetti ip. Ques<strong>to</strong> risulta<strong>to</strong> sarebbe<br />

arriva<strong>to</strong> però ad un cos<strong>to</strong> troppo al<strong>to</strong>. Infatti le jpcap sono solo un wrapper<br />

at<strong>to</strong>rno alle celeberrime pcap (scritte <strong>in</strong> C). Usarle si sarebbe tradot<strong>to</strong> <strong>in</strong>:<br />

• <strong>in</strong>stallare ulteriori librerie presso l’utente;<br />

• permettere l’esecuzione del client solo con privilegi <strong>di</strong> root.<br />

È chiaro che sono due con<strong>di</strong>zioni che avrebbero troppo pesa<strong>to</strong> sull’utente f<strong>in</strong>ale.<br />

I plug-<strong>in</strong> che desiderano ricevere pacchetti dall’esterno, al loro avvio, preno-<br />

tano <strong>una</strong> porta. Il ges<strong>to</strong>re <strong>in</strong>oltrerà loro tut<strong>to</strong> quello che arriverà su quella porta<br />

correda<strong>to</strong> con <strong>in</strong>formazioni ausiliarie come la porta e l’<strong>in</strong><strong>di</strong>rizzo ip <strong>di</strong> partenza.<br />

1 Cfr. http://sgharea.dyndns.org/me<strong>di</strong>awiki/<strong>in</strong>dex.php/Distributed S<strong>to</strong>rage<br />

2 Cfr. http://pun<strong>to</strong>-<strong>in</strong>formatico.it/p.asp?i=53935<br />

21


2. LE COMPONENTI<br />

Nel caso <strong>di</strong> comunicazioni su tcp, il plug-<strong>in</strong> che riceve il datagramma può spe<strong>di</strong>re<br />

la risposta sullo stesso socket su cui è arriva<strong>to</strong> il datagramma stesso.Il problema<br />

non si pone per comunicazioni udp o s<strong>in</strong>gole tcp.<br />

Connectivity verrà pres<strong>to</strong> migliora<strong>to</strong> aggiungendo la possibilità <strong>di</strong> limitare la<br />

banda da usare. Inoltre, nel momen<strong>to</strong> <strong>in</strong> cui il modulo <strong>di</strong> anonima<strong>to</strong> risulterà<br />

pron<strong>to</strong>, l’<strong>in</strong>frastruttura è <strong>di</strong>segnata per accoglierlo senza cambiare praticamente<br />

nulla nel co<strong>di</strong>ce. Ai plug-<strong>in</strong>, qu<strong>in</strong><strong>di</strong>, basterà cambiare un campo nei messaggi<br />

<strong>in</strong>viati a ques<strong>to</strong> ges<strong>to</strong>re per avvalersi <strong>di</strong> queste due nuove funzionalità.<br />

2.2 Kademlia<br />

Kademlia, come già annuncia<strong>to</strong>, è il pro<strong>to</strong>collo <strong>di</strong> ricerca su cui si basa PariPari<br />

per essere completamente serverless. Inizialmente, abbiamo cerca<strong>to</strong> <strong>di</strong> imple-<br />

mentare quan<strong>to</strong> descrit<strong>to</strong> nel paper <strong>di</strong> Petar Maymounkov e David Mazi‘eres [1]<br />

quan<strong>to</strong> più fedelemente possibile.<br />

Con lettera maiuscola vengono <strong>in</strong><strong>di</strong>cati i no<strong>di</strong>;<br />

Esempio: A, B, C.<br />

Con lettera maiuscola corsiva vengono <strong>in</strong><strong>di</strong>cate le risorse;<br />

Esempio: A, B, C.<br />

X,Y <strong>in</strong><strong>di</strong>cano il nodo generico;<br />

X , Y <strong>in</strong><strong>di</strong>cano l’hash generico;<br />

IPC <strong>in</strong><strong>di</strong>ca le <strong>in</strong>formazioni utili a contattare C.<br />

IDX = ID del nodo X.<br />

hashB = hash della risorsa B.<br />

Tabella 2.1: Pseudoco<strong>di</strong>ce: convenzioni<br />

Ripassiamo ora i pr<strong>in</strong>cipi card<strong>in</strong>e del funzionamen<strong>to</strong> <strong>di</strong> Kademlia. Kademlia<br />

basa il suo funzionamen<strong>to</strong> sulla metrica XOR. Ogni nodo è contrassegna<strong>to</strong> da un<br />

co<strong>di</strong>ce identificativo univoco (d’ora <strong>in</strong> poi semplicemente ID). Ogni risorsa gestita<br />

dalla <strong>rete</strong> è pure contrad<strong>di</strong>st<strong>in</strong>ta da un co<strong>di</strong>ce (d’ora <strong>in</strong> poi semplicmente hash)<br />

appartenente allo stesso spazio <strong>di</strong> ID. Ogni nodo che entra nella <strong>rete</strong> assume,<br />

qu<strong>in</strong><strong>di</strong>, un ID e calcola l’hash <strong>di</strong> tutte le sue risorse da con<strong>di</strong>videre. Kademlia<br />

calcola le <strong>di</strong>stanze, <strong>in</strong>terpretando come un <strong>in</strong>tero lo XOR bit a bit dei due ID (o<br />

dei due hash).<br />

d(A, B) = IDA ⊕ IDB;<br />

22


2.2 KADEMLIA<br />

Successivamente il nodo contatta i no<strong>di</strong> i cui ID sono mol<strong>to</strong> vic<strong>in</strong>i all’hash delle<br />

sue risorse e comunica loro le coord<strong>in</strong>ate per essere raggiun<strong>to</strong>. In ques<strong>to</strong> modo<br />

ogni hash viene assegna<strong>to</strong> al nodo che più gli è vic<strong>in</strong>o e la ricerca <strong>di</strong> <strong>una</strong> risorsa si<br />

riduce alla ricerca <strong>di</strong> un nodo della <strong>rete</strong>. Il nodo che vuole cercare un altro nodo<br />

nella <strong>rete</strong> conoscendone l’ID contatta i no<strong>di</strong> che già conosce richiedendo <strong>in</strong>for-<br />

mazioni sull’ogget<strong>to</strong> della sua ricerca. I no<strong>di</strong> <strong>in</strong>terrogati rispondono fornendogli<br />

l’elenco dei no<strong>di</strong> più vic<strong>in</strong>i <strong>di</strong> cui loro hanno notizia. Successivamente, il nodo<br />

cerca<strong>to</strong>re, iterativamente, <strong>in</strong>terrogherà dalla lista dei no<strong>di</strong> ricevuti i più vic<strong>in</strong>i a<br />

quello cerca<strong>to</strong> f<strong>in</strong>chè ques<strong>to</strong> ciclo non lo condurrà a contattare il nodo che voleva<br />

trovare (ve<strong>di</strong> 2.2).<br />

A cerca Z.<br />

1. A per tutti i no<strong>di</strong> che conosce calcola le <strong>di</strong>stanze da Z:<br />

d(Z, Xi) = IDZ ⊕ IDXi ;<br />

2. A ord<strong>in</strong>a le <strong>di</strong>stanze:<br />

sort d(Z, Xi)<br />

3. A sceglie le <strong>di</strong>stanze m<strong>in</strong>ori<br />

4. A <strong>in</strong>terroga i no<strong>di</strong> corrispondenti.<br />

asks Xi<br />

5. ogni nodo <strong>in</strong>terroga<strong>to</strong> esegue le operazioni 1, 2 e 3.<br />

6. ogni nodo <strong>in</strong>terroga<strong>to</strong> <strong>in</strong>via ad A i suoi risultati:<br />

Xi sends <strong>to</strong> A IPYi<br />

7. A <strong>in</strong>terroga i nuovi no<strong>di</strong> più vic<strong>in</strong>i <strong>di</strong> cui ha ricevu<strong>to</strong> le <strong>in</strong>formazioni:<br />

A asks Yi<br />

8. A ripete questa procedura f<strong>in</strong>o al r<strong>in</strong>venimen<strong>to</strong> <strong>di</strong> IPZ<br />

Tabella 2.2: Pseudoco<strong>di</strong>ce: la ricerca <strong>in</strong> Kademlia<br />

Volendo si può visualizzare ques<strong>to</strong> schema <strong>di</strong> lavoro come <strong>una</strong> <strong>di</strong>scesa lungo un<br />

albero b<strong>in</strong>ario <strong>in</strong> cui le foglie corrispondono ai no<strong>di</strong> della <strong>rete</strong>. Ad ogni sal<strong>to</strong> della<br />

ricerca si procede verso il basso escludendo mezzo sot<strong>to</strong>-albero f<strong>in</strong>o ad arrivare<br />

alla foglia cercata.<br />

Per realizzare ques<strong>to</strong> sistema abbiamo procedu<strong>to</strong> per fasi. Abbiamo prima<br />

<strong>in</strong><strong>di</strong>vidua<strong>to</strong> la struttura <strong>in</strong>terna che avrebbe dovu<strong>to</strong> avere il plug-<strong>in</strong> per poter sal-<br />

vare, come descrit<strong>to</strong> <strong>in</strong> [1], le <strong>in</strong>formazioni riguardo i no<strong>di</strong> con cui sarebbe entra<strong>to</strong><br />

23


2. LE COMPONENTI<br />

00<br />

0 1<br />

01<br />

000 001 010 011<br />

10<br />

11<br />

100 101 110 111<br />

Figura 2.3: Albero <strong>di</strong> Kademlia: ad ogni sal<strong>to</strong> durante la <strong>di</strong>scesa si elim<strong>in</strong>a mezzo<br />

sot<strong>to</strong>-albero.<br />

<strong>in</strong> contat<strong>to</strong>. Successivamente si è focalizzata l’attenzione sulle comunicazioni che<br />

sarebbero <strong>in</strong>tercorse tra i vari no<strong>di</strong> della <strong>rete</strong>. Sono stati <strong>in</strong><strong>di</strong>viduati quattro RPC<br />

pr<strong>in</strong>cipali:<br />

• P<strong>in</strong>g;<br />

• F<strong>in</strong>d Value;<br />

• F<strong>in</strong>d Node;<br />

• S<strong>to</strong>re.<br />

Abbiamo, qu<strong>in</strong><strong>di</strong>, provvedu<strong>to</strong> a def<strong>in</strong>ire i datagrammi per contenre questi RPC e<br />

le relative risposte. Si è procedu<strong>to</strong> a ques<strong>to</strong> lavoro, tenendo sempre <strong>in</strong> mente la<br />

scalabilità della struttura. La parametrizzazione pressochè completa del co<strong>di</strong>ce<br />

permette, <strong>in</strong>fatti, <strong>di</strong> poter cambiare al volo la lunghezza <strong>di</strong> hash e ID e, ad<strong>di</strong>rit-<br />

tura, la possbilità <strong>di</strong> cambiare la metrica <strong>di</strong> misura delle <strong>di</strong>stanze. Inoltre è ora<br />

estremamente semplice poter aggiungere nuove funzionalità a Kademlia proprio<br />

perché non tutti i bit dei datagrammi spe<strong>di</strong>ti (e ricevuti) sono completamente<br />

usati. Abbiamo preferi<strong>to</strong> sacrificare un po’ <strong>di</strong> velocità <strong>di</strong> trasmissione pur <strong>di</strong><br />

mantnere facilmente aggiornabile il pro<strong>to</strong>collo.<br />

Term<strong>in</strong>ata l’architettura <strong>di</strong> base abbiamo, <strong>in</strong><strong>di</strong>vidua<strong>to</strong> due possibili miglio-<br />

ramenti. Col modus operan<strong>di</strong> sopra descrit<strong>to</strong>, ogni volta che il nodo cerca<strong>to</strong>re<br />

24


2.2 KADEMLIA<br />

<strong>in</strong>terroga un suo pari, prima <strong>di</strong> riprendere la ricerca deve aspettare la risposta.<br />

Petar Maymounkov e David Mazi‘eres suggeriscono comunque <strong>di</strong> non aspettare<br />

l’arrivo <strong>di</strong> tutte le risposte ma <strong>di</strong> <strong>in</strong>iziare subi<strong>to</strong> a sondare i nuovi no<strong>di</strong> per rispar-<br />

miare tempo. Tuttavia, è nostra <strong>in</strong>tenzione, usare Kademlia anche per fornire<br />

serivizi <strong>in</strong> cui è <strong>in</strong><strong>di</strong>spensabile <strong>una</strong> latenza mol<strong>to</strong> bassa. Per ottenere prestazioni<br />

ancora migliori abbiamo, allora, pensa<strong>to</strong> <strong>di</strong> <strong>in</strong>trodurre <strong>una</strong> nuova funzionalità. Il<br />

primo nodo, <strong>in</strong>fatti, che chiameremo orig<strong>in</strong>e, oltre a comportarsi come descrit<strong>to</strong><br />

<strong>in</strong> [1], sceglie, tra i no<strong>di</strong> che deve contattare, un pre<strong>di</strong>let<strong>to</strong>. Ques<strong>to</strong> pre<strong>di</strong>let<strong>to</strong><br />

<strong>in</strong>oltrerà <strong>di</strong>rettamente la richiesta <strong>di</strong> ricerca a uno dei no<strong>di</strong> che sta per trasmet-<br />

tere come risposta all’orig<strong>in</strong>e. Ques<strong>to</strong> avviene iterativamente ad ogni sal<strong>to</strong> della<br />

ricerca: il pre<strong>di</strong>let<strong>to</strong> <strong>di</strong> prima generazione sceglie un pre<strong>di</strong>let<strong>to</strong> tra i suoi no<strong>di</strong>,<br />

mentre trasmette la propria risposta <strong>di</strong>rettamente all’orig<strong>in</strong>e. In ques<strong>to</strong> modo,<br />

nel caso la catena <strong>di</strong> pre<strong>di</strong>letti non si <strong>in</strong>terrompa, si arriva quasi a <strong>di</strong>mezzare il<br />

tempo <strong>di</strong> ricerca (ve<strong>di</strong> 2.3).<br />

Supponiamo, <strong>in</strong>fatti, che tra l’orig<strong>in</strong>e e il nodo cerca<strong>to</strong> ci siano 7 salti da<br />

fare. Col me<strong>to</strong>do standard sarebbero necessari 12 comunicazioni per permettere<br />

all’orig<strong>in</strong>e <strong>di</strong> conoscere l’<strong>in</strong><strong>di</strong>rizzo del nodo cerca<strong>to</strong>.<br />

comunicazioni = (<strong>di</strong>stanza − 1) · 2<br />

Invece, col nuovo sistema, <strong>in</strong> caso <strong>di</strong> successo, bastano 6 comunicazioni.<br />

comunicazioni = <strong>di</strong>stanza − 1<br />

Assumendo che tutte le comunicazioni abbiano uguale durata, si avrebbe un<br />

risparmio del 50% <strong>in</strong> term<strong>in</strong>i <strong>di</strong> tempo. In caso <strong>di</strong> <strong>in</strong>successo, d’altra parte, le pre-<br />

stazioni non verrebbero assolutamente variate da<strong>to</strong> che la ricerca cont<strong>in</strong>uerebbe<br />

<strong>in</strong> parallelo seguendo il me<strong>to</strong>do standard.<br />

Come scegliere il pre<strong>di</strong>let<strong>to</strong>? La risposta più imme<strong>di</strong>ata potrebbe essere sce-<br />

gliere un nodo a caso, ma ques<strong>to</strong> non porterebbe a nessun risulta<strong>to</strong> preve<strong>di</strong>bile.<br />

Da<strong>to</strong> che ques<strong>to</strong> miglioramen<strong>to</strong> è sta<strong>to</strong> concepi<strong>to</strong> per accelerare la ricerca, abbia-<br />

mo scel<strong>to</strong> <strong>di</strong> eleggere come pre<strong>di</strong>let<strong>to</strong> il nodo il cui ID è più vic<strong>in</strong>o all’ID del nodo<br />

cerca<strong>to</strong>. Un altro approccio potrebbe <strong>in</strong>vece cercare <strong>di</strong> privilegiare il buon esi<strong>to</strong><br />

della ricerca piut<strong>to</strong>s<strong>to</strong> che puramente la velocità. Petar Maymounkov e David<br />

Mazi‘eres, stu<strong>di</strong>ando la <strong>rete</strong> Gnutella 3 , hanno scoper<strong>to</strong> che statisticamente i no<strong>di</strong><br />

che rimangono on-l<strong>in</strong>e a lungo hanno molte probabilità <strong>di</strong> rimanere attivi ancora<br />

mol<strong>to</strong> tempo. Sulla scorta <strong>di</strong> ques<strong>to</strong> risulta<strong>to</strong> si potrebbe scegliere come elet<strong>to</strong> il<br />

nodo che è attivo da più tempo.<br />

3 Cfr. [9]<br />

25


2. LE COMPONENTI<br />

A cerca Z col sistema del pre<strong>di</strong>let<strong>to</strong>.<br />

1. A per tutti i no<strong>di</strong> che conosce calcola le <strong>di</strong>stanze da Z:<br />

d(Z, Xi) = IDZ ⊕ IDXi ;<br />

2. A ord<strong>in</strong>a le <strong>di</strong>stanze:<br />

sort d(Z, Xi)<br />

3. A sceglie le <strong>di</strong>stanze m<strong>in</strong>ori<br />

4. A sceglie tra i no<strong>di</strong> con <strong>di</strong>stanza m<strong>in</strong>ore il suo pre<strong>di</strong>let<strong>to</strong>:D.<br />

5. A <strong>in</strong>terroga i no<strong>di</strong> con <strong>di</strong>stanze m<strong>in</strong>ori e chiede a D <strong>di</strong> scegliere un<br />

pre<strong>di</strong>let<strong>to</strong>.<br />

A asks Xi<br />

A asks as favorite D.<br />

6. ogni nodo <strong>in</strong>terroga<strong>to</strong> esegue le operazioni 1, 2 e 3.<br />

7. D sceglie un suo pre<strong>di</strong>let<strong>to</strong>,F ,tra i no<strong>di</strong> selezionati e lo <strong>in</strong>terroga<br />

<strong>di</strong>rettamente:<br />

D asks as favorite F.<br />

8. ogni nodo <strong>in</strong>terroga<strong>to</strong> <strong>in</strong>via ad A i suoi risultati:<br />

Xi sends <strong>to</strong> A IPYi<br />

9. A <strong>in</strong>terroga i nuovi no<strong>di</strong> più vic<strong>in</strong>i <strong>di</strong> cui ha ricevu<strong>to</strong> le <strong>in</strong>formazioni:<br />

A asks Yi<br />

10. A ripete questa procedura f<strong>in</strong>o al r<strong>in</strong>venimen<strong>to</strong> <strong>di</strong> IPZ<br />

Tabella 2.3: Pseudoco<strong>di</strong>ce: la ricerca <strong>in</strong> Kademlia col sistema del pre<strong>di</strong>let<strong>to</strong>.<br />

26


Figura 2.4: Il sistema del pre<strong>di</strong>let<strong>to</strong>.<br />

27<br />

2.2 KADEMLIA


2. LE COMPONENTI<br />

Quest’ultima scelta però non assicura ancora che la ricerca tramite pre<strong>di</strong>let<strong>to</strong><br />

term<strong>in</strong>i con successo. Sarebbe più efficace che ogni nodo eleggesse un nume-<br />

ro costante <strong>di</strong> pre<strong>di</strong>letti; quest’approccio tuttavia comporterebbe l’esplosione del<br />

problema <strong>in</strong>ondando la <strong>rete</strong> <strong>di</strong> pre<strong>di</strong>letti e, qu<strong>in</strong><strong>di</strong>, <strong>di</strong> comunicazioni. Sarebbe<br />

preferibile riuscire ad utilizzare più <strong>di</strong> <strong>una</strong> catena <strong>di</strong> pre<strong>di</strong>letti. Ques<strong>to</strong> garan-<br />

tirebbe <strong>una</strong> certa parsimonia nelle comunicazioni e <strong>una</strong> probabilità maggiore <strong>di</strong><br />

arrivare velocemente al nodo cerca<strong>to</strong>. Ancora migliore sarebbe la possibilità <strong>di</strong><br />

far <strong>in</strong>teragire le <strong>di</strong>verse catene <strong>di</strong> pre<strong>di</strong>letti <strong>in</strong> modo che il loro numero sia sempre<br />

costante. Nel caso <strong>una</strong> <strong>di</strong> queste arrivasse <strong>in</strong> un vicolo cieco potrebbe rigenerarsi<br />

a partire da un nodo <strong>di</strong>verso forni<strong>to</strong> da un’altra catena. Questi, appena elencati,<br />

sono i possibili miglioramenti che verranno nel prossimo futuro stu<strong>di</strong>ati e, qu<strong>in</strong><strong>di</strong>,<br />

applicati al proget<strong>to</strong>.<br />

2.3 Altri plug-<strong>in</strong><br />

Al momen<strong>to</strong> sono <strong>in</strong> lavorazione <strong>di</strong>versi altri plug-<strong>in</strong>:<br />

prima:<br />

• web server (http);<br />

• e-mail (smtp, pop3);<br />

• host resolution (dns);<br />

• file shar<strong>in</strong>g (aMule).<br />

Ci sono poi <strong>una</strong> serie <strong>di</strong> altri plug-<strong>in</strong> cui si procederà alla <strong>realizzazione</strong> quan<strong>to</strong><br />

• database <strong>di</strong>stribui<strong>to</strong> (DBMS);<br />

• newsgroup (nntp);<br />

• chat (irc);<br />

2.3.1 Web server (http)<br />

Ques<strong>to</strong> modulo permette la pubblicazione <strong>di</strong> pag<strong>in</strong>e web su <strong>in</strong>ternet. Si rifà<br />

all’RFC 2616 e qu<strong>in</strong><strong>di</strong> implementa il pro<strong>to</strong>collo http 1.1. La sua peculiarità<br />

è quella <strong>di</strong> sfruttare pesantemente l’<strong>in</strong>frastruttura del core e qu<strong>in</strong><strong>di</strong> att<strong>in</strong>gere ai<br />

file che deve ospitare tan<strong>to</strong> dal <strong>di</strong>sco locale, quan<strong>to</strong> dal <strong>di</strong>sco <strong>di</strong> altri client. Di<br />

basilare importanza sarà l’utilizzazione <strong>di</strong> politiche de<strong>di</strong>te alla m<strong>in</strong>imizzazione dei<br />

28


2.3 ALTRI PLUG-IN<br />

tempi <strong>di</strong> latenza. Alcune <strong>di</strong> queste strategie potrebbero essere la pre<strong>di</strong>zione delle<br />

pag<strong>in</strong>e da caricare e il cach<strong>in</strong>g locale <strong>di</strong> quest ultime.<br />

2.3.2 E-mail (smtp, pop3)<br />

Lo sviluppo <strong>di</strong> ques<strong>to</strong> modulo segue RFC 1939, RFC 2821 e RFC 2822. Dei due<br />

server, quello concettulamente più complesso da realizzare <strong>in</strong> modo <strong>di</strong>stribui<strong>to</strong> è<br />

il server pop3. Il problema è, <strong>in</strong>fatti, quello <strong>di</strong> <strong>di</strong>sperdere le e-mail per la <strong>rete</strong><br />

<strong>in</strong> modo che siano sempre <strong>di</strong>sponibili qualora il dest<strong>in</strong>atario volesse recuperarle,<br />

senza dover <strong>di</strong>pendere dallo sta<strong>to</strong> <strong>di</strong> un unico nodo.<br />

È ovvio, però, che le copie<br />

delle e-mail dovranno comunque essere s<strong>in</strong>cronizzate tra <strong>di</strong> loro. Per semplificare<br />

leggermente ques<strong>to</strong> problema, si è opta<strong>to</strong> per salvare le e-mail col sistema mail<strong>di</strong>r<br />

(<strong>una</strong> mail, un file). Quando sarà completa<strong>to</strong> il modulo DBMS, il sistema <strong>di</strong><br />

gestione delle e-mail verrà re-implementa<strong>to</strong> avvalendosi <strong>di</strong> ques<strong>to</strong> nuovo modulo.<br />

Un altro grosso problema affligge, <strong>in</strong>vece, il server smtp. Tutti i maggiori<br />

server <strong>di</strong> posta mon<strong>di</strong>ali, <strong>in</strong>fatti, non accettano il relay<strong>in</strong>g <strong>di</strong> posta da client con<br />

<strong>in</strong><strong>di</strong>rizzo ip d<strong>in</strong>amico. Chiaramente, la stragrande maggioranza dei no<strong>di</strong> della<br />

<strong>rete</strong>, nonostante probabili tempi <strong>di</strong> uptime elevati, avranno ip d<strong>in</strong>amico. Una<br />

soluzione per ora solo abbozzata sarebbe il <strong>di</strong>rottamen<strong>to</strong> della posta fuori da<br />

PariPari solo attraverso host con <strong>in</strong><strong>di</strong>rizzo statico.<br />

ip d<strong>in</strong>amico<br />

stmp.gmail.com<br />

ip d<strong>in</strong>amico<br />

ip statico<br />

ip d<strong>in</strong>amico<br />

Figura 2.5: Dirottamen<strong>to</strong> <strong>di</strong> tutta la posta su host con ip statico.<br />

In ques<strong>to</strong> modo il modulo riuscirebbe a trasformare, anche per l’utente esterno,<br />

PariPari <strong>in</strong> un server e-mail comple<strong>to</strong> e affidabile.<br />

29


2. LE COMPONENTI<br />

2.3.3 Host resolution (dns)<br />

Ques<strong>to</strong> modulo, che implementa RFC 1034 e RFC 1035, rappresenta il pun<strong>to</strong><br />

<strong>di</strong> <strong>in</strong>gresso per l’<strong>in</strong>ternauta alla nostra <strong>rete</strong>. In primo luogo, per semplicità, il<br />

server dns attualmente non è <strong>di</strong>stribui<strong>to</strong>, ma semplicemente copia<strong>to</strong>. Ci saran-<br />

no, <strong>in</strong>fatti, <strong>in</strong> PariPari <strong>di</strong>versi server tutti con lo stesso contenu<strong>to</strong>. Qualora un<br />

utente esterno alla <strong>rete</strong> facesse richiesta <strong>di</strong> un servizio <strong>in</strong>terno alla <strong>rete</strong> stessa,<br />

<strong>in</strong>terrogherebbe uno <strong>di</strong> questi server, il quale fornirebbe all’utente l’<strong>in</strong><strong>di</strong>rizzo cor-<br />

ret<strong>to</strong> della macch<strong>in</strong>a che assolve a quel servizio. La s<strong>in</strong>cronizzazione dei server<br />

sia tra loro che con le macch<strong>in</strong>e che svolgono servizi all’<strong>in</strong>terno della <strong>rete</strong> sarà<br />

<strong>una</strong> delle <strong>di</strong>fficoltà da affrontare nella scrittura del modulo.<br />

È allettante <strong>in</strong>oltre<br />

la possibilità <strong>di</strong> dotare <strong>di</strong> capacità <strong>di</strong> load balanc<strong>in</strong>g questi server dns.<br />

Figura 2.6: Uso <strong>di</strong> server DNS da host esterni la <strong>rete</strong>.<br />

2.3.4 File shar<strong>in</strong>g (aMule)<br />

Ques<strong>to</strong> plug-<strong>in</strong>, secondo i punti <strong>di</strong> vista, è il più o il meno importante del proget<strong>to</strong>.<br />

Semplicemente dovrebbe aggiungere al client <strong>di</strong> PariPari la possibilità <strong>di</strong> entrare<br />

nella <strong>rete</strong> ED2K fornendo i servizi <strong>di</strong> client come eMule. Non dovrebbe riservare<br />

grosse soprprese nè rivelarsi un ambi<strong>to</strong> <strong>di</strong> ricerca, <strong>in</strong> quan<strong>to</strong> sarebbe <strong>una</strong> specie<br />

clone <strong>di</strong> aMule con l’unica peculiarità <strong>di</strong> doversi <strong>in</strong>tegrare col core.<br />

Il motivo dell’<strong>in</strong>clusione <strong>di</strong> ques<strong>to</strong> modulo nel proget<strong>to</strong> è prettamente <strong>di</strong> natura<br />

commerciale. La sua funzione, <strong>in</strong>fatti, è quella <strong>di</strong> <strong>in</strong>vogliare l’ignaro naviga<strong>to</strong>re<br />

a provare il software se non altro usandolo come portale <strong>di</strong> accesso per la più<br />

30


2.3 ALTRI PLUG-IN<br />

grande <strong>rete</strong> <strong>di</strong> fileshar<strong>in</strong>g attualmente <strong>in</strong> uso. L’utente avrebbe <strong>in</strong> seconda battuta<br />

la possibilità <strong>di</strong> provare le <strong>in</strong>novative peculiarità del client. Quest’attenzione al<br />

lancio del prodot<strong>to</strong> è dovuta al fat<strong>to</strong> che <strong>una</strong> <strong>rete</strong> P2P è tan<strong>to</strong> più <strong>in</strong>teressante<br />

per l’utente quan<strong>to</strong> più contenuti può offrire e i contenuti sono legati a doppio<br />

filo al numero degli utenti 4 .<br />

4 Tipico caso <strong>di</strong> effet<strong>to</strong> <strong>rete</strong>. Cfr. http://en.wikipe<strong>di</strong>a.org/wiki/Network effect<br />

31


2. LE COMPONENTI<br />

32


Capi<strong>to</strong>lo 3<br />

Management<br />

Per affrontare un proget<strong>to</strong> così vas<strong>to</strong> e <strong>di</strong>versifica<strong>to</strong>, abbiamo procedu<strong>to</strong> alla crea-<br />

zione <strong>di</strong> un gruppo <strong>di</strong> ricerca. L’importanza <strong>di</strong> questa organizzazione risulta ancor<br />

più evidente pensando che i lavori proseguiranno per almeno un altro paio d’anni.<br />

La presenza <strong>di</strong> un gruppo ben organizza<strong>to</strong> mette il proget<strong>to</strong> al riparo da evenienze<br />

come la morte prematura dello stesso per abbandono dei partecipanti.<br />

La struttura modulare del proget<strong>to</strong> ha, <strong>in</strong> qualche modo, suggeri<strong>to</strong> un ap-<br />

proccio <strong>di</strong>vide and conquer. Ad ogni laureando, <strong>in</strong>fatti, sono state assegnate la<br />

<strong>progettazione</strong> e la <strong>realizzazione</strong> <strong>di</strong> uno o più plug-<strong>in</strong> (secondo la complessità del<br />

plug-<strong>in</strong> e il tipo <strong>di</strong> laurea da conseguire.), sempre sot<strong>to</strong> la supervisione e il con-<br />

trollo <strong>di</strong> quello che potremmo chiamare il coord<strong>in</strong>a<strong>to</strong>re. Scopo del coord<strong>in</strong>a<strong>to</strong>re<br />

è proprio quello <strong>di</strong> assegnare i lavori (<strong>in</strong> accordo col prof. Peserico) e controllare<br />

come questi vengano progettati e implementati. Ha anche la funzione <strong>di</strong> esper<strong>to</strong><br />

on-l<strong>in</strong>e per quegli studenti che non hanno ancora matura<strong>to</strong> <strong>una</strong> certa esperienza<br />

<strong>di</strong> <strong>progettazione</strong> e programmazione <strong>in</strong> Java. Il coord<strong>in</strong>a<strong>to</strong>re è anche il pun<strong>to</strong> <strong>di</strong><br />

comunicazione tra gli studenti e il professore. Ques<strong>to</strong> compi<strong>to</strong>, oltre a permettere<br />

<strong>di</strong> raccogliere le domande per riformularle <strong>in</strong> modo più efficiente e conciso per<br />

l’<strong>in</strong>terazione col rela<strong>to</strong>re, genera <strong>una</strong> specie <strong>di</strong> effet<strong>to</strong> cach<strong>in</strong>g. Spesso, <strong>in</strong>fatti, i<br />

problemi sollevati sono uguali o simili tra loro, e perciò possono essere risolti <strong>in</strong><br />

modo più veloce. L’ultima funzione del coord<strong>in</strong>a<strong>to</strong>re, ma forse la più significativa,<br />

è proprio quella <strong>di</strong> rappresentare il trait d’union tra gli stessi coord<strong>in</strong>a<strong>to</strong>ri. Per<br />

non <strong>di</strong>sperdere il know-how è oltremodo importante che il coord<strong>in</strong>a<strong>to</strong>re provveda<br />

a trasferire le proprie conoscenze non scritte e documentate al suo successore.<br />

Per progetti così estesi, è mol<strong>to</strong> utile per gli sviluppa<strong>to</strong>ri presenti e futu-<br />

ri, la possibilità <strong>di</strong> comprendere la struttura e il funzionamen<strong>to</strong> <strong>di</strong> quan<strong>to</strong> già<br />

scrit<strong>to</strong>. Per adempiere a questa necessità, ad ogni sviluppa<strong>to</strong>re è richies<strong>to</strong> <strong>di</strong><br />

commentare pesantemente il co<strong>di</strong>ce prodot<strong>to</strong>, e <strong>di</strong> scrivere qualche pag<strong>in</strong>a <strong>di</strong> do-<br />

33


3. MANAGEMENT<br />

Figura 3.1: Organizzazione delle risorse umane.<br />

34


cumentazione. Abbiamo scel<strong>to</strong> <strong>di</strong> adottare come l<strong>in</strong>gua del proget<strong>to</strong> l’<strong>in</strong>glese per<br />

evidenti motivi <strong>di</strong> <strong>in</strong>ternazionalizzazione. L’idea, poi, <strong>di</strong> fare ospitare il proget<strong>to</strong><br />

su sourceforge.net avvalora ancora <strong>di</strong> più questa scelta.<br />

Nonostante la natura modulare, che permette il lavoro quasi <strong>in</strong><strong>di</strong>pendente<br />

dei membri del gruppo, abbiamo trova<strong>to</strong> grossissimi problemi <strong>di</strong> comunicazione.<br />

Tutti i moduli, <strong>in</strong>fatti, devono cooperare tra loro ed è essenziale per i vari svi-<br />

luppa<strong>to</strong>ri scambiarsi idee, consigli e richieste. Purtroppo, non è sempre sta<strong>to</strong><br />

semplice gestire <strong>in</strong> modo organico le comunicazioni e le richieste dei vari studenti.<br />

35<br />

3.0


3. MANAGEMENT<br />

36


Conclusioni<br />

Abbiamo già nota<strong>to</strong>, <strong>in</strong> questi pochi mesi <strong>di</strong> vita, come non sia per nulla semplice<br />

gestire un proget<strong>to</strong> così ambizioso. Oltre i problemi <strong>di</strong> ord<strong>in</strong>e tecnico e logistico<br />

che, <strong>in</strong> qualche modo, sono stati risolti, cont<strong>in</strong>uano a presentarsi problemi <strong>di</strong> or-<br />

d<strong>in</strong>e logico. Abbiamo tenta<strong>to</strong> <strong>di</strong> mantenere la struttura del core il più semplice e<br />

funzionale possibile 1 proprio per permettergli <strong>di</strong> crescere e fornire tutte le funzio-<br />

nalità che gli saranno richieste <strong>in</strong> futuro. Purtroppo, nonostante ques<strong>to</strong> sforzo <strong>di</strong><br />

<strong>progettazione</strong>, è già accadu<strong>to</strong> <strong>di</strong> dover riscrivere completamente un modulo 2 per<br />

aggiungergli nuove caratteristiche <strong>in</strong><strong>di</strong>spensabili ad altri plug-<strong>in</strong>.<br />

È preve<strong>di</strong>bile<br />

che, nonostante tut<strong>to</strong>, da oggi al giorno del lancio al pubblico del client, moltis-<br />

simi altri saranno i problemi e le conseguenti correzioni <strong>in</strong> it<strong>in</strong>ere. La speranza è<br />

quella <strong>di</strong> avere imposta<strong>to</strong> il proget<strong>to</strong> <strong>in</strong> modo che questi aggiustamenti <strong>in</strong> corso<br />

d’opera siano i più semplici e più efficienti possibili, garantendo il migliore dei<br />

substrati possibili per i plug-<strong>in</strong> presenti e futuri.<br />

1 Pr<strong>in</strong>cipio KISS http://en.wikipe<strong>di</strong>a.org/wiki/KISS pr<strong>in</strong>ciple<br />

2 È sta<strong>to</strong> completamente riscrit<strong>to</strong> il modulo <strong>di</strong> connettività: Connectivity<br />

37


CONCLUSIONI<br />

38


Appen<strong>di</strong>ce A<br />

Documentazione del proget<strong>to</strong><br />

A.1 Kademlia<br />

This document describes our Kademlia implementation. This client is <strong>in</strong>tended<br />

<strong>to</strong> run over a connectivity layer allow<strong>in</strong>g high scalability and high modularization.<br />

Byte arrays mov<strong>in</strong>g between the Kademlia layer and connectivity layer are mana-<br />

ged by a simple moni<strong>to</strong>r. Kademlia, as described <strong>in</strong> [1], uses four logic RPC and<br />

several ADT that s<strong>to</strong>re <strong>in</strong>formation about the net around a node. A description<br />

of the implementation of the four RPC follows. We expla<strong>in</strong> choices and policies<br />

and f<strong>in</strong>ally most important pieces of code.<br />

The ma<strong>in</strong> class of the package is KadAdt that provides all the primary low<br />

level methods <strong>to</strong> operate on the basic structure of Kademlia. Besides this object<br />

other threads ma<strong>in</strong>ta<strong>in</strong> data consistency and the node runn<strong>in</strong>g 1 .<br />

We analyze the whole package keep<strong>in</strong>g <strong>in</strong> m<strong>in</strong>d how it works, giv<strong>in</strong>g a tran-<br />

sversal view of the <strong>in</strong>volved classes.<br />

A.1.1 ADT<br />

In a Kademlia client there are three <strong>di</strong>fferent ma<strong>in</strong> data structures.<br />

1. the table of buckets;<br />

2. the table of random bytes;<br />

3. the table of s<strong>to</strong>re:<br />

• the table of <strong>in</strong>ternal s<strong>to</strong>re;<br />

• the table of external s<strong>to</strong>re.<br />

1 Accept<strong>in</strong>g new <strong>in</strong>com<strong>in</strong>g connection.<br />

39


Documentazione del proget<strong>to</strong><br />

Table of buckets<br />

This table is built <strong>di</strong>rectly <strong>in</strong> the construc<strong>to</strong>r of the class KadAdt. It is implemen-<br />

ted by an array of Object. Each of these Object is an <strong>in</strong>stance of <strong>di</strong>fferent sizes 2<br />

dopArray.<br />

dopArray is built coupl<strong>in</strong>g two simple arrays:<br />

1. Long[];<br />

2. tripla[].<br />

The first one s<strong>to</strong>res a long value 3 that represents the timestamp obta<strong>in</strong>ed<br />

runn<strong>in</strong>g the <strong>java</strong> method System.currentTimeMillis. A -1 value <strong>in</strong> this field denotes<br />

an empty tripla. So the eras<strong>in</strong>g procedure 4 consists simply <strong>in</strong> putt<strong>in</strong>g a -1 <strong>in</strong><br />

the long cell.<br />

The second array holds <strong>in</strong>stances of the class tripla.<br />

The class tripla is a collection of three <strong>di</strong>fferent Object. It is conceived <strong>to</strong><br />

represent a node on the net. In fact it is structured <strong>in</strong> the follow<strong>in</strong>g way:<br />

InetAddress ip represents the ip address of the node;<br />

<strong>in</strong>t port represents the port address on which the node is listen<strong>in</strong>g;<br />

Str<strong>in</strong>g Hash represents the ID of the node.<br />

Table of random bytes<br />

This table is built us<strong>in</strong>g brand new own made class called triplArray. This class<br />

is composed by three <strong>di</strong>fferent simple arrays:<br />

1. Long[];<br />

2. Byte[];<br />

3. Object[].<br />

The first one s<strong>to</strong>res a long value 5 that represents the timestamp obta<strong>in</strong>ed<br />

runn<strong>in</strong>g the <strong>java</strong> method System.currentTimeMillis. A -1 value <strong>in</strong> this field denotes<br />

2 The size of the dopArray is def<strong>in</strong>ed accord<strong>in</strong>g <strong>to</strong> the kademlia policies.<br />

3 A long encapsulated <strong>in</strong> a Long.<br />

4 And the <strong>in</strong>itialization.<br />

5 A long encapsulated <strong>in</strong> a Long.<br />

40


Appen<strong>di</strong>ce<br />

an empty tripla. So the eras<strong>in</strong>g procedure 6 consists simply <strong>in</strong> putt<strong>in</strong>g a -1 <strong>in</strong><br />

the long cell.<br />

The second array holds the type of the sent request just <strong>to</strong> <strong>in</strong>crease the<br />

security allow<strong>in</strong>g a further check on the answer.<br />

The last array keeps the arrays of random bytes.<br />

Besides this class there is another thread object that runs cont<strong>in</strong>uously <strong>to</strong> de-<br />

lete the <strong>to</strong>o old entries. This class is KadRnd and uses the methods <strong>in</strong> triplArray<br />

<strong>to</strong> f<strong>in</strong>d the obsolete entries and <strong>to</strong> delete them.<br />

Table of s<strong>to</strong>re<br />

There are two <strong>di</strong>fferent tables: the table that s<strong>to</strong>res the node’s own l<strong>in</strong>ks and the<br />

table <strong>to</strong> s<strong>to</strong>re the l<strong>in</strong>k from other clients. They have the same structure and they<br />

are implemented by the class s<strong>to</strong>re. This class is composed by three <strong>di</strong>fferent<br />

simple arrays:<br />

1. Long[];<br />

2. Vec<strong>to</strong>r[];<br />

3. Str<strong>in</strong>g[].<br />

The first one s<strong>to</strong>res a long value 7 that represents the timestamp obta<strong>in</strong>ed<br />

runn<strong>in</strong>g the <strong>java</strong> method System.currentTimeMillis. A -1 value <strong>in</strong> this field denotes<br />

an empty tripla. So the eras<strong>in</strong>g procedure 8 consists simply <strong>in</strong> putt<strong>in</strong>g a -1 <strong>in</strong><br />

the long cell.<br />

The third field holds the hash of the resource whose l<strong>in</strong>k had <strong>to</strong> be saved.<br />

F<strong>in</strong>ally <strong>in</strong> the second field the client keeps a collection of <strong>in</strong>stances of tripla,<br />

that refers <strong>to</strong> the third field.<br />

Besides this two tables, there is another thread called kadS<strong>to</strong>re that keeps<br />

the two tables refreshed. Cont<strong>in</strong>uously, at pre-def<strong>in</strong>ed time <strong>in</strong>tervals, it deletes<br />

the obsolete entries <strong>in</strong> the table that hosts external <strong>in</strong>formation and republishes<br />

<strong>in</strong> the net the old entries <strong>in</strong> the other table.<br />

6 And the <strong>in</strong>itialization.<br />

7 A long encapsulated <strong>in</strong> a Long.<br />

8 And the <strong>in</strong>itialization.<br />

41


Documentazione del proget<strong>to</strong><br />

A.1.2 Communication<br />

S<strong>in</strong>ce now we call communication the RPC and its reply. We’ve def<strong>in</strong>ed a<br />

datagram for each communication; all of them have one header <strong>in</strong> common 9 .<br />

Byte Use Note<br />

0 KaD version the version of the datagram<br />

1,2 size the size of the whole datagram (max 64KB)<br />

3 type the nature of the datagram<br />

4,5,6,7 random byte<br />

Here some more words about the third and the fourth field.<br />

The type field describes the type of the datagram that follow. For each RPC<br />

is assigned a byte value as you can see <strong>in</strong> this list.<br />

1 p<strong>in</strong>g;<br />

2 p<strong>in</strong>g reply;<br />

4 p<strong>in</strong>g check;<br />

5 p<strong>in</strong>g reply check;<br />

6 p<strong>in</strong>g s<strong>in</strong>k check;<br />

7 f<strong>in</strong>g node;<br />

8 f<strong>in</strong>g node reply;<br />

9 f<strong>in</strong>g value;<br />

10 f<strong>in</strong>g value reply;<br />

11 f<strong>in</strong>g value reply ok;<br />

13 s<strong>to</strong>re.<br />

The random byte field is filled by four bytes randomly generated by the<br />

client who send the request. The recipient replies embedd<strong>in</strong>g these four bytes<br />

<strong>in</strong> the answer, this way the sender can understand the match for the answer <strong>to</strong><br />

the question. Moreover this practice <strong>in</strong>creases the security level aga<strong>in</strong>st malicious<br />

datagram sent <strong>to</strong> a client.<br />

9 Other parts of the datagram are <strong>in</strong> common but at the moment they aren’t <strong>in</strong> the header<br />

<strong>in</strong> order <strong>to</strong> keep a more logic structure of the datagram.<br />

42


P<strong>in</strong>g<br />

The PING RPC probes a node <strong>to</strong> see if it is on l<strong>in</strong>e.<br />

Appen<strong>di</strong>ce<br />

This statement expresses the basic role of this RPC. Whenever a node receives<br />

a p<strong>in</strong>g, it answers and adds the sender <strong>to</strong> his table of buckets; the same<br />

behavior must be honored for any other RPC received. Whenever a client receives<br />

a p<strong>in</strong>g it replies <strong>to</strong> the source with a p<strong>in</strong>g reply (with p<strong>in</strong>g reply) and then calls<br />

the method <strong>in</strong>sNodo. So the source receives the p<strong>in</strong>g reply and processes it with<br />

p<strong>in</strong>g s<strong>in</strong>k and erases from the table of random byte the random bytes of the<br />

first request.<br />

<strong>in</strong>sNodo provides the functionalities <strong>to</strong> <strong>in</strong>sert the node passed as argument<br />

<strong>in</strong> the table of buckets. It calculates the <strong>di</strong>stance with XOR metric 10 and<br />

selects the right bucket for <strong>in</strong>sertion. If the bucket has at least one free cell the<br />

node is straitforward <strong>in</strong>serted; otherwise the so called kadInsert thread is run.<br />

kadInsert searches the right bucket 11 for the oldest <strong>in</strong>serted node and then<br />

tries <strong>to</strong> p<strong>in</strong>g check 12 it. If the old node replies, kadInsert refreshes the node <strong>in</strong><br />

the bucket; otherwise it replaces it with the new one.<br />

S<strong>to</strong>re<br />

STORE <strong>in</strong>structs a node <strong>to</strong> s<strong>to</strong>re a (key; value) pair for later retrieval<br />

As described by this statement this RPC purpose is <strong>to</strong> spread the <strong>in</strong>formation<br />

<strong>in</strong> the network <strong>to</strong> f<strong>in</strong>d the host of a resource. In fact every node and every resource<br />

is labeled by an hash <strong>in</strong> the same space. A node that wants <strong>to</strong> share a resource<br />

publishes the hash <strong>to</strong> the right node. The recipient node holds <strong>in</strong> the external<br />

s<strong>to</strong>re the tripla of the sender beside the hash and the timestamp. Meanwhile,<br />

the sender holds <strong>in</strong> its <strong>in</strong>ternal s<strong>to</strong>re the tripla of the recipient, the hash of<br />

the resource and the timestamp. The methods <strong>in</strong>volved <strong>in</strong> these operations are<br />

<strong>di</strong>rectS<strong>to</strong>re and s<strong>to</strong>re s<strong>in</strong>k.<br />

Kademlia s<strong>to</strong>res a resource hash <strong>in</strong> the ID closest node 13 . This task is accom-<br />

plished by the thread kadS<strong>to</strong>rer. kadS<strong>to</strong>rer call<strong>in</strong>g other class and methods,<br />

later exam<strong>in</strong>ed, f<strong>in</strong>ds the right collection of nodes 14 and <strong>in</strong>struct them send<strong>in</strong>g a<br />

s<strong>to</strong>re RPC as described above.<br />

10 Provided by metric<br />

11 Given as an argument<br />

12 The series of p<strong>in</strong>g check, p<strong>in</strong>g reply check and p<strong>in</strong>g s<strong>in</strong>k check acts exactly as the normal<br />

p<strong>in</strong>g suite except fot the type, so they use the same method but <strong>di</strong>fferent parameters.<br />

13 One or more node <strong>in</strong> case resource hash = node ID.<br />

14 Or just one.<br />

43


Documentazione del proget<strong>to</strong><br />

F<strong>in</strong>d Node<br />

FIND NODE takes a 160-bit ID as an argument. The recipient of<br />

a the RPC returns IP address; UDP port; Node ID triples for the k<br />

nodes it knows about closest <strong>to</strong> the target ID. These triples can come<br />

from a s<strong>in</strong>gle k-bucket, or they may come from multiple k-buckets if<br />

the closest k-bucket is not full. In any case, the RPC recipient must<br />

return k items (unless there are fewer than k nodes <strong>in</strong> all its k-buckets<br />

comb<strong>in</strong>ed, <strong>in</strong> which case it returns every node it knows about).<br />

The class that performs any k<strong>in</strong>d of research on Kademlia is kadSearcher.<br />

The kadSearcher thread undertakes several actions. First it searches the local<br />

Table of buckets for the searched node ID. In case of unsuccesfull resear-<br />

ch it puts α tripla <strong>in</strong> a Vec<strong>to</strong>r called cerca<strong>to</strong>re. F<strong>in</strong>ally it runs the th-<br />

read <strong>in</strong>quirer. Furtermore it checks for any found con<strong>di</strong>tion launched by other<br />

processes 15 .<br />

<strong>in</strong>quirer processeses all the tripla <strong>in</strong> cerca<strong>to</strong>re as it follows.<br />

1. It sends a f<strong>in</strong>g node request <strong>to</strong> α unprocessed tripla received from the<br />

same node and marks em as processed.<br />

2. It waits any possible reply from the asked clients.<br />

3. If the research id unsuccesfull it asks all the unprocessedtripla.<br />

F<strong>in</strong>d Value<br />

FIND VALUE behaves like FIND NODE return<strong>in</strong>g IP address; UDP<br />

port; Node ID triples with one exception. If the RPC recipient has<br />

received a STORE RPC for the key, it just returns the s<strong>to</strong>red value.<br />

As mentioned <strong>in</strong> A.1.2 the class that performs this k<strong>in</strong>d of search is still<br />

kadSearcher. The behaviour <strong>to</strong> f<strong>in</strong>d a value is very very similar <strong>to</strong> that <strong>to</strong> f<strong>in</strong>d<br />

a node. Now we consider only the <strong>di</strong>fferences.<br />

The recipient of a f<strong>in</strong>g value checks its table of s<strong>to</strong>re <strong>to</strong> f<strong>in</strong>d recurrences of<br />

the searched hash. If found it replies with f<strong>in</strong>g reply send<strong>in</strong>g the correct tripla 16 .<br />

15 f<strong>in</strong>g s<strong>in</strong>k <strong>in</strong> class kadAdt.<br />

16 Otherwise it follows the behaviour of f<strong>in</strong>d node<br />

44


F<strong>in</strong>d - favorite mode<br />

Appen<strong>di</strong>ce<br />

This is a variation on F<strong>in</strong>d Node and F<strong>in</strong>d Value <strong>in</strong>troduced <strong>to</strong> half the round trip<br />

time. The search<strong>in</strong>g node, while it proceeds with the default search<strong>in</strong>g behaviour,<br />

chooses a favorite among the nodes it is go<strong>in</strong>g <strong>to</strong> contact. This favorite node, other<br />

than answer<strong>in</strong>g the searcher, chooses a favorite, called child, among the nodes it<br />

is go<strong>in</strong>g <strong>to</strong> send <strong>to</strong> the request<strong>in</strong>g node. The favorite node asks its child <strong>di</strong>rectly<br />

for the searched ID or hash and <strong>to</strong> choose another child <strong>to</strong> cont<strong>in</strong>ue the cha<strong>in</strong> of<br />

favorites.<br />

A.1.3 Classes<br />

Here is a list of classes with a brief description.<br />

kadAdt the ma<strong>in</strong> class;<br />

kadSearcher a thread that searches;<br />

kadS<strong>to</strong>rer a thread that searches and then s<strong>to</strong>res;<br />

kadS<strong>to</strong>re a thread that keeps <strong>in</strong> order the table of s<strong>to</strong>re;<br />

<strong>in</strong>quirer a thread used by kadSearcher <strong>to</strong> search;<br />

dopArray ADT <strong>to</strong> implement a bucket 17 ;<br />

triplArray ADT <strong>to</strong> implement table of random bytes;<br />

cop ADT used <strong>in</strong> kadAdt 18 ;<br />

copHashCompara<strong>to</strong>r a compara<strong>to</strong>r on hash <strong>in</strong> object cop used <strong>to</strong> sort triplas;<br />

tripla ADT that represents a node on th net;<br />

triplaDati ADT <strong>to</strong> exchange data with connectivity layer;<br />

kadInsert a thread <strong>to</strong> <strong>in</strong>sert a node <strong>in</strong> case of full bucket;<br />

s<strong>to</strong>re ADT <strong>to</strong> implement a table of s<strong>to</strong>re;<br />

metric static class with methods that calculate XOR <strong>di</strong>stances;<br />

kadCLI Command L<strong>in</strong>e Interface;<br />

17 An element of table of buckets<br />

18 In cerca<strong>to</strong>re<br />

45


Documentazione del proget<strong>to</strong><br />

kadCommand the object <strong>to</strong> communicate with kad;<br />

kadUI User <strong>in</strong>terface called <strong>to</strong> complete kadCommand.<br />

A.1.4 Datagrams<br />

Here is a list of datagrams with a brief description.<br />

Header<br />

tripla<br />

P<strong>in</strong>g<br />

Byte Use Note<br />

0 KaD version the version of the datagram<br />

1,2 size the size of the whole datagram (max 64KB)<br />

3 type the nature of the datagram<br />

4,5,6,7 random byte<br />

P<strong>in</strong>g and P<strong>in</strong>g check<br />

S<strong>to</strong>re<br />

P<strong>in</strong>g reply<br />

0,1,2,3 IP the tripla’s IPv4<br />

4,5 port the tripla’s listen<strong>in</strong>g port<br />

6 + hash length ID the tripla’s ID<br />

Byte Use Note<br />

8,9 port the client’s listen<strong>in</strong>g port<br />

10 + hash length ID the ID’s client<br />

Byte Use Note<br />

8,9 port the client’s listen<strong>in</strong>g port<br />

10 + hash length ID the ID’s client<br />

Byte Use Note<br />

8,9 port the client’s listen<strong>in</strong>g port<br />

10 + hash length ID the ID’s client<br />

10 + hash length + hash length hash the resources’s hash<br />

10 + hash length + hash length + tripla length tripla the answered triplas<br />

46


F<strong>in</strong>d Node<br />

F<strong>in</strong>d Node<br />

Byte Use Note<br />

8,9 port the client’s listen<strong>in</strong>g port<br />

10 + hash length ID the ID’s client<br />

10 + hash length + hash length ID the node’s ID<br />

F<strong>in</strong>d Node reply<br />

Byte Use Note<br />

Appen<strong>di</strong>ce<br />

8,9 port the client’s listen<strong>in</strong>g port<br />

10 + hash length ID the ID’s client<br />

10 + hash length + triplaS length triplas a list of triplas<br />

F<strong>in</strong>d Value<br />

F<strong>in</strong>d Value<br />

Byte Use Note<br />

8,9 port the client’s listen<strong>in</strong>g port<br />

10 + hash length ID the ID’s client<br />

10 + hash length + hash length hash the resources’s hash<br />

F<strong>in</strong>d Value reply<br />

Byte Use Note<br />

8,9 port the client’s listen<strong>in</strong>g port<br />

10 + hash length ID the ID’s client<br />

10 + hash length + triplaS length triplas a list of triplas<br />

F<strong>in</strong>d - favorite mode<br />

Byte Use Note<br />

8,9 port 0 19<br />

10 + hash length ID the ID’s client<br />

10 + hash length + hash length hash the resources’s hash<br />

10 + hash length + hash length + tripla lenght tripla the searcher’s tripla<br />

Other notes: the random bytes are the same 20 along the whole favorites cha<strong>in</strong>.<br />

20 Decided by the first node.<br />

47


Documentazione del proget<strong>to</strong><br />

A.2 Core<br />

This document describes the implementation of the core. The core is designed <strong>to</strong><br />

be as simpler as possible keep<strong>in</strong>g the ability <strong>to</strong> manage the most <strong>di</strong>fferent k<strong>in</strong>ds<br />

of plug-<strong>in</strong>.<br />

The core is formed by two ma<strong>in</strong> structures and it is surrounded by two resource<br />

managers. The ma<strong>in</strong> purpose of the core is <strong>to</strong> launch the plug-<strong>in</strong>s and <strong>to</strong> provide<br />

<strong>to</strong> them a structure <strong>to</strong> communicate. The resource managers are <strong>in</strong>teded <strong>to</strong> let<br />

the plug-<strong>in</strong>s use local resource such as <strong>di</strong>sk capacity and connectivity.<br />

A.2.1 The core<br />

The first role of the core is <strong>to</strong> launch the plug-<strong>in</strong>. This is the rout<strong>in</strong>e <strong>to</strong> accomplish<br />

this mission:<br />

1. It reads the file, know<strong>in</strong>g the name, with method <strong>java</strong>.io.FileInputStream.<br />

2. It def<strong>in</strong>es the bytes read as a class with def<strong>in</strong>eClass;<br />

3. It <strong>in</strong>stantiates the new object with methods from class <strong>java</strong>.lang.reflect.<br />

The bounds <strong>to</strong> allow these operations are the follow<strong>in</strong>g:<br />

• the plug-<strong>in</strong> name is equal <strong>to</strong> the file name;<br />

• the core has <strong>to</strong> pass <strong>to</strong> the plug-<strong>in</strong>, as an argument, the object manager 21 ;<br />

Every launched plug-<strong>in</strong> is associated <strong>to</strong> a <strong>java</strong>.util.concurrent.PriorityBlock<strong>in</strong>Queue.<br />

These associations are s<strong>to</strong>red <strong>in</strong> a simple HashTable. Every request for another<br />

plug-<strong>in</strong> has <strong>to</strong> be pushed <strong>in</strong> the right queue. Consequently, every plug-<strong>in</strong> has <strong>to</strong><br />

check its own queue for <strong>in</strong>com<strong>in</strong>g requests. The requests have <strong>to</strong> be encapsulated<br />

<strong>in</strong> a cocoon <strong>to</strong> travel <strong>in</strong> the core. Every plug-<strong>in</strong> can def<strong>in</strong>e the type of request<br />

<strong>to</strong> receive, this request will be encapsulated <strong>in</strong> cocoon.<br />

cocoon is composed by 5 fields:<br />

priority an <strong>in</strong>t that <strong>in</strong><strong>di</strong>cates the cocoon’s priority;<br />

orig a Str<strong>in</strong>g that <strong>in</strong><strong>di</strong>cates the plug-<strong>in</strong> that generates this cocoon;<br />

data the real payload (Object);<br />

signature a long useful <strong>to</strong> track the question - answer match<strong>in</strong>g;<br />

leave a Str<strong>in</strong>g conta<strong>in</strong><strong>in</strong>g the name of the plug-<strong>in</strong> <strong>to</strong> which send<strong>in</strong>g the answer.<br />

21 That represent the core itself<br />

48


A.2.2 Resource manager<br />

Appen<strong>di</strong>ce<br />

The resource manager follows exactly the same bounds and structure of the other<br />

plug-<strong>in</strong>s. Currently they are dataS<strong>to</strong>rage and Connectivity.<br />

Connectivity<br />

This resource manager provides the functionality <strong>to</strong> send and receive streams of<br />

byte over <strong>in</strong>ternet. The message for this plug-<strong>in</strong> is the object flux:<br />

order a Str<strong>in</strong>g that <strong>in</strong><strong>di</strong>cates the action <strong>to</strong> undertake;<br />

pro<strong>to</strong>col a byte that <strong>in</strong><strong>di</strong>cates the pro<strong>to</strong>col <strong>to</strong> use 22 ;<br />

data the real payload (triplaDati);<br />

speed a <strong>in</strong>t that <strong>in</strong><strong>di</strong>cates the bandwidth <strong>to</strong> use 23 ;<br />

anonima<strong>to</strong> not yet implemented;<br />

socketID a long that <strong>in</strong><strong>di</strong>cates the tcp socket <strong>to</strong> use.<br />

This resource manager handles both the udp and the tcp pro<strong>to</strong>col. A plug-<strong>in</strong><br />

that wants <strong>to</strong> use a port <strong>to</strong> listen on, sends a “book” request <strong>to</strong> Connectivity.<br />

All the traffic <strong>to</strong>wards that port will be forwarded <strong>to</strong> the book<strong>in</strong>g plug-<strong>in</strong>. In<br />

case of tcp communication the plug-<strong>in</strong> can re-use the same socket from which it<br />

received the data.<br />

dataS<strong>to</strong>rage<br />

This resource manager provides the functionality <strong>to</strong> save and retrieve files 24 . The<br />

message for this plug-<strong>in</strong> is the object chunk:<br />

order a Str<strong>in</strong>g that <strong>in</strong><strong>di</strong>cates the action <strong>to</strong> undertake;<br />

name a Str<strong>in</strong>g that <strong>in</strong><strong>di</strong>cates file name;<br />

position a long that <strong>in</strong><strong>di</strong>cates the offset <strong>to</strong> start writ<strong>in</strong>g or read<strong>in</strong>g from;<br />

data the payload s<strong>to</strong>red <strong>in</strong> a byte[];<br />

22 Now only tcp and udp<br />

23 Not yet implemented<br />

24 Now only local operation<br />

49


Documentazione del proget<strong>to</strong><br />

dest<strong>in</strong>ation a Str<strong>in</strong>g conta<strong>in</strong><strong>in</strong>g the name of the plug-<strong>in</strong> <strong>to</strong> send the answer <strong>to</strong>;<br />

fraction a <strong>in</strong>t that <strong>in</strong><strong>di</strong>cates the part <strong>to</strong> write or read;<br />

size a long that <strong>in</strong><strong>di</strong>cates the size of the file;<br />

fileHandler a File that represents the file;<br />

This plug-<strong>in</strong> can undertake three <strong>di</strong>fferent operations. It can manage entire<br />

files, it can operate over pieces of file <strong>in</strong> a dumb mode or <strong>in</strong> an ensured mode.<br />

Entire files are simply handled with the handler of <strong>java</strong>. This resource manager<br />

allows the plug-<strong>in</strong>s <strong>to</strong> deal with pieces of file. It can assemble the pieces <strong>to</strong> make<br />

a whole file or it can read pieces <strong>in</strong> any order from (un)complete files. While<br />

assembl<strong>in</strong>g, it can check for overlapp<strong>in</strong>g problems avoid<strong>in</strong>g them.<br />

A.2.3 Classes<br />

Here is a list of classes with a brief description.<br />

core the ma<strong>in</strong> core class;<br />

loader the class loader;<br />

manager the moni<strong>to</strong>r conta<strong>in</strong><strong>in</strong>g the hastable;<br />

cocoon class <strong>to</strong> def<strong>in</strong>e the message of the core;<br />

accept class <strong>to</strong> accept <strong>in</strong>com<strong>in</strong>g tcp connections;<br />

Daccept class <strong>to</strong> accept <strong>in</strong>com<strong>in</strong>g udp connections;<br />

connect a class <strong>to</strong> manage connections;<br />

Connectivity a wrapper class <strong>to</strong> start Connectivity;<br />

connectServer a thread of Connectivity <strong>to</strong> check the queue for <strong>in</strong>com<strong>in</strong>g<br />

requests;<br />

flux class <strong>to</strong> def<strong>in</strong>e the message of Connectivity;<br />

s<strong>to</strong>rage a class <strong>to</strong> manage <strong>di</strong>sk space;<br />

chunk class <strong>to</strong> def<strong>in</strong>e the message of dataS<strong>to</strong>rage;<br />

dataServer a thread of dataS<strong>to</strong>rage <strong>to</strong> check the queue for <strong>in</strong>com<strong>in</strong>g requests;<br />

50


dataS<strong>to</strong>rage a wrapper class <strong>to</strong> start dataS<strong>to</strong>rage;<br />

fileChunk an ADT <strong>to</strong> keep <strong>in</strong>formation about pieces of files.<br />

51<br />

Appen<strong>di</strong>ce


Documentazione del proget<strong>to</strong><br />

52


Bibliografia<br />

[1] Petar Maymounkov and David Mazi‘eres. Kademlia: A<br />

Peer-<strong>to</strong>-<strong>peer</strong> Information System Based on the XOR Metric.<br />

http://kademlia.scs.cs.nyu.edu.<br />

[2] A. S. Tanenbaum. Reti <strong>di</strong> Calcola<strong>to</strong>ri - Quarta E<strong>di</strong>zione. Pearson Education<br />

Italia, Milano, 2003.<br />

[3] E. Peserico, A. Simonet<strong>to</strong> Progettazione e <strong>realizzazione</strong> <strong>in</strong> Java <strong>di</strong> <strong>una</strong> <strong>rete</strong><br />

P2P anonima e multifunzionale: connettività sicura e affidabile. Padova,<br />

2005.<br />

[4] Wikipe<strong>di</strong>a http://it.wikipe<strong>di</strong>a.org/wiki/P2P.<br />

[5] Wikipe<strong>di</strong>a http://it.wikipe<strong>di</strong>a.org/wiki/BitTorrent.<br />

[6] Wikipe<strong>di</strong>a http://en.wikipe<strong>di</strong>a.org/wiki/Chord project.<br />

[7] http://research.microsoft.com/~antr/PAST/pastry.pdf.<br />

[8] Wikipe<strong>di</strong>a http://it.wikipe<strong>di</strong>a.org/wiki/MUTE.<br />

[9] Wikipe<strong>di</strong>a http://en.wikipe<strong>di</strong>a.org/wiki/Gnutella.<br />

53


BIBLIOGRAFIA<br />

54


Elenco delle figure<br />

1 La <strong>rete</strong> e gli host esterni. . . . . . . . . . . . . . . . . . . . . . . . 5<br />

1.1 La struttura del client. . . . . . . . . . . . . . . . . . . . . . . . . 9<br />

1.2 Formazione <strong>di</strong> tunnel per la comunicazione anonima. . . . . . . . 12<br />

2.1 La Struttura del nucleo. . . . . . . . . . . . . . . . . . . . . . . . 16<br />

2.2 L’<strong>in</strong>capsulamen<strong>to</strong> dei messaggi. . . . . . . . . . . . . . . . . . . . 18<br />

2.3 Albero <strong>di</strong> Kademlia: ad ogni sal<strong>to</strong> durante la <strong>di</strong>scesa si elim<strong>in</strong>a<br />

mezzo sot<strong>to</strong>-albero. . . . . . . . . . . . . . . . . . . . . . . . . . . 24<br />

2.4 Il sistema del pre<strong>di</strong>let<strong>to</strong>. . . . . . . . . . . . . . . . . . . . . . . . 27<br />

2.5 Dirottamen<strong>to</strong> <strong>di</strong> tutta la posta su host con ip statico. . . . . . . . 29<br />

2.6 Uso <strong>di</strong> server DNS da host esterni la <strong>rete</strong>. . . . . . . . . . . . . . 30<br />

3.1 Organizzazione delle risorse umane. . . . . . . . . . . . . . . . . . 34<br />

55


ELENCO DELLE FIGURE<br />

56


Elenco delle tabelle<br />

1.1 Crit<strong>to</strong>grafia: RSA vs AES. . . . . . . . . . . . . . . . . . . . . . . 11<br />

2.1 Pseudoco<strong>di</strong>ce: convenzioni . . . . . . . . . . . . . . . . . . . . . . 22<br />

2.2 Pseudoco<strong>di</strong>ce: la ricerca <strong>in</strong> Kademlia . . . . . . . . . . . . . . . . 23<br />

2.3 Pseudoco<strong>di</strong>ce: la ricerca <strong>in</strong> Kademlia col sistema del pre<strong>di</strong>let<strong>to</strong>. . 26<br />

57


ELENCO DELLE TABELLE<br />

58


R<strong>in</strong>graziamenti<br />

• Daniela perchè c’è per farmi tendere sempre al meglio,<br />

• Davide per il suo uso dell’algebra booleana e la sua funzione <strong>di</strong> censore,<br />

• Anna per come si è de<strong>di</strong>cata col sorriso a tut<strong>to</strong> il repar<strong>to</strong>,<br />

• Lorenzo per come ha sapu<strong>to</strong> gestire il S.Giorgio,<br />

• E.P. per l’entusiasmo che <strong>in</strong>fonde,<br />

• l’allegra compagnia della f<strong>in</strong>e degli esami,<br />

• i compagni del gruppo <strong>di</strong> ricerca <strong>di</strong> PariPari (presenti e passati),<br />

• e tutti coloro che hanno contribui<strong>to</strong> <strong>in</strong> qualche maniera al raggiungimen<strong>to</strong><br />

<strong>di</strong> ques<strong>to</strong> risulta<strong>to</strong>.<br />

entia non sunt multiplicanda s<strong>in</strong>e necessitate.<br />

— Guglielmo <strong>di</strong> Occam

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!