initial commit

This commit is contained in:
2025-08-05 15:53:44 +08:00
commit 09dc02ae52
553 changed files with 137665 additions and 0 deletions

115
third_party/libhv/examples/nmap/main.cpp vendored Executable file
View File

@@ -0,0 +1,115 @@
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "nmap.h"
#include "hsocket.h"
#include "hthreadpool.h"
int host_discover_task(std::string segment, void* nmap) {
Nmap* hosts= (Nmap*)nmap;
return host_discover(segment.c_str(), hosts);
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Usage: nmap segment\n");
printf("Examples: nmap 192.168.1.123\n");
printf(" nmap 192.168.1.x/24\n");
printf(" nmap 192.168.x.x/16\n");
return -1;
}
char* segment = argv[1];
char* split = strchr(segment, '/');
int n = 24;
if (split) {
*split = '\0';
n = atoi(split+1);
if (n != 24 && n != 16) {
return -2;
}
}
Nmap hosts;
char ip[INET_ADDRSTRLEN];
if (n == 24) {
host_discover(segment, &hosts);
}
else if (n == 16) {
Nmap segs;
int up_nsegs = segment_discover(segment, &segs);
if (up_nsegs == 0) return 0;
#if 1
for (auto& pair : segs) {
if (pair.second == 1) {
uint32_t addr = pair.first;
uint8_t* p = (uint8_t*)&addr;
// 0,255 reserved
for (int i = 1; i < 255; ++i) {
p[3] = i;
hosts[addr] = 0;
}
}
}
nmap_discover(&hosts);
#else
if (up_nsegs == 1) {
for (auto& pair : segs) {
if (pair.second == 1) {
inet_ntop(AF_INET, (void*)&pair.first, ip, sizeof(ip));
host_discover(ip, &hosts);
}
}
}
else {
// ThreadPool + host_discover
Nmap* hosts = new Nmap[up_nsegs];
// use ThreadPool
HThreadPool tp(4);
tp.start();
std::vector<std::future<int>> futures;
int i = 0;
for (auto& pair : segs) {
if (pair.second == 1) {
inet_ntop(AF_INET, (void*)&pair.first, ip, sizeof(ip));
auto future = tp.commit(host_discover_task, std::string(ip), &hosts[i++]);
futures.push_back(std::move(future));
}
}
// wait all task done
int nhosts = 0;
for (auto& future : futures) {
nhosts += future.get();
}
// filter up hosts
std::vector<uint32_t> up_hosts;
for (int i = 0; i < up_nsegs; ++i) {
Nmap& nmap = hosts[i];
for (auto& host : nmap) {
if (host.second == 1) {
up_hosts.push_back(host.first);
}
}
}
delete[] hosts;
}
#endif
}
// filter up hosts
std::vector<uint32_t> up_hosts;
for (auto& pair : hosts) {
if (pair.second == 1) {
up_hosts.push_back(pair.first);
}
}
// print up hosts
printf("Up hosts %lu:\n", (unsigned long)up_hosts.size());
for (auto& host : up_hosts) {
inet_ntop(AF_INET, (void*)&host, ip, sizeof(ip));
printf("%s\n", ip);
}
return 0;
}

177
third_party/libhv/examples/nmap/nmap.cpp vendored Executable file
View File

@@ -0,0 +1,177 @@
#include "nmap.h"
#include "hloop.h"
#include "hstring.h"
#include "hsocket.h"
#include "netinet.h"
using namespace hv;
#define MAX_RECVFROM_TIMEOUT 5000 // ms
#define MAX_SENDTO_PERSOCKET 1024
typedef struct nmap_ctx_s {
Nmap* nmap;
int send_cnt;
int recv_cnt;
int up_cnt;
int idle_cnt;
} nmap_ctx_t;
static void on_idle(hidle_t* idle) {
hloop_t* loop = hevent_loop(idle);
nmap_ctx_t* ctx = (nmap_ctx_t*)hloop_userdata(loop);
ctx->idle_cnt++;
if (ctx->idle_cnt == 1) {
// try again?
}
hloop_stop(loop);
}
static void on_timer(htimer_t* timer) {
hloop_t* loop = hevent_loop(timer);
hloop_stop(loop);
}
static void on_recvfrom(hio_t* io, void* buf, int readbytes) {
//printd("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
/*
char localaddrstr[SOCKADDR_STRLEN] = {0};
char peeraddrstr[SOCKADDR_STRLEN] = {0};
printd("[%s] <=> [%s]\n",
SOCKADDR_STR(hio_localaddr(io), localaddrstr),
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
*/
hloop_t* loop = hevent_loop(io);
nmap_ctx_t* ctx = (nmap_ctx_t*)hloop_userdata(loop);
if (++ctx->recv_cnt == ctx->send_cnt) {
//hloop_stop(loop);
}
Nmap* nmap = ctx->nmap;
struct sockaddr_in* peeraddr = (struct sockaddr_in*)hio_peeraddr(io);
auto iter = nmap->find(peeraddr->sin_addr.s_addr);
if (iter != nmap->end()) {
if (iter->second == 0) {
iter->second = 1;
if (++ctx->up_cnt == nmap->size()) {
hloop_stop(loop);
}
}
}
}
int nmap_discover(Nmap* nmap) {
hloop_t* loop = hloop_new(0);
uint64_t start_hrtime = hloop_now_hrtime(loop);
nmap_ctx_t ctx;
ctx.nmap = nmap;
ctx.send_cnt = 0;
ctx.recv_cnt = 0;
ctx.up_cnt = 0;
ctx.idle_cnt = 0;
hloop_set_userdata(loop, &ctx);
char recvbuf[128];
// icmp
char sendbuf[44]; // 20IP + 44ICMP = 64
icmp_t* icmp_req = (icmp_t*)sendbuf;
icmp_req->icmp_type = ICMP_ECHO;
icmp_req->icmp_code = 0;
icmp_req->icmp_id = getpid();
for (int i = 0; i < sizeof(sendbuf) - sizeof(icmphdr_t); ++i) {
icmp_req->icmp_data[i] = i;
}
struct sockaddr_in peeraddr;
hio_t* io = NULL;
for (auto iter = nmap->begin(); iter != nmap->end(); ++iter) {
if (iter->second == 1) continue;
if (ctx.send_cnt % MAX_SENDTO_PERSOCKET == 0) {
// socket
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd < 0) {
perror("socket");
if (errno == EPERM) {
fprintf(stderr, "please use root or sudo to create a raw socket.\n");
}
return -socket_errno();
}
nonblocking(sockfd);
so_sndbuf(sockfd, 425984); // 416K
io = hio_get(loop, sockfd);
if (io == NULL) return -1;
hio_set_type(io, HIO_TYPE_IP);
struct sockaddr_in localaddr;
socklen_t addrlen = sizeof(localaddr);
memset(&localaddr, 0, addrlen);
localaddr.sin_addr.s_addr = htonl(INADDR_ANY);
hio_set_localaddr(io, (struct sockaddr*)&localaddr, addrlen);
hrecvfrom(loop, sockfd, recvbuf, sizeof(recvbuf), on_recvfrom);
}
icmp_req->icmp_seq = iter->first;
icmp_req->icmp_cksum = 0;
icmp_req->icmp_cksum = checksum((uint8_t*)icmp_req, sizeof(sendbuf));
socklen_t addrlen = sizeof(peeraddr);
memset(&peeraddr, 0, addrlen);
peeraddr.sin_family = AF_INET;
peeraddr.sin_addr.s_addr = iter->first;
hio_set_peeraddr(io, (struct sockaddr*)&peeraddr, addrlen);
hsendto(loop, hio_fd(io), sendbuf, sizeof(sendbuf), NULL);
++ctx.send_cnt;
}
htimer_add(loop, on_timer, MAX_RECVFROM_TIMEOUT, 1);
hidle_add(loop, on_idle, 3);
hloop_run(loop);
uint64_t end_hrtime = hloop_now_hrtime(loop);
hloop_free(&loop);
// print result
char ip[INET_ADDRSTRLEN];
auto iter = nmap->begin();
while (iter != nmap->end()) {
inet_ntop(AF_INET, (void*)&iter->first, ip, sizeof(ip));
printd("%s\t is %s.\n", ip, iter->second == 0 ? "down" : "up");
++iter;
}
printd("Nmap done: %lu IP addresses (%d hosts up) scanned in %.2f seconds\n",
(unsigned long)nmap->size(), ctx.up_cnt, (end_hrtime-start_hrtime)/1000000.0f);
return ctx.up_cnt;
}
int segment_discover(const char* segment16, Nmap* nmap) {
StringList strlist = split(segment16, '.');
if (strlist.size() != 4) return -1;
uint32_t addr = 0;
uint8_t* p = (uint8_t*)&addr;
p[0] = atoi(strlist[0].c_str());
p[1] = atoi(strlist[1].c_str());
p[3] = 1;
printd("Nmap scan %u.%u.x.1...\n", p[0], p[1]);
nmap->clear();
for (int i = 0; i < 256; ++i) {
p[2] = i;
(*nmap)[addr] = 0;
}
return nmap_discover(nmap);
}
int host_discover(const char* segment24, Nmap* nmap) {
StringList strlist = split(segment24, '.');
if (strlist.size() != 4) return -1;
uint32_t addr = 0;
uint8_t* p = (uint8_t*)&addr;
p[0] = atoi(strlist[0].c_str());
p[1] = atoi(strlist[1].c_str());
p[2] = atoi(strlist[2].c_str());
printd("Nmap scan %u.%u.%u.x...\n", p[0], p[1], p[2]);
// 0,255 reserved
nmap->clear();
for (int i = 1; i < 255; ++i) {
p[3] = i;
(*nmap)[addr] = 0;
}
return nmap_discover(nmap);
}

19
third_party/libhv/examples/nmap/nmap.h vendored Executable file
View File

@@ -0,0 +1,19 @@
#ifndef HV_NMAP_H_
#define HV_NMAP_H_
#include <stdint.h>
#include <map>
// addr => 0:down 1:up
typedef std::map<uint32_t, int> Nmap;
// ip = segment + host
// segment16: 192.168.x.x
// segment24: 192.168.1.x
// @return up_cnt
int nmap_discover(Nmap* nmap);
int segment_discover(const char* segment16, Nmap* nmap);
int host_discover(const char* segment24, Nmap* nmap);
#endif // HV_NMAP_H_