?? net.c
字號(hào):
#include <stdlib.h>#include <string.h>#include <time.h>#include <assert.h>#include <pthread.h>#include <stdio.h>#include <errno.h> #include <unistd.h>#include <fcntl.h>#include <sys/socket.h>#include <sys/types.h> #include <sys/ioctl.h> #include <sys/time.h>#include <linux/in.h>//#include <arpa/inet.h>#include <linux/if_arp.h>#include <linux/in.h>#include <linux/if_ether.h>#include <linux/filter.h>/*獲取主機(jī)IP、MAC地址以及子網(wǎng)掩碼*/int GetHostAddr(unsigned char * pMac, unsigned char * pIP, unsigned char * pMask){ int s,nRes; unsigned char * p; unsigned long nl; struct ifreq ifr; s=socket(PF_INET, SOCK_DGRAM,0 ); if( s >= 0) { bzero(&ifr, sizeof(ifr)); strncpy(ifr.ifr_name, "eth0",16); /*Get Host IP*/ if(ioctl (s, SIOCGIFADDR, &ifr)>=0) { if(pIP) { nl = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr; memcpy((void*)pIP, (void*)&nl, 4); p = pIP; printf("Local Host:%02X%02X%02X%02X", p,p[1],p[2],p[3]); } } /*Get host netmask*/ if(ioctl (s, SIOCGIFNETMASK, &ifr)>=0) { if(pMask) { nl = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr; memcpy((void*)pMask, (void*)&nl, 4); p = pMask; printf("Local Host:%02X%02X%02X%02X", p,p[1],p[2],p[3]); } } nRes = ioctl(s, SIOCGIFHWADDR, &ifr); close(s); if ( nRes< 0) perror("ioctl() get hardware address error:"); else { p = (unsigned char*) &ifr.ifr_hwaddr.sa_data; if(pMac) memcpy(pMac, p, 6); return 6; } } return -1;}int GetBridgeAddr(unsigned char * pMac, unsigned char * pIP){ int s,nRes,n, i,ifindex; unsigned char buffer[2048]; unsigned char hostmac[12]; unsigned char *iphead, *ethhead; unsigned long ip; struct ifreq ethreq; struct sockaddr_ll socket_address; n = -1; bzero(buffer, sizeof(buffer)); s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (s != -1) { strncpy(ethreq.ifr_name, "eth0", IFNAMSIZ); if (ioctl(s, SIOCGIFINDEX, ðreq) != -1) { ifindex = ethreq.ifr_ifindex; if (ioctl(s, SIOCGIFHWADDR, ðreq) != -1) { for (i = 0; i < 6; i++) hostmac[i] = ethreq.ifr_hwaddr.sa_data[i]; socket_address.sll_family = PF_PACKET; socket_address.sll_protocol = htons(ETH_P_IP); socket_address.sll_ifindex = ifindex; socket_address.sll_hatype = ARPHRD_ETHER; socket_address.sll_pkttype = PACKET_OTHERHOST; socket_address.sll_halen = ETH_ALEN; socket_address.sll_addr[6] = 0x00; socket_address.sll_addr[7] = 0x00; socket_address.sll_addr[0] = 0x03; socket_address.sll_addr[1] = 0x00; socket_address.sll_addr[2] = 0x00; socket_address.sll_addr[3] = 0x00; socket_address.sll_addr[4] = 0x00; socket_address.sll_addr[5] = 0x07; /*Send the discovery packet 4 times.*/ buffer[0] = 0x03; buffer[1] = 0x00; buffer[2] = 0x00; buffer[3] = 0x00; buffer[4] = 0x00; buffer[5] = 0x07; buffer[12] = 0x38; buffer[13] = 0x42; memcpy(buffer+6, hostmac, 6); buffer[0x12] = 0x01; buffer[0x14] = 0x81; buffer[0x15] = 0x04; buffer[0x17] = 0x01; buffer[0x20] = 0x01; buffer[0x24] = 0x03; sendto(s, buffer, 70, 0, (struct sockaddr*)&socket_address, sizeof(socket_address)); sendto(s, buffer, 70, 0, (struct sockaddr*)&socket_address, sizeof(socket_address)); sendto(s, buffer, 70, 0, (struct sockaddr*)&socket_address, sizeof(socket_address)); sendto(s, buffer, 70, 0, (struct sockaddr*)&socket_address, sizeof(socket_address)); nRes = 0; while(nRes<1000) { n = recvfrom(s,buffer,2048,0,NULL,NULL); if (n>=42) { if(memcmp(buffer,hostmac, 6)==0 && buffer[12] == 0x38 && buffer[13] == 0x42) { memcpy(pMac,buffer +6, 6); memcpy(&ip, buffer+0x4a,4); //sprintf(buffer, "Bridge IP:%08X", ip); pIP[0] = (ip&0xFF000000) >>24; pIP[1] = (ip&0x00FF0000) >>16; pIP[2] = (ip&0x0000FF00) >>8; pIP[3] = ip&0x000000FF; n = 6; break; } } nRes ++; } if(nRes ==1000) n = -1; } } } close(s); return n;}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -