+static int load_address(const char *fn, uint32_t *addr) {
+ FILE *f;
+ unsigned a, b, c, d;
+
+ assert(fn);
+ assert(addr);
+
+ if (!(f = fopen(fn, "r"))) {
+
+ if (errno == ENOENT) {
+ *addr = 0;
+ return 0;
+ }
+
+ daemon_log(LOG_ERR, "fopen() failed: %s", strerror(errno));
+ goto fail;
+ }
+
+ if (fscanf(f, "%u.%u.%u.%u\n", &a, &b, &c, &d) != 4) {
+ daemon_log(LOG_ERR, "Parse failure");
+ goto fail;
+ }
+
+ fclose(f);
+
+ *addr = htonl((a << 24) | (b << 16) | (c << 8) | d);
+ return 0;
+
+fail:
+ if (f)
+ fclose(f);
+
+ return -1;
+}
+
+static int save_address(const char *fn, uint32_t addr) {
+ FILE *f;
+ char buf[32];
+
+ assert(fn);
+
+ if (!(f = fopen(fn, "w"))) {
+ daemon_log(LOG_ERR, "fopen() failed: %s", strerror(errno));
+ goto fail;
+ }
+
+ fprintf(f, "%s\n", inet_ntop(AF_INET, &addr, buf, sizeof (buf)));
+ fclose(f);
+
+ return 0;
+
+fail:
+ if (f)
+ fclose(f);
+
+ return -1;
+}
+