1
1
/*
2
- * Copyright (c) 2010 Cisco Systems, Inc. All rights reserved.
2
+ * Copyright (c) 2010-2019 Cisco Systems, Inc. All rights reserved.
3
3
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
4
4
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights
5
5
* reserved.
48
48
#include "opal/constants.h"
49
49
#include "opal/util/if.h"
50
50
#include "opal/util/output.h"
51
+ #include "opal/util/show_help.h"
51
52
#include "opal/util/string_copy.h"
52
53
#include "opal/mca/if/if.h"
53
54
#include "opal/mca/if/base/base.h"
54
55
56
+ #define LOG_PREFIX "mca: if: linux_ipv6: "
57
+
55
58
static int if_linux_ipv6_open (void );
56
59
57
60
/* Discovers Linux IPv6 interfaces */
@@ -77,7 +80,36 @@ opal_if_base_component_t mca_if_linux_ipv6_component = {
77
80
},
78
81
};
79
82
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
+
81
113
static int if_linux_ipv6_open (void )
82
114
{
83
115
#if OPAL_ENABLE_IPV6
@@ -86,49 +118,45 @@ static int if_linux_ipv6_open(void)
86
118
char ifname [IF_NAMESIZE ];
87
119
unsigned int idx , pfxlen , scope , dadstat ;
88
120
struct in6_addr a6 ;
89
- int iter ;
90
121
uint32_t flag ;
91
- unsigned int addrbyte [16 ];
122
+ char addrhex [sizeof a6 .s6_addr * 2 + 1 ];
123
+ char addrstr [INET6_ADDRSTRLEN ];
92
124
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 ,
98
126
& idx , & pfxlen , & scope , & dadstat , ifname ) != EOF ) {
99
127
opal_if_t * intf ;
100
128
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
+
101
139
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 );
107
142
108
143
/* Only interested in global (0x00) scope */
109
144
if (scope != 0x00 ) {
110
145
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 );
116
148
continue ;
117
149
}
118
150
119
151
intf = OBJ_NEW (opal_if_t );
120
152
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" ,
122
154
(unsigned long )sizeof (opal_if_t ));
123
155
fclose (f );
124
156
return OPAL_ERR_OUT_OF_RESOURCE ;
125
157
}
126
158
intf -> af_family = AF_INET6 ;
127
159
128
- for (iter = 0 ; iter < 16 ; iter ++ ) {
129
- a6 .s6_addr [iter ] = addrbyte [iter ];
130
- }
131
-
132
160
/* now construct the opal_if_t */
133
161
opal_string_copy (intf -> if_name , ifname , IF_NAMESIZE );
134
162
intf -> if_index = opal_list_get_size (& opal_if_list )+ 1 ;
@@ -147,17 +175,12 @@ static int if_linux_ipv6_open(void)
147
175
to list */
148
176
opal_list_append (& opal_if_list , & (intf -> super ));
149
177
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 );
155
180
} /* of while */
156
181
fclose (f );
157
182
}
158
183
#endif /* OPAL_ENABLE_IPV6 */
159
184
160
185
return OPAL_SUCCESS ;
161
186
}
162
-
163
-
0 commit comments