+/* SPDX-License-Identifier: LGPL-2.1+ */
/***
- This file is part of systemd.
-
Copyright (C) 2014 Axis Communications AB. All rights reserved.
Copyright (C) 2015 Tom Gundersen
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
+#include "sd-id128.h"
#include "sd-ipv4acd.h"
#include "sd-ipv4ll.h"
return NULL;
sd_ipv4acd_unref(ll->acd);
- free(ll);
-
- return NULL;
+ return mfree(ll);
}
int sd_ipv4ll_new(sd_ipv4ll **ret) {
if (r < 0)
return r;
- *ret = ll;
- ll = NULL;
+ *ret = TAKE_PTR(ll);
return 0;
}
}
static bool ipv4ll_address_is_valid(const struct in_addr *address) {
- uint32_t addr;
-
assert(address);
if (!in_addr_is_link_local(AF_INET, (const union in_addr_union *) address))
return false;
- addr = be32toh(address->s_addr);
-
- if ((addr & 0x0000FF00) == 0x0000 ||
- (addr & 0x0000FF00) == 0xFF00)
- return false;
-
- return true;
+ return !IN_SET(be32toh(address->s_addr) & 0x0000FF00U, 0x0000U, 0xFF00U);
}
int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address) {
addr = htobe32((h & UINT32_C(0x0000FFFF)) | IPV4LL_NETWORK);
} while (addr == ll->address ||
- (be32toh(addr) & 0x0000FF00) == 0x0000 ||
- (be32toh(addr) & 0x0000FF00) == 0xFF00);
+ IN_SET(be32toh(addr) & 0x0000FF00U, 0x0000U, 0xFF00U));
(void) in_addr_to_string(AF_INET, &(union in_addr_union) { .in.s_addr = addr }, &address);
log_ipv4ll(ll, "Picked new IP address %s.", strna(address));
return sd_ipv4ll_set_address(ll, &(struct in_addr) { addr });
}
+int sd_ipv4ll_restart(sd_ipv4ll *ll) {
+ ll->address = 0;
+
+ return sd_ipv4ll_start(ll);
+}
+
#define MAC_HASH_KEY SD_ID128_MAKE(df,04,22,98,3f,ad,14,52,f9,87,2e,d1,9c,70,e2,f2)
int sd_ipv4ll_start(sd_ipv4ll *ll) {