iPXE
pccrd.c
Go to the documentation of this file.
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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stddef.h>
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <ctype.h>
32#include <errno.h>
33#include <assert.h>
34#include <ipxe/pccrd.h>
35
36/** @file
37 *
38 * Peer Content Caching and Retrieval: Discovery Protocol [MS-PCCRD]
39 *
40 * This protocol manages to ingeniously combine the excessive
41 * verbosity of XML with a paucity of actual information. For
42 * example: even in version 2.0 of the protocol it is still not
43 * possible to discover which peers hold a specific block within a
44 * given segment.
45 *
46 * For added bonus points, version 1.0 of the protocol is specified to
47 * use a case-sensitive string comparison (for SHA2 digest values) but
48 * nothing specifies whether the strings in question should be in
49 * upper or lower case. There are example strings given in the
50 * specification, but the author skilfully manages to leave the issue
51 * unresolved by using the somewhat implausible digest value of
52 * "0200000000000000000000000000000000000000000000000000000000000000".
53 *
54 * Just in case you were thinking that the silver lining of the choice
55 * to use an XML-based protocol would be the ability to generate and
56 * process messages with standard tools, version 2.0 of the protocol
57 * places most of the critical information inside a Base64-encoded
58 * custom binary data structure. Within an XML element, naturally.
59 *
60 * I hereby announce this specification to be the 2015 winner of the
61 * prestigious "UEFI HII API" award for incompetent design.
62 */
63
64/** Discovery request format */
65#define PEERDIST_DISCOVERY_REQUEST \
66 "<?xml version=\"1.0\" encoding=\"utf-8\"?>" \
67 "<soap:Envelope " \
68 "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" " \
69 "xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" " \
70 "xmlns:wsd=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" " \
71 "xmlns:PeerDist=\"http://schemas.microsoft.com/p2p/" \
72 "2007/09/PeerDistributionDiscovery\">" \
73 "<soap:Header>" \
74 "<wsa:To>" \
75 "urn:schemas-xmlsoap-org:ws:2005:04:discovery" \
76 "</wsa:To>" \
77 "<wsa:Action>" \
78 "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe" \
79 "</wsa:Action>" \
80 "<wsa:MessageID>" \
81 "urn:uuid:%s" \
82 "</wsa:MessageID>" \
83 "</soap:Header>" \
84 "<soap:Body>" \
85 "<wsd:Probe>" \
86 "<wsd:Types>" \
87 "PeerDist:PeerDistData" \
88 "</wsd:Types>" \
89 "<wsd:Scopes MatchBy=\"http://schemas.xmlsoap.org/ws/" \
90 "2005/04/discovery/strcmp0\">" \
91 "%s" \
92 "</wsd:Scopes>" \
93 "</wsd:Probe>" \
94 "</soap:Body>" \
95 "</soap:Envelope>"
96
97/**
98 * Construct discovery request
99 *
100 * @v uuid Message UUID string
101 * @v id Segment identifier string
102 * @ret request Discovery request, or NULL on failure
103 *
104 * The request is dynamically allocated; the caller must eventually
105 * free() the request.
106 */
107char * peerdist_discovery_request ( const char *uuid, const char *id ) {
108 char *request;
109 int len;
110
111 /* Construct request */
113 if ( len < 0 )
114 return NULL;
115
116 return request;
117}
118
119/**
120 * Locate discovery reply tag
121 *
122 * @v data Reply data (not NUL-terminated)
123 * @v len Length of reply data
124 * @v tag XML tag
125 * @ret found Found tag (or NULL if not found)
126 */
127static char * peerdist_discovery_reply_tag ( char *data, size_t len,
128 const char *tag ) {
129 size_t tag_len = strlen ( tag );
130
131 /* Search, allowing for the fact that the reply data is not
132 * cleanly NUL-terminated and may contain embedded NULs due to
133 * earlier parsing.
134 */
135 for ( ; len >= tag_len ; data++, len-- ) {
136 if ( strncmp ( data, tag, tag_len ) == 0 )
137 return data;
138 }
139 return NULL;
140}
141
142/**
143 * Locate discovery reply values
144 *
145 * @v data Reply data (not NUL-terminated, will be modified)
146 * @v len Length of reply data
147 * @v name XML tag name
148 * @ret values Tag values (or NULL if not found)
149 *
150 * The reply data is modified by adding NULs and moving characters as
151 * needed to produce a NUL-separated list of values, terminated with a
152 * zero-length string.
153 *
154 * This is not supposed to be a full XML parser; it's supposed to
155 * include just enough functionality to allow PeerDist discovery to
156 * work with existing implementations.
157 */
158static char * peerdist_discovery_reply_values ( char *data, size_t len,
159 const char *name ) {
160 char buf[ 2 /* "</" */ + strlen ( name ) + 1 /* ">" */ + 1 /* NUL */ ];
161 char *open;
162 char *close;
163 char *start;
164 char *end;
165 char *in;
166 char *out;
167 char c;
168
169 /* Locate opening tag */
170 snprintf ( buf, sizeof ( buf ), "<%s>", name );
172 if ( ! open )
173 return NULL;
174 start = ( open + strlen ( buf ) );
175 len -= ( start - data );
176 data = start;
177
178 /* Locate closing tag */
179 snprintf ( buf, sizeof ( buf ), "</%s>", name );
181 if ( ! close )
182 return NULL;
183 assert ( close >= open );
184 end = close;
185
186 /* Strip initial whitespace, convert other whitespace
187 * sequences to single NULs, add terminating pair of NULs.
188 * This will probably overwrite part of the closing tag.
189 */
190 for ( in = start, out = start ; in < end ; in++ ) {
191 c = *in;
192 if ( isspace ( c ) ) {
193 if ( ( out > start ) && ( out[-1] != '\0' ) )
194 *(out++) = '\0';
195 } else {
196 *(out++) = c;
197 }
198 }
199 *(out++) = '\0';
200 *(out++) = '\0';
201 assert ( out < ( close + strlen ( buf ) ) );
202
203 return start;
204}
205
206/**
207 * Parse discovery reply
208 *
209 * @v data Reply data (not NUL-terminated, will be modified)
210 * @v len Length of reply data
211 * @v reply Discovery reply to fill in
212 * @ret rc Return status code
213 *
214 * The discovery reply includes pointers to strings within the
215 * modified reply data.
216 */
218 struct peerdist_discovery_reply *reply ) {
219 static const struct peerdist_discovery_block_count zcount = {
220 .hex = "00000000",
221 };
223 unsigned int max;
224 unsigned int i;
225 char *scopes;
226 char *xaddrs;
227 char *blockcount;
228 char *in;
229 char *out;
230 size_t skip;
231
232 /* Find <wsd:Scopes> tag */
233 scopes = peerdist_discovery_reply_values ( data, len, "wsd:Scopes" );
234 if ( ! scopes ) {
235 DBGC ( reply, "PCCRD %p missing <wsd:Scopes> tag\n", reply );
236 return -ENOENT;
237 }
238
239 /* Find <wsd:XAddrs> tag */
240 xaddrs = peerdist_discovery_reply_values ( data, len, "wsd:XAddrs" );
241 if ( ! xaddrs ) {
242 DBGC ( reply, "PCCRD %p missing <wsd:XAddrs> tag\n", reply );
243 return -ENOENT;
244 }
245
246 /* Find <PeerDist:BlockCount> tag */
248 "PeerDist:BlockCount" );
249 if ( ! blockcount ) {
250 DBGC ( reply, "PCCRD %p missing <PeerDist:BlockCount> tag\n",
251 reply );
252 return -ENOENT;
253 }
254
255 /* Determine maximum number of segments (according to number
256 * of entries in the block count list).
257 */
258 max = ( strlen ( blockcount ) / sizeof ( *count ) );
259 count = container_of ( blockcount,
261
262 /* Eliminate any segments with a zero block count */
263 for ( i = 0, in = scopes, out = scopes ; *in ; i++, in += skip ) {
264
265 /* Fail if we have overrun the maximum number of segments */
266 if ( i >= max ) {
267 DBGC ( reply, "PCCRD %p too many segment IDs\n",
268 reply );
269 return -EPROTO;
270 }
271
272 /* Delete segment if block count is zero */
273 skip = ( strlen ( in ) + 1 /* NUL */ );
274 if ( memcmp ( count[i].hex, zcount.hex,
275 sizeof ( zcount.hex ) ) == 0 )
276 continue;
277 strcpy ( out, in );
278 out += skip;
279 }
280 out[0] = '\0'; /* Ensure list is terminated with a zero-length string */
281
282 /* Fill in discovery reply */
283 reply->ids = scopes;
284 reply->locations = xaddrs;
285
286 return 0;
287}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
__be32 in[4]
Definition CIB_PRM.h:7
__be32 out[4]
Definition CIB_PRM.h:8
int asprintf(char **strp, const char *fmt,...)
Write a formatted string to newly allocated memory.
Definition asprintf.c:42
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
const char * name
Definition ath9k_hw.c:1986
#define max(x, y)
Definition ath.h:41
int isspace(int character)
Check to see if character is a space.
Definition ctype.c:42
Character types.
ring len
Length.
Definition dwmac.h:226
uint64_t tag
Identity tag.
Definition edd.h:1
uint8_t data[48]
Additional event data.
Definition ena.h:11
Error codes.
#define DBGC(...)
Definition compiler.h:505
uint32_t start
Starting offset.
Definition netvsc.h:1
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ENOENT
No such file or directory.
Definition errno.h:515
#define EPROTO
Protocol error.
Definition errno.h:625
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
u8 request[0]
List of IEs requested.
Definition ieee80211.h:2
String functions.
uint32_t end
Ending offset.
Definition netvsc.h:7
static char * peerdist_discovery_reply_values(char *data, size_t len, const char *name)
Locate discovery reply values.
Definition pccrd.c:158
static char * peerdist_discovery_reply_tag(char *data, size_t len, const char *tag)
Locate discovery reply tag.
Definition pccrd.c:127
int peerdist_discovery_reply(char *data, size_t len, struct peerdist_discovery_reply *reply)
Parse discovery reply.
Definition pccrd.c:217
char * peerdist_discovery_request(const char *uuid, const char *id)
Construct discovery request.
Definition pccrd.c:107
#define PEERDIST_DISCOVERY_REQUEST
Discovery request format.
Definition pccrd.c:65
Peer Content Caching and Retrieval: Discovery Protocol [MS-PCCRD].
char hex[8]
Count (as an eight-digit hex value)
Definition pccrd.h:1
int open(const char *uri_string)
Open file.
Definition posix_io.c:176
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
int strncmp(const char *first, const char *second, size_t max)
Compare strings.
Definition string.c:187
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
char * strcpy(char *dest, const char *src)
Copy string.
Definition string.c:347
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
A PeerDist discovery reply block count.
Definition pccrd.h:25
char hex[8]
Count (as an eight-digit hex value)
Definition pccrd.h:27
A PeerDist discovery reply.
Definition pccrd.h:31
char * ids
List of segment ID strings.
Definition pccrd.h:36
char * locations
List of peer locations.
Definition pccrd.h:41
A universally unique ID.
Definition uuid.h:16
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383
static struct evtchn_close * close
Definition xenevent.h:24