tweaks and cleanups

This commit is contained in:
cathugger 2018-05-31 14:01:33 +00:00
parent 25457c8741
commit 0208fc3fad

145
main.c
View file

@ -10,6 +10,7 @@
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <sodium/randombytes.h> #include <sodium/randombytes.h>
#include <sodium/utils.h>
#include "types.h" #include "types.h"
#include "likely.h" #include "likely.h"
@ -111,7 +112,7 @@ VEC_STRUCT(tstatsvec,struct tstatstruct);
#endif #endif
static void onionready(char *sname, const u8 *secret, const u8 *pubonion) static void onionready(char *sname,const u8 *secret,const u8 *pubonion)
{ {
if (endwork) if (endwork)
return; return;
@ -264,7 +265,7 @@ again:
// version byte // version byte
pk[PUBLIC_LEN + 2] = 0x03; pk[PUBLIC_LEN + 2] = 0x03;
// base32 // base32
strcpy(base32_to(&sname[direndpos],pk,PUBONION_LEN), ".onion"); strcpy(base32_to(&sname[direndpos],pk,PUBONION_LEN),".onion");
onionready(sname,secret,pubonion.raw); onionready(sname,secret,pubonion.raw);
pk[PUBLIC_LEN] = 0; pk[PUBLIC_LEN] = 0;
goto initseed; goto initseed;
@ -276,6 +277,8 @@ next:
end: end:
free(sname); free(sname);
POSTFILTER POSTFILTER
sodium_memzero(secret,sizeof(secret));
sodium_memzero(seed,sizeof(seed));
return 0; return 0;
} }
@ -315,7 +318,7 @@ static void *dofastwork(void *task)
#endif #endif
PREFILTER PREFILTER
memcpy(secret, skprefix, skprefixlen); memcpy(secret,skprefix,skprefixlen);
wpk[PUBLIC_LEN] = 0; wpk[PUBLIC_LEN] = 0;
memset(&pubonion,0,sizeof(pubonion)); memset(&pubonion,0,sizeof(pubonion));
memcpy(pubonion.raw,pkprefix,pkprefixlen); memcpy(pubonion.raw,pkprefix,pkprefixlen);
@ -392,6 +395,8 @@ initseed:
end: end:
free(sname); free(sname);
POSTFILTER POSTFILTER
sodium_memzero(secret,sizeof(secret));
sodium_memzero(seed,sizeof(seed));
return 0; return 0;
} }
@ -401,7 +406,7 @@ static void printhelp(FILE *out,const char *progname)
"Usage: %s filter [filter...] [options]\n" "Usage: %s filter [filter...] [options]\n"
" %s -f filterfile [options]\n" " %s -f filterfile [options]\n"
"Options:\n" "Options:\n"
"\t-h - print help\n" "\t-h - print help to stdout and quit\n"
"\t-f - instead of specifying filter(s) via commandline, specify filter file which contains filters separated by newlines\n" "\t-f - instead of specifying filter(s) via commandline, specify filter file which contains filters separated by newlines\n"
"\t-q - do not print diagnostic output to stderr\n" "\t-q - do not print diagnostic output to stderr\n"
"\t-x - do not print onion names\n" "\t-x - do not print onion names\n"
@ -419,9 +424,31 @@ static void printhelp(FILE *out,const char *progname)
"\t-T - do not reset statistics counters when printing\n" "\t-T - do not reset statistics counters when printing\n"
,progname,progname); ,progname,progname);
fflush(out); fflush(out);
exit(1);
} }
enum {
Q_ADDITIONAL = 100,
Q_UNRECOGNISED = 101,
Q_NOSTATISTICS = 102,
Q_FAILOPENOUTPUT = 103,
Q_FAILTHREAD = 104,
Q_FAILTIME = 105,
} ;
static void e_additional()
{
fprintf(stderr,"additional argument required\n");
exit(Q_ADDITIONAL);
}
#ifndef STATISTICS
static void e_nostatistics()
{
fprintf(stderr,"statistics support not compiled in\n");
exit(Q_NOSTATISTICS);
}
#endif
static void setworkdir(const char *wd) static void setworkdir(const char *wd)
{ {
free(workdir); free(workdir);
@ -430,7 +457,7 @@ static void setworkdir(const char *wd)
workdir = 0; workdir = 0;
workdirlen = 0; workdirlen = 0;
if (!quietflag) if (!quietflag)
fprintf(stderr, "unset workdir\n"); fprintf(stderr,"unset workdir\n");
return; return;
} }
unsigned needslash = 0; unsigned needslash = 0;
@ -439,7 +466,7 @@ static void setworkdir(const char *wd)
char *s = malloc(l + needslash + 1); char *s = malloc(l + needslash + 1);
if (!s) if (!s)
abort(); abort();
memcpy(s, wd, l); memcpy(s,wd,l);
if (needslash) if (needslash)
s[l++] = '/'; s[l++] = '/';
s[l] = 0; s[l] = 0;
@ -474,13 +501,15 @@ int main(int argc,char **argv)
setvbuf(stderr,0,_IONBF,0); setvbuf(stderr,0,_IONBF,0);
fout = stdout; fout = stdout;
pthread_mutex_init(&keysgenerated_mutex, 0); pthread_mutex_init(&keysgenerated_mutex,0);
pthread_mutex_init(&fout_mutex, 0); pthread_mutex_init(&fout_mutex,0);
const char *progname = argv[0]; const char *progname = argv[0];
if (argc <= 1) if (argc <= 1) {
printhelp(stderr,progname); printhelp(stderr,progname);
argc--, argv++; exit(1);
}
argc--; argv++;
while (argc--) { while (argc--) {
arg = *argv++; arg = *argv++;
@ -491,17 +520,19 @@ int main(int argc,char **argv)
++numargit; ++numargit;
if (*arg == '-') { if (*arg == '-') {
if (numargit > 1) { if (numargit > 1) {
fprintf(stderr, "unrecognised argument: -\n"); fprintf(stderr,"unrecognised argument: -\n");
exit(1); exit(Q_UNRECOGNISED);
} }
++arg; ++arg;
if (!*arg) if (!*arg)
ignoreargs = 1; ignoreargs = 1;
else if (!strcmp(arg, "help")) else if (!strcmp(arg,"help") || !strcmp(arg,"usage")) {
printhelp(stdout,progname); printhelp(stdout,progname);
exit(0);
}
else { else {
fprintf(stderr, "unrecognised argument: --%s\n", arg); fprintf(stderr,"unrecognised argument: --%s\n",arg);
exit(1); exit(Q_UNRECOGNISED);
} }
numargit = 0; numargit = 0;
} }
@ -510,15 +541,15 @@ int main(int argc,char **argv)
ignoreargs = 1; ignoreargs = 1;
continue; continue;
} }
else if (*arg == 'h') else if (*arg == 'h') {
printhelp(stdout,progname); printhelp(stdout,progname);
exit(0);
}
else if (*arg == 'f') { else if (*arg == 'f') {
if (argc--) if (argc--)
loadfilterfile(*argv++); loadfilterfile(*argv++);
else { else
fprintf(stderr, "additional argument required\n"); e_additional();
exit(1);
}
} }
else if (*arg == 'q') else if (*arg == 'q')
++quietflag; ++quietflag;
@ -527,44 +558,34 @@ int main(int argc,char **argv)
else if (*arg == 'o') { else if (*arg == 'o') {
if (argc--) if (argc--)
outfile = *argv++; outfile = *argv++;
else { else
fprintf(stderr, "additional argument required\n"); e_additional();
exit(1);
}
} }
else if (*arg == 'F') else if (*arg == 'F')
dirnameflag = 1; dirnameflag = 1;
else if (*arg == 'd') { else if (*arg == 'd') {
if (argc--) { if (argc--)
setworkdir(*argv++); setworkdir(*argv++);
} else
else { e_additional();
fprintf(stderr, "additional argument required\n");
}
} }
else if (*arg == 't' || *arg == 'j') { else if (*arg == 't' || *arg == 'j') {
if (argc--) if (argc--)
numthreads = atoi(*argv++); numthreads = atoi(*argv++);
else { else
fprintf(stderr, "additional argument required\n"); e_additional();
exit(1);
}
} }
else if (*arg == 'n') { else if (*arg == 'n') {
if (argc--) if (argc--)
numneedgenerate = (size_t)atoll(*argv++); numneedgenerate = (size_t)atoll(*argv++);
else { else
fprintf(stderr, "additional argument required\n"); e_additional();
exit(1);
}
} }
else if (*arg == 'N') { else if (*arg == 'N') {
if (argc--) if (argc--)
numwords = atoi(*argv++); numwords = atoi(*argv++);
else { else
fprintf(stderr, "additional argument required\n"); e_additional();
exit(1);
}
} }
else if (*arg == 'Z') else if (*arg == 'Z')
fastkeygen = 0; fastkeygen = 0;
@ -574,34 +595,29 @@ int main(int argc,char **argv)
#ifdef STATISTICS #ifdef STATISTICS
reportdelay = 10000000; reportdelay = 10000000;
#else #else
fprintf(stderr,"statistics support not compiled in\n"); e_nostatistics();
exit(1);
#endif #endif
} }
else if (*arg == 'S') { else if (*arg == 'S') {
#ifdef STATISTICS #ifdef STATISTICS
if (argc--) if (argc--)
reportdelay = (u64)atoll(*argv++) * 1000000; reportdelay = (u64)atoll(*argv++) * 1000000;
else { else
fprintf(stderr, "additional argument required\n"); e_additional();
exit(1);
}
#else #else
fprintf(stderr,"statistics support not compiled in\n"); e_nostatistics();
exit(1);
#endif #endif
} }
else if (*arg == 'T') { else if (*arg == 'T') {
#ifdef STATISTICS #ifdef STATISTICS
realtimestats = 0; realtimestats = 0;
#else #else
fprintf(stderr,"statistics support not compiled in\n"); e_nostatistics();
exit(1);
#endif #endif
} }
else { else {
fprintf(stderr, "unrecognised argument: -%c\n", *arg); fprintf(stderr,"unrecognised argument: -%c\n",*arg);
exit(1); exit(Q_UNRECOGNISED);
} }
if (numargit) if (numargit)
goto nextarg; goto nextarg;
@ -625,8 +641,13 @@ int main(int argc,char **argv)
fprintf(stderr,"WARNING: -N switch will produce bogus results because we can't know filter width. reconfigure with --enable-besort and recompile.\n"); fprintf(stderr,"WARNING: -N switch will produce bogus results because we can't know filter width. reconfigure with --enable-besort and recompile.\n");
#endif #endif
if (outfile) if (outfile) {
fout = fopen(outfile, "w"); fout = fopen(outfile,"w");
if (!fout) {
perror("failed to open output file");
exit(Q_FAILOPENOUTPUT);
}
}
if (workdir) if (workdir)
createdir(workdir,1); createdir(workdir,1);
@ -672,8 +693,8 @@ int main(int argc,char **argv)
#endif #endif
tret = pthread_create(&VEC_BUF(threads,i),0,fastkeygen ? dofastwork : dowork,tp); tret = pthread_create(&VEC_BUF(threads,i),0,fastkeygen ? dofastwork : dowork,tp);
if (tret) { if (tret) {
fprintf(stderr,"error while making " FSZ "th thread: %d\n",i,tret); fprintf(stderr,"error while making " FSZ "th thread: %d (%s)\n",i,tret,strerror(tret));
exit(1); exit(Q_FAILTHREAD);
} }
} }
@ -681,8 +702,8 @@ int main(int argc,char **argv)
struct timespec nowtime; struct timespec nowtime;
u64 istarttime,inowtime,ireporttime = 0,elapsedoffset = 0; u64 istarttime,inowtime,ireporttime = 0,elapsedoffset = 0;
if (clock_gettime(CLOCK_MONOTONIC,&nowtime) < 0) { if (clock_gettime(CLOCK_MONOTONIC,&nowtime) < 0) {
fprintf(stderr, "failed to get time\n"); perror("failed to get time");
exit(1); exit(Q_FAILTIME);
} }
istarttime = (1000000 * (u64)nowtime.tv_sec) + ((u64)nowtime.tv_nsec / 1000); istarttime = (1000000 * (u64)nowtime.tv_sec) + ((u64)nowtime.tv_nsec / 1000);
#endif #endif
@ -760,11 +781,11 @@ int main(int argc,char **argv)
} }
if (!quietflag) if (!quietflag)
fprintf(stderr, "waiting for threads to finish..."); fprintf(stderr,"waiting for threads to finish...");
for (size_t i = 0;i < VEC_LENGTH(threads);++i) for (size_t i = 0;i < VEC_LENGTH(threads);++i)
pthread_join(VEC_BUF(threads,i),0); pthread_join(VEC_BUF(threads,i),0);
if (!quietflag) if (!quietflag)
fprintf(stderr, " done.\n"); fprintf(stderr," done.\n");
pthread_mutex_destroy(&keysgenerated_mutex); pthread_mutex_destroy(&keysgenerated_mutex);
pthread_mutex_destroy(&fout_mutex); pthread_mutex_destroy(&fout_mutex);