Example #1
0
DWORD
DNSUnmarshallRRHeader(
    HANDLE         hRecvBuffer,
    PDNS_RR_HEADER pRRHeader
    )
{
    DWORD dwError = 0;
    DWORD dwRead = 0;
    WORD wnType = 0;
    WORD wnClass = 0;
    WORD wnRDataSize = 0;
    DWORD dwnTTL = 0;

    dwError = DNSUnmarshallDomainName(
                hRecvBuffer,
                &pRRHeader->pDomainName);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnType,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    pRRHeader->wType = ntohs(wnType);
    
    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnClass,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    pRRHeader->wClass = ntohs(wnClass);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&dwnTTL,
                sizeof(DWORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    pRRHeader->dwTTL = ntohl(dwnTTL);
    
    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnRDataSize,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    pRRHeader->wRDataSize = htons(wnRDataSize);

error:

    return dwError;
}
Example #2
0
DWORD
DNSUnmarshallRData(
    HANDLE hRecvBuffer,
    DWORD  dwSize,
    PBYTE* ppRData,
    PDWORD pdwRead
    )
{
    DWORD dwError = 0;
    PBYTE pMemory = NULL;

    dwError = DNSAllocateMemory(
                dwSize,
                (PVOID *)&pMemory);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)pMemory,
                dwSize,
                pdwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    *ppRData = pMemory;

cleanup:

    return(dwError);

error:

    if (pMemory) {
        DNSFreeMemory(pMemory);
    }

    *ppRData = NULL;
    
    goto cleanup;
}
Example #3
0
DWORD
DNSStdReceiveStdResponse(
    HANDLE         hDNSHandle,
    PDNS_RESPONSE* ppDNSResponse
    )
{
    PDNS_RESPONSE pDNSResponse = NULL;
    DWORD dwError = 0;
    WORD wnIdentification,wIdentification = 0;
    WORD wnParameter, wParameter = 0;
    WORD wnQuestions, wQuestions = 0;
    WORD wnAnswers, wAnswers = 0;
    WORD wnAdditionals, wAdditionals = 0;
    WORD wnAuthoritys, wAuthoritys = 0;
    DWORD dwRead = 0;
    PDNS_RR_RECORD * ppDNSAnswerRecords = NULL;
    PDNS_RR_RECORD *  ppDNSAdditionalRecords = NULL;
    PDNS_RR_RECORD *  ppDNSAuthorityRecords = NULL;
    PDNS_QUESTION_RECORD * ppDNSQuestionRecords = NULL;
    HANDLE hRecvBuffer = (HANDLE)NULL;

    dwError = DNSCreateReceiveBuffer(&hRecvBuffer);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    dwError = DNSReceiveBufferContext(
                hDNSHandle,
                hRecvBuffer,
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);

    dwError = DNSDumpRecvBufferContext(hRecvBuffer);
    BAIL_ON_LWDNS_ERROR(dwError);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnIdentification,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    wIdentification = ntohs(wnIdentification);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnParameter,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    wParameter = ntohs(wnParameter);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnQuestions,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    wQuestions = ntohs(wnQuestions);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnAnswers,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    wAnswers = ntohs(wnAnswers);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnAuthoritys,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    wAuthoritys = ntohs(wnAuthoritys);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnAdditionals,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    wAdditionals = ntohs(wnAdditionals);

    if (wQuestions)
    {
        dwError = DNSStdUnmarshallQuestionSection(
                    hRecvBuffer, 
                    wQuestions, 
                    &ppDNSQuestionRecords);
        BAIL_ON_LWDNS_ERROR(dwError);
    }

    if (wAnswers)
    {
        dwError = DNSStdUnmarshallAnswerSection(
                    hRecvBuffer, 
                    wAnswers, 
                    &ppDNSAnswerRecords);
        BAIL_ON_LWDNS_ERROR(dwError);
    }

    if (wAuthoritys)
    {
        dwError = DNSStdUnmarshallAuthoritySection(
                    hRecvBuffer, 
                    wAuthoritys, 
                    &ppDNSAuthorityRecords);
        BAIL_ON_LWDNS_ERROR(dwError);
    }

    if (wAdditionals)
    {
        dwError = DNSStdUnmarshallAdditionalSection(
                    hRecvBuffer, 
                    wAdditionals, 
                    &ppDNSAdditionalRecords);
        BAIL_ON_LWDNS_ERROR(dwError);
    }

    dwError = DNSStdAllocateResponse(&pDNSResponse);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    pDNSResponse->wIdentification = wIdentification;
    pDNSResponse->wParameter = wParameter;
    pDNSResponse->wQuestions = wQuestions;
    pDNSResponse->wAnswers = wAnswers;
    pDNSResponse->wAuthoritys = wAuthoritys;
    pDNSResponse->wAdditionals = wAdditionals;

    pDNSResponse->ppQuestionRRSet = ppDNSQuestionRecords;
    pDNSResponse->ppAnswerRRSet = ppDNSAnswerRecords;
    pDNSResponse->ppAuthorityRRSet = ppDNSAuthorityRecords;
    pDNSResponse->ppAdditionalRRSet = ppDNSAdditionalRecords;
    
    *ppDNSResponse = pDNSResponse;
    
cleanup:

    if (hRecvBuffer != (HANDLE)NULL)
    {
        DNSFreeReceiveBufferContext(hRecvBuffer);
    }

    return dwError;

error:

    if (ppDNSAnswerRecords)
    {
        DNSFreeRecordList(
                ppDNSAnswerRecords,
                wAnswers);
    }
    
    if (ppDNSAdditionalRecords)
    {
        DNSFreeRecordList(
                ppDNSAdditionalRecords,
                wAdditionals);
    }
    
    if (ppDNSAuthorityRecords)
    {
        DNSFreeRecordList(
                ppDNSAuthorityRecords,
                wAuthoritys);
    }
    
    if (ppDNSQuestionRecords)
    {
        DNSFreeQuestionRecordList(
                ppDNSQuestionRecords,
                wQuestions);
    }
    
    *ppDNSResponse = NULL;

    goto cleanup;
}
Example #4
0
DWORD
DNSUnmarshallDomainName(
    HANDLE hRecvBuffer, 
    PDNS_DOMAIN_NAME * ppDomainName
    )
{
    DWORD dwError = 0;
    PDNS_DOMAIN_LABEL pLabel = NULL;
    PDNS_DOMAIN_NAME pDomainName = NULL;
    BYTE uLen = 0;
    DWORD dwRead = 0;
    BYTE uLen1, uLen2 = 0;
    WORD wOffset = 0;
    BOOLEAN bDone = FALSE;
    
    dwError = DNSAllocateMemory(
                sizeof(DNS_DOMAIN_NAME),
                (PVOID *)&pDomainName);
    BAIL_ON_LWDNS_ERROR(dwError);

    do
    {
        dwError = DNSUnmarshallBuffer(
                    hRecvBuffer,
                    &uLen1,
                    sizeof(char),
                    &dwRead);
        BAIL_ON_LWDNS_ERROR(dwError);

        switch (uLen1 & 0xC0)
        {
            case 0xC0: /* pointer to string */

                dwError = DNSUnmarshallBuffer(
                            hRecvBuffer,
                            &uLen2,
                            sizeof(char),
                            &dwRead);
                BAIL_ON_LWDNS_ERROR(dwError);

                wOffset = ntohs((((WORD)(uLen1) << 8)|uLen2) & 0x3FFF);

                dwError = DNSUnmarshallDomainNameAtOffset(
                            hRecvBuffer,
                            wOffset,
                            &pLabel);
                BAIL_ON_LWDNS_ERROR(dwError);

                dwError = DNSAppendLabel(
                                pDomainName->pLabelList,
                                pLabel,
                                &pDomainName->pLabelList);
                BAIL_ON_LWDNS_ERROR(dwError);
                pLabel = NULL;

                break;

            case 0x00: /* string component */

                {
                    CHAR szLabel[65];

                    dwError = DNSReceiveBufferMoveBackIndex(hRecvBuffer, 1);
                    BAIL_ON_LWDNS_ERROR(dwError);

                    dwError = DNSUnmarshallBuffer(
                                hRecvBuffer,
                                &uLen,
                                sizeof(char),
                                &dwRead);
                    BAIL_ON_LWDNS_ERROR(dwError);

                    if (uLen == 0)
                    {
                        bDone = TRUE;
                        break;
                    }

                    memset(szLabel, 0, sizeof(szLabel));

                    dwError = DNSUnmarshallBuffer(
                                hRecvBuffer,
                                (PBYTE)szLabel,
                                uLen,
                                &dwRead);
                    BAIL_ON_LWDNS_ERROR(dwError);

                    dwError = DNSAllocateMemory(
                                sizeof(DNS_DOMAIN_LABEL),
                                (PVOID *)&pLabel);
                    BAIL_ON_LWDNS_ERROR(dwError);

                    dwError = DNSAllocateString(szLabel, &pLabel->pszLabel);
                    BAIL_ON_LWDNS_ERROR(dwError);

                    dwError = DNSAppendLabel(
                                pDomainName->pLabelList,
                                pLabel,
                                &pDomainName->pLabelList);
                    BAIL_ON_LWDNS_ERROR(dwError);

                    pLabel = NULL;
                }

                break;

            default:   /* unexpected */

                dwError = LWDNS_ERROR_BAD_RESPONSE;
                BAIL_ON_LWDNS_ERROR(dwError);

                break;
        }
    } while (!bDone);

    *ppDomainName = pDomainName;
    
cleanup:

    return(dwError);
    
error:

    if (pDomainName)
    {
        DNSFreeDomainName(pDomainName);
    }
    
    if (pLabel)
    {
        DNSFreeLabel(pLabel);
    }
    
    *ppDomainName = NULL;
    
    goto cleanup;
}
Example #5
0
static
DWORD
DNSStdUnmarshallQuestionSection(    
    HANDLE hReceiveBuffer,
    WORD wQuestions,
    PDNS_QUESTION_RECORD ** pppDNSQuestionRecords
    )
{
    DWORD dwError = 0;
    DWORD i = 0;
    PDNS_QUESTION_RECORD pDNSQuestionRecord = NULL;
    PDNS_QUESTION_RECORD * ppDNSQuestionRecords = NULL;

    dwError = DNSAllocateMemory(
                    wQuestions * sizeof(PDNS_QUESTION_RECORD), 
                    (PVOID *)&ppDNSQuestionRecords);
    BAIL_ON_LWDNS_ERROR(dwError);

    for (i = 0; i < wQuestions; i++)
    {   
        DWORD dwRead = 0;
        WORD  wnQueryClass = 0;
        WORD  wnQueryType = 0;
        
        dwError = DNSAllocateMemory(
                    sizeof(DNS_QUESTION_RECORD),
                    (PVOID *)&pDNSQuestionRecord);
        BAIL_ON_LWDNS_ERROR(dwError);

        dwError = DNSUnmarshallDomainName(
                    hReceiveBuffer,
                    &pDNSQuestionRecord->pDomainName);
        BAIL_ON_LWDNS_ERROR(dwError);

        dwError = DNSUnmarshallBuffer(
                    hReceiveBuffer,
                    (PBYTE)&wnQueryType,
                    (DWORD)sizeof(WORD),
                    &dwRead);
        BAIL_ON_LWDNS_ERROR(dwError);
        pDNSQuestionRecord->wQueryType = ntohs(wnQueryType);
        
        dwError = DNSUnmarshallBuffer(
                    hReceiveBuffer,
                    (PBYTE)&wnQueryClass,
                    (DWORD)sizeof(WORD),
                    &dwRead);
        BAIL_ON_LWDNS_ERROR(dwError);
        
        pDNSQuestionRecord->wQueryClass = ntohs(wnQueryClass);

        *(ppDNSQuestionRecords + i) = pDNSQuestionRecord;
        pDNSQuestionRecord = NULL;
    }    

    *pppDNSQuestionRecords = ppDNSQuestionRecords;
    
cleanup:

    return dwError;

error:

    if (ppDNSQuestionRecords)
    {
        DNSFreeQuestionRecordList(
                ppDNSQuestionRecords,
                wQuestions);
    }
    
    if (pDNSQuestionRecord)
    {
        DNSFreeQuestionRecord(pDNSQuestionRecord);
    }

    *pppDNSQuestionRecords = NULL;

    goto cleanup; 
}
Example #6
0
DWORD
DNSUpdateReceiveUpdateResponse(
    HANDLE hDNSHandle,
    PDNS_UPDATE_RESPONSE * ppDNSResponse
    )
{
    PDNS_UPDATE_RESPONSE pDNSResponse = NULL;
    DWORD dwError = 0;
    WORD wnIdentification,wIdentification = 0;
    WORD wnParameter, wParameter = 0;
    WORD wnZones, wZones = 0;
    WORD wnPRs, wPRs = 0;
    WORD wnAdditionals, wAdditionals = 0;
    WORD wnUpdates, wUpdates = 0;
    DWORD dwRead = 0;
    PDNS_RR_RECORD * ppDNSPRRecords = NULL;
    PDNS_RR_RECORD *  ppDNSAdditionalRecords = NULL;
    PDNS_RR_RECORD *  ppDNSUpdateRecords = NULL;
    PDNS_ZONE_RECORD * ppDNSZoneRecords = NULL;
    HANDLE hRecvBuffer = (HANDLE)NULL;

    dwError = DNSCreateReceiveBuffer(&hRecvBuffer);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    dwError = DNSReceiveBufferContext(
                hDNSHandle, 
                hRecvBuffer,
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);

    dwError = DNSDumpRecvBufferContext(hRecvBuffer);
    BAIL_ON_LWDNS_ERROR(dwError);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnIdentification,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    wIdentification = ntohs(wnIdentification);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnParameter,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    wParameter = ntohs(wnParameter);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnZones,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    wZones = ntohs(wnZones);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnPRs,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    wPRs = ntohs(wnPRs);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnUpdates,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    wUpdates = ntohs(wnUpdates);

    dwError = DNSUnmarshallBuffer(
                hRecvBuffer,
                (PBYTE)&wnAdditionals,
                sizeof(WORD),
                &dwRead);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    wAdditionals = ntohs(wnAdditionals);

    if (wZones)
    {
        dwError = DNSUpdateUnmarshallZoneSection(
                    hRecvBuffer,
                    wZones,
                    &ppDNSZoneRecords);
        BAIL_ON_LWDNS_ERROR(dwError);
    }

    if (wPRs)
    {
        dwError = DNSUpdateUnmarshallPRSection(
                    hRecvBuffer,
                    wPRs,
                    &ppDNSPRRecords);
        BAIL_ON_LWDNS_ERROR(dwError);
    }

    if (wUpdates)
    {
        dwError = DNSUpdateUnmarshallUpdateSection(
                    hRecvBuffer,
                    wUpdates,
                    &ppDNSUpdateRecords);
        BAIL_ON_LWDNS_ERROR(dwError);
    }

    if (wAdditionals)
    {
        dwError = DNSUpdateUnmarshallAdditionalSection(
                    hRecvBuffer,
                    wAdditionals,
                    &ppDNSAdditionalRecords);
        BAIL_ON_LWDNS_ERROR(dwError);
    }

    dwError = DNSUpdateAllocateResponse(&pDNSResponse);
    BAIL_ON_LWDNS_ERROR(dwError);
    
    pDNSResponse->wIdentification = wIdentification;
    pDNSResponse->wParameter = wParameter;
    pDNSResponse->wZones = wZones;
    pDNSResponse->wPRs = wPRs;
    pDNSResponse->wUpdates = wUpdates;
    pDNSResponse->wAdditionals = wAdditionals;

    pDNSResponse->ppZoneRRSet = ppDNSZoneRecords;
    pDNSResponse->ppPRRRSet = ppDNSPRRecords;
    pDNSResponse->ppUpdateRRSet = ppDNSUpdateRecords;
    pDNSResponse->ppAdditionalRRSet = ppDNSAdditionalRecords;
    
    *ppDNSResponse = pDNSResponse;

cleanup:

    if (hRecvBuffer != (HANDLE)NULL)
    {
        DNSFreeReceiveBufferContext(hRecvBuffer);
    }

    return dwError;
    
error:

    if (pDNSResponse)
    {
        DNSUpdateFreeResponse(pDNSResponse);
    }
    
    if (ppDNSPRRecords)
    {
        DNSFreeRecordList(
                ppDNSPRRecords,
                wPRs);
    }
    
    if (ppDNSAdditionalRecords)
    {
        DNSFreeRecordList(
                ppDNSAdditionalRecords,
                wAdditionals);
    }
    
    if (ppDNSUpdateRecords)
    {
        DNSFreeRecordList(
                ppDNSUpdateRecords,
                wUpdates);
    }
    
    if (ppDNSZoneRecords)
    {
        DNSFreeZoneRecordList(
                ppDNSZoneRecords,
                wZones);
    }

    *ppDNSResponse = NULL;

    goto cleanup;
}    
Example #7
0
DWORD
DNSUpdateUnmarshallZoneSection(    
    HANDLE hReceiveBuffer,
    WORD wZones,
    PDNS_ZONE_RECORD ** pppDNSZoneRecords
    )
{
    DWORD dwError = 0;
    DWORD i = 0;
    PDNS_ZONE_RECORD pDNSZoneRecord = NULL;
    PDNS_ZONE_RECORD * ppDNSZoneRecords = NULL;
    
    dwError = DNSAllocateMemory(
                wZones * sizeof(PDNS_ZONE_RECORD), 
                (PVOID *)&ppDNSZoneRecords);
    BAIL_ON_LWDNS_ERROR(dwError);

    for (i = 0; i < wZones; i++)
    {
        DWORD dwRead = 0;
        WORD wnZoneClass = 0;
        WORD wnZoneType = 0;
        
        dwError = DNSAllocateMemory(
                    sizeof(DNS_ZONE_RECORD),
                    (PVOID *)&pDNSZoneRecord);
        BAIL_ON_LWDNS_ERROR(dwError);

        dwError = DNSUnmarshallDomainName(
                    hReceiveBuffer,
                    &pDNSZoneRecord->pDomainName);
        BAIL_ON_LWDNS_ERROR(dwError);

        dwError = DNSUnmarshallBuffer(
                    hReceiveBuffer,
                    (PBYTE)&wnZoneType,
                    (DWORD)sizeof(WORD),
                    &dwRead);
        BAIL_ON_LWDNS_ERROR(dwError);
        
        pDNSZoneRecord->wZoneType = ntohs(wnZoneType);
        
        dwError = DNSUnmarshallBuffer(
                    hReceiveBuffer,
                    (PBYTE)&wnZoneClass,
                    (DWORD)sizeof(WORD),
                    &dwRead);
        BAIL_ON_LWDNS_ERROR(dwError);
        
        pDNSZoneRecord->wZoneClass = ntohs(wnZoneClass);

        *(ppDNSZoneRecords + i) = pDNSZoneRecord;
        pDNSZoneRecord = NULL;
    }    

    *pppDNSZoneRecords = ppDNSZoneRecords;
    
cleanup:

    return(dwError);

error:

    if (pDNSZoneRecord)
    {
        DNSFreeZoneRecord(pDNSZoneRecord);
    }
    
    if (ppDNSZoneRecords)
    {
        DNSFreeZoneRecordList(
                ppDNSZoneRecords,
                wZones);
    }
    
    *pppDNSZoneRecords = NULL;

    goto cleanup;   
}