/*
* Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
*/
+#define _GNU_SOURCE
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
FILE *f = NULL;
int ret = -1;
- asprintf(&fname, "%s/%s.bin", data_dir, network_name(net));
+ if (asprintf(&fname, "%s/%s.bin", data_dir, network_name(net)) < 0)
+ return -1;
+
f = fopen(fname, "r");
free(fname);
!net->net_data_len)
return -1;
- asprintf(&fname, "%s/%s.bin.XXXXXXXX", data_dir, network_name(net));
+ if (asprintf(&fname, "%s/%s.bin.XXXXXXXX", data_dir, network_name(net)) < 0)
+ return -1;
+
fd = mkstemp(fname);
if (fd < 0)
goto error;
hdr->len = sizeof(*ehdr);
- fread(&ehdr->nonce, sizeof(ehdr->nonce), 1, pex_urandom);
+ if (fread(&ehdr->nonce, sizeof(ehdr->nonce), 1, pex_urandom) != 1)
+ return NULL;
hash = pex_network_hash(auth_key, ehdr->nonce);
*(uint64_t *)hdr->id ^= hash;
ctx->ext = ext;
ctx->req_id = req->req_id;
- __pex_msg_init_ext(pubkey, auth_key, PEX_MSG_UPDATE_RESPONSE, ext);
+ if (!__pex_msg_init_ext(pubkey, auth_key, PEX_MSG_UPDATE_RESPONSE, ext))
+ return;
+
res = pex_msg_append(sizeof(*res));
res->req_id = req->req_id;
res->data_len = len;
- fread(e_key_priv, sizeof(e_key_priv), 1, pex_urandom);
+ if (!fread(e_key_priv, sizeof(e_key_priv), 1, pex_urandom))
+ return;
+
curve25519_clamp_secret(e_key_priv);
curve25519_generate_public(res->e_key, e_key_priv);
curve25519(enc_key, e_key_priv, peer_key);
return false;
}
- __pex_msg_init_ext(ctx->pubkey, ctx->auth_key,
- PEX_MSG_UPDATE_RESPONSE_DATA, ctx->ext);
+ if (!__pex_msg_init_ext(ctx->pubkey, ctx->auth_key,
+ PEX_MSG_UPDATE_RESPONSE_DATA, ctx->ext))
+ return false;
+
res_ext = pex_msg_append(sizeof(*res_ext));
res_ext->req_id = ctx->req_id;
res_ext->offset = ctx->cur - ctx->data;
memcpy(&ctx->addr, addr, sizeof(ctx->addr));
memcpy(ctx->auth_key, auth_key, sizeof(ctx->auth_key));
memcpy(ctx->priv_key, priv_key, sizeof(ctx->priv_key));
- fread(&ctx->req_id, sizeof(ctx->req_id), 1, pex_urandom);
+ if (!fread(&ctx->req_id, sizeof(ctx->req_id), 1, pex_urandom))
+ return NULL;
list_add_tail(&ctx->list, &requests);
if (!gc_timer.pending)
uloop_timeout_set(&gc_timer, 1000);
- __pex_msg_init_ext(pubkey, auth_key, PEX_MSG_UPDATE_REQUEST, ext);
+ if (!__pex_msg_init_ext(pubkey, auth_key, PEX_MSG_UPDATE_REQUEST, ext)) {
+ free(ctx);
+ return NULL;
+ }
+
req = pex_msg_append(sizeof(*req));
req->cur_version = cpu_to_be64(cur_version);
req->req_id = ctx->req_id;