source: trunk/Packages/synapse/source/demo/snmpserv/snmp.pas

Last change on this file was 2, checked in by chronos, 12 years ago
  • Přidáno: Základní kostra projektu.
  • Přidáno: Knihovna synapse.
File size: 2.1 KB
Line 
1unit snmp;
2
3interface
4
5uses
6 Classes, blcksock, winsock, Synautil, SysUtils, SNMPSend, asn1util;
7
8type
9 TUDPSnmpDaemon = class(TThread)
10 private
11 Sock:TUDPBlockSocket;
12 snmprec: TSNMPRec;
13 public
14 Constructor Create;
15 Destructor Destroy; override;
16 procedure Execute; override;
17 procedure ProcessSnmpRequest(PDU: integer; var OID, Value: string;
18 var valuetype: integer);
19 end;
20
21
22implementation
23
24{ TUDPSnmpDaemon }
25
26Constructor TUDPSnmpDaemon.Create;
27begin
28 sock:=TUDPBlockSocket.create;
29 snmprec := TSNMPRec.create;
30 FreeOnTerminate:=true;
31 inherited create(false);
32end;
33
34Destructor TUDPSnmpDaemon.Destroy;
35begin
36 Sock.free;
37 snmprec.free;
38 inherited Destroy;
39end;
40
41procedure TUDPSnmpDaemon.Execute;
42var
43 Buf: string;
44 n: integer;
45 mib: TSNMPMib;
46 oid, value: string;
47 valuetype: integer;
48begin
49 with sock do
50 begin
51 bind('0.0.0.0','161');
52 if sock.LastError<>0 then
53 exit;
54 repeat
55 if terminated then break;
56 buf := sock.RecvPacket(1000);
57 if sock.lasterror = 0 then
58 begin
59 snmprec.Clear;
60 snmprec.DecodeBuf(buf);
61 for n := 0 to snmprec.MIBCount - 1 do
62 begin
63 mib := snmprec.MIBByIndex(n);
64 if mib <> nil then
65 begin
66 oid := mib.OID;
67 value := mib.Value;
68 valuetype := mib.valuetype;
69 ProcessSnmpRequest(snmprec.PDUType, oid, value, valuetype);
70 mib.OID := oid;
71 mib.Value := value;
72 mib.valuetype := valuetype;
73 end;
74 end;
75 snmprec.PDUType := PDUGetResponse;
76 snmprec.ErrorStatus := 0;
77 Buf := snmprec.EncodeBuf;
78 sock.SendString(Buf);
79 end;
80 until false;
81 end;
82end;
83
84procedure TUDPSnmpDaemon.ProcessSnmpRequest(PDU: integer; var OID, Value: string;
85 var valuetype: integer);
86begin
87 if PDU = PDUGetRequest then
88 begin
89 if OID = '1.3.6.1.2.1.1.1.0' then
90 begin
91 Value := 'Synapse SNMP agent demo';
92 Valuetype := ASN1_OCTSTR;
93 end;
94 end;
95end;
96
97end.
Note: See TracBrowser for help on using the repository browser.