【问题标题】:recvfrom() function does not workrecvfrom() 函数不起作用
【发布时间】:2013-09-08 12:47:01
【问题描述】:

我有开源鳕鱼(这是它的链接) 访问https://github.com/jtRIPper/dns-tcp-socks-proxy/blob/master/dns_proxy.c 在这部分鳕鱼

 while(1) 
    {
        // receive a dns request from the client
        printf("wait for a dns request from the client\n");
        len = recvfrom(sock, buffer->buffer, 2048, 0, (struct sockaddr *)&dns_client, &dns_client_size);
        printf("dns request received\n");


        // fork so we can keep receiving requests
        if (fork() != 0) { continue; }

        // the tcp query requires the length to precede the packet, so we put the length there
        query = malloc(len + 3);
        query[0] = 0;
        query[1] = len;
        memcpy(query + 2, buffer->buffer, len);

        // forward the packet to the tcp dns server
        fprintf(LOG_FILE, "tcp query call\n");
        tcp_query(query, buffer, len + 2);

        // send the reply back to the client (minus the length at the beginning)
        sendto(sock, buffer->buffer + 2, buffer->length - 2, 0, (struct sockaddr *)&dns_client, sizeof(dns_client));

        free(buffer->buffer);
        free(buffer);
        free(query);

        exit(0);

,recvfrom() 函数不起作用,我无法继续并显示“收到的 dns 请求\n”是什么问题?然后当我使用 netstat -upan inn command lin 时,我看到了这个

活动的 Internet 连接(服务器和已建立) Proto Recv-Q Send-Q 本地地址 外部地址 状态 PID/程序名称 udp 0 0 127.0.0.1:951 0.0.0.0:* 1623/rpc.statd
UDP 0 0 0.0.0.0:54721 0.0.0.0:* 2214/avahi-daemon: udp 0 0 0.0.0.0:45085 0.0.0.0:* 1623/rpc.statd
udp 0 0 127.0.0.1:53 0.0.0.0:* 4084/dns_proxy
udp 0 0 0.0.0.0:68 0.0.0.0:* 1628/dhclient
udp 0 0 0.0.0.0:111 0.0.0.0:* 1582/rpcbind
udp 0 0 0.0.0.0:631 0.0.0.0:* 2323/cupsd
UDP 0 0 0.0.0.0:5353 0.0.0.0:* 2214/avahi 守护进程: udp 0 0 0.0.0.0:42756 0.0.0.0:* 1628/dhclient
udp 0 0 0.0.0.0:1900 0.0.0.0:* 3306/minissdpd
udp 0 0 0.0.0.0:908 0.0.0.0:* 1582/rpcbind
udp6 0 0 :::111 :::* 1582/rpcbind
udp6 0 0 :::34443 :::* 1623/rpc.statd
udp6 0 0 :::5353 :::* 2214/avahi-daemon: udp6 0 0 :::62844 :::* 1628/dhclient
udp6 0 0 :::54654 :::* 2214/avahi-daemon: udp6 0 0 :::908 :::* 1582/rpcbind

【问题讨论】:

    标签: c sockets tcp dns udp


    【解决方案1】:

    类似的修改(在recvfrom() 之后添加 printf)对我来说效果很好。除了打印之外,您对程序是否进行了任何其他更改?

    这些是我测试它的步骤:

    1. git 克隆仓库
    2. 将 printfs 添加到源代码
    3. 制作
    4. 编辑 dns_proxy.conf 以记录 /dev/null 以外的其他位置
    5. 在另一个终端,ssh someuser@a.box.somewhere -D localhost:9050
    6. sudo ./dns_proxy
    7. 测试:host ftp.funet.fi

    顺便说一句。在您建议的位置添加printf() 会在您运行其他应用程序(如 www 浏览器或电子邮件客户端)的桌面上产生大量输出,因此请小心。也许您可以使用其余源使用的日志记录约定,例如

    if (LOG == 1) { fprintf(LOG_FILE, "Using DNS server: %s\n", inet_ntoa(*(struct in_addr *)&remote_dns)); }
    

    顺便说一句。如果您已手动创建或编辑它,请记住在运行 dns_proxy 之前备份您的 /etc/resolv.conf。使用 tailf "your_dns_proxy_logfile.log" 看看发生了什么。

    顺便说一句。该程序不是很健壮。它泄漏 fds,string_value() 中有一个非一,udp_listener() 执行malloc()memcpy() 而不检查recvfrom() 的返回值。在我的机器上,它有很多段错误。不过,似乎只是勉强发挥作用。

    编辑以下是我对原始版本所做的一些更改。 https://github.com/thuovila/dns-tcp-socks-proxy/ 在这些修改之后,它不会为每个中断的recvfrom() 进行段错误。更改已合并到上游存储库。

    【讨论】:

    • 我不完全理解第 5 步。你能解释一下吗?这个命令到底做了什么?
    • 我想我现在明白了。我一步一步地按照你的指示做了,我的问题就解决了。 dns_proxy 运行良好。
    • 好的,太好了。很高兴我能帮助你。它可能会进一步优化,但现在似乎可以正常工作。
    【解决方案2】:

    可能一开始就没有数据可以接收。您看不到“dns request received\n”这一事实意味着 recvfrom() 调用处于阻塞状态,等待接收数据。您需要调查发件人是否正在发送数据。接下来,您可能应该重新检查 sock 是否绑定在正确的端口上。您可能希望共享发件人的代码以及发生绑定的 recvfrom 代码。

    【讨论】:

    • 我打开浏览器并输入一些网址,例如 www.yahoo.com 这意味着我发送了请求,但我在命令行中看不到任何内容
    • 您可能需要进行一些调试。如果您运行的是 Linux/Windows,您能否使用“netstat -upan”检查输出的内容?在 Linux 上,它会告诉你所有打开的 UDP 端口,你应该看到 DNS 就是其中之一。如果可行,那么下一步就是使用数据包调试器(如 tcpdump/windump)来查看 UDP 数据包是否真的发送到您的服务器。
    • 我已经编辑了问题并添加了 netstat -upan 命令的输出
    • 酷! dns_proxy 似乎正在运行:“udp 0 0 127.0.0.1:53 0.0.0.0:* 4084/dns_proxy”。这与 LISTEN_PORT(值 53)相同。困扰我的是还有另一个 DNS UDP 连接:“udp 0 0 0.0.0.0:68 0.0.0.0:* 1628/dhclient” 对于这个,DHCP 服务器位于端口 68。你的 DNS 请求是否服务器位于 68 端口?您是否在浏览器中解析了 URL?此时,我建议在 DNS 请求中执行 tcpdump 并验证 hte 端口号。
    • dns_proxy 会覆盖 /etc/resolv.conf,因此任何使用普通 dns 解析器库的应用程序都将恢复为使用 UDP localhost:53。 avahi 守护程序侦听 mdns。根据 /etc/nsswitch.conf 中定义的顺序,在常规 dns 查找之前或之后完成,但在任何一种情况下,mdns 中不存在的任何内容都应从 dns_proxy 解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2016-01-29
    • 1970-01-01
    • 2021-10-10
    • 2018-11-14
    • 2021-12-17
    • 2011-05-04
    相关资源
    最近更新 更多