C中的DNS客户端 [英] DNS client in C

查看:141
本文介绍了C中的DNS客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开展一个学校项目,要求我实施DNS客户端,而不使用任何库函数。

I am currently working on a school project which asks me to implement a DNS client, without using any library functions.

我已经到了发送DNS请求并接收回复的地步。我正在解答答复。我收到一个char *数组的答复,我想把它转换成一个有意义的结构,从中我可以解析答案。
我经历了RFC,我读了关于数据包结构,但是在C中实现它给我的问题。

I have got to the point where i send a DNS request and Receive the Reply. I'm getting stuck at the parsing of the reply. I receive the reply in a char* array and i want to convert it into some meaningful structure, from which i can parse the answer. I went through the RFC and i read about the packet structure, but implementing it in C is giving me problems.

任何人都可以给我任何例子,在C,或者可能用任何其他语言来解释这是如何做到的。或者任何对一本书的引用也是很好的。

Can anyone give me any examples, in C, or maybe in any other language that explains how this is done. Or any reference to a book is also fine.

其他详细信息:

所以,以下是结构我正在使用

So, the following are the structures that i'm using.

struct result{
  int type;
  struct res_ip_cname ip_cname;
  struct res_error error;
  struct res_mx_ns mx_ns;
};

struct res_ip_cname{
  char* lst;
  int sec;
  char* auth_flag;
};

struct res_error{
  char * info;
};

struct res_mx_ns{
  char * name;
  unsigned short pref;
  int sec;
  char* auth_flag;
};

我有一个char *缓冲区[],其中im存储从服务器收到的响应。而且,我需要从这个缓冲区中提取信息并填充结构结果。

I have a char* buffer[], where im storing the response the i receive from the server. And, i need to extract information from this buffer and populate the structure result.

谢谢,
Chander

Thanks, Chander

推荐答案

看起来像我从RFC发现的任何东西(是的,我已经写了很多DNS数据包解码软件)。

Your structures don't look like anything I recognise from the RFCs (yes, I've written lots of DNS packet decoding software).

查看 RFC 1035 ,特别是您可以直接从其中显示的字段布局映射所需的大多数结构。

Look at RFC 1035 in particular - most of the structures you need can be mapped directly from the field layouts show therein.

例如,你需要一个标题(见s4.1.1):

For example, you need a header (see s4.1.1):

struct dns_header {
     uint16_t     query_id;
     uint16_t     flags;
     uint16_t     qdcount;
     uint16_t     ancount;
     uint16_t     nscount;
     uint16_t     arcount;
};

不要忘记使用 ntohs()将这些字段的有线格式转换为机器的本机字节顺序。网络订单是大端的,大多数机器这些天是小端。

Don't forget to use ntohs() to convert the wire format of these fields into your machine's native byte order. The network order is big-endian, and most machines these days are little-endian.

你需要一个问题结构(见s4.1.2),和一个通用的资源记录结构(见s4.1.3)。

You'll need a "question" structure (see s4.1.2), and a generic "resource record" structure too (see s4.1.3).

请注意,这两个的线格式开始可变长度标签,也可以包括压缩指针(见s4.1.4)。这意味着你不能在这些情况下将整个线框简单地映射到C结构上。

Note however that the wire format of both of these starts with a variable length "label", which can also include compression pointers (see s4.1.4). This means that you can't in these cases trivially map the whole wire block onto a C structure.

希望这有帮助...

这篇关于C中的DNS客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆