(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.3     initialize();
107 mike  1.2 
108 kumpf 1.4     //void*   newValue;
109               OctetString*   newValue;
110 kumpf 1.3     char*   entV2Trap;
111               int	    vb_link_flag = 0;
112           
113               VarBind *vb = NULL;
114               VarBind *vb2 = NULL;
115 kumpf 1.4     VarBind *vb3 = NULL;
116 kumpf 1.3     
117               OID	    *object = NULL;
118           
119               // getting IP address of the host
120               char* hostname = System::getHostName().allocateCString();
121               char **p;
122               struct hostent *hp;
123               struct in_addr in;
124               hp=gethostbyname(hostname);
125               p = hp->h_addr_list;
126               (void)memcpy(&in.s_addr, *p, sizeof(in.s_addr));
127               char* IP_string = inet_ntoa(in);
128               
129               // converting destination into Transport
130               TransportInfo   global_ti;
131               global_ti.type = SR_IP_TRANSPORT;
132               // address to which to send trap
133               global_ti.t_ipAddr = inet_addr(destination.allocateCString());
134               // port to which to send trap
135               global_ti.t_ipPort = htons((unsigned short)GetSNMPTrapPort());  
136           
137 kumpf 1.3     // formatting agent(host) address into OctetString format
138               OctetString* agent_addr;
139           
140               SR_INT32 s1, s2, s3, s4;
141               SR_UINT32 ipaddr;
142           
143               // pull out each of the 4 octet values from IP address
144               sscanf(IP_string,"%d.%d.%d.%d", &s1, &s2, &s3, &s4);
145            
146               // Probably should perform some checks on values for
147               // s1, s2, s3, and s4 here to make sure values are
148               // between 0 and 255
149            
150               // create an empty 4 length OctetString
151               agent_addr = MakeOctetString(NULL,4);
152            
153               // fill in values for OctetString
154               agent_addr->octet_ptr[0] = (unsigned char)s1;
155               agent_addr->octet_ptr[1] = (unsigned char)s2;
156               agent_addr->octet_ptr[2] = (unsigned char)s3;
157               agent_addr->octet_ptr[3] = (unsigned char)s4;
158 kumpf 1.3 
159 kumpf 1.4     // ATTN-NU-20020312 : Write code to get enterprise, genTrap and 
160               // specTrap from trapOid. 
161 kumpf 1.3 
162               String enterprise;
163               
164               OID* enterpriseOid = MakeOIDFromDot(enterprise.allocateCString());
165               
166               // getting trap data for do_trap()
167               entV2Trap = trapOid.allocateCString();
168 mike  1.2 
169 kumpf 1.3     // getting trapOid
170               OID *sendtrapOid = MakeOIDFromDot(trapOid.allocateCString());
171 mike  1.2 
172 kumpf 1.4     for(Uint32 i = 0; i < vbOids.size(); i++)
173 mike  1.2     {
174 kumpf 1.3 	if ((object = MakeOIDFromDot(vbOids[i].allocateCString())) == NULL)
175                   {
176                       cout << "Invalid OID received: " << vbOids[i] << endl;
177                       return;
178                   } 
179           
180                   if (strcmp(vbTypes[i].allocateCString(),"OctetString") == 0)
181                   {
182 kumpf 1.4             newValue = CloneOctetString(MakeOctetStringFromText
183                                      (vbValues[i].allocateCString()));
184                       if (newValue == NULL)
185                       {
186                           cout << "Invalid Value provided : " << vbValues[i] << endl;
187                           return;
188                       }
189                       if ((vb2 = MakeVarBindWithValue(object, 
190 kumpf 1.3 		(OID *) NULL, 
191           		OCTET_PRIM_TYPE, 
192           		newValue)) == NULL)
193                       {
194                           cout << "Invalid OctetString value: " << vbValues[i] << endl;
195                           return;
196                       }
197                   }
198 mike  1.2         else
199                   {
200 kumpf 1.4             newValue = CloneOctetString(MakeOctetString(
201 kumpf 1.3 		(unsigned char *) vbValues[i].allocateCString(),
202 kumpf 1.4 		strlen(vbValues[i].allocateCString())));
203 kumpf 1.3 
204 kumpf 1.4             if (newValue == NULL)
205                       {
206                           cout << "Invalid Value provided : " << vbValues[i] << endl;
207                           return;
208                       }
209                       if ((vb2 = MakeVarBindWithValue(object, 
210 kumpf 1.3 		(OID *) NULL, 
211           		INTEGER_TYPE, 
212           		newValue)) == NULL)
213 mike  1.2             {
214 kumpf 1.3                 cout << "Invalid Integer Value: " << vbValues[i] << endl;
215                           return;
216 mike  1.2             }
217                   }
218 kumpf 1.4         if (i == 0)
219                   {
220                       vb = vb2;
221                       vb3 = vb2;
222                   }
223                   else
224                   {
225                       vb3->next_var = vb2;
226                       vb3 = vb3->next_var;
227                   }
228           
229           	FreeOID(object);
230 kumpf 1.3     }
231               
232 kumpf 1.4     vb3->next_var = NULL;
233 kumpf 1.3 
234               // Now sending the trap
235               if (trapType == String("SNMPGeneric"))
236               {
237           	do_trap(6, 4, vb2, enterpriseOid, entV2Trap);
238                   FreeVarBindList(vb);
239                   FreeVarBindList(vb2);
240               }
241               else if (trapType == String("SNMPv1"))
242               {
243 kumpf 1.4         cout << "Sending SNMPv1 Trap : " << trapOid << endl;
244 kumpf 1.3         SendNotificationToDestSMIv1Params(
245                       1,					// notifyType
246                       6,					// genTrap
247                       1,					// specTrap
248                       enterpriseOid,			// enterprise
249                       agent_addr,				// agent_addr
250                       vb2,				// vb
251                       NULL,				// contextName
252                       1,					// retryCount
253                       1,					// timeout
254                       MakeOctetStringFromText(community.allocateCString()),	// securityName,
255                       SR_SECURITY_LEVEL_NOAUTH,		// securityLevel
256                       SR_SECURITY_MODEL_V1,		// securityModel
257                       &global_ti,				// Transport Info
258                       0);					// cfg_chk
259           
260                   FreeVarBindList(vb);
261                   FreeVarBindList(vb2);
262               }
263               else if (trapType == String("SNMPv2"))
264               {
265 kumpf 1.4         cout << "Sending SNMPv2 Trap : " << trapOid << endl;
266 kumpf 1.3         SendNotificationToDestSMIv2Params(
267                       1,					// notifyType
268                       sendtrapOid,			// snmpTrapOID
269                       agent_addr,				// agent_addr
270 kumpf 1.4             //vb2,				// vb
271                       vb,				// vb
272 kumpf 1.3             NULL,				// contextName
273                       1,					// retryCount
274                       100,				// timeout
275                       MakeOctetStringFromText(community.allocateCString()),	// securityName or community
276                       SR_SECURITY_LEVEL_NOAUTH,		// securityLevel
277                       SR_SECURITY_MODEL_V1,		// securityModel
278                       &global_ti,				// TransportInfo
279                       0);					// cfg_chk
280                   FreeVarBindList(vb);
281                   FreeVarBindList(vb2);
282               }
283               else
284               {
285                   cout << "Trap type not supported : " << trapType << endl;
286                   exit(2);
287 mike  1.2     }
288           }
289           
290           PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2