LCOV - code coverage report
Current view: top level - basic - in-addr-util.c (source / functions) Hit Total Coverage
Test: systemd test coverage Lines: 89 166 53.6 %
Date: 2015-07-29 18:47:03 Functions: 6 13 46.2 %

          Line data    Source code
       1             : /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
       2             : 
       3             : /***
       4             :   This file is part of systemd.
       5             : 
       6             :   Copyright 2014 Lennart Poettering
       7             : 
       8             :   systemd is free software; you can redistribute it and/or modify it
       9             :   under the terms of the GNU Lesser General Public License as published by
      10             :   the Free Software Foundation; either version 2.1 of the License, or
      11             :   (at your option) any later version.
      12             : 
      13             :   systemd is distributed in the hope that it will be useful, but
      14             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      16             :   Lesser General Public License for more details.
      17             : 
      18             :   You should have received a copy of the GNU Lesser General Public License
      19             :   along with systemd; If not, see <http://www.gnu.org/licenses/>.
      20             : ***/
      21             : 
      22             : #include <arpa/inet.h>
      23             : 
      24             : #include "in-addr-util.h"
      25             : 
      26           0 : int in_addr_is_null(int family, const union in_addr_union *u) {
      27           0 :         assert(u);
      28             : 
      29           0 :         if (family == AF_INET)
      30           0 :                 return u->in.s_addr == 0;
      31             : 
      32           0 :         if (family == AF_INET6)
      33             :                 return
      34           0 :                         u->in6.s6_addr32[0] == 0 &&
      35           0 :                         u->in6.s6_addr32[1] == 0 &&
      36           0 :                         u->in6.s6_addr32[2] == 0 &&
      37           0 :                         u->in6.s6_addr32[3] == 0;
      38             : 
      39           0 :         return -EAFNOSUPPORT;
      40             : }
      41             : 
      42           0 : int in_addr_is_link_local(int family, const union in_addr_union *u) {
      43           0 :         assert(u);
      44             : 
      45           0 :         if (family == AF_INET)
      46           0 :                 return (be32toh(u->in.s_addr) & 0xFFFF0000) == (169U << 24 | 254U << 16);
      47             : 
      48           0 :         if (family == AF_INET6)
      49           0 :                 return IN6_IS_ADDR_LINKLOCAL(&u->in6);
      50             : 
      51           0 :         return -EAFNOSUPPORT;
      52             : }
      53             : 
      54          14 : int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {
      55          14 :         assert(a);
      56          14 :         assert(b);
      57             : 
      58          14 :         if (family == AF_INET)
      59           6 :                 return a->in.s_addr == b->in.s_addr;
      60             : 
      61           8 :         if (family == AF_INET6)
      62             :                 return
      63          24 :                         a->in6.s6_addr32[0] == b->in6.s6_addr32[0] &&
      64          16 :                         a->in6.s6_addr32[1] == b->in6.s6_addr32[1] &&
      65          24 :                         a->in6.s6_addr32[2] == b->in6.s6_addr32[2] &&
      66           8 :                         a->in6.s6_addr32[3] == b->in6.s6_addr32[3];
      67             : 
      68           0 :         return -EAFNOSUPPORT;
      69             : }
      70             : 
      71          18 : int in_addr_prefix_intersect(
      72             :                 int family,
      73             :                 const union in_addr_union *a,
      74             :                 unsigned aprefixlen,
      75             :                 const union in_addr_union *b,
      76             :                 unsigned bprefixlen) {
      77             : 
      78             :         unsigned m;
      79             : 
      80          18 :         assert(a);
      81          18 :         assert(b);
      82             : 
      83             :         /* Checks whether there are any addresses that are in both
      84             :          * networks */
      85             : 
      86          18 :         m = MIN(aprefixlen, bprefixlen);
      87             : 
      88          18 :         if (family == AF_INET) {
      89             :                 uint32_t x, nm;
      90             : 
      91           9 :                 x = be32toh(a->in.s_addr ^ b->in.s_addr);
      92           9 :                 nm = (m == 0) ? 0 : 0xFFFFFFFFUL << (32 - m);
      93             : 
      94           9 :                 return (x & nm) == 0;
      95             :         }
      96             : 
      97           9 :         if (family == AF_INET6) {
      98             :                 unsigned i;
      99             : 
     100           9 :                 if (m > 128)
     101           0 :                         m = 128;
     102             : 
     103         136 :                 for (i = 0; i < 16; i++) {
     104             :                         uint8_t x, nm;
     105             : 
     106         130 :                         x = a->in6.s6_addr[i] ^ b->in6.s6_addr[i];
     107             : 
     108         130 :                         if (m < 8)
     109          44 :                                 nm = 0xFF << (8 - m);
     110             :                         else
     111          86 :                                 nm = 0xFF;
     112             : 
     113         130 :                         if ((x & nm) != 0)
     114           3 :                                 return 0;
     115             : 
     116         127 :                         if (m > 8)
     117          81 :                                 m -= 8;
     118             :                         else
     119          46 :                                 m = 0;
     120             :                 }
     121             : 
     122           6 :                 return 1;
     123             :         }
     124             : 
     125           0 :         return -EAFNOSUPPORT;
     126             : }
     127             : 
     128          14 : int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen) {
     129          14 :         assert(u);
     130             : 
     131             :         /* Increases the network part of an address by one. Returns
     132             :          * positive it that succeeds, or 0 if this overflows. */
     133             : 
     134          14 :         if (prefixlen <= 0)
     135           0 :                 return 0;
     136             : 
     137          14 :         if (family == AF_INET) {
     138             :                 uint32_t c, n;
     139             : 
     140           6 :                 if (prefixlen > 32)
     141           0 :                         prefixlen = 32;
     142             : 
     143           6 :                 c = be32toh(u->in.s_addr);
     144           6 :                 n = c + (1UL << (32 - prefixlen));
     145           6 :                 if (n < c)
     146           2 :                         return 0;
     147           4 :                 n &= 0xFFFFFFFFUL << (32 - prefixlen);
     148             : 
     149           4 :                 u->in.s_addr = htobe32(n);
     150           4 :                 return 1;
     151             :         }
     152             : 
     153           8 :         if (family == AF_INET6) {
     154           8 :                 struct in6_addr add = {}, result;
     155           8 :                 uint8_t overflow = 0;
     156             :                 unsigned i;
     157             : 
     158           8 :                 if (prefixlen > 128)
     159           0 :                         prefixlen = 128;
     160             : 
     161             :                 /* First calculate what we have to add */
     162           8 :                 add.s6_addr[(prefixlen-1) / 8] = 1 << (7 - (prefixlen-1) % 8);
     163             : 
     164         136 :                 for (i = 16; i > 0; i--) {
     165         128 :                         unsigned j = i - 1;
     166             : 
     167         128 :                         result.s6_addr[j] = u->in6.s6_addr[j] + add.s6_addr[j] + overflow;
     168         128 :                         overflow = (result.s6_addr[j] < u->in6.s6_addr[j]);
     169             :                 }
     170             : 
     171           8 :                 if (overflow)
     172           2 :                         return 0;
     173             : 
     174           6 :                 u->in6 = result;
     175           6 :                 return 1;
     176             :         }
     177             : 
     178           0 :         return -EAFNOSUPPORT;
     179             : }
     180             : 
     181          10 : int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
     182             :         char *x;
     183             :         size_t l;
     184             : 
     185          10 :         assert(u);
     186          10 :         assert(ret);
     187             : 
     188          10 :         if (family == AF_INET)
     189           5 :                 l = INET_ADDRSTRLEN;
     190           5 :         else if (family == AF_INET6)
     191           5 :                 l = INET6_ADDRSTRLEN;
     192             :         else
     193           0 :                 return -EAFNOSUPPORT;
     194             : 
     195          10 :         x = new(char, l);
     196          10 :         if (!x)
     197           0 :                 return -ENOMEM;
     198             : 
     199          10 :         errno = 0;
     200          10 :         if (!inet_ntop(family, u, x, l)) {
     201           0 :                 free(x);
     202           0 :                 return errno ? -errno : -EINVAL;
     203             :         }
     204             : 
     205          10 :         *ret = x;
     206          10 :         return 0;
     207             : }
     208             : 
     209          76 : int in_addr_from_string(int family, const char *s, union in_addr_union *ret) {
     210             : 
     211          76 :         assert(s);
     212          76 :         assert(ret);
     213             : 
     214          76 :         if (!IN_SET(family, AF_INET, AF_INET6))
     215           0 :                 return -EAFNOSUPPORT;
     216             : 
     217          76 :         errno = 0;
     218          76 :         if (inet_pton(family, s, ret) <= 0)
     219           2 :                 return errno ? -errno : -EINVAL;
     220             : 
     221          74 :         return 0;
     222             : }
     223             : 
     224           5 : int in_addr_from_string_auto(const char *s, int *family, union in_addr_union *ret) {
     225             :         int r;
     226             : 
     227           5 :         assert(s);
     228           5 :         assert(family);
     229           5 :         assert(ret);
     230             : 
     231           5 :         r = in_addr_from_string(AF_INET, s, ret);
     232           5 :         if (r >= 0) {
     233           3 :                 *family = AF_INET;
     234           3 :                 return 0;
     235             :         }
     236             : 
     237           2 :         r = in_addr_from_string(AF_INET6, s, ret);
     238           2 :         if (r >= 0) {
     239           2 :                 *family = AF_INET6;
     240           2 :                 return 0;
     241             :         }
     242             : 
     243           0 :         return -EINVAL;
     244             : }
     245             : 
     246           0 : unsigned char in_addr_netmask_to_prefixlen(const struct in_addr *addr) {
     247           0 :         assert(addr);
     248             : 
     249           0 :         return 32 - u32ctz(be32toh(addr->s_addr));
     250             : }
     251             : 
     252           0 : struct in_addr* in_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen) {
     253           0 :         assert(addr);
     254           0 :         assert(prefixlen <= 32);
     255             : 
     256             :         /* Shifting beyond 32 is not defined, handle this specially. */
     257           0 :         if (prefixlen == 0)
     258           0 :                 addr->s_addr = 0;
     259             :         else
     260           0 :                 addr->s_addr = htobe32((0xffffffff << (32 - prefixlen)) & 0xffffffff);
     261             : 
     262           0 :         return addr;
     263             : }
     264             : 
     265           0 : int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
     266           0 :         uint8_t msb_octet = *(uint8_t*) addr;
     267             : 
     268             :         /* addr may not be aligned, so make sure we only access it byte-wise */
     269             : 
     270           0 :         assert(addr);
     271           0 :         assert(prefixlen);
     272             : 
     273           0 :         if (msb_octet < 128)
     274             :                 /* class A, leading bits: 0 */
     275           0 :                 *prefixlen = 8;
     276           0 :         else if (msb_octet < 192)
     277             :                 /* class B, leading bits 10 */
     278           0 :                 *prefixlen = 16;
     279           0 :         else if (msb_octet < 224)
     280             :                 /* class C, leading bits 110 */
     281           0 :                 *prefixlen = 24;
     282             :         else
     283             :                 /* class D or E, no default prefixlen */
     284           0 :                 return -ERANGE;
     285             : 
     286           0 :         return 0;
     287             : }
     288             : 
     289           0 : int in_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
     290             :         unsigned char prefixlen;
     291             :         int r;
     292             : 
     293           0 :         assert(addr);
     294           0 :         assert(mask);
     295             : 
     296           0 :         r = in_addr_default_prefixlen(addr, &prefixlen);
     297           0 :         if (r < 0)
     298           0 :                 return r;
     299             : 
     300           0 :         in_addr_prefixlen_to_netmask(mask, prefixlen);
     301           0 :         return 0;
     302             : }
     303             : 
     304           0 : int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen) {
     305           0 :         assert(addr);
     306             : 
     307           0 :         if (family == AF_INET) {
     308             :                 struct in_addr mask;
     309             : 
     310           0 :                 if (!in_addr_prefixlen_to_netmask(&mask, prefixlen))
     311           0 :                         return -EINVAL;
     312             : 
     313           0 :                 addr->in.s_addr &= mask.s_addr;
     314           0 :                 return 0;
     315             :         }
     316             : 
     317           0 :         if (family == AF_INET6) {
     318             :                 unsigned i;
     319             : 
     320           0 :                 for (i = 0; i < 16; i++) {
     321             :                         uint8_t mask;
     322             : 
     323           0 :                         if (prefixlen >= 8) {
     324           0 :                                 mask = 0xFF;
     325           0 :                                 prefixlen -= 8;
     326             :                         } else {
     327           0 :                                 mask = 0xFF << (8 - prefixlen);
     328           0 :                                 prefixlen = 0;
     329             :                         }
     330             : 
     331           0 :                         addr->in6.s6_addr[i] &= mask;
     332             :                 }
     333             : 
     334           0 :                 return 0;
     335             :         }
     336             : 
     337           0 :         return -EAFNOSUPPORT;
     338             : }

Generated by: LCOV version 1.11