zoukankan      html  css  js  c++  java
  • 嵌入式 hi3518平台增加路由代码

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-family:Courier New;">  
    2. /********************************** (C) COPYRIGHT *******************************  
    3. * File Name          : add_route.c  
    4. * Author             : skdkjzz  
    5. * Date               : 2014/08/09  
    6. * Description        : 设置静态ip,添加路由表  
    7. *********************************************************************************/  
    8.   
    9. #include <error.h>   
    10. #include <stdio.h>   
    11. #include <string.h>  
    12. #include <net/if.h>  
    13. #include <stdlib.h>   
    14. #include <arpa/inet.h>   
    15. #include <asm/types.h>            /* glibc 2 conflicts with linux/types.h */  
    16. #include <net/route.h>  
    17. #include <sys/errno.h>  
    18. #include <sys/ioctl.h>  
    19. #include <sys/types.h>  
    20. #include <sys/socket.h>  
    21. #include <netinet/in.h>    
    22. #include <net/if_arp.h>    
    23. #include <netinet/if_ether.h>  
    24.   
    25. #include <fcntl.h>  
    26. #include <stdio.h>  
    27. #include <stdlib.h>  
    28. #include <errno.h>  
    29. #include <string.h>  
    30. #include <unistd.h>  
    31. #include <signal.h>  
    32. #include <dirent.h>  
    33. #include <arpa/inet.h>  
    34. #include <pthread.h>  
    35. #include <sys/vfs.h>  
    36. #include <sys/time.h>  
    37. #include <sys/wait.h>  
    38. #include <sys/ioctl.h>  
    39. #include <sys/types.h>  
    40. #include <sys/socket.h>  
    41. #include <net/if.h>  
    42. #include <netinet/in.h>  
    43. #include <net/route.h>  
    44. #include <net/if_arp.h>  
    45.   
    46. #define SET_SA_FAMILY(addr, family)                          
    47.     memset ((char *) &(addr), '', sizeof(addr));          
    48.     addr.sa_family = (family);  
    49.   
    50. #define SIN_ADDR(x)        (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr)  
    51.   
    52. int SetIfAddr(char *Ifname, char *Ipaddr, char *mask)    
    53. {    
    54.    int fd;    
    55.    //int rc;    
    56.    struct ifreq ifr;     
    57.    struct sockaddr_in *sin;    
    58.    struct rtentry  rt;    
    59.    
    60.     fd = socket(AF_INET, SOCK_DGRAM, 0);    
    61.    if(fd 0)    
    62.    {    
    63.             perror("socket  error");         
    64.            return -2;         
    65.     }    
    66.     memset(&ifr,0,sizeof(ifr));     
    67.     strcpy(ifr.ifr_name,Ifname);     
    68.    sin = (struct sockaddr_in*)&ifr.ifr_addr;         
    69.    sin->sin_family = AF_INET;    
    70.      
    71.     //IP地址    
    72.    if(inet_aton(Ipaddr,&(sin->sin_addr)) 0)       
    73.     {         
    74.        perror("inet_aton   error");         
    75.         return -3;         
    76.     }        
    77.     
    78.    if(ioctl(fd,SIOCSIFADDR,&ifr) 0)       
    79.     {         
    80.         perror("ioctl   SIOCSIFADDR   error");         
    81.         return -4;         
    82.     }    
    83.     //子网掩码    
    84.    if(inet_aton(mask,&(sin->sin_addr)) 0)       
    85.    {         
    86.        perror("inet_pton   error");         
    87.        return -5;         
    88.     }        
    89.    if(ioctl(fd, SIOCSIFNETMASK, &ifr) 0)    
    90.     {    
    91.        perror("ioctl");    
    92.         return -6;    
    93.     }    
    94.   
    95.    return 0;  
    96.    close(fd);    
    97.  }  
    98.   
    99. /*  
    100.  * funtion:set the file lock  
    101.  * params:fd is file Num,type is F_WRLCk,F_RDLCK or F_UNLCK  
    102.  -----------------------  
    103.  * time:2012-08-22 22:17  
    104.  * version:1.0  
    105.  */  
    106. int joseph_lock_set(int fd, int type)  
    107. {  
    108.     struct flock  lock_file;  
    109.     lock_file.l_whence = SEEK_SET;  
    110.     lock_file.l_start = 0;  
    111.     lock_file.l_len = 0;  
    112.     lock_file.l_type = type;  
    113.     lock_file.l_pid = -1;  
    114.     fcntl(fd, F_GETLK, &lock_file);  
    115.   
    116.     if (lock_file.l_type != F_UNLCK)  
    117.     {  
    118.         if (lock_file.l_type == F_RDLCK)   
    119.         {  
    120.         }  
    121.         else if (lock_file.l_type == F_WRLCK)   
    122.         {  
    123.         }             
    124.     }  
    125.     lock_file.l_type = type;  
    126.     if ((fcntl(fd, F_SETLKW, &lock_file)) 0)  
    127.     {  
    128.         return 1;  
    129.     }  
    130.     switch(lock_file.l_type)  
    131.     {  
    132.         case F_RDLCK:  
    133.             {  
    134.             }  
    135.             break;  
    136.         case F_WRLCK:  
    137.             {  
    138.             }  
    139.             break;  
    140.         case F_UNLCK:  
    141.             {  
    142.                 return 1;  
    143.             }  
    144.             break;  
    145.         default:  
    146.             break;  
    147.     }/* end of switch  */  
    148.     return 0;  
    149. }  
    150.   
    151. #define JOSEPH_ETH0_IFCG "/etc/sysconfig/network-scripts/ifcfg-eth0"  
    152.   
    153. int joseph_read_user_file(char *param_name)  
    154. {  
    155.     int fd;  
    156.     int param_value;  
    157.     char buf_name[64];  
    158.     char buf[128];  
    159.     FILE *fp;  
    160.     strcpy(buf_name,param_name);  
    161.     fp = fopen(JOSEPH_ETH0_IFCG, "r");  
    162.     if(fp == NULL)  
    163.     {  
    164.         return -1;  
    165.     }  
    166.     fd=fileno(fp);  
    167.   
    168.     joseph_lock_set(fd, F_RDLCK);//set the write lock  
    169.     while(fgets(buf, sizeof(buf), fp)){  
    170.   
    171.         if(strstr(buf,buf_name)){  
    172.   
    173.             printf("%s:[%d] %s = %s",__FUNCTION__,__LINE__,param_name,(buf+strlen(buf_name)+1));  
    174.   
    175.         }  
    176.     }  
    177.   
    178.     joseph_lock_set(fd, F_UNLCK);// unlock the file  
    179.     fclose(fp);  
    180.   
    181.     return 0;  
    182. }  
    183.   
    184. void joseph_alter_ipnc_param(char *joseph_param_name, char *joseph_param_values)  
    185. {  
    186.     char buf[1024];  
    187.     char conf_path[64] = JOSEPH_ETH0_IFCG;  
    188.   
    189.     if((joseph_param_name == NULL) ||( joseph_param_values == NULL))  
    190.     {  
    191.         return;  
    192.     }  
    193.   
    194.     if((strlen(joseph_param_name) == 0) ||( strlen(joseph_param_values) == 0))  
    195.     {  
    196.         return;  
    197.     }  
    198.     sprintf(buf,"sed -i 's/%s=.*/%s=%s/g' %s",  
    199.             joseph_param_name,joseph_param_name,joseph_param_values,conf_path);  
    200.     system(buf);  
    201.     return;  
    202. }  
    203.   
    204. int  main ()  
    205. {  
    206.   
    207. #if 1  
    208.     static int sock_fd = -1;        /* socket for doing interface ioctls */  
    209.     struct rtentry rt;  
    210.     FILE *db_fd = (FILE *) 0;  
    211.     char ifname[32];  
    212.     u_int32_t dstaddr, gateway;  
    213.   
    214.     int j,k;  
    215.     char *p;  
    216.     FILE *fp;  
    217.     char cmd[30];  
    218.     char cmd1[5][20];  
    219.     memset(cmd, 0, sizeof(cmd));  
    220.     memset(cmd1, 0, sizeof(cmd1));  
    221.  if((fp=fopen("/etc/sysconfig/network-scripts/ifcfg-eth0","r"))==NULL){  
    222.             printf("Read ifcfg-eth0 error! ");  
    223.         return -1;  
    224.         }  
    225.     for(j=0;j<5;j++){  
    226.     if((p=fgets(cmd,1024,fp))==NULL){  
    227.         printf("read data error! ");  
    228.         return -2;  
    229.       }  
    230.     while(*p!='=')p++;  
    231.     p++;   
    232.     k=0;  
    233.     while(*p!=' ')  
    234.     {   
    235.         cmd1[j][k]=*p;  
    236.         k++;  
    237.         p++;   
    238.     }  
    239.     memset(cmd, 0, sizeof(cmd));  
    240.       
    241.     }  
    242.   
    243.   
    244.    if(SetIfAddr(cmd1[0],cmd1[1],cmd1[2])!=0)  
    245.   {  
    246.        printf("config ip failed! ");  
    247.         return -7;    
    248.   }    
    249.   
    250.    
    251.     strcpy(ifname, cmd1[0]);  
    252.     dstaddr = inet_addr(cmd1[3]);  
    253.     gateway = inet_addr(cmd1[4]);  
    254.   
    255.     /* Get an internet socket for doing socket ioctls. */  
    256.     sock_fd = socket(AF_INET, SOCK_DGRAM, 0);  
    257.       
    258.     /*open for debug*/  
    259.     db_fd = fopen("/tmp/addrt_db.txt","a+");  
    260.     fprintf(db_fd, "ifname=%s ouraddr=%x gateway=%x ", ifname, dstaddr, gateway);  
    261.     memset (&rt, 0, sizeof (rt));  
    262.     /*set Destination addr*/  
    263.     SET_SA_FAMILY (rt.rt_dst, AF_INET);  
    264.     SIN_ADDR(rt.rt_dst) = dstaddr;  
    265.      
    266.     /*set gw addr*/  
    267.     SET_SA_FAMILY (rt.rt_gateway, AF_INET);  
    268.     SIN_ADDR(rt.rt_gateway) = gateway;  
    269.     fprintf(db_fd,"mygateway=%x ", SIN_ADDR(rt.rt_gateway));  
    270.          
    271.     /*set genmask addr*/  
    272.     SET_SA_FAMILY (rt.rt_genmask, AF_INET);   
    273.     SIN_ADDR(rt.rt_genmask) = 0L;  
    274.      
    275.     rt.rt_dev = ifname;  
    276.   
    277.     rt.rt_flags = RTF_GATEWAY;  
    278.   
    279.     if (ioctl(sock_fd, SIOCADDRT, &rt) 0)   
    280.     {  
    281.       fprintf(db_fd,"route add err num=%m ",errno);  
    282.       printf("Err ");  
    283.       return 0;  
    284.     }  
    285.           
    286.     fprintf(db_fd,"route add success route=%x ", SIN_ADDR(rt.rt_gateway));  
    287.     fclose(db_fd);  
    288.  #else  
    289.  joseph_read_user_file("DEVICE");  
    290.  joseph_read_user_file("IPADDR");  
    291.  joseph_read_user_file("GATEWAY");  
    292.  joseph_read_user_file("DNS");  
    293.  joseph_read_user_file("BROADCAST");  
    294.  joseph_read_user_file("BOOTPROTO");  
    295.  joseph_read_user_file("ONBOOT");  
    296.  joseph_alter_ipnc_param("IPADDR","10.10.1.250");  
    297.  joseph_read_user_file("IPADDR");  
    298.  #endif  
    299.    
    300.     return 0;  
    301. }  
    302. </span>  
  • 相关阅读:
    禁止root远程登录解决办法
    小肥羊
    高级英语
    通用做饭general
    2021/03/10,创新与毁灭
    pandaExpressRound2
    2021/03/08,经历建立的你
    系统与细节
    报税
    事业portfolio
  • 原文地址:https://www.cnblogs.com/lidabo/p/5383966.html
Copyright © 2011-2022 走看看