使用網路服務

Azure 球體可以執行靜態 IP 位址、 動態主機配置通訊協定 (DHCP) 伺服器,以及適用于網路介面 的簡易網路時間通訊協定 (SNTP) 伺服器 。 DHCP 伺服器可讓 Azure 球體應用程式為網路上的外部裝置設定網路參數。 外部裝置可以使用 SNTP 伺服器來同步處理它與 Azure 球體的時間。

網路設定

您可以設定乙太網路和 Wi-Fi 網路介面,以便在 Azure 球體裝置上同時執行。 乙太網路和 Wi-Fi 網路介面可以連線至公用 (連線的網際網路) 或私人網路。 至少一個介面必須連線到公用網路。 一次只能設定一個乙太網路介面。

私人和公用網路介面

如果您同時使用公用和私人網路介面,例如私人乙太網路搭配公用 Wi-Fi,Azure 球體裝置將不會做為路由器。 它不會自動將乙太網路上收到的封包傳遞到 Wi-Fi 網路,反之亦然。 您的應用程式必須實作在這兩個網路上傳送和接收資訊的所有邏輯。

雙公用網路介面

如果您使用啟用動態 IP 位址的兩個網路介面,作業系統會在選取 DNS 伺服器位址做為主機名稱解析時,嘗試使用第一個連線到網路的介面。 如果介面與網路中斷連線,則會自動使用來自其他連線介面的 DNS 伺服器位址。

靜態 IP 位址

您可以在乙太網路或 Wi-Fi 介面上設定靜態 IP 位址。 若要設定靜態 IP 位址設定,您的應用程式必須使用 Applibs 網路 API,應用程式 資訊清單 必須啟用 NetworkConfig 功能。

設定靜態 IP 位址時,也必須設定 自訂 DNS ,以確保 Azure 球體作業系統繼續如預期般運作。

[私人網路服務]範例示範如何將 Azure 球體連線到私人網路並使用多個網路服務。

此程式碼片段示範如何使用靜態 IP 位址設定網路介面。

請在 app_manifest.json 檔案的 [功能] 區段中包含下列一行,如下所示:

"Capabilities": {
  "NetworkConfig": true
}

在應用程式中包含這些頁首檔案:

#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <applibs/log.h>
#include <applibs/networking.h>

設定 IP 設定的 IP 位址、子網路遮罩和閘道。

static const char staticIpInDotNotation[] = "yourStaticIp"; // Your static IP in x.x.x.x notation.
static const char subnetMaskInDotNotation[] =
    "yourSubnetMask"; // Your subnet mask in x.x.x.x notation.
static const char gatewayIpInDotNotation[] = "yourGatewayIp"; // Your gateway IP in x.x.x.x notation.

指定要設定的網路介面:

static const char networkInterfaceToConfigure[] = "yourNetworkInterface"; // Your network interface.

將網路位址轉譯成整數,然後將它套用至指定的網路介面。

struct in_addr staticIpAddress;
struct in_addr subnetMask;
struct in_addr gatewayIpAddress;

Networking_IpConfig ipConfig;

// Convert the addresses from the numbers-and-dots notation into integers.
if (inet_pton(AF_INET, staticIpInDotNotation, &staticIpAddress) != 1) {
    Log_Debug("ERROR: Invalid static IP address or address family specified.\n");
    return -1;
}
if (inet_pton(AF_INET, subnetMaskInDotNotation, &subnetMask) != 1) {
    Log_Debug("ERROR: Invalid subnet mask or address family specified.\n");
    return -1;
}
if (inet_pton(AF_INET, gatewayIpInDotNotation, &gatewayIpAddress) != 1) {
    Log_Debug("ERROR: Invalid gateway IP address or address family specified.\n");
    return -1;
}

Networking_IpConfig_Init(&ipConfig);
Networking_IpConfig_EnableStaticIp(&ipConfig, staticIpAddress, subnetMask, gatewayIpAddress);

int result =
    Networking_IpConfig_EnableCustomDns(&ipConfig, dnsServers, numOfDnsServerAddressSpecified);

if (result != 0) {
    Log_Debug("ERROR: Networking_IpConfig_EnableCustomDns: %d (%s)\n", errno, strerror(errno));
    Networking_IpConfig_Destroy(&ipConfig);
    return -1;
}

int result = Networking_IpConfig_Apply(networkInterfaceToConfigure, &ipConfig);
Networking_IpConfig_Destroy(&ipConfig);

if (result != 0) {
    Log_Debug("ERROR: Networking_IpConfig_Apply: %d (%s)\n", errno, strerror(errno));
    return -1;
}

靜態 DNS 位址

如果您已使用靜態 IP 設定裝置,並且需要名稱解析,您的應用程式必須設定靜態 DNS 位址。 使用 Networking_IpConfig_EnableCustomDns 並設定一或多個有效的 DNS 解析程式。 如果已設定多個解析程式,系統會查詢所有解析程式,且第一個有效的 DNS 回應會滿足查詢。 如果 是透過 DHCP 設定目前解析程式,也可能會使用Networking_IpConfig_EnableCustomDns來覆寫目前的解析程式。

若要使用自訂 DNS 伺服器設定網路介面,應用程式資訊清單必須啟用 NetworkConfig 功能。

請在 app_manifest.json 檔案的 [功能] 區段中包含下列一行,如下所示:

"Capabilities": {
  "NetworkConfig": true
}

此程式碼片段示範如何使用自訂 DNS 伺服器設定網路介面。

在應用程式中包含這些頁首檔案:

#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <applibs/log.h>
#include <applibs/networking.h>

指定 DNS 伺服器的數目。 最多可以指定三個 DNS 伺服器。 下列程式碼會設定要使用的三個 DNS 伺服器 IP 位址陣列。

// A maximum of 3 DNS server addresses can be specified.
static const size_t numOfDnsServerAddressSpecified = 3;
static const char *dnsServerIpAddress[] = {
    "yourDnsServer1", "yourDnsServer2", "yourDnsServer3"}; // Your DNS servers in x.x.x.x notation.

指定要設定的網路介面。

static const char networkInterfaceToConfigure[] = "yourNetworkInterface"; // Your network interface.

將網路位址轉譯成整數並套用設定。 此 DNS 設定會覆寫 DHCP 指定的任何 DNS 伺服器。

Networking_IpConfig ipConfig;

// Convert the addresses from the numbers-and-dots notation into integers.
struct in_addr dnsServers[numOfDnsServerAddressSpecified];
for (int i = 0; i < numOfDnsServerAddressSpecified; i++) {
    if (inet_pton(AF_INET, dnsServerIpAddress[i], &dnsServers[i]) != 1) {
        Log_Debug("ERROR: Invalid DNS server address or address family specified.\n");
        return -1;
    }
}

Networking_IpConfig_Init(&ipConfig);

int result =
    Networking_IpConfig_EnableCustomDns(&ipConfig, dnsServers, numOfDnsServerAddressSpecified);

if (result != 0) {
    Log_Debug("ERROR: Networking_IpConfig_EnableCustomDns: %d (%s)\n", errno, strerror(errno));
    Networking_IpConfig_Destroy(&ipConfig);
    return -1;
}

result = Networking_IpConfig_Apply(networkInterfaceToConfigure, &ipConfig);
Networking_IpConfig_Destroy(&ipConfig);

if (result != 0) {
    Log_Debug("ERROR: Networking_IpConfig_Apply: %d (%s)\n", errno, strerror(errno));
    return -1;
}

DHCP 伺服器

透過乙太網路介面連線至 Azure 球體的外部用戶端裝置,必須指派 IP 位址和其他網路參數,以便與 Azure 球體裝置上的伺服器應用程式通訊。 不過,有些外部裝置不支援設定這些參數的方式。 Azure 球體支援 DHCP 伺服器,應用程式可透過該伺服器提供此設定。 應用程式必須在其應用程式資訊清單中啟用DhcpService功能。

Azure 球體應用程式會呼叫 Networking_DhcpServerConfig_Init 設定伺服器,為用戶端裝置提供 IP 位址、子網路遮罩、閘道位址、租賃持續時間,以及最多三個 NTP 伺服器位址。 目前版本中只能設定一個 IP 位址。 接著它會呼叫 Networking_DhcpServer_Start 在特定網路介面上啟動伺服器。 DHCP 伺服器啟動之後,用戶端裝置可以傳送廣播 DHCP 訊息,以在指定的子網路上從 DHCP 伺服器探索並要求 IP 位址。

SNTP 伺服器

SNTP 伺服器可讓用戶端裝置同步處理其系統時間與 Azure 球體裝置的時間。 若要使用伺服器,Azure 球體應用程式必須在其應用程式資訊清單中啟用SntpService功能。

若要啟動伺服器,Azure 球體應用程式會呼叫 Networking_SntpServer_Start ,並指定伺服器執行的網路介面。 用戶端裝置和 Azure 球體裝置必須位於伺服器執行所在的相同網路本機子網中。 Azure 球體裝置必須至少連線到一個公用網路,這樣它才能從公用網路時間通訊協定 (NTP) 伺服器取得目前的時間。 SNTP 伺服器在有目前時間之前不會回應查詢。

注意

雖然應用程式可以直接設定系統時間,但不建議使用此設定,因為當裝置斷電時,時間不會持續。 管理系統時間,且 Azure 球體上的 RTC 有更多資訊。

聆聽埠

如果 Azure 球體應用程式會聆聽傳入 TCP 或 UDP 連線,應用程式 資訊清單 必須指定應用程式使用的埠。 例如:

"Capabilities": {
  "AllowedTcpServerPorts": [ 11000 ],
  "AllowedUdpServerPorts": [ 1024, 50000 ]
} 

樣品

  • DNS 服務探索範例示範如何查詢和處理來自 DNS 伺服器的回應。
  • [私人網路服務]範例示範如何將 Azure 球體連線到私人網路並使用多個網路服務。