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 kumpf 1.6 _trapOid[strlen(_trapOid)-1] = '\0';
|
120 kumpf 1.5 OID *sendtrapOid = MakeOIDFromDot(_trapOid);
121
122 // Destination : converting destination into Transport
123 char* trap_dest = destination.allocateCString();
124 TransportInfo global_ti;
125 global_ti.type = SR_IP_TRANSPORT;
126 global_ti.t_ipAddr = inet_addr(trap_dest);
127 global_ti.t_ipPort = htons((unsigned short)GetSNMPTrapPort());
128 delete [] trap_dest;
129
130 // Community Name
131 char* _community = community.allocateCString();
132 OctetString* community_name = MakeOctetStringFromText(_community);
133 delete [] _community;
134
|
135 kumpf 1.3 // getting IP address of the host
136 char* hostname = System::getHostName().allocateCString();
137 char **p;
138 struct hostent *hp;
139 struct in_addr in;
140 hp=gethostbyname(hostname);
141 p = hp->h_addr_list;
142 (void)memcpy(&in.s_addr, *p, sizeof(in.s_addr));
143 char* IP_string = inet_ntoa(in);
|
144 kumpf 1.5 delete [] hostname;
|
145 kumpf 1.3
146 // formatting agent(host) address into OctetString format
147 OctetString* agent_addr;
148
149 SR_INT32 s1, s2, s3, s4;
150 SR_UINT32 ipaddr;
151
152 // pull out each of the 4 octet values from IP address
153 sscanf(IP_string,"%d.%d.%d.%d", &s1, &s2, &s3, &s4);
154
155 // Probably should perform some checks on values for
156 // s1, s2, s3, and s4 here to make sure values are
157 // between 0 and 255
158
159 // create an empty 4 length OctetString
160 agent_addr = MakeOctetString(NULL,4);
161
162 // fill in values for OctetString
163 agent_addr->octet_ptr[0] = (unsigned char)s1;
164 agent_addr->octet_ptr[1] = (unsigned char)s2;
165 agent_addr->octet_ptr[2] = (unsigned char)s3;
166 kumpf 1.3 agent_addr->octet_ptr[3] = (unsigned char)s4;
167
|
168 kumpf 1.4 // ATTN-NU-20020312 : Write code to get enterprise, genTrap and
169 // specTrap from trapOid.
|
170 kumpf 1.3
|
171 kumpf 1.5 //NU_SNMP
172
173 SR_INT32 genTrap = 0;
174 SR_INT32 specTrap = 0;
|
175 kumpf 1.3
|
176 kumpf 1.5 OID* enterpriseOid ;
177
178 Array<String> standard_traps;
179
180 standard_traps.append(String("1.3.6.1.6.3.1.1.5.1"));
181 standard_traps.append(String("1.3.6.1.6.3.1.1.5.2"));
182 standard_traps.append(String("1.3.6.1.6.3.1.1.5.3"));
183 standard_traps.append(String("1.3.6.1.6.3.1.1.5.4"));
184 standard_traps.append(String("1.3.6.1.6.3.1.1.5.5"));
185 standard_traps.append(String("1.3.6.1.6.3.1.1.5.6"));
186
187 Array<String> oids;
188 String tmpoid = trapOid;
189
190 while(tmpoid.find(".") != PEG_NOT_FOUND)
191 {
192 oids.append(tmpoid.subString(0, tmpoid.find(".")));
193 tmpoid = tmpoid.subString(tmpoid.find(".") + 1);
194 }
195 oids.append(tmpoid);
196
197 kumpf 1.5 String ent;
198 if (oids[oids.size()-2] == "0")
199 {
200 ent = oids[0];
201 for (Uint8 i = 1; i < oids.size()-2; i++)
202 ent = ent + "." + oids[i];
203 }
204
205 if (Contains(standard_traps, trapOid))
206 {
207 ent = oids[0];
208 for (Uint8 i = 1; i < oids.size()-1; i++)
209 ent = ent + "." + oids[i];
210
211 char* gtrap = ent.allocateCString();
212 genTrap = atoi(gtrap) - 1;
213 enterpriseOid = sendtrapOid;
214 delete [] gtrap;
215 }
216 else
217 {
218 kumpf 1.5 genTrap = 6;
|
219 mike 1.2
|
220 kumpf 1.5 ent = oids[0];
221 for (Uint8 i = 1; i < oids.size()-1; i++)
222 ent = ent + "." + oids[i];
223 char* strap = oids[oids.size()-1].allocateCString();
224 specTrap = atoi(strap);
225 delete [] strap;
|
226 mike 1.2
|
227 kumpf 1.5 if (oids[oids.size()-2] == "0")
228 {
229 ent = oids[0];
230 for (Uint8 i = 1; i < oids.size()-2; i++)
231 ent = ent + "." + oids[i];
232
233 char* _ent = ent.allocateCString();
234 enterpriseOid = MakeOIDFromDot(_ent);
235 delete [] _ent;
236 }
237 else
238 {
239 ent = oids[0];
240 for (Uint8 i = 1; i < oids.size()-1; i++)
241 ent = ent + "." + oids[i];
242
243 char* _ent = ent.allocateCString();
244 enterpriseOid = MakeOIDFromDot(_ent);
245 delete [] _ent;
246 }
247 }
248 kumpf 1.5
249 char* _vbOid;
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 _vbValue = vbValues[i].allocateCString();
256
257 if ((object = MakeOIDFromDot(_vbOid)) == NULL)
|
258 kumpf 1.3 {
259 cout << "Invalid OID received: " << vbOids[i] << endl;
260 return;
261 }
262
|
263 kumpf 1.6 if (vbTypes[i] == String("OctetString"))
|
264 kumpf 1.3 {
|
265 kumpf 1.5 newValue = CloneOctetString(MakeOctetStringFromText(_vbValue));
|
266 kumpf 1.4 if (newValue == NULL)
267 {
268 cout << "Invalid Value provided : " << vbValues[i] << endl;
269 return;
270 }
271 if ((vb2 = MakeVarBindWithValue(object,
|
272 kumpf 1.3 (OID *) NULL,
273 OCTET_PRIM_TYPE,
274 newValue)) == NULL)
275 {
276 cout << "Invalid OctetString value: " << vbValues[i] << endl;
277 return;
278 }
279 }
|
280 mike 1.2 else
281 {
|
282 kumpf 1.6 int vbvalue = atoi(_vbValue);
283 void* value = &vbvalue;
|
284 kumpf 1.3
|
285 kumpf 1.4 if (newValue == NULL)
286 {
287 cout << "Invalid Value provided : " << vbValues[i] << endl;
288 return;
289 }
290 if ((vb2 = MakeVarBindWithValue(object,
|
291 kumpf 1.3 (OID *) NULL,
292 INTEGER_TYPE,
|
293 kumpf 1.6 value)) == NULL)
|
294 mike 1.2 {
|
295 kumpf 1.3 cout << "Invalid Integer Value: " << vbValues[i] << endl;
296 return;
|
297 mike 1.2 }
298 }
|
299 kumpf 1.4 if (i == 0)
300 {
301 vb = vb2;
302 vb3 = vb2;
303 }
304 else
305 {
306 vb3->next_var = vb2;
307 vb3 = vb3->next_var;
308 }
309
310 FreeOID(object);
|
311 kumpf 1.3 }
|
312 kumpf 1.5
313 delete [] _vbValue;
314 delete [] _vbOid;
|
315 kumpf 1.3
|
316 kumpf 1.4 vb3->next_var = NULL;
|
317 kumpf 1.3
318 // Now sending the trap
|
319 kumpf 1.6 if (trapType == String("SNMPv1"))
|
320 kumpf 1.3 {
321 SendNotificationToDestSMIv1Params(
|
322 kumpf 1.5 1, // notifyType - TRAP
323 genTrap, // genTrap
324 specTrap, // specTrap
|
325 kumpf 1.3 enterpriseOid, // enterprise
326 agent_addr, // agent_addr
|
327 kumpf 1.5 vb, // vb
|
328 kumpf 1.3 NULL, // contextName
329 1, // retryCount
330 1, // timeout
|
331 kumpf 1.5 community_name, // securityName,
|
332 kumpf 1.3 SR_SECURITY_LEVEL_NOAUTH, // securityLevel
333 SR_SECURITY_MODEL_V1, // securityModel
334 &global_ti, // Transport Info
335 0); // cfg_chk
336 }
337 else if (trapType == String("SNMPv2"))
338 {
339 SendNotificationToDestSMIv2Params(
|
340 kumpf 1.6 1, // notifyType - NOTIFICATION
|
341 kumpf 1.3 sendtrapOid, // snmpTrapOID
342 agent_addr, // agent_addr
|
343 kumpf 1.5 vb, // vb
|
344 kumpf 1.3 NULL, // contextName
345 1, // retryCount
346 100, // timeout
|
347 kumpf 1.5 community_name, // securityName or community
|
348 kumpf 1.3 SR_SECURITY_LEVEL_NOAUTH, // securityLevel
349 SR_SECURITY_MODEL_V1, // securityModel
350 &global_ti, // TransportInfo
351 0); // cfg_chk
352 FreeVarBindList(vb);
353 FreeVarBindList(vb2);
354 }
355 else
356 {
357 cout << "Trap type not supported : " << trapType << endl;
|
358 kumpf 1.5 delete [] _trapOid;
|
359 kumpf 1.3 exit(2);
|
360 mike 1.2 }
|
361 kumpf 1.6
362 FreeVarBindList(vb);
363 FreeVarBindList(vb2);
364 FreeVarBindList(vb3);
|
365 kumpf 1.5
366 delete [] _trapOid;
|
367 mike 1.2 }
368
369 PEGASUS_NAMESPACE_END
|