iPXE
pccrd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stddef.h>
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <ctype.h>
00031 #include <errno.h>
00032 #include <assert.h>
00033 #include <ipxe/pccrd.h>
00034 
00035 /** @file
00036  *
00037  * Peer Content Caching and Retrieval: Discovery Protocol [MS-PCCRD]
00038  *
00039  * This protocol manages to ingeniously combine the excessive
00040  * verbosity of XML with a paucity of actual information.  For
00041  * example: even in version 2.0 of the protocol it is still not
00042  * possible to discover which peers hold a specific block within a
00043  * given segment.
00044  *
00045  * For added bonus points, version 1.0 of the protocol is specified to
00046  * use a case-sensitive string comparison (for SHA2 digest values) but
00047  * nothing specifies whether the strings in question should be in
00048  * upper or lower case.  There are example strings given in the
00049  * specification, but the author skilfully manages to leave the issue
00050  * unresolved by using the somewhat implausible digest value of
00051  * "0200000000000000000000000000000000000000000000000000000000000000".
00052  *
00053  * Just in case you were thinking that the silver lining of the choice
00054  * to use an XML-based protocol would be the ability to generate and
00055  * process messages with standard tools, version 2.0 of the protocol
00056  * places most of the critical information inside a Base64-encoded
00057  * custom binary data structure.  Within an XML element, naturally.
00058  *
00059  * I hereby announce this specification to be the 2015 winner of the
00060  * prestigious "UEFI HII API" award for incompetent design.
00061  */
00062 
00063 /** Discovery request format */
00064 #define PEERDIST_DISCOVERY_REQUEST                                            \
00065         "<?xml version=\"1.0\" encoding=\"utf-8\"?>"                          \
00066         "<soap:Envelope "                                                     \
00067             "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" "         \
00068             "xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" " \
00069             "xmlns:wsd=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" "  \
00070             "xmlns:PeerDist=\"http://schemas.microsoft.com/p2p/"              \
00071                              "2007/09/PeerDistributionDiscovery\">"           \
00072           "<soap:Header>"                                                     \
00073             "<wsa:To>"                                                        \
00074               "urn:schemas-xmlsoap-org:ws:2005:04:discovery"                  \
00075             "</wsa:To>"                                                       \
00076             "<wsa:Action>"                                                    \
00077               "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe"         \
00078             "</wsa:Action>"                                                   \
00079             "<wsa:MessageID>"                                                 \
00080               "urn:uuid:%s"                                                   \
00081             "</wsa:MessageID>"                                                \
00082           "</soap:Header>"                                                    \
00083           "<soap:Body>"                                                       \
00084             "<wsd:Probe>"                                                     \
00085               "<wsd:Types>"                                                   \
00086                 "PeerDist:PeerDistData"                                       \
00087               "</wsd:Types>"                                                  \
00088               "<wsd:Scopes MatchBy=\"http://schemas.xmlsoap.org/ws/"          \
00089                                     "2005/04/discovery/strcmp0\">"            \
00090                 "%s"                                                          \
00091               "</wsd:Scopes>"                                                 \
00092             "</wsd:Probe>"                                                    \
00093           "</soap:Body>"                                                      \
00094         "</soap:Envelope>"
00095 
00096 /**
00097  * Construct discovery request
00098  *
00099  * @v uuid              Message UUID string
00100  * @v id                Segment identifier string
00101  * @ret request         Discovery request, or NULL on failure
00102  *
00103  * The request is dynamically allocated; the caller must eventually
00104  * free() the request.
00105  */
00106 char * peerdist_discovery_request ( const char *uuid, const char *id ) {
00107         char *request;
00108         int len;
00109 
00110         /* Construct request */
00111         len = asprintf ( &request, PEERDIST_DISCOVERY_REQUEST, uuid, id );
00112         if ( len < 0 )
00113                 return NULL;
00114 
00115         return request;
00116 }
00117 
00118 /**
00119  * Locate discovery reply tag
00120  *
00121  * @v data              Reply data (not NUL-terminated)
00122  * @v len               Length of reply data
00123  * @v tag               XML tag
00124  * @ret found           Found tag (or NULL if not found)
00125  */
00126 static char * peerdist_discovery_reply_tag ( char *data, size_t len,
00127                                              const char *tag ) {
00128         size_t tag_len = strlen ( tag );
00129 
00130         /* Search, allowing for the fact that the reply data is not
00131          * cleanly NUL-terminated and may contain embedded NULs due to
00132          * earlier parsing.
00133          */
00134         for ( ; len >= tag_len ; data++, len-- ) {
00135                 if ( strncmp ( data, tag, tag_len ) == 0 )
00136                         return data;
00137         }
00138         return NULL;
00139 }
00140 
00141 /**
00142  * Locate discovery reply values
00143  *
00144  * @v data              Reply data (not NUL-terminated, will be modified)
00145  * @v len               Length of reply data
00146  * @v name              XML tag name
00147  * @ret values          Tag values (or NULL if not found)
00148  *
00149  * The reply data is modified by adding NULs and moving characters as
00150  * needed to produce a NUL-separated list of values, terminated with a
00151  * zero-length string.
00152  *
00153  * This is not supposed to be a full XML parser; it's supposed to
00154  * include just enough functionality to allow PeerDist discovery to
00155  * work with existing implementations.
00156  */
00157 static char * peerdist_discovery_reply_values ( char *data, size_t len,
00158                                                 const char *name ) {
00159         char buf[ 2 /* "</" */ + strlen ( name ) + 1 /* ">" */ + 1 /* NUL */ ];
00160         char *open;
00161         char *close;
00162         char *start;
00163         char *end;
00164         char *in;
00165         char *out;
00166         char c;
00167 
00168         /* Locate opening tag */
00169         snprintf ( buf, sizeof ( buf ), "<%s>", name );
00170         open = peerdist_discovery_reply_tag ( data, len, buf );
00171         if ( ! open )
00172                 return NULL;
00173         start = ( open + strlen ( buf ) );
00174         len -= ( start - data );
00175         data = start;
00176 
00177         /* Locate closing tag */
00178         snprintf ( buf, sizeof ( buf ), "</%s>", name );
00179         close = peerdist_discovery_reply_tag ( data, len, buf );
00180         if ( ! close )
00181                 return NULL;
00182         assert ( close >= open );
00183         end = close;
00184 
00185         /* Strip initial whitespace, convert other whitespace
00186          * sequences to single NULs, add terminating pair of NULs.
00187          * This will probably overwrite part of the closing tag.
00188          */
00189         for ( in = start, out = start ; in < end ; in++ ) {
00190                 c = *in;
00191                 if ( isspace ( c ) ) {
00192                         if ( ( out > start ) && ( out[-1] != '\0' ) )
00193                                 *(out++) = '\0';
00194                 } else {
00195                         *(out++) = c;
00196                 }
00197         }
00198         *(out++) = '\0';
00199         *(out++) = '\0';
00200         assert ( out < ( close + strlen ( buf ) ) );
00201 
00202         return start;
00203 }
00204 
00205 /**
00206  * Parse discovery reply
00207  *
00208  * @v data              Reply data (not NUL-terminated, will be modified)
00209  * @v len               Length of reply data
00210  * @v reply             Discovery reply to fill in
00211  * @ret rc              Return status code
00212  *
00213  * The discovery reply includes pointers to strings within the
00214  * modified reply data.
00215  */
00216 int peerdist_discovery_reply ( char *data, size_t len,
00217                                struct peerdist_discovery_reply *reply ) {
00218         static const struct peerdist_discovery_block_count zcount = {
00219                 .hex = "00000000",
00220         };
00221         struct peerdist_discovery_block_count *count;
00222         unsigned int max;
00223         unsigned int i;
00224         char *scopes;
00225         char *xaddrs;
00226         char *blockcount;
00227         char *in;
00228         char *out;
00229         size_t skip;
00230 
00231         /* Find <wsd:Scopes> tag */
00232         scopes = peerdist_discovery_reply_values ( data, len, "wsd:Scopes" );
00233         if ( ! scopes ) {
00234                 DBGC ( reply, "PCCRD %p missing <wsd:Scopes> tag\n", reply );
00235                 return -ENOENT;
00236         }
00237 
00238         /* Find <wsd:XAddrs> tag */
00239         xaddrs = peerdist_discovery_reply_values ( data, len, "wsd:XAddrs" );
00240         if ( ! xaddrs ) {
00241                 DBGC ( reply, "PCCRD %p missing <wsd:XAddrs> tag\n", reply );
00242                 return -ENOENT;
00243         }
00244 
00245         /* Find <PeerDist:BlockCount> tag */
00246         blockcount = peerdist_discovery_reply_values ( data, len,
00247                                                        "PeerDist:BlockCount" );
00248         if ( ! blockcount ) {
00249                 DBGC ( reply, "PCCRD %p missing <PeerDist:BlockCount> tag\n",
00250                        reply );
00251                 return -ENOENT;
00252         }
00253 
00254         /* Determine maximum number of segments (according to number
00255          * of entries in the block count list).
00256          */
00257         max = ( strlen ( blockcount ) / sizeof ( *count ) );
00258         count = container_of ( blockcount,
00259                                struct peerdist_discovery_block_count, hex[0] );
00260 
00261         /* Eliminate any segments with a zero block count */
00262         for ( i = 0, in = scopes, out = scopes ; *in ; i++, in += skip ) {
00263 
00264                 /* Fail if we have overrun the maximum number of segments */
00265                 if ( i >= max ) {
00266                         DBGC ( reply, "PCCRD %p too many segment IDs\n",
00267                                reply );
00268                         return -EPROTO;
00269                 }
00270 
00271                 /* Delete segment if block count is zero */
00272                 skip = ( strlen ( in ) + 1 /* NUL */ );
00273                 if ( memcmp ( count[i].hex, zcount.hex,
00274                               sizeof ( zcount.hex ) ) == 0 )
00275                         continue;
00276                 strcpy ( out, in );
00277                 out += skip;
00278         }
00279         out[0] = '\0'; /* Ensure list is terminated with a zero-length string */
00280 
00281         /* Fill in discovery reply */
00282         reply->ids = scopes;
00283         reply->locations = xaddrs;
00284 
00285         return 0;
00286 }