从 DNS 服务器提取 MX 记录

发布于 2024-07-26 12:05:55 字数 145 浏览 5 评论 0 原文

我正在编写一个应用程序,要求我对 MX 记录进行 DNS 查找。 我不确定是否有人有从事此类工作的经验,但如果您有的话,我们将不胜感激。

编辑: 我想要的是一个可以发送电子邮件警报的应用程序。 问题是我需要让应用程序能够查找域的 MX 记录。

I am writing an application that is requiring me to do a DNS lookup for an MX record. I'm not sure if anyone has had experience doing this kind of work but if you do, any help would be appreciated.

EDIT:
The thing that I'm going for is an application that will send an e-mail alert. The problem is I need to have the application be able to lookup the MX record for a domain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暖树树初阳… 2024-08-02 12:05:55

最简单的方法是简单地使用常用的工具。

基本的“dig”命令将通过以下查询将记录返回给您:

dig mx example.com

如果您只想要包含 mx 记录的行...

dig mx example.com | grep -v '^;' | grep example.com

dig 在大多数 linux / unix 机器上都可用。

如果你在 Windows 上,你可以使用 nslookup

nslookup -type=mx example.com

然后解析这些常用工具的输出。

编辑:来自网络的套接字的简单 C 示例

既然您将“C”作为标签,我猜您正在寻找使用原始套接字进行 MX 查找的源代码。 我从 http://www.developerweb.net/forum/showthread.php 复制了此内容?t=3550。 这可能是您正在寻找的更多内容?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <resolv.h>

int main (int argc, char *argv[])
{
    u_char nsbuf[4096];
    char dispbuf[4096];
    ns_msg msg;
    ns_rr rr;
    int i, j, l;

    if (argc < 2) {
        printf ("Usage: %s <domain>[...]\n", argv[0]);
        exit (1);
    }

    for (i = 1; i < argc; i++) {
        l = res_query (argv[i], ns_c_any, ns_t_mx, nsbuf, sizeof (nsbuf));
        if (l < 0) {
            perror (argv[i]);
        } else {
#ifdef USE_PQUERY
/* this will give lots of detailed info on the request and reply */
            res_pquery (&_res, nsbuf, l, stdout);
#else
/* just grab the MX answer info */
            ns_initparse (nsbuf, l, &msg);
            printf ("%s :\n", argv[i]);
            l = ns_msg_count (msg, ns_s_an);
            for (j = 0; j < l; j++) {
                ns_parserr (&msg, ns_s_an, j, &rr);
                ns_sprintrr (&msg, &rr, NULL, NULL, dispbuf, sizeof (dispbuf));
                printf ("%s\n", dispbuf);
            }
#endif
        }
    }

    exit (0);
}

The simplest method is to simply use commonly available tools.

The basic "dig" command will return the records to you via this query:

dig mx example.com

If you want just the lines with the mx records...

dig mx example.com | grep -v '^;' | grep example.com

dig is available on most linux / unix boxes.

If you're on windows you can use nslookup

nslookup -type=mx example.com

Then just parse the output of these common tools.

EDIT: Simple C example of sockets from the web

Since you put "C" as a tag, I guess you're looking for source code to do MX lookups using raw sockets. I copied this from http://www.developerweb.net/forum/showthread.php?t=3550. It may be more what you're looking for?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <resolv.h>

int main (int argc, char *argv[])
{
    u_char nsbuf[4096];
    char dispbuf[4096];
    ns_msg msg;
    ns_rr rr;
    int i, j, l;

    if (argc < 2) {
        printf ("Usage: %s <domain>[...]\n", argv[0]);
        exit (1);
    }

    for (i = 1; i < argc; i++) {
        l = res_query (argv[i], ns_c_any, ns_t_mx, nsbuf, sizeof (nsbuf));
        if (l < 0) {
            perror (argv[i]);
        } else {
#ifdef USE_PQUERY
/* this will give lots of detailed info on the request and reply */
            res_pquery (&_res, nsbuf, l, stdout);
#else
/* just grab the MX answer info */
            ns_initparse (nsbuf, l, &msg);
            printf ("%s :\n", argv[i]);
            l = ns_msg_count (msg, ns_s_an);
            for (j = 0; j < l; j++) {
                ns_parserr (&msg, ns_s_an, j, &rr);
                ns_sprintrr (&msg, &rr, NULL, NULL, dispbuf, sizeof (dispbuf));
                printf ("%s\n", dispbuf);
            }
#endif
        }
    }

    exit (0);
}
请叫√我孤独 2024-08-02 12:05:55

我注意到你是为 Linux 编写的。 常规程序在类 Unix 系统上发送邮件的惯用方式是:

  • 在子进程中运行 /usr/bin/mail 并在其标准输入上向其发送邮件消息(请参阅邮件联机帮助页); 或
  • 连接到 127.0.0.1:25 并向本地邮件守护程序提供要传递的消息。

这两种方式都假定本地邮件程序已配置为将邮件传递到它必须去的地方; 在配置良好的 Linux 机器上,这是一个合理的假设。

如果这没有吸引力,第二好的方法是让您的程序接受要使用的本地邮件中继服务器的地址,然后仅在端口 25 上连接到该服务器。

换句话说,只要有可能,请使用现有的邮件中继来发送您的邮件。 这些邮件中继将拥有从您正在运行的网络中获取邮件所需的所有本地知识 - 仅查找 MX 并尝试直接发送到目的地并不总是有效。

如果您已阅读所有内容,但仍然想要查找 MX 记录,请尝试 adnslibrary,它负责处理 DNS 解析中涉及的所有繁琐的细节(相信我,它繁琐,而且很容易出错!)。

I notice that you're writing for Linux. The idomatic way for a regular program to send mail on Unix-like systems is either:

  • Run /usr/bin/mail in a subprocess and send it the mail message on its standard input (see the mail manpage); or
  • Connect to 127.0.0.1:25 and give the local mail daemon the message to deliver.

Both ways presume that the local mailer is configured to pass mail on to where it has to go; on a well-configured Linux box this is a fair assumption.

If that doesn't appeal, the second-best way is for your program to accept the address of a local mail relay server to use, and just connect to that server on port 25.

In other words, wherever possible, use an existing mail relay to send your mail on. Those mail relays will have all the local knowledge that might be necessary to get mail out of the network that you're running on - just looking up the MX and trying to send directly to the destination is not always going to work.

If you've read all that and you still want to look up MX records, try the adns library, it takes care of all the tedious details involved in DNS resolution (and believe me, it is tedious, and easy to get wrong!).

年少掌心 2024-08-02 12:05:55

在Linux上:

host -t mx google.com

on linux:

host -t mx google.com
破晓 2024-08-02 12:05:55

[免责声明:我曾经是 SnertSoft 的产品的快乐 Beta 测试者,并且我仍在运行他们的几个产品]

实际上手动执行此操作有其陷阱,例如,当您必须处理截断的响应并需要从 UDP 切换时到 TCP。

SnertSoftlibsnert(免费,需要点击许可证)已经有一个返回条目向量的 C 实现。 如果可能的话,尽量避免“not-invented-here”:)

您确实必须重新实现 MTA 而不是使用某些现有的解决方案吗?

[Disclaimer: I used to be a happy beta tester of SnertSoft's stuff, and I'm still running several of their products]

Actually doing this by hand has its pitfalls, e.g. when you have to handle a truncated response and need to switch from UDP to TCP.

SnertSoft's libsnert (free, click-through-license required) already has a C implementation for this returning a vector of entries. Try to avoid "not-invented-here" if possible :)

Do you indeed have to reimplement an MTA instead of using some existing solution?

没︽人懂的悲伤 2024-08-02 12:05:55

查看 NLnet Labs 的 ldns。 该库处理低级数据包处理,并具有自己的内置解析器客户端。

该文档包括示例代码,它完全符合您的要求。

Take a look at ldns, from NLnet Labs. This library handles low level packet handling and has its own resolver client built-in.

The documentation includes example code that does exactly what you require.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文