Skip to content

Commit

Permalink
Add UDP support
Browse files Browse the repository at this point in the history
  • Loading branch information
rodjek committed Jun 4, 2011
1 parent 5a65a74 commit 4e68f99
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/iptables.rb
@@ -1,4 +1,5 @@
require 'iptables/protocol/tcp'
require 'iptables/protocol/udp'
require 'iptables/rule'

module IPTables
Expand Down
31 changes: 31 additions & 0 deletions lib/iptables/protocol/udp.rb
@@ -0,0 +1,31 @@
module IPTables
module Protocol
module UDP
attr_reader :source_port, :destination_port

def self.extended(base)
base.mod_opts[:protocol] = []
end

def source_port=(value)
value = value.to_i
unless value > 0
raise "UDP source port must be a valid integer greater than 0"
end

@source_port = value
@mod_opts[:protocol] << "--sport" << source_port
end

def destination_port=(value)
value = value.to_i
unless value > 0
raise "UDP destination port must be a valid integer greater than 0"
end

@destination_port = value
@mod_opts[:protocol] << "--dport" << destination_port
end
end
end
end
1 change: 1 addition & 0 deletions lib/iptables/rule.rb
Expand Up @@ -11,6 +11,7 @@ def initialize
def protocol=(value)
protocols = {
:tcp => IPTables::Protocol::TCP,
:udp => IPTables::Protocol::UDP,
}

value = value.to_sym unless value.is_a? Symbol
Expand Down
4 changes: 0 additions & 4 deletions spec/iptables/protocol_tcp_spec.rb
Expand Up @@ -18,10 +18,6 @@
it { should respond_to(:tcp_flags=).with(1).argument }
it { should respond_to(:syn).with(0).arguments }
it { should respond_to(:syn=).with(1).arguments }
it { should respond_to(:tcp_option).with(0).arguments }
it { should respond_to(:tcp_option=).with(1).argument }
it { should respond_to(:mss).with(0).arguments }
it { should respond_to(:mss=).with(1).argument }

context "when creating a rule with source_port" do
subject do
Expand Down
50 changes: 50 additions & 0 deletions spec/iptables/protocol_udp_spec.rb
@@ -0,0 +1,50 @@
require 'spec_helper'

describe IPTables::Rule do
describe "when protocol is udp" do
subject do
rule = IPTables::Rule.new
rule.protocol = :udp
rule
end

its(:protocol) { should == :udp }

it { should respond_to(:source_port).with(0).arguments }
it { should respond_to(:source_port=).with(1).argument }
it { should respond_to(:destination_port).with(0).arguments }
it { should respond_to(:destination_port=).with(1).argument }

context "and creating a rule with source_port" do
subject do
rule = IPTables::Rule.new
rule.chain = :input
rule.target = :accept
rule.protocol = :udp
rule.source_port = 80
rule
end

its(:source_port) { should == 80 }
its(:to_iptables) {
should == "-A INPUT -p udp --sport 80 -j ACCEPT"
}
end

context "and creating a rule with destination_port" do
subject do
rule = IPTables::Rule.new
rule.chain = :input
rule.target = :accept
rule.protocol = :udp
rule.destination_port = 443
rule
end

its(:destination_port) { should == 443 }
its(:to_iptables) {
should == "-A INPUT -p udp --dport 443 -j ACCEPT"
}
end
end
end

0 comments on commit 4e68f99

Please sign in to comment.