network-structures.tm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <TeXmacs|2.1>
  2. <project|scheme-gnunet.tm>
  3. <style|tmmanual>
  4. <\body>
  5. <index|network structure><index|netstruct>The modules <scm|(gnu gnunet
  6. netstruct procedural)> and <scm|(gnu gnunet netstruct syntactic)> can be
  7. used for formatting messages to be sent over the network or to a
  8. service.<space|1em>The macros <scm|define-type><index|define-type> and
  9. <scm|structure/packed><index|structure/packed> can be used to define new
  10. structures, like this:
  11. <\scm-code>
  12. (define-type /:msg:nse:estimate/example
  13. \ \ (structure/packed
  14. \ \ \ (properties '((message-symbol msg:nse:estimate)))
  15. \ \ \ (synopsis "Network size estimate")
  16. \ \ \ (documentation "Some explanation")
  17. \ \ \ (field (header /:message-header))
  18. \ \ \ (field (size-estimate ieee-double/big)
  19. \ \ \ \ \ \ \ \ \ \ (synopsis "Timestamp for when the estimate was
  20. made"))))
  21. </scm-code>
  22. This example is taken from the <scm|(gnu gnunet nse struct)> module and
  23. oversimplified.<space|1em>All its components will gradually
  24. explained.<space|1em>First, what actually is a network
  25. structure?<space|1em>This question is ambigious, because \<#2018\>network
  26. structure\<#2019\> can refer to either the <with|font-shape|italic|value>
  27. or the <with|font-shape|italic|type>.<space|1em>The
  28. <with|font-shape|italic|value> is a sequence of octets, i.e., a sequence of
  29. numbers in the closed range 0\U255.<space|1em>The
  30. <with|font-shape|italic|type> describes how the
  31. <with|font-shape|italic|value> is structured.
  32. As an example, consider figure <reference|networkstructex>.<space|1em>There,
  33. the value is <scm|0 12 1 65 64 51 238 123 71 27 58 149> and the type is
  34. <scm|/:msg:nse:estimate/example>.
  35. <big-figure|<tree|<scm|/:msg:nse:estimate/example>|<tree|<scm|header>
  36. <scm|/:message-header>|<tree|<scm|size> <scm|u16/big>|0
  37. 12>|<tree|<scm|type> <scm|u16/big>|1 65>>|<tree|<scm|size-estimate>
  38. <scm|ieee-double/big>|64 51 238 123 71 27 58 149>>|A network structure,
  39. both <with|font-shape|italic|value> and
  40. <with|font-shape|italic|type>.<label|networkstructex>>
  41. This value has a <with|font-shape|italic|header> with a
  42. <with|font-shape|italic|size> \<#2018\>0 0 0 12\<#2019\> of type
  43. <scm|u32/big>, so the <with|font-shape|italic|size> is 12.<space|1em>The
  44. <with|font-shape|italic|header> also has a <with|font-shape|italic|type>
  45. \<#2018\>0 0 1 65\<#2019\> of type <scm|u32/big>, so the type is
  46. <math|256\<times\>1+65=321>.<space|1em>The value also has a
  47. <with|font-shape|italic|size estimate> of type
  48. <scm|ieee-double/big>.<space|1em>The octets <scm|64 51 238 123 71 27 58
  49. 149>, \ interpreted as an IEEE double, form the number
  50. <math|19.93\<ldots\>>.
  51. <section|Documentation>
  52. A network structure can optionally have embedded
  53. documentation.<space|1em>More specifically, network structures can
  54. optionally have the <with|font-shape|italic|synopsis><index|synopsis>,
  55. <with|font-shape|italic|documentation><index|documentation> and
  56. <with|font-shape|italic|properties><index|properties> set.<space|1em>The
  57. <with|font-shape|italic|synopsis> is a short description of what the
  58. network structure represents, typically a one-liner.<space|1em>The
  59. <with|font-shape|italic|documentation> can be more detailed, explaining how
  60. the structure works, can be used, is used and should be used.<space|1em>The
  61. <with|font-shape|italic|properties> form a free-formed association list.
  62. The synopsis, documentation and properties can be set on structured created
  63. with <scm|structure/packed> and individual fields and can be accessed with
  64. the procedures <scm|documentation>, <scm|synopsis> and <scm|properties>.
  65. The following properties are defined in Scheme-GNUnet:
  66. <\description>
  67. <\item*>
  68. <\code>
  69. message-symbol
  70. </code>
  71. </item*>
  72. <index|message-symbol>For <with|font-shape|italic|message structures>,
  73. this is a list of the names of the <with|font-shape|italic|message types>
  74. (see <reference|sec:message type> Message type database) this structure
  75. can be used for \U most of the time, there's only a single such message
  76. type, but sometimes a single structure can be used for multiple message
  77. types.
  78. <item*|c-type>
  79. <label|c-type>The value is the name of the equivalent type in the C
  80. implementation, if any, as a symbol. This can be useful if you know the
  81. name of the C type but not the name of the Scheme type: in that case, you
  82. can do <shell|git grep -F GNUNET_MessageHeader> in a git checkout of the
  83. source code of Scheme-GNUnet to discover that Scheme-GNUnet's name for
  84. <cpp|GNUNET_MessageHeader> is <scm|/:message-header>.
  85. </description>
  86. <todo|TODO: it would be nice to use the properties and documentation to
  87. automatically create a form of documentation, with some cross-references to
  88. the C code>
  89. <section|Reading and writing>
  90. The procedures <scm|read%><index|read%>, <scm|set%!><index|set%!>,
  91. <scm|sizeof><index|sizeof> and <scm|select><index|select> from <scm|(gnu
  92. gnunet netstruct procedural)> or the like-named macros from <scm|(gnu
  93. gnunet netstruct syntactic)> can be used for reading and writing network
  94. structures.<space|1em>The macros from <scm|syntactic> behave like the
  95. procedures from <scm|procedural> with some optimisations at expansion
  96. time.<space|1em>The procedures will be demonstrated with the
  97. <scm|/:msg:nse:estimate/example> network structure defined previously.
  98. First, create a memory slice with <scm|make-slice/read-write> from
  99. <scm|(gnu gnunet utils bv-slice)>.<space|1em>The required size can be
  100. determinded with <scm|(sizeof /:msg:nse:estimate/example
  101. '())>.<space|1em>The role of <scm|'()> will be explained later.
  102. <\scm-code>
  103. (import (gnu gnunet netstruct procedural)
  104. \ \ \ \ \ \ \ \ (gnu gnunet netstruct syntactic)
  105. \ \ \ \ \ \ \ \ (gnu gnunet utils bv-slice)
  106. \ \ \ \ \ \ \ \ (gnu gnunet util struct))
  107. \;
  108. (define-type /:msg:nse:estimate/example
  109. \ \ (structure/packed
  110. \ \ \ (field (header /:message-header))
  111. \ \ \ (field (size-estimate ieee-double/big))))
  112. \;
  113. (define message
  114. \ \ (make-slice/read-write (sizeof /:msg:nse:estimate/example '())))
  115. </scm-code>
  116. The fields of <scm|message> can be set with <scm|(set%! netstruct '(field
  117. ...) slice value)>.<space|1em>The following code sets all the fields:
  118. <\scm-code>
  119. (set%! /:msg:nse:estimate/example '(header size) message
  120. \ \ \ \ \ \ \ (sizeof /:msg:nse:estimate/example '()))
  121. (set%! /:msg:nse:estimate/example '(header type) message 165)
  122. (set%! /:msg:nse:estimate/example '(size-estimate) message 19.2)
  123. </scm-code>
  124. The size of an individual field can be determined with <scm|(sizeof
  125. netstruct '(field ...))>.<space|1em>For example, the following code
  126. determines the size of the \<#2018\>size\<#2019\> field in the header:
  127. <\scm-code>
  128. (sizeof /:msg:nse:estimate/example '(header size)) ; 12
  129. </scm-code>
  130. The fields can also be read:
  131. <\scm-code>
  132. (read% /:msg:nse:estimate/example '(header size) message) ; 12
  133. (read% /:msg:nse:estimate/example '(header type) message) ; 165
  134. (read% /:msg:nse:estimate/example '(size-estimate) message) ; 19.2
  135. </scm-code>
  136. <section|Primitive types>
  137. There are a number of pre-defined types.<space|1em>First, there is
  138. <scm|u8>, a single octet that is interpreted as an integer in the closed
  139. range <math|<around*|[|0,255|]>>.<space|1em>There are also types
  140. <scm|uN/endian> for <math|N\<in\><around*|{|16,32,64|}>> and
  141. <math|endian\<in\><around*|{|little,big|}>>, which interprets <math|N/8>
  142. octets as integers in the closed range <math|<around*|[|0,2<rsup|N>-1|]>>.<space|1em>The
  143. types <scm|ieee-double/big> and <scm|ieee-double/little> are 8 octets long
  144. and represent floating-point numbers in IEEE 754 format
  145. (\<#2018\>binary64\<#2019\>).
  146. <section|Packing>
  147. In contrast to C structures, Scheme-GNUnet network structures are always
  148. packed \V there are no \<#2018\>gaps\<#2019\> between fields.
  149. </body>
  150. <\initial>
  151. <\collection>
  152. <associate|page-medium|paper>
  153. <associate|save-aux|false>
  154. </collection>
  155. </initial>