You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stddef.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <assert.h>
  31. #include <ipxe/pccrd.h>
  32. /** @file
  33. *
  34. * Peer Content Caching and Retrieval: Discovery Protocol [MS-PCCRD]
  35. *
  36. * This protocol manages to ingeniously combine the excessive
  37. * verbosity of XML with a paucity of actual information. For
  38. * example: even in version 2.0 of the protocol it is still not
  39. * possible to discover which peers hold a specific block within a
  40. * given segment.
  41. *
  42. * For added bonus points, version 1.0 of the protocol is specified to
  43. * use a case-sensitive string comparison (for SHA2 digest values) but
  44. * nothing specifies whether the strings in question should be in
  45. * upper or lower case. There are example strings given in the
  46. * specification, but the author skilfully manages to leave the issue
  47. * unresolved by using the somewhat implausible digest value of
  48. * "0200000000000000000000000000000000000000000000000000000000000000".
  49. *
  50. * Just in case you were thinking that the silver lining of the choice
  51. * to use an XML-based protocol would be the ability to generate and
  52. * process messages with standard tools, version 2.0 of the protocol
  53. * places most of the critical information inside a Base64-encoded
  54. * custom binary data structure. Within an XML element, naturally.
  55. *
  56. * I hereby announce this specification to be the 2015 winner of the
  57. * prestigious "UEFI HII API" award for incompetent design.
  58. */
  59. /** Discovery request format */
  60. #define PEERDIST_DISCOVERY_REQUEST \
  61. "<?xml version=\"1.0\" encoding=\"utf-8\"?>" \
  62. "<soap:Envelope " \
  63. "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" " \
  64. "xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" " \
  65. "xmlns:wsd=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" " \
  66. "xmlns:PeerDist=\"http://schemas.microsoft.com/p2p/" \
  67. "2007/09/PeerDistributionDiscovery\">" \
  68. "<soap:Header>" \
  69. "<wsa:To>" \
  70. "urn:schemas-xmlsoap-org:ws:2005:04:discovery" \
  71. "</wsa:To>" \
  72. "<wsa:Action>" \
  73. "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe" \
  74. "</wsa:Action>" \
  75. "<wsa:MessageID>" \
  76. "urn:uuid:%s" \
  77. "</wsa:MessageID>" \
  78. "</soap:Header>" \
  79. "<soap:Body>" \
  80. "<wsd:Probe>" \
  81. "<wsd:Types>" \
  82. "PeerDist:PeerDistData" \
  83. "</wsd:Types>" \
  84. "<wsd:Scopes MatchBy=\"http://schemas.xmlsoap.org/ws/" \
  85. "2005/04/discovery/strcmp0\">" \
  86. "%s" \
  87. "</wsd:Scopes>" \
  88. "</wsd:Probe>" \
  89. "</soap:Body>" \
  90. "</soap:Envelope>"
  91. /**
  92. * Construct discovery request
  93. *
  94. * @v uuid Message UUID string
  95. * @v id Segment identifier string
  96. * @ret request Discovery request, or NULL on failure
  97. *
  98. * The request is dynamically allocated; the caller must eventually
  99. * free() the request.
  100. */
  101. char * peerdist_discovery_request ( const char *uuid, const char *id ) {
  102. char *request;
  103. int len;
  104. /* Construct request */
  105. len = asprintf ( &request, PEERDIST_DISCOVERY_REQUEST, uuid, id );
  106. if ( len < 0 )
  107. return NULL;
  108. return request;
  109. }
  110. /**
  111. * Locate discovery reply tag
  112. *
  113. * @v data Reply data (not NUL-terminated)
  114. * @v len Length of reply data
  115. * @v tag XML tag
  116. * @ret found Found tag (or NULL if not found)
  117. */
  118. static char * peerdist_discovery_reply_tag ( char *data, size_t len,
  119. const char *tag ) {
  120. size_t tag_len = strlen ( tag );
  121. /* Search, allowing for the fact that the reply data is not
  122. * cleanly NUL-terminated and may contain embedded NULs due to
  123. * earlier parsing.
  124. */
  125. for ( ; len >= tag_len ; data++, len-- ) {
  126. if ( strncmp ( data, tag, tag_len ) == 0 )
  127. return data;
  128. }
  129. return NULL;
  130. }
  131. /**
  132. * Locate discovery reply values
  133. *
  134. * @v data Reply data (not NUL-terminated, will be modified)
  135. * @v len Length of reply data
  136. * @v name XML tag name
  137. * @ret values Tag values (or NULL if not found)
  138. *
  139. * The reply data is modified by adding NULs and moving characters as
  140. * needed to produce a NUL-separated list of values, terminated with a
  141. * zero-length string.
  142. *
  143. * This is not supposed to be a full XML parser; it's supposed to
  144. * include just enough functionality to allow PeerDist discovery to
  145. * work with existing implementations.
  146. */
  147. static char * peerdist_discovery_reply_values ( char *data, size_t len,
  148. const char *name ) {
  149. char buf[ 2 /* "</" */ + strlen ( name ) + 1 /* ">" */ + 1 /* NUL */ ];
  150. char *open;
  151. char *close;
  152. char *start;
  153. char *end;
  154. char *in;
  155. char *out;
  156. char c;
  157. /* Locate opening tag */
  158. snprintf ( buf, sizeof ( buf ), "<%s>", name );
  159. open = peerdist_discovery_reply_tag ( data, len, buf );
  160. if ( ! open )
  161. return NULL;
  162. start = ( open + strlen ( buf ) );
  163. len -= ( start - data );
  164. data = start;
  165. /* Locate closing tag */
  166. snprintf ( buf, sizeof ( buf ), "</%s>", name );
  167. close = peerdist_discovery_reply_tag ( data, len, buf );
  168. if ( ! close )
  169. return NULL;
  170. assert ( close >= open );
  171. end = close;
  172. /* Strip initial whitespace, convert other whitespace
  173. * sequences to single NULs, add terminating pair of NULs.
  174. * This will probably overwrite part of the closing tag.
  175. */
  176. for ( in = start, out = start ; in < end ; in++ ) {
  177. c = *in;
  178. if ( isspace ( c ) ) {
  179. if ( ( out > start ) && ( out[-1] != '\0' ) )
  180. *(out++) = '\0';
  181. } else {
  182. *(out++) = c;
  183. }
  184. }
  185. *(out++) = '\0';
  186. *(out++) = '\0';
  187. assert ( out < ( close + strlen ( buf ) ) );
  188. return start;
  189. }
  190. /**
  191. * Parse discovery reply
  192. *
  193. * @v data Reply data (not NUL-terminated, will be modified)
  194. * @v len Length of reply data
  195. * @v reply Discovery reply to fill in
  196. * @ret rc Return status code
  197. *
  198. * The discovery reply includes pointers to strings within the
  199. * modified reply data.
  200. */
  201. int peerdist_discovery_reply ( char *data, size_t len,
  202. struct peerdist_discovery_reply *reply ) {
  203. static const struct peerdist_discovery_block_count zcount = {
  204. .hex = "00000000",
  205. };
  206. struct peerdist_discovery_block_count *count;
  207. unsigned int max;
  208. unsigned int i;
  209. char *scopes;
  210. char *xaddrs;
  211. char *blockcount;
  212. char *in;
  213. char *out;
  214. size_t skip;
  215. /* Find <wsd:Scopes> tag */
  216. scopes = peerdist_discovery_reply_values ( data, len, "wsd:Scopes" );
  217. if ( ! scopes ) {
  218. DBGC ( reply, "PCCRD %p missing <wsd:Scopes> tag\n", reply );
  219. return -ENOENT;
  220. }
  221. /* Find <wsd:XAddrs> tag */
  222. xaddrs = peerdist_discovery_reply_values ( data, len, "wsd:XAddrs" );
  223. if ( ! xaddrs ) {
  224. DBGC ( reply, "PCCRD %p missing <wsd:XAddrs> tag\n", reply );
  225. return -ENOENT;
  226. }
  227. /* Find <PeerDist:BlockCount> tag */
  228. blockcount = peerdist_discovery_reply_values ( data, len,
  229. "PeerDist:BlockCount" );
  230. if ( ! blockcount ) {
  231. DBGC ( reply, "PCCRD %p missing <PeerDist:BlockCount> tag\n",
  232. reply );
  233. return -ENOENT;
  234. }
  235. /* Determine maximum number of segments (according to number
  236. * of entries in the block count list).
  237. */
  238. max = ( strlen ( blockcount ) / sizeof ( *count ) );
  239. count = container_of ( blockcount,
  240. struct peerdist_discovery_block_count, hex[0] );
  241. /* Eliminate any segments with a zero block count */
  242. for ( i = 0, in = scopes, out = scopes ; *in ; i++, in += skip ) {
  243. /* Fail if we have overrun the maximum number of segments */
  244. if ( i >= max ) {
  245. DBGC ( reply, "PCCRD %p too many segment IDs\n",
  246. reply );
  247. return -EPROTO;
  248. }
  249. /* Delete segment if block count is zero */
  250. skip = ( strlen ( in ) + 1 /* NUL */ );
  251. if ( memcmp ( count[i].hex, zcount.hex,
  252. sizeof ( zcount.hex ) ) == 0 )
  253. continue;
  254. strcpy ( out, in );
  255. out += skip;
  256. }
  257. out[0] = '\0'; /* Ensure list is terminated with a zero-length string */
  258. /* Fill in discovery reply */
  259. reply->ids = scopes;
  260. reply->locations = xaddrs;
  261. return 0;
  262. }