root/opal/mca/pmix/pmix4x/pmix/src/mca/pif/linux_ipv6/pif_linux_ipv6.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. if_linux_ipv6_open

   1 /*
   2  * Copyright (c) 2010      Cisco Systems, Inc.  All rights reserved.
   3  * Copyright (c) 2010      Oracle and/or its affiliates.  All rights reserved.
   4  * Copyright (c) 2018      Intel, Inc.  All rights reserved.
   5  * $COPYRIGHT$
   6  *
   7  * Additional copyrights may follow
   8  *
   9  * $HEADER$
  10  */
  11 
  12 #include "pmix_config.h"
  13 #include "pmix_common.h"
  14 
  15 #include <string.h>
  16 #ifdef HAVE_UNISTD_H
  17 #include <unistd.h>
  18 #endif
  19 #include <errno.h>
  20 #ifdef HAVE_SYS_TYPES_H
  21 #include <sys/types.h>
  22 #endif
  23 #ifdef HAVE_SYS_SOCKET_H
  24 #include <sys/socket.h>
  25 #endif
  26 #ifdef HAVE_SYS_SOCKIO_H
  27 #include <sys/sockio.h>
  28 #endif
  29 #ifdef HAVE_SYS_IOCTL_H
  30 #include <sys/ioctl.h>
  31 #endif
  32 #ifdef HAVE_NETINET_IN_H
  33 #include <netinet/in.h>
  34 #endif
  35 #ifdef HAVE_ARPA_INET_H
  36 #include <arpa/inet.h>
  37 #endif
  38 #ifdef HAVE_NET_IF_H
  39 #include <net/if.h>
  40 #endif
  41 #ifdef HAVE_NETDB_H
  42 #include <netdb.h>
  43 #endif
  44 #ifdef HAVE_IFADDRS_H
  45 #include <ifaddrs.h>
  46 #endif
  47 
  48 #include "src/util/output.h"
  49 #include "src/util/pif.h"
  50 #include "src/mca/pif/pif.h"
  51 #include "src/mca/pif/base/base.h"
  52 
  53 static int if_linux_ipv6_open(void);
  54 
  55 /* Discovers Linux IPv6 interfaces */
  56 pmix_pif_base_component_t mca_pif_linux_ipv6_component = {
  57     /* First, the mca_component_t struct containing meta information
  58        about the component itself */
  59     .base = {
  60         PMIX_PIF_BASE_VERSION_2_0_0,
  61 
  62         /* Component name and version */
  63         "linux_ipv6",
  64         PMIX_MAJOR_VERSION,
  65         PMIX_MINOR_VERSION,
  66         PMIX_RELEASE_VERSION,
  67 
  68         /* Component open and close functions */
  69         if_linux_ipv6_open,
  70         NULL
  71     },
  72     .data = {
  73         /* This component is checkpointable */
  74         PMIX_MCA_BASE_METADATA_PARAM_CHECKPOINT
  75     },
  76 };
  77 
  78 /* configure using getifaddrs(3) */
  79 static int if_linux_ipv6_open(void)
  80 {
  81     FILE *f;
  82     if ((f = fopen("/proc/net/if_inet6", "r"))) {
  83         char ifname[IF_NAMESIZE];
  84         unsigned int idx, pfxlen, scope, dadstat;
  85         struct in6_addr a6;
  86         int iter;
  87         uint32_t flag;
  88         unsigned int addrbyte[16];
  89 
  90         while (fscanf(f, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x %x %x %x %x %20s\n",
  91                       &addrbyte[0], &addrbyte[1], &addrbyte[2], &addrbyte[3],
  92                       &addrbyte[4], &addrbyte[5], &addrbyte[6], &addrbyte[7],
  93                       &addrbyte[8], &addrbyte[9], &addrbyte[10], &addrbyte[11],
  94                       &addrbyte[12], &addrbyte[13], &addrbyte[14], &addrbyte[15],
  95                       &idx, &pfxlen, &scope, &dadstat, ifname) != EOF) {
  96             pmix_pif_t *intf;
  97 
  98             pmix_output_verbose(1, pmix_pif_base_framework.framework_output,
  99                                 "found interface %2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x scope %x\n",
 100                                 addrbyte[0], addrbyte[1], addrbyte[2], addrbyte[3],
 101                                 addrbyte[4], addrbyte[5], addrbyte[6], addrbyte[7],
 102                                 addrbyte[8], addrbyte[9], addrbyte[10], addrbyte[11],
 103                                 addrbyte[12], addrbyte[13], addrbyte[14], addrbyte[15], scope);
 104 
 105             /* we don't want any other scope less than link-local */
 106             if (scope < 0x20) {
 107                 pmix_output_verbose(1, pmix_pif_base_framework.framework_output,
 108                                     "skipping interface %2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x scope %x\n",
 109                                     addrbyte[0], addrbyte[1], addrbyte[2], addrbyte[3],
 110                                     addrbyte[4], addrbyte[5], addrbyte[6], addrbyte[7],
 111                                     addrbyte[8], addrbyte[9], addrbyte[10], addrbyte[11],
 112                                     addrbyte[12], addrbyte[13], addrbyte[14], addrbyte[15], scope);
 113                 continue;
 114             }
 115 
 116             intf = PMIX_NEW(pmix_pif_t);
 117             if (NULL == intf) {
 118                 pmix_output(0, "pmix_ifinit: unable to allocate %lu bytes\n",
 119                             (unsigned long)sizeof(pmix_pif_t));
 120                 fclose(f);
 121                 return PMIX_ERR_OUT_OF_RESOURCE;
 122             }
 123             intf->af_family = AF_INET6;
 124 
 125             for (iter = 0; iter < 16; iter++) {
 126                 a6.s6_addr[iter] = addrbyte[iter];
 127             }
 128 
 129             /* now construct the pmix_pif_t */
 130             pmix_strncpy(intf->if_name, ifname, IF_NAMESIZE-1);
 131             intf->if_index = pmix_list_get_size(&pmix_if_list)+1;
 132             intf->if_kernel_index = (uint16_t) idx;
 133             ((struct sockaddr_in6*) &intf->if_addr)->sin6_addr = a6;
 134             ((struct sockaddr_in6*) &intf->if_addr)->sin6_family = AF_INET6;
 135             ((struct sockaddr_in6*) &intf->if_addr)->sin6_scope_id = scope;
 136             intf->if_mask = pfxlen;
 137             if (PMIX_SUCCESS == pmix_ifindextoflags(pmix_ifnametoindex (ifname), &flag)) {
 138                 intf->if_flags = flag;
 139             } else {
 140                 intf->if_flags = IFF_UP;
 141             }
 142 
 143             /* copy new interface information to heap and append
 144                to list */
 145             pmix_list_append(&pmix_if_list, &(intf->super));
 146             pmix_output_verbose(1, pmix_pif_base_framework.framework_output,
 147                                 "added interface %2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x\n",
 148                                 addrbyte[0], addrbyte[1], addrbyte[2], addrbyte[3],
 149                                 addrbyte[4], addrbyte[5], addrbyte[6], addrbyte[7],
 150                                 addrbyte[8], addrbyte[9], addrbyte[10], addrbyte[11],
 151                                 addrbyte[12], addrbyte[13], addrbyte[14], addrbyte[15]);
 152         } /* of while */
 153         fclose(f);
 154     }
 155 
 156     return PMIX_SUCCESS;
 157 }

/* [<][>][^][v][top][bottom][index][help] */