如何在 Wireless broadcast 封包?
較好的方式應該是存取 link layer 來送封包 (ex: Scheduler::instance().schedule(ll, pkt, 0.0);)
感覺起來屬於模擬比較底層的 network layer
但是要用到 link layer 的方式我短時間沒辦法搞懂
想搞懂可以參考下列網址:
Unicast and Broadcast Packet on NS2
追蹤ns-2.28中802.11的程式碼
How to create a new routing agent
How to implement protocol in NS2 (PDF)
Solution to Broadcast to all neighbors
因為 Project deadline 將至
所以改以參考 apps/ping.cc 方式傳送廣播封包
如果同時要用到 broadcast 和 unicast 的話
以目前我所知
此種方法並不適合
原因會在後面提到
這種方法送廣播封包時以下列程式碼傳送
[cpp]
// Create a new packet
Packet* pkt = allocpkt();
// Access the IP header for the new packet
hdr_ip* iph = HDR_IP(pkt);
iph->daddr() = IP_BROADCAST;
iph->dport() = iph->sport();
send(pkt, (Handler*) 0);
[/cpp]
接收時只要判斷 address 是不是 IP_BROADCAST 就能知道是不是廣播封包
[cpp]
// Access the IP header for the received packet:
hdr_ip* hdrip = hdr_ip::access(pkt);
// check if in brdcast mode
if ((u_int32_t)hdrip->daddr() == IP_BROADCAST) {
printf("Recv Broadcast\n");
}
[/cpp]
唯一特別要注意的是
要用這種方法 tcl 檔有特別的寫法
節錄比較特別的地方如下
[code]
set opt(rp) DumbAgent ;# routing protocol
set dv_distance0 [new Agent/DV_Distance]
$ns attach-agent $node(0) $dv_distance0
set dv_distance1 [new Agent/DV_Distance]
$ns attach-agent $node(1) $dv_distance1
set dv_distance2 [new Agent/DV_Distance]
$ns attach-agent $node(2) $dv_distance2
$ns at 1.0 "$dv_distance1 start"
[/code]
用這種方式有蠻嚴重的缺點
使用其他 unicast 的 Agent (ex: Agent/TCP) 會出現錯誤
此外少了 routing protocol 當然不是鄰近的節點收不到
至於為什麼不用 routing protocol
是因為試了 DSR 後
發現這種 broadcast 方法收到封包不會呼叫到自訂的 void DV_Distance::recv(Packet* pkt, Handler*) {}
推測是 DSR 擋住了
若改用 AODV 也會因為自訂的 Agent 沒有互相 connect
沒有指定目標會出現 Classfier::no-slot{} default handler (tcl/lib/ns-lib.tcl) 錯誤
如果指定目標就變成 unicast 點對點的傳輸了
因為 dv-distance 基本上只用到 broadcast
並沒有用到 unicast 的封包
所以目前以這種寫法變通
No comments:
Post a Comment