mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-11-28 10:28:46 +00:00
add libipset
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
# This file is part of libasyncns.
|
||||
#
|
||||
# Copyright 2005-2008 Lennart Poettering
|
||||
#
|
||||
# libasyncns 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.
|
||||
#
|
||||
# libasyncns 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 libasyncns. If not, see
|
||||
# <http://www.gnu.org/licenses/>.
|
||||
|
||||
noinst_LTLIBRARIES = libipset.la
|
||||
|
||||
bdd_src = bdd/assignments.c bdd/basics.c bdd/bdd-iterator.c bdd/expanded.c \
|
||||
bdd/reachable.c bdd/read.c bdd/write.c
|
||||
map_src = map/allocation.c map/inspection.c map/ipv4_map.c map/ipv6_map.c \
|
||||
map/storage.c
|
||||
set_src = set/allocation.c set/inspection.c set/ipv4_set.c set/ipv6_set.c \
|
||||
set/iterator.c set/storage.c
|
||||
|
||||
libipset_la_SOURCES = ${bdd_src} ${map_src} ${set_src}
|
||||
|
||||
libipset_la_LDFLAGS = -static
|
||||
@@ -0,0 +1,50 @@
|
||||
/* -*- coding: utf-8 -*-
|
||||
* ----------------------------------------------------------------------
|
||||
* Copyright © 2009-2012, RedJack, LLC.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Please see the LICENSE.txt file in this distribution for license
|
||||
* details.
|
||||
* ----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <libcork/core.h>
|
||||
|
||||
#include "ipset/bdd/nodes.h"
|
||||
#include "ipset/ipset.h"
|
||||
|
||||
|
||||
void
|
||||
ipmap_init(struct ip_map *map, int default_value)
|
||||
{
|
||||
/* The map starts empty, so every value assignment should yield the
|
||||
* default. */
|
||||
map->cache = ipset_node_cache_new();
|
||||
map->default_bdd = ipset_terminal_node_id(default_value);
|
||||
map->map_bdd = map->default_bdd;
|
||||
}
|
||||
|
||||
|
||||
struct ip_map *
|
||||
ipmap_new(int default_value)
|
||||
{
|
||||
struct ip_map *result = cork_new(struct ip_map);
|
||||
ipmap_init(result, default_value);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ipmap_done(struct ip_map *map)
|
||||
{
|
||||
ipset_node_decref(map->cache, map->map_bdd);
|
||||
ipset_node_cache_free(map->cache);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ipmap_free(struct ip_map *map)
|
||||
{
|
||||
ipmap_done(map);
|
||||
free(map);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/* -*- coding: utf-8 -*-
|
||||
* ----------------------------------------------------------------------
|
||||
* Copyright © 2009-2012, RedJack, LLC.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Please see the LICENSE.txt file in this distribution for license
|
||||
* details.
|
||||
* ----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <libcork/core.h>
|
||||
|
||||
#include "ipset/bdd/nodes.h"
|
||||
#include "ipset/bits.h"
|
||||
#include "ipset/errors.h"
|
||||
#include "ipset/ipset.h"
|
||||
|
||||
|
||||
/**
|
||||
* Given a BDD variable number, return the index of the corresponding
|
||||
* bit in an IP address. IPv4 addresses use variables 1-32; IPv6
|
||||
* addresses use 1-128. (Variable 0 is used to identify the kind of
|
||||
* address — TRUE for IPv4, FALSE for IPv6.)
|
||||
*/
|
||||
|
||||
static unsigned int
|
||||
IPMAP_NAME(bit_for_var)(ipset_variable var)
|
||||
{
|
||||
return (var - 1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* An assignment function that can be used to evaluate an IP map BDD.
|
||||
*/
|
||||
|
||||
static bool
|
||||
IPMAP_NAME(assignment)(const void *addr, ipset_variable var)
|
||||
{
|
||||
if (var == 0) {
|
||||
return IP_DISCRIMINATOR_VALUE;
|
||||
} else {
|
||||
unsigned int bit = IPMAP_NAME(bit_for_var)(var);
|
||||
return IPSET_BIT_GET(addr, bit);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
IPMAP_NAME(get)(struct ip_map *map, CORK_IP *elem)
|
||||
{
|
||||
return ipset_node_evaluate
|
||||
(map->cache, map->map_bdd, IPMAP_NAME(assignment), elem);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
IPMAP_NAME(set_network)(struct ip_map *map, CORK_IP *elem,
|
||||
unsigned int cidr_prefix, int value)
|
||||
{
|
||||
/* Special case — the BDD for a netmask that's out of range never
|
||||
* evaluates to true. */
|
||||
if (cidr_prefix > IP_BIT_SIZE) {
|
||||
cork_error_set
|
||||
(IPSET_ERROR, IPSET_PARSE_ERROR,
|
||||
"CIDR block %u out of range [0..%u]", cidr_prefix, IP_BIT_SIZE);
|
||||
return;
|
||||
}
|
||||
|
||||
ipset_node_id new_bdd =
|
||||
ipset_node_insert
|
||||
(map->cache, map->map_bdd,
|
||||
IPMAP_NAME(assignment), elem, cidr_prefix + 1, value);
|
||||
ipset_node_decref(map->cache, map->map_bdd);
|
||||
map->map_bdd = new_bdd;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
IPMAP_NAME(set)(struct ip_map *map, CORK_IP *elem, int value)
|
||||
{
|
||||
ipset_node_id new_bdd =
|
||||
ipset_node_insert
|
||||
(map->cache, map->map_bdd,
|
||||
IPMAP_NAME(assignment), elem, IP_BIT_SIZE + 1, value);
|
||||
ipset_node_decref(map->cache, map->map_bdd);
|
||||
map->map_bdd = new_bdd;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/* -*- coding: utf-8 -*-
|
||||
* ----------------------------------------------------------------------
|
||||
* Copyright © 2009-2012, RedJack, LLC.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Please see the LICENSE.txt file in this distribution for license
|
||||
* details.
|
||||
* ----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <libcork/core.h>
|
||||
|
||||
#include "ipset/bdd/nodes.h"
|
||||
#include "ipset/ipset.h"
|
||||
|
||||
bool
|
||||
ipmap_is_empty(const struct ip_map *map)
|
||||
{
|
||||
/* Since BDDs are unique, any map that maps all addresses to the
|
||||
* default value is “empty”. */
|
||||
return (map->map_bdd == map->default_bdd);
|
||||
}
|
||||
|
||||
bool
|
||||
ipmap_is_equal(const struct ip_map *map1, const struct ip_map *map2)
|
||||
{
|
||||
return ipset_node_cache_nodes_equal
|
||||
(map1->cache, map1->map_bdd, map2->cache, map2->map_bdd);
|
||||
}
|
||||
|
||||
size_t
|
||||
ipmap_memory_size(const struct ip_map *map)
|
||||
{
|
||||
return ipset_node_memory_size(map->cache, map->map_bdd);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ipmap_ip_set(struct ip_map *map, struct cork_ip *addr, int value)
|
||||
{
|
||||
if (addr->version == 4) {
|
||||
ipmap_ipv4_set(map, &addr->ip.v4, value);
|
||||
} else {
|
||||
ipmap_ipv6_set(map, &addr->ip.v6, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ipmap_ip_set_network(struct ip_map *map, struct cork_ip *addr,
|
||||
unsigned int cidr_prefix, int value)
|
||||
{
|
||||
if (addr->version == 4) {
|
||||
ipmap_ipv4_set_network(map, &addr->ip.v4, cidr_prefix, value);
|
||||
} else {
|
||||
ipmap_ipv6_set_network(map, &addr->ip.v6, cidr_prefix, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
ipmap_ip_get(struct ip_map *map, struct cork_ip *addr)
|
||||
{
|
||||
if (addr->version == 4) {
|
||||
return ipmap_ipv4_get(map, &addr->ip.v4);
|
||||
} else {
|
||||
return ipmap_ipv6_get(map, &addr->ip.v6);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/* -*- coding: utf-8 -*-
|
||||
* ----------------------------------------------------------------------
|
||||
* Copyright © 2009-2012, RedJack, LLC.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Please see the LICENSE.txt file in this distribution for license
|
||||
* details.
|
||||
* ----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* The IPv4 and IPv6 map types are basically identical, except for the
|
||||
* names of the functions, and the size of the values that are being
|
||||
* stored. Rather than having two mostly duplicate definitions of each
|
||||
* function, we define “template functions” where anything that depends
|
||||
* on the size of the IP address is defined using the following macros.
|
||||
*/
|
||||
|
||||
|
||||
/* The name of the cork_ipvX type. */
|
||||
#define CORK_IP struct cork_ipv4
|
||||
|
||||
/* The number of bits in an IPvX address. */
|
||||
#define IP_BIT_SIZE 32
|
||||
|
||||
/* The value of the discriminator variable for an IPvX address. */
|
||||
#define IP_DISCRIMINATOR_VALUE true
|
||||
|
||||
/* Creates a identifier of the form “ipset_ipv4_<basename>”. */
|
||||
#define IPSET_NAME(basename) ipset_ipv4_##basename
|
||||
|
||||
/* Creates a identifier of the form “ipmap_ipv4_<basename>”. */
|
||||
#define IPMAP_NAME(basename) ipmap_ipv4_##basename
|
||||
|
||||
|
||||
/* Now include all of the templates. */
|
||||
#include "inspection-template.c.in"
|
||||
@@ -0,0 +1,37 @@
|
||||
/* -*- coding: utf-8 -*-
|
||||
* ----------------------------------------------------------------------
|
||||
* Copyright © 2009-2012, RedJack, LLC.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Please see the LICENSE.txt file in this distribution for license
|
||||
* details.
|
||||
* ----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* The IPv4 and IPv6 map types are basically identical, except for the
|
||||
* names of the functions, and the size of the values that are being
|
||||
* stored. Rather than having two mostly duplicate definitions of each
|
||||
* function, we define “template functions” where anything that depends
|
||||
* on the size of the IP address is defined using the following macros.
|
||||
*/
|
||||
|
||||
|
||||
/* The name of the cork_ipvX type. */
|
||||
#define CORK_IP struct cork_ipv6
|
||||
|
||||
/* The number of bits in an IPvX address. */
|
||||
#define IP_BIT_SIZE 128
|
||||
|
||||
/* The value of the discriminator variable for an IPvX address. */
|
||||
#define IP_DISCRIMINATOR_VALUE false
|
||||
|
||||
/* Creates a identifier of the form “ipset_ipv6_<basename>”. */
|
||||
#define IPSET_NAME(basename) ipset_ipv6_##basename
|
||||
|
||||
/* Creates a identifier of the form “ipmap_ipv6_<basename>”. */
|
||||
#define IPMAP_NAME(basename) ipmap_ipv6_##basename
|
||||
|
||||
|
||||
/* Now include all of the templates. */
|
||||
#include "inspection-template.c.in"
|
||||
@@ -0,0 +1,101 @@
|
||||
/* -*- coding: utf-8 -*-
|
||||
* ----------------------------------------------------------------------
|
||||
* Copyright © 2009-2012, RedJack, LLC.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Please see the LICENSE.txt file in this distribution for license
|
||||
* details.
|
||||
* ----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <libcork/core.h>
|
||||
|
||||
#include "ipset/bdd/nodes.h"
|
||||
#include "ipset/errors.h"
|
||||
#include "ipset/ipset.h"
|
||||
|
||||
|
||||
static void
|
||||
create_errno_error(FILE *stream)
|
||||
{
|
||||
if (ferror(stream)) {
|
||||
cork_error_set(IPSET_ERROR, IPSET_IO_ERROR, "%s", strerror(errno));
|
||||
} else {
|
||||
cork_unknown_error();
|
||||
}
|
||||
}
|
||||
|
||||
struct file_consumer {
|
||||
/* file_consumer is a subclass of cork_stream_consumer */
|
||||
struct cork_stream_consumer parent;
|
||||
/* the file to write the data into */
|
||||
FILE *fp;
|
||||
};
|
||||
|
||||
static int
|
||||
file_consumer_data(struct cork_stream_consumer *vself,
|
||||
const void *buf, size_t size, bool is_first)
|
||||
{
|
||||
struct file_consumer *self =
|
||||
cork_container_of(vself, struct file_consumer, parent);
|
||||
size_t bytes_written = fwrite(buf, 1, size, self->fp);
|
||||
/* If there was an error writing to the file, then signal this to
|
||||
* the producer */
|
||||
if (bytes_written == size) {
|
||||
return 0;
|
||||
} else {
|
||||
create_errno_error(self->fp);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
file_consumer_eof(struct cork_stream_consumer *vself)
|
||||
{
|
||||
/* We don't close the file, so there's nothing special to do at
|
||||
* end-of-stream. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
ipmap_save_to_stream(struct cork_stream_consumer *stream,
|
||||
const struct ip_map *map)
|
||||
{
|
||||
return ipset_node_cache_save(stream, map->cache, map->map_bdd);
|
||||
}
|
||||
|
||||
int
|
||||
ipmap_save(FILE *fp, const struct ip_map *map)
|
||||
{
|
||||
struct file_consumer stream = {
|
||||
{ file_consumer_data, file_consumer_eof, NULL }, fp
|
||||
};
|
||||
return ipmap_save_to_stream(&stream.parent, map);
|
||||
}
|
||||
|
||||
|
||||
struct ip_map *
|
||||
ipmap_load(FILE *stream)
|
||||
{
|
||||
struct ip_map *map;
|
||||
ipset_node_id new_bdd;
|
||||
|
||||
/* It doesn't matter what default value we use here, because we're
|
||||
* going to replace it with the default BDD we load in from the
|
||||
* file. */
|
||||
map = ipmap_new(0);
|
||||
new_bdd = ipset_node_cache_load(stream, map->cache);
|
||||
if (cork_error_occurred()) {
|
||||
ipmap_free(map);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
map->map_bdd = new_bdd;
|
||||
return map;
|
||||
}
|
||||
Reference in New Issue
Block a user