2018年5月21日 星期一

研究 | IP Version 6 over PPP (RFC 5072)

IPV6CP (IPv6 Control Protocol)

用途:
     溝通並設定 PPPoE 通道上,兩端裝置的 link-local IPv6 address。

程序:

1. 首先建立 PPPoE 通道,兩端裝置透過 PPPoE 建立一個 session。完成底下四個步驟後(PADI/PADO/PADR/PADS),PPPoE client 會從 PPPoE server 取得一個 session ID。

      client       server

 (1)   PADI ----->

 (2)        <----- PADO

 (3)   PADR ----->

 (4)        <----- PADS

 說明:
      PADI (PPPoE Active Discovery Initiation) 是 broadcast
      PADO (PPPoE Active Discovery Offer)
      PADR (PPPoE Active Discovery Request)
      PADS (PPPoE Active Discovery Session-confirmation)

2. 再透過 LCP 來協議雙方的 MRU, magic number 與認證協定(PAP, CHAP)等資訊。

說明:
     LCP  (Link Control Protocol)
     MRU  (Maximum Receive Unit)
     PAP  (Password Authentication Protocol)
     CHAP (Challenge Handshake Authentication Protocol)

3. PPPoE client 用協議後的認證協定(PAP, CHAP)與 PPPoE server 進行認證。

4. 雙方使用 IPV6CP,協議 link-local IPv6 address 的 interface identifier。雙方各自產生 64-bit interface identifier 傳給對方互相確認,最後將 link-local prefix(fe80) 加上協議後的 64-bit interface identifier 各自產生自己的 link-local IPv6 address。

說明:
     64-bit interface identifier 可以用底下三種方式產生
     a. EUI-48 or EUI-64
     b. 其他具有唯一性的來源 ex: serial number
     c. 隨機號碼 (random number)

2018年5月8日 星期二

Bash Shell | 如何在 script 中引入 include 其它 script

當 script 越寫越複雜後,會想要把共用 function 獨立寫在一個 script,再利用像 C 語言的 #include 方式來引入,此時可以使用 builtin command source 來達成,source 也可以用同義符號 ( . ) 替代。

source filename

or

. filename


底下是一個會將參數累加起來的 function

範例:

共用 script (func.sh)

#!/bin/sh

sum()
{
        total=0
        count=0

        echo The PID of sum\(\) = $$

        if [ "$#" -lt "2" ]; then
                echo "The number of arguments should larger than 1."
                return 255;
        fi

        for i in $@
        do
                ((total += i))
                ((count += 1))

                echo -n $i

                if [ "$count" -lt "$#" ]; then
                        echo -n " + "
                fi
        done

        echo " = $total"

        return $total

}

測試 script (test.sh)

#!/bin/sh

source ./func.sh

echo The PID of $0 = $$
echo ""

sum 1 2
echo The return of sum\(\) = $?
echo ""

sum 3 4 5
echo The return of sum\(\) = $?
echo ""

sum 6
echo The return of sum\(\) = $?

結果:


The PID of ./test.sh = 2075

The PID of sum() = 2075
1 + 2 = 3
The return of sum() = 3

The PID of sum() = 2075
3 + 4 + 5 = 12
The return of sum() = 12

The PID of sum() = 2075
The number of arguments should larger than 1.
The return of sum() = 255

說明:

test.sh 在開頭使用 source 引入 func.sh 後呼叫了三次 sum(),從 PID 可以看出,sum() 是由執行 test.sh 的 process 所執行。