1 mike 1.2 //%/////////////////////////////////////////////////////////////////////////////
2 //
|
3 kumpf 1.10 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
|
5 mike 1.2 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 kumpf 1.10 // 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 mike 1.2 // 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 kumpf 1.10 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
14 mike 1.2 // 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 kumpf 1.10 // 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 mike 1.2 // 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 //==============================================================================
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 kumpf 1.8 #include <Pegasus/Common/Destroyer.h>
|
33 mike 1.2 #include <fcntl.h>
34 #include <unistd.h>
|
35 kumpf 1.3 #include <ctype.h>
|
36 mike 1.2 #include <sys/stat.h>
37 #include "snmpDeliverTrap_emanate.h"
|
38 kumpf 1.4 #include "prnt_lib.h"
|
39 mike 1.2
|
40 kumpf 1.3 static char *sr_filename = __FILE__;
41
42 IPCFunctionP IPCfp; /* IPC functions pointer */
43
44 // This code should be generated by Emanate
45 // Since we do not have MIB objects defined,
46 // just defined here to load subagent as library
47
48 // The objects internal to the agent
49 ObjectInfo OidList[] =
50 {
51 { { 0, NULL },
52 #ifndef LIGHT
53 NULL,
54 #endif /* LIGHT */
55 0, 0, 0, 0,
56 NULL, NULL }
57 };
58
59 // This code should be generated by Emanate in k_* routine
60 // Since we do not have MIB objects defined,
61 kumpf 1.3 // just defined here to pass compile
62
63 // Called by the master agent during initialization */
64 int k_initialize()
65 {
66 return 1;
67 }
|
68 kumpf 1.9
69 PEGASUS_NAMESPACE_BEGIN
70
71 PEGASUS_USING_STD;
|
72 kumpf 1.3
73 snmpDeliverTrap_emanate::snmpDeliverTrap_emanate()
74 {
75
76 }
77
78 snmpDeliverTrap_emanate::~snmpDeliverTrap_emanate()
79 {
80
81 }
82
83 void snmpDeliverTrap_emanate::initialize()
84 {
85 #ifndef SR_UDS_IPC
86 InitIPCArrayTCP(&IPCfp);
87 #else /* SR_UDS_IPC */
88 InitIPCArrayUDS(&IPCfp);
89 #endif /* SR_UDS_IPC */
90
91 if(InitSubagent() == -1)
92 {
93 kumpf 1.3 cout << "InitSubagent Failed to initialize" << endl;
94 exit(1);
95 }
96 }
97
98 void snmpDeliverTrap_emanate::deliverTrap(
99 const String& trapOid,
100 const String& community,
|
101 mike 1.2 const String& destination,
102 const String& trapType,
103 Array<String>& vbOids,
104 Array<String>& vbTypes,
105 Array<String>& vbValues)
106 {
|
107 kumpf 1.5 OctetString* newValue;
|
108 kumpf 1.3
109 VarBind *vb = NULL;
110 VarBind *vb2 = NULL;
|
111 kumpf 1.4 VarBind *vb3 = NULL;
|
112 kumpf 1.3
113 OID *object = NULL;
114
|
115 kumpf 1.5 // Initializing with Master agent
116 initialize();
117
118 // TRAP OID: getting trapOid
|
119 kumpf 1.11 CString _trapOid = trapOid.getCString();
120 _trapOid[strlen(_trapOid)-1] = '\0';
121 OID *sendtrapOid = MakeOIDFromDot(_trapOid);
|
122 kumpf 1.5
123 // Destination : converting destination into Transport
124 TransportInfo global_ti;
125 global_ti.type = SR_IP_TRANSPORT;
|
126 kumpf 1.11 global_ti.t_ipAddr = inet_addr(destination.getCString());
|
127 kumpf 1.5 global_ti.t_ipPort = htons((unsigned short)GetSNMPTrapPort());
128
129 // Community Name
|
130 kumpf 1.8 OctetString* community_name = MakeOctetStringFromText(
|
131 kumpf 1.11 community.getCString());
|
132 kumpf 1.5
|
133 kumpf 1.3 // getting IP address of the host
134 char **p;
135 struct hostent *hp;
136 struct in_addr in;
|
137 kumpf 1.11 hp=gethostbyname(System::getHostName().getCString());
|
138 kumpf 1.3 p = hp->h_addr_list;
139 (void)memcpy(&in.s_addr, *p, sizeof(in.s_addr));
140 char* IP_string = inet_ntoa(in);
141
142 // formatting agent(host) address into OctetString format
143 OctetString* agent_addr;
144
145 SR_INT32 s1, s2, s3, s4;
146 SR_UINT32 ipaddr;
147
148 // pull out each of the 4 octet values from IP address
149 sscanf(IP_string,"%d.%d.%d.%d", &s1, &s2, &s3, &s4);
150
151 // Probably should perform some checks on values for
152 // s1, s2, s3, and s4 here to make sure values are
153 // between 0 and 255
154
155 // create an empty 4 length OctetString
156 agent_addr = MakeOctetString(NULL,4);
157
158 // fill in values for OctetString
159 kumpf 1.3 agent_addr->octet_ptr[0] = (unsigned char)s1;
160 agent_addr->octet_ptr[1] = (unsigned char)s2;
161 agent_addr->octet_ptr[2] = (unsigned char)s3;
162 agent_addr->octet_ptr[3] = (unsigned char)s4;
163
|
164 kumpf 1.4 // specTrap from trapOid.
|
165 kumpf 1.5 SR_INT32 genTrap = 0;
166 SR_INT32 specTrap = 0;
|
167 kumpf 1.3
|
168 kumpf 1.5 OID* enterpriseOid ;
169
170 Array<String> standard_traps;
171
172 standard_traps.append(String("1.3.6.1.6.3.1.1.5.1"));
173 standard_traps.append(String("1.3.6.1.6.3.1.1.5.2"));
174 standard_traps.append(String("1.3.6.1.6.3.1.1.5.3"));
175 standard_traps.append(String("1.3.6.1.6.3.1.1.5.4"));
176 standard_traps.append(String("1.3.6.1.6.3.1.1.5.5"));
177 standard_traps.append(String("1.3.6.1.6.3.1.1.5.6"));
178
179 Array<String> oids;
180 String tmpoid = trapOid;
181
182 while(tmpoid.find(".") != PEG_NOT_FOUND)
183 {
184 oids.append(tmpoid.subString(0, tmpoid.find(".")));
185 tmpoid = tmpoid.subString(tmpoid.find(".") + 1);
186 }
187 oids.append(tmpoid);
188
189 kumpf 1.5 String ent;
190 if (oids[oids.size()-2] == "0")
191 {
192 ent = oids[0];
193 for (Uint8 i = 1; i < oids.size()-2; i++)
194 ent = ent + "." + oids[i];
195 }
196
197 if (Contains(standard_traps, trapOid))
198 {
199 ent = oids[0];
200 for (Uint8 i = 1; i < oids.size()-1; i++)
201 ent = ent + "." + oids[i];
202
|
203 kumpf 1.11 genTrap = atoi(ent.getCString()) - 1;
|
204 kumpf 1.5 enterpriseOid = sendtrapOid;
205 }
206 else
207 {
208 genTrap = 6;
|
209 mike 1.2
|
210 kumpf 1.5 ent = oids[0];
211 for (Uint8 i = 1; i < oids.size()-1; i++)
212 ent = ent + "." + oids[i];
|
213 kumpf 1.11 specTrap = atoi(oids[oids.size()-1].getCString());
|
214 mike 1.2
|
215 kumpf 1.5 if (oids[oids.size()-2] == "0")
216 {
217 ent = oids[0];
218 for (Uint8 i = 1; i < oids.size()-2; i++)
219 ent = ent + "." + oids[i];
220
|
221 kumpf 1.11 enterpriseOid = MakeOIDFromDot(ent.getCString());
|
222 kumpf 1.5 }
223 else
224 {
225 ent = oids[0];
226 for (Uint8 i = 1; i < oids.size()-1; i++)
227 ent = ent + "." + oids[i];
228
|
229 kumpf 1.11 enterpriseOid = MakeOIDFromDot(ent.getCString());
|
230 kumpf 1.5 }
231 }
232
|
233 kumpf 1.4 for(Uint32 i = 0; i < vbOids.size(); i++)
|
234 mike 1.2 {
|
235 kumpf 1.11 if ((object = MakeOIDFromDot(vbOids[i].getCString())) == NULL)
|
236 kumpf 1.3 {
237 cout << "Invalid OID received: " << vbOids[i] << endl;
238 return;
239 }
240
|
241 kumpf 1.6 if (vbTypes[i] == String("OctetString"))
|
242 kumpf 1.3 {
|
243 kumpf 1.11 newValue = CloneOctetString(MakeOctetStringFromText(vbValues[i].getCString()));
|
244 kumpf 1.4 if (newValue == NULL)
245 {
246 cout << "Invalid Value provided : " << vbValues[i] << endl;
247 return;
248 }
249 if ((vb2 = MakeVarBindWithValue(object,
|
250 kumpf 1.3 (OID *) NULL,
251 OCTET_PRIM_TYPE,
252 newValue)) == NULL)
253 {
254 cout << "Invalid OctetString value: " << vbValues[i] << endl;
255 return;
256 }
257 }
|
258 mike 1.2 else
259 {
|
260 kumpf 1.8 int vbvalue = atoi(_vbValue.getPointer());
|
261 kumpf 1.6 void* value = &vbvalue;
|
262 kumpf 1.3
|
263 kumpf 1.4 if (newValue == NULL)
264 {
265 cout << "Invalid Value provided : " << vbValues[i] << endl;
266 return;
267 }
268 if ((vb2 = MakeVarBindWithValue(object,
|
269 kumpf 1.3 (OID *) NULL,
270 INTEGER_TYPE,
|
271 kumpf 1.6 value)) == NULL)
|
272 mike 1.2 {
|
273 kumpf 1.3 cout << "Invalid Integer Value: " << vbValues[i] << endl;
274 return;
|
275 mike 1.2 }
276 }
|
277 kumpf 1.4 if (i == 0)
278 {
279 vb = vb2;
280 vb3 = vb2;
281 }
282 else
283 {
284 vb3->next_var = vb2;
285 vb3 = vb3->next_var;
286 }
287
288 FreeOID(object);
|
289 kumpf 1.3 }
|
290 kumpf 1.5
|
291 kumpf 1.4 vb3->next_var = NULL;
|
292 kumpf 1.3
293 // Now sending the trap
|
294 kumpf 1.6 if (trapType == String("SNMPv1"))
|
295 kumpf 1.3 {
296 SendNotificationToDestSMIv1Params(
|
297 kumpf 1.5 1, // notifyType - TRAP
298 genTrap, // genTrap
299 specTrap, // specTrap
|
300 kumpf 1.3 enterpriseOid, // enterprise
301 agent_addr, // agent_addr
|
302 kumpf 1.5 vb, // vb
|
303 kumpf 1.3 NULL, // contextName
304 1, // retryCount
305 1, // timeout
|
306 kumpf 1.5 community_name, // securityName,
|
307 kumpf 1.3 SR_SECURITY_LEVEL_NOAUTH, // securityLevel
308 SR_SECURITY_MODEL_V1, // securityModel
309 &global_ti, // Transport Info
310 0); // cfg_chk
311 }
312 else if (trapType == String("SNMPv2"))
313 {
314 SendNotificationToDestSMIv2Params(
|
315 kumpf 1.6 1, // notifyType - NOTIFICATION
|
316 kumpf 1.3 sendtrapOid, // snmpTrapOID
317 agent_addr, // agent_addr
|
318 kumpf 1.5 vb, // vb
|
319 kumpf 1.3 NULL, // contextName
320 1, // retryCount
321 100, // timeout
|
322 kumpf 1.5 community_name, // securityName or community
|
323 kumpf 1.3 SR_SECURITY_LEVEL_NOAUTH, // securityLevel
324 SR_SECURITY_MODEL_V1, // securityModel
325 &global_ti, // TransportInfo
326 0); // cfg_chk
327 FreeVarBindList(vb);
328 FreeVarBindList(vb2);
329 }
330 else
331 {
332 cout << "Trap type not supported : " << trapType << endl;
333 exit(2);
|
334 mike 1.2 }
|
335 kumpf 1.6
336 FreeVarBindList(vb);
337 FreeVarBindList(vb2);
338 FreeVarBindList(vb3);
|
339 mike 1.2 }
340
341 PEGASUS_NAMESPACE_END
|