+}
+
+/**
+ * Return a new conf_context and increment the talloc reference count on
+ * the discover_context struct.
+ * @param ctx Parent talloc context
+ * @param orig Original discover_context
+ * @return Pointer to new conf_context
+ */
+static struct conf_context *copy_context(void *ctx, struct discover_context *dc)
+{
+ struct pxe_parser_info *info;
+ struct conf_context *conf;
+
+ conf = talloc_zero(ctx, struct conf_context);
+
+ if (!conf)
+ return NULL;
+
+ conf->get_pair = conf_get_pair_space;
+ conf->process_pair = pxe_process_pair;
+ conf->finish = pxe_finish;
+ info = talloc_zero(conf, struct pxe_parser_info);
+ if (!info) {
+ talloc_free(conf);
+ return NULL;
+ }
+ conf->parser_info = info;
+
+ /*
+ * The discover_context may be freed once pxe_parse() returns, but the
+ * callback will still need it. Take a reference so that that it will
+ * persist until the last callback completes.
+ */
+ conf->dc = talloc_reference(conf, dc);
+
+ return conf;
+}
+
+static int pxe_parse(struct discover_context *dc)
+{
+ struct pb_url *pxe_base_url, *file_url;
+ struct conf_context *conf = NULL;
+ struct load_url_result *result;
+ void *ctx = talloc_parent(dc);
+ struct pxe_parser_info *info;
+ char **pxe_conf_files;
+ bool complete_url;
+
+ /* Expects dhcp event parameters to support network boot */
+ if (!dc->event)
+ return -1;
+
+ dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
+ if (!dc->conf_url)
+ return -1;
+
+ /*
+ * Retrieving PXE configs over the network can take some time depending
+ * on factors such as slow network, malformed paths, bad DNS, and
+ * overzealous firewalls. Instead of blocking the discover server while
+ * we wait for these, spawn an asynchronous job that will attempt to
+ * retrieve each possible URL until it successfully finds one, and
+ * parse and process the resulting file in a callback.
+ */
+ conf = copy_context(ctx, dc);
+ if (!conf)
+ return -1;
+
+ if (complete_url) {
+ /* Check if this file has already been downloaded */
+ if (event_get_param(dc->event, "pxeconffile-local"))
+ file_url = pb_url_parse(dc, event_get_param(dc->event,
+ "pxeconffile-local"));
+ else
+ file_url = dc->conf_url;
+
+ if (!file_url) {
+ pb_log_fn("Failed to parse conf url!\n");
+ goto out_conf;
+ }
+
+ device_handler_status_dev_info(conf->dc->handler,
+ dc->device,
+ _("Requesting config %s"),
+ pb_url_to_string(conf->dc->conf_url));
+
+ /* we have a complete URL; use this and we're done. */
+ result = load_url_async(conf->dc, file_url,
+ pxe_conf_parse_cb, conf, NULL, ctx);
+ if (!result) {
+ pb_log("load_url_async fails for %s\n",
+ dc->conf_url->path);
+ goto out_conf;
+ }
+ } else {
+ pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
+ if (!pxe_conf_files)
+ goto out_conf;
+
+ pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
+ if (!pxe_base_url)
+ goto out_pxe_conf;
+
+ info = conf->parser_info;
+ info->pxe_conf_files = pxe_conf_files;
+ info->pxe_base_url = pxe_base_url;
+
+ device_handler_status_dev_info(conf->dc->handler,
+ conf->dc->device,
+ _("Probing from base %s"),
+ pb_url_to_string(pxe_base_url));
+
+ pxe_load_next_filename(conf);
+ }