---
# System prepended metadata

title: SIP
tags: [Construction]

---

## About this project

- SIP流程
    
    由下圖可知，當 Client 1 撥打電話給 Client 2 時，會先發送 INVITE 請求並且與 Client 1 請求代理伺服器授權。當前置動作完成以後， SIP Server 會發送 INVITE 給 Client 2 ，此時雙方的電話將會響起，假設雙方都接受電話請求，此時開始進行對話。最後，當一方送出 BYE 訊息時，系統會執行通話終止，並且同時送出 BYE 訊息給另一方結束此次通話。
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/38cdfbad-be12-42bc-be5c-1d78ac80cd50/Untitled.png)
    
- kamailio是開源SIP服務器，通過一些簡單配置和參數設置就可以進行使用，在這裡是安裝到ubunt20.04。並且需要在同一個LAN下。
- 安裝依賴
    
    ```
    $ sudo apt install libssl-dev libmysqlclient-dev autoconf libcurl4-openssl-dev libxml2-dev libpcre3-dev
    ```
    
- 安裝mysql
    
    ```
    $ sudo apt install mysql-server
    ```
    
    安裝完成後，登錄mysql
    
    ```
    $ mysql -u root
    ```
    
    *這邊會出現錯誤*
    
    `ERROR 1698 (28000): Access denied for user 'root'@'localhost’`
    
    這時需要重新設定參數，打開mysql的配置文件:
    
    ```
    $ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
    ```
    
    在最後面追加此訊息:
    
    ```
    skip-grant-tables
    ```
    
    然後重新啟動mysql服務
    
    ```
    $ service mysql restart
    ```
    
    現在便可以成功登錄到mysql中
    
    ```
    $ mysql -u root -p
    ```
    
    接下來執行以下命令(適用於mysql8)
    
    ```
    -> use mysql;
    -> flush privileges;
    -> update user set plugin='mysql_native_password' WHERE user='root' AND host='localhost';
    -> ALTER user 'root'@'localhost' IDENTIFIED BY 'newpassword';
    ```
    
    以上指令中只有**newpassword需要進行更改成自己的密碼**，例如:
    
    ```
    -> ALTER user 'root'@'localhost' IDENTIFIED BY '1117';
    ```
    
    若出現以下錯誤訊息，重啟mysql即可(service mysql restart)，在重新上一步
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6ade8fa4-cd78-4f4d-adeb-c9d29d60994e/Untitled.png)
    
    執行完成後，可以使用以下命令查看是否修改完成:
    
    ```
    -> select user, plugin from user; 
    ```
    
    初始表格
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c9a32957-36c3-4b9d-829b-2c71b1603f14/Untitled.png)
    
    修改過後的表格
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a0095520-a754-4295-8fc9-36982aaf1506/Untitled.png)
    

- 安裝kamailio
    
    ```
    $ wget -O- https://deb.kamailio.org/kamailiodebkey.gpg | sudo apt-key add -
    $ sudo vim /etc/apt/sources.list
    ```
    
    將下面兩行新增至 sources.list檔案中的最下面:
    
    ```
    deb     http://deb.kamailio.org/kamailio56 bullseye main
    deb-src http://deb.kamailio.org/kamailio56 bullseye main
    ```
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d4f7e3f3-1669-4130-8a5a-53ac7ff9d83a/Untitled.png)
    
    kamailio installation:
    
    ```
    $ sudo apt update
    $ sudo apt -y install default-mysql-server
    $ sudo apt -y install kamailio kamailio-mysql-modules
    ```
    
    確認kamailio modules是否都有下載到
    
    ```
    $ sudo apt search kamailio
    ```
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/69a3c1f1-fad5-4678-9e25-5c93aaf3b360/Untitled.png)
    
    設定配置文件:
    
    **這邊要注意到virtual box 的網路介面卡是否跟手機wifi是在同一個網域下**
    
    **(一定要一樣喔不然連接不到伺服器)**
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/56768753-920a-497d-b1de-d8735fb0496f/Untitled.png)
    
    ```
    $ ifconfig  #用於查看ip address
    ```
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b06b8db3-87c1-4c4f-a75e-fb1700b44868/Untitled.png)
    
    ```
    $ sudo vim /etc/kamailio/kamctlrc
    ```
    
    將下面三行註解掉， SIP_DOMAIN 設定為自己的 ip address 可以由 ifconfig 查看
    
    ```
    SIP_DOMAIN=your_ip_address
    DBENGINE=MYSQL
    DBRWPW="kamailiorw"
    ```
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ee274271-1b12-4608-8e8a-bc05a7656b71/Untitled.png)
    
    為了啟用 MySQL 服務器後端、用戶身份驗證和持久用戶位置，請在第一行後添加：
    
    ```
    $ sudo vim /etc/kamailio/kamailio.cfg
    ```
    
    ```
    #!define WITH_MYSQL
    #!define WITH_AUTH
    #!define WITH_USRLOCDB
    ```
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b5754b5c-473f-4a89-a60a-de2aae029cb5/Untitled.png)
    
    創建 Kamailio 所需的 database，MySQL password為你所設定的newpassword:
    
    ```
    $ sudo kamdbctl create
    ```
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/101d7774-370c-4eb3-8b05-e527709eef45/Untitled.png)
    
    character set name 設定為 utf16 ，接下來的詢問都按y，database就創建成功啦!
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4b120935-6410-4169-90a7-706173738375/Untitled.png)
    
    運行kamailio
    
    ```
    $ kamctl start
    or
    $ systemctl start kamailio 
    ```
    
    執行次命令時如遇到如下錯誤:
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a1bfdad4-42e0-4247-a255-cf696d3c99d0/Untitled.png)
    
    這個問題是kamailio的pid文件沒有權限導致的，可以在`/run`目錄下使用指令:
    
    ```
    $ sudo mkdir kamailio
    $ sudo chown 當前用戶名 /run/kamailio
    ```
    
    將文件權限轉移到當前用戶，這樣即可解決問題。
    
    查看 kamailio 運行狀態(綠燈)
    
    ```
    $ systemctl status kamailio 
    ```
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4c3f2fb4-0cc0-42df-8a6c-888ee4020f81/Untitled.png)
    
    如果要重新啟動kamailio可以使用以下指令:
    
    ```
    $ systemctl restart kamailio 
    ```
    
    如果要停止kamailio運行(白燈)可以使用以下指令:
    
    ```
    $ kamctl stop
    or
    $ systemctl stop kamailio 
    ```
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d11284ef-8704-4132-806e-631e57b57042/Untitled.png)
    
    kamailio增加帳號 (若要求輸入密碼，密碼為DBRWPW所設定密碼，預設為kamailiore)
    
    ```
    $ kamctl add username password
    
    example:
    $ kamcrl add ping 1117
    ```
    
    手機下載 Linphone(電腦也可以使用)
    
    !!!很重要 手機WiFi必須跟kamailio在同一個LAN下，使用前請務必先確認!!!
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b3c8ea8e-3750-4c52-9a3f-299c1a8ed7b2/Untitled.png)
    
    ![A3085342-25F8-4782-B398-1F0322C91E9B.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/55484c55-423b-486c-a03e-023d4ee13fb4/A3085342-25F8-4782-B398-1F0322C91E9B.png)
    
    ![DF666165-C2D1-42D2-8162-62695EACB766.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/25ce0a56-f8cf-40a2-8a21-8000ccd2a980/DF666165-C2D1-42D2-8162-62695EACB766.png)
    
    ![F4B92F74-B9B6-42A9-AC89-2513641FB2C5.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4ca58b90-3b47-4334-8ded-0e4f4d4bd37a/F4B92F74-B9B6-42A9-AC89-2513641FB2C5.png)
    
    ![AE5A8BFD-4ECC-4DFA-81DF-8B390D30F310.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e5fa96cf-020b-4f45-b3cc-109f5854cee4/AE5A8BFD-4ECC-4DFA-81DF-8B390D30F310.png)
    
    ![67033910-7222-48A5-B620-4C688142384E.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4d9fdea0-8bde-415f-815c-cc58a9a43d71/67033910-7222-48A5-B620-4C688142384E.png)
    
    ![49B7E6DE-69E5-4E31-AF16-82E592FD57F3.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/3ffb7696-60d3-454b-996a-066e153f276c/49B7E6DE-69E5-4E31-AF16-82E592FD57F3.png)
    
    電腦畫面
    
    ![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c92e9f07-4f06-4489-aa5b-132d453de9b0/Untitled.png)
    

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2159dbd6-73f3-4817-86a3-e88bd5cebd4a/Untitled.png)

## Project tasks

[Tasks](https://www.notion.so/d4253e6fa16a4d68a328a978b454e12f?pvs=21)