summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPietro Bonfa <bonfus@gmail.com>2016-10-10 21:44:50 +0200
committerPietro Bonfa <bonfus@gmail.com>2016-10-10 21:44:50 +0200
commitf5da6d8db758db9367807b45fdf4fb1a5d09e815 (patch)
tree60dd493de08188e082adb149df5382c6a0d008eb
parentbb08cc0ae4789084f88586460c9c617fe6cbb328 (diff)
better comments and if logic
-rw-r--r--main.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/main.c b/main.c
index 5a5c82b..71775b6 100644
--- a/main.c
+++ b/main.c
@@ -743,6 +743,7 @@ static size_t load_save(uint8_t **out_data)
743 } 743 }
744} 744}
745 745
746/* Loads a list of allowed hostnames and ports from file. Format is hostname:port*/
746void load_rules() 747void load_rules()
747{ 748{
748 char * ahost=NULL; 749 char * ahost=NULL;
@@ -750,6 +751,7 @@ void load_rules()
750 char line[100 + 1] = ""; 751 char line[100 + 1] = "";
751 FILE *file = NULL; 752 FILE *file = NULL;
752 rule *rule_obj = NULL; 753 rule *rule_obj = NULL;
754 int valid_rules = 0;
753 755
754 file = fopen(rules_file, "r"); 756 file = fopen(rules_file, "r");
755 757
@@ -758,13 +760,10 @@ void load_rules()
758 return; 760 return;
759 } 761 }
760 762
761 int linen = 0;
762 while (fgets(line, sizeof(line), file)) { 763 while (fgets(line, sizeof(line), file)) {
763 /* note that fgets don't strip the terminating \n, checking its
764 presence would allow to handle lines longer that sizeof(line) */
765 if(line) 764 if(line)
766 { 765 {
767 // allow comments & white lines 766 /* allow comments & white lines */
768 if (line[0]=='#'||line[0]=='\n') { 767 if (line[0]=='#'||line[0]=='\n') {
769 continue; 768 continue;
770 } 769 }
@@ -782,7 +781,7 @@ void load_rules()
782 rule_obj->host = strdup(ahost); 781 rule_obj->host = strdup(ahost);
783 782
784 LL_APPEND(rules, rule_obj); 783 LL_APPEND(rules, rule_obj);
785 linen++; 784 valid_rules++;
786 } else { 785 } else {
787 log_printf(L_WARNING, "Invalid port in line: %s\n", line); 786 log_printf(L_WARNING, "Invalid port in line: %s\n", line);
788 } 787 }
@@ -790,23 +789,27 @@ void load_rules()
790 log_printf(L_WARNING, "Could not parse line: %s\n", line); 789 log_printf(L_WARNING, "Could not parse line: %s\n", line);
791 } 790 }
792 } else { 791 } else {
792 /* reached end of file*/
793 break; 793 break;
794 } 794 }
795 } 795 }
796 fclose(file); 796 fclose(file);
797 nrules = linen; 797
798 /* save valid rules in global variable */
799 nrules = valid_rules;
798 800
799 log_printf(L_INFO, "Loaded %d rules\n", nrules); 801 log_printf(L_INFO, "Loaded %d rules\n", nrules);
800 if (nrules==0 && rules_policy == VALIDATE){ 802 if (nrules==0 && rules_policy != NONE){
801 log_printf(L_WARNING, "No rules loaded! NO CONNECTIONS WILL BE ALLOWED!\n"); 803 log_printf(L_WARNING, "No rules loaded! NO CONNECTIONS WILL BE ALLOWED!\n");
802 } 804 }
803} 805}
804 806
807/* Clear rules loaded into memory */
805void clear_rules() 808void clear_rules()
806{ 809{
807 int i; 810 int i;
808 rule * elt, *tmp; 811 rule * elt, *tmp;
809 /* now delete each element, use the safe iterator */ 812 /* delete each elemen using the safe iterator */
810 LL_FOREACH_SAFE(rules,elt,tmp) { 813 LL_FOREACH_SAFE(rules,elt,tmp) {
811 LL_DELETE(rules,elt); 814 LL_DELETE(rules,elt);
812 free(elt->host); 815 free(elt->host);