@@ -221,7 +221,7 @@ int get_etherinfo_link(etherinfo_py *self)
221221 *
222222 * @return Returns 1 on success, otherwise 0.
223223 */
224- int get_etherinfo (etherinfo_py * self , nlQuery query )
224+ int get_etherinfo_address (etherinfo_py * self , nlQuery query )
225225{
226226 struct nl_cache * addr_cache ;
227227 struct rtnl_addr * addr ;
@@ -246,54 +246,60 @@ int get_etherinfo(etherinfo_py *self, nlQuery query)
246246 return 0 ;
247247 }
248248
249- /* Query the for requested info vai NETLINK */
249+ /* Query the for requested info via NETLINK */
250+
251+ /* Extract IP address information */
252+ if ( rtnl_addr_alloc_cache (get_nlc (), & addr_cache ) < 0 ) {
253+ nl_cache_free (addr_cache );
254+ return 0 ;
255+ }
256+ addr = rtnl_addr_alloc ();
257+ /* FIXME: Error handling? */
258+ rtnl_addr_set_ifindex (addr , ethinf -> index );
259+
250260 switch ( query ) {
251261 case NLQRY_ADDR4 :
252- case NLQRY_ADDR6 :
253- /* Extract IP address information */
254- if ( rtnl_addr_alloc_cache (get_nlc (), & addr_cache ) < 0 ) {
255- nl_cache_free (addr_cache );
262+ rtnl_addr_set_family (addr , AF_INET );
263+
264+ /* Make sure we don't have any old IPv4 addresses saved */
265+ Py_XDECREF (ethinf -> ipv4_addresses );
266+ ethinf -> ipv4_addresses = PyList_New (0 );
267+ if (!ethinf -> ipv4_addresses ) {
268+ rtnl_addr_put (addr );
269+ nl_cache_free (addr_cache );
256270 return 0 ;
257271 }
258- addr = rtnl_addr_alloc ();
259- rtnl_addr_set_ifindex (addr , ethinf -> index );
260-
261- if ( query == NLQRY_ADDR4 ) {
262- rtnl_addr_set_family (addr , AF_INET );
263-
264- /* Make sure we don't have any old IPv4 addresses saved */
265- Py_XDECREF (ethinf -> ipv4_addresses );
266- ethinf -> ipv4_addresses = PyList_New (0 );
267- if (!ethinf -> ipv4_addresses ) {
268- rtnl_addr_put (addr );
269- nl_cache_free (addr_cache );
270- return 0 ;
271- }
272- assert (ethinf -> ipv4_addresses );
273- addrlist = ethinf -> ipv4_addresses ;
274- } else if ( query == NLQRY_ADDR6 ) {
275- rtnl_addr_set_family (addr , AF_INET6 );
276-
277- /* Likewise for IPv6 addresses: */
278- Py_XDECREF (ethinf -> ipv6_addresses );
279- ethinf -> ipv6_addresses = PyList_New (0 );
280- if (!ethinf -> ipv6_addresses ) {
281- rtnl_addr_put (addr );
282- nl_cache_free (addr_cache );
283- return 0 ;
284- }
285- assert (ethinf -> ipv6_addresses );
286- addrlist = ethinf -> ipv6_addresses ;
272+ assert (ethinf -> ipv4_addresses );
273+ addrlist = ethinf -> ipv4_addresses ;
274+ ret = 1 ;
275+ break ;
276+
277+ case NLQRY_ADDR6 :
278+ rtnl_addr_set_family (addr , AF_INET6 );
279+
280+ /* Likewise for IPv6 addresses: */
281+ Py_XDECREF (ethinf -> ipv6_addresses );
282+ ethinf -> ipv6_addresses = PyList_New (0 );
283+ if (!ethinf -> ipv6_addresses ) {
284+ rtnl_addr_put (addr );
285+ nl_cache_free (addr_cache );
286+ return 0 ;
287287 }
288- /* Retrieve all address information - common code for NLQRY_ADDR4 and NLQRY_ADDR6*/
289- nl_cache_foreach_filter (addr_cache , OBJ_CAST (addr ), callback_nl_address , addrlist );
290- rtnl_addr_put (addr );
291- nl_cache_free (addr_cache );
292- ret = 1 ;
293- break ;
288+ assert (ethinf -> ipv6_addresses );
289+ addrlist = ethinf -> ipv6_addresses ;
290+ ret = 1 ;
291+ break ;
294292
295293 default :
296294 ret = 0 ;
297295 }
296+
297+ if ( ret == 1 ) {
298+ /* Retrieve all address information - common code for NLQRY_ADDR4 and NLQRY_ADDR6*/
299+ nl_cache_foreach_filter (addr_cache , OBJ_CAST (addr ), callback_nl_address , addrlist );
300+ rtnl_addr_put (addr );
301+ nl_cache_free (addr_cache );
302+ }
303+
298304 return ret ;
299305}
0 commit comments