5252#include "opal/mca/if/if.h"
5353#include "opal/mca/if/base/base.h"
5454
55+ #define LOG_PREFIX "mca: if: linux_ipv6: "
56+
5557static int if_linux_ipv6_open (void );
5658
5759/* Discovers Linux IPv6 interfaces */
@@ -77,7 +79,34 @@ opal_if_base_component_t mca_if_linux_ipv6_component = {
7779 },
7880};
7981
80- /* configure using getifaddrs(3) */
82+ static bool hex2int (char hex , int * dst )
83+ {
84+ if ('0' <= hex && hex <= '9' ) {
85+ * dst = hex - '0' ;
86+ } else if ('A' <= hex && hex <= 'F' ) {
87+ * dst = hex - 'A' + 10 ;
88+ } else if ('a' <= hex && hex <= 'f' ) {
89+ * dst = hex - 'a' + 10 ;
90+ } else {
91+ return false;
92+ }
93+ return true;
94+
95+ }
96+
97+ static bool hexdecode (const char * src , char * dst , size_t dstsize )
98+ {
99+ int hi , lo ;
100+ for (size_t i = 0 ; i < dstsize ; i ++ ) {
101+ if (hex2int (src [i * 2 ], & hi ) && hex2int (src [i * 2 + 1 ], & lo )) {
102+ dst [i ] = 16 * hi + lo ;
103+ } else {
104+ return false;
105+ }
106+ }
107+ return true;
108+ }
109+
81110static int if_linux_ipv6_open (void )
82111{
83112#if OPAL_ENABLE_IPV6
@@ -86,49 +115,42 @@ static int if_linux_ipv6_open(void)
86115 char ifname [IF_NAMESIZE ];
87116 unsigned int idx , pfxlen , scope , dadstat ;
88117 struct in6_addr a6 ;
89- int iter ;
90118 uint32_t flag ;
91- unsigned int addrbyte [16 ];
119+ char addrhex [sizeof a6 .s6_addr * 2 + 1 ];
120+ char addrstr [INET6_ADDRSTRLEN ];
92121
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 ],
122+ while (fscanf (f , "%s %x %x %x %x %s\n" , addrhex ,
98123 & idx , & pfxlen , & scope , & dadstat , ifname ) != EOF ) {
99124 opal_if_t * intf ;
100125
126+ if (!hexdecode (addrhex , a6 .s6_addr , sizeof a6 .s6_addr )) {
127+ opal_output (0 , LOG_PREFIX "skipped interface %s: failed to hexdecode %s\n" ,
128+ ifname , addrhex );
129+ continue ;
130+ };
131+ inet_ntop (AF_INET6 , a6 .s6_addr , addrstr , sizeof addrstr );
132+
101133 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 );
134+ LOG_PREFIX "found interface %s inet6 %s scope %x\n" ,
135+ ifname , addrstr , scope );
107136
108137 /* Only interested in global (0x00) scope */
109138 if (scope != 0x00 ) {
110139 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 );
140+ LOG_PREFIX "skipped interface %s inet6 %s scope %x\n" ,
141+ ifname , addrstr , scope );
116142 continue ;
117143 }
118144
119145 intf = OBJ_NEW (opal_if_t );
120146 if (NULL == intf ) {
121- opal_output (0 , "opal_ifinit: unable to allocate %lu bytes\n" ,
147+ opal_output (0 , LOG_PREFIX " unable to allocate %lu bytes\n" ,
122148 (unsigned long )sizeof (opal_if_t ));
123149 fclose (f );
124150 return OPAL_ERR_OUT_OF_RESOURCE ;
125151 }
126152 intf -> af_family = AF_INET6 ;
127153
128- for (iter = 0 ; iter < 16 ; iter ++ ) {
129- a6 .s6_addr [iter ] = addrbyte [iter ];
130- }
131-
132154 /* now construct the opal_if_t */
133155 opal_string_copy (intf -> if_name , ifname , IF_NAMESIZE );
134156 intf -> if_index = opal_list_get_size (& opal_if_list )+ 1 ;
@@ -147,17 +169,12 @@ static int if_linux_ipv6_open(void)
147169 to list */
148170 opal_list_append (& opal_if_list , & (intf -> super ));
149171 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 ]);
172+ LOG_PREFIX "added interface %s inet6 %s scope %x\n" ,
173+ ifname , addrstr , scope );
155174 } /* of while */
156175 fclose (f );
157176 }
158177#endif /* OPAL_ENABLE_IPV6 */
159178
160179 return OPAL_SUCCESS ;
161180}
162-
163-
0 commit comments