📄 wsnmp_ec.c
字号:
// wsnmp_ec.c
//
// WinSNMP Entity/Context Functions and helpers
// Copyright 1995-1997 ACE*COMM Corp
// Rleased to Microsoft under Contract
// Beta 1 version, 970228
// Bob Natale (bnatale@acecomm.com)
//
// 980424 - BobN
// - Mods to SnmpStrToEntity() to support corresponding
// - mods to SnmpStrToIpxAddress() to permit '.' char as
// - netnum/nodenum separator
// 970310 - Typographical changes
//
#include "winsnmp.inc"
SNMPAPI_STATUS SNMPAPI_CALL SnmpStrToIpxAddress (LPCSTR, LPBYTE, LPBYTE);
// SnmpStrToEntity
HSNMP_ENTITY SNMPAPI_CALL
SnmpStrToEntity (IN HSNMP_SESSION hSession,
IN LPCSTR entityString)
{
DWORD strLen;
LPCSTR tstStr;
LPSTR profilePtr;
LPSTR comma = ",";
DWORD nEntity;
SNMPAPI_STATUS lError = SNMPAPI_SUCCESS;
HSNMP_SESSION lSession = 0;
char profileBuf[256];
LPENTITY pEntity;
if (TaskData.hTask == 0)
{
lError = SNMPAPI_NOT_INITIALIZED;
goto ERROR_OUT;
}
if (!snmpValidTableEntry(&SessDescr, HandleToUlong(hSession)-1))
{
lError = SNMPAPI_SESSION_INVALID;
goto ERROR_OUT;
}
// We have a valid session at this point...
lSession = hSession; // save it for possible error return
if (!entityString || (strLen = lstrlen(entityString)) == 0)
{
lError = SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
// Must go through ERROR_PRECHECK label after next statement...
EnterCriticalSection (&cs_ENTITY);
// Search for Entity table entry to use
lError = snmpAllocTableEntry(&EntsDescr, &nEntity);
if (lError != SNMPAPI_SUCCESS)
goto ERROR_PRECHECK;
pEntity = snmpGetTableEntry(&EntsDescr, nEntity);
pEntity->version = 0;
pEntity->nPolicyTimeout = DEFTIMEOUT;
pEntity->nPolicyRetry = DEFRETRY;
if (strLen > MAX_FRIEND_NAME_LEN)
strLen = MAX_FRIEND_NAME_LEN;
switch (TaskData.nTranslateMode)
{
case SNMPAPI_TRANSLATED:
// the entity is picked up from NP_WSNMP.INI, from [Entities] section:
// [Entities]
// EntityFriendlyName = ver#, ipaddr, timeout#, retries#, port#[,]
// ------
// Get the whole buffer
if (!GetPrivateProfileString ("Entities", entityString, "",
profileBuf, sizeof(profileBuf), "NP_WSNMP.INI"))
{
snmpFreeTableEntry(&EntsDescr, nEntity);
lError = SNMPAPI_ENTITY_UNKNOWN;
goto ERROR_PRECHECK;
}
// pick up the ver# first (mandatory)
profilePtr = strtok (profileBuf, comma);
// if no token, is like we have a key with no value
// bail out with SNMPAPI_NOOP
if (profilePtr == NULL)
{
snmpFreeTableEntry(&EntsDescr, nEntity);
lError = SNMPAPI_NOOP;
goto ERROR_PRECHECK;
}
pEntity->version = atoi (profilePtr);
// pick up the dotted ip address (mandatory)
tstStr = strtok (NULL, comma); // Save real address string
// if no address is specified, we don't have the vital info, so there's nothing to do
// bail with SNMPAPI_NOOP
if (tstStr == NULL)
{
snmpFreeTableEntry(&EntsDescr, nEntity);
lError = SNMPAPI_NOOP;
goto ERROR_PRECHECK;
}
// pick up the timeout# (optional)
if (profilePtr = strtok (NULL, comma))
{
// The local database entry uses milliseconds for the timeout interval
pEntity->nPolicyTimeout = atol (profilePtr);
// Adjust for centiseconds, as used by the WinSNMP API
pEntity->nPolicyTimeout /= 10;
// pick up the retry# (optional)
if (profilePtr = strtok (NULL, comma))
{
pEntity->nPolicyRetry = atol (profilePtr);
// pick up the port# (optional)
if (profilePtr = strtok (NULL, comma))
pEntity->addr.inet.sin_port = htons ((short)atoi (profilePtr));
}
}
break;
// "version" was set to 0 above
// if _V2, it will be incremented twice
// if _V1, it will be incremented only once
case SNMPAPI_UNTRANSLATED_V2:
pEntity->version++;
case SNMPAPI_UNTRANSLATED_V1:
pEntity->version++;
tstStr = entityString; // Save real address string
break;
default:
snmpFreeTableEntry(&EntsDescr, nEntity);
lError = SNMPAPI_MODE_INVALID;
goto ERROR_PRECHECK;
} // end_switch
CopyMemory (pEntity->name, entityString, strLen);
if (strncmp(tstStr, "255.255.255.255", 15) && inet_addr (tstStr) == INADDR_NONE)
{ // Not AF_INET, try AF_IPX
if (SnmpStrToIpxAddress (tstStr,
pEntity->addr.ipx.sa_netnum,
pEntity->addr.ipx.sa_nodenum) == SNMPAPI_FAILURE)
{
LeaveCriticalSection (&cs_ENTITY);
return ((HSNMP_ENTITY)SaveError (hSession, SNMPAPI_ENTITY_UNKNOWN));
}
pEntity->addr.ipx.sa_family = AF_IPX;
if (pEntity->addr.ipx.sa_socket == 0)
pEntity->addr.ipx.sa_socket = htons (IPX_SNMP_PORT);
}
else
{ // AF_INET
pEntity->addr.inet.sin_family = AF_INET;
if (pEntity->addr.inet.sin_port == 0)
pEntity->addr.inet.sin_port = htons (IP_SNMP_PORT);
pEntity->addr.inet.sin_addr.s_addr = inet_addr (tstStr);
}
// Record the creating session
pEntity->Session = hSession;
// Initialize refCount for SnmpFreeEntity garbage collection
pEntity->refCount = 1;
ERROR_PRECHECK:
LeaveCriticalSection (&cs_ENTITY);
ERROR_OUT:
if (lError == SNMPAPI_SUCCESS)
return ((HSNMP_ENTITY)(nEntity+1));
else // Failure cases
return ((HSNMP_ENTITY)SaveError (lSession, lError));
} //end_SnmpStrToEntity
// SnmpEntityToStr
SNMPAPI_STATUS SNMPAPI_CALL
SnmpEntityToStr (IN HSNMP_ENTITY hEntity,
IN smiUINT32 size,
OUT LPSTR string)
{
DWORD nEntity = HandleToUlong(hEntity) - 1;
SNMPAPI_STATUS lError = SNMPAPI_SUCCESS;
HSNMP_SESSION lSession = 0;
LPSTR str;
smiUINT32 len;
char tmpStr[24];
LPENTITY pEntity;
if (TaskData.hTask == 0)
{
lError = SNMPAPI_NOT_INITIALIZED;
goto ERROR_OUT;
}
if (!snmpValidTableEntry(&EntsDescr, nEntity))
{
lError = SNMPAPI_ENTITY_UNKNOWN;
goto ERROR_OUT;
}
pEntity = snmpGetTableEntry(&EntsDescr, nEntity);
lSession = pEntity->Session;
if (size == 0)
{
lError = SNMPAPI_SIZE_INVALID;
goto ERROR_OUT;
}
if (IsBadWritePtr(string, size))
{
lError = SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
len = 0;
if (TaskData.nTranslateMode == SNMPAPI_TRANSLATED)
{
str = pEntity->name;
len = lstrlen (str);
}
else
{
if (pEntity->addr.inet.sin_family == AF_INET)
{
str = inet_ntoa (pEntity->addr.inet.sin_addr);
len = lstrlen (str);
}
else if (pEntity->addr.ipx.sa_family == AF_IPX)
{
SnmpIpxAddressToStr (pEntity->addr.ipx.sa_netnum,
pEntity->addr.ipx.sa_nodenum,
tmpStr);
str = tmpStr;
len = lstrlen (str);
}
else
{
lError = SNMPAPI_ENTITY_INVALID;
goto ERROR_OUT;
}
}
if (len >= size)
{
CopyMemory (string, str, size);
string[size-1] = '\0';
lError = SNMPAPI_OUTPUT_TRUNCATED;
goto ERROR_OUT;
}
else
{
lstrcpy (string, str);
return (len+1);
}
// Failure cases
ERROR_OUT:
return (SaveError (lSession, lError));
} // End_SnmpEntityToStr
// SnmpFreeEntity
SNMPAPI_STATUS SNMPAPI_CALL
SnmpFreeEntity (IN HSNMP_ENTITY hEntity)
{
DWORD nEntity;
SNMPAPI_STATUS lError = SNMPAPI_SUCCESS;
LPENTITY pEntity;
if (TaskData.hTask == 0)
{
lError = SNMPAPI_NOT_INITIALIZED;
goto ERROR_OUT;
}
nEntity = HandleToUlong(hEntity) - 1;
if (!snmpValidTableEntry(&EntsDescr, nEntity))
{
lError = SNMPAPI_ENTITY_INVALID;
goto ERROR_OUT;
}
pEntity = snmpGetTableEntry(&EntsDescr, nEntity);
EnterCriticalSection (&cs_ENTITY);
// Decrement refCount (unless already 0 [error])
if (pEntity->refCount)
pEntity->refCount--;
// Now actually free it...
if (pEntity->Agent == 0 && // but not if it's an Agent
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -