遇到问题:
1。接入一个不知道网段网段的局域网;
2。需要发现局域网的所有的ip;
处理步骤
1。用原始套接字,监听arp消息,获取接口的mac地址;
2。根据收到的arp广播,可以从arp协议的sip字段,提取出源ip;
3。根据ip地址,然后掩码上24位,然后发送从1 ~255的地址的arp 广播;
4。通过收到的arp repy 来获取所有的局域网地址;
5。如果收到了新的ip地址24位掩码。不等于原来保持的ip地址24位掩码,掩码就变成16位;然后发送65535个报文;(猜想掩码位16位做处理)
下面代码是参考,需要用我个人封装好的libevent库才可以运行,如果需要可以联系我~~
- #include <unistd.h>
- #include <errno.h>
- #include <netdb.h>
- #include <signal.h>
- #include <sys/socket.h>
- #include <sys/poll.h>
- #include <sys/ioctl.h>
- #include <netinet/if_ether.h>
- #include <net/if_arp.h>
- #include <netinet/udp.h>
- #include <netinet/ip.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <net/if.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <map>
- #include <iostream>
-
- #include <fcntl.h>
- using namespace std;
- #include "cneos_event.h"
- enum {
- ARP_MSG_SIZE = 0x2a
- };
- typedef map<int, string> ARP;
-
- ARP arp_table;
-
- #define MASK_16 0xffff0000
- #define MASK_24 0xffffff00
- int network=0;
- uint32_t sip;
- uint8_t smac[6];
-
- char interface[] = "eno16777984";
-
-
-
- char* strncpy_IFNAMSIZ(char *dst, const char *src)
- {
- #ifndef IFNAMSIZ
- enum { IFNAMSIZ = 16 };
- #endif
- return strncpy(dst, src, IFNAMSIZ);
- }
-
- struct arpMsg {
- /* Ethernet header */
- uint8_t h_dest[6]; /* 00 destination ether addr */
- uint8_t h_source[6]; /* 06 source ether addr */
- uint16_t h_proto; /* 0c packet type ID field */
-
- /* ARP packet */
- uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */
- uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */
- uint8_t hlen; /* 12 hardware address length (must be 6) */
- uint8_t plen; /* 13 protocol address length (must be 4) */
- uint16_t operation; /* 14 ARP opcode */
- uint8_t sHaddr[6]; /* 16 sender's hardware address */
- uint8_t sInaddr[4]; /* 1c sender's IP address */
- uint8_t tHaddr[6]; /* 20 target's hardware address */
- uint8_t tInaddr[4]; /* 26 target's IP address */
- uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */
- } PACKED;
- const int const_int_1 = 1;
-
- //设置套接子为广播
- int setsockopt_broadcast(int fd)
- {
- return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
- }
-
- char* safe_strncpy(char *dst, const char *src, size_t size)
- {
- if (!size) return dst;
- dst[--size] = '\0';
- return strncpy(dst, src, size);
- }
- int set_socket_bloking(int socket)
- {
- int flags = fcntl(socket, F_GETFL, 0);
- fcntl(socket, F_SETFL, flags & ~O_NONBLOCK);
- }
-
-
- //发送ARP请求
- int arpping(int s,uint32_t test_ip, uint32_t from_ip)
- {
- int timeout_ms;
-
- int rv = 1; /* "no reply received" yet */
- struct sockaddr addr; /* for interface name */
- struct arpMsg arp;
-
- //构造ARP数据包
- /* send arp request */
- memset(&arp, 0, sizeof(arp));
- memset(arp.h_dest, 0xff, 6); /* MAC DA */
- memcpy(arp.h_source, smac, 6); /* MAC SA */
- arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
- arp.htype = htons(ARPHRD_ETHER); /* hardware type */
- arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
- arp.hlen = 6; /* hardware address length */
- arp.plen = 4; /* protocol address length */
- arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
- memcpy(arp.sHaddr, smac, 6); /* source hardware address */
- memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
- /* tHaddr is zero-fiiled */ /* target hardware address */
- memcpy(arp.tInaddr, &test_ip, sizeof(test_ip)); /* target IP address */
- //构造目标网络地址
- memset(&addr, 0, sizeof(addr));
- safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
- //set_socket_bloking(s);
-
- int try_count = 0;
- while (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
- {
- printf("send to error\n");
- try_count++;
- if (try_count == 1000) break;
- }
-
- return rv;
- }
-
-
- int read_interface(const char *interface, int *ifindex, uint32_t *addr, uint8_t *arp)
- {
- int fd;
- struct ifreq ifr;
- struct sockaddr_in *our_ip;
-
- memset(&ifr, 0, sizeof(ifr));
- fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
-
- ifr.ifr_addr.sa_family = AF_INET;
- strncpy_IFNAMSIZ(ifr.ifr_name, interface);
- if (addr) {
- if (ioctl(fd, SIOCGIFADDR, &ifr) != 0) {
- perror("ioctl");
- close(fd);
- return -1;
- }
- our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
- *addr = our_ip->sin_addr.s_addr;
- printf("ip of %s = %s \n", interface, inet_ntoa(our_ip->sin_addr));
- }
-
- if (ifindex) {
- if (ioctl(fd, SIOCGIFINDEX, &ifr) != 0) {
- close(fd);
- return -1;
- }
- printf("adapter index %d", ifr.ifr_ifindex);
- *ifindex = ifr.ifr_ifindex;
- }
-
- if (arp) {
- if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0) {
- close(fd);
- return -1;
- }
- memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
- printf("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x\n",
- arp[0],arp[1],arp[2],arp[3],arp[4], arp[5]);
- }
- close(fd);
- return 0;
- }
-
- void ReadEventCb(struct bufferevent *bev, void *data)
- {
- char buffer[2000];
- int len = GetReadBuffer(buffer, sizeof(buffer));
- if (len >= sizeof(struct arpMsg))
- {
- struct arpMsg *arp1 = (struct arpMsg *)buffer;
- char mac[20];
- sprintf(mac,"%02x:%02x:%02x:%02x:%02x:%02x",
- arp1->sHaddr[0],
- arp1->sHaddr[1],
- arp1->sHaddr[2],
- arp1->sHaddr[3],
- arp1->sHaddr[4],
- arp1->sHaddr[5]);
- //if (arp1->htype == htons(0x0001) && arp1->h_proto == htons(0x0800))
- int sip = ntohl(((int *)(arp1->sInaddr))[0]);
- if (sip)
- {
-
- if (sip&MASK_24 != network && arp_table.find(sip) == arp_table.end()) {
- network = sip >> 8 << 8;
- printf("ip of %s %d.%d.%d.%d \n", mac, arp1->sInaddr[0], arp1->sInaddr[1], arp1->sInaddr[2], arp1->sInaddr[3]);
- for (int i = 1; i < 255; i++)
- {
- arpping(bufferevent_getfd(bev), htonl(network + i), htonl(network+254));
- }
- }
- arp_table[sip] = mac;
-
- }
- }
-
- }
-
- int main(int argc, char **argv)
- {
- uint32_t TEST_IP =1||inet_addr(argv[1]);
- //网卡名字
-
- read_interface(interface, NULL, &sip, smac); //根据网卡名字读取本地的mac地址和ip地址
-
- int arp_fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
- if (arp_fd <=0 || setsockopt_broadcast(arp_fd) == -1) {
- perror("cannot enable bcast on raw socket");
- }
- evutil_make_socket_nonblocking(arp_fd);
- AddBuffEvent(arp_fd, ReadEventCb, NULL, NULL,NULL);
- event_run();
-
- return 0;
- }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。