Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1n6MvL-0004AO-KH for mharc-grub-devel@gnu.org; Sat, 08 Jan 2022 20:23:07 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37126) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n6MvK-0004AG-BR for grub-devel@gnu.org; Sat, 08 Jan 2022 20:23:06 -0500 Received: from mail2.cableone.syn-alias.com ([69.168.106.66]:46916 helo=mail.cableone.net) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n6MvI-00044t-JY for grub-devel@gnu.org; Sat, 08 Jan 2022 20:23:06 -0500 X-Authed-Username: c2JhbG91c2VrQGNhYmxlb25lLm5ldA== Authentication-Results: smtp02.lapis.bos.sync.lan smtp.user=sbalousek@cableone.net; auth=pass (LOGIN) Received: from [24.117.60.89] ([24.117.60.89:51124] helo=wickedloop.com) by mail.cableone.net (envelope-from ) (ecelerity 3.6.25.56547 r(Core:3.6.25.0)) with ESMTPSA (cipher=DHE-RSA-AES128-GCM-SHA256) id 0B/F1-29959-6F83AD16; Sat, 08 Jan 2022 20:23:02 -0500 From: Stephen Balousek To: grub-devel@gnu.org Cc: Daniel Kiper , Stephen Balousek Subject: [PATCH v2] http: Allow use of non-standard TCP/IP ports Date: Sat, 8 Jan 2022 18:22:47 -0700 Message-Id: <20220109012247.19808-1-sbalousek@wickedloop.com> In-Reply-To: <20211103152852.dn2fkfdom3s5p4sp@tomti.i.net-space.pl> References: <20211103152852.dn2fkfdom3s5p4sp@tomti.i.net-space.pl> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Vade-Verdict: clean X-Vade-Analysis-1: gggruggvucftvghtrhhoucdtuddrgedvuddrudegiedgfeegucetufdoteggodetrfdotffvucfrrhho X-Vade-Analysis-2: fhhilhgvmecuufgjpfetvefqtfdpuffrmfdpgffpggdquffrmfdpqfgfvfenuceurghilhhouhhtmecu X-Vade-Analysis-3: fedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvufffkfgjfhgggfes X-Vade-Analysis-4: tdekredttddttdenucfhrhhomhepufhtvghphhgvnhcuuegrlhhouhhsvghkuceoshgsrghlohhushgv X-Vade-Analysis-5: khesfihitghkvggulhhoohhprdgtohhmqeenucggtffrrghtthgvrhhnpeffieehtdffjeffiefhgedt X-Vade-Analysis-6: teduteehtdeiueeffeehvdefvdfftdetjeeiiedvffenucffohhmrghinhepfihitghkvggulhhoohhp X-Vade-Analysis-7: rdgtohhmpdgvgigrmhhplhgvrdgtohhmnecukfhppedvgedruddujedriedtrdekleenucevlhhushht X-Vade-Analysis-8: vghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpedvgedruddujedriedtrdekledphhgvlhhopeif X-Vade-Analysis-9: ihgtkhgvughlohhophdrtghomhdpmhgrihhlfhhrohhmpehssggrlhhouhhsvghkseifihgtkhgvughl X-Vade-Analysis-10: ohhophdrtghomhdprhgtphhtthhopehgrhhusgdquggvvhgvlhesghhnuhdrohhrghdprhgtphhtthho X-Vade-Analysis-11: pegukhhiphgvrhesnhgvthdqshhprggtvgdrphhlpdhmthgrhhhoshhtpehsmhhtphdrlhgrphhishdr X-Vade-Analysis-12: sghoshdrshihnhgtrdhlrghnpdhnsggprhgtphhtthhopedvpdgruhhthhgpuhhsvghrpehssggrlhho X-Vade-Analysis-13: uhhsvghksegtrggslhgvohhnvgdrnhgvth X-Vade-Client: LAPIS Received-SPF: none client-ip=69.168.106.66; envelope-from=sbalousek@wickedloop.com; helo=mail.cableone.net X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 09 Jan 2022 01:23:06 -0000 Allow the use of HTTP servers listening on ports other 80. This is done with an extension to the http notation: (http[,server[,port]]) - or - (http[,server[:port]]) Signed-off-by: Stephen Balousek --- Hi Daniel, Happy New Year! I apologize for the long delay. I really wanted to test the changes for an HTTP server over IPv6, but not knowing much about setting up an IPv6 network made for a pretty steep learning curve! Anyway, please find attached a new version of the patch for allowing the use of non-standard HTTP server ports. I added some comments to the code for parsing the port number from an IPv6 address, and added relevant examples to the INFO document. Also, you mentioned: I think it should be "port_number == GRUB_ULONG_MAX" instead of "port_number == 0" here. I took a closer look at grub_strtoul() and that function actually returns zero (0) when a non-numeric value is encounteded in the string. So, functionally, I think these changes are correct. Thank you for your patience and all the help! - Steve docs/grub.texi | 33 +++++++++++++++++++++++++++++++++ grub-core/net/http.c | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/docs/grub.texi b/docs/grub.texi index f8b4b3b21..a95d86f95 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -3004,6 +3004,39 @@ environment variable @samp{net_default_server} is used. Before using the network drive, you must initialize the network. @xref{Network}, for more information. +For the @samp{http} network protocol, @code{@var{server}} may specify a +port number other than the default value of @samp{80}. The server name +and port number are separated by either @samp{,} or @samp{:}. +For IPv6 addresses, the server name and port number may only be separated +by @samp{,}. + +@itemize @bullet +@item +@code{(http,@var{server},@var{port})} + +@item +@code{(http,@var{server}:@var{port})} +@end itemize + +These examples all reference an @samp{http} server at address +@samp{192.168.0.100} listening on the non-standard port of @samp{3000}. +In these examples, the DNS name @samp{grub.example.com} is resolved +to @samp{192.168.0.100}. + +@example +(http,grub.example.com,3000) +(http,grub.example.com:3000) +(http,192.168.0.100,3000) +(http,192.168.0.100:3000) +@end example + +Referencing an @samp{http} server over IPv6 on the non-standard +port of @samp{3000} would look like this: + +@example +(http,2001:dead:beef::1,3000) +@end example + If you boot GRUB from a CD-ROM, @samp{(cd)} is available. @xref{Making a GRUB bootable CD-ROM}, for details. diff --git a/grub-core/net/http.c b/grub-core/net/http.c index b616cf40b..f457cd010 100644 --- a/grub-core/net/http.c +++ b/grub-core/net/http.c @@ -312,6 +312,9 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial) int i; struct grub_net_buff *nb; grub_err_t err; + char *server_name; + char *port_string; + unsigned long port_number; nb = grub_netbuff_alloc (GRUB_NET_TCP_RESERVE_SIZE + sizeof ("GET ") - 1 @@ -390,10 +393,42 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial) grub_netbuff_put (nb, 2); grub_memcpy (ptr, "\r\n", 2); - data->sock = grub_net_tcp_open (file->device->net->server, - HTTP_PORT, http_receive, + port_string = grub_strrchr (file->device->net->server, ','); + if (port_string == NULL) + { + /* If ',port' is not found in the http server string, look for ':port' */ + port_string = grub_strrchr (file->device->net->server, ':'); + /* For IPv6 addresses, the ':port' syntax is not supported and ',port' must be used. */ + if (port_string != NULL && grub_strchr (file->device->net->server, ':') != port_string) + port_string = NULL; + } + if (port_string != NULL) + { + port_number = grub_strtoul (port_string + 1, 0, 10); + if (port_number == 0 && grub_errno == GRUB_ERR_BAD_NUMBER) + return grub_error (GRUB_ERR_BAD_NUMBER, N_("non-numeric or invalid port number `%s'"), port_string + 1); + if (port_number == 0 || port_number > 65535) + return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("port number `%s' not in the range of 1 to 65535"), port_string + 1); + + server_name = grub_strdup (file->device->net->server); + if (server_name == NULL) + return grub_errno; + server_name[port_string - file->device->net->server] = '\0'; + } + else + { + port_number = HTTP_PORT; + server_name = file->device->net->server; + } + + data->sock = grub_net_tcp_open (server_name, + port_number, http_receive, http_err, NULL, file); + + if (server_name != file->device->net->server) + grub_free (server_name); + if (!data->sock) { grub_netbuff_free (nb); -- 2.34.1