+ /*
+ * 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, *url;
+ char **pxe_conf_files, **filename;
+ struct conf_context *conf = NULL;
+ struct load_url_result *result;
+ void *ctx = talloc_parent(dc);
+ 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 for each URL we can
+ * parse and process the resulting files in a callback. A separate
+ * conf_context is created for each job.
+ */
+ conf = copy_context(ctx, dc);
+ if (!conf)
+ return -1;
+
+ if (complete_url) {
+ /* we have a complete URL; use this and we're done. */
+ result = load_url_async(conf->dc, conf->dc->conf_url,
+ pxe_conf_parse_cb, conf);
+ 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;
+
+ for (filename = pxe_conf_files; *filename; filename++) {
+ if (!conf) {
+ conf = copy_context(ctx, dc);
+ }
+ url = pb_url_join(conf->dc, pxe_base_url, *filename);
+ if (!url)
+ continue;
+ result = load_url_async(conf, url, pxe_conf_parse_cb,
+ conf);
+ if (!result) {
+ pb_log("load_url_async fails for %s\n",
+ conf->dc->conf_url->path);
+ talloc_free(conf);
+ }
+ /* conf now needed by callback, don't reuse */
+ conf = NULL;
+ }
+
+ talloc_free(pxe_base_url);
+ talloc_free(pxe_conf_files);
+ }
+
+ return 0;