tutorial.texi 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. \input texinfo
  2. @setfilename gnunet-tutorial.info
  3. @documentencoding UTF-8
  4. @settitle GNUnet Tutorial
  5. @c @exampleindent 2
  6. @include version.texi
  7. @copying
  8. Copyright @copyright{} 2001-2018 GNUnet e.V.
  9. Permission is granted to copy, distribute and/or modify this document
  10. under the terms of the GNU Free Documentation License, Version 1.3 or
  11. any later version published by the Free Software Foundation; with no
  12. Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
  13. copy of the license is included in the section entitled ``GNU Free
  14. Documentation License''.
  15. A copy of the license is also available from the Free Software
  16. Foundation Web site at @url{http://www.gnu.org/licenses/fdl.html}.
  17. Alternately, this document is also available under the General
  18. Public License, version 3 or later, as published by the Free Software
  19. Foundation. A copy of the license is included in the section entitled
  20. ``GNU General Public License''.
  21. A copy of the license is also available from the Free Software
  22. Foundation Web site at @url{http://www.gnu.org/licenses/gpl.html}.
  23. @end copying
  24. @dircategory Tutorial
  25. @direntry
  26. * GNUnet-Tutorial: (gnunet-tutorial). C Tutorial for GNunet
  27. @end direntry
  28. @titlepage
  29. @title GNUnet C Tutorial
  30. @subtitle A Tutorial for GNUnet @value{VERSION} (C version)
  31. @author The GNUnet Developers
  32. @page
  33. @vskip 0pt plus 1filll
  34. @insertcopying
  35. @end titlepage
  36. @contents
  37. @c **** TODO
  38. @c 1. Update content?
  39. @c 2. Either reference main documentation or
  40. @c 3. Merge this into main documentation
  41. @node Top
  42. @top Introduction
  43. This tutorials explains how to install GNUnet on a
  44. GNU/Linux system and gives an introduction on how
  45. GNUnet can be used to develop a Peer-to-Peer application.
  46. Detailed installation instructions for
  47. various operating systems and a detailed list of all
  48. dependencies can be found on our website at
  49. @uref{https://docs.gnunet.org/#Installation} and in our
  50. Reference Documentation (GNUnet Handbook).
  51. Please read this tutorial carefully since every single step is
  52. important, and do not hesitate to contact the GNUnet team if you have
  53. any questions or problems! Visit this link in your webbrowser to learn
  54. how to contact the GNUnet team:
  55. @uref{https://gnunet.org/en/contact.html}
  56. @menu
  57. * Installing GNUnet:: Installing GNUnet
  58. * Introduction to GNUnet Architecture:: Introduction to GNUnet Architecture
  59. * First Steps with GNUnet:: First Steps with GNUnet
  60. * Developing Applications:: Developing Applications
  61. * GNU Free Documentation License:: The license of this manual
  62. @detailmenu
  63. --- The Detailed Node Listing ---
  64. Installing GNUnet
  65. * Obtaining a stable version::
  66. * Installing Build Tool Chain and Dependencies::
  67. * Obtaining the latest version from Git::
  68. * Compiling and Installing GNUnet::
  69. * Common Issues - Check your GNUnet installation::
  70. Introduction to GNUnet Architecture
  71. First Steps with GNUnet
  72. * Configure your peer::
  73. * Start a peer::
  74. * Monitor a peer::
  75. * Starting Two Peers by Hand::
  76. * Starting Peers Using the Testbed Service::
  77. Developing Applications
  78. * gnunet-ext::
  79. * Adapting the Template::
  80. * Writing a Client Application::
  81. * Writing a Service::
  82. * Interacting directly with other Peers using the CORE Service::
  83. * Storing peer-specific data using the PEERSTORE service::
  84. * Using the DHT::
  85. * Debugging with gnunet-arm::
  86. @end detailmenu
  87. @end menu
  88. @node Installing GNUnet
  89. @chapter Installing GNUnet
  90. First of all you have to install a current version of GNUnet.
  91. You can download a tarball of a stable version from GNU FTP mirrors
  92. or obtain the latest development version from our Git repository.
  93. Most of the time you should prefer to download the stable version
  94. since with the latest development version things can be broken,
  95. functionality can be changed or tests can fail. You should only use
  96. the development version if you know that you require a certain
  97. feature or a certain issue has been fixed since the last release.
  98. @menu
  99. * Obtaining a stable version::
  100. * Installing Build Tool Chain and Dependencies::
  101. * Obtaining the latest version from Git::
  102. * Compiling and Installing GNUnet::
  103. * Common Issues - Check your GNUnet installation::
  104. @end menu
  105. @node Obtaining a stable version
  106. @section Obtaining a stable version
  107. Download the tarball from
  108. @indicateurl{https://ftp.gnu.org/gnu/gnunet/gnunet-@value{VERSION}.tar.gz}.
  109. Make sure to download the associated @file{.sig} file and to verify the
  110. authenticity of the tarball against it, like this:
  111. @example
  112. $ wget https://ftp.gnu.org/gnu/gnunet/gnunet-@value{VERSION}.tar.gz.sig
  113. $ gpg --verify-files gnunet-@value{VERSION}.tar.gz.sig
  114. @end example
  115. @noindent
  116. If this command fails because you do not have the required public key,
  117. then you need to run the following command to import it:
  118. @example
  119. $ gpg --keyserver keys.gnupg.net --recv-keys 48426C7E
  120. @end example
  121. @noindent
  122. and rerun the @code{gpg --verify-files} command.
  123. @b{Note:}@
  124. @b{The pub key to sign the 0.10.1 release has been
  125. revoked}. You will get an error message stating that
  126. @b{there is no known public key or that it has been revoked}.
  127. The next release of GNUnet will have a valid signature
  128. again. We are sorry for the inconvenience this causes.
  129. Another possible source you could use is our
  130. "gnunet" git repository which, since the change from SVN to git in 2016,
  131. has mandatory signed commits by every developer.
  132. After verifying the signature you can extract the tarball.
  133. The resulting directory will be renamed to @file{gnunet}, which we will
  134. be using in the remainder of this document to refer to the
  135. root of the source directory.
  136. @example
  137. $ tar xvzf gnunet-@value{VERSION}.tar.gz
  138. $ mv gnunet-@value{VERSION} gnunet
  139. @end example
  140. @c FIXME: This can be irritating for the reader - First we say git should
  141. @c be avoid unless it is really required, and then we write this
  142. @c paragraph:
  143. @noindent
  144. However, please note that stable versions can be very outdated.
  145. As a developer you are @b{strongly} encouraged to use the version
  146. from @uref{https://git.gnunet.org/, the git server}.
  147. @node Installing Build Tool Chain and Dependencies
  148. @section Installing Build Tool Chain and Dependencies
  149. To successfully compile GNUnet, you need the tools to build GNUnet and
  150. the required dependencies. Please take a look at the
  151. GNUnet Reference Documentation
  152. (@pxref{Dependencies, The GNUnet Reference Documentation,, gnunet, The GNUnet Reference Documentation})
  153. for a list of required dependencies and
  154. (@pxref{Generic installation instructions, The GNUnet Reference Documentation,, gnunet, The GNUnet Reference Documentation})
  155. read its Installation chapter for specific instructions for
  156. your Operating System.
  157. Please check the notes at the end of the configure process about
  158. required dependencies.
  159. For GNUnet bootstrapping support and the HTTP(S) plugin you should
  160. install @uref{https://gnunet.org/en/gnurl.html, libgnurl}.
  161. For the filesharing service you should install at least one of the
  162. datastore backends (MySQL, SQlite and PostgreSQL are supported).
  163. @node Obtaining the latest version from Git
  164. @section Obtaining the latest version from Git
  165. The latest development version can be obtained from our Git repository.
  166. To get the code you need to have @code{Git} installed. Usually your
  167. Operating System package manager should provide a suitable distribution
  168. of git (otherwise check out Guix or Nix). If you are using an Operating
  169. System based on Debian's apt:
  170. @example
  171. $ sudo apt-get install git
  172. @end example
  173. This is required for obtaining the repository, which is achieved with
  174. the following command:
  175. @example
  176. $ git clone https://git.gnunet.org/gnunet
  177. @end example
  178. @noindent
  179. After cloning the repository, you have to execute the @file{bootstrap}
  180. script in the new directory:
  181. @example
  182. $ cd gnunet
  183. $ ./bootstrap
  184. @end example
  185. @noindent
  186. The remainder of this tutorial will assume that you have the
  187. Git branch ``master'' checked out.
  188. @node Compiling and Installing GNUnet
  189. @section Compiling and Installing GNUnet
  190. Note: This section is a duplication of the more in depth
  191. @pxref{GNUnet Installation Handbook, The GNUnet Reference Documentation,, gnunet, The GNUnet Reference Documentation}.
  192. First, you need to install libgnupgerror 1.27 (or later) and
  193. libgcrypt 1.7.6 (or later):
  194. @example
  195. $ export GNUPGFTP="https://www.gnupg.org/ftp/gcrypt"
  196. $ wget $GNUPGFTP/libgpg-error/libgpg-error-1.27.tar.bz2
  197. $ tar xf libgpg-error-1.27.tar.bz2
  198. $ cd libgpg-error-1.27
  199. $ ./configure
  200. $ make
  201. $ sudo make install
  202. $ cd ..
  203. @end example
  204. @example
  205. $ export GNUPGFTP="https://www.gnupg.org/ftp/gcrypt"
  206. $ wget $GNUPGFTP/libgcrypt/libgcrypt-1.7.6.tar.bz2
  207. $ tar xf libgcrypt-1.7.6.tar.bz2
  208. $ cd libgcrypt-1.7.6
  209. $ ./configure
  210. $ make
  211. $ sudo make install
  212. $ cd ..
  213. @end example
  214. @menu
  215. * Installation::
  216. @end menu
  217. @node Installation
  218. @subsection Installation
  219. Assuming all dependencies are installed, the following commands will
  220. compile and install GNUnet in your home directory. You can specify the
  221. directory where GNUnet will be installed by changing the
  222. @code{--prefix} value when calling @command{./configure}. If
  223. you do not specifiy a prefix, GNUnet is installed in the directory
  224. @file{/usr/local}. When developing new applications you may want
  225. to enable verbose logging by adding @code{--enable-logging=verbose}:
  226. @example
  227. $ export PREFIX=$HOME
  228. $ ./configure --prefix=$PREFIX --enable-logging
  229. $ make
  230. $ make install
  231. @end example
  232. @noindent
  233. After installing GNUnet you have to add your GNUnet installation
  234. to your path environmental variable. In addition you have to
  235. create the @file{.config} directory in your home directory
  236. (unless it already exists) where GNUnet stores its data and an
  237. empty GNUnet configuration file:
  238. @example
  239. $ export PATH=$PATH:$PREFIX/bin
  240. $ echo export PATH=$PREFIX/bin:\\$PATH >> ~/.bashrc
  241. $ mkdir ~/.config/
  242. $ touch ~/.config/gnunet.conf
  243. @end example
  244. @node Common Issues - Check your GNUnet installation
  245. @section Common Issues - Check your GNUnet installation
  246. You should check your installation to ensure that installing GNUnet
  247. was successful up to this point. You should be able to access GNUnet's
  248. binaries and run GNUnet's self check.
  249. @example
  250. $ which gnunet-arm
  251. $PREFIX/bin/gnunet-arm
  252. @end example
  253. @noindent
  254. should return $PREFIX/bin/gnunet-arm (where $PREFIX is the location
  255. you have set earlier). It should be located in your
  256. GNUnet installation and the output should not be empty.
  257. If you see an output like:
  258. @example
  259. $ which gnunet-arm
  260. @end example
  261. @noindent
  262. check your PATH variable to ensure GNUnet's @file{bin} directory is
  263. included.
  264. GNUnet provides tests for all of its subcomponents. Assuming you have
  265. successfully built GNUnet, run
  266. @example
  267. $ cd gnunet
  268. $ make check
  269. @end example
  270. @noindent
  271. to execute tests for all components. @command{make check} traverses all
  272. subdirectories in @file{src}. For every subdirectory you should
  273. get a message like this:
  274. @example
  275. make[2]: Entering directory `/home/$USER/gnunet/contrib'
  276. PASS: test_gnunet_prefix
  277. =============
  278. 1 test passed
  279. =============
  280. @end example
  281. @node Introduction to GNUnet Architecture
  282. @chapter Introduction to GNUnet Architecture
  283. GNUnet is organized in layers and services. Each service is composed of a
  284. main service implementation and a client library for other programs to use
  285. the service's functionality, described by an API.
  286. @c This approach is shown in
  287. @c FIXME: enable this once the commented block below works:
  288. @c figure~\ref fig:service.
  289. Some services provide an additional command line tool to enable the user
  290. to interact with the service.
  291. Very often it is other GNUnet services that will use these APIs to build
  292. the higher layers of GNUnet on top of the lower ones. Each layer expands
  293. or extends the functionality of the service below (for instance, to build
  294. a mesh on top of a DHT).
  295. @c FXIME: See comment above.
  296. @c See figure ~\ref fig:interaction for an illustration of this approach.
  297. @c ** @image filename[, width[, height[, alttext[, extension]]]]
  298. @c FIXME: Texlive (?) 20112 makes the assumption that this means
  299. @c 'images/OBJECTNAME.txt' but later versions of it (2017) use this
  300. @c syntax as described below.
  301. @c TODO: Checkout the makedoc script Guile uses.
  302. @c FIXME!!!
  303. @c @image{images/gnunet-tutorial-service,,5in,Service with API and network protocol,.png}
  304. @c @image{images/gnunet-tutorial-system,,5in,The layered system architecture of GNUnet,.png}
  305. @c \begin{figure}[!h]
  306. @c \begin{center}
  307. @c % \begin{subfigure}
  308. @c \begin{subfigure}[b]{0.3\textwidth}
  309. @c \centering
  310. @c \includegraphics[width=\textwidth]{figs/Service.pdf}
  311. @c \caption{Service with API and network protocol}
  312. @c \label{fig:service}
  313. @c \end{subfigure}
  314. @c ~~~~~~~~~~
  315. @c \begin{subfigure}[b]{0.3\textwidth}
  316. @c \centering
  317. @c \includegraphics[width=\textwidth]{figs/System.pdf}
  318. @c \caption{Service interaction}
  319. @c \label{fig:interaction}
  320. @c \end{subfigure}
  321. @c \end{center}
  322. @c \caption{GNUnet's layered system architecture}
  323. @c \end{figure}
  324. The main service implementation runs as a standalone process in the
  325. Operating System and the client code runs as part of the client program,
  326. so crashes of a client do not affect the service process or other clients.
  327. The service and the clients communicate via a message protocol to be
  328. defined and implemented by the programmer.
  329. @node First Steps with GNUnet
  330. @chapter First Steps with GNUnet
  331. @menu
  332. * Configure your peer::
  333. * Start a peer::
  334. * Monitor a peer::
  335. * Starting Two Peers by Hand::
  336. * Starting Peers Using the Testbed Service::
  337. @end menu
  338. @node Configure your peer
  339. @section Configure your peer
  340. First of all we need to configure your peer. Each peer is started with
  341. a configuration containing settings for GNUnet itself and its services.
  342. This configuration is based on the default configuration shipped with
  343. GNUnet and can be modified. The default configuration is located in the
  344. @file{$PREFIX/share/gnunet/config.d} directory. When starting a peer, you
  345. can specify a customized configuration using the the @command{-c} command
  346. line switch when starting the ARM service and all other services. When
  347. using a modified configuration the default values are loaded and only
  348. values specified in the configuration file will replace the default
  349. values.
  350. Since we want to start additional peers later, we need some modifications
  351. from the default configuration. We need to create a separate service
  352. home and a file containing our modifications for this peer:
  353. @example
  354. $ mkdir ~/gnunet1/
  355. $ touch peer1.conf
  356. @end example
  357. @noindent
  358. Now add the following lines to @file{peer1.conf} to use this directory.
  359. For simplified usage we want to prevent the peer to connect to the GNUnet
  360. network since this could lead to confusing output. This modifications
  361. will replace the default settings:
  362. @example
  363. [PATHS]
  364. # Use this directory to store GNUnet data
  365. GNUNET_HOME = ~/gnunet1/
  366. [hostlist]
  367. # prevent bootstrapping
  368. SERVERS =
  369. @end example
  370. @node Start a peer
  371. @section Start a peer
  372. Each GNUnet instance (called peer) has an identity (peer ID) based on a
  373. cryptographic public private key pair. The peer ID is the printable hash
  374. of the public key.
  375. GNUnet services are controlled by a master service, the so called
  376. @dfn{Automatic Restart Manager} (ARM). ARM starts, stops and even
  377. restarts services automatically or on demand when a client connects.
  378. You interact with the ARM service using the @command{gnunet-arm} tool.
  379. GNUnet can then be started with @command{gnunet-arm -s} and stopped with
  380. @command{gnunet-arm -e}. An additional service not automatically started
  381. can be started using @command{gnunet-arm -i <service name>} and stopped
  382. using @command{gnunet-arm -k <servicename>}.
  383. Once you have started your peer, you can use many other GNUnet commands
  384. to interact with it. For example, you can run:
  385. @example
  386. $ gnunet-peerinfo -s
  387. @end example
  388. @noindent
  389. to obtain the public key of your peer.
  390. You should see an output containing the peer ID similar to:
  391. @example
  392. I am peer `0PA02UVRKQTS2C .. JL5Q78F6H0B1ACPV1CJI59MEQUMQCC5G'.
  393. @end example
  394. @node Monitor a peer
  395. @section Monitor a peer
  396. In this section, we will monitor the behaviour of our peer's DHT
  397. service with respect to a specific key. First we will start
  398. GNUnet and then start the DHT service and use the DHT monitor tool
  399. to monitor the PUT and GET commands we issue ussing the
  400. @command{gnunet-dht-put} and @command{gnunet-dht-get} commands.
  401. Using the ``monitor'' line given below, you can observe the behavior
  402. of your own peer's DHT with respect to the specified KEY:
  403. @example
  404. # start gnunet with all default services:
  405. $ gnunet-arm -c ~/peer1.conf -s
  406. # start DHT service:
  407. $ gnunet-arm -c ~/peer1.conf -i dht
  408. $ cd ~/gnunet/src/dht;
  409. $ ./gnunet-dht-monitor -c ~/peer1.conf -k KEY
  410. @end example
  411. @noindent
  412. Now open a separate terminal and change again to
  413. the @file{gnunet/src/dht} directory:
  414. @example
  415. $ cd ~/gnunet/src/dht
  416. # put VALUE under KEY in the DHT:
  417. $ ./gnunet-dht-put -c ~/peer1.conf -k KEY -d VALUE
  418. # get key KEY from the DHT:
  419. $ ./gnunet/src/dht/gnunet-dht-get -c ~/peer1.conf -k KEY
  420. # print statistics about current GNUnet state:
  421. $ gnunet-statistics -c ~/peer1.conf
  422. # print statistics about DHT service:
  423. $ gnunet-statistics -c ~/peer1.conf -s dht
  424. @end example
  425. @node Starting Two Peers by Hand
  426. @section Starting Two Peers by Hand
  427. This section describes how to start two peers on the same machine by hand.
  428. The process is rather painful, but the description is somewhat
  429. instructive. In practice, you might prefer the automated method
  430. (@pxref{Starting Peers Using the Testbed Service}).
  431. @menu
  432. * Setup a second peer::
  433. * Start the second peer and connect the peers::
  434. * How to connect manually::
  435. @end menu
  436. @node Setup a second peer
  437. @subsection Setup a second peer
  438. We will now start a second peer on your machine.
  439. For the second peer, you will need to manually create a modified
  440. configuration file to avoid conflicts with ports and directories.
  441. A peers configuration file is by default located
  442. in @file{~/.gnunet/gnunet.conf}. This file is typically very short
  443. or even empty as only the differences to the defaults need to be
  444. specified. The defaults are located in many files in the
  445. @file{$PREFIX/share/gnunet/config.d} directory.
  446. To configure the second peer, use the files
  447. @file{$PREFIX/share/gnunet/config.d} as a template for your main
  448. configuration file:
  449. @example
  450. $ cat $PREFIX/share/gnunet/config.d/*.conf > peer2.conf
  451. @end example
  452. @noindent
  453. Now you have to edit @file{peer2.conf} and change:
  454. @itemize
  455. @item @code{GNUNET\_TEST\_HOME} under @code{PATHS}
  456. @item Every (uncommented) value for ``@code{PORT}'' (add 10000) in any
  457. section (the option may be commented out if @code{PORT} is
  458. prefixed by "\#", in this case, UNIX domain sockets are used
  459. and the PORT option does not need to be touched)
  460. @item Every value for ``@code{UNIXPATH}'' in any section
  461. (e.g. by adding a "-p2" suffix)
  462. @end itemize
  463. to a fresh, unique value. Make sure that the PORT numbers stay
  464. below 65536. From now on, whenever you interact with the second peer,
  465. you need to specify @command{-c peer2.conf} as an additional
  466. command line argument.
  467. Now, generate the 2nd peer's private key:
  468. @example
  469. $ gnunet-peerinfo -s -c peer2.conf
  470. @end example
  471. @noindent
  472. This may take a while, generate entropy using your keyboard or mouse
  473. as needed. Also, make sure the output is different from the
  474. gnunet-peerinfo output for the first peer (otherwise you made an
  475. error in the configuration).
  476. @node Start the second peer and connect the peers
  477. @subsection Start the second peer and connect the peers
  478. Then, you can start a second peer using:
  479. @example
  480. $ gnunet-arm -c peer2.conf -s
  481. $ gnunet-arm -c peer2.conf -i dht
  482. $ ~/gnunet/src/dht/gnunet-dht-put -c peer2.conf -k KEY -d VALUE
  483. $ ~/gnunet/src/dht/gnunet-dht-get -c peer2.conf -k KEY
  484. @end example
  485. If you want the two peers to connect, you have multiple options:
  486. @itemize
  487. @item UDP neighbour discovery (automatic)
  488. @item Setup a bootstrap server
  489. @item Connect manually
  490. @end itemize
  491. To setup peer 1 as bootstrapping server change the configuration of
  492. the first one to be a hostlist server by adding the following lines to
  493. @file{peer1.conf} to enable bootstrapping server:
  494. @example
  495. [hostlist]
  496. OPTIONS = -p
  497. @end example
  498. @noindent
  499. Then change @file{peer2.conf} and replace the ``@code{SERVERS}''
  500. line in the ``@code{[hostlist]}'' section with
  501. ``@code{http://localhost:8080/}''. Restart both peers using:
  502. @example
  503. # stop first peer
  504. $ gnunet-arm -c peer1.conf -e
  505. # start first peer
  506. $ gnunet-arm -c peer1.conf -s
  507. # start second peer
  508. $ gnunet-arm -c peer2.conf -s
  509. @end example
  510. @noindent
  511. Note that if you start your peers without changing these settings, they
  512. will use the ``global'' hostlist servers of the GNUnet P2P network and
  513. likely connect to those peers. At that point, debugging might become
  514. tricky as you're going to be connected to many more peers and would
  515. likely observe traffic and behaviors that are not explicitly controlled
  516. by you.
  517. @node How to connect manually
  518. @subsection How to connect manually
  519. If you want to use the @code{peerinfo} tool to connect your
  520. peers, you should:
  521. @itemize
  522. @item Set @code{IMMEDIATE_START = NO} in section @code{hostlist}
  523. (to not connect to the global GNUnet)
  524. @item Start both peers running @command{gnunet-arm -c peer1.conf -s}
  525. and @command{gnunet-arm -c peer2.conf -s}
  526. @item Get @code{HELLO} message of the first peer running
  527. @command{gnunet-peerinfo -c peer1.conf -g}
  528. @item Give the output to the second peer by running
  529. @command{gnunet-peerinfo -c peer2.conf -p '<output>'}
  530. @end itemize
  531. Check that they are connected using @command{gnunet-core -c peer1.conf},
  532. which should give you the other peer's peer identity:
  533. @example
  534. $ gnunet-core -c peer1.conf
  535. Peer `9TVUCS8P5A7ILLBGO6 [...shortened...] 1KNBJ4NGCHP3JPVULDG'
  536. @end example
  537. @node Starting Peers Using the Testbed Service
  538. @section Starting Peers Using the Testbed Service
  539. @c \label{sec:testbed}
  540. GNUnet's testbed service is used for testing scenarios where
  541. a number of peers are to be started. The testbed can manage peers
  542. on a single host or on multiple hosts in a distributed fashion.
  543. On a single affordable computer, it should be possible to run
  544. around tens of peers without drastically increasing the load on the
  545. system.
  546. The testbed service can be access through its API
  547. @file{include/gnunet\_testbed\_service.h}. The API provides many
  548. routines for managing a group of peers. It also provides a helper
  549. function @code{GNUNET\_TESTBED\_test\_run()} to quickly setup a
  550. minimalistic testing environment on a single host.
  551. This function takes a configuration file which will be used as a
  552. template configuration for the peers. The testbed takes care of
  553. modifying relevant options in the peers' configuration such as
  554. @code{SERVICEHOME}, @code{PORT}, @code{UNIXPATH} to unique values
  555. so that peers run without running into conflicts. It also checks
  556. and assigns the ports in configurations only if they are free.
  557. Additionally, the testbed service also reads its options from the
  558. same configuration file. Various available options and details
  559. about them can be found in the testbed default configuration file
  560. @file{src/testbed/testbed.conf}.
  561. With the testbed API, a sample test case can be structured as follows:
  562. @example
  563. @verbatiminclude examples/testbed_test.c
  564. @end example
  565. @noindent
  566. The source code for the above listing can be found at
  567. @c FIXME: This is not the correct URL. Where is the file?
  568. @uref{https://git.gnunet.org/gnunet.git/tree/doc/documentation/testbed_test.c}
  569. or in the @file{doc/} folder of your repository check-out.
  570. After installing GNUnet, the above source code can be compiled as:
  571. @example
  572. $ export CPPFLAGS="-I/path/to/gnunet/headers"
  573. $ export LDFLAGS="-L/path/to/gnunet/libraries"
  574. $ gcc $CPPFLAGS $LDFLAGS -o testbed-test testbed_test.c \
  575. -lgnunettestbed -lgnunetdht -lgnunetutil
  576. # Generate (empty) configuration
  577. $ touch template.conf
  578. # run it (press CTRL-C to stop)
  579. $ ./testbed-test
  580. @end example
  581. @noindent
  582. The @code{CPPFLAGS} and @code{LDFLAGS} are necessary if GNUnet
  583. is installed into a different directory other than @file{/usr/local}.
  584. All of testbed API's peer management functions treat management
  585. actions as operations and return operation handles. It is expected
  586. that the operations begin immediately, but they may get delayed (to
  587. balance out load on the system). The program using the API then has
  588. to take care of marking the operation as ``done'' so that its
  589. associated resources can be freed immediately and other waiting
  590. operations can be executed. Operations will be canceled if they are
  591. marked as ``done'' before their completion.
  592. An operation is treated as completed when it succeeds or fails.
  593. Completion of an operation is either conveyed as events through
  594. @dfn{controller event callback} or through respective
  595. @dfn{operation completion callbacks}.
  596. In functions which support completion notification
  597. through both controller event callback and operation
  598. completion callback, first the controller event callback will be
  599. called. If the operation is not marked as done in that callback
  600. or if the callback is given as NULL when creating the operation,
  601. the operation completion callback will be called. The API
  602. documentation shows which event are to be expected in the
  603. controller event notifications. It also documents any exceptional
  604. behaviour.
  605. Once the peers are started, test cases often need to connect
  606. some of the peers' services. Normally, opening a connect to
  607. a peer's service requires the peer's configuration. While using
  608. testbed, the testbed automatically generates per-peer configuration.
  609. Accessing those configurations directly through file system is
  610. discouraged as their locations are dynamically created and will be
  611. different among various runs of testbed. To make access to these
  612. configurations easy, testbed API provides the function
  613. @code{GNUNET\_TESTBED\_service\_connect()}. This function fetches
  614. the configuration of a given peer and calls the @dfn{Connect Adapter}.
  615. In the example code, it is the @code{dht\_ca}. A connect adapter is
  616. expected to open the connection to the needed service by using the
  617. provided configuration and return the created service connection handle.
  618. Successful connection to the needed service is signaled through
  619. @code{service\_connect\_comp\_cb}.
  620. A dual to connect adapter is the @dfn{Disconnect Adapter}. This callback
  621. is called after the connect adapter has been called when the operation
  622. from @code{GNUNET\_TESTBED\_service\_connect()} is marked as ``done''.
  623. It has to disconnect from the service with the provided service
  624. handle (@code{op\_result}).
  625. Exercise: Find out how many peers you can run on your system.
  626. Exercise: Find out how to create a 2D torus topology by changing the
  627. options in the configuration file.
  628. @xref{Supported Topologies, The GNUnet Reference Documentation ,, gnunet, The GNUnet Reference Documentation},
  629. then use the DHT API to store and retrieve values in the network.
  630. @node Developing Applications
  631. @chapter Developing Applications
  632. @menu
  633. * gnunet-ext::
  634. * Adapting the Template::
  635. * Writing a Client Application::
  636. * Writing a Service::
  637. * Interacting directly with other Peers using the CORE Service::
  638. * Storing peer-specific data using the PEERSTORE service::
  639. * Using the DHT::
  640. * Debugging with gnunet-arm::
  641. @end menu
  642. @node gnunet-ext
  643. @section gnunet-ext
  644. To develop a new peer-to-peer application or to extend GNUnet we provide
  645. a template build system for writing GNUnet extensions in C. It can be
  646. obtained as follows:
  647. @example
  648. $ git clone https://git.gnunet.org/gnunet-ext
  649. $ cd gnunet-ext/
  650. $ ./bootstrap
  651. $ ./configure --prefix=$PREFIX --with-gnunet=$PREFIX
  652. $ make
  653. $ make install
  654. $ make check
  655. @end example
  656. @noindent
  657. The GNUnet ext template includes examples and a working buildsystem
  658. for a new GNUnet service. A common GNUnet service consists of the
  659. following parts which will be discussed in detail in the remainder
  660. of this document. The functionality of a GNUnet service is implemented in:
  661. @itemize
  662. @item the GNUnet service (gnunet-ext/src/ext/gnunet-service-ext.c)
  663. @item the client API (gnunet-ext/src/ext/ext_api.c)
  664. @item the client application using the service API
  665. (gnunet-ext/src/ext/gnunet-ext.c)
  666. @end itemize
  667. The interfaces for these entities are defined in:
  668. @itemize
  669. @item client API interface (gnunet-ext/src/ext/ext.h)
  670. @item the service interface (gnunet-ext/src/include/gnunet_service_SERVICE.h)
  671. @item the P2P protocol (gnunet-ext/src/include/gnunet_protocols_ext.h)
  672. @end itemize
  673. In addition the ext systems provides:
  674. @itemize
  675. @item a test testing the API (gnunet-ext/src/ext/test_ext_api.c)
  676. @item a configuration template for the service
  677. (gnunet-ext/src/ext/ext.conf.in)
  678. @end itemize
  679. @node Adapting the Template
  680. @section Adapting the Template
  681. The first step for writing any extension with a new service is to
  682. ensure that the @file{ext.conf.in} file contains entries for the
  683. @code{UNIXPATH}, @code{PORT} and @code{BINARY} for the service in a
  684. section named after the service.
  685. If you want to adapt the template rename the @file{ext.conf.in} to
  686. match your services name, you have to modify the @code{AC\_OUTPUT}
  687. section in @file{configure.ac} in the @file{gnunet-ext} root.
  688. @node Writing a Client Application
  689. @section Writing a Client Application
  690. When writing any client application (for example, a command-line
  691. tool), the basic structure is to start with the
  692. @code{GNUNET\_PROGRAM\_run} function. This function will parse
  693. command-line options, setup the scheduler and then invoke the
  694. @code{run} function (with the remaining non-option arguments)
  695. and a handle to the parsed configuration (and the configuration
  696. file name that was used, which is typically not needed):
  697. @example
  698. @verbatiminclude examples/001.c
  699. @end example
  700. @menu
  701. * Handling command-line options::
  702. * Writing a Client Library::
  703. * Writing a user interface::
  704. @end menu
  705. @node Handling command-line options
  706. @subsection Handling command-line options
  707. Options can then be added easily by adding global variables and
  708. expanding the @code{options} array. For example, the following would
  709. add a string-option and a binary flag (defaulting to @code{NULL} and
  710. @code{GNUNET\_NO} respectively):
  711. @example
  712. @verbatiminclude examples/002.c
  713. @end example
  714. Issues such as displaying some helpful text describing options using
  715. the @code{--help} argument and error handling are taken care of when
  716. using this approach. Other @code{GNUNET\_GETOPT\_}-functions can be used
  717. to obtain integer value options, increment counters, etc. You can
  718. even write custom option parsers for special circumstances not covered
  719. by the available handlers. To check if an argument was specified by the
  720. user you initialize the variable with a specific value (e.g. NULL for
  721. a string and GNUNET\_SYSERR for a integer) and check after parsing
  722. happened if the values were modified.
  723. Inside the @code{run} method, the program would perform the
  724. application-specific logic, which typically involves initializing and
  725. using some client library to interact with the service. The client
  726. library is supposed to implement the IPC whereas the service provides
  727. more persistent P2P functions.
  728. Exercise: Add a few command-line options and print them inside
  729. of @code{run}. What happens if the user gives invalid arguments?
  730. @node Writing a Client Library
  731. @subsection Writing a Client Library
  732. The first and most important step in writing a client library is to
  733. decide on an API for the library. Typical API calls include
  734. connecting to the service, performing application-specific requests
  735. and cleaning up. Many examples for such service APIs can be found
  736. in the @file{gnunet/src/include/gnunet\_*\_service.h} files.
  737. Then, a client-service protocol needs to be designed. This typically
  738. involves defining various message formats in a header that will be
  739. included by both the service and the client library (but is otherwise
  740. not shared and hence located within the service's directory and not
  741. installed by @command{make install}). Each message must start with a
  742. @code{struct GNUNET\_MessageHeader} and must be shorter than 64k. By
  743. convention, all fields in IPC (and P2P) messages must be in big-endian
  744. format (and thus should be read using @code{ntohl} and similar
  745. functions and written using @code{htonl} and similar functions).
  746. Unique message types must be defined for each message struct in the
  747. @file{gnunet\_protocols.h} header (or an extension-specific include
  748. file).
  749. @menu
  750. * Connecting to the Service::
  751. * Sending messages::
  752. * Receiving Replies from the Service::
  753. @end menu
  754. @node Connecting to the Service
  755. @subsubsection Connecting to the Service
  756. Before a client library can implement the application-specific protocol
  757. with the service, a connection must be created:
  758. @example
  759. @verbatiminclude examples/003.c
  760. @end example
  761. @noindent
  762. As a result a @code{GNUNET\_MQ\_Handle} is returned
  763. which can to used henceforth to transmit messages to the service.
  764. The complete MQ API can be found in @file{gnunet\_mq\_lib.h}.
  765. The @code{hanlders} array in the example above is incomplete.
  766. Here is where you will define which messages you expect to
  767. receive from the service, and which functions handle them.
  768. The @code{error\_cb} is a function that is to be called whenever
  769. there are errors communicating with the service.
  770. @node Sending messages
  771. @subsubsection Sending messages
  772. In GNUnet, messages are always sent beginning with a
  773. @code{struct GNUNET\_MessageHeader} in big endian format.
  774. This header defines the size and the type of the
  775. message, the payload follows after this header.
  776. @example
  777. @verbatiminclude examples/004.c
  778. @end example
  779. @noindent
  780. Existing message types are defined in @file{gnunet\_protocols.h}.
  781. A common way to create a message is with an envelope:
  782. @example
  783. @verbatiminclude examples/005.c
  784. @end example
  785. @noindent
  786. Exercise: Define a message struct that includes a 32-bit
  787. unsigned integer in addition to the standard GNUnet MessageHeader.
  788. Add a C struct and define a fresh protocol number for your message.
  789. Protocol numbers in gnunet-ext are defined
  790. in @file{gnunet-ext/src/include/gnunet_protocols_ext.h}
  791. Exercise: Find out how you can determine the number of messages
  792. in a message queue.
  793. Exercise: Find out how you can determine when a message you
  794. have queued was actually transmitted.
  795. Exercise: Define a helper function to transmit a 32-bit
  796. unsigned integer (as payload) to a service using some given client
  797. handle.
  798. @node Receiving Replies from the Service
  799. @subsubsection Receiving Replies from the Service
  800. Clients can receive messages from the service using the handlers
  801. specified in the @code{handlers} array we specified when connecting
  802. to the service. Entries in the the array are usually created using
  803. one of two macros, depending on whether the message is fixed size
  804. or variable size. Variable size messages are managed using two
  805. callbacks, one to check that the message is well-formed, the other
  806. to actually process the message. Fixed size messages are fully
  807. checked by the MQ-logic, and thus only need to provide the handler
  808. to process the message. Note that the prefixes @code{check\_}
  809. and @code{handle\_} are mandatory.
  810. @example
  811. @verbatiminclude examples/006.c
  812. @end example
  813. @noindent
  814. Exercise: Expand your helper function to receive a response message
  815. (for example, containing just the @code{struct GNUnet MessageHeader}
  816. without any payload). Upon receiving the service's response, you
  817. should call a callback provided to your helper function's API.
  818. Exercise: Figure out where you can pass values to the
  819. closures (@code{cls}).
  820. @node Writing a user interface
  821. @subsection Writing a user interface
  822. Given a client library, all it takes to access a service now is to
  823. combine calls to the client library with parsing command-line
  824. options.
  825. Exercise: Call your client API from your @code{run()} method in your
  826. client application to send a request to the service. For example,
  827. send a 32-bit integer value based on a number given at the
  828. command-line to the service.
  829. @node Writing a Service
  830. @section Writing a Service
  831. Before you can test the client you've written so far, you'll
  832. need to also implement the corresponding service.
  833. @menu
  834. * Code Placement::
  835. * Starting a Service::
  836. @end menu
  837. @node Code Placement
  838. @subsection Code Placement
  839. New services are placed in their own subdirectory under
  840. @file{gnunet/src}. This subdirectory should contain the API
  841. implementation file @file{SERVICE\_api.c}, the description of
  842. the client-service protocol @file{SERVICE.h} and P2P protocol
  843. @file{SERVICE\_protocol.h}, the implementation of the service itself
  844. @file{gnunet-service-SERVICE.h} and several files for tests,
  845. including test code and configuration files.
  846. @node Starting a Service
  847. @subsection Starting a Service
  848. The key API definition for creating a service is the
  849. @code{GNUNET\_SERVICE\_MAIN} macro:
  850. @example
  851. @verbatiminclude examples/007.c
  852. @end example
  853. @noindent
  854. In addition to the service name and flags, the macro takes three
  855. functions, typically called @code{run}, @code{client\_connect\_cb} and
  856. @code{client\_disconnect\_cb} as well as an array of message handlers
  857. that will be called for incoming messages from clients.
  858. A minimal version of the three central service funtions would look
  859. like this:
  860. @example
  861. @verbatiminclude examples/008.c
  862. @end example
  863. @noindent
  864. Exercise: Write a stub service that processes no messages at all
  865. in your code. Create a default configuration for it, integrate it
  866. with the build system and start the service from
  867. @command{gnunet-service-arm} using @command{gnunet-arm -i NAME}.
  868. Exercise: Figure out how to set the closure (@code{cls}) for handlers
  869. of a service.
  870. Exercise: Figure out how to send messages from the service back to the
  871. client.
  872. Each handler function in the service @b{must} eventually (possibly in some
  873. asynchronous continuation) call
  874. @code{GNUNET\_SERVICE\_client\_continue()}. Only after this call
  875. additional messages from the same client may
  876. be processed. This way, the service can throttle processing messages
  877. from the same client.
  878. Exercise: Change the service to ``handle'' the message from your
  879. client (for now, by printing a message). What happens if you
  880. forget to call @code{GNUNET\_SERVICE\_client\_continue()}?
  881. @node Interacting directly with other Peers using the CORE Service
  882. @section Interacting directly with other Peers using the CORE Service
  883. FIXME: This section still needs to be updated to the lastest API!
  884. One of the most important services in GNUnet is the @code{CORE} service
  885. managing connections between peers and handling encryption between peers.
  886. One of the first things any service that extends the P2P protocol
  887. typically does is connect to the @code{CORE} service using:
  888. @example
  889. @verbatiminclude examples/009.c
  890. @end example
  891. @menu
  892. * New P2P connections::
  893. * Receiving P2P Messages::
  894. * Sending P2P Messages::
  895. * End of P2P connections::
  896. @end menu
  897. @node New P2P connections
  898. @subsection New P2P connections
  899. Before any traffic with a different peer can be exchanged, the peer must
  900. be known to the service. This is notified by the @code{CORE}
  901. @code{connects} callback, which communicates the identity of the new
  902. peer to the service:
  903. @example
  904. @verbatiminclude examples/010.c
  905. @end example
  906. @noindent
  907. Note that whatever you return from @code{connects} is given as the
  908. @code{cls} argument to the message handlers for messages from
  909. the respective peer.
  910. Exercise: Create a service that connects to the @code{CORE}. Then
  911. start (and connect) two peers and print a message once your connect
  912. callback is invoked.
  913. @node Receiving P2P Messages
  914. @subsection Receiving P2P Messages
  915. To receive messages from @code{CORE}, you pass the desired
  916. @code{handlers} to the @code{GNUNET\_CORE\_connect()} function,
  917. just as we showed for services.
  918. It is your responsibility to process messages fast enough or
  919. to implement flow control. If an application does not process
  920. CORE messages fast enough, CORE will randomly drop messages
  921. to not keep a very long queue in memory.
  922. Exercise: Start one peer with a new service that has a message
  923. handler and start a second peer that only has your ``old'' service
  924. without message handlers. Which ``connect'' handlers are invoked when
  925. the two peers are connected? Why?
  926. @node Sending P2P Messages
  927. @subsection Sending P2P Messages
  928. You can transmit messages to other peers using the @code{mq} you were
  929. given during the @code{connect} callback. Note that the @code{mq}
  930. automatically is released upon @code{disconnect} and that you must
  931. not use it afterwards.
  932. It is your responsibility to not over-fill the message queue, GNUnet
  933. will send the messages roughly in the order given as soon as possible.
  934. Exercise: Write a service that upon connect sends messages as
  935. fast as possible to the other peer (the other peer should run a
  936. service that ``processes'' those messages). How fast is the
  937. transmission? Count using the STATISTICS service on both ends. Are
  938. messages lost? How can you transmit messages faster? What happens if
  939. you stop the peer that is receiving your messages?
  940. @node End of P2P connections
  941. @subsection End of P2P connections
  942. If a message handler returns @code{GNUNET\_SYSERR}, the remote
  943. peer shuts down or there is an unrecoverable network
  944. disconnection, CORE notifies the service that the peer disconnected.
  945. After this notification no more messages will be received from the
  946. peer and the service is no longer allowed to send messages to the peer.
  947. The disconnect callback looks like the following:
  948. @example
  949. @verbatiminclude examples/011.c
  950. @end example
  951. @noindent
  952. Exercise: Fix your service to handle peer disconnects.
  953. @node Storing peer-specific data using the PEERSTORE service
  954. @section Storing peer-specific data using the PEERSTORE service
  955. GNUnet's PEERSTORE service offers a persistorage for arbitrary
  956. peer-specific data. Other GNUnet services can use the PEERSTORE
  957. to store, retrieve and monitor data records. Each data record
  958. stored with PEERSTORE contains the following fields:
  959. @itemize
  960. @item subsystem: Name of the subsystem responsible for the record.
  961. @item peerid: Identity of the peer this record is related to.
  962. @item key: a key string identifying the record.
  963. @item value: binary record value.
  964. @item expiry: record expiry date.
  965. @end itemize
  966. The first step is to start a connection to the PEERSTORE service:
  967. @example
  968. @verbatiminclude examples/012.c
  969. @end example
  970. The service handle @code{peerstore_handle} will be needed for
  971. all subsequent PEERSTORE operations.
  972. @menu
  973. * Storing records::
  974. * Retrieving records::
  975. * Monitoring records::
  976. * Disconnecting from PEERSTORE::
  977. @end menu
  978. @node Storing records
  979. @subsection Storing records
  980. To store a new record, use the following function:
  981. @example
  982. @verbatiminclude examples/013.c
  983. @end example
  984. @noindent
  985. The @code{options} parameter can either be
  986. @code{GNUNET_PEERSTORE_STOREOPTION_MULTIPLE} which means that multiple
  987. values can be stored under the same key combination
  988. (subsystem, peerid, key), or @code{GNUNET_PEERSTORE_STOREOPTION_REPLACE}
  989. which means that PEERSTORE will replace any existing values under the
  990. given key combination (subsystem, peerid, key) with the new given value.
  991. The continuation function @code{cont} will be called after the store
  992. request is successfully sent to the PEERSTORE service. This does not
  993. guarantee that the record is successfully stored, only that it was
  994. received by the service.
  995. The @code{GNUNET_PEERSTORE_store} function returns a handle to the store
  996. operation. This handle can be used to cancel the store operation only
  997. before the continuation function is called:
  998. @example
  999. @verbatiminclude examples/013.1.c
  1000. @end example
  1001. @node Retrieving records
  1002. @subsection Retrieving records
  1003. To retrieve stored records, use the following function:
  1004. @example
  1005. @verbatiminclude examples/014.c
  1006. @end example
  1007. @noindent
  1008. The values of @code{peer} and @code{key} can be @code{NULL}. This
  1009. allows the iteration over values stored under any of the following
  1010. key combinations:
  1011. @itemize
  1012. @item (subsystem)
  1013. @item (subsystem, peerid)
  1014. @item (subsystem, key)
  1015. @item (subsystem, peerid, key)
  1016. @end itemize
  1017. The @code{callback} function will be called once with each retrieved
  1018. record and once more with a @code{NULL} record to signal the end of
  1019. results.
  1020. The @code{GNUNET_PEERSTORE_iterate} function returns a handle to the
  1021. iterate operation. This handle can be used to cancel the iterate
  1022. operation only before the callback function is called with a
  1023. @code{NULL} record.
  1024. @node Monitoring records
  1025. @subsection Monitoring records
  1026. PEERSTORE offers the functionality of monitoring for new records
  1027. stored under a specific key combination (subsystem, peerid, key).
  1028. To start the monitoring, use the following function:
  1029. @example
  1030. @verbatiminclude examples/015.c
  1031. @end example
  1032. @noindent
  1033. Whenever a new record is stored under the given key combination,
  1034. the @code{callback} function will be called with this new
  1035. record. This will continue until the connection to the PEERSTORE
  1036. service is broken or the watch operation is canceled:
  1037. @example
  1038. @verbatiminclude examples/016.c
  1039. @end example
  1040. @node Disconnecting from PEERSTORE
  1041. @subsection Disconnecting from PEERSTORE
  1042. When the connection to the PEERSTORE service is no longer needed,
  1043. disconnect using the following function:
  1044. @example
  1045. @verbatiminclude examples/017.c
  1046. @end example
  1047. @noindent
  1048. If the @code{sync_first} flag is set to @code{GNUNET_YES},
  1049. the API will delay the disconnection until all store requests
  1050. are received by the PEERSTORE service. Otherwise, it will
  1051. disconnect immediately.
  1052. @node Using the DHT
  1053. @section Using the DHT
  1054. The DHT allows to store data so other peers in the P2P network can
  1055. access it and retrieve data stored by any peers in the network.
  1056. This section will explain how to use the DHT. Of course, the first
  1057. thing to do is to connect to the DHT service:
  1058. @example
  1059. @verbatiminclude examples/018.c
  1060. @end example
  1061. @noindent
  1062. The second parameter indicates how many requests in parallel to expect.
  1063. It is not a hard limit, but a good approximation will make the DHT more
  1064. efficient.
  1065. @menu
  1066. * Storing data in the DHT::
  1067. * Obtaining data from the DHT::
  1068. * Implementing a block plugin::
  1069. * Monitoring the DHT::
  1070. @end menu
  1071. @node Storing data in the DHT
  1072. @subsection Storing data in the DHT
  1073. Since the DHT is a dynamic environment (peers join and leave frequently)
  1074. the data that we put in the DHT does not stay there indefinitely. It is
  1075. important to ``refresh'' the data periodically by simply storing it
  1076. again, in order to make sure other peers can access it.
  1077. The put API call offers a callback to signal that the PUT request has been
  1078. sent. This does not guarantee that the data is accessible to others peers,
  1079. or even that is has been stored, only that the service has requested to
  1080. a neighboring peer the retransmission of the PUT request towards its final
  1081. destination. Currently there is no feedback about whether or not the data
  1082. has been sucessfully stored or where it has been stored. In order to
  1083. improve the availablilty of the data and to compensate for possible
  1084. errors, peers leaving and other unfavorable events, just make several
  1085. PUT requests!
  1086. @example
  1087. @verbatiminclude examples/019.c
  1088. @end example
  1089. @noindent
  1090. Exercise: Store a value in the DHT periodically to make sure it
  1091. is available over time. You might consider using the function
  1092. @code{GNUNET\_SCHEDULER\_add\_delayed} and call
  1093. @code{GNUNET\_DHT\_put} from inside a helper function.
  1094. @node Obtaining data from the DHT
  1095. @subsection Obtaining data from the DHT
  1096. As we saw in the previous example, the DHT works in an asynchronous mode.
  1097. Each request to the DHT is executed ``in the background'' and the API
  1098. calls return immediately. In order to receive results from the DHT, the
  1099. API provides a callback. Once started, the request runs in the service,
  1100. the service will try to get as many results as possible (filtering out
  1101. duplicates) until the timeout expires or we explicitly stop the request.
  1102. It is possible to give a ``forever'' timeout with
  1103. @code{GNUNET\_TIME\_UNIT\_FOREVER\_REL}.
  1104. If we give a route option @code{GNUNET\_DHT\_RO\_RECORD\_ROUTE}
  1105. the callback will get a list of all the peers the data has travelled,
  1106. both on the PUT path and on the GET path.
  1107. @example
  1108. @verbatiminclude examples/020.c
  1109. @end example
  1110. @noindent
  1111. Exercise: Store a value in the DHT and after a while retrieve it.
  1112. Show the IDs of all the peers the requests have gone through.
  1113. In order to convert a peer ID to a string, use the function
  1114. @code{GNUNET\_i2s}. Pay attention to the route option parameters
  1115. in both calls!
  1116. @node Implementing a block plugin
  1117. @subsection Implementing a block plugin
  1118. In order to store data in the DHT, it is necessary to provide a block
  1119. plugin. The DHT uses the block plugin to ensure that only well-formed
  1120. requests and replies are transmitted over the network.
  1121. The block plugin should be put in a file @file{plugin\_block\_SERVICE.c}
  1122. in the service's respective directory. The
  1123. mandatory functions that need to be implemented for a block plugin are
  1124. described in the following sections.
  1125. @menu
  1126. * Validating requests and replies::
  1127. * Deriving a key from a reply::
  1128. * Initialization of the plugin::
  1129. * Shutdown of the plugin::
  1130. * Integration of the plugin with the build system::
  1131. @end menu
  1132. @node Validating requests and replies
  1133. @subsubsection Validating requests and replies
  1134. The evaluate function should validate a reply or a request. It returns
  1135. a @code{GNUNET\_BLOCK\_EvaluationResult}, which is an enumeration. All
  1136. possible answers are in @file{gnunet\_block\_lib.h}. The function will
  1137. be called with a @code{reply\_block} argument of @code{NULL} for
  1138. requests. Note that depending on how @code{evaluate} is called, only
  1139. some of the possible return values are valid. The specific meaning of
  1140. the @code{xquery} argument is application-specific. Applications that
  1141. do not use an extended query should check that the @code{xquery\_size}
  1142. is zero. The block group is typically used to filter duplicate
  1143. replies.
  1144. @example
  1145. @verbatiminclude examples/021.c
  1146. @end example
  1147. @noindent
  1148. Note that it is mandatory to detect duplicate replies in this function
  1149. and return the respective status code. Duplicate detection is
  1150. typically done using the Bloom filter block group provided by
  1151. @file{libgnunetblockgroup.so}. Failure to do so may cause replies to
  1152. circle in the network.
  1153. @node Deriving a key from a reply
  1154. @subsubsection Deriving a key from a reply
  1155. The DHT can operate more efficiently if it is possible to derive a key
  1156. from the value of the corresponding block. The @code{get\_key}
  1157. function is used to obtain the key of a block --- for example, by
  1158. means of hashing. If deriving the key is not possible, the function
  1159. should simply return @code{GNUNET\_SYSERR} (the DHT will still work
  1160. just fine with such blocks).
  1161. @example
  1162. @verbatiminclude examples/022.c
  1163. @end example
  1164. @node Initialization of the plugin
  1165. @subsubsection Initialization of the plugin
  1166. The plugin is realized as a shared C library. The library must export
  1167. an initialization function which should initialize the plugin. The
  1168. initialization function specifies what block types the plugin cares
  1169. about and returns a struct with the functions that are to be used for
  1170. validation and obtaining keys (the ones just defined above).
  1171. @example
  1172. @verbatiminclude examples/023.c
  1173. @end example
  1174. @node Shutdown of the plugin
  1175. @subsubsection Shutdown of the plugin
  1176. Following GNUnet's general plugin API concept, the plugin must
  1177. export a second function for cleaning up. It usually does very
  1178. little.
  1179. @example
  1180. @verbatiminclude examples/024.c
  1181. @end example
  1182. @node Integration of the plugin with the build system
  1183. @subsubsection Integration of the plugin with the build system
  1184. In order to compile the plugin, the @file{Makefile.am} file for the
  1185. service SERVICE should contain a rule similar to this:
  1186. @c Actually this is a Makefile not C. But the whole structure of examples
  1187. @c must be improved.
  1188. @example
  1189. @verbatiminclude examples/025.Makefile.am
  1190. @end example
  1191. @noindent
  1192. Exercise: Write a block plugin that accepts all queries
  1193. and all replies but prints information about queries and replies
  1194. when the respective validation hooks are called.
  1195. @node Monitoring the DHT
  1196. @subsection Monitoring the DHT
  1197. It is possible to monitor the functioning of the local
  1198. DHT service. When monitoring the DHT, the service will
  1199. alert the monitoring program of any events, both started
  1200. locally or received for routing from another peer.
  1201. The are three different types of events possible: a
  1202. GET request, a PUT request or a response (a reply to a GET).
  1203. Since the different events have different associated data,
  1204. the API gets 3 different callbacks (one for each message type)
  1205. and optional type and key parameters, to allow for filtering of
  1206. messages. When an event happens, the appropiate callback is
  1207. called with all the information about the event.
  1208. @example
  1209. @verbatiminclude examples/026.c
  1210. @end example
  1211. @node Debugging with gnunet-arm
  1212. @section Debugging with gnunet-arm
  1213. Even if services are managed by @command{gnunet-arm}, you can
  1214. start them with @command{gdb} or @command{valgrind}. For
  1215. example, you could add the following lines to your
  1216. configuration file to start the DHT service in a @command{gdb}
  1217. session in a fresh @command{xterm}:
  1218. @example
  1219. [dht]
  1220. PREFIX=xterm -e gdb --args
  1221. @end example
  1222. @noindent
  1223. Alternatively, you can stop a service that was started via
  1224. ARM and run it manually:
  1225. @example
  1226. $ gnunet-arm -k dht
  1227. $ gdb --args gnunet-service-dht -L DEBUG
  1228. $ valgrind gnunet-service-dht -L DEBUG
  1229. @end example
  1230. @noindent
  1231. Assuming other services are well-written, they will automatically
  1232. re-integrate the restarted service with the peer.
  1233. GNUnet provides a powerful logging mechanism providing log
  1234. levels @code{ERROR}, @code{WARNING}, @code{INFO} and @code{DEBUG}.
  1235. The current log level is configured using the @code{$GNUNET_FORCE_LOG}
  1236. environmental variable. The @code{DEBUG} level is only available if
  1237. @command{--enable-logging=verbose} was used when running
  1238. @command{configure}. More details about logging can be found under
  1239. @uref{https://docs.gnunet.org/#Logging}.
  1240. You should also probably enable the creation of core files, by setting
  1241. @code{ulimit}, and echo'ing @code{1} into
  1242. @file{/proc/sys/kernel/core\_uses\_pid}. Then you can investigate the
  1243. core dumps with @command{gdb}, which is often the fastest method to
  1244. find simple errors.
  1245. Exercise: Add a memory leak to your service and obtain a trace
  1246. pointing to the leak using @command{valgrind} while running the service
  1247. from @command{gnunet-service-arm}.
  1248. @c *********************************************************************
  1249. @node GNU Free Documentation License
  1250. @appendix GNU Free Documentation License
  1251. @cindex license, GNU Free Documentation License
  1252. @include fdl-1.3.texi
  1253. @bye