20xx年电大网络编程技术试题答案小抄内容摘要:

ce address :::%s\n,i_ntoa())。 27. ip = (struct ip *)buffer。 /*See if this is a TCP packet*/ 28. if(ipip_protocol == 6) { 29. printf(IP header length :::%d\n,ipip_length)。 30. printf(Protocol :::%d\n,ipip_protocol)。 31. tcp = (struct tcp *)(buffer +( 4*ipip_length))。 32. printf(Source port :::%d\n,ntohs(tcptcp_source_port))。 33. printf(Dest port :::%d\n,ntohs(tcptcp_dest_port))。 34. } 35. } 专业好文档 36.} 37 int Open_Raw_Socket() { 38. int sock。 39. if((sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP)) 0){ /*Then the socket was not created properly and must die*/ 40. perror(The raw socket was not created)。 41. exit(0)。 42. }。 43. return(sock); 44. } 45 int Set_Promisc(char *interface,int sock ) { 46. struct ifreq ifr。 47. strncpy(,interface,strnlen(interface)+1)。 48. if((ioctl(sock,SIOCGIFFLAGS,amp。 ifr) == 1) ) { /*Could not retrieve flags for the interface*/ 49. perror(Could not retrive flags for the interface)。 50. exit(0)。 51. } 52. printf(The interface is ::: %s\n,interface); 53. perror(Retrieved flags from interface successfully)。 54. |= IFF_PROMISC。 55. if (ioctl (sock,SIOCSIFFLAGS,amp。 ifr) == 1 ) { /*Could not set the flags on the interface */ 56. perror(Could not set the PROMISC flag:)。 57. exit(0); 58. } 59. printf(Setting interface ::: %s ::: to promisc,interface)。 60. return(0)。 61. } /***********************EOF**********************************/ 上面这段程序中有很详细的注解,不过我想还是有必要说一说,首先第 10 行 intOpen_Raw_Socket(void); 是我们的自定义函数,具体内容如下: 专业好文档 37 int Open_Raw_Socket() { 38. int sock。 39. if((sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP)) 0){ /*Then the socket was not created properly and must die*/ 40. perror(The raw socket was not created)。 41. exit(0)。 42. }。 43. return(sock); 44. } 第 39行 if((sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP)) 0) { 这里我们调用了 socket 函数,使创建了了一个原始 套接口 ,使之收到TCP/IP 信息包。 接下来第 11行 int Set_Promisc(char *interface,intsock),这也是我们的自定义函数,目的是把 网卡 置于混杂模式,具体内容如下: 45 int Set_Promisc(char *interface,int sock ) { 46. struct ifreq ifr。 47. strncpy(,interface,strnlen(interface)+1)。 48. if((ioctl(sock,SIOCGIFFLAGS,amp。 ifr) == 1) ) { /*Could not retrieve flags for the interface*/ 49. perror(Could not retrive flags for the interface)。 50. exit(0)。 51. } 52. printf(The interface is ::: %s\n,interface); 53. perror(Retrieved flags from interface。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。