Some more micro-optimizations

git-svn-id: https://shorewall.svn.sourceforge.net/svnroot/shorewall/trunk@5697 fbd18981-670d-0410-9b5c-8dc0c1a9a2bb
This commit is contained in:
teastep 2007-03-26 02:53:51 +00:00
parent 377b23e183
commit da9c54a426
3 changed files with 123 additions and 124 deletions

View File

@ -38,6 +38,7 @@ our @EXPORT = qw(ALLIPv4
create_temp_object create_temp_object
finalize_object finalize_object
emit emit
emitj
emit_unindented emit_unindented
emit_as_is emit_as_is
save_progress_message save_progress_message
@ -179,6 +180,18 @@ sub emit ( $ ) {
} }
} }
sub emitj {
if ( $object ) {
#
# 'compile' as opposed to 'check'
#
for my $line ( @_ ) {
emit $line;
}
}
}
# #
# Write passed message to the object with no indentation. # Write passed message to the object with no indentation.
# #

View File

@ -100,8 +100,7 @@ sub setup_providers() {
$match =~ s/ /\|/g; $match =~ s/ /\|/g;
emit join ( "\n", emitj ( "ip route show table $duplicate | while read net route; do",
"ip route show table $duplicate | while read net route; do",
' case $net in', ' case $net in',
' default|nexthop)', ' default|nexthop)',
' ;;', ' ;;',
@ -186,8 +185,7 @@ sub setup_providers() {
$gateway = '-' unless $gateway; $gateway = '-' unless $gateway;
if ( $gateway eq 'detect' ) { if ( $gateway eq 'detect' ) {
emit join( "\n", emitj ( "gateway=\$(detect_gateway $interface)\n",
"gateway=\$(detect_gateway $interface)\n",
'if [ -n "$gateway" ]; then', 'if [ -n "$gateway" ]; then',
" run_ip route replace \$gateway src \$(find_first_interface_address $interface) dev $interface table $number", " run_ip route replace \$gateway src \$(find_first_interface_address $interface) dev $interface table $number",
" run_ip route add default via \$gateway dev $interface table $number", " run_ip route add default via \$gateway dev $interface table $number",
@ -262,8 +260,7 @@ sub setup_providers() {
emit "\nrulenum=0\n"; emit "\nrulenum=0\n";
emit join( "\n" , emitj ( "find_interface_addresses $interface | while read address; do",
"find_interface_addresses $interface | while read address; do",
' qt ip rule del from $address', ' qt ip rule del from $address',
" run_ip rule add from \$address pref \$(( $rulebase + \$rulenum )) table $number", " run_ip rule add from \$address pref \$(( $rulebase + \$rulenum )) table $number",
" echo \"qt ip rule del from \$address\" >> \${VARDIR}/undo_routing", " echo \"qt ip rule del from \$address\" >> \${VARDIR}/undo_routing",
@ -345,8 +342,7 @@ sub setup_providers() {
emit "\nif [ -z \"\$NOROUTES\" ]; then"; emit "\nif [ -z \"\$NOROUTES\" ]; then";
push_indent; push_indent;
emit join( "\n", emitj ( '#',
'#',
'# Undo any changes made since the last time that we [re]started -- this will not restore the default route', '# Undo any changes made since the last time that we [re]started -- this will not restore the default route',
'#', '#',
'undo_routing', 'undo_routing',
@ -387,8 +383,7 @@ sub setup_providers() {
if ( $providers ) { if ( $providers ) {
if ( $balance ) { if ( $balance ) {
emit join ( "\n", emitj ( 'if [ -n "$DEFAULT_ROUTE" ]; then',
'if [ -n "$DEFAULT_ROUTE" ]; then',
' run_ip route replace default scope global $DEFAULT_ROUTE', ' run_ip route replace default scope global $DEFAULT_ROUTE',
" progress_message \"Default route '\$(echo \$DEFAULT_ROUTE | sed 's/\$\\s*//')' Added\"", " progress_message \"Default route '\$(echo \$DEFAULT_ROUTE | sed 's/\$\\s*//')' Added\"",
'else', 'else',

View File

@ -71,21 +71,18 @@ sub generate_script_1 {
emit join ( '', "#\n# Compiled firewall script generated by Shorewall-pl ", $env{VERSION}, " - $date\n#" ); emit join ( '', "#\n# Compiled firewall script generated by Shorewall-pl ", $env{VERSION}, " - $date\n#" );
if ( $ENV{EXPORT} ) { if ( $ENV{EXPORT} ) {
emit join ( "\n", emitj ( 'SHAREDIR=/usr/share/shorewall-lite',
'SHAREDIR=/usr/share/shorewall-lite',
'CONFDIR=/etc/shorewall-lite', 'CONFDIR=/etc/shorewall-lite',
'VARDIR=/var/lib/shorewall-lite', 'VARDIR=/var/lib/shorewall-lite',
'PRODUCT="Shorewall Lite"' ); 'PRODUCT="Shorewall Lite"' );
copy "$env{SHAREDIR}/lib.base"; copy "$env{SHAREDIR}/lib.base";
emit join ( "\n", emitj ( '################################################################################',
'################################################################################',
'# End of /usr/share/shorewall/lib.base', '# End of /usr/share/shorewall/lib.base',
'################################################################################' ); '################################################################################' );
} else { } else {
emit join ( "\n", emitj ( 'SHAREDIR=/usr/share/shorewall',
'SHAREDIR=/usr/share/shorewall',
'CONFDIR=/etc/shorewall', 'CONFDIR=/etc/shorewall',
'VARDIR=/var/lib/shorewall', 'VARDIR=/var/lib/shorewall',
'PRODUCT=\'Shorewall\'', 'PRODUCT=\'Shorewall\'',
@ -110,14 +107,12 @@ sub generate_script_1 {
push_indent; push_indent;
if ( $ENV{EXPORT} ) { if ( $ENV{EXPORT} ) {
emit join ( "\n", emitj ( '#',
'#',
'# These variables are required by the library functions called in this script', '# These variables are required by the library functions called in this script',
'#', '#',
'CONFIG_PATH="/etc/shorewall-lite:/usr/share/shorewall-lite"' ); 'CONFIG_PATH="/etc/shorewall-lite:/usr/share/shorewall-lite"' );
} else { } else {
emit join ( "\n", emitj ( 'if [ ! -f ${SHAREDIR}/version ]; then',
'if [ ! -f ${SHAREDIR}/version ]; then',
' fatal_error "This script requires Shorewall which do not appear to be installed on this system (did you forget \"-e\" when you compiled?)"', ' fatal_error "This script requires Shorewall which do not appear to be installed on this system (did you forget \"-e\" when you compiled?)"',
'fi', 'fi',
'', '',
@ -134,8 +129,7 @@ sub generate_script_1 {
propagateconfig; propagateconfig;
emit join ( "\n" , emitj ( '[ -n "${COMMAND:=restart}" ]',
'[ -n "${COMMAND:=restart}" ]',
'[ -n "${VERBOSE:=0}" ]', '[ -n "${VERBOSE:=0}" ]',
'[ -n "${RESTOREFILE:=$RESTOREFILE}" ]', '[ -n "${RESTOREFILE:=$RESTOREFILE}" ]',
'[ -n "$LOGFORMAT" ] || LOGFORMAT="Shorewall:%s:%s:"', '[ -n "$LOGFORMAT" ] || LOGFORMAT="Shorewall:%s:%s:"',
@ -154,8 +148,7 @@ sub generate_script_1 {
append_file 'params' if $config{EXPORTPARAMS}; append_file 'params' if $config{EXPORTPARAMS};
emit join ( "\n", emitj ( '',
'',
"STOPPING=", "STOPPING=",
"COMMENT=\n", # Maintain compability with lib.base "COMMENT=\n", # Maintain compability with lib.base
'#', '#',
@ -319,8 +312,7 @@ stop_firewall() {
if ( @$criticalhosts ) { if ( @$criticalhosts ) {
if ( $config{ADMINISABSENTMINDED} ) { if ( $config{ADMINISABSENTMINDED} ) {
emit join ( "\n", emitj ( ' for chain in INPUT OUTPUT; do',
' for chain in INPUT OUTPUT; do',
' setpolicy $chain ACCEPT', ' setpolicy $chain ACCEPT',
" done\n", " done\n",
" setpolicy FORWARD DROP\n", " setpolicy FORWARD DROP\n",
@ -488,8 +480,7 @@ sub generate_script_2 () {
emit ''; emit '';
for my $interface ( @{find_interfaces_by_option 'norfc1918'} ) { for my $interface ( @{find_interfaces_by_option 'norfc1918'} ) {
emit join( "\n", emitj ( "addr=\$(ip -f inet addr show $interface 2> /dev/null | grep 'inet\ ' | head -n1)",
"addr=\$(ip -f inet addr show $interface 2> /dev/null | grep 'inet\ ' | head -n1)",
'if [ -n "$addr" ]; then', 'if [ -n "$addr" ]; then',
' addr=$(echo $addr | sed \'s/inet //;s/\/.*//;s/ peer.*//\')', ' addr=$(echo $addr | sed \'s/inet //;s/\/.*//;s/ peer.*//\')',
' for network in 10.0.0.0/8 176.16.0.0/12 192.168.0.0/16; do', ' for network in 10.0.0.0/8 176.16.0.0/12 192.168.0.0/16; do',