📄 wsnmp_ut.c
字号:
}
if (IsBadReadPtr (xOID, sizeof(smiOID)) ||
IsBadReadPtr (yOID, sizeof(smiOID)))
{
lError = SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
if (IsBadReadPtr (xOID->ptr, xOID->len * sizeof(UINT)) ||
IsBadReadPtr (yOID->ptr, yOID->len * sizeof(UINT)))
{
lError = SNMPAPI_OID_INVALID;
goto ERROR_OUT;
}
// Test input pointers for readability
if (IsBadWritePtr (result, sizeof(smiINT)))
{
lError = (result == NULL) ? SNMPAPI_NOOP : SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
j = min(xOID->len, yOID->len);
if (maxlen) j = min(j, maxlen);
while (i < j)
{
if (*result = xOID->ptr[i] - yOID->ptr[i]) // deliberate assignment
return (SNMPAPI_SUCCESS); // not equal...got a winner!
i++;
}
if (j == maxlen) // asked for a limit
return (SNMPAPI_SUCCESS); // and...got a draw!
*result = xOID->len - yOID->len; // size matters!
return SNMPAPI_SUCCESS;
//
ERROR_OUT:
return (SaveError (0, lError));
} // end_SnmpOidCompare
SNMPAPI_STATUS SNMPAPI_CALL
SnmpEncodeMsg (IN HSNMP_SESSION hSession,
IN HSNMP_ENTITY hSrc,
IN HSNMP_ENTITY hDst,
IN HSNMP_CONTEXT hCtx,
IN HSNMP_PDU hPdu,
IN OUT smiLPOCTETS msgBufDesc)
{
smiUINT32 version = 0;
DWORD nCtx;
DWORD nPdu;
DWORD nVbl;
smiOCTETS tmpContext;
smiLPBYTE msgAddr = NULL;
smiUINT32 lError = SNMPAPI_SUCCESS;
HSNMP_SESSION lSession = 0;
LPPDUS pPdu;
LPENTITY pEntSrc, pEntDst;
LPCTXT pCtxt;
// Basic error checks
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
// Check for writable output buffer
if (IsBadWritePtr (msgBufDesc, sizeof(smiOCTETS)))
{
lError = (msgBufDesc == NULL) ? SNMPAPI_NOOP : SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
// srcEntity not currently used
if (hSrc)
{
if (!snmpValidTableEntry(&EntsDescr, HandleToUlong(hSrc)-1))
{
lError = SNMPAPI_ENTITY_INVALID;
goto ERROR_OUT;
}
pEntSrc = snmpGetTableEntry(&EntsDescr, HandleToUlong(hSrc)-1);
}
// dstEntity is required for *accurate* msg version info
if (hDst)
{
if (!snmpValidTableEntry(&EntsDescr, HandleToUlong(hDst)-1))
{
lError = SNMPAPI_ENTITY_INVALID;
goto ERROR_OUT;
}
pEntDst = snmpGetTableEntry(&EntsDescr, HandleToUlong(hDst)-1);
version = pEntDst->version-1;
}
nCtx = HandleToUlong(hCtx) - 1;
if (!snmpValidTableEntry(&CntxDescr, nCtx))
{
lError = SNMPAPI_CONTEXT_INVALID;
goto ERROR_OUT;
}
pCtxt = snmpGetTableEntry(&CntxDescr, nCtx);
nPdu = HandleToUlong(hPdu) - 1;
if (!snmpValidTableEntry(&PDUsDescr, nPdu))
{
ERROR_PDU:
lError = SNMPAPI_PDU_INVALID;
goto ERROR_OUT;
}
pPdu = snmpGetTableEntry(&PDUsDescr, nPdu);
// Necessary PDU data checks
nVbl = HandleToUlong(pPdu->VBL);
if (!snmpValidTableEntry(&VBLsDescr, nVbl-1))
goto ERROR_PDU;
// Check out for SNMPv1 Trap PDU type...uses different PDU structure!
// ???
// Check for SNMPv2c PDU types
if (pPdu->type == SNMP_PDU_TRAP ||
pPdu->type == SNMP_PDU_INFORM)
version = 1;
// Now Build it
tmpContext.len = pCtxt->commLen;
tmpContext.ptr = pCtxt->commStr;
if (!(BuildMessage (version, &tmpContext,
pPdu, pPdu->appReqId,
&msgAddr, &msgBufDesc->len)))
goto ERROR_PDU;
// Copy Snmp message to caller's buffer...
// App must free following alloc via SnmpFreeDescriptor()
if (!(msgBufDesc->ptr = (smiLPBYTE)GlobalAlloc (GPTR, msgBufDesc->len)))
lError = SNMPAPI_ALLOC_ERROR;
else // SUCCESS
CopyMemory (msgBufDesc->ptr, msgAddr, msgBufDesc->len);
ERROR_OUT:
// Clean up
if (msgAddr)
GlobalFree (msgAddr);
if (lError == SNMPAPI_SUCCESS)
return (msgBufDesc->len);
else // Failure cases
return (SaveError (lSession, lError));
}
SNMPAPI_STATUS SNMPAPI_CALL
SnmpDecodeMsg (IN HSNMP_SESSION hSession,
OUT LPHSNMP_ENTITY hSrc,
OUT LPHSNMP_ENTITY hDst,
OUT LPHSNMP_CONTEXT hCtx,
OUT LPHSNMP_PDU hPdu,
IN smiLPCOCTETS msgPtr)
{
DWORD nPdu;
smiLPOCTETS community;
smiUINT32 version;
SNMPAPI_STATUS lError = SNMPAPI_SUCCESS;
HSNMP_SESSION lSession = 0;
LPPDUS pPdu;
if (TaskData.hTask == 0)
{
lError = SNMPAPI_NOT_INITIALIZED;
goto ERROR_OUT;
}
if (!snmpValidTableEntry(&SessDescr, HandleToUlong(hSession)-1))
{
lError = SNMPAPI_SESSION_INVALID;
goto ERROR_OUT;
}
// Valid session...save for possible error return
lSession = hSession;
if (IsBadReadPtr(msgPtr, sizeof(smiOCTETS)) ||
IsBadReadPtr(msgPtr->ptr, msgPtr->len))
{
lError = SNMPAPI_ALLOC_ERROR;
goto ERROR_OUT;
}
if (hSrc == NULL && hDst == NULL && hCtx == NULL && hPdu == NULL)
{
lError = SNMPAPI_NOOP;
goto ERROR_OUT;
}
if ((hDst != NULL && IsBadWritePtr(hDst, sizeof(HSNMP_ENTITY))) ||
(hSrc != NULL && IsBadWritePtr(hSrc, sizeof(HSNMP_ENTITY))))
{
lError = SNMPAPI_ENTITY_INVALID;
goto ERROR_OUT;
}
if (hCtx != NULL && IsBadWritePtr(hCtx, sizeof(HSNMP_CONTEXT)))
{
lError = SNMPAPI_CONTEXT_INVALID;
goto ERROR_OUT;
}
if (IsBadWritePtr(hPdu, sizeof(HSNMP_PDU)))
{
lError = SNMPAPI_PDU_INVALID;
goto ERROR_OUT;
}
EnterCriticalSection (&cs_PDU);
lError = snmpAllocTableEntry(&PDUsDescr, &nPdu);
if (lError != SNMPAPI_SUCCESS)
goto ERROR_PRECHECK;
pPdu = snmpGetTableEntry(&PDUsDescr, nPdu);
if (ParseMessage (msgPtr->ptr, msgPtr->len, &version, &community, pPdu))
{ // non-zero = some error code
lError = SNMPAPI_MESSAGE_INVALID;
SnmpFreePdu((HSNMP_PDU)(nPdu+1));
goto ERROR_PRECHECK;
}
if (hDst != NULL) *hDst = 0;
if (hSrc != NULL) *hSrc = 0;
if (hCtx != NULL)
{
smiUINT32 nMode;
EnterCriticalSection (&cs_XMODE);
SnmpGetTranslateMode (&nMode);
SnmpSetTranslateMode (SNMPAPI_UNTRANSLATED_V1);
*hCtx = SnmpStrToContext (hSession, community);
SnmpSetTranslateMode (nMode);
LeaveCriticalSection (&cs_XMODE);
}
FreeOctetString (community);
pPdu->Session = hSession;
if (hPdu != NULL)
*hPdu = (HSNMP_PDU)(nPdu+1);
else
SnmpFreePdu((HSNMP_PDU)(nPdu+1));
ERROR_PRECHECK:
LeaveCriticalSection (&cs_PDU);
if (lError == SNMPAPI_SUCCESS)
{
SaveError(lSession, lError);
return (msgPtr->len);
}
ERROR_OUT:
return (SaveError (lSession, lError));
} // end_SnmpDecodeMsg()
#define NETLEN 4
#define NODELEN 6
char *cHexDigits = "0123456789ABCDEF";
SNMPAPI_STATUS SNMPAPI_CALL
SnmpStrToIpxAddress (LPCSTR str, LPBYTE netnum, LPBYTE nodenum)
{
LPSTR netPtr, nodePtr, pStr;
DWORD i, j;
char tmpStr[24];
BYTE c1, c2;
if (!str || !netnum || !nodenum)
return (SNMPAPI_FAILURE);
lstrcpy (tmpStr, str);
netPtr = strtok (tmpStr, "-:.");
if (netPtr == NULL)
return (SNMPAPI_FAILURE);
if (lstrlen (netPtr) != NETLEN*2)
return (SNMPAPI_FAILURE);
nodePtr = netPtr + (NETLEN*2) + 1;
if (lstrlen (nodePtr) != NODELEN*2)
return (SNMPAPI_FAILURE);
_strupr (netPtr);
for (i = 0, j = 0; j < NETLEN; j++)
{
pStr = strchr (cHexDigits, netPtr[i++]);
if (pStr == NULL)
return (SNMPAPI_FAILURE);
c1 = (BYTE)(pStr - cHexDigits);
pStr = strchr (cHexDigits, netPtr[i++]);
if (pStr == NULL)
return (SNMPAPI_FAILURE);
c2 = (BYTE)(pStr - cHexDigits);
netnum[j] = c2 | c1 << 4;
}
_strupr (nodePtr);
for (i = 0, j = 0; j < NODELEN; j++)
{
pStr = strchr (cHexDigits, nodePtr[i++]);
if (pStr == NULL)
return (SNMPAPI_FAILURE);
c1 = (BYTE)(pStr - cHexDigits);
pStr = strchr (cHexDigits, nodePtr[i++]);
if (pStr == NULL)
return (SNMPAPI_FAILURE);
c2 = (BYTE)(pStr - cHexDigits);
nodenum[j] = c2 | c1 << 4;
}
return (SNMPAPI_SUCCESS);
}
SNMPAPI_STATUS SNMPAPI_CALL
SnmpIpxAddressToStr (LPBYTE netnum, LPBYTE nodenum, LPSTR str)
{
DWORD i, j;
for (i = 0, j = 0; i < NETLEN; i++)
{
str[j++] = cHexDigits[(netnum[i] & 0xF0) >> 4];
str[j++] = cHexDigits[netnum[i] & 0x0F];
}
str[j++] = ':';
for (i = 0; i < NODELEN; i++)
{
str[j++] = cHexDigits[(nodenum[i] & 0xF0) >> 4];
str[j++] = cHexDigits[nodenum[i] & 0x0F];
}
str[j] = '\0';
return (SNMPAPI_SUCCESS);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -