nieluj / nntp-proxy

simple NNTP proxy with SSL support
Other
58 stars 25 forks source link

UDP usage #10

Closed nomandera closed 11 years ago

nomandera commented 11 years ago

Can the docs be updated to explain UDP usage:

netstat -lnptu | grep nntp-proxy tcp 0 0 0.0.0.0:119 0.0.0.0:* LISTEN 7552/nntp-proxy udp 0 0 0.0.0.0:39051 0.0.0.0:* 7552/nntp-proxy udp 0 0 0.0.0.0:51992 0.0.0.0:* 7552/nntp-proxy udp 0 0 0.0.0.0:45871 0.0.0.0:* 7552/nntp-proxy

lkmikkel commented 11 years ago

Hmm... nntp-proxy doesn't use UDP weird?? maybe some libevent voodo?

lkmikkel commented 11 years ago

libevent before version 2.1 is using UDP socket to detect IPv6 WTF see 1.7 https://raw.github.com/libevent/libevent/master/whatsnew-2.1.txt

nieluj commented 11 years ago

The nntp-proxy does not explicitly open an UDP socket. The bufferevent_socket_connect_hostname function will open such a socket when contacting a DNS server during a hostname resolution.

Using strace, you can observe the following behaviour:

open("/etc/resolv.conf", O_RDONLY|O_CLOEXEC) = 6
fstat64(6, {st_mode=S_IFREG|0644, st_size=71, ...}) = 0
read(6, "nameserver 127.0.0.1\nnameserver "..., 71) = 71
close(6)                                = 0
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 6
fcntl64(6, F_GETFD)                     = 0
fcntl64(6, F_SETFD, FD_CLOEXEC)         = 0
fcntl64(6, F_GETFL)                     = 0x2 (flags O_RDWR)
fcntl64(6, F_SETFL, O_RDWR|O_NONBLOCK)  = 0
epoll_ctl(3, EPOLL_CTL_ADD, 6, {EPOLLIN, {u32=6, u64=6}}) = 0
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 7
fcntl64(7, F_GETFD)                     = 0
fcntl64(7, F_SETFD, FD_CLOEXEC)         = 0
fcntl64(7, F_GETFL)                     = 0x2 (flags O_RDWR)
fcntl64(7, F_SETFL, O_RDWR|O_NONBLOCK)  = 0
epoll_ctl(3, EPOLL_CTL_ADD, 7, {EPOLLIN, {u32=7, u64=7}}) = 0
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 8
fcntl64(8, F_GETFD)                     = 0
fcntl64(8, F_SETFD, FD_CLOEXEC)         = 0
fcntl64(8, F_GETFL)                     = 0x2 (flags O_RDWR)
fcntl64(8, F_SETFL, O_RDWR|O_NONBLOCK)  = 0
epoll_ctl(3, EPOLL_CTL_ADD, 8, {EPOLLIN, {u32=8, u64=8}}) = 0

So I suppose that libevent opens a SOCK_DGRAM socket for each DNS server in /etc/resolv.conf, then calls epoll on those sockets for providing asynchronous DNS resolution.

You can try to comment one of the three servers in /etc/resolv.conf, restart nntp-proxy and see if the number of opened UDP sockets has diminished.

nomandera commented 11 years ago

So I went the other way and added a 4th nameserver to /etc/resolv.conf.

This seems to confirm the proposed answer:

netstat -lnptu | grep nntp-proxy | grep udp udp 0 0 0.0.0.0:53134 0.0.0.0:* 32614/nntp-proxy udp 0 0 0.0.0.0:56476 0.0.0.0:* 32614/nntp-proxy udp 0 0 0.0.0.0:36920 0.0.0.0:* 32614/nntp-proxy udp 0 0 0.0.0.0:36569 0.0.0.0:* 32614/nntp-proxy