@@ -97,16 +97,14 @@ static void callback_nl_link(struct nl_object *obj, void *arg)
9797 */
9898static void callback_nl_address (struct nl_object * obj , void * arg )
9999{
100- struct etherinfo * ethi = (struct etherinfo * ) arg ;
100+ PyObject * py_addrlist = (PyObject * ) arg ;
101101 struct rtnl_addr * rtaddr = (struct rtnl_addr * ) obj ;
102102 PyObject * addr_obj = NULL ;
103103 int af_family = -1 ;
104104
105- if ( ethi == NULL ) {
105+ if ( py_addrlist == NULL ) {
106106 return ;
107107 }
108- assert (ethi -> ipv4_addresses );
109- assert (ethi -> ipv6_addresses );
110108
111109 /* Ensure that we're processing only known address types.
112110 * Currently only IPv4 and IPv6 is handled
@@ -121,9 +119,8 @@ static void callback_nl_address(struct nl_object *obj, void *arg)
121119 if (!addr_obj ) {
122120 return ;
123121 }
124- /* Append the IP address object to the proper address list */
125- PyList_Append ((af_family == AF_INET6 ? ethi -> ipv6_addresses : ethi -> ipv4_addresses ),
126- addr_obj );
122+ /* Append the IP address object to the address list */
123+ PyList_Append (py_addrlist , addr_obj );
127124 Py_DECREF (addr_obj );
128125}
129126
@@ -152,6 +149,8 @@ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query)
152149 struct rtnl_addr * addr ;
153150 struct rtnl_link * link ;
154151 struct etherinfo * ethinf = NULL ;
152+ PyObject * addrlist = NULL ;
153+
155154 int ret = 0 ;
156155
157156 if ( !data || !data -> ethinfo ) {
@@ -207,7 +206,8 @@ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query)
207206 ret = 1 ;
208207 break ;
209208
210- case NLQRY_ADDR :
209+ case NLQRY_ADDR4 :
210+ case NLQRY_ADDR6 :
211211 /* Extract IP address information */
212212 if ( rtnl_addr_alloc_cache (* data -> nlc , & addr_cache ) < 0 ) {
213213 nl_cache_free (addr_cache );
@@ -216,26 +216,35 @@ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query)
216216 addr = rtnl_addr_alloc ();
217217 rtnl_addr_set_ifindex (addr , ethinf -> index );
218218
219- /* Make sure we don't have any old IPv4 addresses saved */
220- Py_XDECREF (ethinf -> ipv4_addresses );
221- ethinf -> ipv4_addresses = PyList_New (0 );
222- if (!ethinf -> ipv4_addresses ) {
223- rtnl_addr_put (addr );
224- nl_cache_free (addr_cache );
225- return 0 ;
226- }
219+ if ( query == NLQRY_ADDR4 ) {
220+ rtnl_addr_set_family (addr , AF_INET );
227221
228- /* Likewise for IPv6 addresses: */
229- Py_XDECREF (ethinf -> ipv6_addresses );
230- ethinf -> ipv6_addresses = PyList_New (0 );
231- if (!ethinf -> ipv6_addresses ) {
232- rtnl_addr_put (addr );
233- nl_cache_free (addr_cache );
234- return 0 ;
235- }
222+ /* Make sure we don't have any old IPv4 addresses saved */
223+ Py_XDECREF (ethinf -> ipv4_addresses );
224+ ethinf -> ipv4_addresses = PyList_New (0 );
225+ if (!ethinf -> ipv4_addresses ) {
226+ rtnl_addr_put (addr );
227+ nl_cache_free (addr_cache );
228+ return 0 ;
229+ }
230+ assert (ethinf -> ipv4_addresses );
231+ addrlist = ethinf -> ipv4_addresses ;
232+ } else if ( query == NLQRY_ADDR6 ) {
233+ rtnl_addr_set_family (addr , AF_INET6 );
236234
237- /* Retrieve all address information */
238- nl_cache_foreach_filter (addr_cache , OBJ_CAST (addr ), callback_nl_address , ethinf );
235+ /* Likewise for IPv6 addresses: */
236+ Py_XDECREF (ethinf -> ipv6_addresses );
237+ ethinf -> ipv6_addresses = PyList_New (0 );
238+ if (!ethinf -> ipv6_addresses ) {
239+ rtnl_addr_put (addr );
240+ nl_cache_free (addr_cache );
241+ return 0 ;
242+ }
243+ assert (ethinf -> ipv6_addresses );
244+ addrlist = ethinf -> ipv6_addresses ;
245+ }
246+ /* Retrieve all address information - common code for NLQRY_ADDR4 and NLQRY_ADDR6*/
247+ nl_cache_foreach_filter (addr_cache , OBJ_CAST (addr ), callback_nl_address , addrlist );
239248 rtnl_addr_put (addr );
240249 nl_cache_free (addr_cache );
241250 ret = 1 ;
0 commit comments