ioctl 擷取本機網卡ip地址 | socket() 複製代碼

來源:互聯網
上載者:User
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="cpp">#include <string.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <net/if.h>#include <stdio.h>#include <netinet/in.h>#include <arpa/inet.h>int main(){    int inet_sock;    struct ifreq ifr;    inet_sock = socket(AF_INET, SOCK_DGRAM, 0);     //eth0為介面到名稱    strcpy(ifr.ifr_name, "eth1");    //SIOCGIFADDR標誌代表擷取介面地址    if (ioctl(inet_sock, SIOCGIFADDR, &ifr) ==  0)          perror("ioctl");    printf("%s\n", inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr)); //轉換成點分十進位的格式    return 0;}
 
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">--------------------------------------------------------------------------------------------------------------------------------------------------------------</span>

ifreq結構定義在/usr/include/net/if.h,用來配置ip地址,啟用介面,配置MTU等介面資訊的。
其中包含了一個介面的名字和具體內容——(是個共用體,有可能是IP地址,廣播位址,子網路遮罩,MAC號,MTU或其他內容)。
ifreq包含在ifconf結構中。而ifconf結構通常是用來儲存所有介面的資訊的。

--------------------------------------------------------------------------------------------------------------------------------------------------------------

用ioctl獲得本地ip地址時要用到兩個結構體ifconf和ifreq,它們對於大多數人
來說都是比較陌生的,這裡給大家一種比較簡單的理解方法,當然只一種協助
理解的方法,在描述中可能會有一些地方與真實定義有所出入,僅供參考.

首先先認識一下ifconf和ifreq:

//ifconf通常是用來儲存所有介面資訊的
//if.h
struct ifconf 
{
    int    ifc_len;            /* size of buffer    */
    union 
    {
        char *ifcu_buf;                        /* input from user->kernel*/
        struct ifreq *ifcu_req;        /* return from kernel->user*/
    } ifc_ifcu;
};
#define    ifc_buf    ifc_ifcu.ifcu_buf        /* buffer address    */
#define    ifc_req    ifc_ifcu.ifcu_req        /* array of structures    */
 
//ifreq用來儲存某個介面的資訊
//if.h
struct ifreq {
    char ifr_name[IFNAMSIZ];
    union {
        struct sockaddr ifru_addr;
        struct sockaddr ifru_dstaddr;
        struct sockaddr ifru_broadaddr;
        short ifru_flags;
        int ifru_metric;
        caddr_t ifru_data;
    } ifr_ifru;
};
#define ifr_addr ifr_ifru.ifru_addr
#define ifr_dstaddr ifr_ifru.ifru_dstaddr
#define ifr_broadaddr ifr_ifru.ifru_broadaddr

 

上邊這兩個結構看起來比較複雜,我們現在把它們簡單化一些:
比如說現在我們向實現獲得本地IP的功能。

我們的做法是:
1. 先通過ioctl獲得本地所有介面的資訊,並儲存在ifconf中
2. 再從ifconf中取出每一個ifreq中表示ip地址的資訊

具體使用時我們可以認為ifconf就有兩個成員:
ifc_len 和 ifc_buf,如圖一所示:    

  

ifc_len:表示用來存放所有介面資訊的緩衝區長度
ifc_buf:表示存放介面資訊的緩衝區

所以我們需要在程式開始時對ifconf的ifc_led和ifc_buf進行初始化
接下來使用ioctl擷取所有介面資訊,完成後ifc_len記憶體放實際獲得的借口資訊總長度
並且資訊被存放在ifc_buf中。
如下圖示:(假設讀到兩個介面資訊)


接下來我們只需要從一個一個的介面資訊擷取ip地址資訊即可。

下面有一個簡單的參考:

#include 
#include 
#include 
#include 
#include in.h>
#include <string.h>
#include if.h>
#include 
 
int main()
{
    int i=0;
    int sockfd;
  struct ifconf ifconf;
  unsigned char buf[512];
  struct ifreq *ifreq;
  
  //初始化ifconf
  ifconf.ifc_len = 512;
  ifconf.ifc_buf = buf;
  
    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0)
    {
        perror("socket");
        exit(1);
    }  
  ioctl(sockfd, SIOCGIFCONF, &ifconf);    //擷取所有介面資訊
  
  //接下來一個一個的擷取IP地址
  ifreq = (struct ifreq*)buf;  
  for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i>0; i--)
  {
//      if(ifreq->ifr_flags == AF_INET){            //for ipv4
          printf("name = [%s]\n", ifreq->ifr_name);
      printf("local addr = [%s]\n", 
                      inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));
      ifreq++;
//  }
  }
    return 0;
}
  此方法僅供參考,也適用於擷取其他資訊。

-------------------------------------------------------------------------------------------------------------------------------------------------------------- 簡述:
建立一個套介面。
#include <winsock.h>
SOCKET PASCAL FAR socket( int af, int type, int protocol);
af:一個地址描述。目前僅支援AF_INET格式,也就是說ARPA Internet地址格式。
type:新套介面的類型描述。
protocol:套介面所用的協議。如調用者不想指定,可用0。
注釋
   socket()函數用於根據指定的地址族、資料類型和協議來分配一個套介面的描述字及其所用的資源。如果協議protocol未指定(等於0),則使用預設的串連方式。
對於使用一給定地址族的某一特定套介面,只支援一種協議。但地址族可設為AF_UNSPEC(未指定),這樣的話協議參數就要指定了。協議號特定於進行通訊的“通訊域”。支援下述類型描述:
類型 解釋
SOCK_STREAM 提供有序的、可靠的、雙向的和基於串連的位元組流,使用帶外資料傳送機制,為Internet地址族使用TCP。
SOCK_DGRAM 支援不需連線的、不可靠的和使用固定大小(通常很小)緩衝區的資料報服務,為Internet地址族使用UDP。
SOCK_STREAM類型的套介面為全雙向的位元組流。對於流類套介面,在接收或發送資料前必需處於已串連狀態。用 connect()調用建立與另一套介面的串連,串連成功後,即可用 send()和recv()傳送資料。當會話結束後,調用closesocket()。帶外資料根據規定用send()和recv()來接收。
實現SOCK_STREAM類型套介面的通訊協議保證資料不會丟失也不會重複。如果終端協議有緩衝區空間,且資料不能在一定時間成功發送,則認為串連中斷,其後續的調用也將以WSAETIMEOUT錯誤返回。
SOCK_DGRAM類型套介面允許使用 sendto()和recvfrom()從任意連接埠發送或接收資料報。如果這樣一個套介面用connect()與一個指定連接埠串連,則可用send()和recv()與該連接埠進行資料報的發送與接收。
傳回值
  若無錯誤發生,socket()返回引用新套介面的描述字。否則的話,返回INVAID_SOCKET錯誤,應用程式可通過WSAGetLastError()擷取相應錯誤碼。
錯誤碼:
WSANOTINITIALISED:在使用此API之前應首先成功地調用WSAStartup()。
WSAENETDOWN:WINDOWS套介面實現檢測到網路子系統失效。
WSAEAFNOSUPPORT:不支援指定的地址族。
WSAEINPROGRESS:一個阻塞的WINDOWS套介面調用正在運行中。
WSAEMFILE:無可用檔案描述字。
WSAENOBUFS:無可用緩衝區,無法建立套介面。
WSAEPROTONOSUPPORT:不支援指定的協議。
WSAEPROTOTYPE:指定的協議不適用於本套介面。
WSAESOCKTNOSUPPORT:本地址族中不支援該類型套介面。
參見
   accept(), bind(), connect(), getsockname(), getsockopt(), setsockopt(), listen(), recv(), recvfrom(), select(), send(), sendto(), shutdown(), ioctlsocket().4.2 資料庫常式
socket()
函數原型:
SOCKET WSAAPI  socket(
int af,
int type,
int protocol
);
該函數及參數定義包含在winsock2.h標頭檔中,在MSDN中查不到具體參數。
參數選項及定義:
地址族af:(常用AF_INET實現TCP/UDP協議)
#define AF_UNSPEC       0               /* unspecified */
#define AF_UNIX         1               /* local to host (pipes, portals) */
#define AF_INET         2               /* internetwork: UDP, TCP, etc. */
#define AF_IMPLINK      3               /* arpanet imp addresses */
#define AF_PUP          4               /* pup protocols: e.g. BSP */
#define AF_CHAOS        5               /* mit CHAOS protocols */
#define AF_NS           6               /* XEROX NS protocols */
#define AF_IPX          AF_NS           /* IPX protocols: IPX, SPX, etc. */
#define AF_ISO          7               /* ISO protocols */
#define AF_OSI          AF_ISO          /* OSI is ISO */
#define AF_ECMA         8               /* european computer manufacturers */
#define AF_DATAKIT      9               /* datakit protocols */
#define AF_CCITT        10              /* CCITT protocols, X.25 etc */
#define AF_SNA          11              /* IBM SNA */
#define AF_DECnet       12              /* DECnet */
#define AF_DLI          13              /* Direct data link interface */ #define AF_LAT          14              /* LAT */
#define AF_HYLINK       15              /* NSC Hyperchannel */
#define AF_APPLETALK    16              /* AppleTalk */
#define AF_NETBIOS      17              /* NetBios-style addresses */
#define AF_VOICEVIEW    18              /* VoiceView */
#define AF_FIREFOX      19              /* Protocols from Firefox */
#define AF_UNKNOWN1     20              /* Somebody is using this! */
#define AF_BAN          21              /* Banyan */
#define AF_ATM          22              /* Native ATM Services */
#define AF_INET6        23              /* Internetwork Version 6 */
#define AF_CLUSTER      24              /* Microsoft Wolfpack */
#define AF_12844        25              /* IEEE 1284.4 WG AF */
通訊端類型type:
#define SOCK_STREAM     1               /* stream socket */
#define SOCK_DGRAM      2               /* datagram socket */
#define SOCK_RAW        3               /* raw-protocol interface */
#define SOCK_RDM        4               /* reliably-delivered message */
#define SOCK_SEQPACKET  5               /* sequenced packet stream */
協議類型protocol:
#define IPPROTO_IP              0               /* dummy for IP */
#define IPPROTO_ICMP            1               /* control message protocol */
#define IPPROTO_IGMP            2               /* internet group management protocol */
#define IPPROTO_GGP             3               /* gateway^2 (deprecated) */
#define IPPROTO_TCP             6               /* tcp */
#define IPPROTO_PUP             12              /* pup */
#define IPPROTO_UDP             17              /* user datagram protocol */
#define IPPROTO_IDP             22              /* xns idp */
#define IPPROTO_ND              77              /* UNOFFICIAL net disk proto */

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.