日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

【转载】linux环境下tcpdump源代码分析

發布時間:2025/3/15 linux 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转载】linux环境下tcpdump源代码分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

linux環境下tcpdump源代碼分析

 原文時間?2013-10-11 13:13:02??CSDN博客 原文鏈接 ?http://blog.csdn.net/han_dawei/article/details/12615199 主題?Tcpdump?源碼分析  作者:韓大衛 @ 吉林師范大學 tcpdump.c 是tcpdump 工具的main.c, 本文旨對tcpdump的框架有簡單了解,只展示linux平臺使用的一部分核心代碼。 Tcpdump 的使用目的就是打印出指定條件的報文,即使有再多的正則表達式作為過濾條件。所以只要懂得tcpdump -nXXi eth0 的實現原理即可。 進入main之前,先看一些頭文件 netdissect.h里定義了一個數據結構struct netdissect_options來描述tcdpump支持的所有參數動作,每一個參數有對應的flag, 在tcpdump 的main 里面, 會根據用戶的傳入的參數來增加相應flag數值, 最后根據這些flag數值來實現特定動作。各個參數含義請參考源代碼注釋。 struct netdissect_options { int ndo_aflag; /* translate network and broadcast addresses */ //打印出以太網頭部 int ndo_eflag; /* print ethernet header */ int ndo_fflag; /* don't translate "foreign" IP address */ int ndo_Kflag; /* don't check TCP checksums */ //不將地址轉換為名字 int ndo_nflag; /* leave addresses as numbers */ int ndo_Nflag; /* remove domains from printed host names */ int ndo_qflag; /* quick (shorter) output */ int ndo_Rflag; /* print sequence # field in AH/ESP*/ int ndo_sflag; /* use the libsmi to translate OIDs */ int ndo_Sflag; /* print raw TCP sequence numbers */ // 報文到達時間 int ndo_tflag; /* print packet arrival time */ int ndo_Uflag; /* "unbuffered" output of dump files */ int ndo_uflag; /* Print undecoded NFS handles */ //詳細信息 int ndo_vflag; /* verbose */ // 十六進制打印報文 int ndo_xflag; /* print packet in hex */ // 十六進制和ASCII碼打印報文 int ndo_Xflag; /* print packet in hex/ascii */ //ASCII碼顯示打印報文 int ndo_Aflag; /* print packet only in ascii observing TAB, * LF, CR and SPACE as graphical chars */ ... //默認的打印函數 void (*ndo_default_print)(netdissect_options *, register const u_char *bp, register u_int length); void (*ndo_info)(netdissect_options *, int verbose); ... } interface.h 接口頭文件,定義了一堆宏就為了方便調用struct netdissect_options里的成員。 #ifndef NETDISSECT_REWORKED extern netdissect_options *gndo; ... #define nflag gndo->ndo_nflag ... #define tflag gndo->ndo_tflag ... #define vflag gndo->ndo_vflag #define xflag gndo->ndo_xflag #define Xflag gndo->ndo_Xflag ... #endif tcpdump.c int main(int argc, char **argv) { register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName; pcap_handler callback; int type; struct bpf_program fcode; struct print_info printinfo; ... //netdissect_options中一些參數初始化 gndo->ndo_Oflag=1; gndo->ndo_Rflag=1; gndo->ndo_dlt=-1; gndo->ndo_default_print=ndo_default_print; gndo->ndo_printf=tcpdump_printf; gndo->ndo_error=ndo_error; gndo->ndo_warning=ndo_warning; gndo->ndo_snaplen = DEFAULT_SNAPLEN; ... opterr = 0; while ( /*經典的getopt框架。 字符數組為tcpdump 支持的全部參數。可以看到, 參數x, X,t這些參數后面沒有:或::, 這說明這些參數會產生疊加的效果。 */ (op = getopt(argc, argv, "aA" B_FLAG "c:C:d" D_FLAG "eE:fF:G:i:" I_FLAG "KlLm:M:nNOpqr:Rs:StT:u" U_FLAG "vw:W:xXy:Yz:Z:")) != -1) switch (op) { ... //case 里面的處理大多相似,以下僅用-i,-X,-x做例。 //-i 參數用來指定網口 case 'i': if (optarg[0] == '0' && optarg[1] == 0) error("Invalid adapter index"); device = optarg; break; … //-x 為以十六進制打印報文,如使用-xxxflag數值為2,后面根據xflag>1來打印出鏈路層頭部 case 'x': ++xflag; ++suppress_default_print; break; case 'X': ++Xflag; ++suppress_default_print; break; //case 'n', case 'A'等操作類似如上 ... } ... /*展開核心代碼前處理信號,信號處理函數cleanup會調用info()來打印當用戶按ctrl+c等發送中止信號時tcpdump顯示已處理報文的統計信息。 3 packets captured 3 packets received by filter 0 packets dropped by kernel */ (void)setsignal(SIGPIPE, cleanup); (void)setsignal(SIGTERM, cleanup); (void)setsignal(SIGINT, cleanup); (void)setsignal(SIGCHLD, child_cleanup); ... //-r 參數讀取指定文件, 在此忽略 if (RFileName != NULL) { ... } else { //如果沒有-i 參數來指定網絡接口, 那么調用 pcap_lookupdev()來尋找可用的網絡接口 if (device == NULL) { device = pcap_lookupdev(ebuf); if (device == NULL) error("%s", ebuf); } /*pcap_open_live() 定義為: pcap_t *pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf) device為要打開的指定設備 snaplen為最大報文長度, 由-s 指定. 默認為65536. Promise 為是否要將網口配置為混雜模式, 由-p 指定,!Pflag:默認為是。 to_ms 為超時時間。 *ebuf 為傳遞錯誤信息使用。 函數返回捕獲報文的句柄。 */ *ebuf = '\0'; pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf); if (pd ==

轉載于:https://www.cnblogs.com/Recan/p/6034520.html

總結

以上是生活随笔為你收集整理的【转载】linux环境下tcpdump源代码分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。