#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "types.h"
#include "distcc.h"
#include "trace.h"
#include "access.h"
#include "exitcode.h"
static const in_addr_t allones = 0xffffffffUL;
int dcc_parse_mask(const char *spec,
in_addr_t *value,
in_addr_t *mask)
{
int value_len;
struct in_addr ia;
int mask_bits;
char *value_str;
int matched;
const char *mask_str;
value_len = strcspn(spec, "/");
value_str = strdup(spec);
value_str[value_len] = '\0';
matched = inet_pton(AF_INET, value_str, &ia);
if (!matched) {
rs_log_error("can't parse internet address \"%s\"", value_str);
free(value_str);
return EXIT_BAD_ARGUMENTS;
}
free(value_str);
*value = ia.s_addr;
mask_str = &spec[value_len + 1];
if (spec[value_len] && *mask_str) {
mask_bits = atoi(mask_str);
if (mask_bits < 0 || mask_bits > 32) {
rs_log_error("invalid mask \"%s\"", mask_str);
return EXIT_BAD_ARGUMENTS;
}
if (mask_bits == 32)
*mask = allones;
else
*mask = htonl(~(allones >> mask_bits));
} else {
*mask = allones;
}
return 0;
}
int dcc_check_address(in_addr_t client,
in_addr_t value,
in_addr_t mask)
{
if ((client & mask) == (value & mask)) {
rs_trace("match client %#lx, value %#lx, mask %#lx",
(long) client, (long) value, (long) mask);
return 0;
} else {
rs_trace("deny client %#lx, value %#lx, mask %#lx",
(long) client, (long) value, (long) mask);
return EXIT_ACCESS_DENIED;
}
}