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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
187 changes: 187 additions & 0 deletions pysnmp/carrier/asyncore/stream/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2018, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pysnmp/license.html
#
# Author: Edgar Sousa <edg@edgsousa.xyz>
#
import errno
import socket
import struct
import sys

from pysnmp import debug
from pysnmp.carrier import sockmsg, error
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?

errno.ENOTCONN: True,
errno.ECONNRESET: False,
errno.ECONNREFUSED: False,
errno.EAGAIN: False,
errno.EWOULDBLOCK: False,
errno.WSAEWOULDBLOCK: False,}

if hasattr(errno, 'EBADFD'):
# bad FD may happen upon FD closure on n-1 select() event
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?

sockType = socket.SOCK_STREAM

def __init__(self, sock=None, sockMap=None):
DgramSocketTransport.__init__(self, sock, sockMap)
self.__outQueue = []
self._sendto = lambda s, b, a: s.send(b)

def __recvfrom(s, sz):
header_data = bytearray(s.recv(2))
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?

seq_len &= 0x7f
payload_len = bytearray(s.recv(seq_len))
header_data += payload_len
remaining_bytes, = struct.unpack("!I", bytearray([0]*(4-seq_len))+payload_len)
else:
remaining_bytes = seq_len

payload_data = bytearray(s.recv(remaining_bytes))

return bytes(header_data+payload_data), self.addressType(s.getpeername())

self._recvfrom = __recvfrom


def openClientMode(self, iface=None, targetAddress=None):
# do nothing
if targetAddress:
self.socket.setblocking(1)
self.socket.connect(targetAddress)
self.socket.setblocking(0)
return self

def openServerMode(self, iface):
# doesn't make sense
return self

def enableBroadcast(self, flag=1):
try:
self.socket.setsockopt(
socket.SOL_SOCKET, socket.SO_BROADCAST, flag
)
except socket.error:
raise error.CarrierError('setsockopt() for SO_BROADCAST failed: %s' % (sys.exc_info()[1],))
debug.logger & debug.flagIO and debug.logger('enableBroadcast: %s option SO_BROADCAST on socket %s' % (flag and "enabled" or "disabled", self.socket.fileno()))
return self

def enablePktInfo(self, flag=1):
if (not hasattr(self.socket, 'sendmsg') or
not hasattr(self.socket, 'recvmsg')):
raise error.CarrierError('sendmsg()/recvmsg() interface is not supported by this OS and/or Python version')

try:
if self.socket.family in (socket.AF_INET, socket.AF_INET6):
self.socket.setsockopt(socket.SOL_IP, socket.IP_PKTINFO, flag)
if self.socket.family == socket.AF_INET6:
self.socket.setsockopt(socket.SOL_IPV6, socket.IPV6_RECVPKTINFO, flag)
except socket.error:
raise error.CarrierError('setsockopt() for %s failed: %s' % (self.socket.family == socket.AF_INET6 and "IPV6_RECVPKTINFO" or "IP_PKTINFO", sys.exc_info()[1]))

self._sendto = sockmsg.getSendTo(self.addressType)
self._recvfrom = sockmsg.getRecvFrom(self.addressType)

debug.logger & debug.flagIO and debug.logger('enablePktInfo: %s option %s on socket %s' % (self.socket.family == socket.AF_INET6 and "IPV6_RECVPKTINFO" or "IP_PKTINFO", flag and "enabled" or "disabled", self.socket.fileno()))
return self

def enableTransparent(self, flag=1):
try:
if self.socket.family == socket.AF_INET:
self.socket.setsockopt(
socket.SOL_IP, socket.IP_TRANSPARENT, flag
)
if self.socket.family == socket.AF_INET6:
self.socket.setsockopt(
socket.SOL_IPV6, socket.IP_TRANSPARENT, flag
)
except socket.error:
raise error.CarrierError('setsockopt() for IP_TRANSPARENT failed: %s' % sys.exc_info()[1])
except OSError:
raise error.CarrierError('IP_TRANSPARENT socket option requires superuser priveleges')

debug.logger & debug.flagIO and debug.logger('enableTransparent: %s option IP_TRANSPARENT on socket %s' % (flag and "enabled" or "disabled", self.socket.fileno()))
return self

def sendMessage(self, outgoingMessage, transportAddress):
self.__outQueue.append(
(outgoingMessage, self.normalizeAddress(transportAddress))
)
debug.logger & debug.flagIO and debug.logger('sendMessage: outgoingMessage queued (%d octets) %s' % (len(outgoingMessage), debug.hexdump(outgoingMessage)))

def normalizeAddress(self, transportAddress):
if not isinstance(transportAddress, self.addressType):
transportAddress = self.addressType(transportAddress)
if not transportAddress.getLocalAddress():
transportAddress.setLocalAddress(self.getLocalAddress())
return transportAddress

def getLocalAddress(self):
# one evil OS does not seem to support getsockname() for DGRAM sockets
try:
return self.socket.getsockname()
except Exception:
return '0.0.0.0', 0

# asyncore API
def handle_connect(self):
pass

def writable(self):
return self.__outQueue

def handle_write(self):
outgoingMessage, transportAddress = self.__outQueue.pop(0)
debug.logger & debug.flagIO and debug.logger('handle_write: transportAddress %r -> %r outgoingMessage (%d octets) %s' % (transportAddress.getLocalAddress(), transportAddress, len(outgoingMessage), debug.hexdump(outgoingMessage)))
if not transportAddress:
debug.logger & debug.flagIO and debug.logger('handle_write: missing dst address, loosing outgoing msg')
return
try:
self._sendto(
self.socket, outgoingMessage, transportAddress
)
except socket.error:
if sys.exc_info()[1].args[0] in sockErrors:
debug.logger & debug.flagIO and debug.logger('handle_write: ignoring socket error %s' % (sys.exc_info()[1],))
else:
raise error.CarrierError('sendto() failed for %s: %s' % (transportAddress, sys.exc_info()[1]))

def readable(self):
return 1

def handle_read(self):
try:
incomingMessage, transportAddress = self._recvfrom(self.socket, 65535)
transportAddress = self.normalizeAddress(transportAddress)
debug.logger & debug.flagIO and debug.logger(
'handle_read: transportAddress %r -> %r incomingMessage (%d octets) %s' % (transportAddress, transportAddress.getLocalAddress(), len(incomingMessage), debug.hexdump(incomingMessage)))
if not incomingMessage:
self.handle_close()
return
else:
self._cbFun(self, transportAddress, incomingMessage)
return
except socket.error:
if sys.exc_info()[1].args[0] in sockErrors:
debug.logger & debug.flagIO and debug.logger('handle_read: known socket error %s' % (sys.exc_info()[1],))
sockErrors[sys.exc_info()[1].args[0]] and self.handle_close()
return
else:
raise error.CarrierError('recvfrom() failed: %s' % (sys.exc_info()[1],))

def handle_close(self):
self.socket.shutdown()
self.socket.close()
23 changes: 23 additions & 0 deletions pysnmp/carrier/asyncore/stream/tcp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2018, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pysnmp/license.html
#
# Author: Edgar Sousa <edg@edgsousa.xyz>
#
from socket import AF_INET

from pysnmp.carrier.asyncore.stream.base import StreamSocketTransport
from pysnmp.carrier.base import AbstractTransportAddress

domainName = snmpTCPDomain = (1, 3, 6, 1, 2, 1, 100, 1, 5)


class TcpTransportAddress(tuple, AbstractTransportAddress):
pass


class TcpSocketTransport(StreamSocketTransport):
sockFamily = AF_INET
addressType = TcpTransportAddress
8 changes: 8 additions & 0 deletions pysnmp/entity/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
from pyasn1.compat.octets import null
from pysnmp.carrier.asyncore.dgram import udp, udp6
from pysnmp.carrier.asyncore.stream import tcp
from pysnmp.proto.secmod.rfc3414.auth import hmacmd5, hmacsha, noauth
from pysnmp.proto.secmod.rfc3414.priv import des, nopriv
from pysnmp.proto.secmod.rfc3826.priv import aes
Expand All @@ -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! ;-)


# Auth protocol
usmHMACMD5AuthProtocol = hmacmd5.HmacMd5.serviceID
Expand Down Expand Up @@ -312,6 +314,12 @@ def addTargetAddr(snmpEngine, addrName, transportDomain, transportAddress,
if sourceAddress is None:
sourceAddress = ('::', 0)
sourceAddress = TransportAddressIPv6(sourceAddress)
elif transportDomain[:len(snmpTCPDomain)] == snmpTCPDomain:
TransportAddressIPv4, = mibBuilder.importSymbols('TRANSPORT-ADDRESS-MIB', 'TransportAddressIPv4')
transportAddress = TransportAddressIPv4(transportAddress)
if sourceAddress is None:
sourceAddress = ('0.0.0.0', 0)
sourceAddress = TransportAddressIPv4(sourceAddress)

snmpEngine.msgAndPduDsp.mibInstrumController.writeVars(
(snmpTargetAddrEntry.name + (9,) + tblIdx, 'destroy'),
Expand Down
7 changes: 7 additions & 0 deletions pysnmp/entity/rfc3413/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,18 @@ def getTargetAddr(snmpEngine, snmpTargetAddrName):
snmpTargetAddrTAddress = transport.addressType(
TransportAddressIPv6(snmpTargetAddrTAddress)
).setLocalAddress(TransportAddressIPv6(snmpSourceAddrTAddress))
elif snmpTargetAddrTDomain[:len(config.snmpTCPDomain)] == config.snmpTCPDomain:
TransportAddressIPv4, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols(
'TRANSPORT-ADDRESS-MIB', 'TransportAddressIPv4')
snmpTargetAddrTAddress = transport.addressType(
TransportAddressIPv4(snmpTargetAddrTAddress)
).setLocalAddress(TransportAddressIPv4(snmpSourceAddrTAddress))
elif snmpTargetAddrTDomain[:len(config.snmpLocalDomain)] == config.snmpLocalDomain:
snmpTargetAddrTAddress = transport.addressType(
snmpTargetAddrTAddress
)


nameToTargetMap[snmpTargetAddrName] = (
snmpTargetAddrTDomain,
snmpTargetAddrTAddress,
Expand Down
56 changes: 55 additions & 1 deletion pysnmp/hlapi/v3arch/asyncore/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,63 @@

from pysnmp import error
from pysnmp.carrier.asyncore.dgram import udp, udp6
from pysnmp.carrier.asyncore.stream import tcp
from pysnmp.hlapi.transport import AbstractTransportTarget

__all__ = ['Udp6TransportTarget', 'UdpTransportTarget']
__all__ = ['Udp6TransportTarget', 'UdpTransportTarget', 'TcpTransportTarget']

class TcpTransportTarget(AbstractTransportTarget):
"""Creates TCP/IPv4 configuration and initialize socket API.
This object can be used for adding new entries to Local Configuration
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?


Parameters
----------
transportAddr: :py:class:`tuple`
Indicates remote address in Python :py:mod:`socket` module format
which is a tuple of FQDN, port where FQDN is a string representing
either hostname or IPv4 address in quad-dotted form, port is an
integer.
timeout: :py:class:`int`
Response timeout in seconds.
retries: :py:class:`int`
Maximum number of request retries, 0 retries means just a single
request.
tagList: :py:class:`str`
Arbitrary string that contains a list of space-separated tag
strings used to select target addresses and/or SNMP configuration
(see :RFC:`3413#section-4.1.1`, :RFC:`2576#section-5.3` and
:py:class:`~pysnmp.hlapi.CommunityData` object).

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?

>>> TcpTransportTarget(('demo.snmplabs.com', 161))
TcpTransportTarget(('195.218.195.228', 161), timeout=1, retries=5, tagList='')
>>>

"""
transportDomain = tcp.domainName
protoTransport = tcp.TcpSocketTransport

def openClientMode(self):
self.transport = self.protoTransport().openClientMode(self.iface, self.transportAddr)
return self.transport

def _resolveAddr(self, transportAddr):
try:
return socket.getaddrinfo(transportAddr[0],
transportAddr[1],
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP)[0][4][:2]
except socket.gaierror:
raise error.PySnmpError('Bad IPv4/TCP transport address %s: %s' % (
'@'.join([str(x) for x in transportAddr]), sys.exc_info()[1]))



class UdpTransportTarget(AbstractTransportTarget):
Expand Down
4 changes: 2 additions & 2 deletions pysnmp/smi/mibs/TRANSPORT-ADDRESS-MIB.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

elif address_family != socket.AF_INET6:
raise socket.error(
'Unknown address family %s' % (address_family,)
Expand Down Expand Up @@ -84,7 +84,7 @@ def inet_pton(address_family, ip_string):

def inet_ntop(address_family, packed_ip):
if address_family == socket.AF_INET:
return inet_ntop(packed_ip)
return socket.inet_ntoa(packed_ip)
elif address_family != socket.AF_INET6:
raise socket.error(
'Unknown address family %s' % (address_family,)
Expand Down