(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 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     // specTrap from trapOid. 
169 kumpf 1.5     SR_INT32 genTrap = 0;
170               SR_INT32 specTrap = 0;
171 kumpf 1.3     
172 kumpf 1.5     OID* enterpriseOid ;
173           
174               Array<String> standard_traps;
175           
176               standard_traps.append(String("1.3.6.1.6.3.1.1.5.1"));
177               standard_traps.append(String("1.3.6.1.6.3.1.1.5.2"));
178               standard_traps.append(String("1.3.6.1.6.3.1.1.5.3"));
179               standard_traps.append(String("1.3.6.1.6.3.1.1.5.4"));
180               standard_traps.append(String("1.3.6.1.6.3.1.1.5.5"));
181               standard_traps.append(String("1.3.6.1.6.3.1.1.5.6"));
182           
183               Array<String> oids;
184               String tmpoid = trapOid;
185           
186               while(tmpoid.find(".") != PEG_NOT_FOUND)
187               {
188                   oids.append(tmpoid.subString(0, tmpoid.find(".")));
189                   tmpoid = tmpoid.subString(tmpoid.find(".") + 1);
190               }
191               oids.append(tmpoid);
192           
193 kumpf 1.5     String ent;
194               if (oids[oids.size()-2] == "0")
195               {
196                   ent = oids[0];
197                   for (Uint8 i = 1; i < oids.size()-2; i++)
198                       ent = ent + "." + oids[i];
199               }
200           
201               if (Contains(standard_traps, trapOid))
202               {
203           	ent = oids[0];
204           	for (Uint8 i = 1; i < oids.size()-1; i++)
205                       ent = ent + "." + oids[i];
206           
207           	char* gtrap = ent.allocateCString();
208                   genTrap = atoi(gtrap) - 1;
209                   enterpriseOid = sendtrapOid;
210                   delete [] gtrap;
211               }
212               else
213               {
214 kumpf 1.5 	genTrap = 6;
215 mike  1.2 
216 kumpf 1.5 	ent = oids[0];
217           	for (Uint8 i = 1; i < oids.size()-1; i++)
218                       ent = ent + "." + oids[i];
219           	char* strap = oids[oids.size()-1].allocateCString();
220                   specTrap = atoi(strap);
221                   delete [] strap;
222 mike  1.2 
223 kumpf 1.5         if (oids[oids.size()-2] == "0")
224                   {
225                       ent = oids[0];
226           	    for (Uint8 i = 1; i < oids.size()-2; i++)
227           		ent = ent + "." + oids[i];
228           
229           	    char* _ent = ent.allocateCString();
230                       enterpriseOid = MakeOIDFromDot(_ent);
231                       delete [] _ent;
232                   }
233                   else
234                   {
235           	    ent = oids[0];
236                       for (Uint8 i = 1; i < oids.size()-1; i++)
237                           ent = ent + "." + oids[i];
238           
239                       char* _ent = ent.allocateCString();
240                       enterpriseOid = MakeOIDFromDot(_ent);
241                       delete [] _ent;
242                   }
243               }
244 kumpf 1.5 
245               char* _vbOid;
246               char* _vbValue;
247               
248 kumpf 1.4     for(Uint32 i = 0; i < vbOids.size(); i++)
249 mike  1.2     {
250 kumpf 1.5 	_vbOid = vbOids[i].allocateCString();
251           	_vbValue = vbValues[i].allocateCString();
252           
253           	if ((object = MakeOIDFromDot(_vbOid)) == NULL)
254 kumpf 1.3         {
255                       cout << "Invalid OID received: " << vbOids[i] << endl;
256                       return;
257                   } 
258           
259 kumpf 1.6         if (vbTypes[i] == String("OctetString"))
260 kumpf 1.3         {
261 kumpf 1.5             newValue = CloneOctetString(MakeOctetStringFromText(_vbValue));
262 kumpf 1.4             if (newValue == NULL)
263                       {
264                           cout << "Invalid Value provided : " << vbValues[i] << endl;
265                           return;
266                       }
267                       if ((vb2 = MakeVarBindWithValue(object, 
268 kumpf 1.3 		(OID *) NULL, 
269           		OCTET_PRIM_TYPE, 
270           		newValue)) == NULL)
271                       {
272                           cout << "Invalid OctetString value: " << vbValues[i] << endl;
273                           return;
274                       }
275                   }
276 mike  1.2         else
277                   {
278 kumpf 1.6             int vbvalue = atoi(_vbValue);
279                       void* value = &vbvalue;
280 kumpf 1.3 
281 kumpf 1.4             if (newValue == NULL)
282                       {
283                           cout << "Invalid Value provided : " << vbValues[i] << endl;
284                           return;
285                       }
286                       if ((vb2 = MakeVarBindWithValue(object, 
287 kumpf 1.3 		(OID *) NULL, 
288           		INTEGER_TYPE, 
289 kumpf 1.6 		value)) == NULL)
290 mike  1.2             {
291 kumpf 1.3                 cout << "Invalid Integer Value: " << vbValues[i] << endl;
292                           return;
293 mike  1.2             }
294                   }
295 kumpf 1.4         if (i == 0)
296                   {
297                       vb = vb2;
298                       vb3 = vb2;
299                   }
300                   else
301                   {
302                       vb3->next_var = vb2;
303                       vb3 = vb3->next_var;
304                   }
305           
306           	FreeOID(object);
307 kumpf 1.3     }
308 kumpf 1.5 
309               delete [] _vbValue;
310               delete [] _vbOid;
311 kumpf 1.3     
312 kumpf 1.4     vb3->next_var = NULL;
313 kumpf 1.3 
314               // Now sending the trap
315 kumpf 1.6     if (trapType == String("SNMPv1"))
316 kumpf 1.3     {
317                   SendNotificationToDestSMIv1Params(
318 kumpf 1.5             1,					// notifyType - TRAP
319                       genTrap,				// genTrap
320                       specTrap,				// specTrap
321 kumpf 1.3             enterpriseOid,			// enterprise
322                       agent_addr,				// agent_addr
323 kumpf 1.5             vb,					// vb
324 kumpf 1.3             NULL,				// contextName
325                       1,					// retryCount
326                       1,					// timeout
327 kumpf 1.5             community_name,			// securityName,
328 kumpf 1.3             SR_SECURITY_LEVEL_NOAUTH,		// securityLevel
329                       SR_SECURITY_MODEL_V1,		// securityModel
330                       &global_ti,				// Transport Info
331                       0);					// cfg_chk
332               }
333               else if (trapType == String("SNMPv2"))
334               {
335                   SendNotificationToDestSMIv2Params(
336 kumpf 1.6             1,					// notifyType - NOTIFICATION
337 kumpf 1.3             sendtrapOid,			// snmpTrapOID
338                       agent_addr,				// agent_addr
339 kumpf 1.5             vb,					// vb
340 kumpf 1.3             NULL,				// contextName
341                       1,					// retryCount
342                       100,				// timeout
343 kumpf 1.5             community_name,			// securityName or community
344 kumpf 1.3             SR_SECURITY_LEVEL_NOAUTH,		// securityLevel
345                       SR_SECURITY_MODEL_V1,		// securityModel
346                       &global_ti,				// TransportInfo
347                       0);					// cfg_chk
348                   FreeVarBindList(vb);
349                   FreeVarBindList(vb2);
350               }
351               else
352               {
353                   cout << "Trap type not supported : " << trapType << endl;
354 kumpf 1.5 	delete [] _trapOid;
355 kumpf 1.3         exit(2);
356 mike  1.2     }
357 kumpf 1.6 
358               FreeVarBindList(vb);
359               FreeVarBindList(vb2);
360               FreeVarBindList(vb3);
361 kumpf 1.5 
362               delete [] _trapOid;
363 mike  1.2 }
364           
365           PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2