⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 group__wpcap__tut8.html

📁 Winpcap是一个强大的网络开发库
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    <span class="keywordtype">int</span> <a class="code" href="structpcap__pkthdr.html#ac5771ed2efd92508bb4fe650f2ce7d7" title="length of portion present">caplen</a>, sync;    u_int res;    <a class="code" href="structpcap__send__queue.html" title="A queue of raw packets that will be sent to the network with pcap_sendqueue_transmit()...">pcap_send_queue</a> *squeue;    <span class="keyword">struct </span><a class="code" href="structpcap__pkthdr.html" title="Header of a packet in the dump file.">pcap_pkthdr</a> *pktheader;    u_char *pktdata;    <span class="keywordtype">float</span> cpu_time;    u_int npacks = 0;    errno_t fopen_error;    <span class="comment">/* Check the validity of the command line */</span>    <span class="keywordflow">if</span> (argc &lt;= 2 || argc &gt;= 5)    {        usage();        <span class="keywordflow">return</span>;    }            <span class="comment">/* Retrieve the length of the capture file */</span>    fopen_error = fopen_s(&amp;capfile, argv[1],<span class="stringliteral">"rb"</span>);    <span class="keywordflow">if</span>(fopen_error != 0){        printf(<span class="stringliteral">"Error opening the file, errno %d.\n"</span>, fopen_error);        <span class="keywordflow">return</span>;    }        fseek(capfile , 0, SEEK_END);    caplen= ftell(capfile)- <span class="keyword">sizeof</span>(<span class="keyword">struct </span><a class="code" href="structpcap__file__header.html" title="Header of a libpcap dump file.">pcap_file_header</a>);    fclose(capfile);                <span class="comment">/* Chek if the timestamps must be respected */</span>    <span class="keywordflow">if</span>(argc == 4 &amp;&amp; argv[3][0] == <span class="charliteral">'s'</span>)        sync = TRUE;    <span class="keywordflow">else</span>        sync = FALSE;    <span class="comment">/* Open the capture */</span>    <span class="comment">/* Create the source string according to the new WinPcap syntax */</span>    <span class="keywordflow">if</span> ( <a class="code" href="group__wpcapfunc.html#ga3111e10f930a9772a32a922b26948b0" title="Accept a set of strings (host name, port, ...), and it returns the complete source...">pcap_createsrcstr</a>( source,         <span class="comment">// variable that will keep the source string</span>                            <a class="code" href="group__remote__source__ID.html#g9188ad0fc0d12fc51d1e9f5e78bf9440" title="Internal representation of the type of source in use (file, remote/local interface)...">PCAP_SRC_FILE</a>,  <span class="comment">// we want to open a file</span>                            NULL,           <span class="comment">// remote host</span>                            NULL,           <span class="comment">// port on the remote host</span>                            argv[1],        <span class="comment">// name of the file we want to open</span>                            errbuf          <span class="comment">// error buffer</span>                            ) != 0)    {        fprintf(stderr,<span class="stringliteral">"\nError creating a source string\n"</span>);        <span class="keywordflow">return</span>;    }        <span class="comment">/* Open the capture file */</span>    <span class="keywordflow">if</span> ( (indesc= <a class="code" href="group__wpcapfunc.html#g2b64c7b6490090d1d37088794f1f1791" title="Open a generic source in order to capture / send (WinPcap only) traffic.">pcap_open</a>(source, 65536, <a class="code" href="group__remote__open__flags.html#g9134ce51a9a6a7d497c3dee5affdc3b9" title="Defines if the adapter has to go in promiscuous mode.">PCAP_OPENFLAG_PROMISCUOUS</a>, 1000, NULL, errbuf) ) == NULL)    {        fprintf(stderr,<span class="stringliteral">"\nUnable to open the file %s.\n"</span>, source);        <span class="keywordflow">return</span>;    }    <span class="comment">/* Open the output adapter */</span>    <span class="keywordflow">if</span> ( (outdesc= <a class="code" href="group__wpcapfunc.html#g2b64c7b6490090d1d37088794f1f1791" title="Open a generic source in order to capture / send (WinPcap only) traffic.">pcap_open</a>(argv[2], 100, <a class="code" href="group__remote__open__flags.html#g9134ce51a9a6a7d497c3dee5affdc3b9" title="Defines if the adapter has to go in promiscuous mode.">PCAP_OPENFLAG_PROMISCUOUS</a>, 1000, NULL, errbuf) ) == NULL)    {        fprintf(stderr,<span class="stringliteral">"\nUnable to open adapter %s.\n"</span>, source);        <span class="keywordflow">return</span>;    }    <span class="comment">/* Check the MAC type */</span>    <span class="keywordflow">if</span> (<a class="code" href="group__wpcapfunc.html#g64c019f851f7da6892d51cca15f12ace" title="Return the link layer of an adapter.">pcap_datalink</a>(indesc) != <a class="code" href="group__wpcapfunc.html#g64c019f851f7da6892d51cca15f12ace" title="Return the link layer of an adapter.">pcap_datalink</a>(outdesc))    {        printf(<span class="stringliteral">"Warning: the datalink of the capture differs from the one of the selected interface.\n"</span>);        printf(<span class="stringliteral">"Press a key to continue, or CTRL+C to stop.\n"</span>);        getchar();    }    <span class="comment">/* Allocate a send queue */</span>    squeue = <a class="code" href="group__wpcapfunc.html#gb940e69631b7cc7f2232a69ea02b86d9" title="Allocate a send queue.">pcap_sendqueue_alloc</a>(caplen);    <span class="comment">/* Fill the queue with the packets from the file */</span>    <span class="keywordflow">while</span> ((res = <a class="code" href="group__wpcapfunc.html#g439439c2eae61161dc1efb1e03a81133" title="Read a packet from an interface or from an offline capture.">pcap_next_ex</a>( indesc, &amp;pktheader, &amp;pktdata)) == 1)    {        <span class="keywordflow">if</span> (<a class="code" href="group__wpcapfunc.html#g4c57ea320d71dbfe55c5665af9db1297" title="Add a packet to a send queue.">pcap_sendqueue_queue</a>(squeue, pktheader, pktdata) == -1)        {            printf(<span class="stringliteral">"Warning: packet buffer too small, not all the packets will be sent.\n"</span>);            <span class="keywordflow">break</span>;        }        npacks++;    }    <span class="keywordflow">if</span> (res == -1)    {        printf(<span class="stringliteral">"Corrupted input file.\n"</span>);        <a class="code" href="group__wpcapfunc.html#g72624f7a9932cc2124abf661001e0aa4" title="Destroy a send queue.">pcap_sendqueue_destroy</a>(squeue);        <span class="keywordflow">return</span>;    }    <span class="comment">/* Transmit the queue */</span>        cpu_time = (float)clock ();    <span class="keywordflow">if</span> ((res = <a class="code" href="group__wpcapfunc.html#ga4d55eb047a1cccc0e28397ce04ee097" title="Send a queue of raw packets to the network.">pcap_sendqueue_transmit</a>(outdesc, squeue, sync)) &lt; squeue-&gt;<a class="code" href="structpcap__send__queue.html#b4853420730b2b590cd3ad0e72fa80ca" title="Current size of the queue, in bytes.">len</a>)    {        printf(<span class="stringliteral">"An error occurred sending the packets: %s. Only %d bytes were sent\n"</span>, <a class="code" href="group__wpcapfunc.html#g81305cb154e4497e95bbb9b708631a3a" title="return the error text pertaining to the last pcap library error.">pcap_geterr</a>(outdesc), res);    }        cpu_time = (clock() - cpu_time)/CLK_TCK;        printf (<span class="stringliteral">"\n\nElapsed time: %5.3f\n"</span>, cpu_time);    printf (<span class="stringliteral">"\nTotal packets generated = %d"</span>, npacks);    printf (<span class="stringliteral">"\nAverage packets per second = %d"</span>, (<span class="keywordtype">int</span>)((<span class="keywordtype">double</span>)npacks/cpu_time));    printf (<span class="stringliteral">"\n"</span>);    <span class="comment">/* free the send queue */</span>    <a class="code" href="group__wpcapfunc.html#g72624f7a9932cc2124abf661001e0aa4" title="Destroy a send queue.">pcap_sendqueue_destroy</a>(squeue);    <span class="comment">/* Close the input file */</span>    <a class="code" href="group__wpcapfunc.html#ga45a5e1a4ba9925bb3586dcbeec78560" title="close the files associated with p and deallocates resources.">pcap_close</a>(indesc);    <span class="comment">/* </span><span class="comment">     * lose the output adapter </span><span class="comment">     * IMPORTANT: remember to close the adapter, otherwise there will be no guarantee that all the </span><span class="comment">     * packets will be sent!</span><span class="comment">     */</span>    <a class="code" href="group__wpcapfunc.html#ga45a5e1a4ba9925bb3586dcbeec78560" title="close the files associated with p and deallocates resources.">pcap_close</a>(outdesc);    <span class="keywordflow">return</span>;}<span class="keywordtype">void</span> usage(){        printf(<span class="stringliteral">"\nSendcap, sends a libpcap/tcpdump capture file to the net. Copyright (C) 2002 Loris Degioanni.\n"</span>);    printf(<span class="stringliteral">"\nUsage:\n"</span>);    printf(<span class="stringliteral">"\t sendcap file_name adapter [s]\n"</span>);    printf(<span class="stringliteral">"\nParameters:\n"</span>);    printf(<span class="stringliteral">"\nfile_name: the name of the dump file that will be sent to the network\n"</span>);    printf(<span class="stringliteral">"\nadapter: the device to use. Use \"WinDump -D\" for a list of valid devices\n"</span>);    printf(<span class="stringliteral">"\ns: if present, forces the packets to be sent synchronously, i.e. respecting the timestamps in the dump file. This option will work only under Windows NTx.\n\n"</span>);    exit(0);}</pre></div><p><a class="el" href="group__wpcap__tut7.html">&lt;&lt;&lt; Previous</a> <a class="el" href="group__wpcap__tut9.html">Next &gt;&gt;&gt;</a> </div><hr><p align="right"><img border="0" src="winpcap_small.gif" align="absbottom" width="91" height="27">documentation. Copyright (c) 2002-2005 Politecnico di Torino. Copyright (c) 2005-2008CACE Technologies. All rights reserved.</p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -