(file) Return to snmpDeliverTrap_emanate.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Handler / snmpIndicationHandler

  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 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           PEGASUS_NAMESPACE_BEGIN
 41           
 42           PEGASUS_USING_STD;
 43           
 44 kumpf 1.3 static char *sr_filename = __FILE__;
 45           
 46           IPCFunctionP IPCfp;  /* IPC functions pointer  */
 47           
 48           // This code should be generated by Emanate 
 49           // Since we do not have MIB objects defined,
 50           // just defined here to load subagent as library
 51           
 52           // The objects internal to the agent 
 53           ObjectInfo OidList[] = 
 54           {
 55           { { 0, NULL },
 56           #ifndef LIGHT
 57               NULL,
 58           #endif /* LIGHT */
 59               0, 0, 0, 0,
 60               NULL, NULL }
 61           };
 62           
 63           // This code should be generated by Emanate in k_* routine
 64           // Since we do not have MIB objects defined,
 65 kumpf 1.3 // just defined here to pass compile 
 66           
 67           // Called by the master agent during initialization */
 68           int k_initialize()
 69           {
 70              return 1;
 71           }
 72           
 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 kumpf 1.3     InitIPCArrayTCP(&IPCfp);
 87           #else /* SR_UDS_IPC */
 88               InitIPCArrayUDS(&IPCfp);
 89           #endif /* SR_UDS_IPC */
 90           
 91               if(InitSubagent() == -1) 
 92               {
 93           	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.8     ArrayDestroyer<char> _trapOid(trapOid.allocateCString());
120               _trapOid.getPointer()[strlen(_trapOid.getPointer())-1] = '\0';
121               OID *sendtrapOid = MakeOIDFromDot(_trapOid.getPointer());
122 kumpf 1.5 
123               // Destination : converting destination into Transport
124 kumpf 1.8     ArrayDestroyer<char> trap_dest(destination.allocateCString());
125 kumpf 1.5     TransportInfo   global_ti;
126               global_ti.type = SR_IP_TRANSPORT;
127 kumpf 1.8     global_ti.t_ipAddr = inet_addr(trap_dest.getPointer());
128 kumpf 1.5     global_ti.t_ipPort = htons((unsigned short)GetSNMPTrapPort());
129           
130               // Community Name
131 kumpf 1.8     ArrayDestroyer<char> _community(community.allocateCString());
132               OctetString* community_name = MakeOctetStringFromText(
133                                                 _community.getPointer());
134 kumpf 1.5 
135 kumpf 1.3     // getting IP address of the host
136 kumpf 1.8     ArrayDestroyer<char> hostname(System::getHostName().allocateCString());
137 kumpf 1.3     char **p;
138               struct hostent *hp;
139               struct in_addr in;
140 kumpf 1.8     hp=gethostbyname(hostname.getPointer());
141 kumpf 1.3     p = hp->h_addr_list;
142               (void)memcpy(&in.s_addr, *p, sizeof(in.s_addr));
143               char* IP_string = inet_ntoa(in);
144               
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 kumpf 1.3     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               agent_addr->octet_ptr[3] = (unsigned char)s4;
166           
167 kumpf 1.4     // specTrap from trapOid. 
168 kumpf 1.5     SR_INT32 genTrap = 0;
169               SR_INT32 specTrap = 0;
170 kumpf 1.3     
171 kumpf 1.5     OID* enterpriseOid ;
172           
173               Array<String> standard_traps;
174           
175               standard_traps.append(String("1.3.6.1.6.3.1.1.5.1"));
176               standard_traps.append(String("1.3.6.1.6.3.1.1.5.2"));
177               standard_traps.append(String("1.3.6.1.6.3.1.1.5.3"));
178               standard_traps.append(String("1.3.6.1.6.3.1.1.5.4"));
179               standard_traps.append(String("1.3.6.1.6.3.1.1.5.5"));
180               standard_traps.append(String("1.3.6.1.6.3.1.1.5.6"));
181           
182               Array<String> oids;
183               String tmpoid = trapOid;
184           
185               while(tmpoid.find(".") != PEG_NOT_FOUND)
186               {
187                   oids.append(tmpoid.subString(0, tmpoid.find(".")));
188                   tmpoid = tmpoid.subString(tmpoid.find(".") + 1);
189               }
190               oids.append(tmpoid);
191           
192 kumpf 1.5     String ent;
193               if (oids[oids.size()-2] == "0")
194               {
195                   ent = oids[0];
196                   for (Uint8 i = 1; i < oids.size()-2; i++)
197                       ent = ent + "." + oids[i];
198               }
199           
200               if (Contains(standard_traps, trapOid))
201               {
202           	ent = oids[0];
203           	for (Uint8 i = 1; i < oids.size()-1; i++)
204                       ent = ent + "." + oids[i];
205           
206 kumpf 1.8 	ArrayDestroyer<char> gtrap(ent.allocateCString());
207                   genTrap = atoi(gtrap.getPointer()) - 1;
208 kumpf 1.5         enterpriseOid = sendtrapOid;
209               }
210               else
211               {
212           	genTrap = 6;
213 mike  1.2 
214 kumpf 1.5 	ent = oids[0];
215           	for (Uint8 i = 1; i < oids.size()-1; i++)
216                       ent = ent + "." + oids[i];
217 kumpf 1.8 	ArrayDestroyer<char> strap(oids[oids.size()-1].allocateCString());
218                   specTrap = atoi(strap.getPointer());
219 mike  1.2 
220 kumpf 1.5         if (oids[oids.size()-2] == "0")
221                   {
222                       ent = oids[0];
223           	    for (Uint8 i = 1; i < oids.size()-2; i++)
224           		ent = ent + "." + oids[i];
225           
226 kumpf 1.8 	    ArrayDestroyer<char> _ent(ent.allocateCString());
227                       enterpriseOid = MakeOIDFromDot(_ent.getPointer());
228 kumpf 1.5         }
229                   else
230                   {
231           	    ent = oids[0];
232                       for (Uint8 i = 1; i < oids.size()-1; i++)
233                           ent = ent + "." + oids[i];
234           
235 kumpf 1.8             ArrayDestroyer<char> _ent(ent.allocateCString());
236                       enterpriseOid = MakeOIDFromDot(_ent.getPointer());
237 kumpf 1.5         }
238               }
239           
240 kumpf 1.4     for(Uint32 i = 0; i < vbOids.size(); i++)
241 mike  1.2     {
242 kumpf 1.8         ArrayDestroyer<char> _vbOid(vbOids[i].allocateCString());
243                   ArrayDestroyer<char> _vbValue(vbValues[i].allocateCString());
244 kumpf 1.5 
245 kumpf 1.8 	if ((object = MakeOIDFromDot(_vbOid.getPointer())) == NULL)
246 kumpf 1.3         {
247                       cout << "Invalid OID received: " << vbOids[i] << endl;
248                       return;
249                   } 
250           
251 kumpf 1.6         if (vbTypes[i] == String("OctetString"))
252 kumpf 1.3         {
253 kumpf 1.8             newValue = CloneOctetString(MakeOctetStringFromText(_vbValue.getPointer()));
254 kumpf 1.4             if (newValue == NULL)
255                       {
256                           cout << "Invalid Value provided : " << vbValues[i] << endl;
257                           return;
258                       }
259                       if ((vb2 = MakeVarBindWithValue(object, 
260 kumpf 1.3 		(OID *) NULL, 
261           		OCTET_PRIM_TYPE, 
262           		newValue)) == NULL)
263                       {
264                           cout << "Invalid OctetString value: " << vbValues[i] << endl;
265                           return;
266                       }
267                   }
268 mike  1.2         else
269                   {
270 kumpf 1.8             int vbvalue = atoi(_vbValue.getPointer());
271 kumpf 1.6             void* value = &vbvalue;
272 kumpf 1.3 
273 kumpf 1.4             if (newValue == NULL)
274                       {
275                           cout << "Invalid Value provided : " << vbValues[i] << endl;
276                           return;
277                       }
278                       if ((vb2 = MakeVarBindWithValue(object, 
279 kumpf 1.3 		(OID *) NULL, 
280           		INTEGER_TYPE, 
281 kumpf 1.6 		value)) == NULL)
282 mike  1.2             {
283 kumpf 1.3                 cout << "Invalid Integer Value: " << vbValues[i] << endl;
284                           return;
285 mike  1.2             }
286                   }
287 kumpf 1.4         if (i == 0)
288                   {
289                       vb = vb2;
290                       vb3 = vb2;
291                   }
292                   else
293                   {
294                       vb3->next_var = vb2;
295                       vb3 = vb3->next_var;
296                   }
297           
298           	FreeOID(object);
299 kumpf 1.3     }
300 kumpf 1.5 
301 kumpf 1.4     vb3->next_var = NULL;
302 kumpf 1.3 
303               // Now sending the trap
304 kumpf 1.6     if (trapType == String("SNMPv1"))
305 kumpf 1.3     {
306                   SendNotificationToDestSMIv1Params(
307 kumpf 1.5             1,					// notifyType - TRAP
308                       genTrap,				// genTrap
309                       specTrap,				// specTrap
310 kumpf 1.3             enterpriseOid,			// enterprise
311                       agent_addr,				// agent_addr
312 kumpf 1.5             vb,					// vb
313 kumpf 1.3             NULL,				// contextName
314                       1,					// retryCount
315                       1,					// timeout
316 kumpf 1.5             community_name,			// securityName,
317 kumpf 1.3             SR_SECURITY_LEVEL_NOAUTH,		// securityLevel
318                       SR_SECURITY_MODEL_V1,		// securityModel
319                       &global_ti,				// Transport Info
320                       0);					// cfg_chk
321               }
322               else if (trapType == String("SNMPv2"))
323               {
324                   SendNotificationToDestSMIv2Params(
325 kumpf 1.6             1,					// notifyType - NOTIFICATION
326 kumpf 1.3             sendtrapOid,			// snmpTrapOID
327                       agent_addr,				// agent_addr
328 kumpf 1.5             vb,					// vb
329 kumpf 1.3             NULL,				// contextName
330                       1,					// retryCount
331                       100,				// timeout
332 kumpf 1.5             community_name,			// securityName or community
333 kumpf 1.3             SR_SECURITY_LEVEL_NOAUTH,		// securityLevel
334                       SR_SECURITY_MODEL_V1,		// securityModel
335                       &global_ti,				// TransportInfo
336                       0);					// cfg_chk
337                   FreeVarBindList(vb);
338                   FreeVarBindList(vb2);
339               }
340               else
341               {
342                   cout << "Trap type not supported : " << trapType << endl;
343                   exit(2);
344 mike  1.2     }
345 kumpf 1.6 
346               FreeVarBindList(vb);
347               FreeVarBindList(vb2);
348               FreeVarBindList(vb3);
349 mike  1.2 }
350           
351           PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2