0

次のコード例を見てください

https://gist.github.com/3825444

/*
Testing arbitrary raw ip packets
works only if datagram is filled with 0
filling with anything else will not send any packets, or atleast wireshark does not detect anything
this is strange
*/

#include<stdio.h>
#include<string.h> //memset
#include<sys/socket.h>
#include<stdlib.h> //for exit(0);
#include<errno.h> //For errno - the error number
#include<netinet/tcp.h> //Provides declarations for tcp header
#include<netinet/ip.h>  //Provides declarations for ip header


int main (void)
{
    //Create a raw socket
    int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);

    if(s < 0)
    {
        perror("socket");
    }

    //Datagram to represent the packet
    char datagram[4096] , source_ip[32];

    struct sockaddr_in sin;

    strcpy(source_ip , "192.168.1.2");

    sin.sin_family = AF_INET;
    sin.sin_port = htons(80);
    sin.sin_addr.s_addr = inet_addr ("1.2.3.4");

    memset (datagram, 2 , 4096);    /* zero out the buffer */

    //IP_HDRINCL to tell the kernel that headers are included in the packet
    int one = 1;
    const int *val = &one;
    if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
    {
        printf ("Error setting IP_HDRINCL. Error number : %d . Error message : %s \n" , errno , strerror(errno));
        exit(0);
    }

    //Uncommend the loop if you want to flood :)
    while (1)
    {
        //Send the packet
        if (sendto (s,      /* our socket */
                    datagram,   /* the buffer containing headers and data */
                    512,    /* total length of our datagram */
                    0,      /* routing flags, normally always 0 */
                    (struct sockaddr *) &sin,   /* socket addr, just like in */
                    sizeof (sin)) < 0)      /* a normal send() */
        {
            perror("sendto");
        }
        //Data send successfully
        else
        {
            printf ("Packet Send \n");
        }
    }

    return 0;
}

上記のプログラムはパケットを生成しません。少なくとも、wireshark はパケットを検出しません。

ただし、データグラムが次のようにして0で埋められた場合

memset (datagram, 0 , 4096); /* zero out the buffer */

その後、大量のパケットが生成され、wireshark によって検出されます。

なぜそのような違いがあるのですか?

4

1 に答える 1

1

ヘッダーにゴミを入れています。2 の設定が失敗するよりも、0 の設定が成功する方が注目に値します。

于 2013-08-09T08:57:48.553 に答える