/****************************************************************************
   Program:     $Id: util.c,v 1.5 2006/11/19 21:01:17 rbeverly Exp $
   Author:      Rob Beverly <rbeverly@mit.edu>
   Date:        $Date: 2006/11/19 21:01:17 $
   Description: Spoofer utility routines
****************************************************************************/
#include "spoof.h"
#include <stdarg.h>

/* Useful error condition functions */
void fatal(char *fmt, ...) {
    va_list ap;
    fflush(stdout);
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    exit(-1);
}

/* Useful error condition functions */
void pfatal(char *err) {
    fflush(stdout);
    perror(err);
    exit(-1);
}

/* A ntohl() for big endian architectures who null macro it */
unsigned long byte32Swap(unsigned long in) {
    return ( (in & 0x000000ff)<<24 | (in & 0x0000ff00)<<8 |
             (in & 0x00ff0000)>>8 | (in & 0xff000000)>>24 );
}
unsigned short byte16Swap(unsigned short in) {
    return ( (in & 0x00ff)<<8 | (in & 0xff00)>>8 );
}

/* Check Priviledges */
void checkroot() {
#ifndef _WIN32
    if (getuid() != 0) {
        fatal("** Must be run as root!\n");
    }
#endif
}

/* Windows socket API startup */
void win_init() {
#ifdef _WIN32
    WSADATA wsd;
    if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0) {
        winfatal("** WSAStartup() failed");
    }
#endif
}

/* Hi there */
void banner() {
    printf(">> IP Spoofing Tester v0.%d\n", VERSION);
    printf(">> Copyright (c) 2004-2006\n");
    printf(">> Rob Beverly <rbeverly at mit dot edu>\n");
    printf(">> http://%s\n", REPORT_HOST);
    printf(">>\n");
}

/* Portable microsecond sleep */
void spoofSleep() {
    unsigned int gap;

    /* random interpacket delay 0-0.5s */
    gap = (unsigned int)((double)rand() / ((double)RAND_MAX + 1) * 500);
#ifdef _WIN32
    Sleep(gap);
#else
    usleep(gap * 1000);
#endif
}


syntax highlighted by Code2HTML, v. 0.9.1