5454 */
5555static void callback_nl_link (struct nl_object * obj , void * arg )
5656{
57- PyEtherInfo * ethi = (PyEtherInfo * ) arg ;
58- struct rtnl_link * link = (struct rtnl_link * ) obj ;
59- char hwaddr [130 ];
60-
61- if ( (ethi == NULL ) || (ethi -> hwaddress != NULL ) ) {
62- return ;
63- }
64-
65- memset (& hwaddr , 0 , 130 );
66- nl_addr2str (rtnl_link_get_addr (link ), hwaddr , sizeof (hwaddr ));
67- if ( ethi -> hwaddress ) {
68- Py_XDECREF (ethi -> hwaddress );
69- }
70- ethi -> hwaddress = PyStr_FromFormat ("%s" , hwaddr );
57+ PyEtherInfo * ethi = (PyEtherInfo * ) arg ;
58+ struct rtnl_link * link = (struct rtnl_link * ) obj ;
59+ char hwaddr [130 ];
60+
61+ if ( (ethi == NULL ) || (ethi -> hwaddress != NULL ) ) {
62+ return ;
63+ }
64+
65+ memset (& hwaddr , 0 , 130 );
66+ nl_addr2str (rtnl_link_get_addr (link ), hwaddr , sizeof (hwaddr ));
67+ if ( ethi -> hwaddress ) {
68+ Py_XDECREF (ethi -> hwaddress );
69+ }
70+ ethi -> hwaddress = PyStr_FromFormat ("%s" , hwaddr );
7171}
7272
7373
@@ -80,31 +80,31 @@ static void callback_nl_link(struct nl_object *obj, void *arg)
8080 */
8181static void callback_nl_address (struct nl_object * obj , void * arg )
8282{
83- PyObject * py_addrlist = (PyObject * ) arg ;
84- struct rtnl_addr * rtaddr = (struct rtnl_addr * ) obj ;
85- PyObject * addr_obj = NULL ;
86- int af_family = -1 ;
87-
88- if ( py_addrlist == NULL ) {
89- return ;
90- }
91-
92- /* Ensure that we're processing only known address types.
93- * Currently only IPv4 and IPv6 is handled
94- */
95- af_family = rtnl_addr_get_family (rtaddr );
96- if ( af_family != AF_INET && af_family != AF_INET6 ) {
97- return ;
98- }
83+ PyObject * py_addrlist = (PyObject * ) arg ;
84+ struct rtnl_addr * rtaddr = (struct rtnl_addr * ) obj ;
85+ PyObject * addr_obj = NULL ;
86+ int af_family = -1 ;
9987
100- /* Prepare a new Python object with the IP address */
101- addr_obj = make_python_address_from_rtnl_addr (rtaddr );
102- if (!addr_obj ) {
103- return ;
104- }
105- /* Append the IP address object to the address list */
106- PyList_Append (py_addrlist , addr_obj );
107- Py_DECREF (addr_obj );
88+ if ( py_addrlist == NULL ) {
89+ return ;
90+ }
91+
92+ /* Ensure that we're processing only known address types.
93+ * Currently only IPv4 and IPv6 is handled
94+ */
95+ af_family = rtnl_addr_get_family (rtaddr );
96+ if ( af_family != AF_INET && af_family != AF_INET6 ) {
97+ return ;
98+ }
99+
100+ /* Prepare a new Python object with the IP address */
101+ addr_obj = make_python_address_from_rtnl_addr (rtaddr );
102+ if (!addr_obj ) {
103+ return ;
104+ }
105+ /* Append the IP address object to the address list */
106+ PyList_Append (py_addrlist , addr_obj );
107+ Py_DECREF (addr_obj );
108108}
109109
110110
@@ -118,38 +118,38 @@ static void callback_nl_address(struct nl_object *obj, void *arg)
118118 */
119119static int _set_device_index (PyEtherInfo * self )
120120{
121- struct nl_cache * link_cache ;
122- struct rtnl_link * link ;
123-
124- /* Find the interface index we're looking up.
125- * As we don't expect it to change, we're reusing a "cached"
126- * interface index if we have that
127- */
128- if ( self -> index < 0 ) {
129- if ( (errno = rtnl_link_alloc_cache (get_nlc (), AF_UNSPEC , & link_cache )) < 0 ) {
121+ struct nl_cache * link_cache ;
122+ struct rtnl_link * link ;
123+
124+ /* Find the interface index we're looking up.
125+ * As we don't expect it to change, we're reusing a "cached"
126+ * interface index if we have that
127+ */
128+ if ( self -> index < 0 ) {
129+ if ( (errno = rtnl_link_alloc_cache (get_nlc (), AF_UNSPEC , & link_cache )) < 0 ) {
130130 PyErr_SetString (PyExc_OSError , nl_geterror (errno ));
131131 return 0 ;
132132 }
133133 link = rtnl_link_get_by_name (link_cache , PyStr_AsString (self -> device ));
134134 if ( !link ) {
135- errno = ENODEV ;
136- PyErr_SetFromErrno (PyExc_IOError );
137- nl_cache_free (link_cache );
135+ errno = ENODEV ;
136+ PyErr_SetFromErrno (PyExc_IOError );
137+ nl_cache_free (link_cache );
138138 return 0 ;
139139 }
140- self -> index = rtnl_link_get_ifindex (link );
141- if ( self -> index <= 0 ) {
142- errno = ENODEV ;
143- PyErr_SetFromErrno (PyExc_IOError );
144- rtnl_link_put (link );
145- nl_cache_free (link_cache );
146- return 0 ;
147- }
148-
149- rtnl_link_put (link );
150- nl_cache_free (link_cache );
151- }
152- return 1 ;
140+ self -> index = rtnl_link_get_ifindex (link );
141+ if ( self -> index <= 0 ) {
142+ errno = ENODEV ;
143+ PyErr_SetFromErrno (PyExc_IOError );
144+ rtnl_link_put (link );
145+ nl_cache_free (link_cache );
146+ return 0 ;
147+ }
148+
149+ rtnl_link_put (link );
150+ nl_cache_free (link_cache );
151+ }
152+ return 1 ;
153153}
154154
155155
@@ -168,43 +168,43 @@ static int _set_device_index(PyEtherInfo *self)
168168 */
169169int get_etherinfo_link (PyEtherInfo * self )
170170{
171- struct nl_cache * link_cache ;
172- struct rtnl_link * link ;
173- int err = 0 ;
174-
175- if ( !self ) {
176- return 0 ;
177- }
178-
179- /* Open a NETLINK connection on-the-fly */
180- if ( !open_netlink (self ) ) {
181- PyErr_Format (PyExc_RuntimeError ,
182- "Could not open a NETLINK connection for %s" ,
183- PyStr_AsString (self -> device ));
184- return 0 ;
185- }
186-
187- if ( _set_device_index (self ) != 1 ) {
188- return 0 ;
189- }
171+ struct nl_cache * link_cache ;
172+ struct rtnl_link * link ;
173+ int err = 0 ;
190174
191- /* Extract MAC/hardware address of the interface */
192- if ( ( err = rtnl_link_alloc_cache ( get_nlc (), AF_UNSPEC , & link_cache )) < 0 ) {
193- PyErr_SetString ( PyExc_OSError , nl_geterror ( err ));
194- return 0 ;
195- }
196- link = rtnl_link_alloc ();
197- if ( ! link ) {
198- errno = ENOMEM ;
199- PyErr_SetFromErrno ( PyExc_OSError );
200- return 0 ;
201- }
202- rtnl_link_set_ifindex ( link , self -> index );
203- nl_cache_foreach_filter ( link_cache , OBJ_CAST ( link ), callback_nl_link , self );
204- rtnl_link_put ( link ) ;
205- nl_cache_free ( link_cache );
175+ if ( ! self ) {
176+ return 0 ;
177+ }
178+
179+ /* Open a NETLINK connection on-the-fly */
180+ if ( ! open_netlink ( self ) ) {
181+ PyErr_Format ( PyExc_RuntimeError ,
182+ "Could not open a NETLINK connection for %s" ,
183+ PyStr_AsString ( self -> device ) );
184+ return 0 ;
185+ }
186+
187+ if ( _set_device_index ( self ) != 1 ) {
188+ return 0 ;
189+ }
206190
207- return 1 ;
191+ /* Extract MAC/hardware address of the interface */
192+ if ( (err = rtnl_link_alloc_cache (get_nlc (), AF_UNSPEC , & link_cache )) < 0 ) {
193+ PyErr_SetString (PyExc_OSError , nl_geterror (err ));
194+ return 0 ;
195+ }
196+ link = rtnl_link_alloc ();
197+ if ( !link ) {
198+ errno = ENOMEM ;
199+ PyErr_SetFromErrno (PyExc_OSError );
200+ return 0 ;
201+ }
202+ rtnl_link_set_ifindex (link , self -> index );
203+ nl_cache_foreach_filter (link_cache , OBJ_CAST (link ), callback_nl_link , self );
204+ rtnl_link_put (link );
205+ nl_cache_free (link_cache );
206+
207+ return 1 ;
208208}
209209
210210
@@ -221,65 +221,65 @@ int get_etherinfo_link(PyEtherInfo *self)
221221 */
222222PyObject * get_etherinfo_address (PyEtherInfo * self , nlQuery query )
223223{
224- struct nl_cache * addr_cache ;
225- struct rtnl_addr * addr ;
226- PyObject * addrlist = NULL ;
227- int err = 0 ;
228-
229- if ( !self ) {
230- return NULL ;
231- }
232-
233- /* Open a NETLINK connection on-the-fly */
234- if ( !open_netlink (self ) ) {
235- PyErr_Format (PyExc_RuntimeError ,
236- "Could not open a NETLINK connection for %s" ,
237- PyStr_AsString (self -> device ));
238- return NULL ;
239- }
224+ struct nl_cache * addr_cache ;
225+ struct rtnl_addr * addr ;
226+ PyObject * addrlist = NULL ;
227+ int err = 0 ;
228+
229+ if ( !self ) {
230+ return NULL ;
231+ }
232+
233+ /* Open a NETLINK connection on-the-fly */
234+ if ( !open_netlink (self ) ) {
235+ PyErr_Format (PyExc_RuntimeError ,
236+ "Could not open a NETLINK connection for %s" ,
237+ PyStr_AsString (self -> device ));
238+ return NULL ;
239+ }
240240
241241 if (!_set_device_index (self )) {
242242 return NULL ;
243243 }
244244
245- /* Query the for requested info via NETLINK */
245+ /* Query the for requested info via NETLINK */
246246 /* Extract IP address information */
247247 if ( (err = rtnl_addr_alloc_cache (get_nlc (), & addr_cache )) < 0 ) {
248- PyErr_SetString (PyExc_OSError , nl_geterror (err ));
249- nl_cache_free (addr_cache );
250- return NULL ;
248+ PyErr_SetString (PyExc_OSError , nl_geterror (err ));
249+ nl_cache_free (addr_cache );
250+ return NULL ;
251251 }
252252
253253 addr = rtnl_addr_alloc ();
254254
255255 if ( !addr ) {
256- errno = ENOMEM ;
257- PyErr_SetFromErrno (PyExc_OSError );
258- return NULL ;
256+ errno = ENOMEM ;
257+ PyErr_SetFromErrno (PyExc_OSError );
258+ return NULL ;
259259 }
260260 rtnl_addr_set_ifindex (addr , self -> index );
261261
262- switch ( query ) {
263- case NLQRY_ADDR4 :
264- rtnl_addr_set_family (addr , AF_INET );
265- break ;
262+ switch ( query ) {
263+ case NLQRY_ADDR4 :
264+ rtnl_addr_set_family (addr , AF_INET );
265+ break ;
266266
267- case NLQRY_ADDR6 :
268- rtnl_addr_set_family (addr , AF_INET6 );
269- break ;
267+ case NLQRY_ADDR6 :
268+ rtnl_addr_set_family (addr , AF_INET6 );
269+ break ;
270270
271- default :
272- return NULL ;
273- }
271+ default :
272+ return NULL ;
273+ }
274274
275- /* Retrieve all address information */
276- addrlist = PyList_New (0 ); /* The list where to put the address object */
277- assert (addrlist );
275+ /* Retrieve all address information */
276+ addrlist = PyList_New (0 ); /* The list where to put the address object */
277+ assert (addrlist );
278278
279- /* Loop through all configured addresses */
280- nl_cache_foreach_filter (addr_cache , OBJ_CAST (addr ), callback_nl_address , addrlist );
281- rtnl_addr_put (addr );
282- nl_cache_free (addr_cache );
279+ /* Loop through all configured addresses */
280+ nl_cache_foreach_filter (addr_cache , OBJ_CAST (addr ), callback_nl_address , addrlist );
281+ rtnl_addr_put (addr );
282+ nl_cache_free (addr_cache );
283283
284- return addrlist ;
284+ return addrlist ;
285285}
0 commit comments