星期六, 10月 31, 2020

SIP REFER

SIP Method REFER 透過 Refer-To 提供第三方資訊給對方去聯繫,並隱含訂閱 refer Event 回報是否聯繫成功 (Event subscriptions 定義在 RFC3265)。在 INVITE Dialog 外也可使用,會建立一個 Dialog。

應用

Call Transfer。例如 A 和 B 通話中,判斷需要和 C 談,所以送 REFER 給 B 提供 C 的 Contact 資訊。B 允許下嘗試用來呼叫 C。B 將會回報是否聯繫成功給 A。

Refer-To (r) 信頭欄位

REFER 請求必須要 Refer-To,Refer-To 只用在 REFER 請求,提供參照的 URL,可加密。由於 Contact 是 Route/Record-Route 機制的重要部份,不能用來表示參照的 URL。

格式

( name-addr / addr-spec ) * (SEMI generic-param)

範例

Refer-To: sip:alice@atlanta.example.com

Refer-To: <sip:bob@biloxi.example.net?Accept-Contact=sip:bobsdesk.
       biloxi.example.net&Call-ID%3D55432%40alicepc.atlanta.example.com>

Refer-To: <sip:dave@denver.example.org?Replaces=12345%40192.168.118.3%3B
          to-tag%3D12345%3Bfrom-tag%3D5FFE-3994>

Refer-To: <sip:carol@cleveland.example.org;method=SUBSCRIBE>

Refer-To: http://www.ietf.org

REFER 請求處理

  • 回 400 Bad Request 如果沒有 Refer-To 或多於一個 Refer-To。
  • Proxy 或 UA 可回 100 Trying 或適當的 4xx ~ 6xx。
  • 接受回 202 Accepted,並訂閱 refer,馬上 NOTIFY 回報。
    • 聯繫 Refer-To 提供的 URL,例如有參數 method=INVITE 用 INVITE。
    • NOTIFY 回報使用和 REFER 相同的 Dialog。Event 是 refer,可能有 id 參數放 REFER CSeq 序號區別多個 REFER。含信體 message/sipfrag (RFC3240)。一開始 Subscription-State expires 參數告知 duration,也可能告知停刊。後續發 REFER 者可停訂或續訂 refer。
    • 信體基本要有 SIP 回應狀態行,可包含其它 SIP 信頭欄位。
      • SIP/2.0 100 Trying (聯繫進行中)
      • SIP/2.0 200 OK (聯繫成功)
      • SIP/2.0 503 Service Unavailable (聯繫失敗)
      • SIP/2.0 603 Declined (參照失敗)

其它

只有 REFER 能訂閱 refer

refer event package

RFC3515 2.4.7 以後未看

參考

  1. RFC3515 The SIP Refer Method

星期日, 10月 25, 2020

SIP OPTIONS

SIP Method,可用在 Dialog 內或 Dialog 外。RFC3261 §11

UA 註冊成功後,約每分鐘送 SIP OPTIONS 確認 UA 是否還在,不在一直確認到註冊逾時。

星期六, 10月 24, 2020

星期日, 10月 18, 2020

line buffer

stream 有 3 種緩衝方式 -- unbuffered、block buffered、和 line buffered:
  • block buffered:一般檔案採用的方式,以 block 方式 (滿 block?) 寫入。檔案頭次 I/O 操作會 malloc() 取得緩衝區。
  • line buffered:一般終端機的輸入 (stdin) 或輸出 (stdout) 採用的方式,遇到換行傳送。
  • unbuffered:沒緩衝、馬上輸出。stderr 預設是 unbuffered。
改變緩衝方式。
int setvbuf(FILE *stream, char *buf, int mode, size_t size);

mode 是 _IONBF (unbuffered)、_IOLBF (line buffered)、和 _IOFBF (fully buffered,指 block buffer?) 之一。除了 unbuffered,size 大小的 buf 取代原本的緩衝區 (會釋出?)。如果 buf 是 NULL,在下次讀寫操作時自動配置新的緩衝區。setvbuf() 可能只能在 stream 任何其它操作之前使用。

注意:需要確認 stream 關閉時,buffer 是可用的。

void setbuf(FILE *stream, char *buf); 相當於 setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);

void setbuffer(FILE *stream, char *buf, size_t size); 相當於 setvbuf(stream, buf, buf ? _IOFBF : _IONBF, size);

void setlinebuf(FILE *stream); 相當於 setvbuf(stream, NULL, _IOLBF, 0);

fflush() 強迫輸出。 (見 fclose().)

註:開啟 file stream
#include <stdio.h>
fopen(), fdopen(), freopen

皆會用到 mode,可能是 r、r+、w、w+、a、a+。為了相容,都可以加上 b。

fopen():開啟檔名
fdopen():開啟 file descriptor
freopen():fclose() 再 fopen()?用來改變 stdin、stdout、stderr 使用的 file。

參考

  1. https://linux.die.net/man/3/setlinebuf
  2. https://jenyiw.pixnet.net/blog/post/9391777https://jenyiw.pixnet.net/blog/post/9391585:fwrite() 是 library 函數,使用緩衝區減少頻繁呼叫 syscall write(),在多次少量時效率會比直接用 write() 好。但 fwrite() 多了一次緩衝讀寫,在一次多量效率就會比 write() 差。
  3. 相關函數:fseek()、fgetpos()、open()、fclose()、fileno()、fmemopen()、fopencookie()。
  4. https://man7.org/linux/man-pages/man3/termios.3.html
  5. https://blog.xuite.net/uwlib_mud/twblog/108242774
  6. http://lwn.net/images/pdf/LDD3/ch18.pdf
  7. termios
  8. 在 canonical 模式 (正規模式),終端設備會處理特殊字元,一次一行的方式輸入給應用程式。例如 Linux 的 shell指令。非正規模式終端設備不會處理特殊字元,一次一個字元的方式輸入給應用程式。
  9. tty 名稱來自 teletypewriter 的縮寫,用來表示任何序列埠樣式的裝置,,可能是實體的裝置如 serial ports、USB-to-serial-port converters、一些需要特殊處理的數據機,或者是虛擬裝置用來登入。

星期六, 10月 17, 2020

Switch Abstraction Interface (SAI) 交換機抽象層介面

開放運算計畫 Open Compute Project (OCP) 制訂的網路交換機ASIC編程的開放標準 C API,能使同一套軟體堆疊執行於不同廠牌交換機硬體上。

Azure Cloud Switch (ACS):Microsoft 專為資料中心網路互通而設計的Linux版作業系統

PWM

 Pulse Width Modulation

星期六, 10月 10, 2020

Precision Time Protocol

精確時間協定 (Precision Time Protocol, PTP) 透過網路封包同步絕對時間、頻率、和相位,達到毫秒級精確度。

版本
  • IEEE 1588-2002 (PTPv1):未廣泛採用。
  • IEEE 1588-2008 (PTPv2):沒向前相容。有 profile 定義 PTP 運作參數和選項,已定義的 profile 有 telecommunications、electric power distribution 和 audiovisual。IEEE 802.1AS (gPTP) 是 adaptation PTP 和 Audio Video Bridging and Time-Sensitive Networking 使用。
  • IEEE 1588-2019 (PTPv2.1) 相容 2008 改善。

名詞

  • master 和 slave:
  • grandmaster:master 作為同步源頭
  • ordinary clock:單一網路界面設備,可以是 master 或 slave。
  • boundary clock (BC、邊界時鐘):多網路界面設備,其中一個作為 PTP slave,其它則為 master。
  • Transparent clock (TC、透明時鐘):多網路界面設備,如同一般 switch 或 router 轉送所有封包,但不作為 master 或 slave。有兩種:
    • end-to-end (端對端):額外累算 event 封包停留時間更新 correction 欄位。
    • peer-to-peer (點對點):只更新 Sync 和 Follow Up 的 correction 欄位,除了累算 event 封包停留時間,再減去 Link propagation delay。
封包

PTP Message

34-byte HeaderVariable-length BodyOptional Suffix
PTP Header
OffsetOctetsField說明
01transportSpecificmessageType
11ReservedversionPTP
22messageLength長度含 Header、Body、和 Suffix。
41domainNumber
51Reserved
62flags
88correlationField
164Reserved
2010sourcePortIdentity
302sequenceID
321controlField
331logMessageInterval
messageType
  • event:傳送和接收時紀錄精確的 timestamp,包括
    • Sync:Master 送 orignalTimestamp (T1),用在 ordinary 和邊界時鐘。
    • DelayReq:Slave 送 orignalTimestamp,用在 ordinary 和邊界時鐘。
    • PDelay Req:用在透明時鐘。
    • PDelay Resp:用在透明時鐘。
  • general:不需要紀錄 timestamp,但可能傳送 timestamp,包括
    • FollowUp:2-step 時,Master 送 orignalTimestamp,用在 ordinary 和邊界時鐘。
    • Delay Resp:Master 送 DelayReq 的 receiveTimestamp (T4) 及 requestingPortIdentify,用在 ordinary 和邊界時鐘。
    • PDelay Resp Follow Up:用在透明時鐘。
    • Announce:用在 Best Master Clock Algorithm 來建立 clock 階層和選擇 grandmaster。
    • Management:網管使用來 monitor, configure and maintain PTP 系統。
    • Signaling:clock 間非 time-critical 通訊。

sourcePortIndentity

Announce

Sync/DelayReq/FollowUp

DelayResp/PDelayReq/PDelayResp/PDelayRespFollowUp

  • UDP:Multicast address 224.0.1.129~224.0.1.132,UDP Port 319 for Event message,UDP Port 320 for general message
  • Ethernet
  • 使用 TAI Time:1970/1/1 午夜開始,48-bit Seconds 32-bit Nanoseconds => overflow in 8‘925‘512 years
  • 提供 UTC offset 和 Leap second 轉換 TAI 為 UTC。
Best Master Clock Algorithm (BMCA):決定 Port state 是 Master、Slave、或 Passive,和 Clock quality 最好的 node 送 «Announce» 訊息,自動建立 Master-Slave 階層:
  • 所有 node 聽 «Announce» 訊息,包含 Clock quality 資訊 (Class, Priorities and Qualities)。
  • 如果在時間內沒收到 «Announce» 訊息,自己變成 Master 並開始送 «Announce» 訊息。
  • 如果收到 «Announce» 訊息的 Clock quality 比自己好,停送 «Announce» 並變為 Slave。
  • 如果收到 «Announce» 訊息的 Clock quality 比自己差,持續在 Master 並繼續週期送 «Announce»。
Clock quality 的比較
  1. Priority1: 設定的 clock priority
  2. ClockClass: Clock 的 traceability
  3. ClockAccuracy: Clock 的 accuracy (跟網路 layer 有關?)
  4. OffsetScaledLogVariance: Clock 的 stability
  5. Priority2: 設定的 second order clock priority
  6. ClockIdentity: Clock 的 unique identifier
此外,node 在多個 port 收到 «Announce» 訊息時,需要決定一個 port
  1. StepsRemoved: over how many hops the frame came
  2. SenderPortIdentity: sender port 的 unique identifier
  3. ReceiverPortIdentity : receiver port 的 unique identifier
PTP Timestamps
  • 精確度依靠 timestamps,越高網路階層越差。PTP timestamp 在 Ethernet 是取網路線的 Start of Frame Delimiter (SFD),大多實作在 MAC 和 PHY 間取,需要硬體支援,需要補償 PHY 的收送延遲。
  • 調整頻率:
    • T1: Master 週期傳送 Sync 的時間。T1 透過 Sync 或 Follow Up 告知 Slave。如果 Sync 傳送時還不知道真正傳送時間,才用 Follow Up 送,稱為 2-Step。
    • T2: Slave 紀錄接收 Sync 的時間。
    • Slave 調整時間,讓 T2 和 T1 的變化速度一致,達到頻率同步。Drift = ((T2'-T2) - (T1'-T1))/(T1'-T1)
  • 調整相位:需要知道傳輸延遲,假設傳輸是對稱的,
    • E2E:頻率同步後
      • T3: Slave 傳送 Delay Request 的時間,收到 Sync 後不久傳送。Slave 只是紀錄 T3,Delay Request 並不含 T3 欄位。
      • T4: Master 接收 Delay Request 的時間。T4 透過 Delay Response 告知 Slave
      • Delay = (T2 - T1 + T4 - T3)/2
    • P2P:可用在頻率不同步時
      • T3:Slave 傳送 «PDelayReq» 的時間。
      • T4:Master 收到 «PDelayReq» 的時間,透過 PDelayResp 告知 Slave。
      • T5:Master 傳送 «PDelayResp» 的時間,透過 PDelayResp 或 PDelayRespFollowUp 告知 Slave。
      • T6:Slave 收到 PDelayResp 的時間。
      • Delay = (T4 - T3 + T6 - T5)/2
    • 相位差異是 Offset = T2 - T1 - Delay。
  • Delaym→s + Offset = T2 - T1, Delays→m - Offset = T4 - T3,理想狀況下 Delay = Delaym→s = Delays→m = (T2 - T1 + T4 -T3)/2,可算出 Offset = T2 - T1 - Delay。
  • Delay 來自硬體和軟體,包括 Link delay、serialization delay、queuing、protocol stack 等。Delay 的不對稱和變化、以及時脈產生器的不穩定,會影響同步精確度,。

Topology changes are considered every «Announce» interval•Offset and drift are adjusted every «Sync» interval•Delays are calculated every «Delay» interval•«Announce», «Sync» and «Delay» intervals don’t have to be (and often are not) the same

參考
  1. http://ip-clock.com/ieee-1588-primer/
  2. https://www.nettimelogic.com/resources/PTP%20Basics.pdf
  3. http://www.chronos.co.uk/files/pdfs/cal/TechnicalBrief-IEEE1588v2PTP.pdf 
  4. https://www.ieee802.org/1/files/public/docs2008/as-garner-1588v2-summary-0908.pdf
  5. EDS-405A-PTP 是專為即時控制應用而設計的 5 埠IEEE 1588v2 PTP 交換器,搭載硬體設計時間戳記,提供精準的網路時間同步,同時支援邊界時鐘及透明時鐘,透明時鐘支援端對端 (2步驟) 及點對點 (2步驟) 模式,高精確度時間 (誤差小於1 μs),支援 Modbus TCP、PROFINET RT 和 EtherNet/IP,提供更好的 SCADA 整合。
    • Clock Mode: E2E 2-step TC、P2P 2-step TC、E2E BC、P2P BC
    • synchronization message time interval: 128 ms, 256 ms, 512 ms, 1s, or 2 s.
    • announce message interval: 1s, 2s, 4s, 8s, 16s
    • The multiple of announce message receipt timeout by the announce message interval:2, 3, 4, 5, 6, 7, 8, 9, or 10
    • ...
  6. https://lirobo.blogspot.com/2014/09/am335x-common-platform-time-sync.html

星期六, 10月 03, 2020

RADIUS 遠端用戶撥入驗證服務

Remote Authentication Dial-In User Service (RADIUS) 是主從式網路應用協定,提供用戶使用網路服務集中式認證、授權、和記帳 (Authentication, Authorization, and Accounting, AAA) 管理,定義在 RFC2865RFC2866

Network Access Server (NAS)

也就是 RADIUS Client,負責 passing 用戶認證和記帳資訊給 RADIUS Server 或 Accounting Server。
RADIUS 流程
                                        +---------+  +------------+
+------+                +-----+         | RADIUS  |  |   RADIUS   |
| User |                | NAS |         |  Server |  | Accounting |
+--+---+                +--+--+         +----+----+  +------+-----+
   | 連線請求 (用戶名、密碼)  |                 |              |
   |---------------------->| Access-Request  |              |
   |                       |---------------->|              |
   |                       | Access-Accept   |              |
   | 連線通知               |<----------------|              |
   |<----------------------|                                |
   |                       | Accounting-Request start       |
   |                       |------------------------------->|
   |                       | Accounting-Response            |
   |                       |<-------------------------------|
   /                       /                                /
   /                       /                                /
   | 斷線請求               |                                |
   |---------------------->| Accounting-Request stop        |
   |                       |------------------------------->|
   |                       | Accounting-Response            |
   | 斷線通知               |<-------------------------------|
   |<----------------------|                                |

RADIUS Server 使用 UDP port 1812。早期用 UDP port 1645 會和「datametrics」衝突。RADIUS Accounting Server 使用 UDP port 1813。早期用 UDP port 1646 會和「sa-msg-port」衝突。

封包格式
    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |     Code      |  Identifier   |            Length             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                                                               |
   |                         Authenticator                         |
   |                           16 octets                           |
   |                                                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Attributes ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-
  • Code:type of RADIUS packet
    • 1= Access-Request
    • 2= Access-Accept
    • 3= Access-Reject
    • 4= Accounting-Request
    • 5= Accounting-Response:Server 收到 Accounting-Request 後成功紀錄後才回,不然不回應。
    • 11= Access-Challenge
    • 12= Status-Server, experimental
    • 13= Status-Server, experimental
    • invalid Code:silently discarded.
  • Identifier:辨別重傳,和回應配對請求。新 Access-Request 和 Accounting-Request 訊息用新的 Identifier,重傳用原本的 Identifier。Access-Accept、Access-Reject、和 Access-Challenge 用 Access-Request 的 Identifier。Accounting-Response 用 Accounting-Request 的 Identifier。
  • Length:從 Code 開始到所有 Attribute 的長度,20~4095,如果封包比 Length 短:silently discarded。
  • Authenticator:用來 authenticate Client 和 Server 間的訊息。
    • 在 Access-Request...
    • 在 Accounting-Request 是 Code、Identifier、Length、16 zero octets、attributes、加上 shared secret 的 MD5 checksum。The NAS and RADIUS accounting server share a secret.
    • 在回應是 Code、Identifier、Length、請求的 Authenticator、attributes、加上 shared secret 的 MD5 hash。
  • Attributes:Attributes may have multiple instances, in such a case the order of attributes of the same type SHOULD be preserved. The order of attributes of different types is not required to be preserved.
Attribute 格式
 0                   1                   2
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|     Type      |    Length     |  Value ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Length 值包含 Type、Length、和 Value,最小是 2。Value 的格式和長度由 Type 和 Length 決定,完整 Type 列表見 IANA。Value 的類型有:
  • string:1-253 octets 二進位資料 (0 ~ 255),未必有 NULL 結尾。注意:至少 1 octet。
  • text:1-253 octets UTF-8 encoded 的 10646 characters,未必有 NULL 結尾,是 string 的 subset。注意:至少 1 octet。
  • address:32-bit value, most significant octet first.
  • integer:32-bit unsigned value, most significant octet first.
  • time:32-bit unsigned value, most significant octet first。從 1970 UTC 開始妙數。
#typelenvalue說明
5NAS-Port6integer用戶使用的界面編號。
26Vendor-Specific≥7vsa4-byte Vender-Id + string。Vendor-Id 是 Vendor 的 SMI Network Management Private Enterprise Code。string 由 Vendor 自行定義,例如 1-octet vendor type + 1-octet length + value。
40Acct-Status-Type6enumAccounting-Request 記帳開始或結束:
1=Start
2=Stop
3=Interim-Update
7=Accounting-On
8=Accounting-Off
9-14=Reserved for Tunnel Accounting
15=Reserved for Failed
41Acct-Delay-Time6integer送這個紀錄時延遲的秒數。
42Acct-Input-Octets6integer
43Acct-Output-Octets6integer
44Acct-Session-Id≥3text方便
45Acct-Authentic6enumAccounting-Request 時表示認證方式
46Acct-Session-Time6integer服務秒數,Accounting-Request 的 Acct-Status-Type=Stop 時回報。
47Acct-Input-Packets6integer
48Acct-Output-Packets6integer
49Acct-Terminate-Cause6enum
50Acct-Multi-Session-Id≥3text
51Acct-Link-Count6integer
61NAS-Port-Type6enum用戶使用的界面類型:
0=Async
1=Sync
2=ISDN Sync
3=ISDN Async V.120
4=ISDN Async V.110
5=Virtual
6=PIAFS
7=HDLC Clear Channel
8=X.25
9=X.75
10=G.3 Fax
11=SDSL
12=ADSL-CAP
13=ADSL-DMT
14=IDSL
15=Ethernet
16=xDSL
17=Cable
18=Wireless - Other
19=Wireless - IEEE 802.11
87NAS-Port-Id≥3text描述用戶使用的界面。例如「ISDN 7/2:D:1」。

認證和授權

記帳

服務開始:送 Accounting Start (type of service being delivered and the user it is being delivered to) 給 RADIUS Accounting server 服務結束:送 Accounting Stop (type of service that was delivered and optionally statistics such as elapsed time, input and output octets, or input and output packets) 給 RADIUS Accounting server Accounting-Request (whether for Start or Stop) 成功的話 RADIUS Accounting server 回 Accounting-Response acknowledgment 建議 Client continue attempting to send the Accounting-Request 直到收到 acknowledgement, using some form of backoff. alternate server (after a number of tries to the primary server fail, or in a round-robin fashion)

Accounting-Request 的 Attribute
  • 必須放的:NAS-IP-Address 或 NAS-Identifier
  • 應該放的:NAS-Port or NAS- Port-Type attribute or both unless the service does not involve a port or the NAS does not distinguish among its ports
  • 如果有 Framed-IP-Address,必須含用戶的 IP address。可能是透過 Access-Accept 指定或協調的。
  • 不能放的:User-Password, CHAP-Password, Reply-Message, State。

如果 Accounting-Request 有 invalid Length,整個 request MUST be silently discarded。

參考

  1. https://en.wikipedia.org/wiki/RADIUS
  2. 802.1X
  3. RADIUS Server:FreeRadius
  4. CDR configuration with Radius Accounting:CISCO 設備只送 Accounting-Request stop 作為 CDR,可使用標準 RADIUS Attribute 或 CISCO VSA Attribute