Mercurial > routeflapper
comparison src/routeconfig.cpp @ 4:180d26aa2a17
Fedora 9 compile and const correctness.
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 12 Jun 2008 17:51:33 -0700 |
parents | bb3f804f13a0 |
children |
comparison
equal
deleted
inserted
replaced
3:4a81cc2da570 | 4:180d26aa2a17 |
---|---|
18 #include <map> | 18 #include <map> |
19 #include <stdint.h> | 19 #include <stdint.h> |
20 #include <stdlib.h> | 20 #include <stdlib.h> |
21 #include <time.h> | 21 #include <time.h> |
22 | 22 |
23 char *token_announce; | 23 const char *token_announce; |
24 char *token_file; | 24 const char *token_file; |
25 char *token_include; | 25 const char *token_include; |
26 char *token_index_ip; | 26 const char *token_index_ip; |
27 char *token_index_length; | 27 const char *token_index_length; |
28 char *token_index_path; | 28 const char *token_index_path; |
29 char *token_index_value; | 29 const char *token_index_value; |
30 char *token_ip; | 30 const char *token_ip; |
31 char *token_lbrace; | 31 const char *token_lbrace; |
32 char *token_path; | 32 const char *token_path; |
33 char *token_rbrace; | 33 const char *token_rbrace; |
34 char *token_reset; | 34 const char *token_reset; |
35 char *token_semi; | 35 const char *token_semi; |
36 char *token_slash; | 36 const char *token_slash; |
37 char *token_withdraw; | 37 const char *token_withdraw; |
38 | 38 |
39 const int training = 100; // need 100 hours uptime before using the statistics | 39 const int training = 100; // need 100 hours uptime before using the statistics |
40 const float origin_threshold = 2.9; // a bit less than 1 + decay + decay^2 | 40 const float origin_threshold = 2.9; // a bit less than 1 + decay + decay^2 |
41 const float adjacent_threshold = 2.9; // "" | 41 const float adjacent_threshold = 2.9; // "" |
42 const float decay = 0.99; // hourly exponential decay | 42 const float decay = 0.99; // hourly exponential decay |
140 0xffffffff}; | 140 0xffffffff}; |
141 | 141 |
142 | 142 |
143 //////////////////////////////////////////////// | 143 //////////////////////////////////////////////// |
144 // | 144 // |
145 char *suspicious_name(suspicion s) | 145 const char *suspicious_name(suspicion s) |
146 { | 146 { |
147 char *ss = ""; | 147 const char *ss = ""; |
148 switch (s) { | 148 switch (s) { |
149 case suspicious_none: ss = "none"; break; | 149 case suspicious_none: ss = "none"; break; |
150 case suspicious_origin: ss = "origin"; break; | 150 case suspicious_origin: ss = "origin"; break; |
151 case suspicious_adjacency: ss = "adjacency"; break; | 151 case suspicious_adjacency: ss = "adjacency"; break; |
152 default: break; | 152 default: break; |
189 inet_ntop(AF_INET, (const u_char *)&nip, adr, sizeof(adr)); | 189 inet_ntop(AF_INET, (const u_char *)&nip, adr, sizeof(adr)); |
190 char ctbuf[maxlen]; | 190 char ctbuf[maxlen]; |
191 ctime_r(&t, ctbuf); | 191 ctime_r(&t, ctbuf); |
192 int ii = strlen(ctbuf); | 192 int ii = strlen(ctbuf); |
193 if (ii > 1) ctbuf[ii-1] = '\0'; // remove trailing newline | 193 if (ii > 1) ctbuf[ii-1] = '\0'; // remove trailing newline |
194 snprintf(buf, sizeof(buf), "*** smtp connection at %s from %s via prefix %s/%d being removed", ctbuf, adr, name(prefix_length).c_str(), prefix_length); | 194 snprintf(buf, sizeof(buf), "*** smtp connection at %s from %s via prefix %s being removed", ctbuf, adr, name(prefix_length).c_str()); |
195 my_syslog(buf); | 195 my_syslog(buf); |
196 } | 196 } |
197 } | 197 } |
198 announced = false; | 198 announced = false; |
199 smtp_connections.clear(); | 199 smtp_connections.clear(); |
478 suspicion RIB::suspicious(uint32_t ip) | 478 suspicion RIB::suspicious(uint32_t ip) |
479 { | 479 { |
480 if (!stable) return suspicious_none; | 480 if (!stable) return suspicious_none; |
481 suspicion rc = suspicious_none; | 481 suspicion rc = suspicious_none; |
482 route_prefix *r1 = NULL; | 482 route_prefix *r1 = NULL; |
483 int pl; | 483 int pl = 0; |
484 pthread_mutex_lock(&rib_mutex); | 484 pthread_mutex_lock(&rib_mutex); |
485 for (int i=32; i>=0; i--) { | 485 for (int i=32; i>=0; i--) { |
486 route_prefixes &p = prefixes[i]; | 486 route_prefixes &p = prefixes[i]; |
487 uint32_t network = ip & masks[i]; | 487 uint32_t network = ip & masks[i]; |
488 route_prefix r(network); | 488 route_prefix r(network); |
571 | 571 |
572 | 572 |
573 | 573 |
574 //////////////////////////////////////////////// | 574 //////////////////////////////////////////////// |
575 // | 575 // |
576 ROUTECONFIG::ROUTECONFIG(TOKEN &tok, char *file_name_) { | 576 ROUTECONFIG::ROUTECONFIG(TOKEN &tok, const char *file_name_) { |
577 tokp = &tok; | 577 tokp = &tok; |
578 file_name = file_name_; | 578 file_name = file_name_; |
579 open(true); | 579 open(true); |
580 } | 580 } |
581 | 581 |
698 } | 698 } |
699 | 699 |
700 | 700 |
701 //////////////////////////////////////////////// | 701 //////////////////////////////////////////////// |
702 // | 702 // |
703 int ip_address(char *have); | 703 int ip_address(const char *have); |
704 int ip_address(char *have) { | 704 int ip_address(const char *have) { |
705 int ipaddr = 0; | 705 int ipaddr = 0; |
706 in_addr ip; | 706 in_addr ip; |
707 if (inet_aton(have, &ip)) ipaddr = ip.s_addr; | 707 if (inet_aton(have, &ip)) ipaddr = ip.s_addr; |
708 else { | 708 else { |
709 struct hostent *host = gethostbyname(have); | 709 struct hostent *host = gethostbyname(have); |
714 | 714 |
715 | 715 |
716 //////////////////////////////////////////////// | 716 //////////////////////////////////////////////// |
717 // | 717 // |
718 | 718 |
719 PATTERN::PATTERN(TOKEN &tok, pattern_style style_, char *pattern_, int index1_, int index2_) | 719 PATTERN::PATTERN(TOKEN &tok, pattern_style style_, const char *pattern_, int index1_, int index2_) |
720 { | 720 { |
721 style = style_; | 721 style = style_; |
722 pattern = pattern_; | 722 pattern = pattern_; |
723 index1 = index1_; | 723 index1 = index1_; |
724 index2 = index2_; | 724 index2 = index2_; |
739 PATTERN::~PATTERN() { | 739 PATTERN::~PATTERN() { |
740 regfree(&re); | 740 regfree(&re); |
741 } | 741 } |
742 | 742 |
743 | 743 |
744 bool PATTERN::process(char *buf, CONFIG &con, char *file_name, int pattern_index) | 744 bool PATTERN::process(char *buf, CONFIG &con, const char *file_name, int pattern_index) |
745 { | 745 { |
746 if (pattern) { | 746 if (pattern) { |
747 const int nmatch = max(index1, index2) + 1; | 747 const int nmatch = max(index1, index2) + 1; |
748 regmatch_t match[nmatch]; | 748 regmatch_t match[nmatch]; |
749 if (0 == regexec(&re, buf, nmatch, match, 0)) { | 749 if (0 == regexec(&re, buf, nmatch, match, 0)) { |
809 } | 809 } |
810 return false; | 810 return false; |
811 } | 811 } |
812 | 812 |
813 | 813 |
814 void PATTERN::dump(int level, int index, char *token) | 814 void PATTERN::dump(int level, int index, const char *token) |
815 { | 815 { |
816 char indent[maxlen]; | 816 char indent[maxlen]; |
817 int i = min(maxlen-1, level*4); | 817 int i = min(maxlen-1, level*4); |
818 memset(indent, ' ', i); | 818 memset(indent, ' ', i); |
819 indent[i] = '\0'; | 819 indent[i] = '\0'; |
857 //////////////////////////////////////////////// | 857 //////////////////////////////////////////////// |
858 // helper to discard the strings held by a string_set | 858 // helper to discard the strings held by a string_set |
859 // | 859 // |
860 void discard(string_set &s) { | 860 void discard(string_set &s) { |
861 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | 861 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { |
862 free(*i); | 862 free((void*)*i); |
863 } | 863 } |
864 s.clear(); | 864 s.clear(); |
865 } | 865 } |
866 | 866 |
867 | 867 |
868 //////////////////////////////////////////////// | 868 //////////////////////////////////////////////// |
869 // helper to register a string in a string set | 869 // helper to register a string in a string set |
870 // | 870 // |
871 char* register_string(string_set &s, char *name) { | 871 const char* register_string(string_set &s, const char *name) { |
872 string_set::iterator i = s.find(name); | 872 string_set::iterator i = s.find(name); |
873 if (i != s.end()) return *i; | 873 if (i != s.end()) return *i; |
874 char *x = strdup(name); | 874 char *x = strdup(name); |
875 s.insert(x); | 875 s.insert(x); |
876 return x; | 876 return x; |
878 | 878 |
879 | 879 |
880 //////////////////////////////////////////////// | 880 //////////////////////////////////////////////// |
881 // register a global string | 881 // register a global string |
882 // | 882 // |
883 char* register_string(char *name) { | 883 const char* register_string(const char *name) { |
884 return register_string(all_strings, name); | 884 return register_string(all_strings, name); |
885 } | 885 } |
886 | 886 |
887 | 887 |
888 //////////////////////////////////////////////// | 888 //////////////////////////////////////////////// |
901 } | 901 } |
902 | 902 |
903 | 903 |
904 //////////////////////////////////////////////// | 904 //////////////////////////////////////////////// |
905 // | 905 // |
906 bool tsa(TOKEN &tok, char *token); | 906 bool tsa(TOKEN &tok, const char *token); |
907 bool tsa(TOKEN &tok, char *token) { | 907 bool tsa(TOKEN &tok, const char *token) { |
908 char *have = tok.next(); | 908 const char *have = tok.next(); |
909 if (have == token) return true; | 909 if (have == token) return true; |
910 tok.token_error(token, have); | 910 tok.token_error(token, have); |
911 return false; | 911 return false; |
912 } | 912 } |
913 | 913 |
914 | 914 |
915 //////////////////////////////////////////////// | 915 //////////////////////////////////////////////// |
916 // | 916 // |
917 bool parse_path(TOKEN &tok, ROUTECONFIG &con, char *tokk, pattern_style style); | 917 bool parse_path(TOKEN &tok, ROUTECONFIG &con, const char *tokk, pattern_style style); |
918 bool parse_path(TOKEN &tok, ROUTECONFIG &con, char *tokk, pattern_style style) { | 918 bool parse_path(TOKEN &tok, ROUTECONFIG &con, const char *tokk, pattern_style style) { |
919 char *pattern = tok.next(); | 919 const char *pattern = tok.next(); |
920 int index = 0; | 920 int index = 0; |
921 if (!tsa(tok, token_lbrace)) return false; | 921 if (!tsa(tok, token_lbrace)) return false; |
922 while (true) { | 922 while (true) { |
923 char *have = tok.next(); | 923 const char *have = tok.next(); |
924 if (!have) break; | 924 if (!have) break; |
925 if (have == token_rbrace) break; | 925 if (have == token_rbrace) break; |
926 if (have == tokk) { | 926 if (have == tokk) { |
927 index = tok.nextint(); | 927 index = tok.nextint(); |
928 if (!tsa(tok, token_semi)) return false; | 928 if (!tsa(tok, token_semi)) return false; |
936 } | 936 } |
937 | 937 |
938 | 938 |
939 bool parse_announce_withdraw(TOKEN &tok, ROUTECONFIG &con, pattern_style style); | 939 bool parse_announce_withdraw(TOKEN &tok, ROUTECONFIG &con, pattern_style style); |
940 bool parse_announce_withdraw(TOKEN &tok, ROUTECONFIG &con, pattern_style style) { | 940 bool parse_announce_withdraw(TOKEN &tok, ROUTECONFIG &con, pattern_style style) { |
941 char *pattern = tok.next(); | 941 const char *pattern = tok.next(); |
942 int index1 = 0; | 942 int index1 = 0; |
943 int index2 = 0; | 943 int index2 = 0; |
944 if (!tsa(tok, token_lbrace)) return false; | 944 if (!tsa(tok, token_lbrace)) return false; |
945 while (true) { | 945 while (true) { |
946 char *have = tok.next(); | 946 const char *have = tok.next(); |
947 if (!have) break; | 947 if (!have) break; |
948 if (have == token_rbrace) break; | 948 if (have == token_rbrace) break; |
949 if (have == token_index_value) { | 949 if (have == token_index_value) { |
950 index1 = tok.nextint(); | 950 index1 = tok.nextint(); |
951 if (!tsa(tok, token_semi)) return false; | 951 if (!tsa(tok, token_semi)) return false; |
963 } | 963 } |
964 | 964 |
965 | 965 |
966 bool parse_routeconfig(TOKEN &tok, CONFIG &dc); | 966 bool parse_routeconfig(TOKEN &tok, CONFIG &dc); |
967 bool parse_routeconfig(TOKEN &tok, CONFIG &dc) { | 967 bool parse_routeconfig(TOKEN &tok, CONFIG &dc) { |
968 char *name = tok.next(); | 968 const char *name = tok.next(); |
969 if (!tsa(tok, token_lbrace)) return false; | 969 if (!tsa(tok, token_lbrace)) return false; |
970 ROUTECONFIGP con = new ROUTECONFIG(tok, name); | 970 ROUTECONFIGP con = new ROUTECONFIG(tok, name); |
971 if (con->failed()) { | 971 if (con->failed()) { |
972 delete con; | 972 delete con; |
973 return false; | 973 return false; |
974 } | 974 } |
975 dc.add_routeconfig(con); | 975 dc.add_routeconfig(con); |
976 while (true) { | 976 while (true) { |
977 char *have = tok.next(); | 977 const char *have = tok.next(); |
978 if (!have) break; | 978 if (!have) break; |
979 if (have == token_rbrace) break; | 979 if (have == token_rbrace) break; |
980 if (have == token_reset) { | 980 if (have == token_reset) { |
981 if (!parse_path(tok, *con, NULL, style_reset)) return false; | 981 if (!parse_path(tok, *con, NULL, style_reset)) return false; |
982 } | 982 } |
1003 | 1003 |
1004 | 1004 |
1005 //////////////////////////////////////////////// | 1005 //////////////////////////////////////////////// |
1006 // parse a config file | 1006 // parse a config file |
1007 // | 1007 // |
1008 bool load_conf(CONFIG &dc, char *fn) { | 1008 bool load_conf(CONFIG &dc, const char *fn) { |
1009 int count = 0; | 1009 int count = 0; |
1010 TOKEN tok(fn, &dc.config_files); | 1010 TOKEN tok(fn, &dc.config_files); |
1011 while (true) { | 1011 while (true) { |
1012 char *have = tok.next(); | 1012 const char *have = tok.next(); |
1013 if (!have) break; | 1013 if (!have) break; |
1014 if (have == token_file) { | 1014 if (have == token_file) { |
1015 if (!parse_routeconfig(tok, dc)) return false; | 1015 if (!parse_routeconfig(tok, dc)) return false; |
1016 count++; | 1016 count++; |
1017 } | 1017 } |