]> git.proxmox.com Git - mirror_frr.git/blobdiff - babeld/util.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[mirror_frr.git] / babeld / util.c
index 011f3824eb6a5f0bbc361badd503630baf30a68f..c6606e4f0e39f8c3fe1e1ae722fc82d354c2a2df 100644 (file)
@@ -1,20 +1,4 @@
-/*  
- *  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
 
@@ -37,6 +21,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
@@ -44,6 +32,7 @@ THE SOFTWARE.
 #include <time.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <limits.h>
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -54,10 +43,15 @@ THE SOFTWARE.
 #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 */
@@ -97,7 +91,7 @@ timeval_minus_msec(const struct timeval *s1, const struct timeval *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;
@@ -200,6 +194,26 @@ parse_msec(const char *string)
     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)
@@ -237,7 +251,7 @@ mask_prefix(unsigned char *restrict ret,
     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] =
@@ -287,8 +301,14 @@ format_eui64(const unsigned char *eui)
     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
@@ -300,8 +320,7 @@ parse_address(const char *address, unsigned char *addr_r, int *af_r)
 
     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;
     }
@@ -398,8 +417,7 @@ v4tov6(unsigned char *dst, const unsigned char *src)
 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));
 }
 
@@ -423,7 +441,7 @@ uchar_to_in6addr(struct in6_addr *dest, const unsigned char *src)
 }
 
 int
-daemonise()
+daemonise(void)
 {
     int rc;