Esempio n. 1
0
static void check_timespec_diff(void)
{
    unsigned long long diff;
    struct timespec start, end;

    timespec_set(start, 1, 30000);

    timespec_set(end, start.tv_sec, start.tv_nsec + 10000);
    timespec_diff(start, end, &diff);

    printf("Checking 10000 == %llu\n", diff);

    if (diff != 10000) {
        printf("Failure!\n");
        exit(FAIL);
    }

    timespec_set(end, start.tv_sec + 5, start.tv_nsec - 5000);
    timespec_diff(start, end, &diff);

    printf("Checking 4999995000 == %llu\n", diff);

    if (diff != 4999995000llu) {
        printf("Failure!\n");
        exit(FAIL);
    }
}
Esempio n. 2
0
    header make_header()
    {
        //you can customize header here
        header rs;
        //timestamp
        timespec dr,now;
        timespec_set(&dr,duration_);
        clock_gettime(CLOCK_REALTIME,&now);
        timer_add(&now,&dr,&rs.timestamp);
        //quantum
        rs.quantum = 1;

        //make ehternet header
        static uint32_t dst = 0;
        static uint32_t src = UINT32_MAX-1;
        rs.eth = new sniff_ethernet;
        sniff_ethernet *eth = rs.eth;
        uint32_t *mac_dst = (uint32_t *)(eth->ether_dhost + 2);
        uint32_t *mac_src = (uint32_t *)(eth->ether_shost + 2);
        *mac_dst = htonl(dst);
        *mac_src = htonl(src);
        eth->ether_dhost[2] = 0x0c;
        eth->ether_shost[0] = 0x00;
        eth->ether_shost[1] = 0x02;
        eth->ether_shost[2] = 0xb3;
        //make ip header
        rs.ip = new sniff_ip;
        rs.ip->ip_src.s_addr = htonl(src);
        rs.ip->ip_dst.s_addr = htonl(dst/2);
        //make tcp header
        rs.tcp = new sniff_tcp;
        rs.tcp->th_dport = htons(10071);
        rs.tcp->th_sport = htons(10087);
        --src;
        ++dst;
        return rs;
    }
Esempio n. 3
0
int timeout_set(const double seconds)
{
    struct timespec   now, then;
    struct itimerspec when;
    double            next;
    int               timeout, i;

    /* Timeout must be in the future. */
    if (seconds <= 0.0)
        return -1;

    /* Get current time, */
    if (clock_gettime(CLOCK_REALTIME, &now))
        return -1;

    /* and calculate when the timeout should fire. */
    then = now;
    timespec_add(&then, seconds);

    /* Find an unused timeout. */
    for (timeout = 0; timeout < TIMEOUTS; timeout++)
        if (!(__sync_fetch_and_or(&timeout_state[timeout], TIMEOUT_USED) & TIMEOUT_USED))
            break;

    /* No unused timeouts? */
    if (timeout >= TIMEOUTS)
        return -1;

    /* Clear all but TIMEOUT_USED from the state, */
    __sync_and_and_fetch(&timeout_state[timeout], TIMEOUT_USED);

    /* update the timeout details, */
    timeout_time[timeout] = then;

    /* and mark the timeout armable. */
    __sync_or_and_fetch(&timeout_state[timeout], TIMEOUT_ARMED);

    /* How long till the next timeout? */
    next = seconds;
    for (i = 0; i < TIMEOUTS; i++)
        if ((__sync_fetch_and_or(&timeout_state[i], 0) & (TIMEOUT_USED | TIMEOUT_ARMED | TIMEOUT_PASSED)) == (TIMEOUT_USED | TIMEOUT_ARMED)) {
            const double secs = timespec_diff(timeout_time[i], now);
            if (secs >= 0.0 && secs < next)
                next = secs;
        }

    /* Calculate duration when to fire the timeout next, */
    timespec_set(&when.it_value, next);
    when.it_interval.tv_sec = 0;
    when.it_interval.tv_nsec = 0L;

    /* and arm the timer. */
    if (timer_settime(timeout_timer, 0, &when, NULL)) {
        /* Failed. */
        __sync_and_and_fetch(&timeout_state[timeout], 0);
        return -1;
    }

    /* Return the timeout number. */
    return timeout;
}