在Linux下如果要使用接口标志要加什么头文件吗?因为我在使用IFF_UP时会出错,说是未定义

在Linux下如果要使用接口标志要加什么头文件吗?因为我在使用IFF_UP时会出错,说是未定义,第1张

头文件一般放在/usr/include目录下,

用grep 'IFF_UP' /usr/include/*.* |less

这个命令查找一下在哪个头文件里面有定义。

#include <time.h>

#include "tspeed.h"

int main ()

{

struct if_speed ndev

int ret = 0

bzero(&ndev,sizeof(ndev))

sprintf(ndev.ifs_name,"eth0")

ndev.ifs_us = 100000

printf("Get %s Speed",ndev.ifs_name)

#if 1

ret = get_if_speed(&ndev)

if(ret <0)

printf("[Fail]\n")

else

printf("[OK]\n")

float ispeed ,ospeed

while(1){

time_t *timep = malloc(sizeof(*timep))

time(timep)

char *s = ctime(timep)

ispeed = ndev.ifs_ispeed * 1.0/(ndev.ifs_us/1000 * 0.001)

ospeed = ndev.ifs_ospeed * 1.0/(ndev.ifs_us/1000 * 0.001)

#if 0

printf("%s: Up Speed: %fMB/s || Down Speed: %fMB/s\r",

ndev.ifs_name,ispeed/(1024.0*1024.0),ospeed/(1024.0*1024.0))

#endif

sleep(3)

#if 1

printf("*****************************************************\n")

printf("*****************************************\n")

printf("***************************\n")

printf("*************\n")

printf("Time is %s || Down Speed: %fKB/s\n",s,ospeed/(1024.0))

#endif

get_if_speed(&ndev)

}

#endif

return 0

} /* ----- End of main() ----- */

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

#include "tspeed.h"

int get_if_dbytes(struct if_info* ndev)

{

assert(ndev)

struct ifaddrs *ifa_list = NULL

struct ifaddrs *ifa = NULL

struct if_data *ifd = NULL

int ret = 0

ret = getifaddrs(&ifa_list)

if(ret <0) {

perror("Get Interface Address Fail:")

goto end

}

for(ifa=ifa_listifaifa=ifa->ifa_next){

if(!(ifa->ifa_flags &IFF_UP) &&!(ifa->ifa_flags &IFF_RUNNING))

continue

if(ifa->ifa_data == 0)

continue

ret = strcmp(ifa->ifa_name,ndev->ifi_name)

if(ret == 0){

ifd = (struct if_data *)ifa->ifa_data

ndev->ifi_ibytes = ifd->ifi_ibytes

ndev->ifi_obytes = ifd->ifi_obytes

break

}

}

freeifaddrs(ifa_list)

end:

return (ret ? -1 : 0)

}

int get_if_speed(struct if_speed *ndev)

{

assert(ndev)

struct if_info *p1=NULL,*p2=NULL

p1 = (struct if_info *)malloc(sizeof(struct if_info))

p2 = (struct if_info *)malloc(sizeof(struct if_info))

bzero(p1,sizeof(struct if_info))

bzero(p2,sizeof(struct if_info))

strncpy(p1->ifi_name,ndev->ifs_name,strlen(ndev->ifs_name))

strncpy(p2->ifi_name,ndev->ifs_name,strlen(ndev->ifs_name))

int ret = 0

ret = get_if_dbytes(p1)

if(ret <0) goto end

usleep(ndev->ifs_us)

ret = get_if_dbytes(p2)

if(ret <0) goto end

ndev->ifs_ispeed = p2->ifi_ibytes - p1->ifi_ibytes

ndev->ifs_ospeed = p2->ifi_obytes - p1->ifi_obytes

end:

free(p1)

free(p2)

return 0

}

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

#ifndef __TSPEED_H__

#define __TSPEED_H__

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <assert.h>

#include <error.h>

#include <ifaddrs.h>

#include <net/if.h>/* for ifconf */

#include <linux/sockios.h> /* for net status mask */

#include <netinet/in.h>/* for sockaddr_in */

#include <sys/socket.h>

#include <sys/ioctl.h>

/* For "open" function */

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

struct if_data

{

/* generic interface information */

u_long ifi_opackets /* packets sent on interface */

u_long ifi_ipackets /* packets received on interface */

u_long ifi_obytes /* total number of octets sent */

u_long ifi_ibytes /* total number of octets received */

}

struct if_info

{

char ifi_name[16]

unsigned long ifi_ibytes

unsigned long ifi_obytes

}

struct if_speed

{

char ifs_name[16]

unsigned long ifs_ispeed

unsigned long ifs_ospeed

unsigned long ifs_us

}

int get_if_dbytes(struct if_info *ndev)

int get_if_speed(struct if_speed *ndev)

#endif

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

纯手打 代码都给你了

ifconfig eth0 up 是使用了 ifconfig 命令来激活网卡

ifup eth0 则是使用了 ifup脚本来激活网卡

用 ifup脚本方式的好处在于可以提供一些高级控制

ifup ifdown 通常存放于 /etc/network/interfaces下面


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7185648.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-02
下一篇 2023-04-02

发表评论

登录后才能评论

评论列表(0条)

保存