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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdio.h>
27 #include <ipxe/http.h>
28 #include <ipxe/settings.h>
29 #include <ipxe/peermux.h>
30 
31 /** @file
32  *
33  * Peer Content Caching and Retrieval (PeerDist) protocol
34  *
35  * This is quite possibly the ugliest protocol I have ever had the
36  * misfortune to encounter, and I've encountered multicast TFTP.
37  */
38 
39 /** PeerDist is globally enabled */
40 static long peerdist_enabled = 1;
41 
42 /**
43  * Check whether or not to support PeerDist encoding for this request
44  *
45  * @v http		HTTP transaction
46  * @ret supported	PeerDist encoding is supported for this request
47  */
http_peerdist_supported(struct http_transaction * http)48 static int http_peerdist_supported ( struct http_transaction *http ) {
49 
50 	/* Allow PeerDist to be globally enabled/disabled */
51 	if ( ! peerdist_enabled )
52 		return 0;
53 
54 	/* Support PeerDist encoding only if we can directly access an
55 	 * underlying data transfer buffer.  Direct access is required
56 	 * in order to support decryption of data received via the
57 	 * retrieval protocol (which provides the AES initialisation
58 	 * vector only after all of the encrypted data has been
59 	 * received).
60 	 *
61 	 * This test simultaneously ensures that we do not attempt to
62 	 * use PeerDist encoding on a request which is itself a
63 	 * PeerDist individual block download, since the individual
64 	 * block downloads do not themselves provide direct access to
65 	 * an underlying data transfer buffer.
66 	 */
67 	return ( xfer_buffer ( &http->xfer ) != NULL );
68 }
69 
70 /**
71  * Format HTTP "X-P2P-PeerDist" header
72  *
73  * @v http		HTTP transaction
74  * @v buf		Buffer
75  * @v len		Length of buffer
76  * @ret len		Length of header value, or negative error
77  */
http_format_p2p_peerdist(struct http_transaction * http,char * buf,size_t len)78 static int http_format_p2p_peerdist ( struct http_transaction *http,
79 				      char *buf, size_t len ) {
80 	int supported = http_peerdist_supported ( http );
81 	int missing;
82 
83 	/* PeerDist wants us to inform the server whenever we make a
84 	 * request for data that was missing from local peers
85 	 * (presumably for statistical purposes only).  We use the
86 	 * heuristic of assuming that the combination of "this request
87 	 * may not itself use PeerDist content encoding" and "this is
88 	 * a range request" probably indicates that we are making a
89 	 * PeerDist block raw range request for missing data.
90 	 */
91 	missing = ( http->request.range.len && ( ! supported ) );
92 
93 	/* Omit header if PeerDist encoding is not supported and we
94 	 * are not reporting a missing data request.
95 	 */
96 	if ( ! ( supported || missing ) )
97 		return 0;
98 
99 	/* Construct header */
100 	return snprintf ( buf, len, "Version=1.1%s",
101 			  ( missing ? ", MissingDataRequest=true" : "" ) );
102 }
103 
104 /** HTTP "X-P2P-PeerDist" header */
105 struct http_request_header http_request_p2p_peerdist __http_request_header = {
106 	.name = "X-P2P-PeerDist",
107 	.format = http_format_p2p_peerdist,
108 };
109 
110 /**
111  * Format HTTP "X-P2P-PeerDistEx" header
112  *
113  * @v http		HTTP transaction
114  * @v buf		Buffer
115  * @v len		Length of buffer
116  * @ret len		Length of header value, or negative error
117  */
http_format_p2p_peerdistex(struct http_transaction * http,char * buf,size_t len)118 static int http_format_p2p_peerdistex ( struct http_transaction *http,
119 					char *buf, size_t len ) {
120 	int supported = http_peerdist_supported ( http );
121 
122 	/* Omit header if PeerDist encoding is not supported */
123 	if ( ! supported )
124 		return 0;
125 
126 	/* Construct header */
127 	return snprintf ( buf, len, ( "MinContentInformation=1.0, "
128 				      "MaxContentInformation=2.0" ) );
129 }
130 
131 /** HTTP "X-P2P-PeerDist" header */
132 struct http_request_header http_request_p2p_peerdistex __http_request_header = {
133 	.name = "X-P2P-PeerDistEx",
134 	.format = http_format_p2p_peerdistex,
135 };
136 
137 /**
138  * Initialise PeerDist content encoding
139  *
140  * @v http		HTTP transaction
141  * @ret rc		Return status code
142  */
http_peerdist_init(struct http_transaction * http)143 static int http_peerdist_init ( struct http_transaction *http ) {
144 
145 	return peermux_filter ( &http->content, &http->transfer, http->uri );
146 }
147 
148 /** PeerDist HTTP content encoding */
149 struct http_content_encoding peerdist_encoding __http_content_encoding = {
150 	.name = "peerdist",
151 	.supported = http_peerdist_supported,
152 	.init = http_peerdist_init,
153 };
154 
155 /** PeerDist enabled setting */
156 const struct setting peerdist_setting __setting ( SETTING_MISC, peerdist ) = {
157 	.name = "peerdist",
158 	.description = "PeerDist enabled",
159 	.type = &setting_type_int8,
160 };
161 
162 /**
163  * Apply PeerDist settings
164  *
165  * @ret rc		Return status code
166  */
apply_peerdist_settings(void)167 static int apply_peerdist_settings ( void ) {
168 
169 	/* Fetch global PeerDist enabled setting */
170 	if ( fetch_int_setting ( NULL, &peerdist_setting,
171 				 &peerdist_enabled ) < 0 ) {
172 		peerdist_enabled = 1;
173 	}
174 	DBGC ( &peerdist_enabled, "PEERDIST is %s\n",
175 	       ( peerdist_enabled ? "enabled" : "disabled" ) );
176 
177 	return 0;
178 }
179 
180 /** PeerDist settings applicator */
181 struct settings_applicator peerdist_applicator __settings_applicator = {
182 	.apply = apply_peerdist_settings,
183 };
184