Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 140 lines (136 sloc) 4.041 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#include "parser.h"

/*
* Print a pointer. If it finds a nother pointer while it is printing it will call it's self recursivly on that
* new pointer.
* array - 0th index of array
* i - int pointint to the offset
*/
int _print_pointer( unsigned char* array, int i){
    unsigned char tempanswer[512]; // Need this for resolving pointers
    int offset = (int)array[i+1];

    while( array[offset] != 0x00 ){
        // It's a pointer print it, then move up i past it
        if(array[offset] == 0xC0){
           memcpy(&tempanswer,array,SIZE_OF_RESP);
           _print_pointer(array,offset);
           array[offset] = '\0'; //We don't want to resolve this pointer agian. It could cause an infanite loop.
           offset+=2;
           break;
        }
        if(array[offset]>0x21){
            printf("%c",array[offset]);
        }else{
            printf(".");
        }
        offset++;
    }
    return 1;
}
/*
* I'm sure this function is laying around in some lib. oh well!
* copy size number of byets from block into dest.
* *cough* memcopy() *cough*
*/
void _byte_copy2(unsigned char *block,unsigned char *dest, int size){
    int i = 0;
    while(i<size){
        dest[i]=block[i];
        i++;
    }
    return;
}
/*
* Print out RDATA. array is a pointer to the response. Size if RDLENTH, index is where the data should start.
*/
int _data_print( unsigned char* array, unsigned char size, int index, int ip){
    int i;
    unsigned char tempanswer[512]; // Need this for resolving pointers
    for(i=0;i<size;i++){
        if(array[index+i] == 0xC0){
           memcpy(&tempanswer,array,SIZE_OF_RESP);
           _print_pointer(array,index+i);
           array[index] = '\0'; //We don't want to resolve this pointer agian. It could cause an infanite loop.

           continue;
        }
        if(i+1<size){
            if(ip){
                printf("%i.",array[index+i]);
            }else{
                if(array[index+i]>0x21){
                    if(array[index+i]>0x21){
                        printf("%c",array[index+i]);
                    }else{
                        printf(".");
                    }
                }
            }
        }else{
            if(ip){
            printf("%i",array[index+i]);
            }
        }
    }
    return 1;
}
/*
* Used for debugging. Prints out a string of unsigned char's length "size"
*/
int _hex_print2( unsigned char* array, unsigned char size){
    int i;
    for(i=0;i<size;i++){
        printf("%02hhX ",array[i]);
    }
    return 1;
}
/*
* Given a dns response in answer, parse all the needed ip's out of it.
*/
int parse_answer(unsigned char *answer, struct DNS_REQUEST *data){
    unsigned char acount;
    unsigned char tempanswer[512]; // Need this for resolving pointers
    int i,index,len;
    int type;
    _byte_copy2(&answer[7],&acount,1);
    printf("there were %i answers\n",acount);
    index = data->size; //Let's where the header ends
    for(i=0;i<acount;i++){
        /* NAME */
        printf("NAME: ");
        memcpy(&tempanswer,answer,SIZE_OF_RESP);
        _print_pointer(tempanswer,index);
        printf("\n");
        index +=2;
        /* TYPE */
        printf("TYPE: ");
        type = answer[index+1];
        _hex_print2(&answer[index],2);
        printf("\n");
        index+=2;
        /* CLASS */
        printf("CLASS: ");
        _hex_print2(&answer[index],2);
        printf("\n");
        index+=2;
        /* TTL */
        printf("TTL (32 bit): ");
        _hex_print2(&answer[index],4);
        printf("\n");
        index+=4;
        /* RDLENTH */
        printf("RDLENGTH: ");
        _hex_print2(&answer[index],2);
        printf("\n");
        len = (int)answer[index+1];
        index+=2;
        printf("DATA: ");
        memcpy(&tempanswer,answer,SIZE_OF_RESP);
        if(type==5){
            _data_print(tempanswer,len,index,0);
        }else{
            _data_print(tempanswer,len,index,1);
        }
        index = index+len;
        printf("\n\n");
    }
    return 1;
}
Something went wrong with that request. Please try again.