|
|
Регистрация Восстановить пароль |
Повторная активизация e-mail |
Регистрация | Задать вопрос |
Заплачу за решение |
Новые сообщения |
Сообщения за день |
Расширенный поиск |
Правила |
Всё прочитано |
|
Опции темы | Поиск в этой теме |
24.10.2011, 16:26 | #1 |
Новичок
Джуниор
Регистрация: 24.10.2011
Сообщений: 1
|
Формарование DNS запроса
Пишу DNS клиент, проблема в следущем, сервер отвечает непонятно что: пакет с моим id и еще что-то, длинной всего 12 бит! Подскажите пожалуйста может я пакет криво формирую!
Код HTML:
#include "stdafx.h" #include <WinSock2.h> #include <Ws2tcpip.h> #include <iostream> struct DNS_HEADER { unsigned short id; // identification number unsigned char rd :1; // recursion desired unsigned char tc :1; // truncated message unsigned char aa :1; // authoritive answer unsigned char opcode :4; // purpose of message unsigned char qr :1; // query/response flag unsigned char rcode :4; // response code unsigned char cd :1; // checking disabled unsigned char ad :1; // authenticated data unsigned char z :1; // its z! reserved unsigned char ra :1; // recursion available unsigned short q_count; // number of question entries unsigned short ans_count; // number of answer entries unsigned short auth_count; // number of authority entries unsigned short add_count; // number of resource entries }; void ChangetoDnsNameFormat (unsigned char*,unsigned char*); struct QUESTION { unsigned short qtype; unsigned short qclass; }; int _tmain(int argc, _TCHAR* argv[]) { WSAData wd; SOCKET soc; struct sockaddr_in dns_addr; int iResult; unsigned char buf[65536],*qname,*reader; struct DNS_HEADER *dns = NULL; struct QUESTION *qinfo = NULL; unsigned char host[100]; printf("nEnter Hostname to Lookup : "); gets((char*)host); dns = (struct DNS_HEADER *)&buf; dns->id = (32767); dns->qr = 0; //This is a query dns->opcode = 0; //This is a standard query dns->aa = 0; //Not Authoritative dns->tc = 0; //This message is not truncated dns->rd = 1; //Recursion Desired dns->ra = 0; //Recursion not available! hey we dont have it (lol) dns->z = 0; dns->ad = 0; dns->cd = 0; dns->rcode = 0; dns->q_count = htons(1); //we have only 1 question dns->ans_count = 0; dns->auth_count = 0; dns->add_count = 0; //point to the query portion qname =(unsigned char*)&buf[sizeof(struct DNS_HEADER)]; ChangetoDnsNameFormat(qname,host); qinfo =(struct QUESTION*)&buf[sizeof(struct DNS_HEADER) + (strlen((const char*)host) + 1)]; //fill it qinfo->qtype = htons(1); //we are requesting the ipv4 address qinfo->qclass = htons(1); //its internet (lol) WSAStartup (0x202, &wd); soc = socket(AF_INET,SOCK_DGRAM,0);// socket(AF_INET,SOCK_STREAM,0); dns_addr.sin_family=AF_INET; dns_addr.sin_port=htons(53); dns_addr.sin_addr.s_addr=inet_addr("81.30.199.94"); /* iResult = connect(soc,(sockaddr*) &dns_addr,sizeof(dns_addr)); if ( iResult == SOCKET_ERROR) { closesocket (soc); printf("Unable to connect to server: %ld\n", WSAGetLastError()); WSACleanup(); Sleep(5000); return 1; } */ //char *sendbuff; //sendbuff="11001100110011000010000003www2ya2ru00101"; char recvbuff[512]; //int nsize; // if(sendto(s,(char*)buf,sizeof(struct DNS_HEADER) + (strlen((const char*)qname)+1) + sizeof(struct QUESTION),0,(struct sockaddr*)&dest,sizeof(dest))==SOCKET_ERROR) //{ //printf("%d error",WSAGetLastError()); //} sendto(soc,(char*)buf,sizeof(struct DNS_HEADER) + (strlen((const char*)qname)+1) + sizeof(struct QUESTION), 0 , (sockaddr*) &dns_addr,sizeof(dns_addr)); printf("%s",buf); int size_buf= sizeof(struct DNS_HEADER) + (strlen((const char*)host)+1) + sizeof(struct QUESTION); iResult = sendto(soc,(char*)buf,size_buf,0,(struct sockaddr*)&dns_addr,sizeof(dns_addr)); if (iResult == SOCKET_ERROR) { printf("send failed: %d\n", WSAGetLastError()); closesocket(soc); WSACleanup(); Sleep(5000); return 1; } /* ///разбираем ответ printf("Received."); dns=(struct DNS_HEADER*)buf; reader=&buf[sizeof(struct DNS_HEADER) + (strlen((const char*)qname)+1) + sizeof(struct QUESTION)]; printf("nThe response contains : "); printf("n %d Questions.",ntohs(dns->q_count)); printf("n %d Answers.",ntohs(dns->ans_count)); printf("n %d Authoritative Servers.",ntohs(dns->auth_count)); printf("n %d Additional records.nn",ntohs(dns->add_count)); //reading answers //stop=0; */ sockaddr_in server_addr; int server_addr_size=sizeof(server_addr); iResult = recvfrom(soc, &recvbuff[0], 512, 0, (sockaddr*)&server_addr,&server_addr_size); closesocket(soc); WSACleanup(); return 2; Sleep(5000); } void ChangetoDnsNameFormat(unsigned char* dns,unsigned char* host) { int lock=0 , i; strcat((char*)host,"."); for(i=0;i<(int)strlen((char*)host);i++) { if(host[i]=='.') { *dns++=i-lock; for(;lock<i;lock++) { *dns++=host[lock]; } lock++; } } *dns++='0'; } |
Похожие темы | ||||
Тема | Автор | Раздел | Ответов | Последнее сообщение |
DNS | Dimarik | Общие вопросы .NET | 3 | 19.04.2010 18:04 |
DNS компонент | magsim | Работа с сетью в Delphi | 1 | 01.02.2010 00:33 |
Работа с DNS | ImmortalAlexSan | Помощь студентам | 4 | 10.01.2010 14:30 |
Удаление DNS | wizard68 | Помощь студентам | 6 | 11.12.2009 14:29 |
DNS Client | ikakey | Софт | 0 | 27.03.2009 11:15 |