Python 板


LINE

※ 引述《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在香港或是其他国家 但我人在美国 -- 「叶雪来了!有绳快掰!」 γ By bugbook◢█◣▃▃▃ ◢█◣ ▃▃▃▃▃ . 「嘿嘿…」 █◢▌ | | █▃█ ▊ █ ▊ <  ' \ \_____ ▎◢ ==█ O O ▊◥◣ ▊ ██/ // ____ +▋市民广场 ██ ▅▅   ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄M, - ▆▆▆▆▆▆▆ ◣ @@ || BBS ◣◢▄▄ ◣\/◢ --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 45.135.186.27 (香港)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1572897886.A.90E.html
1F:→ coeric: 既然都知道语法不熟了...人家也喷错误给你 先去排除吧... 11/05 17:01







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP