Re: [問題] 網路課作業求助

作者: D122 (小黑球)   2019-11-05 04:04:39
※ 引述《D122 (小黑球)》之銘言:
: 這是作業題 但並非程式課 而是網路課
: 而小弟在python上基礎還不夠扎實 加上網路知識不足 所以這作業就...
: 想拜託各位幫忙 就算只是指點該如何做也幫大忙了
: 現在也正在思考如何弄 但光是搞懂python還需花點時間
: 題目:
: https://i.imgur.com/9tDdwo6.jpg
: 簡單說就是要做能Send和Recv封包的程式
: 只是輸入條件有限制
這幾天下來 查了一些資料 這是目前根據老師範本修改 弄出來的 還不能動的版本...
因為是急就章的 語法錯誤應該不少
https://ideone.com/yQCoC5
首先 因為不知道python要如何像C一樣用memcpy來分析字串
就用if來分開輸入了(先mode interfacename 然後再根據mode需求輸入不同東西)
再來是比較關鍵的socket 爬了不少教學網站 還有版上的文
只是這次是用mininet虛擬建構 所以不清楚能不能通用
這邊根據作業要求說說我的作法 請大家指點
You’ll need to send ARP requests (which can be either the router, from
<RouterIP>, or destination host) and processes responses to get the correct
destination MAC address, ARP requests should be automatically
generated/processed before sending an response, The ARP messages should be
interoperable with both local hosts (running this same program) and the
router (running the Linux APR implementation).
網路上查到可用 netifaces.ifaddresses 直接取得該主機的IP位置
因為對destinationIP和routerIP用法不熟
所以這邊直接指定interfaceIP作為socket的host 而port就填了7777
不知道這樣能否直接用send和recv連接起來
To perform the correct ARP requests, you’ll need to determine whether the
DestIP resides within the same network as the sending host. To do this you’
ll need to properly analyze the host’s IP and netmask (Recommendation b),
along with the DestIP.
因為這邊老師有提供C的範例程式碼
unsigned int get_netmask(char *if_name, int sockfd){
struct ifreq if_idx;
memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, if_name, IFNAMSIZ-1);
if((ioctl(sockfd, SIOCGIFNETMASK, &if_idx)) == -1)
perror("ioctl():");
return ((struct sockaddr_in *)&if_idx.ifr_netmask)->sin_addr.s_addr;
}
unsigned int get_ip_saddr(char *if_name, int sockfd){
struct ifreq if_idx;
memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, if_name, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFADDR, &if_idx) < 0)
perror("SIOCGIFADDR");
return ((struct sockaddr_in *)&if_idx.ifr_addr)->sin_addr.s_addr;
}
但對netmask還不熟 這邊直接放棄
不過為了之後考試 還是有必要弄清楚原理 所以還會繼續找資料
For IP messages, ensure you properly specify all IP header parameters with
the proper byte order. Choose reasonable values for the TTL and Identifields.
Ensure the checksum value is correctly computed (example below).
這邊雖沒提及 但recommend老師有提供C的IP_checksum
int16_t ip_checksum(void* vdata,size_t length) {
char* data=(char*)vdata;
uint32_t acc=0xffff;
for (size_t i=0;i+1<length;i+=2) {
uint16_t word;
memcpy(&word,data+i,2);
acc+=ntohs(word);
if (acc>0xffff) {
acc-=0xffff;
}
}
if (length&1) {
uint16_t word=0;
memcpy(&word,data+length-1,1);
acc+=ntohs(word);
if (acc>0xffff) {
acc-=0xffff;
}
}
return htons(~acc);
}
所以我自己就慢慢修改成所知python語法(就在程式上面)
並在send裡面輸入message後呼叫 但不知這樣呼叫function和回傳值方法是否正確
During the creation of Ethernet segments, you’ll need to specify the correct
value for type, depending on whether its an ARP (0x806) or and IP (0x0800)
message.
放棄+1
因為根據敘述 我直接copy範例創建mininet的部分
那時已經指定了IP和mac 我認為已經可以了 但考量到要求1 2點
我認為我對題目的了解是不夠 錯誤的 所以對建立Ethernet和ARP request方面還要惡補
雖然知道自己要學的還很多 但這proj我只能現在放著了
只是因為接下來還有考試 我想先把握住能把握的分數
不過還是會繼續學習 還是拜託各位多少指點了
題外話
另外因為用VPN 所以IP在香港或是其他國家 但我人在美國
作者: coeric ( )   2019-11-05 17:01:00
既然都知道語法不熟了...人家也噴錯誤給你 先去排除吧...

Links booklink

Contact Us: admin [ a t ] ucptt.com