1 mike 1.2 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001 BMC Software, Hewlett Packard, IBM, The Open Group,
4 // Tivoli Systems
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
14 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 mike 1.2 //==============================================================================
23 //
24 // Author: Nitin Upasani, Hewlett-Packard Company (Nitin_Upasani@hp.com)
25 //
26 // Modified By:
27 //
28 //%/////////////////////////////////////////////////////////////////////////////
29
30 #include <Pegasus/Common/Config.h>
|
31 kumpf 1.3 #include <Pegasus/Common/System.h>
|
32 mike 1.2 #include <fcntl.h>
33 #include <unistd.h>
|
34 kumpf 1.3 #include <ctype.h>
|
35 mike 1.2 #include <sys/stat.h>
36 #include "snmpDeliverTrap_emanate.h"
|
37 kumpf 1.4 #include "prnt_lib.h"
|
38 mike 1.2
39 PEGASUS_NAMESPACE_BEGIN
40
41 PEGASUS_USING_STD;
42
|
43 kumpf 1.3 static char *sr_filename = __FILE__;
44
45 IPCFunctionP IPCfp; /* IPC functions pointer */
46
47 // This code should be generated by Emanate
48 // Since we do not have MIB objects defined,
49 // just defined here to load subagent as library
50
51 // The objects internal to the agent
52 ObjectInfo OidList[] =
53 {
54 { { 0, NULL },
55 #ifndef LIGHT
56 NULL,
57 #endif /* LIGHT */
58 0, 0, 0, 0,
59 NULL, NULL }
60 };
61
62 // This code should be generated by Emanate in k_* routine
63 // Since we do not have MIB objects defined,
64 kumpf 1.3 // just defined here to pass compile
65
66 // Called by the master agent during initialization */
67 int k_initialize()
68 {
69 return 1;
70 }
71
72 snmpDeliverTrap_emanate::snmpDeliverTrap_emanate()
73 {
74
75 }
76
77 snmpDeliverTrap_emanate::~snmpDeliverTrap_emanate()
78 {
79
80 }
81
82 void snmpDeliverTrap_emanate::initialize()
83 {
84 #ifndef SR_UDS_IPC
85 kumpf 1.3 InitIPCArrayTCP(&IPCfp);
86 #else /* SR_UDS_IPC */
87 InitIPCArrayUDS(&IPCfp);
88 #endif /* SR_UDS_IPC */
89
90 if(InitSubagent() == -1)
91 {
92 cout << "InitSubagent Failed to initialize" << endl;
93 exit(1);
94 }
95 }
96
97 void snmpDeliverTrap_emanate::deliverTrap(
98 const String& trapOid,
99 const String& community,
|
100 mike 1.2 const String& destination,
101 const String& trapType,
102 Array<String>& vbOids,
103 Array<String>& vbTypes,
104 Array<String>& vbValues)
105 {
|
106 kumpf 1.5 OctetString* newValue;
|
107 kumpf 1.3
108 VarBind *vb = NULL;
109 VarBind *vb2 = NULL;
|
110 kumpf 1.4 VarBind *vb3 = NULL;
|
111 kumpf 1.3
112 OID *object = NULL;
113
|
114 kumpf 1.5 // Initializing with Master agent
115 initialize();
116
117 // TRAP OID: getting trapOid
118 char* _trapOid = trapOid.allocateCString();
119 OID *sendtrapOid = MakeOIDFromDot(_trapOid);
120
121 // Destination : converting destination into Transport
122 char* trap_dest = destination.allocateCString();
123 TransportInfo global_ti;
124 global_ti.type = SR_IP_TRANSPORT;
125 global_ti.t_ipAddr = inet_addr(trap_dest);
126 global_ti.t_ipPort = htons((unsigned short)GetSNMPTrapPort());
127 delete [] trap_dest;
128
129 // Community Name
130 char* _community = community.allocateCString();
131 OctetString* community_name = MakeOctetStringFromText(_community);
132 delete [] _community;
133
|
134 kumpf 1.3 // getting IP address of the host
135 char* hostname = System::getHostName().allocateCString();
136 char **p;
137 struct hostent *hp;
138 struct in_addr in;
139 hp=gethostbyname(hostname);
140 p = hp->h_addr_list;
141 (void)memcpy(&in.s_addr, *p, sizeof(in.s_addr));
142 char* IP_string = inet_ntoa(in);
|
143 kumpf 1.5 delete [] hostname;
|
144 kumpf 1.3
145 // formatting agent(host) address into OctetString format
146 OctetString* agent_addr;
147
148 SR_INT32 s1, s2, s3, s4;
149 SR_UINT32 ipaddr;
150
151 // pull out each of the 4 octet values from IP address
152 sscanf(IP_string,"%d.%d.%d.%d", &s1, &s2, &s3, &s4);
153
154 // Probably should perform some checks on values for
155 // s1, s2, s3, and s4 here to make sure values are
156 // between 0 and 255
157
158 // create an empty 4 length OctetString
159 agent_addr = MakeOctetString(NULL,4);
160
161 // fill in values for OctetString
162 agent_addr->octet_ptr[0] = (unsigned char)s1;
163 agent_addr->octet_ptr[1] = (unsigned char)s2;
164 agent_addr->octet_ptr[2] = (unsigned char)s3;
165 kumpf 1.3 agent_addr->octet_ptr[3] = (unsigned char)s4;
166
|
167 kumpf 1.4 // ATTN-NU-20020312 : Write code to get enterprise, genTrap and
168 // specTrap from trapOid.
|
169 kumpf 1.3
|
170 kumpf 1.5 //NU_SNMP
171
172 SR_INT32 genTrap = 0;
173 SR_INT32 specTrap = 0;
|
174 kumpf 1.3
|
175 kumpf 1.5 OID* enterpriseOid ;
176
177 Array<String> standard_traps;
178
179 standard_traps.append(String("1.3.6.1.6.3.1.1.5.1"));
180 standard_traps.append(String("1.3.6.1.6.3.1.1.5.2"));
181 standard_traps.append(String("1.3.6.1.6.3.1.1.5.3"));
182 standard_traps.append(String("1.3.6.1.6.3.1.1.5.4"));
183 standard_traps.append(String("1.3.6.1.6.3.1.1.5.5"));
184 standard_traps.append(String("1.3.6.1.6.3.1.1.5.6"));
185
186 Array<String> oids;
187 String tmpoid = trapOid;
188
189 while(tmpoid.find(".") != PEG_NOT_FOUND)
190 {
191 oids.append(tmpoid.subString(0, tmpoid.find(".")));
192 tmpoid = tmpoid.subString(tmpoid.find(".") + 1);
193 }
194 oids.append(tmpoid);
195
196 kumpf 1.5 String ent;
197 if (oids[oids.size()-2] == "0")
198 {
199 ent = oids[0];
200 for (Uint8 i = 1; i < oids.size()-2; i++)
201 ent = ent + "." + oids[i];
202 }
203
204 if (Contains(standard_traps, trapOid))
205 {
206 ent = oids[0];
207 for (Uint8 i = 1; i < oids.size()-1; i++)
208 ent = ent + "." + oids[i];
209
210 char* gtrap = ent.allocateCString();
211 genTrap = atoi(gtrap) - 1;
212 enterpriseOid = sendtrapOid;
213 delete [] gtrap;
214 }
215 else
216 {
217 kumpf 1.5 genTrap = 6;
|
218 mike 1.2
|
219 kumpf 1.5 ent = oids[0];
220 for (Uint8 i = 1; i < oids.size()-1; i++)
221 ent = ent + "." + oids[i];
222 char* strap = oids[oids.size()-1].allocateCString();
223 specTrap = atoi(strap);
224 delete [] strap;
|
225 mike 1.2
|
226 kumpf 1.5 if (oids[oids.size()-2] == "0")
227 {
228 ent = oids[0];
229 for (Uint8 i = 1; i < oids.size()-2; i++)
230 ent = ent + "." + oids[i];
231
232 char* _ent = ent.allocateCString();
233 enterpriseOid = MakeOIDFromDot(_ent);
234 delete [] _ent;
235 }
236 else
237 {
238 ent = oids[0];
239 for (Uint8 i = 1; i < oids.size()-1; i++)
240 ent = ent + "." + oids[i];
241
242 char* _ent = ent.allocateCString();
243 enterpriseOid = MakeOIDFromDot(_ent);
244 delete [] _ent;
245 }
246 }
247 kumpf 1.5
248 char* _vbOid;
249 char* _vbType;
250 char* _vbValue;
251
|
252 kumpf 1.4 for(Uint32 i = 0; i < vbOids.size(); i++)
|
253 mike 1.2 {
|
254 kumpf 1.5 _vbOid = vbOids[i].allocateCString();
255 _vbType = vbTypes[i].allocateCString();
256 _vbValue = vbValues[i].allocateCString();
257
258 if ((object = MakeOIDFromDot(_vbOid)) == NULL)
|
259 kumpf 1.3 {
260 cout << "Invalid OID received: " << vbOids[i] << endl;
261 return;
262 }
263
|
264 kumpf 1.5 if (strcmp(_vbType,"OctetString") == 0)
|
265 kumpf 1.3 {
|
266 kumpf 1.5 newValue = CloneOctetString(MakeOctetStringFromText(_vbValue));
|
267 kumpf 1.4 if (newValue == NULL)
268 {
269 cout << "Invalid Value provided : " << vbValues[i] << endl;
270 return;
271 }
272 if ((vb2 = MakeVarBindWithValue(object,
|
273 kumpf 1.3 (OID *) NULL,
274 OCTET_PRIM_TYPE,
275 newValue)) == NULL)
276 {
277 cout << "Invalid OctetString value: " << vbValues[i] << endl;
278 return;
279 }
280 }
|
281 mike 1.2 else
282 {
|
283 kumpf 1.4 newValue = CloneOctetString(MakeOctetString(
|
284 kumpf 1.5 (unsigned char *) _vbValue, strlen(_vbValue)));
|
285 kumpf 1.3
|
286 kumpf 1.4 if (newValue == NULL)
287 {
288 cout << "Invalid Value provided : " << vbValues[i] << endl;
289 return;
290 }
291 if ((vb2 = MakeVarBindWithValue(object,
|
292 kumpf 1.3 (OID *) NULL,
293 INTEGER_TYPE,
294 newValue)) == NULL)
|
295 mike 1.2 {
|
296 kumpf 1.3 cout << "Invalid Integer Value: " << vbValues[i] << endl;
297 return;
|
298 mike 1.2 }
299 }
|
300 kumpf 1.4 if (i == 0)
301 {
302 vb = vb2;
303 vb3 = vb2;
304 }
305 else
306 {
307 vb3->next_var = vb2;
308 vb3 = vb3->next_var;
309 }
310
311 FreeOID(object);
|
312 kumpf 1.3 }
|
313 kumpf 1.5
314 delete [] _vbType;
315 delete [] _vbValue;
316 delete [] _vbOid;
|
317 kumpf 1.3
|
318 kumpf 1.4 vb3->next_var = NULL;
|
319 kumpf 1.3
320 // Now sending the trap
321 if (trapType == String("SNMPGeneric"))
322 {
|
323 kumpf 1.5 cout << genTrap << endl;
324 cout << specTrap << endl;
325 PrintVarBindList(vb);
326 //PrintOID(enterpriseOid);
327 cout << _trapOid << endl;
328 cout << "Sending SNMP generic trap : " << trapOid << endl;
329
330 do_trap(genTrap, specTrap, vb, enterpriseOid, _trapOid);
331
332 FreeVarBindList(vb);
|
333 kumpf 1.3 FreeVarBindList(vb2);
334 }
335 else if (trapType == String("SNMPv1"))
336 {
|
337 kumpf 1.5 cout << "Sending SNMPv1 Trap : " << trapOid << endl;
|
338 kumpf 1.3 SendNotificationToDestSMIv1Params(
|
339 kumpf 1.5 1, // notifyType - TRAP
340 genTrap, // genTrap
341 specTrap, // specTrap
|
342 kumpf 1.3 enterpriseOid, // enterprise
343 agent_addr, // agent_addr
|
344 kumpf 1.5 vb, // vb
|
345 kumpf 1.3 NULL, // contextName
346 1, // retryCount
347 1, // timeout
|
348 kumpf 1.5 community_name, // securityName,
|
349 kumpf 1.3 SR_SECURITY_LEVEL_NOAUTH, // securityLevel
350 SR_SECURITY_MODEL_V1, // securityModel
351 &global_ti, // Transport Info
352 0); // cfg_chk
353
354 FreeVarBindList(vb);
355 FreeVarBindList(vb2);
356 }
357 else if (trapType == String("SNMPv2"))
358 {
|
359 kumpf 1.4 cout << "Sending SNMPv2 Trap : " << trapOid << endl;
|
360 kumpf 1.3 SendNotificationToDestSMIv2Params(
|
361 kumpf 1.5 2, // notifyType - NOTIFICATION
|
362 kumpf 1.3 sendtrapOid, // snmpTrapOID
363 agent_addr, // agent_addr
|
364 kumpf 1.5 vb, // vb
|
365 kumpf 1.3 NULL, // contextName
366 1, // retryCount
367 100, // timeout
|
368 kumpf 1.5 community_name, // securityName or community
|
369 kumpf 1.3 SR_SECURITY_LEVEL_NOAUTH, // securityLevel
370 SR_SECURITY_MODEL_V1, // securityModel
371 &global_ti, // TransportInfo
372 0); // cfg_chk
373 FreeVarBindList(vb);
374 FreeVarBindList(vb2);
375 }
376 else
377 {
378 cout << "Trap type not supported : " << trapType << endl;
|
379 kumpf 1.5 delete [] _trapOid;
|
380 kumpf 1.3 exit(2);
|
381 mike 1.2 }
|
382 kumpf 1.5
383 delete [] _trapOid;
|
384 mike 1.2 }
385
386 PEGASUS_NAMESPACE_END
|