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