mirror of
https://github.com/cathugger/mkp224o.git
synced 2025-04-21 06:19: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 \
|
||||
base32_to.c.o \
|
||||
base32_from.c.o \
|
||||
ioutil.c.o \
|
||||
$(ED25519OBJ) \
|
||||
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 <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sodium/randombytes.h>
|
||||
|
||||
#include "types.h"
|
||||
|
@ -19,6 +18,7 @@
|
|||
#include "cpucount.h"
|
||||
#include "keccak.h"
|
||||
#include "ed25519/ed25519.h"
|
||||
#include "ioutil.h"
|
||||
|
||||
// additional leading zero is added by C
|
||||
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)
|
||||
{
|
||||
FILE *fh;
|
||||
|
||||
if (endwork)
|
||||
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)
|
||||
pthread_mutex_unlock(&keysgenerated_mutex);
|
||||
return;
|
||||
|
@ -673,26 +671,18 @@ static void onionready(char *sname, const u8 *secret, const u8 *pubonion)
|
|||
}
|
||||
|
||||
strcpy(&sname[onionendpos], "/hs_ed25519_secret_key");
|
||||
fh = fopen(sname, "wb");
|
||||
if (fh) {
|
||||
fwrite(secret, skprefixlen + SECRET_LEN, 1, fh);
|
||||
fclose(fh);
|
||||
}
|
||||
writetofile(sname,secret,skprefixlen + SECRET_LEN,1);
|
||||
|
||||
strcpy(&sname[onionendpos], "/hostname");
|
||||
fh = fopen(sname, "w");
|
||||
if (fh) {
|
||||
FILE *hfile = fopen(sname,"w");
|
||||
if (hfile) {
|
||||
sname[onionendpos] = '\n';
|
||||
fwrite(&sname[direndpos], ONIONLEN+1, 1, fh);
|
||||
fclose(fh);
|
||||
fwrite(&sname[direndpos],ONIONLEN + 1,1,hfile);
|
||||
fclose(hfile);
|
||||
}
|
||||
|
||||
strcpy(&sname[onionendpos], "/hs_ed25519_public_key");
|
||||
fh = fopen(sname, "wb");
|
||||
if (fh) {
|
||||
fwrite(pubonion, pkprefixlen + PUBLIC_LEN, 1, fh);
|
||||
fclose(fh);
|
||||
}
|
||||
writetofile(sname,pubonion,pkprefixlen + PUBLIC_LEN,0);
|
||||
|
||||
if (fout) {
|
||||
sname[onionendpos] = '\n';
|
||||
|
@ -1164,7 +1154,7 @@ int main(int argc,char **argv)
|
|||
fout = fopen(outfile, "w");
|
||||
|
||||
if (workdir)
|
||||
mkdir(workdir, 0700);
|
||||
createdir(workdir,1);
|
||||
|
||||
direndpos = workdirlen;
|
||||
onionendpos = workdirlen + ONIONLEN;
|
||||
|
|
Loading…
Add table
Reference in a new issue