-/*
- * This file is free software: you may copy, redistribute and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation, either version 2 of the License, or (at your
- * option) any later version.
- *
- * This file 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
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * This file incorporates work covered by the following copyright and
- * permission notice:
- *
+/*
Copyright (c) 2007, 2008 by Juliusz Chroboczek
Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
THE SOFTWARE.
*/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
+#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "babeld.h"
#include "util.h"
-unsigned
-roughly(unsigned value)
+int
+roughly(int value)
{
- return value * 3 / 4 + random() % (value / 2);
+ if(value < 0)
+ return -roughly(-value);
+ else if(value <= 1)
+ return value;
+ else
+ return value * 3 / 4 + random() % (value / 2);
}
/* d = s1 - s2 */
/* d = s + msecs */
void
-timeval_add_msec(struct timeval *d, const struct timeval *s, const int msecs)
+timeval_add_msec(struct timeval *d, const struct timeval *s, int msecs)
{
int usecs;
d->tv_sec = s->tv_sec + msecs / 1000;
return -1;
}
+/* There's no good name for a positive int in C, call it nat. */
+int
+parse_nat(const char *string)
+{
+ long l;
+ char *end;
+
+ l = strtol(string, &end, 0);
+
+ while(*end == ' ' || *end == '\t')
+ end++;
+ if(*end != '\0')
+ return -1;
+
+ if(l < 0 || l > INT_MAX)
+ return -1;
+
+ return (int)l;
+}
+
int
in_prefix(const unsigned char *restrict address,
const unsigned char *restrict prefix, unsigned char plen)
return ret;
}
-static const unsigned char v4prefix[16] =
+const unsigned char v4prefix[16] =
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
static const unsigned char llprefix[16] =
return buf[i];
}
-const char *format_bool(const int b) {
- return b ? "true" : "false";
+const char *
+format_thousands(unsigned int value)
+{
+ static char buf[4][15];
+ static int i = 0;
+ i = (i + 1) % 4;
+ snprintf(buf[i], 15, "%u.%.3u", value / 1000, value % 1000);
+ return buf[i];
}
int
rc = inet_pton(AF_INET, address, &ina);
if(rc > 0) {
- memcpy(addr_r, v4prefix, 12);
- memcpy(addr_r + 12, &ina, 4);
+ v4tov6(addr_r, (const unsigned char *)&ina);
if(af_r) *af_r = AF_INET;
return 0;
}
void
inaddr_to_uchar(unsigned char *dest, const struct in_addr *src)
{
- memcpy(dest, v4prefix, 12);
- memcpy(dest + 12, src, 4);
+ v4tov6(dest, (const unsigned char *)src);
assert(v4mapped(dest));
}
}
int
-daemonise()
+daemonise(void)
{
int rc;