Comments (15)
I thought that missing call to bind() is the reason but I see the same problem with the following code, too:
import os
import re
import socket
import subprocess
from pyroute2 import IPRSocket
from pyroute2.netlink import NLM_F_REQUEST
from pyroute2.netlink import NLM_F_DUMP
from pyroute2.netlink import NLMSG_DONE
from pyroute2.netlink import NLMSG_ERROR
from pyroute2.netlink.iproute import RTM_GETLINK
from pyroute2.netlink.iproute import RTM_NEWLINK
from pyroute2.netlink.rtnl.ifinfmsg import ifinfmsg
def normalize_hwaddr(hwaddr):
return hwaddr.upper().rstrip("\n")
scan = []
nl_socket = IPRSocket()
msg = ifinfmsg()
msg["family"] = socket.AF_UNSPEC
msg["header"]["type"] = RTM_GETLINK
msg["header"]["flags"] = NLM_F_REQUEST | NLM_F_DUMP
msg["header"]["pid"] = os.getpid()
msg["header"]["sequence_number"] = 1
msg.encode()
nl_socket.sendto(msg.buf.getvalue(), (0,0))
finished = False
while not finished:
parts = nl_socket.get()
for part in parts:
if part["header"]["type"] in [NLMSG_DONE, NLMSG_ERROR]:
finished = True
continue
if part["header"]["sequence_number"] != 1:
continue
if part["header"]["type"] == RTM_NEWLINK:
new_link = {}
new_link["netlink_msg"] = part
new_link["index"] = part["index"]
new_link["name"] = part.get_attr("IFLA_IFNAME")
print new_link["name"]
hwaddr = part.get_attr("IFLA_ADDRESS")
new_link["hwaddr"] = normalize_hwaddr(hwaddr)
scan.append(new_link)
nl_socket.close()
from pyroute2.
Thanks, got it. Fixing.
from pyroute2.
Peet, it seems that adding bind() before sendto() actually fixed the problem.
from pyroute2.
Yep. Just wondering, how it worked before w/o bind? o_O
from pyroute2.
Another question, should we keep the old behaviour (w/o bind? how it come?), or just move to the new with bind? I would prefer the latter, but what you'll say?
from pyroute2.
I'm ok with the latter, it's one-line fix in the LNST code for me.
from pyroute2.
Ok, thanks.
from pyroute2.
Hi, I've just come across this problem. What is the reason behind the assert? The example that @jtluka posted uses the socket like a client would (to send requests to the kernel) - no need to bind it to anything. But such sockets should still be close-able.
from pyroute2.
The reason was in data integrity, when you have several sockets, and wanna properly release resources. But you're right, it effectively make impossible to close the socket w/o binding it before.
Since w/o binding the socket doesn't allocate any resource that should be tracked, it would be better to make more smart close()
routine. Wait a minute.
from pyroute2.
Yep, so looks better and still passes test cycle. Thanks!
from pyroute2.
Thanks for the quick fix!
from pyroute2.
You're always welcome :)
from pyroute2.
btw, when can I expect a new version that will contain this fix?
from pyroute2.
By the end of the week, I suppose. I need to finish some test code.
from pyroute2.
great, thanks again!
from pyroute2.
Related Issues (20)
- How get named counter in pyroute2
- Unconditionally importing readline causes curses resize handler to break HOT 6
- source package for 0.7.11 missing on pypi HOT 2
- IPRoute decoding exceptions on linux kernel 6.6 (IFLA_INET6_ADDR_GEN_MODE) HOT 1
- NDP Route Change not properly recognized
- No longer able to rename interface after update to any version later than 0.7.6: "could not setup alt ifnames" HOT 12
- Unable to move interface to network NS HOT 5
- Unable to set IP6_TNL_F_IGN_ENCAP_LIMIT on ip6tnl HOT 1
- NDB Kernel not sending RTM_DELROUTE HOT 6
- Extended capabilities in iwutil scan HOT 1
- linux network namespace. commit after del_ip, will print “fail <class 'NoneType'>”
- `IPRoute2.tc` 'flow' filter buggy when in map mode
- Parse conntrack updates to structure NFCTAttrTuple or dicrinary
- Get conntrack TCP OFFLOAD entries looks broken
- lost src and src_len in ndb.routes.create
- IPRoute.flush_addr(altname='some-alt-name') does not work
- Support for `ip route save` and `ip route restore` commands
- rule add dont work, fails on socket issue, ubuntu 18. tests fails as well HOT 2
- pyroute2.netlink.exceptions.NetlinkDumpInterrupted: (-1, 'dump interrupted')
- shed_cake.py bug, unable to add a cake qdisc under a parent
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyroute2.