2 * Copyright (C) 2009 Luigi Rizzo, Marta Carbone, Universita` di Pisa
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $Id: glue.c 4540 2009-12-16 17:22:47Z marta $
29 * Userland functions missing in linux
37 /* dummy nat functions */
39 ipfw_show_nat(int ac, char **av)
41 fprintf(stderr, "%s unsupported\n", __FUNCTION__);
45 ipfw_config_nat(int ac, char **av)
47 fprintf(stderr, "%s unsupported\n", __FUNCTION__);
52 int optreset; /* missing in linux */
55 #if defined( __linux__ ) || defined(_WIN32)
57 * not implemented in linux.
58 * taken from /usr/src/lib/libc/string/strlcpy.c
61 strlcpy(char *dst, const char *src, size_t siz)
67 /* Copy as many bytes as will fit */
68 if (n != 0 && --n != 0) {
70 if ((*d++ = *s++) == 0)
75 /* Not enough room in dst, add NUL and traverse rest of src */
78 *d = '\0'; /* NUL-terminate dst */
83 return(s - src - 1); /* count does not include NUL */
87 /* missing in linux and windows */
89 strtonum(const char *nptr, long long minval, long long maxval,
92 return strtoll(nptr, (char **)errstr, 0);
96 * set or get system information
97 * XXX lock acquisition/serialize calls
99 * we export this as sys/module/ipfw_mod/parameters/___
100 * This function get or/and set the value of the sysctl passed by
101 * the name parameter. If the old value is not desired,
102 * oldp and oldlenp should be set to NULL.
105 * I do not know how this works in FreeBSD in the case
106 * where there are no write permission on the sysctl var.
107 * We read the value and set return variables in any way
108 * but returns -1 on write failures, regardless the
111 * Since there is no information on types, in the following
112 * code we assume a lenght of 4 is a int.
114 * Returns 0 on success, -1 on errors.
117 sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp,
121 char *basename = "/sys/module/ipfw_mod/parameters/";
122 char filename[256]; /* full filename */
124 int ret = 0; /* return value */
127 if (name == NULL) /* XXX set errno */
130 /* locate the filename */
131 varp = strrchr(name, '.');
132 if (varp == NULL) /* XXX set errno */
135 snprintf(filename, sizeof(filename), "%s%s", basename, varp+1);
138 * XXX we could open the file here, in rw mode
139 * but need to check if a file have write
143 /* check parameters */
144 if (oldp && oldlenp) { /* read mode */
145 fp = fopen(filename, "r");
147 fprintf(stderr, "%s fopen error reading filename %s\n", __FUNCTION__, filename);
151 if (fscanf(fp, "%d", &d) == 1)
152 memcpy(oldp, &d, *oldlenp);
159 if (newp && newlen) { /* write */
160 fp = fopen(filename, "w");
162 fprintf(stderr, "%s fopen error writing filename %s\n", __FUNCTION__, filename);
166 if (fprintf(fp, "%d", *(int*)newp) < 1)
175 #endif /* __linux__ || _WIN32 */