mirror of
https://github.com/cathugger/mkp224o.git
synced 2025-04-22 06:49:09 +00:00
split some io routines into separate file
This commit is contained in:
parent
e6a879949b
commit
39c55c1c2b
4 changed files with 85 additions and 19 deletions
|
@ -23,6 +23,7 @@ MAINOBJ= \
|
||||||
cpucount.c.o \
|
cpucount.c.o \
|
||||||
base32_to.c.o \
|
base32_to.c.o \
|
||||||
base32_from.c.o \
|
base32_from.c.o \
|
||||||
|
ioutil.c.o \
|
||||||
$(ED25519OBJ) \
|
$(ED25519OBJ) \
|
||||||
keccak.c.o
|
keccak.c.o
|
||||||
|
|
||||||
|
|
67
ioutil.c
Normal file
67
ioutil.c
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "types.h"
|
||||||
|
#include "ioutil.h"
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int writeall(FH fd,const u8 *data,size_t len)
|
||||||
|
{
|
||||||
|
ssize_t wrote;
|
||||||
|
while (len) {
|
||||||
|
wrote = write(fd,data,len);
|
||||||
|
if (wrote == -1) {
|
||||||
|
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
|
||||||
|
continue;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
len -= wrote;
|
||||||
|
data += wrote;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
FH createfile(const char *path,int secret)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
do {
|
||||||
|
fd = open(path,O_WRONLY | O_CREAT | O_TRUNC,secret ? 0600 : 0666);
|
||||||
|
if (fd == -1) {
|
||||||
|
if (errno == EINTR)
|
||||||
|
continue;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} while (0);
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
int closefile(FH fd)
|
||||||
|
{
|
||||||
|
int cret;
|
||||||
|
do {
|
||||||
|
cret = close(fd);
|
||||||
|
if (cret == -1) {
|
||||||
|
if (errno == EINTR)
|
||||||
|
continue;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} while (0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int writetofile(const char *path,const u8 *data,size_t len,int secret)
|
||||||
|
{
|
||||||
|
FH fd = createfile(path,secret);
|
||||||
|
int wret = writeall(fd,data,len);
|
||||||
|
int cret = closefile(fd);
|
||||||
|
if (cret == -1)
|
||||||
|
return -1;
|
||||||
|
return wret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int createdir(const char *path,int secret)
|
||||||
|
{
|
||||||
|
return mkdir(path,secret ? 0700 : 0777);
|
||||||
|
}
|
8
ioutil.h
Normal file
8
ioutil.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
typedef int FH;
|
||||||
|
#define FH_invalid -1
|
||||||
|
|
||||||
|
FH createfile(const char *path,int secret);
|
||||||
|
int closefile(FH fd);
|
||||||
|
int writeall(FH,const u8 *data,size_t len);
|
||||||
|
int writetofile(const char *path,const u8 *data,size_t len,int secret);
|
||||||
|
int createdir(const char *path,int secret);
|
28
main.c
28
main.c
|
@ -9,7 +9,6 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sodium/randombytes.h>
|
#include <sodium/randombytes.h>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
@ -19,6 +18,7 @@
|
||||||
#include "cpucount.h"
|
#include "cpucount.h"
|
||||||
#include "keccak.h"
|
#include "keccak.h"
|
||||||
#include "ed25519/ed25519.h"
|
#include "ed25519/ed25519.h"
|
||||||
|
#include "ioutil.h"
|
||||||
|
|
||||||
// additional leading zero is added by C
|
// additional leading zero is added by C
|
||||||
static const char * const pkprefix = "== ed25519v1-public: type0 ==\0\0";
|
static const char * const pkprefix = "== ed25519v1-public: type0 ==\0\0";
|
||||||
|
@ -646,8 +646,6 @@ VEC_STRUCT(tstatsvec,struct tstatstruct);
|
||||||
|
|
||||||
static void onionready(char *sname, const u8 *secret, const u8 *pubonion)
|
static void onionready(char *sname, const u8 *secret, const u8 *pubonion)
|
||||||
{
|
{
|
||||||
FILE *fh;
|
|
||||||
|
|
||||||
if (endwork)
|
if (endwork)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -659,7 +657,7 @@ static void onionready(char *sname, const u8 *secret, const u8 *pubonion)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mkdir(sname,0700) != 0) {
|
if (createdir(sname,1) != 0) {
|
||||||
if (numneedgenerate)
|
if (numneedgenerate)
|
||||||
pthread_mutex_unlock(&keysgenerated_mutex);
|
pthread_mutex_unlock(&keysgenerated_mutex);
|
||||||
return;
|
return;
|
||||||
|
@ -673,26 +671,18 @@ static void onionready(char *sname, const u8 *secret, const u8 *pubonion)
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy(&sname[onionendpos], "/hs_ed25519_secret_key");
|
strcpy(&sname[onionendpos], "/hs_ed25519_secret_key");
|
||||||
fh = fopen(sname, "wb");
|
writetofile(sname,secret,skprefixlen + SECRET_LEN,1);
|
||||||
if (fh) {
|
|
||||||
fwrite(secret, skprefixlen + SECRET_LEN, 1, fh);
|
|
||||||
fclose(fh);
|
|
||||||
}
|
|
||||||
|
|
||||||
strcpy(&sname[onionendpos], "/hostname");
|
strcpy(&sname[onionendpos], "/hostname");
|
||||||
fh = fopen(sname, "w");
|
FILE *hfile = fopen(sname,"w");
|
||||||
if (fh) {
|
if (hfile) {
|
||||||
sname[onionendpos] = '\n';
|
sname[onionendpos] = '\n';
|
||||||
fwrite(&sname[direndpos], ONIONLEN+1, 1, fh);
|
fwrite(&sname[direndpos],ONIONLEN + 1,1,hfile);
|
||||||
fclose(fh);
|
fclose(hfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy(&sname[onionendpos], "/hs_ed25519_public_key");
|
strcpy(&sname[onionendpos], "/hs_ed25519_public_key");
|
||||||
fh = fopen(sname, "wb");
|
writetofile(sname,pubonion,pkprefixlen + PUBLIC_LEN,0);
|
||||||
if (fh) {
|
|
||||||
fwrite(pubonion, pkprefixlen + PUBLIC_LEN, 1, fh);
|
|
||||||
fclose(fh);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fout) {
|
if (fout) {
|
||||||
sname[onionendpos] = '\n';
|
sname[onionendpos] = '\n';
|
||||||
|
@ -1164,7 +1154,7 @@ int main(int argc,char **argv)
|
||||||
fout = fopen(outfile, "w");
|
fout = fopen(outfile, "w");
|
||||||
|
|
||||||
if (workdir)
|
if (workdir)
|
||||||
mkdir(workdir, 0700);
|
createdir(workdir,1);
|
||||||
|
|
||||||
direndpos = workdirlen;
|
direndpos = workdirlen;
|
||||||
onionendpos = workdirlen + ONIONLEN;
|
onionendpos = workdirlen + ONIONLEN;
|
||||||
|
|
Loading…
Add table
Reference in a new issue