add work in progress code for enumerating shell protocol handlers
authorFelix Fietkau <nbd@openwrt.org>
Fri, 9 Sep 2011 19:30:59 +0000 (21:30 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Fri, 9 Sep 2011 19:30:59 +0000 (21:30 +0200)
CMakeLists.txt
proto-shell.c [new file with mode: 0644]
proto/netifd-proto.sh [new file with mode: 0755]
proto/ppp.sh [new file with mode: 0755]

index c06733e6aa074e79161c6138b2cdfbba17eac23b..43ced00261c65ef3f7ca8270a199481754b27db6 100644 (file)
@@ -5,9 +5,20 @@ ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -g3)
 
 SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
 
+IF(APPLE)
+  INCLUDE_DIRECTORIES(/opt/local/include)
+  LINK_DIRECTORIES(/opt/local/lib)
+ENDIF()
+
 IF(DEBUG)
   ADD_DEFINITIONS(-DDEBUG -O0)
 ENDIF()
 
-ADD_EXECUTABLE(netifd main.c utils.c interface.c interface-ip.c proto.c proto-static.c config.c device.c bridge.c vlan.c ubus.c system-dummy.c)
-TARGET_LINK_LIBRARIES(netifd ubox ubus uci)
+
+ADD_EXECUTABLE(netifd
+       main.c utils.c interface.c interface-ip.c
+       proto.c proto-static.c proto-shell.c
+       config.c device.c bridge.c vlan.c ubus.c
+       system-dummy.c)
+
+TARGET_LINK_LIBRARIES(netifd ubox ubus uci json)
diff --git a/proto-shell.c b/proto-shell.c
new file mode 100644 (file)
index 0000000..2b68c35
--- /dev/null
@@ -0,0 +1,118 @@
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <glob.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <libubox/blobmsg_json.h>
+
+#include "netifd.h"
+#include "interface.h"
+#include "interface-ip.h"
+#include "proto.h"
+
+static LIST_HEAD(handlers);
+static int proto_fd, main_fd;
+
+struct proto_shell_handler {
+       struct list_head list;
+       struct proto_handler proto;
+};
+
+#define DUMP_PREFIX    "./"
+#define DUMP_SUFFIX    " dump"
+
+static void proto_shell_add_handler(const char *script, struct json_object *obj)
+{
+       if (json_object_get_type(obj) != json_type_object)
+               return;
+
+       fprintf(stderr, "Add handler for script %s: %s\n", script, json_object_to_json_string(obj));
+}
+
+static void proto_shell_add_script(const char *name)
+{
+       struct json_tokener *tok = NULL;
+       struct json_object *obj;
+       static char buf[512];
+       char *start, *end, *cmd;
+       FILE *f;
+       int buflen, len;
+
+       cmd = alloca(strlen(name) + 1 + sizeof(DUMP_PREFIX) + sizeof(DUMP_SUFFIX));
+       sprintf(cmd, DUMP_PREFIX "%s" DUMP_SUFFIX, name);
+
+       f = popen(cmd, "r");
+       if (!f)
+               return;
+
+       do {
+               buflen = fread(buf, 1, sizeof(buf) - 1, f);
+               if (buflen <= 0)
+                       continue;
+
+               start = buf;
+               len = buflen;
+               do {
+                       end = memchr(start, '\n', len);
+                       if (end)
+                               len = end - start;
+
+                       if (!tok)
+                               tok = json_tokener_new();
+
+                       obj = json_tokener_parse_ex(tok, start, len);
+                       if (!is_error(obj)) {
+                               proto_shell_add_handler(name, obj);
+                               json_object_put(obj);
+                               json_tokener_free(tok);
+                               tok = NULL;
+                       }
+
+                       if (end) {
+                               start = end + 1;
+                               len = buflen - (start - buf);
+                       }
+               } while (len > 0);
+       } while (!feof(f) && !ferror(f));
+
+       if (tok)
+               json_tokener_free(tok);
+
+       pclose(f);
+}
+
+void __init proto_shell_init(void)
+{
+       glob_t g;
+       int i;
+
+       main_fd = open(".", O_RDONLY | O_DIRECTORY);
+       if (main_fd < 0)
+               return;
+
+       if (chdir(main_path)) {
+               perror("chdir(main path)");
+               goto close_cur;
+       }
+
+       if (chdir("./proto"))
+               goto close_cur;
+
+       proto_fd = open(".", O_RDONLY | O_DIRECTORY);
+       if (proto_fd < 0)
+               goto close_cur;
+
+       glob("*.sh", 0, NULL, &g);
+       for (i = 0; i < g.gl_pathc; i++)
+               proto_shell_add_script(g.gl_pathv[i]);
+
+       if (list_empty(&handlers))
+               close(proto_fd);
+
+close_cur:
+       fchdir(main_fd);
+       if (list_empty(&handlers))
+               close(main_fd);
+}
diff --git a/proto/netifd-proto.sh b/proto/netifd-proto.sh
new file mode 100755 (executable)
index 0000000..f850216
--- /dev/null
@@ -0,0 +1,44 @@
+. /usr/share/libubox/jshn.sh
+
+proto_config_add_int() {
+       json_add_int "$1" 5
+}
+
+proto_config_add_string() {
+       json_add_int "$1" 3
+}
+
+proto_config_add_boolean() {
+       json_add_int "$1" 7
+}
+
+add_default_handler() {
+       case "$(type $1 2>/dev/null)" in
+               *function*) return;;
+               *) eval "$1() { return; }"
+       esac
+}
+
+case "$1" in
+       dump)
+               add_protocol() {
+                       immediate=0
+
+                       add_default_handler "$1_init_config"
+
+                       json_init
+                       json_add_string "name" "$1"
+                       eval "$1_init"
+                       json_add_boolean immediate "$immediate"
+                       json_add_object "config"
+                       eval "$1_init_config"
+                       json_close_object
+                       json_dump
+               }
+       ;;
+       *)
+               add_protocol() {
+                       return;
+               }
+       ;;
+esac
diff --git a/proto/ppp.sh b/proto/ppp.sh
new file mode 100755 (executable)
index 0000000..76df2f3
--- /dev/null
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+. netifd-proto.sh
+
+ppp_init_config() {
+       proto_config_add_string "username"
+       proto_config_add_string "password"
+       proto_config_add_int "keepalive"
+}
+
+ppp_init() {
+       return
+}
+
+add_protocol ppp
+
+pppoe_init_config() {
+       ppp_init_config
+}
+
+pppoe_init() {
+       return
+}
+
+add_protocol pppoe