aboutsummaryrefslogtreecommitdiffstats
path: root/src/fnet/arp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fnet/arp.c')
-rw-r--r--src/fnet/arp.c39
1 files changed, 18 insertions, 21 deletions
diff --git a/src/fnet/arp.c b/src/fnet/arp.c
index a7f0a603a..4736f3509 100644
--- a/src/fnet/arp.c
+++ b/src/fnet/arp.c
@@ -48,12 +48,12 @@ void arp_scan(const char *dev, uint32_t ifip, uint32_t ifmask) {
48 48
49// printf("Scanning interface %s (%d.%d.%d.%d/%d)\n", 49// printf("Scanning interface %s (%d.%d.%d.%d/%d)\n",
50// dev, PRINT_IP(ifip & ifmask), mask2bits(ifmask)); 50// dev, PRINT_IP(ifip & ifmask), mask2bits(ifmask));
51 51
52 if (strlen(dev) > IFNAMSIZ) { 52 if (strlen(dev) > IFNAMSIZ) {
53 fprintf(stderr, "Error: invalid network device name %s\n", dev); 53 fprintf(stderr, "Error: invalid network device name %s\n", dev);
54 exit(1); 54 exit(1);
55 } 55 }
56 56
57 // find interface mac address 57 // find interface mac address
58 int sock; 58 int sock;
59 if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) 59 if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
@@ -70,7 +70,7 @@ void arp_scan(const char *dev, uint32_t ifip, uint32_t ifmask) {
70 // open layer2 socket 70 // open layer2 socket
71 if ((sock = socket(PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) 71 if ((sock = socket(PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
72 errExit("socket"); 72 errExit("socket");
73 73
74 // try all possible ip addresses in ascending order 74 // try all possible ip addresses in ascending order
75 uint32_t range = ~ifmask + 1; // the number of potential addresses 75 uint32_t range = ~ifmask + 1; // the number of potential addresses
76 // this software is not supported for /31 networks 76 // this software is not supported for /31 networks
@@ -90,7 +90,7 @@ void arp_scan(const char *dev, uint32_t ifip, uint32_t ifmask) {
90 struct timeval ts; 90 struct timeval ts;
91 ts.tv_sec = 2; // 2 seconds receive timeout 91 ts.tv_sec = 2; // 2 seconds receive timeout
92 ts.tv_usec = 0; 92 ts.tv_usec = 0;
93 93
94 while (1) { 94 while (1) {
95 fd_set rfds; 95 fd_set rfds;
96 FD_ZERO(&rfds); 96 FD_ZERO(&rfds);
@@ -101,21 +101,21 @@ void arp_scan(const char *dev, uint32_t ifip, uint32_t ifmask) {
101 int maxfd = sock; 101 int maxfd = sock;
102 102
103 uint8_t frame[ETH_FRAME_LEN]; // includes eht header, vlan, and crc 103 uint8_t frame[ETH_FRAME_LEN]; // includes eht header, vlan, and crc
104 memset(frame, 0, ETH_FRAME_LEN); 104 memset(frame, 0, ETH_FRAME_LEN);
105 105
106 int nready; 106 int nready;
107 if (dest < last) 107 if (dest < last)
108 nready = select(maxfd + 1, &rfds, &wfds, (fd_set *) 0, NULL); 108 nready = select(maxfd + 1, &rfds, &wfds, (fd_set *) 0, NULL);
109 else 109 else
110 nready = select(maxfd + 1, &rfds, (fd_set *) 0, (fd_set *) 0, &ts); 110 nready = select(maxfd + 1, &rfds, (fd_set *) 0, (fd_set *) 0, &ts);
111 111
112 if (nready < 0) 112 if (nready < 0)
113 errExit("select"); 113 errExit("select");
114 114
115 if (nready == 0) { // timeout 115 if (nready == 0) { // timeout
116 break; 116 break;
117 } 117 }
118 118
119 if (FD_ISSET(sock, &wfds) && dest < last) { 119 if (FD_ISSET(sock, &wfds) && dest < last) {
120 // configure layer2 socket address information 120 // configure layer2 socket address information
121 struct sockaddr_ll addr; 121 struct sockaddr_ll addr;
@@ -125,7 +125,7 @@ void arp_scan(const char *dev, uint32_t ifip, uint32_t ifmask) {
125 addr.sll_family = AF_PACKET; 125 addr.sll_family = AF_PACKET;
126 memcpy (addr.sll_addr, mac, 6); 126 memcpy (addr.sll_addr, mac, 6);
127 addr.sll_halen = htons(6); 127 addr.sll_halen = htons(6);
128 128
129 // build the arp packet header 129 // build the arp packet header
130 ArpHdr hdr; 130 ArpHdr hdr;
131 memset(&hdr, 0, sizeof(hdr)); 131 memset(&hdr, 0, sizeof(hdr));
@@ -138,7 +138,7 @@ void arp_scan(const char *dev, uint32_t ifip, uint32_t ifmask) {
138 memcpy(hdr.sender_ip, (uint8_t *)&src, 4); 138 memcpy(hdr.sender_ip, (uint8_t *)&src, 4);
139 uint32_t dst = htonl(dest); 139 uint32_t dst = htonl(dest);
140 memcpy(hdr.target_ip, (uint8_t *)&dst, 4); 140 memcpy(hdr.target_ip, (uint8_t *)&dst, 4);
141 141
142 // build ethernet frame 142 // build ethernet frame
143 uint8_t frame[ETH_FRAME_LEN]; // includes eht header, vlan, and crc 143 uint8_t frame[ETH_FRAME_LEN]; // includes eht header, vlan, and crc
144 memset(frame, 0, sizeof(frame)); 144 memset(frame, 0, sizeof(frame));
@@ -147,16 +147,16 @@ void arp_scan(const char *dev, uint32_t ifip, uint32_t ifmask) {
147 frame[12] = ETH_P_ARP / 256; 147 frame[12] = ETH_P_ARP / 256;
148 frame[13] = ETH_P_ARP % 256; 148 frame[13] = ETH_P_ARP % 256;
149 memcpy (frame + 14, &hdr, sizeof(hdr)); 149 memcpy (frame + 14, &hdr, sizeof(hdr));
150 150
151 // send packet 151 // send packet
152 int len; 152 int len;
153 if ((len = sendto (sock, frame, 14 + sizeof(ArpHdr), 0, (struct sockaddr *) &addr, sizeof (addr))) <= 0) 153 if ((len = sendto (sock, frame, 14 + sizeof(ArpHdr), 0, (struct sockaddr *) &addr, sizeof (addr))) <= 0)
154 errExit("send"); 154 errExit("send");
155//printf("send %d bytes to %d.%d.%d.%d\n", len, PRINT_IP(dest)); 155//printf("send %d bytes to %d.%d.%d.%d\n", len, PRINT_IP(dest));
156 fflush(0); 156 fflush(0);
157 dest++; 157 dest++;
158 } 158 }
159 159
160 if (FD_ISSET(sock, &rfds)) { 160 if (FD_ISSET(sock, &rfds)) {
161 // read the incoming packet 161 // read the incoming packet
162 int len = recvfrom(sock, frame, ETH_FRAME_LEN, 0, NULL, NULL); 162 int len = recvfrom(sock, frame, ETH_FRAME_LEN, 0, NULL, NULL);
@@ -185,24 +185,21 @@ void arp_scan(const char *dev, uint32_t ifip, uint32_t ifmask) {
185 continue; 185 continue;
186 memcpy(&ip, hdr.sender_ip, 4); 186 memcpy(&ip, hdr.sender_ip, 4);
187 ip = ntohl(ip); 187 ip = ntohl(ip);
188 188
189 if (ip == last_ip) // filter duplicates 189 if (ip == last_ip) // filter duplicates
190 continue; 190 continue;
191 last_ip = ip; 191 last_ip = ip;
192 192
193 // printing 193 // printing
194 if (header_printed == 0) { 194 if (header_printed == 0) {
195 printf(" Network scan:\n"); 195 printf(" Network scan:\n");
196 header_printed = 1; 196 header_printed = 1;
197 } 197 }
198 printf(" %02x:%02x:%02x:%02x:%02x:%02x\t%d.%d.%d.%d\n", 198 printf(" %02x:%02x:%02x:%02x:%02x:%02x\t%d.%d.%d.%d\n",
199 PRINT_MAC(hdr.sender_mac), PRINT_IP(ip)); 199 PRINT_MAC(hdr.sender_mac), PRINT_IP(ip));
200 } 200 }
201 } 201 }
202 } 202 }
203 203
204 close(sock); 204 close(sock);
205} 205}
206
207
208