sockets - C - 通过UDP套接字向DNS服务器发送请求

102 5

我在linux上编写了一个小C程序,它带有3个参数: IP端口和应该发送到给定服务器的数据。


client 8.8.8.8 53 www.example.com



但是我在哪里可以得到OPCODE, QCLASS, QTYPE的可能值?

我的程序

#include <errno.h>


#include <stdio.h>


#include <stdlib.h>


#include <string.h>


#include <sys/types.h>


#include <sys/socket.h>


#include <netdb.h>


#include <unistd.h>



int main (int argc, char **argv) {


 if (argc < 4) {


 printf("Error to few arguments.nUsage: client <ip/host name> <port> <query>n");


 exit(1);


 }



 struct addrinfo *server_addr;


 struct addrinfo hint;


 memset(&hint, 0, sizeof(hint));



 hint.ai_family = AF_UNSPEC;


 hint.ai_socktype = SOCK_DGRAM;


 hint.ai_protocol = IPPROTO_UDP;



 printf("getting server infon");


 /* Heh...ironic */


 int err = getaddrinfo(argv[1], argv[2], &hint, &server_addr);


 if (err != 0) {


 perror("Error while getting server address info");


 exit(1);


 }



 int client;


 /* Iterate over the provided addresses and try to establish a connection */


 for (struct addrinfo *cur_info = server_addr; cur_info != NULL; cur_info = cur_info->ai_next) {


 /* Create corresponding socket */


 client = socket(cur_info->ai_family, cur_info->ai_socktype, cur_info->ai_protocol);



 printf("Trying to establish connectionn");


 err = connect(client, cur_info->ai_addr, sizeof(*(cur_info->ai_addr)));


 /* If connection was established leave loop */


 if (err == 0) break;


 /* If an error occured notify */


 if (err == -1) perror("Error while connecting");


 close(client);


 /* If the current info was the last one consider connection failed */


 if (cur_info->ai_next == NULL) {


 printf("Error could not connect to any servicen");


 exit(1);


 }



 }


 /* Release information as it is no longer needed */


 freeaddrinfo(server_addr);



 printf("Sending requestn");


 err = send(client, argv[3], strlen(argv[3]), 0);


 if (err < 1) {


 perror("Error while sending request");


 exit(1);


 }


 printf("%d b sentn", err);



 char buffer[512];


 int bytes_received = 0;



 printf("receiving answern");


 while ((bytes_received = recv(client, buffer, sizeof(buffer), 0)) != 0) {


 if (bytes_received == -1) {


 perror("Error while receiving message from server");


 break;


 }


 fwrite(buffer, bytes_received, 1, stdout);


 }



 close(client);


}



时间: 原作者:

58 3

res_mkquery函数最初来自BIND/libresolv,但是,几乎存在于所有版本的libc中(尽管未在标准中定义),它都能为你生成查询数据包,你可以检查输出而无需发送,它非常有用。

请注意,来自

原作者:
...