Exemplo n.º 1
0
int timeInit()
{
  __int64 freq;
  if( QueryPerformanceFrequency( (LARGE_INTEGER *) &freq ) )
  {
    QueryPerformanceCounter( (LARGE_INTEGER *) &timerStart );
    timerRes = 1000000.0/(double)freq;
    return 1;
  }
  else
  {
    traceWarning("no high performance counter found\n");
    return 0;
  }
}
Exemplo n.º 2
0
/* Read key control file and return the number of specs stored or a negative
 * error code.
 *
 * As the specs are read in the from and until time values are compared to
 * present time. Only those keys which are valid are stored.
 */
int n2n_read_keyfile(n2n_cipherspec_t *specs,    /* fill out this array of cipherspecs */
                     size_t numspecs,            /* number of slots in the array. */
                     const char *ctrlfile_path)  /* path to control file */
{
    /* Each line contains one cipherspec. */

    int       retval = 0;
    FILE     *fp = NULL;
    size_t    idx = 0;
    time_t    now = time(NULL);

    traceDebug("Reading '%s'\n", ctrlfile_path);

    fp = fopen(ctrlfile_path, "r");
    if (fp)
    {
        /* Read the file a line a time with fgets. */
        char line[N2N_KEYFILE_LINESIZE];
        size_t lineNum = 0;

        while (idx < numspecs)
        {
            n2n_cipherspec_t *k = &(specs[idx]);
            fgets(line, N2N_KEYFILE_LINESIZE, fp);
            ++lineNum;

            if (strlen(line) > 1)
            {
                if (0 == parseKeyLine(k, line))
                {
                    if (k->valid_until > now)
                    {
                        traceInfo(" --> [%u] from %lu, until %lu, transform=%hu, data=%s\n",
                                   idx, k->valid_from, k->valid_until, k->t, k->opaque);

                        ++retval;
                        ++idx;
                    }
                    else
                    {
                        traceInfo(" --X [%u] from %lu, until %lu, transform=%hu, data=%s\n",
                                   idx, k->valid_from, k->valid_until, k->t, k->opaque);

                    }
                }
                else
                {
                    traceWarning("Failed to decode line %u\n", lineNum);
                }
            }

            if (feof(fp))
            {
                break;
            }

            line[0] = 0; /* this line has been consumed */
        }

        fclose(fp);
        fp = NULL;
    }
    else
    {
        traceError("Failed to open '%s'\n", ctrlfile_path);
        retval = -1;
    }

    return retval;
}