47.5 An Example

The following code illustrates a small but complete simulation script for setting up an emulation test using BPF and IP network objects. It was run on a multi-homed machine, and the simulator essentially provides routing capability by reading frames from one interface, passing them through the simulated network, and writing them out via the raw IP network object:

        set me "10.0.1.1"
        set ns [new Simulator]

        $ns use-scheduler RealTime

        #
        # we want the test machine to have ip forwarding disabled, so
        # check this (this is how to do so under FreeBSD at least)
        #

        set ipforw [exec sysctl -n net.inet.ip.forwarding]
        if { $ipforw } {
                puts "can not run with ip forwarding enabled"
                exit 1
        }

        #
        # allocate a BPF type network object and a raw-IP object
        #
        set bpf0 [new Network/Pcap/Live]
        set bpf1 [new Network/Pcap/Live]
        $bpf0 set promisc_ true
        $bpf1 set promisc_ true

        set ipnet [new Network/IP]

        set nd0 [$bpf0 open readonly fxp0]
        set nd1 [$bpf1 open readonly fxp1]
        $ipnet open writeonly

        #
        # try to filter out weird stuff like netbios pkts, arp requests, dns,
        # also, don't catch stuff to/from myself or broadcasted
        #
        set notme "(not ip host $me)"
        set notbcast "(not ether broadcast)"
        set ftp "and port ftp-data"
        set f0len [$bpf0 filter "(ip dst host bit) and $notme and $notbcast"]
        set f1len [$bpf1 filter "(ip src host bit) and $notme and $notbcast"]

        puts "filter lengths: $f0len (bpf0), $f1len (bpf1)"
        puts "dev $nd0 has address [$bpf0 linkaddr]"
        puts "dev $nd1 has address [$bpf1 linkaddr]"

        set a0 [new Agent/Tap]
        set a1 [new Agent/Tap]
        set a2 [new Agent/Tap]
        puts "install nets into taps..."
        $a0 network $bpf0
        $a1 network $bpf1
        $a2 network $ipnet

        set node0 [$ns node]
        set node1 [$ns node]
        set node2 [$ns node]

        $ns simplex-link $node0 $node2 10Mb 10ms DropTail
        $ns simplex-link $node1 $node2 10Mb 10ms DropTail

        $ns attach-agent $node0 $a0
        $ns attach-agent $node1 $a1
        $ns attach-agent $node2 $a2

        $ns connect $a0 $a2
        $ns connect $a1 $a2

        puts "okey"
        $ns run



Tom Henderson 2011-11-05