I forgot to provide my code in the last post, Here is the code
--wpcap.c--
pcap_dumper_t* CDECL wine_pcap_dump_open(pcap_t *p, const char *fname)
{
UNICODE_STRING nt_name, dospathW;
ANSI_STRING fname_dos;
ANSI_STRING fname_unix;
NTSTATUS res;
RtlInitAnsiString(&fname_dos, fname);
res = RtlAnsiStringToUnicodeString(&dospathW, &fname_dos, TRUE);
printf("RtlAnsiStringToUnicodeString retval = %X\n", res);
if(res == STATUS_INVALID_PARAMETER_2)
{
SetLastError(ERROR_FILENAME_EXCED_RANGE);
return NULL;
}
if(!RtlDosPathNameToNtPathName_U(dospathW.Buffer, &nt_name, NULL, NULL))
{
printf("RtlDosPathNameToNtPathName_U retval = %X\n", res);
RtlFreeUnicodeString(&dospathW);
SetLastError(ERROR_FILENAME_EXCED_RANGE);
return NULL;
}
res = wine_nt_to_unix_file_name(&nt_name, &fname_unix, FILE_OPEN_IF, FALSE);
printf("VOID_DEBUG: Nt FileName is %s\n", wine_dbgstr_w(nt_name.Buffer));
printf("wine_nt_to_unix_file_name retval = %X\n", res);
if(res == STATUS_NO_SUCH_FILE)
{
SetLastError(ERROR_SUCCESS);
}
else if(res == STATUS_OBJECT_NAME_INVALID)
{
RtlFreeUnicodeString(&dospathW);
SetLastError(ERROR_INVALID_NAME);
return NULL;
}
else if(res == STATUS_OBJECT_NAME_NOT_FOUND || res == STATUS_OBJECT_PATH_NOT_FOUND)
{
RtlFreeUnicodeString(&dospathW);
SetLastError(ERROR_PATH_NOT_FOUND);
return NULL;
}
else
{
SetLastError(ERROR_FILE_EXISTS);
}
RtlFreeUnicodeString(&nt_name);
RtlFreeUnicodeString(&dospathW);
return pcap_dump_open(p, fname_unix.Buffer);
}
--wpcap.c--
--test.c--
//#define HAVE_REMOTE
#include <pcap.h>
#include <string.h>
#define PCAP_SRC_IF_STRING "rpcap://"
#define PCAP_OPENFLAG_PROMISCUOUS 1
/* 回调函数原型 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
void packet_handler_1(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data);
int main(int argc, char **argv)
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_dumper_t *dumpfile;
/* 检查程序输入参数 */
if(argc != 2)
{
printf("usage: %s filename", argv[0]);
return -1;
}
/* 获取本机设备列表 */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
/* 打印列表 */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
if(i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
/* 释放列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 跳转到选中的适配器 */
for(d=alldevs, i=0; i< inum-1 ; d=d->next, i++);
/* 打开适配器 */
if ( (adhandle= pcap_open_live(d->name, // 设备名
65536, // 要捕捉的数据包的部分
// 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
1, // 混杂模式
1000, // 读取超时时间
errbuf // 错误缓冲池
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 打开堆文件 */
//Change the last byte to a not NULL character
char* str = argv[1];
char *p = str;
// while(*(str++) != '\0');
// str--;
// *str = 'H';
printf("Path is now become %s\n", p);
//printf("ERRCODE %X\n", GetLastError());
//while(malloc(10));
dumpfile = pcap_dump_open(adhandle, str);
printf("ERRCODE %X\n", GetLastError());
if(dumpfile==NULL)
{
fprintf(stderr,"\nError opening output file\n");
return -1;
}
printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);
/* 释放设备列表 */
pcap_freealldevs(alldevs);
/* 开始捕获 */
printf("Catch Loop\n");
pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);
return 0;
}
/* 回调函数,用来处理数据包 */
void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
/* 保存数据包到堆文件 */
pcap_dump(dumpfile, header, pkt_data);
}
--test.c--