[PATCH 2/2] discover-server: Download and parse config files specfied by user
Samuel Mendoza-Jonas
sam.mj at au1.ibm.com
Thu Jul 17 09:58:31 EST 2014
Allow arbritrary conf files to be passed to pxe-parser. This also
enables the pb-event conf at dev option for URLs.
Signed-off-by: Samuel Mendoza-Jonas <sam.mj at au1.ibm.com>
---
discover/device-handler.c | 150 ++++++++++++++++++++++++++++++++++++++++++---
discover/device-handler.h | 4 +-
discover/discover-server.c | 7 +++
discover/user-event.c | 13 +---
4 files changed, 154 insertions(+), 20 deletions(-)
diff --git a/discover/device-handler.c b/discover/device-handler.c
index 1a8649e..8ea4154 100644
--- a/discover/device-handler.c
+++ b/discover/device-handler.c
@@ -17,6 +17,11 @@
#include <process/process.h>
#include <url/url.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
#include "device-handler.h"
#include "discover-server.h"
#include "user-event.h"
@@ -723,21 +728,22 @@ int device_handler_dhcp(struct device_handler *handler,
/* incoming conf event */
int device_handler_conf(struct device_handler *handler,
- struct discover_device *dev, struct pb_url *url)
+ struct discover_device *dev, struct pb_url *url, struct event *event)
{
- struct discover_context *ctx;
+ struct discover_context *ctx;
- /* create our context */
- ctx = device_handler_discover_context_create(handler, dev);
- ctx->conf_url = url;
+ /* create our context */
+ ctx = device_handler_discover_context_create(handler, dev);
+ ctx->conf_url = url;
+ ctx->event = event;
- iterate_parsers(ctx);
+ iterate_parsers(ctx);
- device_handler_discover_context_commit(handler, ctx);
+ device_handler_discover_context_commit(handler, ctx);
- talloc_free(ctx);
+ talloc_free(ctx);
- return 0;
+ return 0;
}
static struct discover_boot_option *find_boot_option_by_id(
@@ -812,6 +818,132 @@ void device_handler_update_config(struct device_handler *handler,
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) {
+ /* ip has complained for some reason; most likely
+ * there is no route to the host - bail out */
+ pb_debug("%s: No route to %s\n",__func__,url->host);
+ 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)
+{
+ struct boot_status *status = talloc(handler,struct boot_status);
+ struct discover_device *dev;
+ struct pb_url *pb_url;
+ struct event *event;
+ struct param *param;
+
+ status->type = BOOT_STATUS_ERROR;
+ status->progress = 0;
+ status->detail = talloc_asprintf(handler,"Received config URL %s",url);
+
+ /* Catch a case where pb-event can send an empty string,
+ * which would otherwise trigger an assert in pb_url_parse
+ */
+ if (!strlen(url)) {
+ status->message = talloc_asprintf(handler,
+ "Missing URL!");
+ goto msg;
+ }
+
+ event = talloc(handler, struct event);
+ event->type = EVENT_TYPE_USER;
+ event->action = EVENT_ACTION_CONF;
+
+ 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->host) {
+ status->message = talloc_asprintf(handler,
+ "Invalid config URL!");
+ goto msg;
+ }
+
+ event->device = device_from_addr(event, pb_url);
+ if (!event->device) {
+ status->message = talloc_asprintf(handler,
+ "Unable to route to host %s\n",
+ pb_url->host);
+ goto msg;
+ }
+
+ dev = discover_device_create(handler, event->device);
+
+ device_handler_conf(handler, dev, pb_url, event);
+ status->type = BOOT_STATUS_INFO;
+ status->message = talloc_asprintf(handler,"Config file %s parsed",
+ pb_url->file);
+msg:
+ boot_status(handler, status);
+}
+
#ifndef PETITBOOT_TEST
static int device_handler_init_sources(struct device_handler *handler)
diff --git a/discover/device-handler.h b/discover/device-handler.h
index 246abf9..f157c0f 100644
--- a/discover/device-handler.h
+++ b/discover/device-handler.h
@@ -76,7 +76,7 @@ int device_handler_discover(struct device_handler *handler,
int device_handler_dhcp(struct device_handler *handler,
struct discover_device *dev, struct event *event);
int device_handler_conf(struct device_handler *handler,
- struct discover_device *dev, struct pb_url *url);
+ struct discover_device *dev, struct pb_url *url, struct event *event);
void device_handler_remove(struct device_handler *handler,
struct discover_device *device);
@@ -117,6 +117,8 @@ void device_handler_boot(struct device_handler *handler,
void device_handler_cancel_default(struct device_handler *handler);
void device_handler_update_config(struct device_handler *handler,
struct config *config);
+void device_handler_process_url(struct device_handler *handler,
+ const char *url);
void device_handler_reinit(struct device_handler *handler);
int device_request_write(struct discover_device *dev, bool *release);
diff --git a/discover/discover-server.c b/discover/discover-server.c
index 80919fd..f14b9f0 100644
--- a/discover/discover-server.c
+++ b/discover/discover-server.c
@@ -216,6 +216,7 @@ static int discover_server_process_message(void *arg)
struct boot_command *boot_command;
struct client *client = arg;
struct config *config;
+ char *url;
int rc;
message = pb_protocol_read_message(client, client->fd);
@@ -262,6 +263,12 @@ static int discover_server_process_message(void *arg)
config);
break;
+ case PB_PROTOCOL_ACTION_RETRIEVE:
+ url = pb_protocol_deserialise_string((void *) client, message);
+
+ device_handler_process_url(client->server->device_handler, url);
+ break;
+
default:
pb_log("%s: invalid action %d\n", __func__, message->action);
return 0;
diff --git a/discover/user-event.c b/discover/user-event.c
index 1f70045..bf5d026 100644
--- a/discover/user-event.c
+++ b/discover/user-event.c
@@ -54,6 +54,8 @@ static const char *event_action_name(enum event_action action)
return "add";
case EVENT_ACTION_REMOVE:
return "remove";
+ case EVENT_ACTION_CONF:
+ return "conf";
case EVENT_ACTION_DHCP:
return "dhcp";
default:
@@ -379,22 +381,13 @@ static int user_event_dhcp(struct user_event *uev, struct event *event)
static int user_event_conf(struct user_event *uev, struct event *event)
{
struct device_handler *handler = uev->handler;
- struct discover_device *dev;
- struct pb_url *url;
const char *val;
val = event_get_param(event, "url");
if (!val)
return 0;
- url = pb_url_parse(event, val);
- if (!url)
- return 0;
-
- dev = discover_device_create(handler, event->device);
-
- device_handler_conf(handler, dev, url);
-
+ device_handler_process_url(handler, val);
return 0;
}
--
1.9.3
More information about the Petitboot
mailing list