#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <linux/if_packet.h> // 对于sockaddr_ll结构,仅Linux支持
#include"arp.h"
#include <linux/if_arp.h>
#define INITIAL_SIZE 1024
// 创建原始套接字
int create_arp_socket() {
int sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
if (sock < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
return sock;
}
// 构建ARP请求报文
void build_arp_packet(struct ether_arp *arp, uint32_t target_ip) {
arp->ar_hrd = htons(ARPHRD_ETHER);
arp->ar_pro = htons(ETHERTYPE_IP);
arp->ar_hln = ETH_ALEN;
arp->ar_pln = sizeof(in_addr_t);
arp->ar_op = htons(ARPOP_REQUEST);
// 设置源MAC(本机MAC)
get_local_mac(arp->arp_sha);
// 设置源IP(本机IP)
struct in_addr local_ip;
get_local_ip(&local_ip);
memcpy(arp->arp_spa, &local_ip, sizeof(local_ip));
// 设置目标MAC(广播)
memset(arp->arp_tha, 0xFF, ETH_ALEN);
// 设置目标IP
memcpy(arp->arp_tpa, &target_ip, sizeof(target_ip));
}
// ARP扫描线程
void* scan_thread(void *arg) {
arp_scanner *scanner = (arp_scanner*)arg;
int sock = create_arp_socket();
while (scanner->config.enable_scan) {
pthread_mutex_lock(&scanner->lock);
config_t cfg = scanner->config;
pthread_mutex_unlock(&scanner->lock);
// 遍历IP范围
for (uint32_t ip = cfg.start_ip; ip <= cfg.end_ip; ip++) {
struct ether_arp arp_req;
build_arp_packet(&arp_req, ip);
// 发送ARP请求
send_arp(sock, &arp_req);
// 按间隔等待
usleep(cfg.packet_interval * 1000);
}
// 等待扫描周期
sleep(cfg.scan_cycle);
}
close(sock);
return NULL;
}
// ARP响应处理
void process_arp_response(arp_scanner *scanner, struct ether_arp *arp) {
uint32_t ip;
memcpy(&ip, arp->arp_spa, sizeof(ip));
pthread_mutex_lock(&scanner->lock);
// 查找或创建条目
arp_entry *entry = find_or_create_entry(scanner, ip);
memcpy(entry->mac, arp->arp_sha, ETH_ALEN);
entry->last_update = time(NULL);
entry->expire_time = entry->last_update + scanner->config.entry_ttl;
pthread_mutex_unlock(&scanner->lock);
}
// 超时清理线程
void* cleanup_thread(void *arg) {
arp_scanner *scanner = (arp_scanner*)arg;
while (1) {
sleep(5); // 每5秒检查一次
pthread_mutex_lock(&scanner->lock);
time_t now = time(NULL);
for (int i = 0; i < scanner->entry_count; ) {
if (scanner->entries[i].expire_time < now) {
// 移除超时条目
memmove(&scanner->entries[i], &scanner->entries[i+1],
(scanner->entry_count - i - 1) * sizeof(arp_entry));
scanner->entry_count--;
} else {
i++;
}
}
pthread_mutex_unlock(&scanner->lock);
}
return NULL;
}
// 动态调整内存
void resize_entries(arp_scanner *scanner, int new_size) {
arp_entry *new_entries = realloc(scanner->entries, new_size * sizeof(arp_entry));
if (new_entries) {
scanner->entries = new_entries;
scanner->max_entries = new_size;
}
}
void update_config(arp_scanner *scanner, config_t new_config) {
pthread_mutex_lock(&scanner->lock);
// 验证IP范围有效性
if (ntohl(new_config.end_ip) < ntohl(new_config.start_ip)) {
fprintf(stderr, "Invalid IP range\n");
} else {
scanner->config = new_config;
}
pthread_mutex_unlock(&scanner->lock);
}
// 配置
config_t default_config = {
.enable_scan = 1,
.scan_cycle = 300, // 5分钟
.packet_interval = 100, // 100毫秒
.entry_ttl = 1800, // 30分钟
.start_ip=1,
.end_ip=1
};
int main() {
default_config.start_ip = inet_addr("192.168.1.1");
default_config.end_ip = inet_addr("192.168.1.254");
arp_scanner scanner = {
.entries = malloc(INITIAL_SIZE * sizeof(arp_entry)),
.entry_count = 0,
.max_entries = INITIAL_SIZE,
.config = default_config
};
pthread_mutex_init(&scanner.lock, NULL);
// 启动扫描线程
pthread_t scan_tid, cleanup_tid;
pthread_create(&scan_tid, NULL, scan_thread, &scanner);
pthread_create(&cleanup_tid, NULL, cleanup_thread, &scanner);
// 启动ARP响应接收
int arp_sock = create_arp_socket();
while (1) {
struct ether_arp arp_resp;
if (recv_arp(arp_sock, &arp_resp) > 0) {
process_arp_response(&scanner, &arp_resp);
}
}
// 清理资源
pthread_join(scan_tid, NULL);
pthread_join(cleanup_tid, NULL);
free(scanner.entries);
close(arp_sock);
return 0;
}
这个代码编译了会报这些错误,给我解决gcc -o arp arp.c -lthread
arp.c: In function ‘build_arp_packet’:
arp.c:37:5: warning: implicit declaration of function ‘get_local_mac’ [-Wimplicit-function-declaration]
get_local_mac(arp->arp_sha);
^
arp.c:41:5: warning: implicit declaration of function ‘get_local_ip’ [-Wimplicit-function-declaration]
get_local_ip(&local_ip);
^
arp.c: In function ‘scan_thread’:
arp.c:67:13: warning: implicit declaration of function ‘send_arp’ [-Wimplicit-function-declaration]
send_arp(sock, &arp_req);
^
arp.c: In function ‘process_arp_response’:
arp.c:88:24: warning: implicit declaration of function ‘find_or_create_entry’ [-Wimplicit-function-declaration]
arp_entry *entry = find_or_create_entry(scanner, ip);
^
arp.c:88:24: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
arp.c: In function ‘main’:
arp.c:173:13: warning: implicit declaration of function ‘recv_arp’ [-Wimplicit-function-declaration]
if (recv_arp(arp_sock, &arp_resp) > 0) {
^
/usr/bin/ld: cannot find -lthread
collect2: error: ld returned 1 exit status
最新发布