support2.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <section id="support-ethernet">
  2. <title>
  3. Ethernet Functions
  4. </title>
  5. <para>
  6. The Open Powerline Toolkit supports raw Ethernet I/O on several popular operating systems, including <productname>Linux</productname>, <productname>Mac OS X</productname> and Microsoft <productname>Windows</productname>. Other operating systems will probably be added over time. These functions are found in the <filename>ether</filename> folder.
  7. </para>
  8. <para>
  9. Each operating system has a different raw Ethernet interface and so some abstraction was needed to support the toolkit for all environments. Our solution was the <structname>channel</structname> which is implemented like a <varname>FILE</varname> pointer but is used like a file descriptor. All toolkit programs, with a few exceptions, perform raw Ethernet I/O by opening a <structname>channel</structname>, reading and writing to it and then closing it.
  10. </para>
  11. <section id="support-channel">
  12. <title>
  13. channel
  14. </title>
  15. <para>
  16. The <structname>channel</structname> structure contains enough information to perform raw Ethernet I/O in several common runtime environments; however, portions of the structure vary depending on the environment. These differences are appled by compile time constants that include required structure members and exclude others. The common structure members are identified and described below. The others elements are not discussed because they may change.
  17. </para>
  18. <programlisting>
  19. typedef struct __packed <varname>channel</varname>
  20. {
  21. signed <varname>fd</varname>;
  22. signed <varname>ifindex</varname>;
  23. char const * <varname>ifname</varname>;
  24. uint8_t <varname>peer</varname> [<constant>ETHER_ADDR_LEN</constant>];
  25. uint8_t <varname>host</varname> [<constant>ETHER_ADDR_LEN</constant>];
  26. uint16_t <varname>type</varname>;
  27. ... &lt;operating system dependent data&gt; ...
  28. signed <varname>timeout</varname>;
  29. flag_t <varname>flags</varname>;
  30. } <varname>CHANNEL</varname>;
  31. </programlisting>
  32. <variablelist>
  33. <varlistentry>
  34. <term>
  35. .<varname>fd</varname>
  36. </term>
  37. <listitem>
  38. <para>
  39. Socket file descriptor.
  40. </para>
  41. </listitem>
  42. </varlistentry>
  43. <varlistentry>
  44. <term>
  45. .<varname>ifindex</varname>
  46. </term>
  47. <listitem>
  48. <para>
  49. Ethernet device index. The index only applies when the toolkit is compiled for <application>LibPcap</application> or <application>WinPcap</application>. This value is the same as that returned in the .<varname>ifr_ifindex</varname> member of the <varname>ifreq</varname> structure available on most operating systems.
  50. </para>
  51. </listitem>
  52. </varlistentry>
  53. <varlistentry>
  54. <term>
  55. .<varname>ifname</varname>
  56. </term>
  57. <listitem>
  58. <para>
  59. The interface name. On Linux, ethernet names are typically <quote>eth0</quote>, <quote>eth1</quote> and so on. On Mac OS X, interface names are <quote>en0</quote>, <quote>en1</quote> and so on. This string is the same as that returned by the <varname>ifr_ifname</varname> member of the <varname>ifreq</varname> structure available on most operating systems.
  60. </para>
  61. </listitem>
  62. </varlistentry>
  63. <varlistentry>
  64. <term>
  65. .<varname>peer</varname>
  66. </term>
  67. <listitem>
  68. <para>
  69. The Ethernet hardware address of some remote device. It is used to encode the <acronym>ODA</acronym> field of outgoing Ethernet frames and format some console messages. It is initialized to the Atheros Local Management Address, <constant>00:B0:52:00:00:01</constant> for HomePlug AV applications. Application programs can, and often do, replace this value at runtime.
  70. </para>
  71. </listitem>
  72. </varlistentry>
  73. <varlistentry>
  74. <term>
  75. .<varname>host</varname>
  76. </term>
  77. <listitem>
  78. <para>
  79. The Ethernet hardware address of the host computer. It is used to encode the <acronym>OSA</acronym> field of outgoing Ethernet frames and format some console messages. This address is initialized to the hardware address assigned to the interface by the host operating system. The value should not change.
  80. </para>
  81. </listitem>
  82. </varlistentry>
  83. <varlistentry>
  84. <term>
  85. .<varname>type</varname>
  86. </term>
  87. <listitem>
  88. <para>
  89. The Ethernet type/length field. It is used to encode the <acronym>MTYPE</acronym> field of outgoing Ethernet frames. The values is initialized to <constant>0x88E1</constant> for HomePlug AV application and <constant>0x887B</constant> for HomePlug 1.0 application. The value should not change.
  90. </para>
  91. </listitem>
  92. </varlistentry>
  93. <varlistentry>
  94. <term>
  95. .<varname>timeout</varname>
  96. </term>
  97. <listitem>
  98. <para>
  99. A time interval. On <productname>Linux</productname> and <productname>Mac OS X</productname>, it is the maximum time that the application will wait for a device to respond when a response is expected. With <productname>LibPcap</productname> and <productname>WinPcap</productname> it the mininum time the application will wait. It is initialized to <constant>50</constant> milliseconds which is a reasonable compromise but most toolkit programs allow the user to change this value.
  100. </para>
  101. </listitem>
  102. </varlistentry>
  103. <varlistentry>
  104. <term>
  105. .<varname>flags</varname>
  106. </term>
  107. <listitem>
  108. <para>
  109. A bitmap where each bit enables a special behavior during channel open or close or packet read or write. Of general interest is the <constant>CHANNEL_VERBOSE</constant> bit which prints outgoing and incoming frames on stderr in hexadecimal dump format. The verbose feature is implemented in for all toolkit programs that perform raw Ethernet I/O and is helpful when debugging device behavior.
  110. </para>
  111. </listitem>
  112. </varlistentry>
  113. </variablelist>
  114. <para>
  115. Since toolkit applications typically communicate with one powerline device at a time, this structure is statically initialized in a stand-alone module that is linked into each application. It is possible to dynamically initialize it, if needed. The structure is declared in <ulink url="channel.h.html">channel.h</ulink> and statically defined in <ulink url="channel.c.html">channel.c</ulink>.
  116. </para>
  117. </section>
  118. <section id="support-closechannel">
  119. <title>
  120. closechannel
  121. </title>
  122. <funcsynopsis>
  123. <funcprototype>
  124. <funcdef>signed <function>closechannel</function></funcdef>
  125. <paramdef>struct channel * <parameter>channel</parameter></paramdef>
  126. </funcprototype>
  127. </funcsynopsis>
  128. <para>
  129. Close the Ethernet socket associated with a <structname>channel</structname> and free associated memory and data structures. Return <constant>0</constant> on success. Return <constant>-1</constant> on failure. This function is declared in <ulink url="channel.h.html">channel.h</ulink> and defined in <ulink url="closechannel.c.html">closechannel.c</ulink>.
  130. </para>
  131. </section>
  132. <section id="support-openchannel">
  133. <title>
  134. openchannel
  135. </title>
  136. <funcsynopsis>
  137. <funcprototype>
  138. <funcdef>signed <function>openchannel</function></funcdef>
  139. <paramdef>struct channel * <parameter>channel</parameter></paramdef>
  140. <paramdef>uint16_t <parameter>protocol</parameter></paramdef>
  141. </funcprototype>
  142. </funcsynopsis>
  143. <para>
  144. Open an Ethernet socket that supports the specified protocol and associate it with the interface referenced by the <varname>channel</varname> structure <structname>.name</structname> member. Initialize the interface as needed. The <varname>protocol</varname> effectively filters incoming frames for the application.
  145. </para>
  146. <para>
  147. Interface initialization differs significantly from environment to environment. The socket descriptor is stored in the <varname>channel</varname> structure <structname>.fd</structname> member and the interface hardware address is stored in the <varname>channel</varname> structure <structname>.host</structname> member. Return <constant>0</constant> on success. Terminate the program with an error message on failure. This function is declared in <ulink url="channel.h.html">channel.h</ulink> and defined in <ulink url="openchannel.c.html">openchannel.c</ulink>.
  148. </para>
  149. </section>
  150. <section id="support-readpacket">
  151. <title>
  152. readpacket
  153. </title>
  154. <funcsynopsis>
  155. <funcprototype>
  156. <funcdef>signed <function>readpacket</function></funcdef>
  157. <paramdef>struct channel * <parameter>channel</parameter></paramdef>
  158. <paramdef>void * <parameter>packet</parameter></paramdef>
  159. <paramdef>signed <parameter>length</parameter></paramdef>
  160. </funcprototype>
  161. </funcsynopsis>
  162. <para>
  163. Read one Ethernet frame from the specified channel. The frame is written into memory starting at address <varname>packet</varname> and is truncated to the specified <varname>length</varname>, if necessary. Return the actual number of bytes read on success. Return <constant>0</constant> on timeout. Return <constant>-1</constant> on network error. This function behaves like the standard library <function>read</function> function. The target memory region remains unchanged on timeout or error. This function is declared in <ulink url="channel.h.html">channel.h</ulink> and defined in <ulink url="readpacket.c.html">readpacket.c</ulink>.
  164. </para>
  165. <para>
  166. On systems using Berkeley Packet Filters, such as MacOS X, the <varname>ODA</varname> field is automatically replaced on transmission to prevent Ethernet address spoofing. This may not be true on other systems but the practice is becoming more common.
  167. </para>
  168. </section>
  169. <section id="support-sendpacket">
  170. <title>
  171. sendpacket
  172. </title>
  173. <funcsynopsis>
  174. <funcprototype>
  175. <funcdef>signed <function>sendpacket</function></funcdef>
  176. <paramdef>struct channel * <parameter>channel</parameter></paramdef>
  177. <paramdef>void * <parameter>packet</parameter></paramdef>
  178. <paramdef>signed <parameter>length</parameter></paramdef>
  179. </funcprototype>
  180. </funcsynopsis>
  181. <para>
  182. Write one Ethernet frame to the specified channel. The frame is read from memory starting at address <varname> packet</varname> and ending at the specified <varname>length</varname>. Return the actual number of bytes sent on success. Return <constant>0</constant> on timeout. Return <constant>-1</constant> on network error. The frame should be properly formatted as an ethernet frame and must be at least 60 bytes long or it will not be sent. This function behaves like the standard library <function>write</function> function. The source memory region is not modified. This function is declared in <ulink url="channel.h.html">channel.h</ulink> and defined in <ulink url="sendpacket.c.html">sendpacket.c</ulink>.
  183. </para>
  184. </section>
  185. </section>