#include "config.h"
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#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"
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_aton(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;
}
*mask = htonl(~((1L << (32 - mask_bits)) - 1));
} else {
*mask = (in_addr_t) 0xffffffffL;
}
return 0;
}
int dcc_check_address(in_addr_t client,
in_addr_t value,
in_addr_t mask)
{
if ((client & mask) == (value & mask))
return 0;
else
return EXIT_ACCESS_DENIED;
}