Skip to contentSkip to conversation

net: Allow use of non-standard TCP/IP ports for HTTP protocol.

PATCH Superseded Stephen Balousek 2 participants 8 messages Sep 6, 2021 – Jan 14, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: Allow use of non-standard TCP/IP ports for HTTP protocol.

Allow the use of HTTP servers listening on ports other 80. This is done with an extension to the http notation of either:

(http[,server[,port]])

- or -

(http[,server[:port]])

---
 grub-core/net/http.c | 43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)
grub-core/net/http.c +39 -4
index b616cf40b..87e496e7f 100644
@@ -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;
+  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,
-				  http_err, NULL,
-				  file);
+  port_string = grub_strrchr(file->device->net->server, ',');
+  if (!port_string)
+    {
+      port_string = grub_strrchr(file->device->net->server, ':');
+      if (grub_strchr(port_string + 1, ']'))
+        {
+          port_string = 0;
+        }
+    }
+  if (port_string)
+    {
+      port_number = grub_strtol(port_string + 1, 0, 10);
+      if (port_number <= 0 || port_number > 65535)
+        {
+          return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("invalid port number `%s'"), port_string + 1);
+        }
+
+      server_name = grub_strdup(file->device->net->server);
+      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);
Signature · 1 line
2.33.0
#1 lore raw
Quoted 19 lines · Stephen Balousek

Allow the use of HTTP servers listening on ports other 80. This is done with an extension to the http notation of either:

(http[,server[,port]])

- or -

(http[,server[:port]])

---
 grub-core/net/http.c | 43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)
grub-core/net/http.c +0 -0
index b616cf40b..87e496e7f 100644

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 317

Quoted 6 lines
grub-core/net/http.c +3 -0
@@ -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;
+  long port_number;

s/long/unsigned long/g

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 396

Quoted 11 lines
grub-core/net/http.c +1 -4
   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,
-				  http_err, NULL,
-				  file);
+  port_string = grub_strrchr(file->device->net->server, ',');

Missing space between grub_strrchr and "(". Same thing should be fixed in the functions calls below.

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 397

Quoted 1 line
grub-core/net/http.c +1 -0
+  if (!port_string)

if (port_string == NULL)

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 400

Quoted 3 lines
grub-core/net/http.c +3 -0
+    {
+      port_string = grub_strrchr(file->device->net->server, ':');
+      if (grub_strchr(port_string + 1, ']'))

You blindly assume port_string is not NULL here.

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 402

Quoted 2 lines
grub-core/net/http.c +2 -0
+        {
+          port_string = 0;

port_string = NULL;

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 403

Quoted 1 line
grub-core/net/http.c +1 -0
+        }

You do not need these curly brackets.

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 405

Quoted 2 lines
grub-core/net/http.c +2 -0
+    }
+  if (port_string)

if (port_string != NULL)

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 407

Quoted 2 lines
grub-core/net/http.c +2 -0
+    {
+      port_number = grub_strtol(port_string + 1, 0, 10);

Please use grub_strtoul() instead of grub_strtol() and handle all errors properly. The strtoul man page is your friend.

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 411

Quoted 4 lines
grub-core/net/http.c +4 -0
+      if (port_number <= 0 || port_number > 65535)
+        {
+          return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("invalid port number `%s'"), port_string + 1);
+        }

Please drop these curly brackets.

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 413

Quoted 1 line
grub-core/net/http.c +1 -0
+      server_name = grub_strdup(file->device->net->server);

grub_strdup() may return NULL...

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 414

Quoted 1 line
grub-core/net/http.c +1 -0
+      server_name[port_string - file->device->net->server] = 0;

s/0/'\0'/

Quoted 6 lines
grub-core/net/http.c +6 -0
+    }
+  else
+    {
+      port_number = HTTP_PORT;
+      server_name = file->device->net->server;
+    }

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 430

Quoted 10 lines
grub-core/net/http.c +10 -0
+
+  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);
+    }

Please drop these curly brackets.

Daniel

#2 lore raw

Hi Daniel,

Thank you for the review. This is my first submission to the project, and I apologize for my lack of familiarity with the conventions and guidelines.

I reworked the patch with your suggested changes, and I also included an attempt at some changes for the documentation.

I am not completely sure how to submit a revised patch to the mailing list and still maintain the email thread, so I am appending the new patch to this message.

Thanks again,
- Steve

Quoted 96 lines · Daniel Kiper
Quoted 19 lines · Stephen Balousek

Allow the use of HTTP servers listening on ports other 80. This is done with an extension to the http notation of either:

   (http[,server[,port]])

- or -

   (http[,server[:port]])
---
  grub-core/net/http.c | 43 +++++++++++++++++++++++++++++++++++++++----
  1 file changed, 39 insertions(+), 4 deletions(-)
grub-core/net/http.c +0 -0
index b616cf40b..87e496e7f 100644

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 317

Quoted 6 lines
grub-core/net/http.c +3 -0
@@ -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;
+  long port_number;

s/long/unsigned long/g

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 396

Quoted 11 lines
grub-core/net/http.c +1 -4
    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,
-				  http_err, NULL,
-				  file);
+  port_string = grub_strrchr(file->device->net->server, ',');

Missing space between grub_strrchr and "(". Same thing should be fixed in the functions calls below.

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 397

Quoted 1 line
grub-core/net/http.c +1 -0
+  if (!port_string)

if (port_string == NULL)

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 400

Quoted 3 lines
grub-core/net/http.c +3 -0
+    {
+      port_string = grub_strrchr(file->device->net->server, ':');
+      if (grub_strchr(port_string + 1, ']'))

You blindly assume port_string is not NULL here.

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 402

Quoted 2 lines
grub-core/net/http.c +2 -0
+        {
+          port_string = 0;

port_string = NULL;

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 403

Quoted 1 line
grub-core/net/http.c +1 -0
+        }

You do not need these curly brackets.

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 405

Quoted 2 lines
grub-core/net/http.c +2 -0
+    }
+  if (port_string)

if (port_string != NULL)

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 407

Quoted 2 lines
grub-core/net/http.c +2 -0
+    {
+      port_number = grub_strtol(port_string + 1, 0, 10);

Please use grub_strtoul() instead of grub_strtol() and handle all errors properly. The strtoul man page is your friend.

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 411

Quoted 4 lines
grub-core/net/http.c +4 -0
+      if (port_number <= 0 || port_number > 65535)
+        {
+          return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("invalid port number `%s'"), port_string + 1);
+        }

Please drop these curly brackets.

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 412

Quoted 1 line
grub-core/net/http.c +1 -0
+      server_name = grub_strdup(file->device->net->server);

grub_strdup() may return NULL...

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 413

Quoted 1 line
grub-core/net/http.c +1 -0
+      server_name[port_string - file->device->net->server] = 0;

s/0/'\0'/

Quoted 6 lines
grub-core/net/http.c +6 -0
+    }
+  else
+    {
+      port_number = HTTP_PORT;
+      server_name = file->device->net->server;
+    }

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 429

Quoted 10 lines
grub-core/net/http.c +10 -0
+
+  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);
+    }

Please drop these curly brackets.

Quoted 1 line

Daniel

From 3efc9a7f00a3e8f96609ee95262e87e61d42ce44 Mon Sep 17 00:00:00 2001
From: Stephen Balousek <[email protected]>
Date: Sun, 10 Oct 2021 15:12:20 -0700
Subject: [PATCH] http: Allow use of non-standard TCP/IP ports
To: [email protected]

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]])

---
 docs/grub.texi       | 12 ++++++++++++ grub-core/net/http.c | 37 +++++++++++++++++++++++++++++++++++--

 2 files changed, 47 insertions(+), 2 deletions(-)
docs/grub.texi +0 -0
index 99d0a0149..dbafe80d2 100644
@@ -3011,6 +3011,18 @@ 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{:}:
+
+@itemize @bullet
+@item
+@code{(http,@var{server},@var{port})}
+
+@item
+@code{(http,@var{server}:@var{port})}
+@end itemize
+
 If you boot GRUB from a CD-ROM, @samp{(cd)} is available. @xref{Making a GRUB bootable CD-ROM}, for details.

grub-core/net/http.c +0 -0
index b616cf40b..eb07e333a 100644
@@ -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,40 @@ 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)
+    {
+      port_string = grub_strrchr (file->device->net->server, ':');
+      if (port_string != NULL && grub_strchr (port_string + 1, ']') != NULL)
+      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);

Signature · 1 line
2.33.0
#3 lore raw

Hi Stephen,

Quoted 4 lines · Stephen Balousek

Hi Daniel,

Thank you for the review. This is my first submission to the project, and I apologize for my lack of familiarity with the conventions and guidelines.

No worries!

Quoted 2 lines

I reworked the patch with your suggested changes, and I also included an attempt at some changes for the documentation.

Thanks!

Quoted 3 lines

I am not completely sure how to submit a revised patch to the mailing list and still maintain the email thread, so I am appending the new patch to this message.

Next time please create a new thread and mark the patch as v2, v3, etc. respectively. You can use "git format-patch" and "git send-email" to do it.

[...]

Quoted 62 lines

From 3efc9a7f00a3e8f96609ee95262e87e61d42ce44 Mon Sep 17 00:00:00 2001
From: Stephen Balousek <[email protected]>
Date: Sun, 10 Oct 2021 15:12:20 -0700
Subject: [PATCH] http: Allow use of non-standard TCP/IP ports
To: [email protected]

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]])

---
 docs/grub.texi       | 12 ++++++++++++ grub-core/net/http.c | 37 +++++++++++++++++++++++++++++++++++--

 2 files changed, 47 insertions(+), 2 deletions(-)
docs/grub.texi +0 -0
index 99d0a0149..dbafe80d2 100644
@@ -3011,6 +3011,18 @@ environment variable @samp{net_default_server} is

used.
 Before using the network drive, you must initialize the network. @xref{Network}, for more information.

docs/grub.texi +12 -0
+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{:}:
+
+@itemize @bullet
+@item
+@code{(http,@var{server},@var{port})}
+
+@item
+@code{(http,@var{server}:@var{port})}
+@end itemize
+

 If you boot GRUB from a CD-ROM, @samp{(cd)} is available. @xref{Making a GRUB bootable CD-ROM}, for details.

grub-core/net/http.c +3 -0
index b616cf40b..eb07e333a 100644
@@ -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

grub-core/net/http.c +0 -0
@@ -390,10 +393,40 @@ http_establish (struct grub_file *file, grub_off_t
 offset, int initial)

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t · line 401

Quoted 10 lines
grub-core/net/http.c +5 -2
    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)
+    {
+      port_string = grub_strrchr (file->device->net->server, ':');
+      if (port_string != NULL && grub_strchr (port_string + 1, ']') !=

I am not sure why you are looking for ']' here. Could you enlighten me?

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t · line 408

Quoted 7 lines
grub-core/net/http.c +6 -0
 NULL)
+      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)

I think it should be "port_number == GRUB_ULONG_MAX" instead of "port_number == 0" here.

Quoted 6 lines
grub-core/net/http.c +4 -0
+      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);
+

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t · line 424

Quoted 10 lines
grub-core/net/http.c +10 -0
+      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;
+    }

Otherwise patch looks much better.

Thanks,

Daniel

#4 lore raw

Hi Daniel,

Thank you for another detailed response. Please let me know how that section related to IPv6 should be commented, and I will submit an updated patch.

- Steve

Quoted 86 lines · Daniel Kiper

Hi Stephen,

Quoted 4 lines · Stephen Balousek

Hi Daniel,

Thank you for the review. This is my first submission to the project, and I apologize for my lack of familiarity with the conventions and guidelines.

No worries!

Quoted 2 lines

I reworked the patch with your suggested changes, and I also included an attempt at some changes for the documentation.

Thanks!

Quoted 3 lines

I am not completely sure how to submit a revised patch to the mailing list and still maintain the email thread, so I am appending the new patch to this message.

Next time please create a new thread and mark the patch as v2, v3, etc. respectively. You can use "git format-patch" and "git send-email" to do it.

[...]

Quoted 65 lines

From 3efc9a7f00a3e8f96609ee95262e87e61d42ce44 Mon Sep 17 00:00:00 2001
From: Stephen Balousek <[email protected]>
Date: Sun, 10 Oct 2021 15:12:20 -0700
Subject: [PATCH] http: Allow use of non-standard TCP/IP ports
To: [email protected]

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]])

---
  docs/grub.texi       | 12 ++++++++++++
  grub-core/net/http.c | 37 +++++++++++++++++++++++++++++++++++--
  2 files changed, 47 insertions(+), 2 deletions(-)
docs/grub.texi +0 -0
index 99d0a0149..dbafe80d2 100644
@@ -3011,6 +3011,18 @@ environment variable @samp{net_default_server} is

used.
 Before using the network drive, you must initialize the network. @xref{Network}, for more information.

docs/grub.texi +12 -0
+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{:}:
+
+@itemize @bullet
+@item
+@code{(http,@var{server},@var{port})}
+
+@item
+@code{(http,@var{server}:@var{port})}
+@end itemize
+
  If you boot GRUB from a CD-ROM, @samp{(cd)} is available. @xref{Making
  a GRUB bootable CD-ROM}, for details.
grub-core/net/http.c +3 -0
index b616cf40b..eb07e333a 100644
@@ -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

grub-core/net/http.c +0 -0
@@ -390,10 +393,40 @@ http_establish (struct grub_file *file, grub_off_t
 offset, int initial)
    grub_netbuff_put (nb, 2);
    grub_memcpy (ptr, "\r\n", 2);
 
Quoted 8 lines

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t · line 401

Quoted 7 lines
grub-core/net/http.c +5 -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)
+    {
+      port_string = grub_strrchr (file->device->net->server, ':');
+      if (port_string != NULL && grub_strchr (port_string + 1, ']') !=

I am not sure why you are looking for ']' here. Could you enlighten me?

IPv6. The idea is to capture the port number '8080' from '[::1]:8080' but not '1' from '[::1]'. I can add a comment here, since I agree the intent is not clear.

Quoted 10 lines

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t · line 408

Quoted 8 lines
grub-core/net/http.c +6 -0
 NULL)
+      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)

I think it should be "port_number == GRUB_ULONG_MAX" instead of "port_number == 0" here.

Right. I don't know what I was thinking here.

Quoted 17 lines
Quoted 7 lines
grub-core/net/http.c +4 -0
+      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);
+

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t · line 424

Quoted 10 lines
grub-core/net/http.c +10 -0
+      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;
+    }
Quoted 1 line

Otherwise patch looks much better.

Thank you.

Quoted 4 lines

Thanks,

Daniel

Signature · 1 line
- Steve
#5 lore raw
Quoted 3 lines · Stephen Balousek

Hi Daniel,

Thank you for another detailed response. Please let me know how that section

You are welcome!

Quoted 100 lines

related to IPv6 should be commented, and I will submit an updated patch.

- Steve

Quoted 86 lines · Daniel Kiper

Hi Stephen,

Quoted 4 lines · Stephen Balousek

Hi Daniel,

Thank you for the review. This is my first submission to the project, and I apologize for my lack of familiarity with the conventions and guidelines.

No worries!

Quoted 2 lines

I reworked the patch with your suggested changes, and I also included an attempt at some changes for the documentation.

Thanks!

Quoted 3 lines

I am not completely sure how to submit a revised patch to the mailing list and still maintain the email thread, so I am appending the new patch to this message.

Next time please create a new thread and mark the patch as v2, v3, etc. respectively. You can use "git format-patch" and "git send-email" to do it.

[...]

Quoted 65 lines

From 3efc9a7f00a3e8f96609ee95262e87e61d42ce44 Mon Sep 17 00:00:00 2001
From: Stephen Balousek <[email protected]>
Date: Sun, 10 Oct 2021 15:12:20 -0700
Subject: [PATCH] http: Allow use of non-standard TCP/IP ports
To: [email protected]

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]])

---
  docs/grub.texi       | 12 ++++++++++++
  grub-core/net/http.c | 37 +++++++++++++++++++++++++++++++++++--
  2 files changed, 47 insertions(+), 2 deletions(-)
docs/grub.texi +0 -0
index 99d0a0149..dbafe80d2 100644
@@ -3011,6 +3011,18 @@ environment variable @samp{net_default_server} is

used.
 Before using the network drive, you must initialize the network. @xref{Network}, for more information.

docs/grub.texi +12 -0
+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{:}:
+
+@itemize @bullet
+@item
+@code{(http,@var{server},@var{port})}
+
+@item
+@code{(http,@var{server}:@var{port})}
+@end itemize
+
  If you boot GRUB from a CD-ROM, @samp{(cd)} is available. @xref{Making
  a GRUB bootable CD-ROM}, for details.
grub-core/net/http.c +3 -0
index b616cf40b..eb07e333a 100644
@@ -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

grub-core/net/http.c +0 -0
@@ -390,10 +393,40 @@ http_establish (struct grub_file *file, grub_off_t
 offset, int initial)
    grub_netbuff_put (nb, 2);
    grub_memcpy (ptr, "\r\n", 2);
 
Quoted 8 lines

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t · line 401

Quoted 7 lines
grub-core/net/http.c +5 -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)
+    {
+      port_string = grub_strrchr (file->device->net->server, ':');
+      if (port_string != NULL && grub_strchr (port_string + 1, ']') !=

I am not sure why you are looking for ']' here. Could you enlighten me?

Quoted 3 lines

IPv6. The idea is to capture the port number '8080' from '[::1]:8080' but not '1' from '[::1]'. I can add a comment here, since I agree the intent is not clear.

Ah, I totally forgot about IPv6. Please add a comment here. Additionally, I think it is worth adding information about IPv6 format to the docs/grub.texi, e.g., as an example of IPv4, IPv6 and normal (DNS) name formatting.

Daniel

#6 lore raw

PATCH v2 1/1 http: Allow use of non-standard TCP/IP ports

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]])

---

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(-)
docs/grub.texi +33 -0
index f8b4b3b21..a95d86f95 100644
@@ -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.
 
grub-core/net/http.c +37 -2
index b616cf40b..f457cd010 100644
@@ -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);
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v2] http: Allow use of non-standard TCP/IP ports

Quoted 5 lines · Stephen Balousek

Allow the use of HTTP servers listening on ports other 80. This is done with an extension to the http notation:

(http[,server[,port]])

Quoted 8 lines

- or -

(http[,server[:port]])

---

Hi Daniel,

Hi Stephen,

Quoted 1 line

Happy New Year!

Happy New Year for you too!

Quoted 1 line

I apologize for the long delay. I really wanted to test the changes

No worries. I know how it works...

Quoted 8 lines

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.

Quoted 8 lines

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.

Yeah, only if there are no digits in the string... Please look below...

Quoted 76 lines

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(-)
docs/grub.texi +33 -0
index f8b4b3b21..a95d86f95 100644
@@ -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.
 
grub-core/net/http.c +7 -2
index b616cf40b..f457cd010 100644
@@ -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' */

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 409

Quoted 10 lines
grub-core/net/http.c +10 -0
+      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);

This check is not reliable. If you want to rely on grub_errno you should reset grub_errno to GRUB_ERR_NONE before grub_strtoul() call. However, this still does not allow you to detect partially invalid strings. So, I think you should do this:

port_number = grub_strtoul (port_string + 1, &endptr, 10);
if (*(port_string + 1) == '\0' || endptr != '\0')
return grub_error (GRUB_ERR_BAD_NUMBER, N_("non-numeric or invalid port number `%s'"), port_string + 1);

Otherwise the patch LGTM.

Daniel