Permalink
Browse files

stuff shuold be working

  • Loading branch information...
uberj committed Mar 8, 2011
1 parent 160fd4d commit b531b1b7d1a6262dd34084a65c9792bc7a72e03a
Showing with 104 additions and 181 deletions.
  1. +3 −0 .gitignore
  2. +1 −1 Makefile
  3. +50 −0 README
  4. BIN c_utils.o
  5. BIN client
  6. +5 −19 client.c
  7. +0 −140 constructer.c
  8. +39 −21 parser.c
  9. +6 −0 parser.h
View
@@ -0,0 +1,3 @@
+*.swp
+*.o
+client
View
@@ -1,7 +1,7 @@
CC = gcc
CFLAGS = -g -Wall
-objects = client.o dns_c.h socket_utils.o constructor.o
+objects = client.o dns_c.h socket_utils.o constructor.o c_utils.o parser.o
shell_lib.a: $(objects)
View
50 README
@@ -1,2 +1,52 @@
http://www.netfor2.com/dns.htm
^ A good site.
+Reference:
+A = 10
+B = 11
+C = 12
+D = 13
+E = 14
+F = 15
+
+Example: example break down:
+ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (bytes)
+
+41 42 81 80 00 01 00 07 00 04 00 04 03 77 77 77 16
+06 67 6F 6F 67 6C 65 03 63 6F 6D 00 00 01 00 01 32
+(question)
+(name) C0 0C
+(type) 00 05
+(class) 00 01
+( ttl ) 00 04 F5 3A
+(rdlength) 00 08
+03 77 77 77 48 01 6C C0 10
+
+C0 2C 00 01 00 01 00 00 00 6D 00 04 64
+4A 7D 7F 67 C0 2C 00 01 00 01 00 00 00 6D 00 04 ...
+4A 7D 7F 68 C0 2C 00 01 00 01 00 00 00 6D 00 04
+4A 7D 7F 69 C0 2C 00 01 00 01 00 00 00 6D 00 04
+4A 7D 7F 6A C0 2C 00 01 00 01 00 00 00 6D 00 04
+4A 7D 7F 93 C0 2C 00 01 00 01 00 00 00 6D 00 04
+4A 7D 7F 63 C0 10 00 02 00 01 00 00 BC 34 00 06
+03 6E 73 33 C0 10 C0 10 00 02 00 01 00 00 BC 34
+00 06 03 6E 73 32 C0 10 C0 10 00 02 00 01 00 00
+BC 34 00 06 03 6E 73 34 C0 10 C0 10 00 02 00 01
+00 00 BC 34 00 06 03 6E 73 31 C0 10 C0 D6 00 01
+00 01 00 00 A2 48 00 04 D8 EF 20 0A C0 B2 00 01
+00 01 00 00 A2 48 00 04 D8 EF 22 0A C0 A0 00 01
+00 01 00 00 A2 48 00 04 D8 EF 24 0A C0 C4 00 01
+00 01 00 00 A2 48 00 04 D8 EF 26 0A 00 00 00 00
+01 00 00 00 00 28 6E 6F E0 07 D3 60 4C 2B 00 00
+80 09 70 7A FF 7F 00 00 00 00 00 00 00 00 00 00
+4E 30 B4 2C 00 00 00 00 BF 00 00 00 00 00 00 00
+80 09 70 7A 01 00 00 00 40 85 A4 60 4C 2B 00 00
+60 43 A1 60 4C 2B 00 00 90 0A 70 7A FF 7F 00 00
+00 40 A1 60 4C 2B 00 00 B6 05 40 00 00 00 00 00
+4E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00 F0 57 56 00 00 00 00 00
+BF 00 00 00 00 00 00 00 F0 57 56 00 00 00 00 00
+06 0A 70 7A FF 7F 00 00 07 0A 70 7A FF 7F 00 00
+00 00 00 00 00 00 00 00 C0 3B C1 60 4C 2B 00 00
+B0 16 40 00 00 00 00 00 B3 08 40 00 00 00 00 00
+01 00 00 00 4E 30 B4 2C E7 16 40 00 00 00 00 00
View
BIN c_utils.o
Binary file not shown.
View
BIN client
Binary file not shown.
View
@@ -14,7 +14,8 @@ int remove_www(char *url);
int main( int argc, char **argv ){
struct DNS_REQUEST question;
- unsigned char answer[512];
+ unsigned char answer[SIZE_OF_RESP];
+ memset(answer,0,SIZE_OF_RESP);
/*
* Ahead of all else, make the head.
*/
@@ -30,10 +31,7 @@ int main( int argc, char **argv ){
question.qtype[1] = '\1'; //0001
question.qclass[0] = '\0'; //0000
question.qclass[1] = '\1'; //0001
-// int qsize = ( sizeof(unsigned char) * strlen(argv[1]) ) + 5;
- //question.query = (unsigned char *) malloc( qsize );
unsigned char query[19];
- remove_www(argv[1]);
question.query = &query[0];
// Fill in that cruft
int i;
@@ -49,9 +47,7 @@ int main( int argc, char **argv ){
* Get the query in the query.
*/
- // start the query where we left off
- // We want the len of the RR to be size of the request url, 1 null byte, and 4
// bytes for qtype and class
build_query(&question,argv[1]);
// Print all the bytes in the DNS_HEADER, the request and the 4 bytes
@@ -62,24 +58,14 @@ int main( int argc, char **argv ){
/*
* Sending request
*/
- send_request(&question,&answer[0]);
+ send_request(&question,answer);
printf("RESPONSE:\n");
- _hex_print(&answer[0],512);
- parse_answer(&answer[0],&question);
+ _hex_print(answer,512);
+ parse_answer(answer,&question);
return 0;
}
-int remove_www(char *url){
- char * temp;
- if(url[0]=='w' && url[1]=='w' && url[2]=='w'){
- temp = (char *) malloc(strlen(url)-2);
- strcpy(temp,&url[4]);
- strcpy(url,temp);
- }
- return 0;
-}
-
int _hex_print( unsigned char* array, int size){
int i;
printf("hex string: \n");
View
@@ -1,140 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include "dns_c.h"
-
-int build_query(char *query_addr);
-int build_header(struct DNS_REQUEST data);
-void _byte_copy(char *block, char *dest, int size){
-void _sub_string(int idx1, int idx2, char *string , char *dest_str);
-/*
- *
- 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- | ID |
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- | QDCOUNT |
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- | ANCOUNT |
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- | NSCOUNT |
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- | ARCOUNT |
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
-
- */
-/*
- * Building a query any way I can.
- * Pass in an address that should be looked up and a
- * DNS_REQUEST struct.
- */
-int build_header(struct DNS_REQUEST data){
- // Byte offset we are at in the packet
- int offset = 0;
- // char is 1 byte, 8 bits
- // Bits 0-15
-/* Line 0 */
- _byte_copy(data->id,data->query+offset,2);
- offset = offset + 2;
-
-/* Line 1 */
- // First byte
- _byte_copy(data->flags1,data->query+offset,2);
- offset = offset + 1;
- // Second byte
- _byte_copy(data->flags2,data->query+offset,2);
- offset = offset + 1;
-
-/* Line 2 */
- _byte_copy(data->qdcount,data->query+offset,2);
- offset = offset + 2;
-
-/* Line 3 - 5 */
- _byte_copy(data->cruft,data->query+offset,6);
- offset = offset + 6;
-
-}
-
-/*
- 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- | |
- / QNAME /
- / /
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- | QTYPE |
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- | QCLASS |
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
-*/
-/*
- * Tricky funtion. Take a string with '.' in it. Start at the first char and count in x until
- * you hit the first '.'. Write X in hex to another string, then all the characters
- * you read over in the other string. Continue until you reach null term.
- *
- * ex: www.google.com
- * 3www6google3com
- */
-int build_query(struct DNS_REQUEST data, char *query_addr){
- // char '0' is 48
- unsigned char count = '0';
- int size = strlen(query_addr);
- unsigned char temp[size];
- int p = 0;
-
- count = count-48;
- // this is going to have to be debugged.
- for(int i=0; query_addr[i] != '\0';i++){
- if(query_addr[i] == '.'){
- temp[p]= count;
- _sub_string(p,i,query_addr,temp[p+1]);
- count = '0';
- count = count - 48;
- p=i;
- }else{
- count = count + 1;
- }
- }
- if(i != size){
- printf("Sanity check failed in build_query\n");
- }
- _byte_copy(tmp,data->query+DNS_HEADER_SIZE,i);
- // Should there be a null terminator in the query?
-
- int offset = DNS_HEADER_SIZE + size;
-
- // Copy in the qtype on qclass bytes
- _byte_copy(data->qtype,temp[offset],2);
-
- _byte_copy(data->qclass,temp[offset+2],2);
- _byte_copy(tmp,data->query+DNS_HEADER_SIZE,size+4);
-}
-/*
- * I'm sure string.h has a function for this. Take a sub string and copy it into another.
- * idx1 is inclusive
- * idx2 is exclusive *cough* java *cough*
- * precondition: idx1 < idx2
- */
-void _sub_string(int idx1, int idx2, char *string , char *dest_str){
- while(idx1 < idx2){
- dest_str[idx1] = string[idx2];
- idx1++;
- }
- return;
-}
-/*
- * Sure this function is laying around in some lib. oh well!
- * copy size number of byets from block into dest.
- */
-void _byte_copy(char *block, char *dest, int size){
- int i = 0;
- while(i<size){
- dest[i]=block[i];
- i++;
- }
- return;
-}
View
@@ -7,19 +7,17 @@
* 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];
- if(array[i] != 0xC0){
- perror("Not a pointer");
- exit(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;
- continue;
-
+ break;
}
if(array[offset]>0x21){
printf("%c",array[offset]);
@@ -46,20 +44,33 @@ void _byte_copy2(unsigned char *block,unsigned char *dest, int size){
/*
* 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 _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){
- printf("%i.",array[index+i]);
+ if(ip){
+ printf("%i.",array[index+i]);
+ }else{
+ if(array[index+1]>0x21){
+ if(array[index+1]>0x21){
+ printf("%c",array[index+i]);
+ }else{
+ printf(".");
+ }
+ }
+ }
}else{
+ if(ip){
printf("%i",array[index+i]);
+ }
}
}
return 1;
@@ -81,6 +92,7 @@ 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
@@ -92,28 +104,34 @@ int parse_answer(unsigned char *answer, struct DNS_REQUEST *data){
printf("\n");
index +=2;
/* TYPE */
- //printf("TYPE: ");
- //_hex_print2(&answer[index],2);
- //printf("\n");
+ 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");
+ printf("CLASS: ");
+ _hex_print2(&answer[index],2);
+ printf("\n");
index+=2;
/* TTL */
- //printf("TTL (32 bit): ");
- //_hex_print2(&answer[index],4);
- //printf("\n");
+ printf("TTL (32 bit): ");
+ _hex_print2(&answer[index],4);
+ printf("\n");
index+=4;
/* RDLENTH */
- //printf("RDLENGTH: ");
- //_hex_print2(&answer[index],2);
- //printf("\n");
+ printf("RDLENGTH: ");
+ _hex_print2(&answer[index],2);
+ printf("\n");
len = (int)answer[index+1];
index+=2;
printf("DATA: ");
- _data_print(answer,len,index);
+ 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");
}
View
@@ -0,0 +1,6 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "dns_c.h"
+
+int parse_answer(unsigned char *answer, struct DNS_REQUEST *data);

0 comments on commit b531b1b

Please sign in to comment.