2
%% <year>1996-2007</year>
3
%% <holder>Ericsson AB, All Rights Reserved</holder>
4
%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
6
6
%% The contents of this file are subject to the Erlang Public License,
7
7
%% Version 1.1, (the "License"); you may not use this file except in
8
8
%% compliance with the License. You should have received a copy of the
9
9
%% Erlang Public License along with this software. If not, it can be
10
10
%% retrieved online at http://www.erlang.org/.
12
12
%% Software distributed under the License is distributed on an "AS IS"
13
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
14
%% the License for the specific language governing rights and limitations
15
15
%% under the License.
17
%% The Initial Developer of the Original Code is Ericsson AB.
20
21
%% ts:run(snmp, snmp_agent_test, [batch]).
66
send_discovery_pdu(Pdu, PacketPid) when record(Pdu, pdu) ->
68
send_discovery_pdu(Pdu, PacketPid) when is_record(Pdu, pdu) ->
67
69
PacketPid ! {send_discovery_pdu, self(), Pdu},
68
70
await_discovery_response_pdu().
70
72
await_discovery_response_pdu() ->
72
{discovery_response,Reply} ->
75
await_discovery_response_pdu()
74
{discovery_response, Reply} ->
79
send_pdu(Pdu, PacketPid) when record(Pdu, pdu) ->
79
send_pdu(Pdu, PacketPid) when is_record(Pdu, pdu) ->
80
80
PacketPid ! {send_pdu, Pdu}.
82
send_msg(Msg, PacketPid, Ip, Udp) when record(Msg, message) ->
82
send_msg(Msg, PacketPid, Ip, Udp) when is_record(Msg, message) ->
83
83
PacketPid ! {send_msg, Msg, Ip, Udp}.
85
85
send_bytes(Bytes, PacketPid) ->
128
128
case mk_discovery_msg(Version, Pdu, VsnHdr, "") of
131
{M, B} when list(B) ->
132
put(discovery,{M,From}),
131
{M, B} when is_list(B) ->
132
put(discovery, {M, From}),
133
133
display_outgoing_message(M),
134
134
udp_send(UdpId, AgentIp, UdpPort, B)
136
packet_loop(SnmpMgr,UdpId,AgentIp,UdpPort,VsnHdr,Version,[]);
136
packet_loop(SnmpMgr, UdpId, AgentIp, UdpPort, VsnHdr, Version, []);
137
138
{send_pdu, Pdu} ->
138
139
d("packet_loop -> received send_pdu with"
139
140
"~n Pdu: ~p", [Pdu]),
272
handle_udp_packet(V,{DiscoReqMsg,From},_UdpId,_Ip,_UdpPort,Bytes,_,_AgentIp) ->
274
handle_udp_packet(V, {DiscoReqMsg, From}, _UdpId, _Ip, _UdpPort,
275
Bytes, _, _AgentIp) ->
273
276
DiscoRspMsg = (catch snmp_pdus:dec_message(Bytes)),
274
277
display_incomming_message(DiscoRspMsg),
275
Reply = (catch check_discovery_result(V,DiscoReqMsg,DiscoRspMsg)),
276
From ! {discovery_response,Reply},
278
_Reply = (catch check_discovery_result(V, DiscoReqMsg, DiscoRspMsg)),
279
case (catch check_discovery_result(V, DiscoReqMsg, DiscoRspMsg)) of
280
{ok, AgentEngineID} when is_list(AgentEngineID) ->
281
%% Ok, step 1 complete, now for step 2
282
%% Which we skip for now
283
OK = {ok, AgentEngineID},
284
From ! {discovery_response, OK},
287
From ! {discovery_response, Error},
279
292
%% This function assumes that the agent and the manager (thats us)
280
293
%% has the same version.
281
check_discovery_result('version-3',DiscoReqMsg,DiscoRspMsg) ->
294
check_discovery_result('version-3', DiscoReqMsg, DiscoRspMsg) ->
282
295
ReqMsgID = getMsgID(DiscoReqMsg),
283
296
RspMsgID = getMsgID(DiscoRspMsg),
284
check_msgID(ReqMsgID,RspMsgID),
285
ReqRequestId = getRequestId('version-3',DiscoReqMsg),
286
RspRequestId = getRequestId('version-3',DiscoRspMsg),
287
check_requestId(ReqRequestId,RspRequestId),
288
{ok,getMsgAuthEngineID(DiscoRspMsg)};
289
check_discovery_result(Version,DiscoReqMsg,DiscoRspMsg) ->
290
ReqRequestId = getRequestId(Version,DiscoReqMsg),
291
RspRequestId = getRequestId(Version,DiscoRspMsg),
292
check_requestId(ReqRequestId,RspRequestId),
293
{ok,getSysDescr(DiscoRspMsg)}.
297
check_msgID(ReqMsgID, RspMsgID),
298
ReqRequestId = getRequestId('version-3', DiscoReqMsg),
299
RspRequestId = getRequestId('version-3', DiscoRspMsg),
300
check_requestId(ReqRequestId, RspRequestId),
301
{ok, getMsgAuthEngineID(DiscoRspMsg)};
302
check_discovery_result(Version, DiscoReqMsg, DiscoRspMsg) ->
303
ReqRequestId = getRequestId(Version, DiscoReqMsg),
304
RspRequestId = getRequestId(Version, DiscoRspMsg),
305
check_requestId(ReqRequestId, RspRequestId),
306
{ok, getSysDescr(DiscoRspMsg)}.
295
check_msgID(ID,ID) ->
308
check_msgID(ID, ID) ->
297
check_msgID(ReqMsgID,RspMsgID) ->
298
throw({error,{invalid_msgID,ReqMsgID,RspMsgID}}).
310
check_msgID(ReqMsgID, RspMsgID) ->
311
throw({error, {invalid_msgID, ReqMsgID, RspMsgID}}).
300
313
check_requestId(Id,Id) ->
302
check_requestId(ReqRequestId,RspRequestId) ->
303
throw({error,{invalid_requestId,ReqRequestId,RspRequestId}}).
315
check_requestId(ReqRequestId, RspRequestId) ->
316
throw({error, {invalid_requestId, ReqRequestId, RspRequestId}}).
305
getMsgID(M) when record(M,message) ->
318
getMsgID(M) when is_record(M, message) ->
306
319
(M#message.vsn_hdr)#v3_hdr.msgID.
308
getRequestId('version-3',M) when record(M,message) ->
321
getRequestId('version-3',M) when is_record(M, message) ->
309
322
((M#message.data)#scopedPdu.data)#pdu.request_id;
310
getRequestId(_Version,M) when record(M,message) ->
323
getRequestId(_Version,M) when is_record(M, message) ->
311
324
(M#message.data)#pdu.request_id;
312
325
getRequestId(Version,M) ->
313
326
io:format("************* ERROR ****************"
344
357
IsReportable = snmp_misc:is_reportable(MsgFlags),
345
358
SecRes = (catch SecModule:process_incoming_msg(list_to_binary(Packet),
346
359
Data,SecParams,SecLevel)),
347
{_SecEngineID, SecName, ScopedPDUBytes, SecData} =
360
{_SecEngineID, SecName, ScopedPDUBytes, SecData, _} =
348
361
check_sec_module_result(SecRes, V3Hdr, Data, IsReportable),
349
case catch snmp_pdus:dec_scoped_pdu(ScopedPDUBytes) of
350
ScopedPDU when record(ScopedPDU, scopedPdu) ->
362
case (catch snmp_pdus:dec_scoped_pdu(ScopedPDUBytes)) of
363
ScopedPDU when is_record(ScopedPDU, scopedPdu) ->
351
364
{ok, ScopedPDU, {MsgId, SecName, SecData}};
352
365
{'EXIT', Reason} ->
353
throw({error, Reason})
366
throw({error, Reason});
368
throw({error, {scoped_pdu_decode_failed, Error}})
370
handle_v3_msg(_Packet, BadMessage) ->
371
throw({error, bad_message, BadMessage}).
356
373
get_security_module(?SEC_USM) ->
569
586
display_message("Incomming",M).
571
588
display_outgoing_message(M) ->
572
display_message("Outgoing",M).
589
display_message("Outgoing", M).
574
display_message(Direction,M) when record(M,message) ->
575
io:format("~s SNMP message:~n",[Direction]),
591
display_message(Direction, M) when is_record(M, message) ->
592
io:format("~s SNMP message:~n", [Direction]),
576
593
V = M#message.version,
577
594
display_version(V),
578
display_hdr(V,M#message.vsn_hdr),
579
display_msg_data(V,Direction,M#message.data);
580
display_message(Direction,M) ->
581
io:format("~s message unknown: ~n~p",[Direction,M]).
595
display_hdr(V, M#message.vsn_hdr),
596
display_msg_data(V, Direction, M#message.data);
597
display_message(Direction, M) ->
598
io:format("~s message unknown: ~n~p", [Direction, M]).
583
600
display_version('version-3') ->
584
601
display_prop("Version",'SNMPv3');