int *errnop, int *h_errnop,
int32_t *ttlp) {
- unsigned lo_ifi;
- char hn[HOST_NAME_MAX+1] = {};
- const char *canonical = NULL;
- size_t l, idx, ms;
- char *r_name;
struct gaih_addrtuple *r_tuple, *r_tuple_prev = NULL;
- struct address *addresses = NULL, *a;
+ _cleanup_free_ struct address *addresses = NULL;
+ _cleanup_free_ char *hn = NULL;
+ const char *canonical = NULL;
unsigned n_addresses = 0, n;
uint32_t local_address_ipv4;
+ struct address *a;
+ size_t l, idx, ms;
+ char *r_name;
+ int lo_ifi;
- if (strcasecmp(name, "localhost") == 0) {
+ assert(name);
+ assert(pat);
+ assert(buffer);
+ assert(errnop);
+ assert(h_errnop);
+
+ if (is_localhost(name)) {
/* We respond to 'localhost', so that /etc/hosts
* is optional */
canonical = "localhost";
local_address_ipv4 = htonl(INADDR_LOOPBACK);
} else {
- /* We respond to our local host name */
-
- if (gethostname(hn, sizeof(hn)-1) < 0) {
- *errnop = errno;
+ hn = gethostname_malloc();
+ if (!hn) {
+ *errnop = ENOMEM;
*h_errnop = NO_RECOVERY;
- return NSS_STATUS_UNAVAIL;
+ return NSS_STATUS_TRYAGAIN;
}
- if (strcasecmp(name, hn) != 0) {
+ /* We respond to our local host name, our our hostname suffixed with a single dot. */
+ if (!streq(name, hn) && !streq_ptr(startswith(name, hn), ".")) {
*errnop = ENOENT;
*h_errnop = HOST_NOT_FOUND;
return NSS_STATUS_NOTFOUND;
lo_ifi = n_addresses <= 0 ? if_nametoindex(LOOPBACK_INTERFACE) : 0;
l = strlen(canonical);
- ms = ALIGN(l+1)+ALIGN(sizeof(struct gaih_addrtuple))*(n_addresses > 0 ? n_addresses : 2);
+ ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * (n_addresses > 0 ? n_addresses : 2);
if (buflen < ms) {
*errnop = ENOMEM;
*h_errnop = NO_RECOVERY;
- free(addresses);
return NSS_STATUS_TRYAGAIN;
}
r_tuple->name = r_name;
r_tuple->family = a->family;
r_tuple->scopeid = a->ifindex;
- memcpy(r_tuple->addr, a->address, 16);
+ memcpy(r_tuple->addr, &a->address, 16);
idx += ALIGN(sizeof(struct gaih_addrtuple));
r_tuple_prev = r_tuple;
if (ttlp)
*ttlp = 0;
- free(addresses);
-
return NSS_STATUS_SUCCESS;
}
struct address *a;
unsigned n, c;
+ assert(canonical);
+ assert(result);
+ assert(buffer);
+ assert(errnop);
+ assert(h_errnop);
+
alen = PROTO_ADDRESS_SIZE(af);
for (a = addresses, n = 0, c = 0; n < n_addresses; a++, n++)
l_additional = additional ? strlen(additional) : 0;
ms = ALIGN(l_canonical+1)+
(additional ? ALIGN(l_additional+1) : 0) +
- sizeof(char*)+
+ sizeof(char*) +
(additional ? sizeof(char*) : 0) +
- (c > 0 ? c : 1)*ALIGN(alen)+
- (c > 0 ? c+1 : 2)*sizeof(char*);
+ (c > 0 ? c : 1) * ALIGN(alen)+
+ (c > 0 ? c+1 : 2) * sizeof(char*);
if (buflen < ms) {
*errnop = ENOMEM;
*h_errnop = NO_RECOVERY;
- free(addresses);
return NSS_STATUS_TRYAGAIN;
}
if (af != a->family)
continue;
- memcpy(r_addr + i*ALIGN(alen), a->address, alen);
+ memcpy(r_addr + i*ALIGN(alen), &a->address, alen);
i++;
}
if (canonp)
*canonp = r_name;
- free(addresses);
-
return NSS_STATUS_SUCCESS;
}
int32_t *ttlp,
char **canonp) {
- char hn[HOST_NAME_MAX+1] = {};
- struct address *addresses = NULL;
- unsigned n_addresses = 0;
+ _cleanup_free_ struct address *addresses = NULL;
const char *canonical, *additional = NULL;
+ _cleanup_free_ char *hn = NULL;
uint32_t local_address_ipv4;
+ unsigned n_addresses = 0;
+
+ assert(name);
+ assert(host);
+ assert(buffer);
+ assert(errnop);
+ assert(h_errnop);
if (af == AF_UNSPEC)
af = AF_INET;
return NSS_STATUS_UNAVAIL;
}
- if (strcasecmp(name, "localhost") == 0) {
+ if (is_localhost(name)) {
canonical = "localhost";
local_address_ipv4 = htonl(INADDR_LOOPBACK);
} else {
- if (gethostname(hn, sizeof(hn)-1) < 0) {
- *errnop = errno;
+ hn = gethostname_malloc();
+ if (!hn) {
+ *errnop = ENOMEM;
*h_errnop = NO_RECOVERY;
- return NSS_STATUS_UNAVAIL;
+ return NSS_STATUS_TRYAGAIN;
}
- if (strcasecmp(name, hn) != 0) {
+ if (!streq(name, hn) && !streq_ptr(startswith(name, hn), ".")) {
*errnop = ENOENT;
*h_errnop = HOST_NOT_FOUND;
return NSS_STATUS_NOTFOUND;
int *errnop, int *h_errnop,
int32_t *ttlp) {
- char hn[HOST_NAME_MAX+1] = {};
- struct address *addresses = NULL;
- struct address *a;
- unsigned n_addresses = 0, n;
- uint32_t local_address_ipv4 = LOCALADDRESS_IPV4;
const char *canonical = NULL, *additional = NULL;
+ uint32_t local_address_ipv4 = LOCALADDRESS_IPV4;
+ _cleanup_free_ struct address *addresses = NULL;
+ _cleanup_free_ char *hn = NULL;
+ unsigned n_addresses = 0, n;
+ struct address *a;
+
+ assert(addr);
+ assert(host);
+ assert(buffer);
+ assert(errnop);
+ assert(h_errnop);
if (len != PROTO_ADDRESS_SIZE(af)) {
*errnop = EINVAL;
if (af != a->family)
continue;
- if (memcmp(addr, a->address, PROTO_ADDRESS_SIZE(af)) == 0)
+ if (memcmp(addr, &a->address, PROTO_ADDRESS_SIZE(af)) == 0)
goto found;
}
*errnop = ENOENT;
*h_errnop = HOST_NOT_FOUND;
- free(addresses);
-
return NSS_STATUS_NOTFOUND;
found:
if (!canonical) {
- if (gethostname(hn, sizeof(hn)-1) < 0) {
- *errnop = errno;
+ hn = gethostname_malloc();
+ if (!hn) {
+ *errnop = ENOMEM;
*h_errnop = NO_RECOVERY;
-
- free(addresses);
-
- return NSS_STATUS_UNAVAIL;
+ return NSS_STATUS_TRYAGAIN;
}
canonical = hn;