-
-
Notifications
You must be signed in to change notification settings - Fork 201
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
base: master
Are you sure you want to change the base?
TCP support #226
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole class seems to be largely a copy of |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 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() |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -19,6 +20,7 @@ | |
# Transports | ||
snmpUDPDomain = udp.snmpUDPDomain | ||
snmpUDP6Domain = udp6.snmpUDP6Domain | ||
snmpTCPDomain = tcp.snmpTCPDomain | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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'), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, seems like an unrelated bug! Let me fix this in master... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,) | ||
|
@@ -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,) | ||
|
There was a problem hiding this comment.
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?