-// SPDX-License-Identifier: GPL-2.0+
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
*/
#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/stat.h>
#include <arpa/inet.h>
#include <netdb.h>
+#include <stdio.h>
#include "unetd.h"
int network_get_endpoint(union network_endpoint *dest, const char *str,
host++;
port = strchr(host, ']');
if (!port)
- return -1;
+ goto out;
*(port++) = 0;
if (!*port)
found:
if (ai_cur->ai_addrlen > sizeof(*dest))
- goto out;
+ goto free_ai;
memcpy(dest, ai_cur->ai_addr, ai_cur->ai_addrlen);
if (!port)
dest->in.sin_port = htons(default_port);
ret = 0;
+free_ai:
+ freeaddrinfo(ai);
+
out:
free(buf);
return ret;
close(fd);
return ret;
}
+
+void *unet_read_file(const char *name, size_t *len)
+{
+ struct stat st;
+ void *data;
+ FILE *f;
+
+ f = fopen(name, "r");
+ if (!f)
+ goto error;
+
+ if (fstat(fileno(f), &st) < 0)
+ goto close;
+
+ if (*len && st.st_size > *len)
+ goto close;
+
+ data = malloc(st.st_size);
+ if (!data)
+ goto close;
+
+ if (fread(data, 1, st.st_size, f) != st.st_size) {
+ free(data);
+ goto close;
+ }
+ fclose(f);
+
+ *len = st.st_size;
+ return data;
+
+close:
+ fclose(f);
+error:
+ *len = 0;
+ return NULL;
+}