Author: marek Date: 2010-04-07 13:15:28 +0200 (Wed, 07 Apr 2010) New Revision: 1629
Removed: branches/batctl-0.2.x/proc.c branches/batctl-0.2.x/proc.h Modified: branches/batctl-0.2.x/Makefile branches/batctl-0.2.x/functions.c branches/batctl-0.2.x/functions.h branches/batctl-0.2.x/main.c branches/batctl-0.2.x/sys.c branches/batctl-0.2.x/sys.h Log: batctl: adapt batctl to new sysfs interface handling
Signed-off-by: Marek Lindner lindner_marek@yahoo.de
Modified: branches/batctl-0.2.x/Makefile =================================================================== --- branches/batctl-0.2.x/Makefile 2010-04-07 11:15:23 UTC (rev 1628) +++ branches/batctl-0.2.x/Makefile 2010-04-07 11:15:28 UTC (rev 1629) @@ -39,8 +39,8 @@ EXTRA_MODULES_C := bisect.c EXTRA_MODULES_H := bisect.h
-SRC_C = main.c bat-hosts.c functions.c proc.c sys.c ping.c traceroute.c tcpdump.c list-batman.c hash.c vis.c $(EXTRA_MODULES_C) -SRC_H = main.h bat-hosts.h functions.h proc.h sys.h ping.h traceroute.h tcpdump.h list-batman.h hash.h allocate.h vis.h $(EXTRA_MODULES_H) +SRC_C = main.c bat-hosts.c functions.c sys.c ping.c traceroute.c tcpdump.c list-batman.c hash.c vis.c $(EXTRA_MODULES_C) +SRC_H = main.h bat-hosts.h functions.h sys.h ping.h traceroute.h tcpdump.h list-batman.h hash.h allocate.h vis.h $(EXTRA_MODULES_H) SRC_O = $(SRC_C:.c=.o)
PACKAGE_NAME = batctl
Modified: branches/batctl-0.2.x/functions.c =================================================================== --- branches/batctl-0.2.x/functions.c 2010-04-07 11:15:23 UTC (rev 1628) +++ branches/batctl-0.2.x/functions.c 2010-04-07 11:15:28 UTC (rev 1629) @@ -164,7 +164,8 @@ fp = fopen(full_path, "r");
if (!fp) { - printf("Error - can't open file '%s': %s\n", full_path, strerror(errno)); + if (!(read_opt & SILENCE_ERRORS)) + printf("Error - can't open file '%s': %s\n", full_path, strerror(errno)); goto out; }
Modified: branches/batctl-0.2.x/functions.h =================================================================== --- branches/batctl-0.2.x/functions.h 2010-04-07 11:15:23 UTC (rev 1628) +++ branches/batctl-0.2.x/functions.h 2010-04-07 11:15:28 UTC (rev 1629) @@ -48,4 +48,5 @@ LOG_MODE = 0x08, USE_READ_BUFF = 0x10, SEARCH_ARGS = 0x20, + SILENCE_ERRORS = 0x40, };
Modified: branches/batctl-0.2.x/main.c =================================================================== --- branches/batctl-0.2.x/main.c 2010-04-07 11:15:23 UTC (rev 1628) +++ branches/batctl-0.2.x/main.c 2010-04-07 11:15:28 UTC (rev 1629) @@ -29,7 +29,6 @@ #include <string.h>
#include "main.h" -#include "proc.h" #include "sys.h" #include "ping.h" #include "traceroute.h" @@ -41,7 +40,7 @@ void print_usage(void) { printf("Usage: batctl [options] commands \n"); printf("commands:\n"); - printf(" \tinterface|if [none|interface] \tdisplay or modify the interface settings\n"); + printf(" \tinterface|if [add|del iface(s)]\tdisplay or modify the interface settings\n"); printf(" \toriginators|o \tdisplay the originator table\n"); printf(" \tinterval|it [orig_interval] \tdisplay or modify the originator interval (in ms)\n"); printf(" \tloglevel|ll [level] \tdisplay or modify the log level\n");
Deleted: branches/batctl-0.2.x/proc.c =================================================================== --- branches/batctl-0.2.x/proc.c 2010-04-07 11:15:23 UTC (rev 1628) +++ branches/batctl-0.2.x/proc.c 2010-04-07 11:15:28 UTC (rev 1629) @@ -1,114 +0,0 @@ -/* - * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors: - * - * Marek Lindner lindner_marek@yahoo.de - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 2 of the GNU General Public - * License as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA - * - */ - - -#include <sys/time.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include "main.h" -#include "proc.h" -#include "functions.h" - -static void interface_usage(void) -{ - printf("Usage: batctl interface [options] [none|interface] \n"); - printf("options:\n"); - printf(" \t -h print this help\n"); -} - -int interface(int argc, char **argv) -{ - int i, res, optchar; - - while ((optchar = getopt(argc, argv, "h")) != -1) { - switch (optchar) { - case 'h': - interface_usage(); - return EXIT_SUCCESS; - default: - interface_usage(); - return EXIT_FAILURE; - } - } - - if (argc == 1) - return read_file(PROC_ROOT_PATH, PROC_INTERFACES, SINGLE_READ); - - for (i = 1; i < argc; i++) { - if (strcmp(argv[i], "none") == 0) - res = write_file(PROC_ROOT_PATH, PROC_INTERFACES, "", NULL); - else - res = write_file(PROC_ROOT_PATH, PROC_INTERFACES, argv[i], NULL); - - if (res != EXIT_SUCCESS) - return res; - } - - return EXIT_SUCCESS; -} - -int handle_table(int argc, char **argv, char *file_path, void table_usage(void)) -{ - int optchar, read_opt = USE_BAT_HOSTS; - - while ((optchar = getopt(argc, argv, "hnw")) != -1) { - switch (optchar) { - case 'h': - table_usage(); - return EXIT_SUCCESS; - case 'n': - read_opt &= ~USE_BAT_HOSTS; - break; - case 'w': - read_opt |= CLR_CONT_READ; - break; - default: - table_usage(); - return EXIT_FAILURE; - } - } - - return read_file(PROC_ROOT_PATH, file_path, read_opt); -} - -int handle_proc_setting(int argc, char **argv, char *file_path, void setting_usage(void)) -{ - int optchar; - - while ((optchar = getopt(argc, argv, "h")) != -1) { - switch (optchar) { - case 'h': - setting_usage(); - return EXIT_SUCCESS; - default: - setting_usage(); - return EXIT_FAILURE; - } - } - - if (argc == 1) - return read_file(PROC_ROOT_PATH, file_path, SINGLE_READ); - - return write_file(PROC_ROOT_PATH, file_path, argv[1], NULL); -}
Deleted: branches/batctl-0.2.x/proc.h =================================================================== --- branches/batctl-0.2.x/proc.h 2010-04-07 11:15:23 UTC (rev 1628) +++ branches/batctl-0.2.x/proc.h 2010-04-07 11:15:28 UTC (rev 1629) @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors: - * - * Marek Lindner lindner_marek@yahoo.de - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 2 of the GNU General Public - * License as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA - * - */ - - - -#define PROC_ROOT_PATH "/proc/net/batman-adv/" -#define PROC_INTERFACES "interfaces" - -int interface(int argc, char **argv); - -int handle_table(int argc, char **argv, char *file_path, void table_usage(void)); -int handle_proc_setting(int argc, char **argv, char *file_path, void setting_usage(void));
Modified: branches/batctl-0.2.x/sys.c =================================================================== --- branches/batctl-0.2.x/sys.c 2010-04-07 11:15:23 UTC (rev 1628) +++ branches/batctl-0.2.x/sys.c 2010-04-07 11:15:28 UTC (rev 1629) @@ -25,12 +25,135 @@ #include <stdlib.h> #include <string.h> #include <errno.h> +#include <sys/types.h> +#include <dirent.h>
#include "main.h" #include "sys.h" #include "functions.h"
+#define PATH_BUFF_LEN 200
+static void interface_usage(void) +{ + printf("Usage: batctl interface [options] [add|del iface(s)] \n"); + printf("options:\n"); + printf(" \t -h print this help\n"); +} + +static int print_interfaces(void) +{ + DIR *iface_base_dir; + struct dirent *iface_dir; + char *path_buff; + int res; + + path_buff = malloc(PATH_BUFF_LEN); + if (!path_buff) { + printf("Error - could not allocate path buffer: out of memory ?\n"); + goto err; + } + + iface_base_dir = opendir(SYS_IFACE_PATH); + if (!iface_base_dir) { + printf("Error - the directory '%s' could not be read: %s\n", + SYS_IFACE_PATH, strerror(errno)); + printf("Is the batman-adv module loaded and sysfs mounted ?\n"); + goto err_buff; + } + + while ((iface_dir = readdir(iface_base_dir)) != NULL) { + snprintf(path_buff, PATH_BUFF_LEN, SYS_MESH_IFACE_FMT, iface_dir->d_name); + res = read_file("", path_buff, SINGLE_READ | USE_READ_BUFF | SILENCE_ERRORS); + if (res != EXIT_SUCCESS) + continue; + + if (line_ptr[strlen(line_ptr) - 1] == '\n') + line_ptr[strlen(line_ptr) - 1] = '\0'; + + if (strcmp(line_ptr, "status: none") == 0) + goto free_line; + + free(line_ptr); + line_ptr = NULL; + + snprintf(path_buff, PATH_BUFF_LEN, SYS_IFACE_STATUS_FMT, iface_dir->d_name); + res = read_file("", path_buff, SINGLE_READ | USE_READ_BUFF | SILENCE_ERRORS); + if (res != EXIT_SUCCESS) { + printf("<error reading status>\n"); + continue; + } + + printf("%s: %s", iface_dir->d_name, line_ptr); + +free_line: + free(line_ptr); + line_ptr = NULL; + } + + free(path_buff); + closedir(iface_base_dir); + return EXIT_SUCCESS; + +err_buff: + free(path_buff); +err: + return EXIT_FAILURE; +} + +int interface(int argc, char **argv) +{ + char *path_buff; + int i, res, optchar; + + while ((optchar = getopt(argc, argv, "h")) != -1) { + switch (optchar) { + case 'h': + interface_usage(); + return EXIT_SUCCESS; + default: + interface_usage(); + return EXIT_FAILURE; + } + } + + if (argc == 1) + return print_interfaces(); + + if ((strcmp(argv[1], "add") != 0) && (strcmp(argv[1], "a") != 0) && + (strcmp(argv[1], "del") != 0) && (strcmp(argv[1], "d") != 0)) { + printf("Error - unknown argument specified: %s\n", argv[1]); + interface_usage(); + goto err; + } + + path_buff = malloc(PATH_BUFF_LEN); + if (!path_buff) { + printf("Error - could not allocate path buffer: out of memory ?\n"); + goto err; + } + + for (i = 2; i < argc; i++) { + snprintf(path_buff, PATH_BUFF_LEN, SYS_MESH_IFACE_FMT, argv[i]); + + if (argv[1][0] == 'a') + res = write_file("", path_buff, "bat0", NULL); + else + res = write_file("", path_buff, "none", NULL); + + if (res != EXIT_SUCCESS) + goto err_buff; + } + + free(path_buff); + return EXIT_SUCCESS; + +err_buff: + free(path_buff); +err: + return EXIT_FAILURE; +} + static void log_usage(void) { printf("Usage: batctl [options] log [logfile]\n");
Modified: branches/batctl-0.2.x/sys.h =================================================================== --- branches/batctl-0.2.x/sys.h 2010-04-07 11:15:23 UTC (rev 1628) +++ branches/batctl-0.2.x/sys.h 2010-04-07 11:15:28 UTC (rev 1629) @@ -31,6 +31,9 @@ #define SYS_VIS_MODE "vis_mode" #define SYS_VIS_DATA "vis_data" #define SYS_ORIG_INTERVAL "orig_interval" +#define SYS_IFACE_PATH "/sys/class/net" +#define SYS_MESH_IFACE_FMT SYS_IFACE_PATH"/%s/batman_adv/mesh_iface" +#define SYS_IFACE_STATUS_FMT SYS_IFACE_PATH"/%s/batman_adv/iface_status"
void originators_usage(void); void trans_local_usage(void); @@ -39,6 +42,7 @@ void vis_mode_usage(void); void orig_interval_usage(void); int log_print(int argc, char **argv); +int interface(int argc, char **argv); int handle_loglevel(int argc, char **argv); int handle_sys_table(int argc, char **argv, char *file_path, void table_usage(void)); int handle_sys_setting(int argc, char **argv, char *file_path, void setting_usage(void));