summaryrefslogtreecommitdiff
path: root/lib/Vyatta/NatRuleCommon.pm
blob: afde08496d7004f15779375a9d8b55004671fb2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#
# Module: NatRuleCommon.pm
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2009 Vyatta, Inc.
# All Rights Reserved.
#
# Author: eng@vyatta.com
# Date: 2011
# Description: Shared NAT rule handling procedures library
#
# **** End License ****
#


package Vyatta::NatRuleCommon;

use strict;
use lib "/opt/vyatta/share/perl5";
require Vyatta::Config;
require Vyatta::IpTables::AddressFilter;
use Vyatta::Misc;
use Vyatta::TypeChecker;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(is_disabled get_num_ipt_rules  get_log_prefix output_xml_elem);

sub is_disabled {
  my $self = shift;
  return 1 if defined $self->{_disable};
  return 0;
}

sub get_num_ipt_rules {
  my $self = shift;
  return 0 if defined $self->{_disable};
  my $ipt_rules = 1;
  if ("$self->{_log}" eq 'enable') {
      $ipt_rules++;
  }
  if (defined $self->{_proto} && $self->{_proto} eq 'tcp_udp') {
      $ipt_rules++;
      $ipt_rules++ if $self->{_log} eq 'enable';
  }
  return $ipt_rules;
}

sub get_log_prefix {
  my ($rule_num, $type, $modifier) = @_;

  # In iptables it allows a 29 character log_prefix, but we ideally
  # want to include "[nat-$type-$num-$target] "
  #                   4   4     4    7        = 19   
  # so no truncation is needed.
  my $log_prefix  = "[NAT-$type-$rule_num";
  $log_prefix .= "-$modifier" if $modifier;
  $log_prefix .= "] ";
  return $log_prefix;
}

sub output_xml_elem {
  my ($name, $value, $fh) = @_;
  print $fh "    <$name>$value</$name>\n";
}


1;