network arp+icmp lab finished
This commit is contained in:
@@ -1,8 +1,52 @@
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
#include "pcap_device.h"
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define PCAP_MAC_ADDR_LEN 6
|
||||
|
||||
static int mac_is_zero(const uint8_t* mac_addr) {
|
||||
if (mac_addr == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < PCAP_MAC_ADDR_LEN; i++) {
|
||||
if (mac_addr[i]) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int read_interface_mac(const char* if_name, uint8_t* mac_addr) {
|
||||
if ((if_name == NULL) || (mac_addr == NULL)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strncpy(ifr.ifr_name, if_name, IFNAMSIZ - 1);
|
||||
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(mac_addr, (uint8_t*)ifr.ifr_hwaddr.sa_data, PCAP_MAC_ADDR_LEN);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load_pcap_lib() {
|
||||
return 0;
|
||||
}
|
||||
@@ -110,7 +154,7 @@ static int pcap_show_list(void) {
|
||||
* @param ip 打开网卡的指定ip
|
||||
* @param 给网卡设置mac
|
||||
*/
|
||||
pcap_t* pcap_device_open(const char* ip, const uint8_t * mac_addr, uint8_t poll_mode) {
|
||||
pcap_t* pcap_device_open(const char* ip, uint8_t * mac_addr, uint8_t poll_mode) {
|
||||
char err_buf[PCAP_ERRBUF_SIZE];
|
||||
struct bpf_program fp;
|
||||
bpf_u_int32 mask;
|
||||
@@ -130,6 +174,32 @@ pcap_t* pcap_device_open(const char* ip, const uint8_t * mac_addr, uint8_t poll_
|
||||
return (pcap_t*)0;
|
||||
}
|
||||
|
||||
if (mac_addr == NULL) {
|
||||
fprintf(stderr, "pcap_open: mac address buffer is null\n");
|
||||
return (pcap_t*)0;
|
||||
}
|
||||
|
||||
if (mac_is_zero(mac_addr)) {
|
||||
if (read_interface_mac(name_buf, mac_addr) < 0) {
|
||||
fprintf(stderr, "pcap_open: failed to query mac address of %s, please configure it manually.\n", name_buf);
|
||||
return (pcap_t*)0;
|
||||
}
|
||||
printf("pcap_open: detected mac %02x:%02x:%02x:%02x:%02x:%02x on %s\n",
|
||||
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5],
|
||||
name_buf);
|
||||
fflush(stdout);
|
||||
} else {
|
||||
if (read_interface_mac(name_buf, mac_addr) < 0) {
|
||||
printf("pcap_open: failed to query mac, using configured value %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
|
||||
fflush(stdout);
|
||||
} else {
|
||||
printf("pcap_open: overriding configured mac with detected value %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
|
||||
if (pcap_lookupnet(name_buf, &net, &mask, err_buf) == -1) {
|
||||
printf("pcap_open: can't find use net card: %s\n", name_buf);
|
||||
net = 0;
|
||||
@@ -166,6 +236,8 @@ pcap_t* pcap_device_open(const char* ip, const uint8_t * mac_addr, uint8_t poll_
|
||||
"(ether dst %02x:%02x:%02x:%02x:%02x:%02x or ether broadcast) and (not ether src %02x:%02x:%02x:%02x:%02x:%02x)",
|
||||
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5],
|
||||
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
|
||||
printf("pcap_open: BPF filter: %s\n", filter_exp);
|
||||
fflush(stdout);
|
||||
if (pcap_compile(pcap, &fp, filter_exp, 0, net) == -1) {
|
||||
printf("pcap_open: couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(pcap));
|
||||
return (pcap_t*)0;
|
||||
@@ -174,6 +246,8 @@ pcap_t* pcap_device_open(const char* ip, const uint8_t * mac_addr, uint8_t poll_
|
||||
printf("pcap_open: couldn't install filter %s: %s\n", filter_exp, pcap_geterr(pcap));
|
||||
return (pcap_t*)0;
|
||||
}
|
||||
printf("pcap_open: Filter installed successfully\n");
|
||||
fflush(stdout);
|
||||
|
||||
return pcap;
|
||||
}
|
||||
@@ -222,4 +296,3 @@ uint32_t pcap_device_read(pcap_t* pcap, uint8_t* buffer, uint32_t length) {
|
||||
fprintf(stderr, "pcap_read: reading packet failed!:%s", pcap_geterr(pcap));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
typedef void (*irq_handler_t)(void* arg, uint8_t is_rx, const uint8_t* data, uint32_t size);
|
||||
|
||||
pcap_t* pcap_device_open(const char* ip, const uint8_t *mac_addr, uint8_t poll_mode);
|
||||
pcap_t* pcap_device_open(const char* ip, uint8_t *mac_addr, uint8_t poll_mode);
|
||||
void pcap_device_close(pcap_t* pcap);
|
||||
uint32_t pcap_device_send(pcap_t* pcap, const uint8_t* buffer, uint32_t length);
|
||||
uint32_t pcap_device_read(pcap_t* pcap, uint8_t* buffer, uint32_t length);
|
||||
|
||||
Reference in New Issue
Block a user