Very nice explanation.  Your two well-chosen examples clarified how
rotation works beautifully.

Thanks, Brian.

Regards,
- Robert

On Tue, Sep 11, 2012 at 10:03 PM, Brian Pitts <[email protected]> wrote:
> Host and dig are a little tricky to run under strace, since they fork
> several times. I might try a simple python program like this.
>
> #!/usr/bin/env python
> import socket
> for x in range(4):
>     socket.getaddrinfo('google.com', 80)
>
> Now, if I trace the connect system call and filter only for port 53,
> I'll see
>
> strace -e trace=connect ./lookup_google.py 2>&1 | grep 53
>
> connect(4, {sa_family=AF_INET, sin_port=htons(53),
> sin_addr=inet_addr("208.67.220.220")}, 16) = 0
> connect(4, {sa_family=AF_INET, sin_port=htons(53),
> sin_addr=inet_addr("208.67.222.222")}, 16) = 0
> connect(4, {sa_family=AF_INET, sin_port=htons(53),
> sin_addr=inet_addr("208.67.220.220")}, 16) = 0
> connect(4, {sa_family=AF_INET, sin_port=htons(53),
> sin_addr=inet_addr("208.67.222.222")}, 16) =
>
> (Instead of using strace, I could have used tshark or tcpdump to watch
> the traffic, e.g. tshark -i wlan0 'udp port 53')
>
> Which is what I would expect when my resolv.conf is
>
> options rotate
> nameserver 208.67.222.222
> nameserver 208.67.220.22
>
> However, what if I change my program to just
>
> #!/usr/bin/env python
> import socket
> socket.getaddrinfo('google.com', 80)
>
> And run it four times?
>
> for i in $(seq 1 4); do strace -e trace=connect ./lookup_google.py 2>&1
> | grep 53; done
>
> connect(4, {sa_family=AF_INET, sin_port=htons(53),
> sin_addr=inet_addr("208.67.220.220")}, 16) = 0
> connect(4, {sa_family=AF_INET, sin_port=htons(53),
> sin_addr=inet_addr("208.67.220.220")}, 16) = 0
> connect(4, {sa_family=AF_INET, sin_port=htons(53),
> sin_addr=inet_addr("208.67.220.220")}, 16) = 0
> connect(4, {sa_family=AF_INET, sin_port=htons(53),
> sin_addr=inet_addr("208.67.220.220")}, 16) = 0
>
> Now you see that it always hits the first nameserver. The rotation only
> takes place for multiple lookups within a single program. That makes
> sense when you think about it. There's not some central program the all
> the programs on your system are talking to to make these lookups (unless
> you're using nscd); instead, each program is loading and executing
> library function independently.

-- 
Central West End Linux Users Group (via Google Groups)
Main page: http://www.cwelug.org
To post: [email protected]
To subscribe: [email protected]
To unsubscribe: [email protected]
More options: http://groups.google.com/group/cwelug

Reply via email to