Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCP support #226

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

TCP support #226

wants to merge 2 commits into from

Conversation

edgsousa
Copy link

Added TCP support based on asyncore. Tested on Windows on python versions 2.7 and 3.5 connecting to a SNMPv3 target (with authPriv) and Travis tests passing.

This can be a good point to start and solve #56.

Copy link
Owner

@etingof etingof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on TCP support! That's a great and long-wanted feature!

I've got a few comments/suggestions inline. Most importantly, I think the way how we locate message header in the incoming stream could be improved.

from ..dgram.base import DgramSocketTransport

# Ignore these socket errors
sockErrors = {errno.ESHUTDOWN: True,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are reusing this map across tcp/udp transports, would it make sense to move it to some common place e.g. asyncore.base module or a common base class?

sockErrors[errno.EBADFD] = True


class StreamSocketTransport(DgramSocketTransport):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole class seems to be largely a copy of DgramSocketTransport. Could we make this common code shared between stream and dgram classes? May be by pushing it to AbstractSocketTransport?

if header_data:
#1st byte is 0x30 - sequence
seq_len = header_data[1]
if seq_len & 0x80 != 0: # BER long definite length
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finding start of message in the stream is hard indeed...

It would be cleaner if we keep transport code independent from the data being transferred. That would involve a bit of refactoring in the protocol engine, though.

Here is where the read message goes from transport/networking code into the protocol engine. I am thinking if we make receiveMessage returning unused bytes (as returned by the underlying pyasn1 decoder) and "unread" them back into some internal queue like __inQueue, but (in case of stream transport) it should probably just a (growing/shrinking) string rather than a list...? Just like you have it in remaining_bytes.

That would let us remove this header finding heuristics completely. And, most importantly, make searching way more reliable.

One of the interesting problems along this way would be to detect and eventually reset the growing inQueue if decoder repeatedly fails to decode it. May be this could be based on a reasonable SNMP message size, like if the buffer grows beyond 4K - drop it.

Does it make sense?

@@ -19,6 +20,7 @@
# Transports
snmpUDPDomain = udp.snmpUDPDomain
snmpUDP6Domain = udp6.snmpUDP6Domain
snmpTCPDomain = tcp.snmpTCPDomain
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that supporting TCP-over-IP6 would be comprise a great follow-up patch! ;-)

Datastore (LCD) managed by :py:class:`~pysnmp.hlapi.SnmpEngine`
class instance.

See :RFC:`1906#section-3` for more information on the UDP transport mapping.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be updated?


Examples
--------
>>> from pysnmp.hlapi.v3arch.asyncore import UdpTransportTarget
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be updated?

@@ -36,7 +36,7 @@

def inet_pton(address_family, ip_string):
if address_family == socket.AF_INET:
return inet_aton(ip_string)
return socket.inet_aton(ip_string)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, seems like an unrelated bug! Let me fix this in master...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick note that this problem has been fixed in master.

@moshesakajo
Copy link

What is the status of adding the TCP support ? Is there an Alpha release for this feature ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants