/* ******************************************************************************** * PARSE THE DNS REPLY * * Description : This function parses the reply message from DNS server. * Arguments : dhdr - is a pointer to the header for DNS message * buf - is a pointer to the reply message. * len - is the size of reply message. * Returns : None * Note : ******************************************************************************** */ uint8 parseMSG(struct dhdr * pdhdr, uint8 * pbuf) { uint16 tmp; uint16 i; uint8 * msg; uint8 * cp; msg = pbuf; memset(pdhdr, 0, sizeof(pdhdr)); pdhdr->id = ntohs(*((uint16*)&msg[0])); tmp = ntohs(*((uint16*)&msg[2])); if (tmp & 0x8000) pdhdr->qr = 1; pdhdr->opcode = (tmp >> 11) & 0xf; if (tmp & 0x0400) pdhdr->aa = 1; if (tmp & 0x0200) pdhdr->tc = 1; if (tmp & 0x0100) pdhdr->rd = 1; if (tmp & 0x0080) pdhdr->ra = 1; pdhdr->rcode = tmp & 0xf; pdhdr->qdcount = ntohs(*((uint16*)&msg[4])); pdhdr->ancount = ntohs(*((uint16*)&msg[6])); pdhdr->nscount = ntohs(*((uint16*)&msg[8])); pdhdr->arcount = ntohs(*((uint16*)&msg[10])); /* Now parse the variable length sections */ cp = &msg[12]; /* Question section */ for (i = 0; i < pdhdr->qdcount; i++) { cp = dns_question(msg, cp); } /* Answer section */ for (i = 0; i < pdhdr->ancount; i++) { cp = dns_answer(msg, cp); } /* Name server (authority) section */ for (i = 0; i < pdhdr->nscount; i++) { ; } /* Additional section */ for (i = 0; i < pdhdr->arcount; i++) { ; } if(pdhdr->rcode == 0) return 1; // No error else return 0; }
void process_question(char *buf, struct sockaddr *addr, int sockfd) { dns_header head; struct sockaddr_in *address = (struct sockaddr_in*)addr; memcpy(&head, buf, sizeof(dns_header)); int len = 0; char *to_send = dns_answer(head.id, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, buf+sizeof(dns_header), &len); //int sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); struct sockaddr_in out; out.sin_family = AF_INET; out.sin_port = address->sin_port; out.sin_addr.s_addr = address->sin_addr.s_addr; if (sendto(sockfd, to_send, len, 0, (struct sockaddr*)&out, sizeof(out)) < 0) perror("sendto"); }
/* ******************************************************************************** * PARSE THE DNS REPLY * * Description : This function parses the reply message from DNS server. * Arguments : dhdr - is a pointer to the header for DNS message * buf - is a pointer to the reply message. * Returns : None * Note : ******************************************************************************** */ u8 parseMSG(struct dhdr * pdhdr, u8 * pbuf, u8 * pSip) { u16 tmp; u16 i; u8 * msg; u8 * cp; msg = pbuf; memset(pdhdr, 0, sizeof(pdhdr)); pdhdr->id = get16(&msg[0]); tmp = get16(&msg[2]); if (tmp & 0x8000) pdhdr->qr = 1; pdhdr->opcode = (tmp >> 11) & 0xf; if (tmp & 0x0400) pdhdr->aa = 1; if (tmp & 0x0200) pdhdr->tc = 1; if (tmp & 0x0100) pdhdr->rd = 1; if (tmp & 0x0080) pdhdr->ra = 1; pdhdr->rcode = tmp & 0xf; pdhdr->qdcount = get16(&msg[4]); pdhdr->ancount = get16(&msg[6]); pdhdr->nscount = get16(&msg[8]); pdhdr->arcount = get16(&msg[10]); #ifdef DBG_DNS printf("dhdr->qdcount : %x\r\n", (u16)dhdr->qdcount); printf("dhdr->ancount : %x\r\n", (u16)dhdr->ancount); printf("dhdr->nscount : %x\r\n", (u16)dhdr->nscount); printf("dhdr->arcount : %x\r\n", (u16)dhdr->arcount); #endif /* Now parse the variable length sections */ cp = &msg[12]; /* Question section */ for (i = 0; i < pdhdr->qdcount; i++) { cp = dns_question(msg, cp); } /* Answer section */ for (i = 0; i < pdhdr->ancount; i++) { cp = dns_answer(msg, cp, pSip); } /* Name server (authority) section */ for (i = 0; i < pdhdr->nscount; i++) { ; } /* Additional section */ for (i = 0; i < pdhdr->arcount; i++) { ; } if(pdhdr->rcode == 0) return 1; // No error else return 0; }