LCOV - code coverage report
Current view: top level - lib/replace/tests - getifaddrs.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 30 33 90.9 %
Date: 2021-09-23 10:06:22 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Unix SMB/CIFS implementation.
       3             :  *
       4             :  * libreplace getifaddrs test
       5             :  *
       6             :  * Copyright (C) Michael Adam <obnox@samba.org> 2008
       7             :  *
       8             :  *   ** NOTE! The following LGPL license applies to the replace
       9             :  *   ** library. This does NOT imply that all of Samba is released
      10             :  *   ** under the LGPL
      11             :  *
      12             :  * This library is free software; you can redistribute it and/or
      13             :  * modify it under the terms of the GNU Lesser General Public
      14             :  * License as published by the Free Software Foundation; either
      15             :  * version 3 of the License, or (at your option) any later version.
      16             :  *
      17             :  * This library is distributed in the hope that it will be useful,
      18             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      19             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      20             :  * Library General Public License for more details.
      21             :  *
      22             :  * You should have received a copy of the GNU Lesser General Public
      23             :  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      24             :  */
      25             : 
      26             : #ifndef AUTOCONF_TEST
      27             : #include "replace.h"
      28             : #include "system/network.h"
      29             : #include "replace-test.h"
      30             : #endif
      31             : 
      32             : #ifdef HAVE_INET_NTOP
      33             : #define rep_inet_ntop inet_ntop
      34             : #endif
      35             : 
      36          36 : static const char *format_sockaddr(struct sockaddr *addr,
      37             :                                    char *addrstring,
      38             :                                    socklen_t addrlen)
      39             : {
      40          36 :         const char *result = NULL;
      41             : 
      42          36 :         if (addr->sa_family == AF_INET) {
      43          16 :                 result = rep_inet_ntop(AF_INET,
      44          16 :                                        &((struct sockaddr_in *)addr)->sin_addr,
      45             :                                        addrstring,
      46             :                                        addrlen);
      47             : #ifdef HAVE_STRUCT_SOCKADDR_IN6
      48          20 :         } else if (addr->sa_family == AF_INET6) {
      49          12 :                 result = rep_inet_ntop(AF_INET6,
      50          12 :                                        &((struct sockaddr_in6 *)addr)->sin6_addr,
      51             :                                        addrstring,
      52             :                                        addrlen);
      53             : #endif
      54             :         }
      55          36 :         return result;
      56             : }
      57             : 
      58           4 : int getifaddrs_test(void)
      59             : {
      60           4 :         struct ifaddrs *ifs = NULL;
      61           4 :         struct ifaddrs *ifs_head = NULL;
      62             :         int ret;
      63             : 
      64           4 :         ret = getifaddrs(&ifs);
      65           4 :         ifs_head = ifs;
      66           4 :         if (ret != 0) {
      67           0 :                 fprintf(stderr, "getifaddrs() failed: %s\n", strerror(errno));
      68           0 :                 return 1;
      69             :         }
      70             : 
      71          29 :         while (ifs) {
      72          29 :                 printf("%-10s ", ifs->ifa_name);
      73          22 :                 if (ifs->ifa_addr != NULL) {
      74             :                         char addrstring[INET6_ADDRSTRLEN];
      75             :                         const char *result;
      76             : 
      77          22 :                         result = format_sockaddr(ifs->ifa_addr,
      78             :                                                  addrstring,
      79             :                                                  sizeof(addrstring));
      80          22 :                         if (result != NULL) {
      81           9 :                                 printf("IP=%s ", addrstring);
      82             :                         }
      83             : 
      84          22 :                         if (ifs->ifa_netmask != NULL) {
      85          14 :                                 result = format_sockaddr(ifs->ifa_netmask,
      86             :                                                          addrstring,
      87             :                                                          sizeof(addrstring));
      88          14 :                                 if (result != NULL) {
      89           9 :                                         printf("NETMASK=%s", addrstring);
      90             :                                 }
      91             :                         } else {
      92           8 :                                 printf("AF=%d ", ifs->ifa_addr->sa_family);
      93             :                         }
      94             :                 } else {
      95           0 :                         printf("<no address>");
      96             :                 }
      97             : 
      98          22 :                 printf("\n");
      99          22 :                 ifs = ifs->ifa_next;
     100             :         }
     101             : 
     102           4 :         freeifaddrs(ifs_head);
     103             : 
     104           4 :         return 0;
     105             : }

Generated by: LCOV version 1.13