Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / doc / gnunet-c-tutorial.tex
1 \documentclass[10pt]{article}
2 \usepackage[ansinew]{inputenc}
3 \usepackage{makeidx,amsmath,amssymb,exscale,multicol,epsfig,graphics,verbatim,ulem}
4 \usepackage{epsfig,geometry,url,listings, subcaption}
5 \usepackage{boxedminipage}
6 \usepackage[T1]{fontenc}%required
7 \usepackage{textcomp}
8 \geometry{headsep=3ex,hscale=0.9}
9 \usepackage{hyperref}
10 \hypersetup{pdftitle={GNUnet C Tutorial},
11   pdfsubject={GNUnet},
12   pdfauthor={Christian Grothoff <christian@grothoff.org>},
13   pdfkeywords={p2p,search,gnunet,tutorial}
14   %,pdfpagemode={FullScreen}
15   }
16
17
18 \lstset{
19 language=bash,
20 basicstyle=\ttfamily,
21 upquote=true,
22 columns=fullflexible,
23 literate={*}{{\char42}}1
24          {-}{{\char45}}1
25 }
26
27 \newcommand{\exercise}[1]{\noindent\begin{boxedminipage}{\textwidth}{\bf Exercise:} #1 \end{boxedminipage}}
28
29 \begin{document}
30
31 \begin{center}
32 \large {A Tutorial for GNUnet 0.10.x (C version)}
33
34 Christian Grothoff $\qquad$ Bart Polot $\qquad$ Matthias Wachs
35
36 \today
37 \end{center}
38 This tutorials explains how to install GNUnet on a GNU/Linux system and gives an introduction on how
39 GNUnet can be used to develop a Peer-to-Peer application. Detailed installation instructions for
40 various operating systems and a detailed list of all dependencies can be found on our website at
41 \url{https://gnunet.org/installation}.
42
43 \textbf{Please read this tutorial carefully since every single step is
44   important and do not hesitate to contact the GNUnet team if you have
45   any questions or problems! Check here how to contact the GNUnet
46   team: \url{https://gnunet.org/contact_information}}
47
48
49 \section{Installing GNUnet}
50
51 First of all you have to install a current version of GNUnet. You can download a
52 tarball of a stable version from GNU FTP mirrors or obtain the latest development
53 version from our Git repository.
54
55 Most of the time you should prefer to download the stable version since with the
56 latest development version things can be broken, functionality can be changed or tests
57 can fail. You should only use the development version if you know that you require a
58 certain feature or a certain issue has been fixed since the last release.
59
60 \subsection{Obtaining a stable version}
61
62 You can download the latest stable version of GNUnet from GNU FTP mirrors:
63 \begin{center}
64 \url{ftp://ftp.gnu.org/gnu/gnunet/gnunet-0.10.x.tar.gz}
65 \end{center}
66 You should also download the signature file and verify the integrity of the tarball.
67 \begin{center}
68 \url{ftp://ftp.gnu.org/gnu/gnunet/gnunet-0.10.x.tar.gz.sig}
69 \end{center}
70 To verify the signature you should first import the GPG key used to sign the tarball
71 \begin{lstlisting}
72 $ gpg --keyserver keys.gnupg.net --recv-keys 48426C7E
73 \end{lstlisting}
74 And use this key to verify the tarball's signature
75 \begin{lstlisting}
76 $ gpg --verify gnunet-0.10.x.tar.gz.sig gnunet-0.10.x.tar.gz
77 \end{lstlisting}
78 After successfully verifying the integrity you can extract the tarball using
79 \begin{lstlisting}
80 $ tar xvzf gnunet-0.10.x.tar.gz
81 $ mv gnunet-0.10.x gnunet               # we will use the directory "gnunet" in the remainder of this document
82 $ cd gnunet
83 \end{lstlisting}
84
85
86 \subsection{Installing Build Tool Chain and Dependencies}
87
88 To successfully compile GNUnet you need the tools to build GNUnet and the required dependencies.
89 Please have a look at \url{https://gnunet.org/dependencies} for a list of required dependencies
90 and \url{https://gnunet.org/generic_installation} for specific instructions for your operating system.
91
92 Please check the notes at the end of the configure process about required dependencies.
93
94 For GNUnet bootstrapping support and the http(s) plugin you should install \texttt{libgnurl}.
95 For the filesharing service you should install at least one of the datastore backends \texttt{mysql},
96 \texttt{sqlite} or \texttt{postgresql}.
97
98 \subsection{Obtaining the latest version from Git}
99
100 The latest development version can obtained from our Git repository. To obtain
101 the code you need Git installed and checkout the repository using:
102 \lstset{language=bash}
103 \begin{lstlisting}
104 $ git clone https://gnunet.org/git/gnunet
105 \end{lstlisting}
106 After cloning the repository you have to execute
107 \lstset{language=bash}
108 \begin{lstlisting}
109 $ cd gnunet
110 $ ./bootstrap
111 \end{lstlisting}
112
113 The remainder of this tutorial assumes that you have Git Master checked out.
114
115
116 \subsection{Compiling and Installing GNUnet}
117
118 First, you need to install at least {\tt libgnupgerror} version
119 1.12\footnote{\url{ftp://ftp.gnupg.org/gcrypt/libgpg-error/libgpg-error-1.12.tar.bz2}}
120 and {\tt libgcrypt} version
121 1.6\footnote{\url{ftp://ftp.gnupg.org/gcrypt/libgcrypt/libgcrypt-1.6.0.tar.bz2}}.
122
123 \lstset{language=bash}
124 \begin{lstlisting}
125 $ wget ftp://ftp.gnupg.org/gcrypt/libgpg-error/libgpg-error-1.12.tar.bz2
126 $ tar xf libgpg-error-1.12.tar.bz2
127 $ cd libgpg-error-1.12
128 $ ./configure
129 $ sudo make install
130 $ cd ..
131 \end{lstlisting}
132
133 \lstset{language=bash}
134 \begin{lstlisting}
135 $ wget ftp://ftp.gnupg.org/gcrypt/libgcrypt/libgcrypt-1.6.0.tar.bz2
136 $ tar xf libgcrypt-1.6.0.tar.bz2
137 $ cd libgcrypt-1.6.0
138 $ ./configure
139 $ sudo make install
140 $ cd ..
141 \end{lstlisting}
142
143 \label{sub:install}
144 Assuming all dependencies are installed, the following commands will
145 compile and install GNUnet in your home directory. You can specify the
146 directory where GNUnet will be installed by changing the
147 \lstinline|--prefix| value when calling \lstinline|./configure|.  If
148 you do not specifiy a prefix, GNUnet is installed in the directory
149 \lstinline|/usr/local|. When developing new applications you may want
150 to enable verbose logging by adding
151 \lstinline|--enable-logging=verbose|:
152
153 \lstset{language=bash}
154 \begin{lstlisting}
155 $ ./configure --prefix=$PREFIX --enable-logging
156 $ make
157 $ make install
158 \end{lstlisting}
159
160 After installing GNUnet you have to add your GNUnet installation to your path
161 environmental variable. In addition you have to create the \lstinline|.config|
162 directory in your home directory where GNUnet stores its data and an empty
163 GNUnet configuration file:
164
165 \lstset{language=bash}
166 \begin{lstlisting}
167 $ export PATH=$PATH:$PREFIX/bin
168 $ echo export PATH=$PREFIX/bin:\\$PATH >> ~/.bashrc
169 $ mkdir ~/.config/
170 $ touch ~/.config/gnunet.conf
171 \end{lstlisting}
172 % $
173
174 \subsection{Common Issues - Check your GNUnet installation}
175
176 You should check your installation to ensure that installing GNUnet
177 was successful up to this point. You should be able to access GNUnet's
178 binaries and run GNUnet's self check.
179 \begin{lstlisting}
180 $ which gnunet-arm
181 \end{lstlisting}
182 should return \lstinline|$PREFIX/bin/gnunet-arm|. It should be
183 located in your GNUnet installation and the output should not be
184 empty. If you see an output like:
185 \begin{lstlisting}
186 $ which gnunet-arm
187 $
188 \end{lstlisting}
189 check your {\tt PATH} variable to ensure GNUnet's {\tt bin} directory is included.
190
191 GNUnet provides tests for all of its subcomponents. Run
192 \begin{lstlisting}
193 $ make check
194 \end{lstlisting}
195 to execute tests for all components. {\tt make check} traverses all subdirectories in {\tt src}.
196 For every subdirectory you should get a message like this:
197
198 \begin{lstlisting}
199 make[2]: Entering directory `/home/$USER/gnunet/contrib'
200 PASS: test_gnunet_prefix
201 =============
202 1 test passed
203 =============
204 \end{lstlisting}
205
206 If you see a message like this:
207
208 \begin{lstlisting}
209 Mar 12 16:57:56-642482 resolver-api-19449 ERROR Must specify `HOSTNAME' for `resolver' in configuration!
210 Mar 12 16:57:56-642573 test_program-19449 ERROR Assertion failed at resolver_api.c:204.
211 /bin/bash: line 5: 19449 Aborted                 (core dumped) ${dir}$tst
212 FAIL: test_program
213 \end{lstlisting}
214 double check the steps performed in ~\ref{sub:install}
215
216
217 \section{Background: GNUnet Architecture}
218
219 GNUnet is organized in layers and services. Each service is composed of a
220 main service implementation and a client library for other programs to use
221 the service's functionality, described by an API. This approach is shown in
222 figure~\ref{fig:service}. Some services provide an additional command line
223 tool to enable the user to interact with the service.
224
225 Very often it is other GNUnet services that will use these APIs to build the
226 higher layers of GNUnet on top of the lower ones. Each layer expands or extends
227 the functionality of the service below (for instance, to build a mesh on top of
228 a DHT). See figure ~\ref{fig:interaction} for an illustration of this approach.
229
230 \begin{figure}[!h]
231   \begin{center}
232 %  \begin{subfigure}
233         \begin{subfigure}[b]{0.3\textwidth}
234                 \centering
235                 \includegraphics[width=\textwidth]{figs/Service.pdf}
236                 \caption{Service with API and network protocol}
237                 \label{fig:service}
238         \end{subfigure}
239         ~~~~~~~~~~
240         \begin{subfigure}[b]{0.3\textwidth}
241                 \centering
242                 \includegraphics[width=\textwidth]{figs/System.pdf}
243                 \caption{Service interaction}
244                 \label{fig:interaction}
245         \end{subfigure}
246   \end{center}
247   \caption{GNUnet's layered system architecture}
248 \end{figure}
249
250 The main service implementation runs as a standalone process in the operating
251 system and the client code runs as part of the client program, so crashes of a
252 client do not affect the service process or other clients. The service and the
253 clients communicate via a message protocol to be defined and implemented by
254 the programmer.
255
256
257 \section{First Steps with GNUnet}
258
259 \subsection{Configure your peer}
260
261 First of all we need to configure your peer. Each peer is started with a configuration containing settings for GNUnet itself and its services. This configuration is based on the default configuration shipped with GNUnet and can be modified. The default configuration is located in the {\tt \$PREFIX/share/gnunet/config.d} directory. When starting a peer, you can specify a customized configuration using the the {\tt$-c$} command line switch when starting the ARM service and all other services. When using a modified configuration the default values are loaded and only values specified in the configuration file will replace the default values.
262
263 Since we want to start additional peers later, we need
264 some modifications from the default configuration. We need to create a separate service home and a file containing our modifications for this peer:
265 \begin{lstlisting}
266 $ mkdir ~/gnunet1/
267 $ touch peer1.conf
268 \end{lstlisting}
269
270 Now add the following lines to peer1.conf to use this directory. For
271 simplified usage we want to prevent the peer to connect to the GNUnet
272 network since this could lead to confusing output. This modifications
273 will replace the default settings:
274 \begin{lstlisting}
275 [PATHS]
276 GNUNET_HOME = ~/gnunet1/                # Use this directory to store GNUnet data
277 [hostlist]
278 SERVERS =                               # prevent bootstrapping
279 \end{lstlisting}
280
281
282 \subsection{Start a peer}
283 Each GNUnet instance (called peer) has an identity (\textit{peer ID}) based on a
284 cryptographic public private key pair. The peer ID is the printable hash of the
285 public key.
286
287 GNUnet services are controlled by a master service the so called \textit{Automatic Restart Manager} (ARM).
288 ARM starts, stops and even restarts services automatically or on demand when a client connects.
289 You interact with the ARM service using the \lstinline|gnunet-arm| tool.
290 GNUnet can then be started with \lstinline|gnunet-arm -s| and stopped with
291 \lstinline|gnunet-arm -e|.  An additional service not automatically started
292 can be started using \lstinline|gnunet-arm -i <service name>| and stopped
293 using \lstinline|gnunet-arm -k <servicename>|.
294
295 Once you have started your peer, you can use many other GNUnet commands
296 to interact with it.  For example, you can run:
297 \lstset{language=bash}
298 \begin{lstlisting}
299 $ gnunet-peerinfo -s
300 \end{lstlisting}
301 to obtain the public key of your peer.
302 You should see an output containing the peer ID similar to:
303 \lstset{language=bash}
304 \begin{lstlisting}
305 I am peer `0PA02UVRKQTS2C .. JL5Q78F6H0B1ACPV1CJI59MEQUMQCC5G'.
306 \end{lstlisting}
307
308
309 \subsection{Monitor a peer}
310
311 In this section, we will monitor the behaviour of our peer's DHT service with respect to a
312 specific key. First we will start GNUnet and then start the DHT service and use the DHT monitor tool
313 to monitor the PUT and GET commands we issue ussing the \lstinline|gnunet-dht-put| and
314 \lstinline|gnunet-dht-get| commands. Using the ``monitor'' line given below, you can observe the behavior of
315 your own peer's DHT with respect to the specified KEY:
316
317 \lstset{language=bash}
318 \begin{lstlisting}
319 $ gnunet-arm -c ~/peer1.conf -s                 # start gnunet with all default services
320 $ gnunet-arm -c ~/peer1.conf -i dht             # start DHT service
321 $ cd ~/gnunet/src/dht;
322 $ ./gnunet-dht-monitor -c ~/peer1.conf -k KEY
323 \end{lstlisting}
324 Now open a separate terminal and change again to the \lstinline|gnunet/src/dht| directory:
325 \begin{lstlisting}
326 $ cd ~/gnunet/src/dht
327 $ ./gnunet-dht-put -c ~/peer1.conf -k KEY -d VALUE              # put VALUE under KEY in the DHT
328 $ ./gnunet/src/dht/gnunet-dht-get -c ~/peer1.conf -k KEY        # get key KEY from the DHT
329 $ gnunet-statistics -c ~/peer1.conf             # print statistics about current GNUnet state
330 $ gnunet-statistics -c ~/peer1.conf -s dht      # print statistics about DHT service
331 \end{lstlisting}
332 % $
333
334
335 \subsection{Starting Two Peers by Hand}
336
337 \subsubsection{Setup a second peer}
338 We will now start a second peer on your machine.
339 For the second peer, you will need to manually create a modified
340 configuration file to avoid conflicts with ports and directories.
341 A peers configuration file is by default located in {\tt ~/.gnunet/gnunet.conf}.
342 This file is typically very short or even empty as only the differences to the
343 defaults need to be specified.  The defaults are located in
344 many files in the {\tt \$PREFIX/share/gnunet/config.d} directory.
345
346 To configure the second peer, use the files {\tt
347   \$PREFIX/share/gnunet/config.d} as a template for your main
348 configuration file:
349 %
350 \lstset{language=bash}
351 \begin{lstlisting}
352 $ cat $PREFIX/share/gnunet/config.d/*.conf > peer2.conf
353 \end{lstlisting}
354 Now you have to edit {\tt peer2.conf} and change:
355 \begin{itemize}
356   \itemsep0em
357   \item{\texttt{SERVICEHOME} under \texttt{PATHS}}
358   \item{Every (uncommented) value for ``\texttt{PORT}'' (add 10000) in any
359         section (the option may be commented out if \texttt{PORT} is
360         prefixed by "\#", in this case, UNIX domain sockets are used
361         and the PORT option does not need to be touched) }
362   \item{Every value for ``\texttt{UNIXPATH}'' in any section (e.g. by adding a "-p2" suffix)}
363 \end{itemize}
364 to a fresh, unique value.  Make sure that the \texttt{PORT} numbers stay
365 below 65536.  From now on, whenever you interact with the second
366 peer, you need to specify {\tt -c peer2.conf} as an additional
367 command line argument.
368
369 Now, generate the 2nd peer's private key:
370
371 \lstset{language=bash}
372 \begin{lstlisting}
373 $ gnunet-peerinfo -s -c peer2.conf
374 \end{lstlisting}
375 % $
376
377 This may take a while, generate entropy using your keyboard or mouse
378 as needed.  Also, make sure the output is different from the {\tt
379   gnunet-peerinfo} output for the first peer (otherwise you made an
380 error in the configuration).
381
382 \subsubsection{Start the second peer and connect the peers}
383
384 Then, you can start a second peer using:
385 \lstset{language=bash}
386 \begin{lstlisting}
387 $ gnunet-arm -c peer2.conf -s
388 $ gnunet-arm -c peer2.conf -i dht
389 $ ~/gnunet/src/dht/gnunet-dht-put -c peer2.conf -k KEY -d VALUE
390 $ ~/gnunet/src/dht/gnunet-dht-get -c peer2.conf -k KEY
391 \end{lstlisting}
392 If you want the two peers to connect, you have multiple options:
393 \begin{itemize}
394 \itemsep0em
395   \item UDP neighbour discovery (automatic)
396   \item Setup a bootstrap server
397   \item Connect manually
398 \end{itemize}
399 To setup peer 1 as bootstrapping server change the configuration of
400 the first one to be a hostlist server by adding the following lines to
401 \texttt{peer1.conf} to enable bootstrapping server:
402  \begin{lstlisting}
403 [hostlist]
404 OPTIONS = -p
405 \end{lstlisting}
406
407 Then change {\tt peer2.conf} and replace the ``\texttt{SERVERS}'' line in the ``\texttt{[hostlist]}'' section with
408 ``\texttt{http://localhost:8080/}''.  Restart both peers using:
409 \begin{lstlisting}
410 $ gnunet-arm -c peer1.conf -e           # stop first peer
411 $ gnunet-arm -c peer1.conf -s           # start first peer
412 $ gnunet-arm -c peer2.conf -s           # start second peer
413 \end{lstlisting}
414
415 Note that if you start your peers without changing these settings, they
416 will use the ``global'' hostlist servers of the GNUnet P2P network and
417 likely connect to those peers.  At that point, debugging might become
418 tricky as you're going to be connected to many more peers and would
419 likely observe traffic and behaviors that are not explicitly controlled
420 by you.
421
422 \subsubsection{How to connect manually}
423
424 If you want to use the \texttt{peerinfo} tool to connect your peers, you should:
425 \begin{itemize}
426 \itemsep0em
427  \item{Set {\tt FORCESTART = NO} in section {\tt hostlist} (to not connect to the global GNUnet)}
428  \item{Start both peers running {\tt gnunet-arm -c peer1.conf -s} and {\tt gnunet-arm -c peer2.conf -s}}
429  \item{Get \texttt{HELLO} message of the first peer running {\tt gnunet-peerinfo -c peer1.conf -g}}
430  \item{Give the output to the second peer by running {\tt gnunet-peerinfo -c peer2.conf -p '<output>'}}
431 \end{itemize}
432
433 Check that they are connected using {\tt gnunet-core -c peer1.conf}, which should give you the other peer's
434 peer identity:
435 \begin{lstlisting}
436 $ gnunet-core -c peer1.conf
437 Peer `9TVUCS8P5A7ILLBGO6 [...shortened...] 1KNBJ4NGCHP3JPVULDG'
438 \end{lstlisting}
439
440 \subsection{Starting Peers Using the Testbed Service}
441
442 GNUnet's testbed service is used for testing scenarios where a number of peers
443 are to be started.  The testbed can manage peers on a single host or on multiple
444 hosts in a distributed fashion.  On a single affordable computer, it should be
445 possible to run around tens of peers without drastically increasing the load on the
446 system.
447
448 The testbed service can be access through its API
449 \texttt{include/gnunet\_testbed\_service.h}.  The API provides many routines for
450 managing a group of peers.  It also provides a helper function
451 \texttt{GNUNET\_TESTBED\_test\_run()} to quickly setup a minimalistic testing
452 environment on a single host.
453
454 This function takes a configuration file which will be used as a template
455 configuration for the peers.  The testbed takes care of modifying relevant
456 options in the peers' configuration such as SERVICEHOME, PORT, UNIXPATH to
457 unique values so that peers run without running into conflicts.  It also checks
458 and assigns the ports in configurations only if they are free.
459
460 Additionally, the testbed service also reads its options from the same
461 configuration file.  Various available options and details about them can be
462 found in the testbed default configuration file \texttt{src/testbed/testbed.conf}.
463
464 With the testbed API, a sample test case can be structured as follows:
465 % <lynX> Is there a way to pick a more readable font for this include?
466 \lstinputlisting[language=C]{testbed_test.c}
467 The source code for the above listing can be found at
468 \url{https://gnunet.org/git/gnunet.git/tree/doc/testbed_test.c}
469 or in the {\tt doc/} folder of your repository check-out.
470 After installing GNUnet, the above source code can be compiled as:
471 \lstset{language=bash}
472 \begin{lstlisting}
473 $ export CPPFLAGS="-I/path/to/gnunet/headers"
474 $ export LDFLAGS="-L/path/to/gnunet/libraries"
475 $ gcc $CPPFLAGS $LDFLAGS -o testbed-test testbed_test.c  -lgnunettestbed -lgnunetdht -lgnunetutil
476 \end{lstlisting}
477 The \texttt{CPPFLAGS} and \texttt{LDFLAGS} are necessary if GNUnet is installed
478 into a different directory other than \texttt{/usr/local}.
479
480 All of testbed API's peer management functions treat management actions as
481 operations and return operation handles.  It is expected that the operations
482 begin immediately, but they may get delayed (to balance out load on the system).
483 The program using the API then has to take care of marking the operation as
484 ``done'' so that its associated resources can be freed immediately and other
485 waiting operations can be executed.  Operations will be canceled if they are
486 marked as ``done'' before their completion.
487
488 An operation is treated as completed when it succeeds or fails.  Completion of
489 an operation is either conveyed as events through \textit{controller event
490   callback} or through respective operation completion callbacks.  In functions
491 which support completion notification through both controller event callback and
492 operation completion callback, first the controller event callback will be
493 called.  If the operation is not marked as done in that callback or if the
494 callback is given as NULL when creating the operation, the operation completion
495 callback will be called.  The API documentation shows which event are to be
496 expected in the controller event notifications.  It also documents any
497 exceptional behaviour.
498
499 Once the peers are started, test cases often need to connect some of the peers'
500 services.  Normally, opening a connect to a peer's service requires the peer's
501 configuration.  While using testbed, the testbed automatically generates
502 per-peer configuration.  Accessing those configurations directly through file
503 system is discouraged as their locations are dynamically created and will be
504 different among various runs of testbed.  To make access to these configurations
505 easy, testbed API provides the function
506 \texttt{GNUNET\_TESTBED\_service\_connect()}.  This function fetches the
507 configuration of a given peer and calls the \textit{Connect Adapter}.
508 In the example code, it is the \texttt{dht\_ca}.  A connect adapter is expected
509 to open the connection to the needed service by using the provided configuration
510 and return the created service connection handle.  Successful connection to the
511 needed service is signaled through \texttt{service\_connect\_comp\_cb}.
512
513 A dual to connect adapter is the \textit{Disconnect Adapter}.  This callback is
514 called after the connect adapter has been called when the operation from
515 \texttt{GNUNET\_TESTBED\_service\_connect()} is marked as ``done''.  It has to
516 disconnect from the service with the provided service handle (\texttt{op\_result}).
517
518 \exercise{Find out how many peers you can run on your system.}
519
520 \exercise{Find out how to create a 2D torus topology by changing the
521   options in the configuration file.\footnote{See \url{https://gnunet.org/content/supported-topologies}}
522   Then use the DHT API to store and retrieve values in the
523   network.}
524
525
526 \section{Developing Applications}
527
528 \subsection{gnunet-ext}
529 To develop a new peer-to-peer application or to extend GNUnet we provide
530 a template build system for writing GNUnet extensions in C. It can be
531 obtained as follows:
532
533 \lstset{language=bash}
534 \begin{lstlisting}
535 $ git clone https://gnunet.org/git/gnunet-ext
536 $ cd gnunet-ext/
537 $ ./bootstrap
538 $ ./configure --prefix=$PREFIX --with-gnunet=$PREFIX
539 $ make
540 $ make install
541 $ make check
542 \end{lstlisting}
543 % $
544
545 The GNUnet ext template includes examples and a working buildsystem for a new GNUnet service.
546 A common GNUnet service consists of the following parts which will be discussed in detail in the
547 remainder of this document. The functionality of a GNUnet service is implemented in:
548
549 \begin{itemize}
550 \itemsep0em
551   \item the GNUnet service (\lstinline|gnunet-ext/src/ext/gnunet-service-ext.c|)
552   \item the client API (\lstinline|gnunet-ext/src/ext/ext_api.c|)
553   \item the client application using the service API (\lstinline|gnunet-ext/src/ext/gnunet-ext.c|)
554
555
556 \end{itemize}
557
558 The interfaces for these entities are defined in:
559 \begin{itemize}
560 \itemsep0em
561   \item client API interface (\lstinline|gnunet-ext/src/ext/ext.h|)
562   \item the service interface (\lstinline|gnunet-ext/src/include/gnunet_service_SERVICE.h|)
563   \item the P2P protocol (\lstinline|gnunet-ext/src/include/gnunet_protocols_ext.h|)
564 \end{itemize}
565
566
567 In addition the \texttt{ext} systems provides:
568 \begin{itemize}
569 \itemsep0em
570   \item a test testing the API (\lstinline|gnunet-ext/src/ext/test_ext_api.c|)
571   \item a configuration template for the service (\lstinline|gnunet-ext/src/ext/ext.conf.in|)
572 \end{itemize}
573
574
575 \subsection{Adapting the Template}
576
577 The first step for writing any extension with a new service is to
578 ensure that the {\tt ext.conf.in} file contains entries for the
579 \texttt{UNIXPATH}, \texttt{PORT} and \texttt{BINARY} for the service in a section named after
580 the service.
581
582 If you want to adapt the template rename the {\tt ext.conf.in} to match your
583 services name, you have to modify the \texttt{AC\_OUTPUT} section in {\tt configure.ac}
584 in the \texttt{gnunet-ext} root.
585
586 \section{Writing a Client Application}
587
588 When writing any client application (for example, a command-line
589 tool), the basic structure is to start with the {\tt
590   GNUNET\_PROGRAM\_run} function.  This function will parse
591 command-line options, setup the scheduler and then invoke the {\tt
592   run} function (with the remaining non-option arguments) and a handle
593 to the parsed configuration (and the configuration file name that was
594 used, which is typically not needed):
595
596 \lstset{language=c}
597 \begin{lstlisting}
598 #include <gnunet/platform.h>
599 #include <gnunet/gnunet_util_lib.h>
600
601 static int ret;
602
603 static void
604 run (void *cls,
605      char *const *args,
606      const char *cfgfile,
607      const struct GNUNET_CONFIGURATION_Handle *cfg)
608 {
609   /* main code here */
610   ret = 0;
611 }
612
613 int
614 main (int argc, char *const *argv)
615 {
616   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
617     GNUNET_GETOPT_OPTION_END
618   };
619   return (GNUNET_OK ==
620           GNUNET_PROGRAM_run (argc,
621                               argv,
622                               "binary-name",
623                               gettext_noop ("binary description text"),
624                               options, &run, NULL)) ? ret : 1;
625 }
626 \end{lstlisting}
627
628 \subsection{Handling command-line options}
629
630 Options can then be added easily by adding global variables and
631 expanding the {\tt options} array.  For example, the following would
632 add a string-option and a binary flag (defaulting to {\tt NULL} and
633 {\tt GNUNET\_NO} respectively):
634
635 \begin{lstlisting}
636 static char *string_option;
637 static int a_flag;
638
639 // ...
640   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
641   {'s', "name", "SOMESTRING",
642      gettext_noop ("text describing the string_option NAME"), 1,
643      &GNUNET_GETOPT_set_string, &string_option},
644     {'f', "flag", NULL,
645      gettext_noop ("text describing the flag option"), 0,
646      &GNUNET_GETOPT_set_one, &a_flag},
647     GNUNET_GETOPT_OPTION_END
648   };
649   string_option = NULL;
650   a_flag = GNUNET_SYSERR;
651 // ...
652 \end{lstlisting}
653
654 Issues such as displaying some helpful text describing options using
655 the {\tt --help} argument and error handling are taken care of when
656 using this approach.  Other {\tt GNUNET\_GETOPT\_}-functions can be used
657 to obtain integer value options, increment counters, etc.  You can
658 even write custom option parsers for special circumstances not covered
659 by the available handlers. To check if an argument was specified by the
660 user you initialize the variable with a specific value (e.g. NULL for
661 a string and GNUNET\_SYSERR for a integer) and check after parsing
662 happened if the values were modified.
663
664 Inside the {\tt run} method, the program would perform the
665 application-specific logic, which typically involves initializing and
666 using some client library to interact with the service.  The client
667 library is supposed to implement the IPC whereas the service provides
668 more persistent P2P functions.
669
670 \exercise{Add a few command-line options and print them inside
671 of {\tt run}.  What happens if the user gives invalid arguments?}
672
673 \subsection{Writing a Client Library}
674
675 The first and most important step in writing a client library is to
676 decide on an API for the library.  Typical API calls include
677 connecting to the service, performing application-specific requests
678 and cleaning up.  Many examples for such service APIs can be found
679 in the {\tt gnunet/src/include/gnunet\_*\_service.h} files.
680
681 Then, a client-service protocol needs to be designed.  This typically
682 involves defining various message formats in a header that will be
683 included by both the service and the client library (but is otherwise
684 not shared and hence located within the service's directory and not
685 installed by {\tt make install}).  Each message must start with a {\tt
686   struct GNUNET\_MessageHeader} and must be shorter than 64k.  By
687 convention, all fields in IPC (and P2P) messages must be in big-endian
688 format (and thus should be read using {\tt ntohl} and similar
689 functions and written using {\tt htonl} and similar functions).
690 Unique message types must be defined for each message struct in the
691 {\tt gnunet\_protocols.h} header (or an extension-specific include
692 file).
693
694 \subsubsection{Connecting to the Service}
695
696 Before a client library can implement the application-specific protocol
697 with the service, a connection must be created:
698
699 \lstset{language=c}
700 \begin{lstlisting}
701   struct GNUNET_MQ_MessageHandlers handlers[] = {
702     // ...
703     GNUNET_MQ_handler_end ()
704   };
705   struct GNUNET_MQ_Handle *mq;
706
707   mq = GNUNET_CLIENT_connect (cfg, "service-name", handlers, &error_cb, NULL);
708 \end{lstlisting}
709
710 As a result a {\tt GNUNET\_MQ\_Handle} is returned
711 which can to used henceforth to transmit messages to
712 the service.
713 The complete MQ API can be found in {\tt gnunet\_mq\_lib.h}.
714 The {\tt hanlders} array in the example above is incomplete.
715 Here is where you will define which messages you expect to
716 receive from the service, and which functions handle them.
717 The {\tt error\_cb} is a function that is to be called whenever
718 there are errors communicating with the service.
719
720 \subsubsection{Sending messages}
721
722 In GNUnet, messages are always sent beginning with a {\tt struct GNUNET\_MessageHeader}
723 in big endian format. This header defines the size and the type of the
724 message, the payload follows after this header.
725
726 \lstset{language=c}
727 \begin{lstlisting}
728 struct GNUNET_MessageHeader
729 {
730
731   /**
732    * The length of the struct (in bytes, including the length field itself),
733    * in big-endian format.
734    */
735   uint16_t size GNUNET_PACKED;
736
737   /**
738    * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
739    */
740   uint16_t type GNUNET_PACKED;
741
742 };
743 \end{lstlisting}
744
745 Existing message types are defined in {\tt gnunet\_protocols.h}\\
746 A common way to create a message is with an envelope:
747
748 \lstset{language=c}
749 \begin{lstlisting}
750 struct GNUNET_MQ_Envelope *env;
751 struct GNUNET_MessageHeader *msg;
752
753 env = GNUNET_MQ_msg_extra (msg, payload_size, GNUNET_MY_MESSAGE_TYPE);
754 memcpy (&msg[1], &payload, payload_size);
755 /* Send message via message queue 'mq': */
756 GNUNET_mq_send (mq, env);
757 \end{lstlisting}
758
759 \exercise{Define a message struct that includes a 32-bit
760 unsigned integer in addition to the standard GNUnet MessageHeader.
761 Add a C struct and define a fresh protocol number for your message.
762 (Protocol numbers in gnunet-ext are defined in \lstinline|gnunet-ext/src/include/gnunet_protocols_ext.h|)}
763
764 \exercise{Find out how you can determine the number of messages in a message queue.}
765
766 \exercise{Find out how you can determine when a message you have queued was actually transmitted.}
767
768 \exercise{Define a helper function to transmit a 32-bit
769 unsigned integer (as payload) to a service using some given client
770 handle.}
771
772
773 \subsubsection{Receiving Replies from the Service}
774
775 Clients can receive messages from the service using the handlers
776 specified in the {\tt handlers} array we specified when connecting
777 to the service.  Entries in the the array are usually created using
778 one of two macros, depending on whether the message is fixed size
779 or variable size.  Variable size messages are managed using two
780 callbacks, one to check that the message is well-formed, the other
781 to actually process the message.  Fixed size messages are fully
782 checked by the MQ-logic, and thus only need to provide the handler
783 to process the message.  Note that the prefixes {\tt check\_}
784 and {\tt handle\_} are mandatory.
785
786 \lstset{language=c}
787 \begin{lstlisting}
788 /**
789  * Function called with MyMessage messages from service.
790  *
791  * @param cls closure
792  * @param msg message received
793  */
794 static void
795 handle_fix (void *cls, const struct MyMessage *msg)
796 {
797   // process 'msg'
798 }
799
800 /**
801  * Function called with MyVarMessage messages from service.
802  *
803  * @param cls closure
804  * @param msg message received
805  * @return #GNUNET_OK if @a msg is well-formed
806  */
807 static int
808 check_var (void *cls, const struct MyVarMessage *msg)
809 {
810   // check 'msg' is well-formed
811   return GNUNET_OK; /* suppose yes */
812 }
813
814 /**
815  * Function called with MyMessage messages from service.
816  *
817  * @param cls closure
818  * @param msg message received
819  */
820 static void
821 handle_var (void *cls, const struct MyVarMessage *msg)
822 {
823   // process 'msg'
824 }
825
826 struct GNUNET_MQ_MessageHandler handlers[] = {
827   GNUNET_MQ_hd_fixed_size (fix,
828                           GNUNET_MESSAGE_TYPE_MY_FIX,
829                           struct MyMessage,
830                           NULL),
831   GNUNET_MQ_hd_fixed_size (var,
832                           GNUNET_MESSAGE_TYPE_MY_VAR,
833                           struct MyVarMessage,
834                           NULL),
835
836   GNUNET_MQ_handler_end ()
837 };
838 \end{lstlisting}
839
840 \exercise{Expand your helper function to receive a response message
841   (for example, containing just the {\tt struct GNUnet MessageHeader}
842   without any payload).  Upon receiving the service's response, you
843   should call a callback provided to your helper function's API.}
844
845 \exercise{Figure out where you can pass values to the closures ({\tt cls}).}
846
847
848 \subsection{Writing a user interface}
849
850 Given a client library, all it takes to access a service now is to
851 combine calls to the client library with parsing command-line
852 options.
853
854 \exercise{Call your client API from your {\tt run()} method in your
855   client application to send a request to the service.  For example,
856   send a 32-bit integer value based on a number given at the
857   command-line to the service.}
858
859
860
861 \section{Writing a Service}
862
863 Before you can test the client you've written so far, you'll need to also
864 implement the corresponding service.
865
866
867 \subsection{Code Placement}
868
869 New services are placed in their own subdirectory under {\tt gnunet/src}.
870 This subdirectory should contain the API implementation file {\tt SERVICE\_api.c},
871 the description of the client-service protocol {\tt SERVICE.h} and P2P protocol
872 {\tt SERVICE\_protocol.h}, the implementation of the service itself
873 {\tt gnunet-service-SERVICE.h} and several files for tests, including test code
874 and configuration files.
875
876 \subsection{Starting a Service}
877
878 The key API definition for creating a service is the {\tt GNUNET\_SERVICE\_MAIN} macro:
879 \lstset{language=C}
880 \begin{lstlisting}
881 GNUNET_SERVICE_MAIN
882 ("service-name",
883  GNUNET_SERVICE_OPTION_NONE,
884  &run,
885  &client_connect_cb,
886  &client_disconnect_cb,
887  NULL,
888  GNUNET_MQ_hd_fixed_size (...),
889  GNUNET_MQ_hd_var_size (...),
890  GNUNET_MQ_handler_end ());
891 \end{lstlisting}
892
893 In addition to the service name and flags, the macro takes three
894 functions, typically called {\tt run}, {\tt client\_connect\_cb} and
895 {\tt client\_disconnect\_cb} as well as an array of message handlers
896 that will be called for incoming messages from clients.
897
898 A minimal version of the three central service funtions would look
899 like this:
900
901 \lstset{language=c}
902 \begin{lstlisting}
903 /**
904  * Launch service.
905  *
906  * @param cls closure
907  * @param c configuration to use
908  * @param service the initialized service
909  */
910 static void
911 run (void *cls,
912      const struct GNUNET_CONFIGURATION_Handle *c,
913      struct GNUNET_SERVICE_Handle *service)
914 {
915 }
916
917 /**
918  * Callback called when a client connects to the service.
919  *
920  * @param cls closure for the service
921  * @param c the new client that connected to the service
922  * @param mq the message queue used to send messages to the client
923  * @return @a c
924  */
925 static void *
926 client_connect_cb (void *cls,
927                    struct GNUNET_SERVICE_Client *c,
928                    struct GNUNET_MQ_Handle *mq)
929 {
930   return c;
931 }
932
933 /**
934  * Callback called when a client disconnected from the service
935  *
936  * @param cls closure for the service
937  * @param c the client that disconnected
938  * @param internal_cls should be equal to @a c
939  */
940 static void
941 client_disconnect_cb (void *cls,
942                       struct GNUNET_SERVICE_Client *c,
943                       void *internal_cls)
944 {
945   GNUNET_assert (c == internal_cls);
946 }
947 \end{lstlisting}
948
949 \exercise{Write a stub service that processes no messages at all
950   in your code.  Create a default configuration for it, integrate it
951   with the build system and start the service from {\tt
952   gnunet-service-arm} using {\tt gnunet-arm -i NAME}.}
953
954 \exercise{Figure out how to set the closure ({\tt cls}) for handlers
955   of a service.}
956
957 \exercise{Figure out how to send messages from the service back to the
958   client.}
959
960 Each handler function in the service {\bf must} eventually (possibly in some
961 asynchronous continuation) call {\tt GNUNET\_SERVICE\_client\_continue()}.
962 Only after this call additional messages from the same client may
963 be processed. This way, the service can throttle processing messages
964 from the same client.
965
966 \exercise{Change the service to ``handle'' the message from your
967   client (for now, by printing a message).  What happens if you
968   forget to call {\tt GNUNET\_SERVICE\_client\_continue()}?}
969
970
971 \section{Interacting directly with other Peers using the CORE Service}
972
973 FIXME: This section still needs to be updated to the lastest API!
974
975 One of the most important services in GNUnet is the \texttt{CORE} service
976 managing connections between peers and handling encryption between peers.
977
978 One of the first things any service that extends the P2P protocol typically does
979 is connect to the \texttt{CORE} service using:
980
981 \lstset{language=C}
982 \begin{lstlisting}
983 #include <gnunet/gnunet_core_service.h>
984
985 struct GNUNET_CORE_Handle *
986 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
987                      void *cls,
988                      GNUNET_CORE_StartupCallback init,
989                      GNUNET_CORE_ConnectEventHandler connects,
990                      GNUNET_CORE_DisconnectEventHandler disconnects,
991                      GNUNET_CORE_MessageCallback inbound_notify,
992                      int inbound_hdr_only,
993                      GNUNET_CORE_MessageCallback outbound_notify,
994                      int outbound_hdr_only,
995                      const struct GNUNET_CORE_MessageHandler *handlers);
996 \end{lstlisting}
997
998 \subsection{New P2P connections}
999
1000 Before any traffic with a different peer can be exchanged, the peer must be
1001 known to the service. This is notified by the \texttt{CORE} {\tt connects} callback,
1002 which communicates the identity of the new peer to the service:
1003
1004 \lstset{language=C}
1005 \begin{lstlisting}
1006 void
1007 connects (void *cls,
1008           const struct GNUNET_PeerIdentity * peer)
1009 {
1010     /* Save identity for later use */
1011     /* Optional: start sending messages to peer */
1012 }
1013 \end{lstlisting}
1014
1015 \exercise{Create a service that connects to the \texttt{CORE}.  Then
1016 start (and connect) two peers and print a message once your connect
1017 callback is invoked.}
1018
1019 \subsection{Receiving P2P Messages}
1020
1021 To receive messages from \texttt{CORE}, services register a set of handlers
1022 (parameter {\tt *handlers} in the \lstinline|GNUNET_CORE_connect| call that are called by \texttt{CORE}
1023 when a suitable message arrives.
1024
1025 \lstset{language=c}
1026 \begin{lstlisting}
1027 static int
1028 callback_function_for_type_one(void *cls,
1029                                const struct GNUNET_PeerIdentity *peer,
1030                                const struct GNUNET_MessageHeader *message)
1031 {
1032     /* Do stuff */
1033     return GNUNET_OK; /* or GNUNET_SYSERR to close the connection */
1034 }
1035
1036 /**
1037  * Functions to handle messages from core
1038  */
1039 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1040   {&callback_function_for_type_one, GNUNET_MESSAGE_TYPE_MYSERVICE_TYPE_ONE, 0},
1041   /* more handlers*/
1042   {NULL, 0, 0}
1043 };
1044 \end{lstlisting}
1045
1046 \exercise{Start one peer with a new service that has a message
1047 handler and start a second peer that only has your ``old'' service
1048 without message handlers.  Which ``connect'' handlers are invoked when
1049 the two peers are connected?  Why?}
1050
1051
1052 \subsection{Sending P2P Messages}
1053
1054 In response to events (connect, disconnect, inbound messages,
1055 timing, etc.) services can then use this API to transmit messages:
1056
1057 \lstset{language=C}
1058 \begin{lstlisting}
1059 typedef size_t
1060 (*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
1061                                           size_t size,
1062                                           void *buf)
1063 {
1064     /* Fill "*buf" with up to "size" bytes, must start with GNUNET_MessageHeader */
1065     return n; /* Total size of the message put in "*buf" */
1066 }
1067
1068 struct GNUNET_CORE_TransmitHandle *
1069 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1070                                    int cork, uint32_t priority,
1071                                    struct GNUNET_TIME_Relative maxdelay,
1072                                    const struct GNUNET_PeerIdentity *target,
1073                                    size_t notify_size,
1074                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
1075                                    void *notify_cls);
1076 \end{lstlisting}
1077
1078 \exercise{Write a service that upon connect sends messages as
1079 fast as possible to the other peer (the other peer should run a
1080 service that ``processes'' those messages).  How fast is the
1081 transmission?  Count using the STATISTICS service on both ends.  Are
1082 messages lost? How can you transmit messages faster?  What happens if
1083 you stop the peer that is receiving your messages?}
1084
1085
1086 \subsection{End of P2P connections}
1087
1088 If a message handler returns {\tt GNUNET\_SYSERR}, the remote peer shuts down or
1089 there is an unrecoverable network disconnection, CORE notifies the service that
1090 the peer disconnected. After this notification no more messages will be received
1091 from the peer and the service is no longer allowed to send messages to the peer.
1092 The disconnect callback looks like the following:
1093
1094 \lstset{language=C}
1095 \begin{lstlisting}
1096 void
1097 disconnects (void *cls,
1098              const struct GNUNET_PeerIdentity * peer)
1099 {
1100     /* Remove peer's identity from known peers */
1101     /* Make sure no messages are sent to peer from now on */
1102 }
1103 \end{lstlisting}
1104
1105 \exercise{Fix your service to handle peer disconnects.}
1106
1107 \section{Storing peer-specific data using the PEERSTORE service}
1108
1109 GNUnet's PEERSTORE service offers a persistorage for arbitrary peer-specific data.
1110 Other GNUnet services can use the PEERSTORE to store, retrieve and monitor data records.
1111 Each data record stored with PEERSTORE contains the following fields:
1112
1113 \begin{itemize}
1114 \itemsep0em
1115   \item subsystem: Name of the subsystem responsible for the record.
1116   \item peerid: Identity of the peer this record is related to.
1117   \item key: a key string identifying the record.
1118   \item value: binary record value.
1119   \item expiry: record expiry date.
1120 \end{itemize}
1121
1122 The first step is to start a connection to the PEERSTORE service:
1123 \begin{lstlisting}
1124 #include "gnunet_peerstore_service.h"
1125
1126 peerstore_handle = GNUNET_PEERSTORE_connect (cfg);
1127 \end{lstlisting}
1128 The service handle \lstinline|peerstore_handle| will be needed for all subsequent
1129 PEERSTORE operations.
1130
1131 \subsection{Storing records}
1132
1133 To store a new record, use the following function:
1134 \begin{lstlisting}
1135 struct GNUNET_PEERSTORE_StoreContext *
1136 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
1137                         const char *sub_system,
1138                         const struct GNUNET_PeerIdentity *peer,
1139                         const char *key,
1140                         const void *value,
1141                         size_t size,
1142                         struct GNUNET_TIME_Absolute expiry,
1143                         enum GNUNET_PEERSTORE_StoreOption options,
1144                         GNUNET_PEERSTORE_Continuation cont,
1145                         void *cont_cls);
1146 \end{lstlisting}
1147
1148 The \lstinline|options| parameter can either be \lstinline|GNUNET_PEERSTORE_STOREOPTION_MULTIPLE|
1149 which means that multiple values can be stored under the same key combination (subsystem, peerid, key),
1150 or \lstinline|GNUNET_PEERSTORE_STOREOPTION_REPLACE| which means that PEERSTORE will replace any
1151 existing values under the given key combination (subsystem, peerid, key) with the new given value.
1152
1153 The continuation function \lstinline|cont| will be called after the store request is successfully
1154 sent to the PEERSTORE service. This does not guarantee that the record is successfully stored, only
1155 that it was received by the service.
1156
1157 The \lstinline|GNUNET_PEERSTORE_store| function returns a handle to the store operation. This handle
1158 can be used to cancel the store operation only before the continuation function is called:
1159 \begin{lstlisting}
1160 void
1161 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc);
1162 \end{lstlisting}
1163
1164 \subsection{Retrieving records}
1165
1166 To retrieve stored records, use the following function:
1167 \begin{lstlisting}
1168 struct GNUNET_PEERSTORE_IterateContext *
1169 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
1170                           const char *sub_system,
1171                           const struct GNUNET_PeerIdentity *peer,
1172                           const char *key,
1173                           struct GNUNET_TIME_Relative timeout,
1174                           GNUNET_PEERSTORE_Processor callback,
1175                           void *callback_cls);
1176 \end{lstlisting}
1177 The values of \lstinline|peer| and \lstinline|key| can be \lstinline|NULL|. This allows the
1178 iteration over values stored under any of the following key combinations:
1179 \begin{itemize}
1180 \itemsep0em
1181   \item (subsystem)
1182   \item (subsystem, peerid)
1183   \item (subsystem, key)
1184   \item (subsystem, peerid, key)
1185 \end{itemize}
1186
1187 The \lstinline|callback| function will be called once with each retrieved record and once
1188 more with a \lstinline|NULL| record to signal the end of results.
1189
1190 The \lstinline|GNUNET_PEERSTORE_iterate| function returns a handle to the iterate operation. This
1191 handle can be used to cancel the iterate operation only before the callback function is called with
1192 a \lstinline|NULL| record.
1193
1194 \subsection{Monitoring records}
1195
1196 PEERSTORE offers the functionality of monitoring for new records stored under a specific key
1197 combination (subsystem, peerid, key). To start the monitoring, use the following function:
1198 \begin{lstlisting}
1199 struct GNUNET_PEERSTORE_WatchContext *
1200 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
1201                         const char *sub_system,
1202                         const struct GNUNET_PeerIdentity *peer,
1203                         const char *key,
1204                         GNUNET_PEERSTORE_Processor callback,
1205                         void *callback_cls);
1206 \end{lstlisting}
1207
1208 Whenever a new record is stored under the given key combination, the \lstinline|callback| function
1209 will be called with this new record. This will continue until the connection to the PEERSTORE service
1210 is broken or the watch operation is canceled:
1211 \begin{lstlisting}
1212 void
1213 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc);
1214 \end{lstlisting}
1215
1216 \subsection{Disconnecting from PEERSTORE}
1217
1218 When the connection to the PEERSTORE service is no longer needed, disconnect using the following
1219 function:
1220 \begin{lstlisting}
1221 void
1222 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first);
1223 \end{lstlisting}
1224
1225 If the \lstinline|sync_first| flag is set to \lstinline|GNUNET_YES|, the API will delay the
1226 disconnection until all store requests are received by the PEERSTORE service. Otherwise,
1227 it will disconnect immediately.
1228
1229
1230 \section{Using the DHT}
1231
1232 The DHT allows to store data so other peers in the P2P network can
1233 access it and retrieve data stored by any peers in the network.
1234 This section will explain how to use the DHT. Of course, the first
1235 thing to do is to connect to the DHT service:
1236 \lstset{language=C}
1237 \begin{lstlisting}
1238 dht_handle = GNUNET_DHT_connect (cfg, parallel_requests);
1239 \end{lstlisting}
1240 The second parameter indicates how many requests in parallel to expect.
1241 It is not a hard limit, but a good approximation will make the DHT more
1242 efficient.
1243
1244 \subsection{Storing data in the DHT}
1245 Since the DHT is a dynamic environment (peers join and leave frequently)
1246 the data that we put in the DHT does not stay there indefinitely. It is
1247 important to ``refresh'' the data periodically by simply storing it again,
1248 in order to make sure other peers can access it.
1249
1250 The put API call offers a callback to signal that the PUT request has been
1251 sent. This does not guarantee that the data is accessible to others peers,
1252 or even that is has been stored, only that the service has requested to
1253 a neighboring peer the retransmission of the PUT request towards its final
1254 destination. Currently there is no feedback about whether or not the data
1255 has been sucessfully stored or where it has been stored. In order to improve
1256 the availablilty of the data and to compensate for possible errors, peers leaving
1257 and other unfavorable events, just make several PUT requests!
1258
1259 \lstset{language=C}
1260 \begin{lstlisting}
1261 void
1262 message_sent_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1263 {
1264     /* Request has left local node */
1265 }
1266
1267 struct GNUNET_DHT_PutHandle *
1268 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
1269                 const struct GNUNET_HashCode *key,
1270                 uint32_t desired_replication_level,
1271                 enum GNUNET_DHT_RouteOption options, /* Route options, see next call */
1272                 enum GNUNET_BLOCK_Type type, size_t size, const void *data,
1273                 struct GNUNET_TIME_Absolute exp, /* When does the data expire? */
1274                 struct GNUNET_TIME_Relative timeout,  /* How long to try to send the request */
1275                 GNUNET_DHT_PutContinuation cont,
1276                 void *cont_cls)
1277 \end{lstlisting}
1278
1279 \exercise{Store a value in the DHT periodically to make sure it is available
1280 over time. You might consider using the function GNUNET\_SCHEDULER\_add\_delayed and
1281 call GNUNET\_DHT\_put from inside a helper function.}
1282
1283
1284 \subsection{Obtaining data from the DHT}
1285 As we saw in the previous example, the DHT works in an asynchronous mode.
1286 Each request to the DHT is executed ``in the background'' and the API
1287 calls return immediately. In order to receive results from the DHT, the
1288 API provides a callback. Once started, the request runs in the service,
1289 the service will try to get as many results as possible (filtering out
1290 duplicates) until the timeout expires or we explicitly stop the request.
1291 It is possible to give a ``forever'' timeout with
1292 {\tt GNUNET\_TIME\_UNIT\_FOREVER\_REL}.
1293
1294 If we give a route option {\tt GNUNET\_DHT\_RO\_RECORD\_ROUTE} the callback
1295 will get a list of all the peers the data has travelled, both on the PUT
1296 path and on the GET path.
1297 \lstset{language=C}
1298 \begin{lstlisting}
1299 static void
1300 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute expiration,
1301                      const struct GNUNET_HashCode *key,
1302                      const struct GNUNET_PeerIdentity *get_path,
1303                      unsigned int get_path_length,
1304                      const struct GNUNET_PeerIdentity *put_path,
1305                      unsigned int put_path_length,
1306                      enum GNUNET_BLOCK_Type type, size_t size, const void *data)
1307 {
1308     /* Do stuff with the data and/or route */
1309     /* Optionally: */
1310     GNUNET_DHT_get_stop (get_handle);
1311 }
1312
1313 get_handle =
1314       GNUNET_DHT_get_start (dht_handle,
1315                             block_type,
1316                             &key,
1317                             replication,
1318                             GNUNET_DHT_RO_NONE, /* Route options */
1319                             NULL, /* xquery: not used here */
1320                             0, /* xquery size */
1321                             &get_result_iterator,
1322                             cls)
1323 \end{lstlisting}
1324
1325 \exercise{Store a value in the DHT and after a while retrieve it. Show the IDs of all
1326 the peers the requests have gone through. In order to convert a peer ID to a string, use
1327 the function GNUNET\_i2s. Pay attention to the route option parameters in both calls!}
1328
1329 \subsection{Implementing a block plugin}
1330
1331 In order to store data in the DHT, it is necessary to provide a block
1332 plugin.  The DHT uses the block plugin to ensure that only well-formed
1333 requests and replies are transmitted over the network.
1334
1335 The block plugin should be put in a file {\tt
1336   plugin\_block\_SERVICE.c} in the service's respective directory. The
1337 mandatory functions that need to be implemented for a block plugin are
1338 described in the following sections.
1339
1340 \subsubsection{Validating requests and replies}
1341
1342 The evaluate function should validate a reply or a request. It returns
1343 a {\tt GNUNET\_BLOCK\_EvaluationResult}, which is an enumeration. All
1344 possible answers are in {\tt gnunet\_block\_lib.h}.  The function will
1345 be called with a {\tt reply\_block} argument of {\tt NULL} for
1346 requests.  Note that depending on how {\tt evaluate} is called, only
1347 some of the possible return values are valid.  The specific meaning of
1348 the {\tt xquery} argument is application-specific.  Applications that
1349 do not use an extended query should check that the {\tt xquery\_size}
1350 is zero.  The block group is typically used to filter duplicate
1351 replies.
1352
1353 \lstset{language=C}
1354 \begin{lstlisting}
1355 static enum GNUNET_BLOCK_EvaluationResult
1356 block_plugin_SERVICE_evaluate (void *cls,
1357                               enum GNUNET_BLOCK_Type type,
1358                               struct GNUNET_BlockGroup *bg,
1359                               const GNUNET_HashCode *query,
1360                               const void *xquery,
1361                               size_t xquery_size,
1362                               const void *reply_block,
1363                               size_t reply_block_size)
1364 {
1365   /* Verify type, block and bg */
1366 }
1367 \end{lstlisting}
1368
1369 Note that it is mandatory to detect duplicate replies in this function
1370 and return the respective status code.  Duplicate detection is
1371 typically done using the Bloom filter block group provided by {\tt
1372   libgnunetblockgroup.so}.  Failure to do so may cause replies to
1373 circle in the network.
1374
1375 \subsubsection{Deriving a key from a reply}
1376
1377 The DHT can operate more efficiently if it is possible to derive a key
1378 from the value of the corresponding block.  The {\tt get\_key}
1379 function is used to obtain the key of a block --- for example, by
1380 means of hashing.  If deriving the key is not possible, the function
1381 should simply return {\tt GNUNET\_SYSERR} (the DHT will still work
1382 just fine with such blocks).
1383
1384 \lstset{language=C}
1385 \begin{lstlisting}
1386 static int
1387 block_plugin_SERVICE_get_key (void *cls, enum GNUNET_BLOCK_Type type,
1388                               const void *block, size_t block_size,
1389                               struct GNUNET_HashCode *key)
1390 {
1391     /* Store the key in the key argument, return GNUNET_OK on success. */
1392 }
1393 \end{lstlisting}
1394
1395 \subsubsection{Initialization of the plugin}
1396
1397 The plugin is realized as a shared C library.  The library must export
1398 an initialization function which should initialize the plugin.  The
1399 initialization function specifies what block types the plugin cares
1400 about and returns a struct with the functions that are to be used for
1401 validation and obtaining keys (the ones just defined above).
1402
1403 \lstset{language=C}
1404 \begin{lstlisting}
1405 void *
1406 libgnunet_plugin_block_SERVICE_init (void *cls)
1407 {
1408   static enum GNUNET_BLOCK_Type types[] =
1409   {
1410     GNUNET_BLOCK_TYPE_SERVICE_BLOCKYPE, /* list of blocks we care about, from gnunet_block_lib.h */
1411     GNUNET_BLOCK_TYPE_ANY       /* end of list */
1412   };
1413   struct GNUNET_BLOCK_PluginFunctions *api;
1414
1415   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
1416   api->evaluate = &block_plugin_SERICE_evaluate;
1417   api->get_key = &block_plugin_SERVICE_get_key;
1418   api->types = types;
1419   return api;
1420 }
1421 \end{lstlisting}
1422
1423 \subsubsection{Shutdown of the plugin}
1424
1425 Following GNUnet's general plugin API concept, the plugin must
1426 export a second function for cleaning up.  It usually does very
1427 little.
1428
1429 \lstset{language=C}
1430 \begin{lstlisting}
1431 void *
1432 libgnunet_plugin_block_SERVICE_done (void *cls)
1433 {
1434   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1435
1436   GNUNET_free (api);
1437   return NULL;
1438 }
1439 \end{lstlisting}
1440
1441
1442 \subsubsection{Integration of the plugin with the build system}
1443
1444 In order to compile the plugin, the {\tt Makefile.am} file for the
1445 service \texttt{SERVICE} should contain a rule similar to this:
1446
1447 \lstset{language=make}
1448 \begin{lstlisting}
1449   plugindir = $(libdir)/gnunet
1450
1451   plugin_LTLIBRARIES = \
1452           libgnunet_plugin_block_ext.la
1453   libgnunet_plugin_block_ext_la_SOURCES = \
1454           plugin_block_ext.c
1455   libgnunet_plugin_block_ext_la_LIBADD = \
1456           $(prefix)/lib/libgnunethello.la \
1457           $(prefix)/lib/libgnunetblock.la \
1458           $(prefix)/lib/libgnunetutil.la
1459   libgnunet_plugin_block_ext_la_LDFLAGS = \
1460           $(GN_PLUGIN_LDFLAGS)
1461   libgnunet_plugin_block_ext_la_DEPENDENCIES = \
1462           $(prefix)/lib/libgnunetblock.la
1463 \end{lstlisting}
1464 % $
1465
1466
1467 \exercise{Write a block plugin that accepts all queries
1468 and all replies but prints information about queries and replies
1469 when the respective validation hooks are called.}
1470
1471
1472
1473 \subsection{Monitoring the DHT}
1474 It is possible to monitor the functioning of the local DHT service. When monitoring
1475 the DHT, the service will alert the monitoring program of any events,
1476 both started locally or received for routing from another peer. The are three different
1477 types of events possible: a GET request, a PUT request or a response (a reply to
1478 a GET).
1479
1480 Since the different events have different associated data, the API gets 3
1481 different callbacks (one for each message type) and optional type and key parameters,
1482 to allow for filtering of messages. When an event happens, the appropiate callback
1483 is called with all the information about the event.
1484 \lstset{language=C}
1485 \begin{lstlisting}
1486 void
1487 get_callback (void *cls,
1488               enum GNUNET_DHT_RouteOption options,
1489               enum GNUNET_BLOCK_Type type,
1490               uint32_t hop_count,
1491               uint32_t desired_replication_level,
1492               unsigned int path_length,
1493               const struct GNUNET_PeerIdentity *path,
1494               const struct GNUNET_HashCode * key)
1495 {
1496 }
1497
1498 void
1499 get_resp_callback (void *cls,
1500                    enum GNUNET_BLOCK_Type type,
1501                    const struct GNUNET_PeerIdentity *get_path,
1502                    unsigned int get_path_length,
1503                    const struct GNUNET_PeerIdentity *put_path,
1504                    unsigned int put_path_length,
1505                    struct GNUNET_TIME_Absolute exp,
1506                    const struct GNUNET_HashCode * key,
1507                    const void *data,
1508                    size_t size)
1509 {
1510 }
1511
1512 void
1513 put_callback (void *cls,
1514               enum GNUNET_DHT_RouteOption options,
1515               enum GNUNET_BLOCK_Type type,
1516               uint32_t hop_count,
1517               uint32_t desired_replication_level,
1518               unsigned int path_length,
1519               const struct GNUNET_PeerIdentity *path,
1520               struct GNUNET_TIME_Absolute exp,
1521               const struct GNUNET_HashCode * key,
1522               const void *data,
1523               size_t size)
1524 {
1525 }
1526
1527 monitor_handle = GNUNET_DHT_monitor_start (dht_handle,
1528                                            block_type, /* GNUNET_BLOCK_TYPE_ANY for all */
1529                                            key, /* NULL for all */
1530                                            &get_callback,
1531                                            &get_resp_callback,
1532                                            &put_callback,
1533                                            cls);
1534 \end{lstlisting}
1535
1536
1537 \section{Debugging with {\tt gnunet-arm}}
1538
1539 Even if services are managed by {\tt gnunet-arm}, you can start them with
1540 {\tt gdb} or {\tt valgrind}.  For example, you could add the following lines
1541 to your configuration file to start the DHT service in a {\tt gdb} session in a
1542 fresh {\tt xterm}:
1543
1544 \begin{verbatim}
1545 [dht]
1546 PREFIX=xterm -e gdb --args
1547 \end{verbatim}
1548
1549 Alternatively, you can stop a service that was started via ARM and run it manually:
1550
1551 \lstset{language=bash}
1552 \begin{lstlisting}
1553 $ gnunet-arm -k dht
1554 $ gdb --args gnunet-service-dht -L DEBUG
1555 $ valgrind gnunet-service-dht -L DEBUG
1556 \end{lstlisting}
1557 % $
1558
1559 Assuming other services are well-written, they will automatically re-integrate the
1560 restarted service with the peer.
1561
1562 GNUnet provides a powerful logging mechanism providing log levels \texttt{ERROR},
1563 \texttt{WARNING}, \texttt{INFO} and \texttt{DEBUG}. The current log level is
1564 configured using the \lstinline|$GNUNET_FORCE_LOG| environmental variable.
1565 The \texttt{DEBUG} level is only available if \lstinline|--enable-logging=verbose| was used when
1566 running \texttt{configure}. More details about logging can be found under
1567 \url{https://gnunet.org/logging}.
1568
1569 You should also probably enable the creation of core files, by setting
1570 {\tt ulimit}, and echo'ing 1 into {\tt /proc/sys/kernel/core\_uses\_pid}.
1571 Then you can investigate the core dumps with {\tt gdb}, which is often
1572 the fastest method to find simple errors.
1573
1574 \exercise{Add a memory leak to your service and obtain a trace
1575 pointing to the leak using {\tt valgrind} while running the service
1576 from {\tt gnunet-service-arm}.}
1577
1578
1579 \end{document}