嵌入式 hi3518平台增加路由代码

<span style="font-family:Courier New;">
/********************************** (C) COPYRIGHT *******************************
* File Name          : add_route.c
* Author             : skdkjzz
* Date               : 2014/08/09
* Description        : 设置静态ip,添加路由表
*********************************************************************************/

#include <error.h> 
#include <stdio.h> 
#include <string.h>
#include <net/if.h>
#include <stdlib.h> 
#include <arpa/inet.h> 
#include <asm/types.h>            /* glibc 2 conflicts with linux/types.h */
#include <net/route.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>  
#include <net/if_arp.h>  
#include <netinet/if_ether.h>

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <dirent.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/vfs.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/route.h>
#include <net/if_arp.h>

#define SET_SA_FAMILY(addr, family)                        \
    memset ((char *) &(addr), '\0', sizeof(addr));        \
    addr.sa_family = (family);

#define SIN_ADDR(x)        (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr)

int SetIfAddr(char *Ifname, char *Ipaddr, char *mask)  
{  
   int fd;  
   //int rc;  
   struct ifreq ifr;   
   struct sockaddr_in *sin;  
   struct rtentry  rt;  
 
    fd = socket(AF_INET, SOCK_DGRAM, 0);  
   if(fd < 0)  
   {  
            perror("socket  error");       
           return -2;       
    }  
    memset(&ifr,0,sizeof(ifr));   
    strcpy(ifr.ifr_name,Ifname);   
   sin = (struct sockaddr_in*)&ifr.ifr_addr;       
   sin->sin_family = AF_INET;  
   
    //IP地址  
   if(inet_aton(Ipaddr,&(sin->sin_addr)) < 0)     
    {       
       perror("inet_aton   error");       
        return -3;       
    }      
  
   if(ioctl(fd,SIOCSIFADDR,&ifr) < 0)     
    {       
        perror("ioctl   SIOCSIFADDR   error");       
        return -4;       
    }  
    //子网掩码  
   if(inet_aton(mask,&(sin->sin_addr)) < 0)     
   {       
       perror("inet_pton   error");       
       return -5;       
    }      
   if(ioctl(fd, SIOCSIFNETMASK, &ifr) < 0)  
    {  
       perror("ioctl");  
        return -6;  
    }  

   return 0;
   close(fd);  
 }

/*
 * funtion:set the file lock
 * params:fd is file Num,type is F_WRLCk,F_RDLCK or F_UNLCK
 -----------------------
 * time:2012-08-22 22:17
 * version:1.0
 */
int joseph_lock_set(int fd, int type)
{
	struct flock  lock_file;
	lock_file.l_whence = SEEK_SET;
	lock_file.l_start = 0;
	lock_file.l_len = 0;
	lock_file.l_type = type;
	lock_file.l_pid = -1;
	fcntl(fd, F_GETLK, &lock_file);

	if (lock_file.l_type != F_UNLCK)
	{
		if (lock_file.l_type == F_RDLCK) 
		{
		}
		else if (lock_file.l_type == F_WRLCK) 
		{
		}			
	}
	lock_file.l_type = type;
	if ((fcntl(fd, F_SETLKW, &lock_file)) < 0)
	{
		return 1;
	}
	switch(lock_file.l_type)
	{
		case F_RDLCK:
			{
			}
			break;
		case F_WRLCK:
			{
			}
			break;
		case F_UNLCK:
			{
				return 1;
			}
			break;
		default:
			break;
	}/* end of switch  */
	return 0;
}

#define JOSEPH_ETH0_IFCG "/etc/sysconfig/network-scripts/ifcfg-eth0"

int joseph_read_user_file(char *param_name)
{
	int fd;
	int param_value;
	char buf_name[64];
	char buf[128];
	FILE *fp;
	strcpy(buf_name,param_name);
	fp = fopen(JOSEPH_ETH0_IFCG, "r");
	if(fp == NULL)
	{
		return -1;
	}
	fd=fileno(fp);

	joseph_lock_set(fd, F_RDLCK);//set the write lock
	while(fgets(buf, sizeof(buf), fp)){

		if(strstr(buf,buf_name)){

			printf("%s:[%d] %s = %s",__FUNCTION__,__LINE__,param_name,(buf+strlen(buf_name)+1));

		}
	}

	joseph_lock_set(fd, F_UNLCK);// unlock the file
	fclose(fp);

	return 0;
}

void joseph_alter_ipnc_param(char *joseph_param_name, char *joseph_param_values)
{
	char buf[1024];
	char conf_path[64] = JOSEPH_ETH0_IFCG;

	if((joseph_param_name == NULL) ||( joseph_param_values == NULL))
	{
		return;
	}

	if((strlen(joseph_param_name) == 0) ||( strlen(joseph_param_values) == 0))
	{
		return;
	}
	sprintf(buf,"sed -i 's/%s=.*/%s=%s/g' %s",\
			joseph_param_name,joseph_param_name,joseph_param_values,conf_path);
	system(buf);
	return;
}

int  main ()
{

#if 1
    static int sock_fd = -1;        /* socket for doing interface ioctls */
    struct rtentry rt;
    FILE *db_fd = (FILE *) 0;
    char ifname[32];
    u_int32_t dstaddr, gateway;

    int j,k;
    char *p;
    FILE *fp;
    char cmd[30];
    char cmd1[5][20];
    memset(cmd, 0, sizeof(cmd));
    memset(cmd1, 0, sizeof(cmd1));
 if((fp=fopen("/etc/sysconfig/network-scripts/ifcfg-eth0","r"))==NULL){
    		printf("Read ifcfg-eth0 error!\n");
        return -1;
    	}
    for(j=0;j<5;j++){
    if((p=fgets(cmd,1024,fp))==NULL){
        printf("read data error!\n");
        return -2;
      }
    while(*p!='=')p++;
    p++; 
    k=0;
    while(*p!='\n')
    { 
    	cmd1[j][k]=*p;
    	k++;
    	p++; 
    }
    memset(cmd, 0, sizeof(cmd));
    
    }


   if(SetIfAddr(cmd1[0],cmd1[1],cmd1[2])!=0)
  {
       printf("config ip failed!\n");
        return -7;	
  }  

 
    strcpy(ifname, cmd1[0]);
    dstaddr = inet_addr(cmd1[3]);
    gateway = inet_addr(cmd1[4]);

    /* Get an internet socket for doing socket ioctls. */
    sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
    
    /*open for debug*/
    db_fd = fopen("/tmp/addrt_db.txt","a+");
    fprintf(db_fd, "ifname=%s ouraddr=%x gateway=%x\n", ifname, dstaddr, gateway);
    memset (&rt, 0, sizeof (rt));
    /*set Destination addr*/
    SET_SA_FAMILY (rt.rt_dst, AF_INET);
    SIN_ADDR(rt.rt_dst) = dstaddr;
   
    /*set gw addr*/
    SET_SA_FAMILY (rt.rt_gateway, AF_INET);
    SIN_ADDR(rt.rt_gateway) = gateway;
    fprintf(db_fd,"mygateway=%x\n", SIN_ADDR(rt.rt_gateway));
       
    /*set genmask addr*/
    SET_SA_FAMILY (rt.rt_genmask, AF_INET); 
    SIN_ADDR(rt.rt_genmask) = 0L;
   
    rt.rt_dev = ifname;

    rt.rt_flags = RTF_GATEWAY;

    if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) 
	{
      fprintf(db_fd,"route add err num=%m\n",errno);
	  printf("Err\n");
      return 0;
    }
        
    fprintf(db_fd,"route add success route=%x\n", SIN_ADDR(rt.rt_gateway));
    fclose(db_fd);
 #else
 joseph_read_user_file("DEVICE");
 joseph_read_user_file("IPADDR");
 joseph_read_user_file("GATEWAY");
 joseph_read_user_file("DNS");
 joseph_read_user_file("BROADCAST");
 joseph_read_user_file("BOOTPROTO");
 joseph_read_user_file("ONBOOT");
 joseph_alter_ipnc_param("IPADDR","10.10.1.250");
 joseph_read_user_file("IPADDR");
 #endif
 
    return 0;
}
</span>

你可能感兴趣的:(嵌入式 hi3518平台增加路由代码)