+ device_handler_update_lang(config->lang);
+ device_handler_reinit(handler);
+}
+
+static char *device_from_addr(void *ctx, struct pb_url *url)
+{
+ char *ipaddr, *buf, *tok, *dev = NULL;
+ const char *delim = " ";
+ struct sockaddr_in *ip;
+ struct sockaddr_in si;
+ struct addrinfo *res;
+ struct process *p;
+ int rc;
+
+ /* Note: IPv4 only */
+ rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
+ if (rc > 0) {
+ ipaddr = url->host;
+ } else {
+ /* need to turn hostname into a valid IP */
+ rc = getaddrinfo(url->host, NULL, NULL, &res);
+ if (rc) {
+ pb_debug("%s: Invalid URL\n",__func__);
+ return NULL;
+ }
+ ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
+ ip = (struct sockaddr_in *) res->ai_addr;
+ inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
+ freeaddrinfo(res);
+ }
+
+ const char *argv[] = {
+ pb_system_apps.ip,
+ "route", "show", "to", "match",
+ ipaddr,
+ NULL
+ };
+
+ p = process_create(ctx);
+
+ p->path = pb_system_apps.ip;
+ p->argv = argv;
+ p->keep_stdout = true;
+
+ rc = process_run_sync(p);
+
+ if (rc || p->exit_status) {
+ /* ip has complained for some reason; most likely
+ * there is no route to the host - bail out */
+ pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
+ pb_debug("ip buf: %s\n", p->stdout_buf);
+ process_release(p);
+ return NULL;
+ }
+
+ buf = p->stdout_buf;
+ /* If a route is found, ip-route output will be of the form
+ * "... dev DEVNAME ... " */
+ tok = strtok(buf, delim);
+ while (tok) {
+ if (!strcmp(tok, "dev")) {
+ tok = strtok(NULL, delim);
+ dev = talloc_strdup(ctx, tok);
+ break;
+ }
+ tok = strtok(NULL, delim);
+ }
+
+ process_release(p);
+ if (dev)
+ pb_debug("%s: Found interface '%s'\n", __func__,dev);
+ return dev;
+}
+
+void device_handler_process_url(struct device_handler *handler,
+ const char *url, const char *mac, const char *ip)
+{
+ struct discover_context *ctx;
+ struct discover_device *dev;
+ struct status *status;
+ struct pb_url *pb_url;
+ struct event *event;
+ struct param *param;
+
+ status = talloc(handler, struct status);
+ status->type = STATUS_ERROR;
+
+ if (!handler->network) {
+ status->message = talloc_asprintf(handler,
+ _("No network configured"));
+ goto msg;
+ }
+
+ event = talloc(handler, struct event);
+ event->type = EVENT_TYPE_USER;
+ event->action = EVENT_ACTION_URL;
+
+ if (url[strlen(url) - 1] == '/') {
+ event->params = talloc_array(event, struct param, 3);
+ param = &event->params[0];
+ param->name = talloc_strdup(event, "pxepathprefix");
+ param->value = talloc_strdup(event, url);
+ param = &event->params[1];
+ param->name = talloc_strdup(event, "mac");
+ param->value = talloc_strdup(event, mac);
+ param = &event->params[2];
+ param->name = talloc_strdup(event, "ip");
+ param->value = talloc_strdup(event, ip);
+ event->n_params = 3;
+ } else {
+ event->params = talloc_array(event, struct param, 1);
+ param = &event->params[0];
+ param->name = talloc_strdup(event, "pxeconffile");
+ param->value = talloc_strdup(event, url);
+ event->n_params = 1;
+ }
+
+ pb_url = pb_url_parse(event, event->params->value);
+ if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
+ status->message = talloc_asprintf(handler,
+ _("Invalid config URL!"));
+ goto msg;
+ }
+
+ if (pb_url->scheme == pb_url_file)
+ event->device = talloc_asprintf(event, "local");
+ else
+ event->device = device_from_addr(event, pb_url);
+
+ if (!event->device) {
+ status->message = talloc_asprintf(status,
+ _("Unable to route to host %s"),
+ pb_url->host);
+ goto msg;
+ }
+
+ dev = discover_device_create(handler, mac, event->device);
+ if (pb_url->scheme == pb_url_file)
+ dev->device->type = DEVICE_TYPE_ANY;
+ ctx = device_handler_discover_context_create(handler, dev);
+ talloc_steal(ctx, event);
+ ctx->event = event;
+
+ iterate_parsers(ctx);
+
+ device_handler_discover_context_commit(handler, ctx);
+
+ talloc_unlink(handler, ctx);
+
+ status->type = STATUS_INFO;
+ status->message = talloc_asprintf(status, _("Config file %s parsed"),
+ pb_url->file);
+msg:
+ device_handler_status(handler, status);
+ talloc_free(status);