Changeset 1595

Show
Ignore:
Timestamp:
03/11/10 20:19:01 (6 months ago)
Author:
marek
Message:

batctl: follow /proc to sysfs conversion & introduce input validation

batman-adv is in the process of migrating to sysfs, hence batctl needs
to adapt the file paths. In addition batctl makes use of the input
validation support of the new files in sysfs.

Signed-off-by: Marek Lindner <lindner_marek@…>

Location:
trunk/batctl
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • trunk/batctl/functions.c

    r1585 r1595  
    3737#include "bat-hosts.h" 
    3838 
    39 #define BATMAN_ADV_TAG "batman-adv:" 
    40  
    4139static struct timeval start_time; 
    4240static char *host_name; 
     
    176174read: 
    177175        while ((read = getline(&line_ptr, &len, fp)) != -1) { 
     176                if (read_opt & SEARCH_ARGS) { 
     177                        /* omit log lines which don't start with the correct tag */ 
     178                        if (strncmp(line_ptr, SEARCH_ARGS_TAG, strlen(SEARCH_ARGS_TAG)) == 0) 
     179                                break; 
     180 
     181                        continue; 
     182                } 
    178183 
    179184                /* the buffer will be handled elsewhere */ 
  • trunk/batctl/functions.h

    r1511 r1595  
    2626 
    2727#define ETH_STR_LEN 17 
     28#define BATMAN_ADV_TAG "batman-adv:" 
     29#define SEARCH_ARGS_TAG "commands:" 
    2830 
    2931/* return time delta from start to end in milliseconds */ 
     
    4648        LOG_MODE = 0x08, 
    4749        USE_READ_BUFF = 0x10, 
     50        SEARCH_ARGS = 0x20, 
    4851}; 
  • trunk/batctl/main.c

    r1541 r1595  
    103103        } else if ((strcmp(argv[1], "originators") == 0) || (strcmp(argv[1], "o") == 0)) { 
    104104 
    105                 ret = handle_table(argc - 1, argv + 1, PROC_ORIGINATORS, originators_usage); 
     105                ret = handle_sys_table(argc - 1, argv + 1, SYS_ORIGINATORS, originators_usage); 
    106106 
    107107        } else if ((strcmp(argv[1], "translocal") == 0) || (strcmp(argv[1], "tl") == 0)) { 
    108108 
    109                 ret = handle_table(argc - 1, argv + 1, PROC_TRANSTABLE_LOCAL, trans_local_usage); 
     109                ret = handle_sys_table(argc - 1, argv + 1, SYS_TRANSTABLE_LOCAL, trans_local_usage); 
    110110 
    111111        } else if ((strcmp(argv[1], "transglobal") == 0) || (strcmp(argv[1], "tg") == 0)) { 
    112112 
    113                 ret = handle_table(argc - 1, argv + 1, PROC_TRANSTABLE_GLOBAL, trans_global_usage); 
     113                ret = handle_sys_table(argc - 1, argv + 1, SYS_TRANSTABLE_GLOBAL, trans_global_usage); 
    114114 
    115115        } else if ((strcmp(argv[1], "loglevel") == 0) || (strcmp(argv[1], "ll") == 0)) { 
     
    143143        } else if ((strcmp(argv[1], "aggregation") == 0) || (strcmp(argv[1], "ag") == 0)) { 
    144144 
    145                 ret = handle_proc_setting(argc - 1, argv + 1, PROC_AGGR, aggregation_usage); 
     145                ret = handle_sys_setting(argc - 1, argv + 1, SYS_AGGR, aggregation_usage); 
    146146 
    147147        } else if ((strcmp(argv[1], "bisect") == 0)) { 
  • trunk/batctl/proc.c

    r1560 r1595  
    6969} 
    7070 
    71 void originators_usage(void) 
    72 { 
    73         printf("Usage: batctl [options] originators \n"); 
    74         printf("options:\n"); 
    75         printf(" \t -h print this help\n"); 
    76         printf(" \t -n don't replace mac addresses with bat-host names\n"); 
    77         printf(" \t -w watch mode - refresh the originator table continuously\n"); 
    78 } 
    79  
    80 void trans_local_usage(void) 
    81 { 
    82         printf("Usage: batctl [options] translocal \n"); 
    83         printf("options:\n"); 
    84         printf(" \t -h print this help\n"); 
    85         printf(" \t -n don't replace mac addresses with bat-host names\n"); 
    86         printf(" \t -w watch mode - refresh the local translation table continuously\n"); 
    87 } 
    88  
    89 void trans_global_usage(void) 
    90 { 
    91         printf("Usage: batctl [options] transglobal \n"); 
    92         printf("options:\n"); 
    93         printf(" \t -h print this help\n"); 
    94         printf(" \t -n don't replace mac addresses with bat-host names\n"); 
    95         printf(" \t -w watch mode - refresh the global translation table continuously\n"); 
    96 } 
    97  
    9871void gw_srv_list_usage(void) 
    9972{ 
     
    11588{ 
    11689        printf("Usage: batctl [options] vis_server \n"); 
    117         printf("options:\n"); 
    118         printf(" \t -h print this help\n"); 
    119 } 
    120  
    121 void aggregation_usage(void) 
    122 { 
    123         printf("Usage: batctl [options] aggregation \n"); 
    12490        printf("options:\n"); 
    12591        printf(" \t -h print this help\n"); 
  • trunk/batctl/proc.h

    r1541 r1595  
    2222#define PROC_ROOT_PATH "/proc/net/batman-adv/" 
    2323#define PROC_INTERFACES "interfaces" 
    24 #define PROC_ORIGINATORS "originators" 
    2524#define PROC_ORIG_INTERVAL "orig_interval" 
    26 #define PROC_TRANSTABLE_LOCAL "transtable_local" 
    27 #define PROC_TRANSTABLE_GLOBAL "transtable_global" 
    2825#define PROC_VIS_SERVER "vis_server" 
    2926#define PROC_VIS_DATA "vis_data" 
    30 #define PROC_AGGR "aggregate_ogm" 
    3127#define PROC_GW_MODE "gateway_mode" 
    3228#define PROC_GW_SRV_LIST "gateway_srv_list" 
     
    3430int interface(int argc, char **argv); 
    3531 
    36 void originators_usage(void); 
    37 void trans_local_usage(void); 
    38 void trans_global_usage(void); 
    3932void orig_interval_usage(void); 
    4033void vis_server_usage(void); 
    41 void aggregation_usage(void); 
    4234void gw_mode_usage(void); 
    4335void gw_srv_list_usage(void); 
  • trunk/batctl/sys.c

    r1567 r1595  
    11/* 
    2  * Copyright (C) 2009 B.A.T.M.A.N. contributors: 
     2 * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors: 
    33 * 
    44 * Marek Lindner <lindner_marek@yahoo.de> 
     
    9595 
    9696        if (argc != 1) { 
    97                 res = write_file(SYS_ROOT_PATH, SYS_LOG_LEVEL, argv[1]); 
     97                res = write_file(SYS_MODULE_PATH, SYS_LOG_LEVEL, argv[1]); 
    9898                goto out; 
    9999        } 
    100100 
    101         res = read_file(SYS_ROOT_PATH, SYS_LOG_LEVEL, SINGLE_READ | USE_READ_BUFF); 
     101        res = read_file(SYS_MODULE_PATH, SYS_LOG_LEVEL, SINGLE_READ | USE_READ_BUFF); 
    102102 
    103103        if (res != EXIT_SUCCESS) 
     
    119119        return res; 
    120120} 
     121 
     122void originators_usage(void) 
     123{ 
     124        printf("Usage: batctl [options] originators \n"); 
     125        printf("options:\n"); 
     126        printf(" \t -h print this help\n"); 
     127        printf(" \t -n don't replace mac addresses with bat-host names\n"); 
     128        printf(" \t -w watch mode - refresh the originator table continuously\n"); 
     129} 
     130 
     131void trans_local_usage(void) 
     132{ 
     133        printf("Usage: batctl [options] translocal \n"); 
     134        printf("options:\n"); 
     135        printf(" \t -h print this help\n"); 
     136        printf(" \t -n don't replace mac addresses with bat-host names\n"); 
     137        printf(" \t -w watch mode - refresh the local translation table continuously\n"); 
     138} 
     139 
     140void trans_global_usage(void) 
     141{ 
     142        printf("Usage: batctl [options] transglobal \n"); 
     143        printf("options:\n"); 
     144        printf(" \t -h print this help\n"); 
     145        printf(" \t -n don't replace mac addresses with bat-host names\n"); 
     146        printf(" \t -w watch mode - refresh the global translation table continuously\n"); 
     147} 
     148 
     149int handle_sys_table(int argc, char **argv, char *file_path, void table_usage(void)) 
     150{ 
     151        int optchar, read_opt = USE_BAT_HOSTS; 
     152 
     153        while ((optchar = getopt(argc, argv, "hnw")) != -1) { 
     154                switch (optchar) { 
     155                case 'h': 
     156                        table_usage(); 
     157                        return EXIT_SUCCESS; 
     158                case 'n': 
     159                        read_opt &= ~USE_BAT_HOSTS; 
     160                        break; 
     161                case 'w': 
     162                        read_opt |= CLR_CONT_READ; 
     163                        break; 
     164                default: 
     165                        table_usage(); 
     166                        return EXIT_FAILURE; 
     167                } 
     168        } 
     169 
     170        return read_file(SYS_BATIF_PATH, file_path, read_opt); 
     171} 
     172 
     173void aggregation_usage(void) 
     174{ 
     175        printf("Usage: batctl [options] aggregation \n"); 
     176        printf("options:\n"); 
     177        printf(" \t -h print this help\n"); 
     178} 
     179 
     180int handle_sys_setting(int argc, char **argv, char *file_path, void setting_usage(void)) 
     181{ 
     182        int optchar, res; 
     183        char *space_ptr, *comma_char, *cmds = NULL; 
     184 
     185        while ((optchar = getopt(argc, argv, "h")) != -1) { 
     186                switch (optchar) { 
     187                case 'h': 
     188                        setting_usage(); 
     189                        return EXIT_SUCCESS; 
     190                default: 
     191                        setting_usage(); 
     192                        return EXIT_FAILURE; 
     193                } 
     194        } 
     195 
     196        if (argc == 1) 
     197                return read_file(SYS_BATIF_PATH, file_path, SINGLE_READ); 
     198 
     199        res = read_file(SYS_BATIF_PATH, file_path, SEARCH_ARGS); 
     200        if (res != EXIT_SUCCESS) 
     201                return res; 
     202 
     203        while ((space_ptr = strchr(line_ptr, ' ')) != NULL) { 
     204                *space_ptr = '\0'; 
     205 
     206                if (strncmp(line_ptr, SEARCH_ARGS_TAG, strlen(SEARCH_ARGS_TAG)) == 0) { 
     207                        cmds = space_ptr + 1; 
     208                        goto next; 
     209                } 
     210 
     211                comma_char = NULL; 
     212                if (line_ptr[strlen(line_ptr) - 1] == ',') { 
     213                        comma_char = line_ptr + strlen(line_ptr) - 1; 
     214                        *comma_char = '\0'; 
     215                } 
     216 
     217                if (strcmp(line_ptr, argv[1]) == 0) 
     218                        goto write_file; 
     219 
     220                *space_ptr = ' '; 
     221                if (comma_char) 
     222                        *comma_char = ','; 
     223 
     224next: 
     225                line_ptr = space_ptr + 1; 
     226        } 
     227 
     228        if (!cmds) 
     229                goto write_file; 
     230 
     231        printf("Error - the supplied argument is invalid: %s\n", argv[1]); 
     232        printf("The following values are allowed: %s", cmds); 
     233        return EXIT_FAILURE; 
     234 
     235write_file: 
     236        return write_file(SYS_BATIF_PATH, file_path, argv[1]); 
     237} 
  • trunk/batctl/sys.h

    r1483 r1595  
    11/* 
    2  * Copyright (C) 2009 B.A.T.M.A.N. contributors: 
     2 * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors: 
    33 * 
    44 * Marek Lindner <lindner_marek@yahoo.de> 
     
    2121 
    2222 
    23 #define SYS_ROOT_PATH "/sys/module/batman_adv/" 
     23#define SYS_MODULE_PATH "/sys/module/batman_adv/" 
     24#define SYS_BATIF_PATH "/sys/class/net/bat0/mesh/" 
    2425#define SYS_LOG_LEVEL "parameters/debug" 
    2526#define SYS_LOG "log" 
     27#define SYS_ORIGINATORS "originators" 
     28#define SYS_TRANSTABLE_LOCAL "transtable_local" 
     29#define SYS_TRANSTABLE_GLOBAL "transtable_global" 
     30#define SYS_AGGR "aggregate_ogm" 
    2631 
    27  
     32void originators_usage(void); 
     33void trans_local_usage(void); 
     34void trans_global_usage(void); 
     35void aggregation_usage(void); 
     36void bonding_usage(void); 
    2837int log_print(int argc, char **argv); 
    2938int handle_loglevel(int argc, char **argv); 
     39int handle_sys_table(int argc, char **argv, char *file_path, void table_usage(void)); 
     40int handle_sys_setting(int argc, char **argv, char *file_path, void setting_usage(void));