New FAMILUG

The PyMiers

Monday 9 June 2014

[network] ping - pong

Vô tình hôm nay hàm "command of today" của tớ trả về kết quả cho lệnh ping, và nó đặt ngay câu hỏi cho tớ rằng "ping là gì?", có chắc bạn sẽ trả lời đúng như những gì lệnh whatis trả về?

$ whatis ping
ping (8)             - send ICMP ECHO_REQUEST to network hosts

Bài viết thực hiện trên:
# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04.4 LTS
Release:        12.04
Codename:       precise
Hãy nhìn thật kỹ và phân tích từng dòng output:

$ping dantri.com -c1
PING dantri.com (222.255.27.51) 56(84) bytes of data.
64 bytes from static.vdc.vn (222.255.27.51): icmp_req=1 ttl=55 time=251 ms

--- dantri.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 251.466/251.466/251.466/0.000 ms

-c1 là option nói rằng chỉ gửi 1 ICMP ECHO_REQUEST duy nhất, nếu không có option này ping sẽ gửi lần lượt, liên tục cho đến khi nó nhận được SIGINT (user bấm Ctrl C)

Dòng output đầu tiên

PING dantri.com (222.255.27.51) 56(84) bytes of data.
dantri.com : domain mà lệnh trên gửi ICMP ECHO_REQUEST đến
222.255.27.51: địa chỉ IP nhận được sau khi phân giải domain (resolve domain name)
56: packetsize. Ở đây là 56 bytes data
84: size thực của package gửi đi = 56 (data) + 20 (IP header without options) + 8 (ICMP header) = 84

Dòng thứ 2
64 bytes from static.vdc.vn (222.255.27.51): icmp_req=1 ttl=55 time=251 ms
64: bytes nhận được. 64 = 56 + 8
icmp_req=1: số thứ tự của packet nhận được
ttl=55: IP Time to Live, là 1 thuộc tính của IP packet biểu diễn số lượng IP router tối đa mà mà packet này có thể đi qua trước khi bị bỏ đi. Như vậy ICMP packet này có thể đi qua 55 router NỮA (đây là TTL của packet gửi từ  máy đích (dantri.com) gửi tới máy mình, còn TTL cho packet mình gửi đi được chỉ định qua option -t NUMBER).(Đọc thêm về TTL ở cuối bài)

time=251ms: thời gian từ lúc gửi ICMP ECHO_REQUEST đến khi nhận được ICMP ECHO_RESPONSE tương ứng.

Dòng tổng kết (summary)
--- dantri.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 251.466/251.466/251.466/0.000 ms  

1 packet đã transmitted (chuyển đi), 1 packet received (nhận được), 0% packet bị mất, tổng thời gian: 0 ms

rtt (Round trip time) giá trị min/avg(Trung bình)/max/mdev(moving standard deviation) : RTT là thời gian cả đi lẫn về của packet. Xem phần dưới để đọc giải thích rõ hơn.

Chi tiết về các khái niệm:
1. RTT
Round-trip time (RTT) is the time it takes for a client to send a request and the server to send a response over the network, not including the time required for data transfer. That is, it includes the back-and-forth time on the wire, but excludes the time to fully download the transferred bytes (and is therefore unrelated to bandwidth). For example, for a browser to initiate a first-time connection with a web server, it must incur a minimum of 3 RTTs: 1 RTT for DNS name resolution; 1 RTT for TCP connection setup; and 1 RTT for the HTTP request and first byte of the HTTP response. Many web pages require dozens of RTTs.

https://developers.google.com/speed/docs/best-practices/rtt

2. mdev
mdev is the moving standard deviation, sometimes also abbreviated "MSTD". It's not possible to calculate the true standard deviation until all the data points have been collected. A moving number is calculated on known data only.

http://serverfault.com/questions/333116/what-does-mdev-mean-in-ping8

3. ICMP packet

ICMP PACKET DETAILS
       An IP header without options is 20 bytes.  An ICMP ECHO_REQUEST packet contains an additional 8 bytes worth of ICMP header followed by an arbitrary amount of data.  When a  packetsize  is given, this indicated the size of this extra piece of data (the default is 56). Thus the amount of data received inside of an IP packet of type ICMP ECHO_REPLY will always be 8 bytes more than the requested data space (the ICMP header).

       -s packetsize
              Specifies the number of data bytes to be sent.  The default is 56, which translates into 64 ICMP data bytes when combined with the 8 bytes of ICMP header data.

from man 1 ping

4. TTL (Time to Live)

Mục đích của TTL:
The purpose of the TTL field is to avoid a situation in which an undeliverable datagram keeps circulating on an Internet system, and such a system eventually becoming swamped by such "immortals".
http://en.wikipedia.org/wiki/Time_to_live#IP_packets     

Giải thích về giá trị TTL trên các OS:

Different operating systems send replies to pings with different TTL (it doesn't depend upon the TTL in the incoming ping). Common starting values include 64, 128 and 255. I.e.

http://serverfault.com/questions/191780/differences-of-the-ttl-value-in-tracert-and-ping

Tìm TTL mặc định của máy mình:
# ping -c1 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.022 ms
...
# ping -c1 -t 234 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.030 ms
# TTL của packet gửi đi là 234, nhưng TTL của packet trả về vẫn mặc định là giá trị 64
Máy này (và hầu hết các Linux OS) đều có TTL mặc định là 64.

Giá trị TTL của packet gửi đi từ máy mình KHÔNG LIÊN QUAN tới giá trị TTL trả về từ máy đích:

Bing.com rõ ràng là phải dùng Windows server và nó có TTL mặc định là 128
# ping -c1 bing.com
PING bing.com (204.79.197.200) 56(84) bytes of data.
64 bytes from a-0001.a-msedge.net (204.79.197.200): icmp_req=1 ttl=121 time=0.775 ms
...
# ping -c1 -t234 bing.com # đặt TTL lên 234 nhưng giá trị TTL packet nhận được không đổi
PING bing.com (204.79.197.200) 56(84) bytes of data.
64 bytes from a-0001.a-msedge.net (204.79.197.200): icmp_req=1 ttl=121 time=0.729 ms
như vậy packet ICMP_RESPONSE từ máy chủ bing.com đã đi qua 9 hop để tới được máy mình (128 - 121 = 9)

The  TTL  value of an IP packet represents the maximum number of IP routers that the packet can go through before being thrown away.  In current practice you can expect each router in the Internet to decrement the TTL field by exactly one.

       The TCP/IP specification states that the TTL field for TCP packets should be set to 60, but many systems use smaller values (4.3 BSD uses 30, 4.2 used 15).

       The maximum possible value of this field is 255, and most Unix systems set the TTL field of ICMP ECHO_REQUEST packets to 255.  This is why you will find you can ``ping'' some  hosts,  but not reach them with telnet(1) or ftp(1).
man 1 ping 


Fun
Ping - pong: theo lịch sử, khi 1 người gửi đinh tính hiệu ping, bên nhận được sẽ trả lại tín hiệu pong. :3 chém gió thế.

Hết.

5 comments:

  1. Như vậy ICMP packet này có thể đi qua 55 router --> Vẫn còn có thể đi quả 55 router nữa. ( đây là kết quả đã trừ đi số router nó đi qua từ host đang ping đến mày mình)

    ReplyDelete
    Replies
    1. ok, thanks Hiệp, sẽ sửa lại đoạn này.

      Delete
    2. đã update lại nội dung và thêm phần giải thích chi tiết về TTL

      Delete
  2. đọc thêm về TTL
    http://www.quora.com/Could-someone-explain-time-to-live-TTL-which-is-an-output-of-ping-commands
    Default TTL
    www.binbert.com/blog/2009/12/default-time-to-live-ttl-values/

    ReplyDelete
  3. rất có thể bạn chưa biết:
    người dùng phải có quyền root mới có thể gửi đi ICMP ECHO_REQUEST,
    lý do ai cũng ping được bởi chương trình ping được setuid

    # ls -la `which ping`
    -rwsr-xr-x 1 root root 34740 Nov 8 2011 /bin/ping

    ping được setuid (chữ s thay cho chữ x ở phần permission cho owner - root)
    Khi chạy ping, user sẽ được tạm thời leo thang (elevate) lên quyền root để thực thi câu lệnh.

    The setuid permission bit tells Linux to run a program with the effective user id of the owner instead of the executor

    http://unix.stackexchange.com/questions/364/allow-setuid-on-shell-scripts?rq=1
    http://en.wikipedia.org/wiki/Setuid
    http://www.cyberciti.biz/faq/unix-bsd-linux-setuid-file/

    ReplyDelete