Skip to content

Commit f415383

Browse files
committed
feat(client): add support for SNMPv3 sessions
1 parent f924025 commit f415383

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/snmp/client.cr

+27-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class SNMP::Client
66
end
77

88
getter socket : UDPSocket
9-
getter session : SNMP::Session
9+
getter session : SNMP::Session | SNMP::V3::Session
1010
getter host, timeout, port
1111

1212
def initialize(@host : String, community = "public", @timeout = 3, @port = 161)
@@ -16,6 +16,12 @@ class SNMP::Client
1616
@session = SNMP::Session.new(community: community)
1717
end
1818

19+
def initialize(@host : String, @session : SNMP::Session | SNMP::V3::Session, @timeout = 3, @port = 161)
20+
@socket = UDPSocket.new
21+
socket.sync = false
22+
socket.read_timeout = timeout
23+
end
24+
1925
private def with_socket
2026
if socket.closed?
2127
@socket = UDPSocket.new
@@ -38,7 +44,12 @@ class SNMP::Client
3844
end
3945

4046
private def get(oid : String, sock : UDPSocket) : SNMP::Message
41-
sock.write_bytes session.get(oid)
47+
check_validation_probe(sock)
48+
49+
message = session.get(oid)
50+
message = session.prepare(message) if message.is_a?(SNMP::V3::Message)
51+
52+
sock.write_bytes message
4253
sock.flush
4354
session.parse(sock.read_bytes(ASN1::BER))
4455
end
@@ -55,7 +66,12 @@ class SNMP::Client
5566
end
5667

5768
private def get_next(oid : String, sock : UDPSocket) : SNMP::Message
58-
sock.write_bytes session.get_next(oid)
69+
check_validation_probe(sock)
70+
71+
message = session.get_next(oid)
72+
message = session.prepare(message) if message.is_a?(SNMP::V3::Message)
73+
74+
sock.write_bytes message
5975
sock.flush
6076
session.parse(sock.read_bytes(ASN1::BER))
6177
end
@@ -92,4 +108,12 @@ class SNMP::Client
92108
end
93109
self
94110
end
111+
112+
protected def check_validation_probe(sock)
113+
if session.must_revalidate?
114+
sock.write_bytes session.engine_validation_probe
115+
sock.flush
116+
session.validate sock.read_bytes(ASN1::BER)
117+
end
118+
end
95119
end

0 commit comments

Comments
 (0)