classification
Title: Implement socket.if_{nametoindex,indextoname} for Windows
Type: enhancement Stage: resolved
Components: Windows Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ZackerySpytz, dtantsur, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: patch

Created on 2019-05-22 11:01 by dtantsur, last changed 2019-05-29 21:58 by steve.dower. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13522 merged ZackerySpytz, 2019-05-23 10:24
Messages (8)
msg343171 - (view) Author: Dmitry Tantsur (dtantsur) Date: 2019-05-22 11:01
These two calls are currently Unix-only in the socket module. However, Windows supports them starting with Vista:
https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-if_indextoname
https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-if_nametoindex

I checked with ctypes and they do seem to work. Unfortunately, if_nameindex is not implemented.
msg343285 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2019-05-23 10:26
I've created a PR for this issue. Few changes are needed to implement these functions on Windows.
msg343303 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-05-23 15:38
Is it worth also implementing if_nameindex() using (I assume) GetIfTable2Ex [1]? Or maybe just the simpler GetIfTable is sufficient - I'm not sure exactly what semantics Unix if_nameindex() has, whether it includes all logical adapters.

[1]: https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getiftable2ex
msg343347 - (view) Author: Dmitry Tantsur (dtantsur) Date: 2019-05-24 07:48
That would be fantastic, but I did not dare asking for it :)

This is how the output of if_nameindex looks on my Fedora 29:

[(1, 'lo'), (2, 'enp0s31f6'), (3, 'wlp4s0'), (4, 'virbr0'), (5, 'virbr0-nic'), (12, 'tun0')]

It includes the loopback, two real adapters (ethernet and wifi) and devices apparently created by libvirt.

This is how it looks on my OpenStack testing environment:

[(1, 'lo'), (2, 'eth0'), (3, 'virbr0'), (4, 'virbr0-nic'), (5, 'ovs-system'), (6, 'br-int'), (7, 'br-ex'), (8, 'brbm'), (9, 'br-tun'), (14, 'ovs-node-0i1'), (15, 'tap-node-0i1'), (16, 'ovs-node-0i2'), (17, 'tap-node-0i2'), (18, 'ovs-node-1i1'), (19, 'tap-node-1i1'), (20, 'ovs-node-1i2'), (21, 'tap-node-1i2'), (22, 'ovs-node-2i1'), (23, 'tap-node-2i1'), (24, 'ovs-node-2i2'), (25, 'tap-node-2i2'), (26, 'ovs-tap'), (27, 'brbm-tap'), (34, 'br-inspector'), (35, 'brbm-inspector'), (55, 'macvtap2'), (56, 'macvtap3'), (79, 'macvtap0'), (80, 'macvtap1')]

It seems to actually include everything.
msg343892 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2019-05-29 15:38
Well, it turns out that implementing if_nameindex() on Windows using GetIfTable2Ex() was also quite simple. I've updated the PR.
msg343900 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-05-29 17:07
Great, thanks Zackery! (And thanks for the ping - I don't notice GitHub notifications.)

Had a few comments about error handling, but it looks great.
msg343913 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2019-05-29 19:30
Thanks, Steve. I've addressed your comments on the PR.
msg343925 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-05-29 21:02
New changeset 8f96c9f8ed2a4795e34b333411451e24f28f74d2 by Steve Dower (Zackery Spytz) in branch 'master':
bpo-37007: Implement socket.if_nametoindex(), if_indextoname() and if_nameindex() on Windows (GH-13522)
https://github.com/python/cpython/commit/8f96c9f8ed2a4795e34b333411451e24f28f74d2
History
Date User Action Args
2019-05-29 21:58:45steve.dowersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-29 21:02:44steve.dowersetmessages: + msg343925
2019-05-29 19:30:14ZackerySpytzsetmessages: + msg343913
2019-05-29 17:07:49steve.dowersetmessages: + msg343900
2019-05-29 16:50:10eryksunsetnosy: - eryksun
2019-05-29 15:38:57ZackerySpytzsetmessages: + msg343892
2019-05-24 07:48:17dtantsursetmessages: + msg343347
2019-05-23 15:38:54steve.dowersetmessages: + msg343303
2019-05-23 10:26:55ZackerySpytzsetnosy: + ZackerySpytz, eryksun
messages: + msg343285
2019-05-23 10:24:17ZackerySpytzsetkeywords: + patch
stage: patch review
pull_requests: + pull_request13437
2019-05-23 07:48:48matrixisesetnosy: + paul.moore, tim.golden, zach.ware, steve.dower
components: + Windows, - Library (Lib)
2019-05-22 11:01:24dtantsurcreate