/*
 * This file is part of RTRlib.
 *
 * This file is subject to the terms and conditions of the MIT license.
 * See the file LICENSE in the top level directory for more details.
 *
 * Website: http://rtrlib.realmv6.org/
*/

#include <arpa/inet.h>
#include <assert.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include "rtrlib/lib/utils.h"

#ifdef __MACH__
#include <mach/mach_time.h>
static double timeconvert = 0.0;
#endif

int rtr_get_monotonic_time(time_t *seconds)
{
#ifdef __MACH__
  if (timeconvert == 0.0) {
    mach_timebase_info_data_t timeBase;
    (void)mach_timebase_info( &timeBase );
    timeconvert = (double)timeBase.numer /
                  (double)timeBase.denom /
                  1000000000.0;
  }
  *seconds = (time_t) mach_absolute_time() * timeconvert;
#else
    struct timespec time;
    if(clock_gettime(CLOCK_MONOTONIC, &time) == -1)
        return -1;
    *seconds = time.tv_sec;
    if((time.tv_nsec *  1000000000) >=5)
        *seconds +=1;
#endif
    return 0;
}

uint32_t rtr_get_bits(const uint32_t val, const uint8_t from, const uint8_t number)
{
    assert(number < 33);
    assert(number > 0);

    uint32_t mask = ~0;
    if(number != 32)
        mask = ~(mask >> number);

    mask >>= from;
    return (mask & val);
}
