在linux中獲取ip及mac**如下
int getipmac()
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, eth_name, strlen(eth_name));
if(ioctl(sock, siocgifaddr, &ifr) == 0)
if(ioctl(sock, siocgifhwaddr, &ifr) == 0)
return 0;
在linux中獲取ip及mac須用到ioctl函式及struct ifreq結構
首先,我們來看看ioctl函式
函式標頭檔案: #include
函式原型: int ioctrl(int fd, int request, .../*(void *agrv */)
返回值:正常返回0,錯誤返回-1
在這裡簡單說一下ioctl
ioctl的第三個引數總為乙個指標,但其型別信賴於request的引數,在這裡我只簡單說一下第三個指標為struct ifreq型別時,第二個引數的可選內容
若對ioctl興趣的可以去度娘找找其他的
我現在簡單介紹下在第三個引數為struct ifreq型別時,request的取值意義:
request
siocsifaddr 設定介面位址
siocgifaddr 獲取介面位址
siocsifflags 設定介面標誌
siocgifflags 獲取介面標誌
siocsifdstaddr 設定點到點的位址
siocgifdstaddr 獲取點到點的位址
siocsifbrdaddr 設定廣播位址
siocgifbrdaddr 獲取廣播位址
siocsifnetmask 設定子網掩碼
siocgifnetmask 獲取子網掩碼
siocgifmtu 獲取mtu
sioc*** 其他的請參考系統實現
說完ioctl,現在來說說struct ifreq結構
struct ifreq
;union
ifr_ifru;
};
LINUX下獲取IP位址和MAC位址
include include include include include include include include ifdef solaris include endif define maxinte ces 16 main argc,argv register int argc reg...
LINUX下獲取IP位址和MAC位址
判斷網絡卡狀態 if buf int ce ifr flags iff up else 獲取當前網絡卡的ip位址 if ioctl fd,siocgifaddr,char buf int ce else this section can t get hardware address,i don t ...
LINUX下獲取IP位址和MAC位址
gcc o mac mac.c include include include include include include include include ifdef solaris include endif define maxinte ces 16 main argc,argv regis...