From bb5e73853e892f8bce70e03344a81b192c2b84ff Mon Sep 17 00:00:00 2001 From: xchaos Date: Wed, 11 Jun 2014 22:44:35 +0000 Subject: [PATCH] macro definitions... git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@242 251d49ef-1d17-4917-a970-b30cf55b089b --- ipstruct.h | 7 +++++++ parsehosts.c | 22 +++++++++++++++++++++- prometheus.c | 42 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/ipstruct.h b/ipstruct.h index d8427de..797fb7b 100644 --- a/ipstruct.h +++ b/ipstruct.h @@ -60,5 +60,12 @@ struct Keyword list(Keyword); }; +struct Macro +{ + char *rewrite_from; + char *rewrite_to; + list(Macro); +}; + void TheIP(char *ipaddr, int is_network); /* function implemented in parsehosts.c */ diff --git a/parsehosts.c b/parsehosts.c index 9ee2c2b..18418ed 100644 --- a/parsehosts.c +++ b/parsehosts.c @@ -10,6 +10,7 @@ extern struct IP *ips, *ip, *sharedip, *networks; extern struct Group *groups, *group; extern struct Keyword *keyword, *defaultkeyword, *keywords; +extern struct Macro *macro, *macros; extern int class_count; extern int ip_count; extern int found_lmsid; @@ -155,7 +156,7 @@ void parse_hosts(char *hosts) parse(hosts) { - str=_; + str = _; if(*str < '0' or *str > '9') { @@ -163,6 +164,25 @@ void parse_hosts(char *hosts) continue; } + /* first, expand (rewrite) any predefined macros, if found*/ + for_each(macro, macros) + { + substring = strstr(str, macro->rewrite_from); + if(substring); + { + int l1, l3; + *substring = 0; + substring += strlen(macro->rewrite_from); + l1 = strlen(str); + l3 = strlen(substring); + string(ptr, l1 + strlen(macro->rewrite_to) + l3); + strcpy(ptr, str); + strcat(ptr, macro->rewrite_to); + strcat(ptr, substring); + str = ptr; + } + } + /* Does this IP share QoS class with some other ? */ substring = strstr(str, "sharing-"); if(substring) diff --git a/prometheus.c b/prometheus.c index 087102e..e091d6e 100644 --- a/prometheus.c +++ b/prometheus.c @@ -29,7 +29,7 @@ #include "cll1-0.6.2.h" #include "ipstruct.h" -const char *version = "0.8.3-j"; +const char *version = "0.8.5-a"; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Versions: 0.8.3 is development release, 0.8.4 will be "stable" */ @@ -58,6 +58,7 @@ const char *ls = "/bin/ls"; /* this is not user configurable :-) * char *config = "/etc/prometheus/prometheus.conf"; /* main configuration file */ char *hosts = "/etc/prometheus/hosts"; /* per-IP bandwidth definition file */ +char *macrosfile = "/etc/prometheus/prometheus.macros"; /* rewrite rules for most common tariffs */ char *iptablesfile = "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/ char *ip6tablesfile = "/var/spool/prometheus.ip6tables"; /* temporary file for ip6tables-restore*/ char *credit = "/var/lib/misc/prometheus.credit"; /* credit log file */ @@ -132,7 +133,8 @@ const int idxtable_bitmask2 = 3; /* this is no longer configurable */ struct IP *ips = NULL, *networks = NULL, *ip, *sharedip; struct Group *groups = NULL, *group; -struct Keyword *keyword, *defaultkeyword=NULL, *keywords=NULL; +struct Keyword *keyword, *defaultkeyword=NULL, *keywords = NULL; +struct Macro *macro, *macros = NULL; #define FREE_CLASS 3 #define OVERLIMIT_CLASS 4 @@ -237,7 +239,7 @@ void get_config(char *config_filename) keyword->leaf_discipline = ""; keyword->allowed_avgmtu = 0; - push(keyword,keywords); + push(keyword, keywords); if(!defaultkeyword) { defaultkeyword = keyword; @@ -450,12 +452,20 @@ void run_iptables_restore(void) char *parse_datafile_line(char *str) { - char *ptr=strchr(str,' '); + char *ptr = strchr(str,' '); + if(!ptr) + { + ptr = strchr(str,'\t'); + } if(ptr) { - *ptr=0; + *ptr = 0; ptr++; + while(*ptr == ' ' || *ptr == '\t') + { + ptr++; + } return ptr; } else @@ -609,7 +619,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); /*-----------------------------------------------------------------*/ parse(credit) { - ptr=parse_datafile_line(_); + ptr = parse_datafile_line(_); if(ptr) { if_exists(ip,ips,eq(ip->addr,_)) @@ -621,6 +631,26 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); done; /* ugly macro end */ } + if(enable_credit && just_flush<9) + { + /*-----------------------------------------------------------------*/ + printf("Parsing macro definition file %s ...\n", macrosfile); + /*-----------------------------------------------------------------*/ + parse(macrosfile) + { + ptr = parse_datafile_line(_); + if(ptr) + { + create(macro, Macro); + macro->rewrite_from = _; + macro->rewrite_to = ptr; + push(macro, macros); + printf("%s -> %s\n", macro->rewrite_from, macro->rewrite_to); + } + } + done; /* ugly macro end */ + } + if(!just_preview) { /*-----------------------------------------------------------------*/ -- 2.30.2