Giter Club home page Giter Club logo

Comments (15)

jtluka avatar jtluka commented on August 28, 2024

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.

svinota avatar svinota commented on August 28, 2024

Thanks, got it. Fixing.

from pyroute2.

jtluka avatar jtluka commented on August 28, 2024

Peet, it seems that adding bind() before sendto() actually fixed the problem.

from pyroute2.

svinota avatar svinota commented on August 28, 2024

Yep. Just wondering, how it worked before w/o bind? o_O

from pyroute2.

svinota avatar svinota commented on August 28, 2024

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.

jtluka avatar jtluka commented on August 28, 2024

I'm ok with the latter, it's one-line fix in the LNST code for me.

from pyroute2.

svinota avatar svinota commented on August 28, 2024

Ok, thanks.

from pyroute2.

olichtne avatar olichtne commented on August 28, 2024

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.

svinota avatar svinota commented on August 28, 2024

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.

svinota avatar svinota commented on August 28, 2024

Yep, so looks better and still passes test cycle. Thanks!

from pyroute2.

olichtne avatar olichtne commented on August 28, 2024

Thanks for the quick fix!

from pyroute2.

svinota avatar svinota commented on August 28, 2024

You're always welcome :)

from pyroute2.

olichtne avatar olichtne commented on August 28, 2024

btw, when can I expect a new version that will contain this fix?

from pyroute2.

svinota avatar svinota commented on August 28, 2024

By the end of the week, I suppose. I need to finish some test code.

from pyroute2.

olichtne avatar olichtne commented on August 28, 2024

great, thanks again!

from pyroute2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.