11/*
2- * Copyright (c) 2010 Cisco Systems, Inc. All rights reserved.
2+ * Copyright (c) 2010-2019 Cisco Systems, Inc. All rights reserved.
33 * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
44 * Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights
55 * reserved.
4848#include "opal/constants.h"
4949#include "opal/util/if.h"
5050#include "opal/util/output.h"
51+ #include "opal/util/show_help.h"
5152#include "opal/util/string_copy.h"
5253#include "opal/mca/if/if.h"
5354#include "opal/mca/if/base/base.h"
5455
56+ #define LOG_PREFIX "mca: if: linux_ipv6: "
57+
5558static int if_linux_ipv6_open (void );
5659
5760/* Discovers Linux IPv6 interfaces */
@@ -77,7 +80,36 @@ opal_if_base_component_t mca_if_linux_ipv6_component = {
7780 },
7881};
7982
80- /* configure using getifaddrs(3) */
83+ #if OPAL_ENABLE_IPV6
84+ static bool hex2int (char hex , int * dst )
85+ {
86+ if ('0' <= hex && hex <= '9' ) {
87+ * dst = hex - '0' ;
88+ } else if ('A' <= hex && hex <= 'F' ) {
89+ * dst = hex - 'A' + 10 ;
90+ } else if ('a' <= hex && hex <= 'f' ) {
91+ * dst = hex - 'a' + 10 ;
92+ } else {
93+ return false;
94+ }
95+ return true;
96+
97+ }
98+
99+ static bool hexdecode (const char * src , uint8_t * dst , size_t dstsize )
100+ {
101+ int hi , lo ;
102+ for (size_t i = 0 ; i < dstsize ; i ++ ) {
103+ if (hex2int (src [i * 2 ], & hi ) && hex2int (src [i * 2 + 1 ], & lo )) {
104+ dst [i ] = 16 * hi + lo ;
105+ } else {
106+ return false;
107+ }
108+ }
109+ return true;
110+ }
111+ #endif
112+
81113static int if_linux_ipv6_open (void )
82114{
83115#if OPAL_ENABLE_IPV6
@@ -86,49 +118,45 @@ static int if_linux_ipv6_open(void)
86118 char ifname [IF_NAMESIZE ];
87119 unsigned int idx , pfxlen , scope , dadstat ;
88120 struct in6_addr a6 ;
89- int iter ;
90121 uint32_t flag ;
91- unsigned int addrbyte [16 ];
122+ char addrhex [sizeof a6 .s6_addr * 2 + 1 ];
123+ char addrstr [INET6_ADDRSTRLEN ];
92124
93- 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" ,
94- & addrbyte [0 ], & addrbyte [1 ], & addrbyte [2 ], & addrbyte [3 ],
95- & addrbyte [4 ], & addrbyte [5 ], & addrbyte [6 ], & addrbyte [7 ],
96- & addrbyte [8 ], & addrbyte [9 ], & addrbyte [10 ], & addrbyte [11 ],
97- & addrbyte [12 ], & addrbyte [13 ], & addrbyte [14 ], & addrbyte [15 ],
125+ while (fscanf (f , "%s %x %x %x %x %s\n" , addrhex ,
98126 & idx , & pfxlen , & scope , & dadstat , ifname ) != EOF ) {
99127 opal_if_t * intf ;
100128
129+ if (!hexdecode (addrhex , a6 .s6_addr , sizeof a6 .s6_addr )) {
130+ char hostname [OPAL_MAXHOSTNAMELEN ] = {0 };
131+ gethostname (hostname , sizeof (hostname ));
132+ opal_show_help ("help-opal-if-linux-ipv6.txt" ,
133+ "fail to parse if_inet6" , true,
134+ hostname , ifname , addrhex );
135+ continue ;
136+ };
137+ inet_ntop (AF_INET6 , a6 .s6_addr , addrstr , sizeof addrstr );
138+
101139 opal_output_verbose (1 , opal_if_base_framework .framework_output ,
102- "found interface %2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x scope %x\n" ,
103- addrbyte [0 ], addrbyte [1 ], addrbyte [2 ], addrbyte [3 ],
104- addrbyte [4 ], addrbyte [5 ], addrbyte [6 ], addrbyte [7 ],
105- addrbyte [8 ], addrbyte [9 ], addrbyte [10 ], addrbyte [11 ],
106- addrbyte [12 ], addrbyte [13 ], addrbyte [14 ], addrbyte [15 ], scope );
140+ LOG_PREFIX "found interface %s inet6 %s scope %x\n" ,
141+ ifname , addrstr , scope );
107142
108143 /* Only interested in global (0x00) scope */
109144 if (scope != 0x00 ) {
110145 opal_output_verbose (1 , opal_if_base_framework .framework_output ,
111- "skipping interface %2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x scope %x\n" ,
112- addrbyte [0 ], addrbyte [1 ], addrbyte [2 ], addrbyte [3 ],
113- addrbyte [4 ], addrbyte [5 ], addrbyte [6 ], addrbyte [7 ],
114- addrbyte [8 ], addrbyte [9 ], addrbyte [10 ], addrbyte [11 ],
115- addrbyte [12 ], addrbyte [13 ], addrbyte [14 ], addrbyte [15 ], scope );
146+ LOG_PREFIX "skipped interface %s inet6 %s scope %x\n" ,
147+ ifname , addrstr , scope );
116148 continue ;
117149 }
118150
119151 intf = OBJ_NEW (opal_if_t );
120152 if (NULL == intf ) {
121- opal_output (0 , "opal_ifinit: unable to allocate %lu bytes\n" ,
153+ opal_output (0 , LOG_PREFIX " unable to allocate %lu bytes\n" ,
122154 (unsigned long )sizeof (opal_if_t ));
123155 fclose (f );
124156 return OPAL_ERR_OUT_OF_RESOURCE ;
125157 }
126158 intf -> af_family = AF_INET6 ;
127159
128- for (iter = 0 ; iter < 16 ; iter ++ ) {
129- a6 .s6_addr [iter ] = addrbyte [iter ];
130- }
131-
132160 /* now construct the opal_if_t */
133161 opal_string_copy (intf -> if_name , ifname , IF_NAMESIZE );
134162 intf -> if_index = opal_list_get_size (& opal_if_list )+ 1 ;
@@ -147,17 +175,12 @@ static int if_linux_ipv6_open(void)
147175 to list */
148176 opal_list_append (& opal_if_list , & (intf -> super ));
149177 opal_output_verbose (1 , opal_if_base_framework .framework_output ,
150- "added interface %2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x:%2x%2x\n" ,
151- addrbyte [0 ], addrbyte [1 ], addrbyte [2 ], addrbyte [3 ],
152- addrbyte [4 ], addrbyte [5 ], addrbyte [6 ], addrbyte [7 ],
153- addrbyte [8 ], addrbyte [9 ], addrbyte [10 ], addrbyte [11 ],
154- addrbyte [12 ], addrbyte [13 ], addrbyte [14 ], addrbyte [15 ]);
178+ LOG_PREFIX "added interface %s inet6 %s scope %x\n" ,
179+ ifname , addrstr , scope );
155180 } /* of while */
156181 fclose (f );
157182 }
158183#endif /* OPAL_ENABLE_IPV6 */
159184
160185 return OPAL_SUCCESS ;
161186}
162-
163-
0 commit comments