- 论坛徽章:
- 0
|
自己用如下代码创建了raw socket,可以获取发给本机ip地址的ip包,但无法获取广播包,想请教下该怎么才能得到发给255.255.255.255的ip包。\r\n/*\r\n* 创建获取网卡数据的套接字\r\n*/\r\neth_t *eth_open(const char *device)\r\n{\r\n eth_t *e;\r\n int n;\r\n struct packet_mreq mr;\r\n\r\n if ((e = MALLOC(sizeof(eth_t))) != NULL) {\r\n MEMSET(e, 0, sizeof(eth_t));\r\n if ((e->fd = socket(PF_PACKET, SOCK_RAW,\r\n htons(ETH_P_ALL))) < 0)\r\n return (eth_close(e));\r\n#ifdef SO_BROADCAST\r\n n = 1;\r\n if (setsockopt(e->fd, SOL_SOCKET, SO_BROADCAST, &n,\r\n sizeof(n)) < 0)\r\n return (eth_close(e));\r\n#endif\r\n strcpy(e->ifr.ifr_name, device);\r\n \r\n if (ioctl(e->fd, SIOCGIFINDEX, &e->ifr) < 0)\r\n return (eth_close(e));\r\n \r\n e->sll.sll_family = AF_PACKET;\r\n e->sll.sll_ifindex = e->ifr.ifr_ifindex;\r\n e->sll.sll_protocol = htons(ETH_P_ALL);\r\n e->sll.sll_halen = 6;\r\n memset(e->sll.sll_addr, 0xFF, 6);\r\n if(bind(e->fd, (struct sockaddr *) &(e->sll), sizeof(struct sockaddr_ll)) == -1)\r\n {\r\n return eth_close(e);\r\n }\r\n MEMSET(&mr, 0, sizeof(mr));\r\n mr.mr_ifindex = e->ifr.ifr_ifindex;\r\n mr.mr_type = PACKET_MR_PROMISC;\r\n if (setsockopt(e->fd, SOL_PACKET,\r\n PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == -1)\r\n {\r\n return eth_close(e);\r\n }\r\n }\r\n if(strcmp(device,\"eth0\") == 0)\r\n {\r\n MEMCPY(e->mac, table[0].mac,6);\r\n }\r\n else\r\n {\r\n MEMCPY(e->mac, table[1].mac,6);\r\n }\r\n return (e);\r\n} |
|