LInux下的網路ioctl使用

2021-10-07 10:07:37 字數 2635 閱讀 8819

最近需要修改linux下的網路引數,遂將linux下用於控制網路引數的 ioctl常用函式記錄一下,使用的時候注意使包裹對應的標頭檔案,其中sockios.**件包含所有的操作命令字,

int main(int argc, char *ar**)

int error;

//struct ethtool_link_settings linkset;

struct ethtool_value edata;

edata.cmd = 0x0000000a;

ifr.ifr_data = (caddr_t) &edata;

if(ioctl(fd,siocethtool, &ifr) == -1)

if(edata.data == 0)

struct ethtool_cmd cmdget;

= ethtool_glinksettings;

cmdget.cmd = ethtool_gset;

= (caddr_t)&linkset;

ifr.ifr_data = (caddr_t)&cmdget;

if(ioctl(fd,siocethtool,&ifr) < 0)

qdebug("the eth0 speed is %d mb/s",cmdget.speed);

qdebug("the etho duplex is %d",cmdget.duplex);

//struct ethtool_cmd cmdset;

cmdget.cmd = ethtool_sset;

cmdget.speed = 10;

cmdget.duplex = 0;

cmdget.autoneg = 1;

ifr.ifr_data = (caddr_t)&cmdget;

if(ioctl(fd,siocethtool,&ifr) < 0)

memset(&cmdget,0,sizeof(cmdget));

cmdget.cmd = ethtool_gset;

ifr.ifr_data = (caddr_t)&cmdget;

if(ioctl(fd,siocethtool,&ifr) < 0)

qdebug("the eth0 speed is %d mb/s",cmdget.speed);

qdebug("the etho duplex is %d",cmdget.duplex);

qdebug("the etho autoneg is %d",cmdget.autoneg);

sleep(1);

if(ioctl(fd,siocethtool,&ifr) < 0)

qdebug("the eth0 speed is %d mb/s",cmdget.speed);

qdebug("the etho duplex is %d",cmdget.duplex);

//set hwaddr

struct ifreq ifr;

char * pmacaddr[6]=;

ifr.ifr_hwaddr.sa_data[0] = pmacaddr[0];

ifr.ifr_hwaddr.sa_data[1] = pmacaddr[1];

ifr.ifr_hwaddr.sa_data[2] = pmacaddr[2];

ifr.ifr_hwaddr.sa_data[3] = pmacaddr[3];

ifr.ifr_hwaddr.sa_data[4] = pmacaddr[4];

ifr.ifr_hwaddr.sa_data[5] = pmacaddr[5];

ifr.ifr_hwaddr.sa_family = arphrd_ether;

if(ioctl(s, siocsifhwaddr, &ifr) == -1)

//add multicast addr

struct ifreq ifr1;

ifr1.ifr_hwaddr.sa_data[0] = 0x01;

ifr1.ifr_hwaddr.sa_data[1] = 0x80;

ifr1.ifr_hwaddr.sa_data[2] = 0xc2;

ifr1.ifr_hwaddr.sa_data[3] = 0x00;

ifr1.ifr_hwaddr.sa_data[4] = 0x00;

ifr1.ifr_hwaddr.sa_data[5] = 0x0e;

ioctl(sockfd, siocaddmulti, &ifr);

//delete multicast addr

struct ifreq ifr2;

ifr2.ifr_hwaddr.sa_data[0] = 0x01;

ifr2.ifr_hwaddr.sa_data[1] = 0x80;

ifr2.ifr_hwaddr.sa_data[2] = 0xc2;

ifr2.ifr_hwaddr.sa_data[3] = 0x00;

ifr2.ifr_hwaddr.sa_data[4] = 0x00;

ifr2.ifr_hwaddr.sa_data[5] = 0x0e;

ioctl(sockfd, siocdelmulti, &ifr);

return a.exec();}

本文為原創作品,如果對你有幫助,請點贊哦!

linux下ioctl遇到的坑

在驅動程式設計裡面經常會用到ioctl的系統呼叫,發現cmd 2的時候,使用者ioctl直接返回 1。原因在於在linux x.xx fs ioctl.c定義的do vfs ioctl函式 1 int do vfs ioctl struct file filp,unsigned int fd,uns...

Linux下ioctl函式理解

一 什麼是ioctl ioctl是裝置驅動程式中對裝置的i o通道進行管理的函式。所謂對i o通道進行管理,就是對裝置的一些特性進行控制,例如串列埠的傳輸波特率 馬達的轉速等等。它的呼叫個數如下 int ioctl int fd,ind cmd,其中fd就是使用者程式開啟裝置時使用open函式返回的...

Linux下的packet socket使用總結

上節講到的raw socket雖然已經能處理ip層的資料了,但是在實際的應用中可能需要獲取更加底層的報文資訊。這就需要用到另一種更加強大的socket建立方式 packet socket,建立之後直接可以操作包括以太層在內的報文資料。packet socket的建立方式如下 socket af pa...