Skip to contentSkip to conversation

Add a url parser

PATCH New Javier Martinez Canillas 1 participant 2 messages Oct 18, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Add a url parser

From: Peter Jones <[email protected]>

This patch adds a url parser that can parse http, https, tftp, and tftps urls, and is easily extensible to handle more types.

It's a little ugly in terms of the arguments it takes.

---

 grub-core/Makefile.core.def |   1 +
 grub-core/kern/misc.c       |  13 +
 grub-core/net/url.c         | 861 ++++++++++++++++++++++++++++++++++++
 include/grub/misc.h         |  45 ++
 include/grub/net/url.h      |  28 ++
 5 files changed, 948 insertions(+)
 create mode 100644 grub-core/net/url.c
 create mode 100644 include/grub/net/url.h
grub-core/Makefile.core.def +1 -0
index 26937041756..e21a701dea4 100644
@@ -2286,6 +2286,7 @@ module = {
   common = net/ethernet.c;
   common = net/arp.c;
   common = net/netbuff.c;
+  common = net/url.c;
 };
 
 module = {
grub-core/kern/misc.c +13 -0
index 3b633d51f4c..2025edef00f 100644
@@ -296,6 +296,19 @@ grub_strrchr (const char *s, int c)
   return p;
 }
 
+char *
+grub_strchrnul (const char *s, int c)
+{
+  do
+    {
+      if (*s == c)
+	break;
+    }
+  while (*s++);
+
+  return (char *) s;
+}
+
 int
 grub_strword (const char *haystack, const char *needle)
 {
grub-core/net/url.c new file +861 -0

861 lines not on this page · raw message

include/grub/misc.h +45 -0

57 lines not on this page · raw message

include/grub/net/url.h new file +28 -0
new file mode 100644
index 00000000000..a215fa27d0a
@@ -0,0 +1,28 @@
+/* url.h - prototypes for url parsing functions */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2016  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_URL_HEADER
+#define GRUB_URL_HEADER	1
+
+int
+EXPORT_FUNC(extract_url_info) (const char *urlbuf, grub_size_t buflen,
+			       char **scheme, char **userinfo,
+			       char **host, int *port, char **file);
+
+#endif /* GRUB_URL_HEADER */
Signature · 1 line
2.21.0
#1 lore raw

PATCH 2/2 efinet and bootp: add support for dhcpv6

From: Peter Jones <[email protected]>

---

 grub-core/net/bootp.c              | 174 +++++++++++++++++++++++++++++
 grub-core/net/drivers/efi/efinet.c |  54 +++++++--
 grub-core/net/net.c                |  72 ++++++++++++
 grub-core/net/tftp.c               |   4 +
 include/grub/efi/api.h             | 129 ++++++++++++++++++++-
 include/grub/net.h                 |  60 ++++++++++
 6 files changed, 479 insertions(+), 14 deletions(-)
grub-core/net/bootp.c +174 -0
index 04cfbb04504..d4f13ab1a43 100644
@@ -23,6 +23,7 @@
 #include <grub/net/ip.h>
 #include <grub/net/netbuff.h>
 #include <grub/net/udp.h>
+#include <grub/net/url.h>
 #include <grub/datetime.h>
 
 struct grub_dhcp_discover_options
@@ -866,6 +867,179 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
 
 static grub_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp;
 
+struct grub_net_network_level_interface *
+grub_net_configure_by_dhcpv6_ack (const char *name,
+				  struct grub_net_card *card,
+				  grub_net_interface_flags_t flags
+				    __attribute__((__unused__)),
+				  const grub_net_link_level_address_t *hwaddr,
+				  const struct grub_net_dhcpv6_packet *packet,
+				  int is_def, char **device, char **path)
+{
+  struct grub_net_network_level_interface *inter = NULL;
+  struct grub_net_network_level_address addr;
+  int mask = -1;
+
+  if (!device || !path)
+    return NULL;
+
+  *device = 0;
+  *path = 0;
+
+  grub_dprintf ("net", "mac address is %02x:%02x:%02x:%02x:%02x:%02x\n",
+		hwaddr->mac[0], hwaddr->mac[1], hwaddr->mac[2],
+		hwaddr->mac[3], hwaddr->mac[4], hwaddr->mac[5]);
+
+  if (is_def)
+    grub_net_default_server = 0;
+
+  if (is_def && !grub_net_default_server && packet)
+    {
+      const grub_uint8_t *options = packet->dhcp_options;
+      unsigned int option_max = 1024 - OFFSET_OF (dhcp_options, packet);
+      unsigned int i;
+
+      for (i = 0; i < option_max - sizeof (grub_net_dhcpv6_option_t); )
+	{
+	  grub_uint16_t num, len;
+	  grub_net_dhcpv6_option_t *opt =
+	    (grub_net_dhcpv6_option_t *)(options + i);
+
+	  num = grub_be_to_cpu16(opt->option_num);
+	  len = grub_be_to_cpu16(opt->option_len);
+
+	  grub_dprintf ("net", "got dhcpv6 option %d len %d\n", num, len);
+
+	  if (len == 0)
+	    break;
+
+	  if (len + i > 1024)
+	    break;
+
+	  if (num == GRUB_NET_DHCP6_BOOTFILE_URL)
+	    {
+	      char *scheme, *userinfo, *host, *file;
+	      char *tmp;
+	      int hostlen;
+	      int port;
+	      int rc = extract_url_info ((const char *)opt->option_data,
+					 (grub_size_t)len,
+					 &scheme, &userinfo, &host, &port,
+					 &file);
+	      if (rc < 0)
+		continue;
+
+	      /* right now this only handles tftp. */
+	      if (grub_strcmp("tftp", scheme))
+		{
+		  grub_free (scheme);
+		  grub_free (userinfo);
+		  grub_free (host);
+		  grub_free (file);
+		  continue;
+		}
+	      grub_free (userinfo);
+
+	      hostlen = grub_strlen (host);
+	      if (hostlen > 2 && host[0] == '[' && host[hostlen-1] == ']')
+		{
+		  tmp = host+1;
+		  host[hostlen-1] = '\0';
+		}
+	      else
+		tmp = host;
+
+	      *device = grub_xasprintf ("%s,%s", scheme, tmp);
+	      grub_free (scheme);
+	      grub_free (host);
+
+	      if (file && *file)
+		{
+		  tmp = grub_strrchr (file, '/');
+		  if (tmp)
+		    *(tmp+1) = '\0';
+		  else
+		    file[0] = '\0';
+		}
+	      else if (!file)
+		file = grub_strdup ("");
+
+	      if (file[0] == '/')
+		{
+		  *path = grub_strdup (file+1);
+		  grub_free (file);
+		}
+	      else
+		*path = file;
+	    }
+	  else if (num == GRUB_NET_DHCP6_IA_NA)
+	    {
+	      const grub_net_dhcpv6_option_t *ia_na_opt;
+	      const grub_net_dhcpv6_opt_ia_na_t *ia_na =
+		(const grub_net_dhcpv6_opt_ia_na_t *)opt;
+	      unsigned int left = len - OFFSET_OF (options, ia_na);
+	      unsigned int j;
+
+	      if ((grub_uint8_t *)ia_na + left >
+		  (grub_uint8_t *)options + option_max)
+		left -= ((grub_uint8_t *)ia_na + left)
+		        - ((grub_uint8_t *)options + option_max);
+
+	      if (len < OFFSET_OF (option_data, opt)
+			+ sizeof (grub_net_dhcpv6_option_t))
+		{
+		  grub_dprintf ("net",
+				"found dhcpv6 ia_na option with no address\n");
+		  continue;
+		}
+
+	      for (j = 0; left > sizeof (grub_net_dhcpv6_option_t); )
+		{
+		  ia_na_opt = (const grub_net_dhcpv6_option_t *)
+			       (ia_na->options + j);
+		  grub_uint16_t ia_na_opt_num, ia_na_opt_len;
+
+		  ia_na_opt_num = grub_be_to_cpu16 (ia_na_opt->option_num);
+		  ia_na_opt_len = grub_be_to_cpu16 (ia_na_opt->option_len);
+		  if (ia_na_opt_len == 0)
+		    break;
+		  if (j + ia_na_opt_len > left)
+		    break;
+		  if (ia_na_opt_num == GRUB_NET_DHCP6_IA_ADDRESS)
+		    {
+		      const grub_net_dhcpv6_opt_ia_address_t *ia_addr;
+
+		      ia_addr = (const grub_net_dhcpv6_opt_ia_address_t *)
+				 ia_na_opt;
+		      addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+		      grub_memcpy(addr.ipv6, ia_addr->ipv6_address,
+				  sizeof (ia_addr->ipv6_address));
+		      inter = grub_net_add_addr (name, card, &addr, hwaddr, 0);
+		    }
+
+		  j += ia_na_opt_len;
+		  left -= ia_na_opt_len;
+		}
+	    }
+
+	  i += len + 4;
+	}
+
+      grub_print_error ();
+    }
+
+  if (is_def)
+    {
+      grub_env_set ("net_default_interface", name);
+      grub_env_export ("net_default_interface");
+    }
+
+    if (inter)
+      grub_net_add_ipv6_local (inter, mask);
+    return inter;
+}
+
+
 void
 grub_bootp_init (void)
 {
grub-core/net/drivers/efi/efinet.c +46 -8

101 lines not on this page · raw message

grub-core/net/net.c +72 -0

78 lines not on this page · raw message

grub-core/net/tftp.c +4 -0
index 7d90bf66e76..1157524fc50 100644
@@ -379,19 +379,23 @@ tftp_open (struct grub_file *file, const char *filename)
       return grub_errno;
     }
 
+  grub_dprintf("tftp", "resolving address for %s\n", file->device->net->server);
   err = grub_net_resolve_address (file->device->net->server, &addr);
   if (err)
     {
+      grub_dprintf("tftp", "Address resolution failed: %d\n", err);
       destroy_pq (data);
       grub_free (data);
       return err;
     }
 
+  grub_dprintf("tftp", "opening connection\n");
   data->sock = grub_net_udp_open (addr,
 				  TFTP_SERVER_PORT, tftp_receive,
 				  file);
   if (!data->sock)
     {
+      grub_dprintf("tftp", "connection failed\n");
       destroy_pq (data);
       grub_free (data);
       return grub_errno;
include/grub/efi/api.h +123 -6

149 lines not on this page · raw message

include/grub/net.h +60 -0

72 lines not on this page · raw message

Signature · 1 line
2.21.0