参数初始化以及ntop主流程启动


 #ifndef WIN32
if((argc == ) && (argv[][] != '-'))
rc = prefs->loadFromFile(argv[]);
else
#endif
//一般启动ntopng的命令: ntopng /etc/ntopng/ntopng.conf 上面的代码就是读取配置文件/etc/ntopng/ntopng.conf里的配置信息。
 prefs->registerNetworkInterfaces();

   if(prefs->get_num_user_specified_interfaces() == ) {
/* We add all interfaces available on this host */
prefs->add_default_interfaces();
}
//通过配置文件配置的interface信息注册可以使用的网卡信息:
//如果没有配置网卡,ntopng会调用pcap的接口去查找所有可用的网卡,并注册。 注:ntopng的配置文件跟命令行参数的输入是一样的 这种统一的配置方式非常方便,值得借鉴。
  // enable all protocols
NDPI_BITMASK_SET_ALL(all);
ndpi_set_protocol_detection_bitmask2(ndpi_struct, &all);
// NetworkInterface类 初始化了协议检查的所有类型,并提供了报文分析的函数。 if(iface == NULL) {
try {
iface = new PcapInterface(ifName);
} catch(...) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to create interface %s", ifName);
iface = NULL;
}
}
//ntopng 默认使用libpcap来处理网卡抓包,这里默认使用PcapInterface这个类,PcapInterface继承了NetworkInterface类。 iface->setCPUAffinity(core_id);
//如果是多核多网卡的服务器,则需要考虑到性能,设置CPU亲和度,保证每个网卡能有对应的CPU处理 ntop->start();
//至此,进入抓包解析流程

HTTPServer的初始化端口和lua CGI支持


  ntop->registerHTTPserver(new HTTPserver(prefs->get_http_port(),
prefs->get_docs_dir(),
prefs->get_scripts_dir()));
//这里注册http服务器,以便后续的web监控使用。 callbacks.begin_request = handle_lua_request;
httpd_v4 = mg_start(&callbacks, NULL, (const char**)http_options);
//然后调用第三方的http库,启用多线程的httpserver。
//callbacks包含了处理http中请求lua脚本的功能函数。 来到third-party/mongoose/mongoose.c文件, 函数mg_start:
// Start master (listening) thread
mg_start_thread(master_thread, ctx); // Start worker threads
for (i = ; i < atoi(ctx->config[NUM_THREADS]); i++) {
if (mg_start_thread(worker_thread, ctx) != ) {
cry(fc(ctx), "Cannot start worker thread: %d", ERRNO);
} else {
ctx->num_threads++;
}
}
//master_thread负责监听,并将accept接受的socket缓存到ctx->queue中;
//worker_thread负责处理ctx->queue缓存的socket处理http请求。 static void prepare_lua_environment(struct mg_connection *conn, lua_State *L)
.....
// Register "print" function which calls mg_write()
lua_pushlightuserdata(L, conn);
lua_pushcclosure(L, lsp_mg_print, );
lua_setglobal(L, "print"); // Register mg_read()
lua_pushlightuserdata(L, conn);
lua_pushcclosure(L, lsp_mg_read, );
lua_setglobal(L, "read");
//上面的代码注册了lua脚本使用的print和read函数。
//prepare_lua_environment函数使得httpserver支持lua脚本作为CGI语言。
  //后续就可以通过lua脚本来回应web客户端的各种request;
//lua又通过lua c api的扩展来获取后面报文分析的结果来填充网页的请求。

抓包和报文分析主流程


下面的代码是在src/ntopng.cpp  start函数:
 for(int i=; i<num_defined_interfaces; i++) {
iface[i]->allocateNetworkStats();
iface[i]->startPacketPolling();
}
//allocateNetworkStats 初始化网络统计功能
//开始抓包
//startPacketPolling 注意是一个虚函数,这里是多态,实际是PcapInterface的startPacketPolling 函数。
下面来到src/PcapInterface.cpp文件:
 void PcapInterface::startPacketPolling() {
pthread_create(&pollLoop, NULL, packetPollLoop, (void*)this);
pollLoopCreated = true;
NetworkInterface::startPacketPolling();
} //startPacketPolling函数创建了一个线程,线程主要处理函数是packetPollLoop。
//static void* packetPollLoop(void* ptr), 就定义在PcapInterface.cpp文件里。 //packetPollLoop函数开始了报文复制和分析的过程
FILE *pcap_list = iface->get_pcap_list();
......
hdr->caplen = min_val(hdr->caplen, iface->getMTU());
iface->dissectPacket(hdr, pkt, &shaped, &p);
//dissectPacket 是NetworkInterface类的成员函数,里面开始了复杂的报文解析流程。。。
//bool dissectPacket(const struct pcap_pkthdr *h, const u_char *packet, bool *shaped, u_int16_t *ndpiProtocol);
//分析出报文的类型之后,将报文传给processPacket函数处理和统计
 bool processPacket(const struct bpf_timeval *when,
const u_int64_t time,
struct ndpi_ethhdr *eth,
u_int16_t vlan_id,
struct ndpi_iphdr *iph,
struct ndpi_ipv6hdr *ip6,
u_int16_t ipsize, u_int16_t rawsize,
const struct pcap_pkthdr *h,
const u_char *packet,
bool *shaped,
u_int16_t *ndpiProtocol); inline void incStats(time_t when, u_int16_t eth_proto, u_int16_t ndpi_proto,
u_int pkt_len, u_int num_pkts, u_int pkt_overhead) {
ethStats.incStats(eth_proto, num_pkts, pkt_len, pkt_overhead);
ndpiStats.incStats(ndpi_proto, , , , pkt_len);
pktStats.incStats(pkt_len);
if(lastSecUpdate == ) lastSecUpdate = when; else if(lastSecUpdate != when) updateSecondTraffic(when);
};
//因涉及到pcap抓包,所以有些常量是pcap定义的
#define DLT_NULL 0 /* BSD loopback encapsulation */
#define DLT_EN10MB 1 /* Ethernet (10Mb) */
#define DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */
#define DLT_AX25 3 /* Amateur Radio AX.25 */
#define DLT_PRONET 4 /* Proteon ProNET Token Ring */
#define DLT_CHAOS 5 /* Chaos */
#define DLT_IEEE802 6 /* 802.5 Token Ring */
#define DLT_ARCNET 7 /* ARCNET, with BSD-style header */
#define DLT_SLIP 8 /* Serial Line IP */
#define DLT_PPP 9 /* Point-to-point Protocol */
#define DLT_FDDI 10 /* FDDI */ /*
* These are types that are different on some platforms, and that
* have been defined by <net/bpf.h> for ages. We use #ifdefs to
* detect the BSDs that define them differently from the traditional
* libpcap <net/bpf.h>
*
* XXX - DLT_ATM_RFC1483 is 13 in BSD/OS, and DLT_RAW is 14 in BSD/OS,
* but I don't know what the right #define is for BSD/OS.
*/
#define DLT_ATM_RFC1483 11 /* LLC-encapsulated ATM */ #ifdef __OpenBSD__
#define DLT_RAW 14 /* raw IP */
#else
#define DLT_RAW 12 /* raw IP */
#endif
												

最新文章

  1. php sprintf 函数的用法
  2. android获得ImageView图片的等级
  3. Linux下安裝Oracle database內核參數設置
  4. linux配置hosts
  5. Linux下Awk详解(转载)
  6. 【转】CSS z-index 属性的使用方法和层级树的概念
  7. [Flex] ButtonBar系列——控制ButtonBar菜单是否可用
  8. hibernate中增加annotation @后不提示信息【转】
  9. 关于bootstrap列偏移的两种方式
  10. Python变量运算字符串等
  11. AJAX 中JSON 和JSONP 的区别 以及请求原理
  12. ORACLE索引监控的简单使用
  13. 关于js中单双引号以及转义符的理解
  14. 自定义注解,andjdk提供的元注解
  15. HDU--1213并查集
  16. ClearCase创建视图与基本命令
  17. shiro 错误登陆次数限制
  18. 【题解】Luogu P2057 [SHOI2007]善意的投票
  19. android 应用程序中执行Linux 命令
  20. smb文件共享实现

热门文章

  1. Hadoop集成
  2. python读写protobuf
  3. Pi
  4. HTML5小游戏UI美化版
  5. jquery.validate.unobtrusive.js实现气泡提示mvc错误
  6. 一步步学习Python-django开发-添加后台管理
  7. 百万行mysql数据库优化(补充)
  8. hdu1043-素数回文
  9. 捕鱼达人代码例子下载地址 Win版
  10. Linux负载均衡软件LVS之三(配置篇)