File: SNMPAgent.pm

package info (click to toggle)
libsnmp-session-perl 1.14~git20221124T101957-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,104 kB
  • sloc: perl: 11,920; ansic: 25; makefile: 15
file content (127 lines) | stat: -rw-r--r-- 3,503 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# SNMPAgent.pm
#
# Object for handling SNMP requests as per draft-ietf-radius-servmib-04.txt
#
# Author: Mike McCauley (mikem@open.com.au)
# Copyright (C) 1997 Open System Consultants
# $Id: SNMPAgent.pm,v 1.2 1999-02-21 22:26:49 leinen Exp $

use SNMP_Session "0.68";
use SNMP_util;
use Socket;

$port = SNMP_Session::standard_udp_port;
$session = SNMP_Session->open('0.0.0.0', '', 
			      $port, undef, $port);


while (1)
{
    &handle_socket_read();
}

#####################################################################
# This function is called whenever there is a packet waiting to
# be read from the SNMP port
sub handle_socket_read
{
    my ($fileno) = @_;

    my ($request, $iaddr, $port) = $session->receive_request();
    if (defined $request)
    {
	my ($type, $requestid, $bindings, $community) 
	    = $session->decode_request($request);
	
	print "SNMPAgent: received request $type, $requestid, $community\n";

	my ($error, $errorstatus, $errorindex);
	$errorstatus = $errorindex = 0;

	# Check the community
	if ($community ne 'public')
	{
	    print "SNMPAgent: wrong community: $community. Ignored\n";
	    return;
	}
	my $index = 0;

	my @results;
	binding: while (!$errorstatus && $bindings ne '') 
	{
	    my $binding;
	    ($binding, $bindings) = BER::decode_sequence($bindings);
	    while (!$errorstatus && $binding ne '')
	    {
		($b, $binding) = BER::decode_sequence($binding);
		$index++;
		if ($type == SNMP_Session::get_request)
		{
		    # SAMPLE code only:
		    my ($oid) = BER::decode_oid($b);  # Binary oid
		    my $poid = BER::pretty_oid($oid);
		    print "get request for $poid\n";
		    my $value = BER::encode_int(12345);
		    push(@results, BER::encode_sequence($oid, $value));
		}
		elsif ($type == SNMP_Session::getnext_request)
		{
		    # SAMPLE code only:
		    my ($oid) = BER::decode_oid($b);  # Binary oid
		    my $poid = BER::pretty_oid($oid);
		    print "getnext request for $poid\n";

		    # fake up the next oid by just incrementing
		    @fromoid = split(/\./, $poid);
		    $fromoid[-1]++;
		    print "changed to @fromoid\n";
		    $oid = BER::encode_oid(@fromoid);
		    my $value = BER::encode_int(12345);
		    push(@results, BER::encode_sequence($oid, $value));
		}
		elsif ($type == SNMP_Session::set_request)
		{
		    # SAMPLE code only:
		    my ($oid, $value) = BER::decode_by_template($b, "%O%@");
		    my $poid = BER::pretty_oid($oid);
		    ($value) = BER::decode_int($value);
		    print "set request for $poid to $value\n";
		    $value = BER::encode_int($value);
		    push(@results, BER::encode_sequence($oid, $value));

		}
		else
		{
		    warn "SNMPAgent: error decoding request: " . $BER::errmsg;
		    return;
		}
		if ($errorstatus)
		{
		    $errorindex = $index;
		    last binding;
		}
	    }
	}

	# OK we've got everything they asked for, so return it
	$request = BER::encode_tagged_sequence(SNMP_Session::get_response,
					     BER::encode_int($requestid),
					     BER::encode_int($errorstatus), 
					     BER::encode_int($errorindex),
					     BER::encode_sequence(@results))
	    || warn "SNMPAgent: error encoding reply: " . $BER::errmsg;

	$session->{remote_addr} = Socket::pack_sockaddr_in($port, $iaddr);
	$session->{community} = $community;
	$request = $session->wrap_request($request);
	# tell the session where to send the reply to
	$session->send_query($request)
	    || warn "SNMPAgent: error sending reply: $!";
    }
    else
    {
	warn "SNMPAgent: receive_request failed: $!";
    }
}

1;