繁体   English   中英

recvfrom()函数不起作用

[英]recvfrom() function does not work

我有开源鳕鱼(这是它的链接)在鳕鱼的这一部分访问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命令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守护程序: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 / dh客户端
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 /杯
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

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

这些是我测试的步骤:

  1. git克隆仓库
  2. 将printfs添加到源
  3. 使
  4. 编辑dns_proxy.conf以记录/ dev / null以外的其他地方
  5. 在另一个终端中, ssh someuser@a.box.somewhere -D localhost:9050
  6. 须藤./dns_proxy
  7. 测试: 主机ftp.funet.fi

顺便说一句。 在您建议的地方添加printf()会在桌面上产生很多输出,而桌面上还有其他应用程序正在运行,例如www-browser或电子邮件客户端,因此请当心。 也许您可以使用其余源使用的日志记录约定,例如

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

BTW2。 如果您手动创建或编辑了dns_proxy,请记住在运行dns_proxy之前先备份/etc/resolv.conf。 使用tailf“ your_dns_proxy_logfile.log”查看最新情况。

顺便说一句3。 该程序不是很健壮。 它将泄漏fds, string_value()存在一个不string_value() ,并且udp_listener()执行malloc()memcpy()而不检查recvfrom()的返回值。 在我的机器上,它经常出现段错误。 似乎只是勉强起作用。

编辑这是我对原件所做的一些更改。 经过这些修改后,对于每个中断的recvfrom()它都不会进行段错误处理( https://github.com/thuovila/dns-tcp-socks-proxy/ recvfrom() 更改已合并到上游存储库中。

首先可能没有数据要接收。 您看不到“已收到dns请求\\ n”这一事实,这意味着recvfrom()调用处于阻止状态,等待接收数据。 您将需要调查发件人是否正在发送数据。 接下来,您可能应该重新检查袜子是否绑定在正确的端口上。 您可能想共享发件人的代码以及发生绑定的recvfrom代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM