version 1.2, 2001/12/13 14:54:23
|
version 1.3, 2002/03/14 00:07:48
|
|
|
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#include <Pegasus/Common/Config.h> | #include <Pegasus/Common/Config.h> |
|
#include <Pegasus/Common/System.h> |
#include <fcntl.h> | #include <fcntl.h> |
#include <unistd.h> | #include <unistd.h> |
|
#include <ctype.h> |
#include <sys/stat.h> | #include <sys/stat.h> |
#include "snmpDeliverTrap_emanate.h" | #include "snmpDeliverTrap_emanate.h" |
| |
|
|
| |
PEGASUS_USING_STD; | PEGASUS_USING_STD; |
| |
void snmpDeliverTrap_emanate::deliverTrap(const String& trapOid, |
static char *sr_filename = __FILE__; |
const String& enterprise, |
|
|
IPCFunctionP IPCfp; /* IPC functions pointer */ |
|
|
|
// This code should be generated by Emanate |
|
// Since we do not have MIB objects defined, |
|
// just defined here to load subagent as library |
|
|
|
// The objects internal to the agent |
|
ObjectInfo OidList[] = |
|
{ |
|
{ { 0, NULL }, |
|
#ifndef LIGHT |
|
NULL, |
|
#endif /* LIGHT */ |
|
0, 0, 0, 0, |
|
NULL, NULL } |
|
}; |
|
|
|
// This code should be generated by Emanate in k_* routine |
|
// Since we do not have MIB objects defined, |
|
// just defined here to pass compile |
|
|
|
// Called by the master agent during initialization */ |
|
int k_initialize() |
|
{ |
|
return 1; |
|
} |
|
|
|
snmpDeliverTrap_emanate::snmpDeliverTrap_emanate() |
|
{ |
|
|
|
} |
|
|
|
snmpDeliverTrap_emanate::~snmpDeliverTrap_emanate() |
|
{ |
|
|
|
} |
|
|
|
void snmpDeliverTrap_emanate::initialize() |
|
{ |
|
#ifndef SR_UDS_IPC |
|
InitIPCArrayTCP(&IPCfp); |
|
#else /* SR_UDS_IPC */ |
|
InitIPCArrayUDS(&IPCfp); |
|
#endif /* SR_UDS_IPC */ |
|
|
|
if(InitSubagent() == -1) |
|
{ |
|
cout << "InitSubagent Failed to initialize" << endl; |
|
exit(1); |
|
} |
|
} |
|
|
|
void snmpDeliverTrap_emanate::deliverTrap( |
|
const String& trapOid, |
|
const String& community, |
const String& destination, | const String& destination, |
const String& trapType, | const String& trapType, |
Array<String>& vbOids, | Array<String>& vbOids, |
Array<String>& vbTypes, | Array<String>& vbTypes, |
Array<String>& vbValues) | Array<String>& vbValues) |
{ | { |
int fifo_fd; |
initialize(); |
struct stat sbuf; |
|
| |
trapHeader myTrapHeader; |
void* newValue; |
trapHeader myTrapData; |
char* entV2Trap; |
|
int vb_link_flag = 0; |
| |
cout << "NITIN: calling deliverTrap()" << endl; |
VarBind *vb = NULL; |
|
VarBind *vb2 = NULL; |
| |
if (stat(_SNMP_FIFO, &sbuf) == -1) |
OID *object = NULL; |
cout << "The fifo is not opened by EMANATE subagent" << endl; |
|
else |
// getting IP address of the host |
|
char* hostname = System::getHostName().allocateCString(); |
|
char **p; |
|
struct hostent *hp; |
|
struct in_addr in; |
|
hp=gethostbyname(hostname); |
|
p = hp->h_addr_list; |
|
(void)memcpy(&in.s_addr, *p, sizeof(in.s_addr)); |
|
char* IP_string = inet_ntoa(in); |
|
|
|
// converting destination into Transport |
|
TransportInfo global_ti; |
|
global_ti.type = SR_IP_TRANSPORT; |
|
// address to which to send trap |
|
global_ti.t_ipAddr = inet_addr(destination.allocateCString()); |
|
// port to which to send trap |
|
global_ti.t_ipPort = htons((unsigned short)GetSNMPTrapPort()); |
|
|
|
// formatting agent(host) address into OctetString format |
|
OctetString* agent_addr; |
|
|
|
SR_INT32 s1, s2, s3, s4; |
|
SR_UINT32 ipaddr; |
|
|
|
// pull out each of the 4 octet values from IP address |
|
sscanf(IP_string,"%d.%d.%d.%d", &s1, &s2, &s3, &s4); |
|
|
|
// Probably should perform some checks on values for |
|
// s1, s2, s3, and s4 here to make sure values are |
|
// between 0 and 255 |
|
|
|
// create an empty 4 length OctetString |
|
agent_addr = MakeOctetString(NULL,4); |
|
|
|
// fill in values for OctetString |
|
agent_addr->octet_ptr[0] = (unsigned char)s1; |
|
agent_addr->octet_ptr[1] = (unsigned char)s2; |
|
agent_addr->octet_ptr[2] = (unsigned char)s3; |
|
agent_addr->octet_ptr[3] = (unsigned char)s4; |
|
|
|
// ATTN : Write code to get enterprise, genTrap and specTrap |
|
// from trapOid. |
|
|
|
// << 03-12-2002 : NU (HP) |
|
|
|
String enterprise; |
|
|
|
OID* enterpriseOid = MakeOIDFromDot(enterprise.allocateCString()); |
|
|
|
// getting trap data for do_trap() |
|
entV2Trap = trapOid.allocateCString(); |
|
|
|
// getting trapOid |
|
OID *sendtrapOid = MakeOIDFromDot(trapOid.allocateCString()); |
|
|
|
for(Uint8 i = 0; i < vbOids.size(); i++) |
{ | { |
if ((fifo_fd = open (_SNMP_FIFO, O_WRONLY|O_NONBLOCK)) == -1) |
if ((object = MakeOIDFromDot(vbOids[i].allocateCString())) == NULL) |
cout << "The fifo is not opened for reading. Subagent is not running" << endl; |
{ |
|
cout << "Invalid OID received: " << vbOids[i] << endl; |
|
return; |
|
} |
|
|
|
if (strcmp(vbTypes[i].allocateCString(),"OctetString") == 0) |
|
{ |
|
newValue = MakeOctetStringFromText(vbValues[i].allocateCString()); |
|
if ((vb = MakeVarBindWithValue(object, |
|
(OID *) NULL, |
|
OCTET_PRIM_TYPE, |
|
newValue)) == NULL) |
|
{ |
|
cout << "Invalid OctetString value: " << vbValues[i] << endl; |
|
return; |
|
} |
|
} |
else | else |
{ | { |
strcpy(myTrapHeader.destination, _CString(destination)); |
newValue = MakeOctetString( |
strcpy(myTrapHeader.snmpType, "SNMPV2"); |
(unsigned char *) vbValues[i].allocateCString(), |
strcpy(myTrapHeader.enterprise, _CString(enterprise)); |
strlen(vbValues[i].allocateCString())); |
strcpy(myTrapHeader.trapOid, _CString(trapOid)); |
|
myTrapHeader.variable_packets = vbOids.size(); |
if ((vb = MakeVarBindWithValue(object, |
|
(OID *) NULL, |
|
INTEGER_TYPE, |
|
newValue)) == NULL) |
|
{ |
|
cout << "Invalid Integer Value: " << vbValues[i] << endl; |
|
return; |
|
} |
|
} |
| |
if (write(fifo_fd, &myTrapHeader, sizeof(myTrapHeader)) <=0) |
if (!vb_link_flag) |
cout << "Error in writing" << endl; |
{ |
|
vb_link_flag = 1; |
|
vb2 = vb; |
|
FreeOID(object); |
|
} |
else | else |
{ | { |
for(int packets = 0; packets<vbOids.size(); packets++) |
vb2->next_var = vb; |
|
vb2 = vb; |
|
FreeOID(object); |
|
} |
|
} |
|
|
|
vb_link_flag = 0; |
|
vb2->next_var = NULL; |
|
vb->next_var = NULL; |
|
|
|
// Now sending the trap |
|
if (trapType == String("SNMPGeneric")) |
{ | { |
trapData myTrapData; |
do_trap(6, 4, vb2, enterpriseOid, entV2Trap); |
strcpy(myTrapData.vbOid, _CString(vbOids[packets])); |
FreeVarBindList(vb); |
strcpy(myTrapData.vbType, _CString(vbTypes[packets])); |
FreeVarBindList(vb2); |
strcpy(myTrapData.vbValue, _CString(vbValues[packets])); |
|
write(fifo_fd, &myTrapData, sizeof(myTrapData)); |
|
} | } |
|
else if (trapType == String("SNMPv1")) |
|
{ |
|
SendNotificationToDestSMIv1Params( |
|
1, // notifyType |
|
6, // genTrap |
|
1, // specTrap |
|
enterpriseOid, // enterprise |
|
agent_addr, // agent_addr |
|
vb2, // vb |
|
NULL, // contextName |
|
1, // retryCount |
|
1, // timeout |
|
MakeOctetStringFromText(community.allocateCString()), // securityName, |
|
SR_SECURITY_LEVEL_NOAUTH, // securityLevel |
|
SR_SECURITY_MODEL_V1, // securityModel |
|
&global_ti, // Transport Info |
|
0); // cfg_chk |
|
|
|
FreeVarBindList(vb); |
|
FreeVarBindList(vb2); |
} | } |
|
else if (trapType == String("SNMPv2")) |
|
{ |
|
SendNotificationToDestSMIv2Params( |
|
1, // notifyType |
|
sendtrapOid, // snmpTrapOID |
|
agent_addr, // agent_addr |
|
vb2, // vb |
|
NULL, // contextName |
|
1, // retryCount |
|
100, // timeout |
|
MakeOctetStringFromText(community.allocateCString()), // securityName or community |
|
SR_SECURITY_LEVEL_NOAUTH, // securityLevel |
|
SR_SECURITY_MODEL_V1, // securityModel |
|
&global_ti, // TransportInfo |
|
0); // cfg_chk |
|
FreeVarBindList(vb); |
|
FreeVarBindList(vb2); |
} | } |
close (fifo_fd); |
else |
|
{ |
|
cout << "Trap type not supported : " << trapType << endl; |
|
exit(2); |
} | } |
} | } |
| |