自己如何实现一个简化的程序ethcheck?

电子说

1.3w人已加入

描述

一、usb网卡应该如何实现?

前文讲了如何利用开源软件ifplugd实现监测网口状态变化,

ifplugd的确可以实现监测网卡的状态,并执行相应脚本,

但是有个前提,就是网口已经注册到系统中,即用ifconfig -a能查看到

如何是usb网卡这种设备,在插入usb口之后网口设备才会注册

使用过程中可能随时会拔掉usb网卡,

那么这种情况下,要想设置usb网卡,那么就就需要修改ifplugd程序。

为了方便大家理解,本文给大家讲解如何自己实现一个简化的程序ethcheck

可以实现自动监测网卡是否存在

rk3568所有网口:

 

rk3568_r:/system # ifconfig -a                                                
lo        Link encap:Local Loopback                                           
          inet addr:127.0.0.1  Mask:255.0.0.0                                 
          inet6 addr: ::1/128 Scope: Host                                     
          UP LOOPBACK RUNNING  MTU:65536  Metric:1                            
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0                  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0                
          collisions:0 txqueuelen:1000                                        
          RX bytes:0 TX bytes:0                                               
                                                                              
dummy0    Link encap:Ethernet  HWaddr fa6c1b:7d                       
          inet6 addr: fe80:6cff1b7d/64 Scope: Link                
          UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1                      
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0                  
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0                
          collisions:0 txqueuelen:1000                                        
          RX bytes:0 TX bytes:560                                             
                                                                              
sit0      Link encap:IPv6-in-IPv4                                             
          NOARP  MTU:1480  Metric:1                                           
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0                  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0                
          collisions:0 txqueuelen:1000                                        
          RX bytes:0 TX bytes:0                                               
                                                                              
ip6tnl0   Link encap:UNSPEC                                                   
          NOARP  MTU:1452  Metric:1                                           
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0                  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0                
          collisions:0 txqueuelen:1000                                        
          RX bytes:0 TX bytes:0                                               
                                                                              
eth1      Link encap:Ethernet  HWaddr 5a63dd:0b  Driver rk_gmac-dwmac 
          UP BROADCAST MULTICAST  MTU:1500  Metric:1                          
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0                  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0                
          collisions:0 txqueuelen:1000                                        
          RX bytes:0 TX bytes:0                                               
          Interrupt:51                                                        
                                                                              
ip_vti0   Link encap:UNSPEC                                                   
          NOARP  MTU:1480  Metric:1                                           
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0                  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0                
          collisions:0 txqueuelen:1000                                        
          RX bytes:0 TX bytes:0                                               
                                                                              
eth0      Link encap:Ethernet  HWaddr 5e63dd:0b  Driver rk_gmac-dwmac 
          UP BROADCAST MULTICAST  MTU:1500  Metric:1                          
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0                  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0                
          collisions:0 txqueuelen:1000                                        
          RX bytes:0 TX bytes:0                                               
          Interrupt:38                                                        
                                                                              
ip6_vti0  Link encap:UNSPEC                                                   
          NOARP  MTU:1364  Metric:1                                           
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0                  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0                
          collisions:0 txqueuelen:1000                                        
          RX bytes:0 TX bytes:0                                               

 

二、程序设计

0.程序框图

USB接口

1. 设置程序为守护进程

因为该程序最终要在后台执行,并且常驻内存,所以必须将该进程设置为守护进程

参考代码如下:

 

void init_daemon(void)
{
 int pid;
 int i;
 
 if(pid=fork())
  exit(0);//是父进程,结束父进程?
 
 else if(pid< 0)
  exit(1);//fork失败,退出?
 
//是第一子进程,后台继续执行?
 setsid();//第一子进程成为新的会话组长和进程组长?
//并与控制终端分离?
 chdir("/tmp");//改变工作目录到/tmp?
 umask(0);//重设文件创建掩模?
 
 for(i=0;i< NOFILE;++i)//关闭打开的文件描述符?
  close(i);

 return;
}

 

2. 确认制定网口是否存在?

要确认指定网口是否存在,主要通过/proc/net/dev 目录下是否有该网口信息:

 

peng@ubuntu:~$ cat /proc/net/dev
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:   26163     292    0    0    0     0          0         0    26163     292    0    0    0     0       0          0
  eth0: 285444708  243273    0    0    0     0          0         0 91828270   88660    0    0    0     0       0          0

 

参考代码:

 

static char * interface_name_cut (char *buf, char **name)
{
  char *stat;
  /* Skip white space.  Line will include header spaces. */
  while (*buf == ' ')
    buf++;
  *name = buf;
  /* Cut interface name. */
  stat = strrchr (buf, ':');
  *stat++ = '�';
  return stat;
}
/*
return value:1 exist 0:no
*/ 
int check_interface_fromproc(char *interface)
{
  FILE *fp;
  char buf[PROCBUFSIZ];
  struct interface *ifp;
  char *name;
 
  /* Open /proc/net/dev. */
  fp = fopen (_PATH_PROC_NET_DEV, "r");
  if (fp == NULL)
    {   
        printf("open proc file error
");
      return -1; 
    }   
 
  /* Drop header lines. */
  fgets (buf, PROCBUFSIZ, fp);
  fgets (buf, PROCBUFSIZ, fp);
 
  /* Only allocate interface structure.  Other jobs will be done in
     if_ioctl.c. */
  while (fgets (buf, PROCBUFSIZ, fp) != NULL)
    {   
      interface_name_cut (buf, &name);
      if(strcmp(interface,name)==0)
          return 1;
    }   
  fclose(fp);
  return 0;
}

 

3. 指定网口不存在

如果检测网口不存在,则需要休眠,然后继续监测/proc/net/dev文件。

4. 如果指定网口存在

则获取该网口的IP地址,然后比较是否是指定的IP地址

网卡IP地址的获取,主要通过系统调用ioctl()的 SIOCGIFADDR命令实现

参考代码如下:

 

int getLocalIp(const char *eth, char *ip) {
    struct ifreq ifr;
    struct sockaddr_in sin;
    int fd;
    bzero(&ifr, sizeof(ifr));
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        return -1;
    }
    strcpy(ifr.ifr_name, eth);
    if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
        close(fd);
        return -1;
    }
    memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
    snprintf(ip, IP_SIZE, "%s", inet_ntoa(sin.sin_addr));
    close(fd);
    return 0;
}

 

读取的ip地址存放在参数ip指向的内存中。

5. ip地址相同

如果网卡地址与指定的ip地址相同,那么不需要修改地址,休眠一段时间(根据实际操作的频率设置时间),然后再监测网口是否存在

6. IP地址不相同

如果ip地址不相同,则需要修改ip地址,

执行我们提前设置好的脚本if.sh即可

 

#!/bin/bash

IPADDR=192.168.40.8
ETHPORT=eth1
echo "ethcheck set" $ETHPORT $IPADDR
echo $#
echo $0
echo $1
echo $2
if [ $# -eq 2 ];then
 if [ $1 = $ETHPORT ];then 
 echo $ETHPORT
 if [ $2 = "up" ];then
 ifconfig $ETHPORT $IPADDR
 sleep 1
 ip rule add from all lookup main pref 9000 
 sleep 1
 echo 1 > /proc/sys/net/ipv4/ip_forward 
 iptables -F
 echo "set" $ETHPORT "done"
 elif [ $2 = "down" ];then
 echo "down"
 elif [ $2 = "disable" ];then
 echo "disable"
 elif [ $2 = "error" ];then
 echo "error"
 fi
 fi
fi

 





审核编辑:刘清

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分