diff options
author | Stef Walter <stef@memberwebs.com> | 2008-06-09 17:22:43 +0000 |
---|---|---|
committer | Stef Walter <stef@memberwebs.com> | 2008-06-09 17:22:43 +0000 |
commit | b9ecd6e5e5b87fe1c4dab960e92246772002dd6a (patch) | |
tree | eb05eb48888e391678fa706ec5f62b012df2456e /common | |
parent | 3add3c0e19b659427a2624ec85daf67019b8ed7f (diff) |
Merge the two dns plugins
Diffstat (limited to 'common')
-rw-r--r-- | common/soa.c | 77 | ||||
-rw-r--r-- | common/soa.h | 6 | ||||
-rw-r--r-- | common/strset.c | 276 | ||||
-rw-r--r-- | common/strset.h | 75 |
4 files changed, 434 insertions, 0 deletions
diff --git a/common/soa.c b/common/soa.c new file mode 100644 index 0000000..c2bbcc4 --- /dev/null +++ b/common/soa.c @@ -0,0 +1,77 @@ + +#include "soa.h" + +#include <assert.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <ctype.h> + +#define SEPARATORS " \t\n\v()" + +int +soa_serial_increment (const char *old_soa, char *new_soa, unsigned int new_len) +{ + unsigned long value; + const char *p, *beg; + char *end; + unsigned int len; + + assert (old_soa); + assert (new_soa); + + /* Check that we can add a digit */ + len = strlen (old_soa); + assert (new_len >= len + 2); + + p = old_soa; + + /* Eat up space at the beginning */ + while (*p && strchr (SEPARATORS, *p)) + ++p; + /* Skip over domain name, to next sep */ + while (*p && !strchr (SEPARATORS, *p)) + ++p; + /* Eat up space between */ + while (*p && strchr (SEPARATORS, *p)) + ++p; + /* Skip over administrator email, to next sep */ + while (*p && !strchr (SEPARATORS, *p)) + ++p; + /* Eat up spaces between */ + while (*p && strchr (SEPARATORS, *p)) + ++p; + + if (*p == 0 || !isdigit (*p)) { + errno = EINVAL; + return -1; + } + + /* Parse the integer here */ + beg = p; + value = strtoul (beg, &end, 10); + if (beg == end || !strchr (SEPARATORS, *end)) { + errno = EINVAL; + return -1; + } + + /* Increment the integer appropriately */ + if (value == 0xFFFFFFFF) + value = 1; /* Zero is recommended against in RFC 1982 */ + else + ++value; + + /* Build up the new SOA */ + memset (new_soa, 0, new_len); + len = beg - old_soa; + strncpy (new_soa, old_soa, len); + snprintf (new_soa + len, 32, "%lu", value); + len = strlen (new_soa); + strcpy (new_soa + len, end); + + len = strlen (new_soa); + assert (len < new_len); + + return len; +} diff --git a/common/soa.h b/common/soa.h new file mode 100644 index 0000000..ca842e3 --- /dev/null +++ b/common/soa.h @@ -0,0 +1,6 @@ +#ifndef SOA_H_ +#define SOA_H_ + +int soa_serial_increment (const char *old_soa, char *new_soa, unsigned int new_len); + +#endif /*SOA_H_*/ diff --git a/common/strset.c b/common/strset.c new file mode 100644 index 0000000..801a358 --- /dev/null +++ b/common/strset.c @@ -0,0 +1,276 @@ +/* + * Copyright (c) 2008, Stefan Walter + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + */ + +/* + * Originally from apache 2.0 + */ + +/* Copyright 2000-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <sys/types.h> +#include <string.h> +#include <assert.h> +#include <stdlib.h> + +#include "strset.h" + +typedef struct _entry { + struct _entry* next; + unsigned int hash; + char *value; +} entry; + +struct _strset { + entry **array; + unsigned int count; + unsigned int max; +}; + +#define INITIAL_MAX 15 /* tunable == 2^n - 1 */ + +static entry** +alloc_array (unsigned int max) +{ + void *ret; + size_t len; + + assert (max); + + len = sizeof (entry*) * (max + 1); + ret = strset_malloc (len); + if (ret) + memset (ret, 0, len); + + return ret; +} + +strset* +strset_create (void) +{ + strset *set = strset_malloc (sizeof (strset)); + if (set) { + memset (set, 0, sizeof (strset)); + set->max = INITIAL_MAX; + set->array = alloc_array (set->max); + if (!set->array) { + strset_free (set); + set = NULL; + } + } + + return set; +} + +void +strset_destroy (strset *set) +{ + unsigned int i; + entry *ent, *next; + + if (!set) + return; + + /* Free all the entries */ + for (i = 0; i < set->max; ++i) { + next = NULL; + for (ent = set->array[i]; ent; ent = next) { + next = ent->next; + + assert (ent->value); + strset_free (ent); + } + } + + /* Free the set itself */ + strset_free (set->array); + strset_free (set); +} + +static int +expand_array (strset* set) +{ + entry** new_array; + entry* ent, *next; + unsigned int new_max, n, i; + + assert (set); + assert (set->max); + assert (set->array); + + new_max = set->max * 2 + 1; + new_array = alloc_array (new_max); + + if(!new_array) + return 0; + + /* Go through all the entries */ + for (i = 0; i < set->max; ++i) { + next = NULL; + for (ent = set->array[i]; ent; ent = next) { + next = ent->next; + + n = ent->hash & new_max; + ent->next = new_array[n]; + new_array[n] = ent; + } + } + + strset_free (set->array); + set->array = new_array; + set->max = new_max; + + return 1; +} + +static entry** +find_entry (strset* set, const char *value, int add) +{ + entry **ep, *e; + const char* p; + unsigned int hash; + size_t len; + + assert (set); + assert (set->max); + assert (set->array); + assert (value); + + hash = 0; + + for(p = value; *p; p++) + hash = hash * 33 + *p; + + /* scan linked list */ + for (ep = &set->array[hash & set->max], e = *ep; + e; ep = &e->next, e = *ep) + { + if (e->hash == hash && strcmp (e->value, value) == 0) + break; + } + + if (e || !add) + return ep; + + /* Add an entry */ + len = strlen (value); + e = strset_malloc (sizeof (entry) + len + 1); + if (e) { + /* Value data points past end of entry */ + e->value = ((char*)e) + sizeof (entry); + memcpy (e->value, value, len + 1); + + e->next = NULL; + e->hash = hash; + + *ep = e; + ++set->count; + } + + return ep; +} + +int +strset_has (strset *set, const char *value) +{ + entry **ep; + + assert (set); + + if (!value) + return 0; + + ep = find_entry (set, value, 0); + return ep && *ep ? 1 : 0; +} + +int +strset_add (strset *set, const char *value) +{ + entry **ep; + + assert (set); + + if (!value) + return -1; + + ep = find_entry (set, value, 1); + if (!ep || !*ep) + return -1; + + if (set->count > set->max) + if (!expand_array (set)) + return -1; + + return 0; +} + +int +strset_remove (strset *set, const char* value) +{ + entry **ep, *old; + + assert (set); + + if (!value) + return -1; + + ep = find_entry (set, value, 0); + if (!ep || !*ep) + return -1; + + old = *ep; + *ep = (*ep)->next; + --set->count; + strset_free (old); + return 0; +} + +unsigned int +strset_size (strset *set) +{ + assert (set); + return set->count; +} diff --git a/common/strset.h b/common/strset.h new file mode 100644 index 0000000..627856b --- /dev/null +++ b/common/strset.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2008, Stefan Walter + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + */ + +/* + * Originally from apache 2.0 + */ + +/* Copyright 2000-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STRSET_H__ +#define __STRSET_H__ + +/* Abstract type for hash tables. */ +typedef struct _strset strset; + +strset* strset_create (void); +void strset_destroy (strset *set); +unsigned int strset_size (strset *set); +int strset_has (strset *set, const char *value); +int strset_add (strset *set, const char *value); +int strset_remove (strset *set, const char *value); + +/* Need to rebuild strset.c if you set this variable */ +#if STRSET_CUSTMEM +extern void* strset_malloc (size_t length); +extern void strset_free (void *ptr); +#else +#define strset_malloc(x) malloc(x) +#define strset_free(x) free(x) +#endif + +#endif /* __STRSET_H__ */ |