📄 wsnmp_ut.c
字号:
// wsnmp_ut.c
//
// WinSNMP Utility 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 SnmpStrToIpxAddress() to permit '.' char
// - as netnum/nodenum separator
// 970310 - Removed extraneous functions
//
#include "winsnmp.inc"
SNMPAPI_STATUS SNMPAPI_CALL
SnmpGetLastError (IN HSNMP_SESSION hSession)
{
DWORD nSes;
if (TaskData.hTask == 0)
return (SNMPAPI_NOT_INITIALIZED);
nSes = HandleToUlong(hSession) - 1;
if (snmpValidTableEntry(&SessDescr, nSes))
{
LPSESSION pSession = snmpGetTableEntry(&SessDescr, nSes);
return pSession->nLastError;
}
else
return (TaskData.nLastError);
} // end_SnmpGetLastError
SNMPAPI_STATUS SNMPAPI_CALL
SnmpStrToOid (IN LPCSTR string,
OUT smiLPOID dstOID)
{
smiUINT32 i;
smiUINT32 compIdx;
SNMPAPI_STATUS lError;
CHAR c;
LPSTR pSep;
// Must be initialized
if (TaskData.hTask == 0)
{
lError = SNMPAPI_NOT_INITIALIZED;
goto ERROR_OUT;
}
// use __try, __except to figure out if 'string' is a
// valid pointer. We cannot use IsBadReadPtr() here, as far as
// we have no idea for how many octets we should look.
__try
{
smiUINT32 sLen;
sLen = strlen(string);
if (sLen == 0 || sLen >= MAXOBJIDSTRSIZE)
{
lError = SNMPAPI_OID_INVALID;
goto ERROR_OUT;
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
lError = SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
// see if the dstOID pointer provided by the caller points to
// a valid memory range. If null is provided, there is nothing
// the API was requested to do!
if (IsBadWritePtr (dstOID, sizeof(smiOID)))
{
lError = (dstOID == NULL) ? SNMPAPI_NOOP : SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
// Ignore initial '.' in string (UNIX-ism)
if (string[0] == '.')
string++;
// figure out how many components this OID has
// count the number of '.' in the string. The OID should
// contain this count + 1 components
dstOID->len = 0;
pSep = (LPSTR)string;
while((pSep = strchr(pSep, '.')) != NULL)
{
pSep++;
dstOID->len++;
}
dstOID->len++;
// don't allow less than 2 components
if (dstOID->len < 2)
{
lError = SNMPAPI_OID_INVALID;
goto ERROR_OUT;
}
// allocate memory for holding the numeric OID components
// this should be released by the caller, through 'SnmpFreeDescriptor()'
dstOID->ptr = (smiLPUINT32)GlobalAlloc(GPTR, dstOID->len * sizeof(smiUINT32));
if (dstOID->ptr == NULL)
{
lError = SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
SetLastError(ERROR_SUCCESS);
compIdx = 0;
// when entering the loop, 'string' doesn't have a heading '.'
while (*string != '\0')
{
dstOID->ptr[compIdx++] = strtoul(string, &pSep, 10);
// if one of the components was overflowing, release the memory and bail out.
if (GetLastError() != ERROR_SUCCESS)
{
lError = SNMPAPI_OID_INVALID;
GlobalFree(dstOID->ptr);
dstOID->ptr = NULL;
goto ERROR_OUT;
}
// if strtoul did not make any progress on the string (two successive dots)
// or it was blocked on something else than a separator or null-termination, then
// there was an error. The OID is still valid but truncated. API return failure
if (pSep == string ||
(*pSep != '.' && *pSep != '\0'))
{
lError = SNMPAPI_OUTPUT_TRUNCATED; // invalid char in sequence
dstOID->len = compIdx; // update the len, for accuracy
goto ERROR_OUT; // the OID is valid, but truncated. The caller should
// release the memory with SnmpFreeDescriptor()
}
// pSep can point only to '.' or '\0'
if (*pSep == '.')
pSep++;
// restart with string from this point
string = pSep;
}
if (dstOID->len < 2)
{
GlobalFree(dstOID->ptr);
dstOID->ptr = NULL;
lError = SNMPAPI_OID_INVALID;
goto ERROR_OUT;
}
return dstOID->len;
ERROR_OUT:
return (SaveError (0, lError));
} // end_SnmpStrToOid()
SNMPAPI_STATUS SNMPAPI_CALL
SnmpOidToStr (IN smiLPCOID srcOID,
IN smiUINT32 strLen,
OUT LPSTR strPtr)
{
SNMPAPI_STATUS lError;
smiUINT32 retLen = 0; // used for successful return
smiUINT32 oidIdx = 0; // max subids is 128
smiUINT32 tmpLen; // used for size of decoded string (with '.')
LPSTR tmpPtr = strPtr; // used for advancing strPtr
char tmpBuf[16]; // room for 1 32-bit decode and '.'
if (TaskData.hTask == 0)
{
lError = SNMPAPI_NOT_INITIALIZED;
goto ERROR_OUT;
}
if (!strLen)
{
lError = SNMPAPI_SIZE_INVALID;
goto ERROR_OUT;
}
if (IsBadReadPtr(srcOID, sizeof(smiOID)) ||
IsBadWritePtr(strPtr, strLen))
{
lError = (strPtr == NULL) ? SNMPAPI_NOOP : SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
if (srcOID->len == 0 || srcOID->len > 128 ||
IsBadReadPtr (srcOID->ptr, srcOID->len * sizeof(smiUINT32)))
{
lError = SNMPAPI_OID_INVALID;
goto ERROR_OUT;
}
while (oidIdx < srcOID->len)
{
_ultoa (srcOID->ptr[oidIdx++], tmpBuf, 10);
lstrcat (tmpBuf, ".");
tmpLen = lstrlen (tmpBuf);
if (strLen < (tmpLen + 1))
{
tmpBuf[strLen] = '\0';
lstrcpy (tmpPtr, tmpBuf);
lError = SNMPAPI_OUTPUT_TRUNCATED;
goto ERROR_OUT;
}
lstrcpy (tmpPtr, tmpBuf);
strLen -= tmpLen;
tmpPtr += tmpLen;
retLen += tmpLen;
} // end_while
*(--tmpPtr) = '\0';
return (retLen);
//
ERROR_OUT:
return (SaveError (0, lError));
} // end_SnmpOidToStr
SNMPAPI_STATUS SNMPAPI_CALL
SnmpOidCopy (IN smiLPCOID srcOID,
OUT smiLPOID dstOID)
{
SNMPAPI_STATUS lError;
if (TaskData.hTask == 0)
{
lError = SNMPAPI_NOT_INITIALIZED;
goto ERROR_OUT;
}
if (IsBadReadPtr (srcOID, sizeof(smiOID)) ||
IsBadReadPtr (srcOID->ptr, srcOID->len) ||
IsBadWritePtr (dstOID, sizeof(smiOID)))
{
lError = (dstOID == NULL) ? SNMPAPI_NOOP : SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
// Check input OID size
if ((srcOID->len == 0) ||(srcOID->len > MAXOBJIDSIZE))
{
lError = SNMPAPI_OID_INVALID;
goto ERROR_OUT;
}
// Using dstOID-> temporarily for byte count
dstOID->len = srcOID->len * sizeof(smiUINT32);
// App must free following alloc via SnmpFreeDescriptor()
if (!(dstOID->ptr = (smiLPUINT32)GlobalAlloc (GPTR, dstOID->len)))
{
lError = SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
CopyMemory (dstOID->ptr, srcOID->ptr, dstOID->len);
// Now make dstOID->len mean the right thing
dstOID->len = srcOID->len;
return (dstOID->len);
//
ERROR_OUT:
return (SaveError (0, lError));
} // end_SnmpOidCopy()
SNMPAPI_STATUS SNMPAPI_CALL
SnmpFreeDescriptor (IN smiUINT32 syntax,
IN smiLPOPAQUE ptr)
{
SNMPAPI_STATUS lError;
if (TaskData.hTask == 0)
{
lError = SNMPAPI_NOT_INITIALIZED;
goto ERROR_OUT;
}
if (!syntax || !ptr || !ptr->ptr)
{
lError = SNMPAPI_OPERATION_INVALID;
goto ERROR_OUT;
}
switch (syntax)
{
case SNMP_SYNTAX_OCTETS:
case SNMP_SYNTAX_IPADDR:
case SNMP_SYNTAX_OPAQUE:
case SNMP_SYNTAX_OID:
if (GlobalFree (ptr->ptr)) // returns not-NULL on error
{
lError = SNMPAPI_OTHER_ERROR;
goto ERROR_OUT;
}
ptr->ptr = NULL;
ptr->len = 0;
break;
default:
lError = SNMPAPI_SYNTAX_INVALID;
goto ERROR_OUT;
} // end_switch
return (SNMPAPI_SUCCESS);
//
ERROR_OUT:
return (SaveError (0, lError));
} // end_SnmpFreeDescriptor
// SnmpOidCompare
//
// Re-worked by 3/17/95 BobN
SNMPAPI_STATUS SNMPAPI_CALL
SnmpOidCompare (IN smiLPCOID xOID,
IN smiLPCOID yOID,
IN smiUINT32 maxlen,
OUT smiLPINT result)
{
smiUINT32 i = 0;
smiUINT32 j = 0;
SNMPAPI_STATUS lError;
if (TaskData.hTask == 0)
{
lError = SNMPAPI_NOT_INITIALIZED;
goto ERROR_OUT;
}
if (maxlen > MAXOBJIDSIZE)
{
lError = SNMPAPI_SIZE_INVALID;
goto ERROR_OUT;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -