]> git.proxmox.com Git - mirror_frr.git/blobdiff - babeld/util.c
Merge pull request #13206 from opensourcerouting/fix/docker_build_alpine_protobuf
[mirror_frr.git] / babeld / util.c
index c6606e4f0e39f8c3fe1e1ae722fc82d354c2a2df..4facdabbc6e34deb3fbfc90f2f89d989267ad9ce 100644 (file)
@@ -1,24 +1,7 @@
+// SPDX-License-Identifier: MIT
 /*
 Copyright (c) 2007, 2008 by Juliusz Chroboczek
 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
 */
 
 #ifdef HAVE_CONFIG_H
@@ -39,6 +22,8 @@ THE SOFTWARE.
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
+#include "lib/network.h"
+
 #include "babel_main.h"
 #include "babeld.h"
 #include "util.h"
@@ -51,7 +36,7 @@ roughly(int value)
     else if(value <= 1)
         return value;
     else
-        return value * 3 / 4 + random() % (value / 2);
+        return value * 3 / 4 + frr_weak_random() % (value / 2);
 }
 
 /* d = s1 - s2 */
@@ -145,7 +130,7 @@ timeval_min_sec(struct timeval *d, time_t secs)
 {
     if(d->tv_sec == 0 || d->tv_sec > secs) {
         d->tv_sec = secs;
-        d->tv_usec = random() % 1000000;
+        d->tv_usec = frr_weak_random() % 1000000;
     }
 }
 
@@ -214,33 +199,13 @@ parse_nat(const char *string)
     return (int)l;
 }
 
-int
-in_prefix(const unsigned char *restrict address,
-          const unsigned char *restrict prefix, unsigned char plen)
-{
-    unsigned char m;
-
-    if(plen > 128)
-        plen = 128;
-
-    if(memcmp(address, prefix, plen / 8) != 0)
-        return 0;
-
-    if(plen % 8 == 0)
-        return 1;
-
-    m = 0xFF << (8 - (plen % 8));
-
-    return ((address[plen / 8] & m) == (prefix[plen / 8] & m));
-}
-
 unsigned char *
 mask_prefix(unsigned char *restrict ret,
             const unsigned char *restrict prefix, unsigned char plen)
 {
-    if(plen >= 128) {
-        memcpy(ret, prefix, 16);
-        return ret;
+       if (plen >= IPV6_MAX_BITLEN) {
+               memcpy(ret, prefix, IPV6_MAX_BYTELEN);
+               return ret;
     }
 
     memset(ret, 0, 16);
@@ -327,9 +292,10 @@ parse_address(const char *address, unsigned char *addr_r, int *af_r)
 
     rc = inet_pton(AF_INET6, address, &ina6);
     if(rc > 0) {
-        memcpy(addr_r, &ina6, 16);
-        if(af_r) *af_r = AF_INET6;
-        return 0;
+           memcpy(addr_r, &ina6, IPV6_MAX_BYTELEN);
+           if (af_r)
+                   *af_r = AF_INET6;
+           return 0;
     }
 
     return -1;
@@ -431,13 +397,13 @@ uchar_to_inaddr(struct in_addr *dest, const unsigned char *src)
 void
 in6addr_to_uchar(unsigned char *dest, const struct in6_addr *src)
 {
-    memcpy(dest, src, 16);
+       memcpy(dest, src, IPV6_MAX_BYTELEN);
 }
 
 void
 uchar_to_in6addr(struct in6_addr *dest, const unsigned char *src)
 {
-    memcpy(dest, src, 16);
+       memcpy(dest, src, IPV6_MAX_BYTELEN);
 }
 
 int