Emsniffer(还没完成,正在做)
来源:互联网 发布:微博短文情感数据集 编辑:程序博客网 时间:2024/04/28 19:58
Emsniffer作用如下,能够抓包并存到文件里(实现);存储包文件的大小能够设定(为实现现阶段)。
源码如下,有相关需求的同志们共同学习,如果可以的话,提出建议更好。
/**********************************************************************
* file: emsniffer.c
* date: 2010,7,19
* Author: yujiliang
* Last Modified:2010,7,21 ,yujiliang
*
* To sniffer all the packets from a Network Interface Card, and save them
* to a file.
* Use libpcap 0.5.2 or version after
**********************************************************************/
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <string.h>
#define NIC_NAME "eth0" /* the NIC to grab */
char errbuf[PCAP_ERRBUF_SIZE]; /* for writting pcap error */
pcap_t * descr; /* pointer to NIC */
pcap_dumper_t * p; /* pointer to pcap dump file for writing */
char filepath[20]; /* file for save dump packets */
struct stat dumpfile_info;
/* do this after press Ctrl-c or some other reasons cause program to stop */
void cleanup()
{
/* Close the pcap device */
pcap_close(descr);
/* close dump file */
pcap_dump_close(p);
stat(filepath, &dumpfile_info); /* compute dumpfile's size */
printf("Dumpfile's size is %d kbytes/n", dumpfile_info.st_size);
fprintf(stderr,"dump over/n");
exit( 0 );
}
/* sniffer & save packets to dumpfile */
void emsniffer(char *dump_file_path, int limit_storage_space) /* limit_storage kbytes function not OK yet */
{
/* find the WAN NIC device */
/* open device for reading and set it in promiscuous mode */
descr = pcap_open_live(NIC_NAME ,BUFSIZ,1,-1,errbuf);
if(descr == NULL)
{
fprintf(stderr,"pcap_open_live(): %s/n",errbuf); exit(1);
}
/* exit & close dumpfile & left promiscuous mode when got ctrl-c */
signal(SIGTERM, cleanup);
signal(SIGINT, cleanup);
signal(SIGQUIT, cleanup);
signal(SIGABRT, cleanup);
/* open the dumpfile for writting packets */
p = pcap_dump_open(descr, dump_file_path);
if (p == NULL)
{
fprintf(stderr,"pcap_dump_open: %s", pcap_geterr(descr)); exit(1);
}
fprintf(stderr,"dump packets begin/n");
/* write all the packets into the dumpfile until press ctrl-c */ /*HAVE BETTER SOLUTION ,TIME NEEDED */
if(pcap_loop(descr, -1, pcap_dump, (u_char *)p) < 0)
{
fprintf(stderr,"pcap_loop: %s", pcap_geterr(descr)); exit(1);
}
}
int main(int argc, char* argv[]) /* ./emsniffer [dump_file_path] [limit_storage_space] */
{
int limitstoragespace;
strcpy(filepath,argv[1]);
limitstoragespace = atoi(argv[2]);
emsniffer(filepath,limitstoragespace);
return 0;
}
以后limit_storage_space的功能做好会加上。
- Emsniffer(还没完成,正在做)
- 还没
- 要做毕业设计了,还没思路呢!!!
- 近好郁闷呀,老板交给的任务还没完成,5555~
- 一个js游戏引擎,没完成呢还,自己写的
- 作为新手接触安卓遇到的问题~~还没完成~~~
- 没做!
- 光缆还没修好
- 为何还没谈恋爱?
- 还没来得及。。。
- 标题还没写完
- 还没面上了
- android还没入门
- 还没起床
- 还没做完
- Java【总结】JFreeChart——刚做了几个demo,还没开始写
- 今天周五了。一大早还没怎么做东西就快12点了呢
- 有点困惑,一个老程序员可以做什么,没想清楚,还得努力学习
- 阻止构造函数转换
- 海量数据的处理方法
- .net/C# 实现汉字到拼音转换
- 内存用光了怎么办
- 内存用光了怎么办
- Emsniffer(还没完成,正在做)
- 如何用SQL注入攻击登陆界面
- gsdgsd
- 运算符重载
- JFreeChart中文乱码和文字模糊问题的通用解决方案
- 构造对象数组
- 给ATL控件添加安全接口
- 史上最全的C位域总结(转)
- 管理者的三层境界- 管事、管人、管组织