/******************************************************************************
*
*
*******************************************************************************
* File name: net.c
* Purpose:
* Author: Felix(HuangFei)
* Version: v01.00
* Compiler: arm-linux-gcc
* Created on: 2014-11-28
*******************************************************************************
* Note:
* Revision history:
* Created by HuangFei 2014-11-28
******************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if_arp.h>
#include <unistd.h>
#include <net/if.h>
#include <net/route.h>
#include <string.h>
#include <ifaddrs.h>
#include "net.h"
#define TRUE 1
#define FALSE 0
#define MAC_ADDRESS_LEN 6
#define PATH_PROCNET_ROUTE "/proc/net/route"
#define IFACE_NAME_LENGTH IF_NAMESIZE
/**********************************************************************
* Function: SetMacAddress
* Description: 设置mac地址
* Input: pInterface :网络接口名字 如:eth0,pMacAddress:mac地址首地址
* Return: 成功 1,失败 0
* Others:
**********************************************************************/
unsigned char SetMacAddress(char *pInterface,char *pMacAddress)
{
struct ifreq ifr;
struct sockaddr *hwaddr;
unsigned char *hwptr;
int sockfd;
int i;
/* 判断接口输入是否正确 */
if((pInterface == NULL)||(pInterface[0]!='e')||(pInterface[1]!='t')||(pInterface[2]!='h'))
{
printf("input inteface error!\n");
return FALSE;
}
if(pMacAddress == NULL)
{
printf("input mac address error\n");
return FALSE;
}
sockfd= socket(AF_INET,SOCK_DGRAM,0);
if (sockfd<0)
{
printf("Can't creat socket \r\n");
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, pInterface, sizeof(ifr.ifr_name)-1);
/*读出socket的信息*/
if (0 > ioctl(sockfd, SIOCGIFFLAGS, &ifr))
{
printf("close the ethx:unknown interface\r\n");
}
ifr.ifr_flags &= ~IFF_UP;
/*将信息写到socket中,从而关闭eth0*/
if (0 > ioctl(sockfd, SIOCSIFFLAGS, &ifr))
{
printf("Can't close the ethx \r\n");
return FALSE;
}
/*---------------------------------------------------------------------*/
/* set the interface eth0's MAC address */
/*---------------------------------------------------------------------*/
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, pInterface, sizeof(ifr.ifr_name)-1);
hwaddr=(struct sockaddr *)&ifr.ifr_hwaddr;
/*ARPHRD_ETHER=1,标识接收的ARP为以太网格式*/
hwaddr->sa_family = ARPHRD_ETHER;
hwptr=hwaddr->sa_data;
for(i=0;i<6;i++)
*hwptr++ = (unsigned char)pMacAddress[i];
if (0 > ioctl(sockfd, SIOCSIFHWADDR, &ifr))
{
printf("Can't set the MAC address\r\n");
}
/*---------------------------------------------------------------------*/
/* open the interface eth0 */
/*---------------------------------------------------------------------*/
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, pInterface, sizeof(ifr.ifr_name)-1);
if (0 > ioctl(sockfd,SIOCGIFFLAGS, &ifr))
{
printf("open the ethx:unknown interface\r\n");
}
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
if (0 > ioctl(sockfd, SIOCSIFFLAGS, &ifr))
{
printf("Can't open the ethx\r\n");
}
close(sockfd);
return TRUE;
}
/**********************************************************************
* Function: SetIpAddress
* Description: 设置ip地址
* Input: pInterface :网络接口名字 如:eth0,IpAddress:ip地址的
* Return: 成功 1,失败 0
* Others:
**********************************************************************/
unsigned char SetIpAddress(char *pInterface,unsigned int IpAddress)
{
struct ifreq ifr;
struct sockaddr_in *sinaddr;
int sockfd;
if((pInterface == NULL)||(pInterface[0]!='e')||(pInterface[1]!='t')||(pInterface[2]!='h'))
{
printf("input inteface error!\n");
return FALSE;
}
if(IpAddress == 0)
{
printf("input ip address error\n");
return FALSE;
}
sockfd= socket(AF_INET,SOCK_DGRAM,0);
if (sockfd<0)
{
printf("Can't creat socket \r\n");
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, pInterface, sizeof(ifr.ifr_name)-1);
sinaddr = (struct sockaddr_in *)&ifr.ifr_addr;
sinaddr->sin_family = AF_INET;
sinaddr->sin_addr.s_addr = IpAddress;
if (0 > ioctl(sockfd, SIOCSIFADDR, &ifr))
{
printf("Can't set ip address\r\n");
}
close(sockfd);
return TRUE;
}
/**********************************************************************
* Function: SetMaskAddress
* Description: 设置子网掩码
* Input: pInterface :网络接口名字 如:eth0,IpMask:子网掩码
* Return: 成功 1,失败 0
* Others:
**********************************************************************/
unsigned char SetMaskAddress(char *pInterface,unsigned int IpMask)
{
struct ifreq ifr;
struct sockaddr_in *sinaddr;
int sockfd;
/* 判断接口输入是否正确 */
if((pInterface == NULL)||(pInterface[0]!='e')||(pInterface[1]!='t')||(pInterface[2]!='h'))
{
printf("input inteface error!\n");
return FALSE;
}
if(IpMask == 0)
{
printf("input mask address error\n");
return FALSE;
}
sockfd= socket(AF_INET,SOCK_DGRAM,0);
if (sockfd<0)
{
printf("Can't creat socket \r\n");
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, pInterface, sizeof(ifr.ifr_name)-1);
sinaddr = (struct sockaddr_in *)&ifr.ifr_netmask;
sinaddr->sin_family = AF_INET;
sinaddr->sin_addr.s_addr = IpMask;
if (0 > ioctl(sockfd, SIOCSIFNETMASK, &ifr))
{
printf("Can't set subnet netmask\r\n");
}
close(sockfd);
return TRUE;
}
/**********************************************************************
* Function: DelGateWay
* Description: 删除指定的网关
* Input: pInterface :网络接口名字 如:eth0,DelGateWay:网关
* Return: 成功 1,失败 0
* Others:
**********************************************************************/
unsigned char DelGateWay(char *pInterface,unsigned int DelGateWay)
{
struct sockaddr_in *sinaddr;
struct rtentry rt;
struct sockaddr rtsockaddr;
int sockfd;
/* 判断接口输入是否正确 */
if((pInterface == NULL)||(pInterface[0]!='e')||(pInterface[1]!='t')||(pInterface[2]!='h'))
{
printf("input inteface error!\n");
return FALSE;
}
if(DelGateWay == 0)
{
printf("input GateWay error!\n");
return FALSE;
}
sockfd= socket(AF_INET,SOCK_DGRAM,0);
if (sockfd<0)
{
printf("Can't creat socket \r\n");
}
/* Clean out the RTREQ structure. */
memset((char *) &rt, 0, sizeof(struct rtentry));
memset((char *)&rtsockaddr,0,sizeof(struct sockaddr));
sinaddr = (struct sockaddr_in *)&rtsockaddr;
sinaddr->sin_family = AF_INET;
/*INADDR_ANY就是指定地址为0.0.0.0的地址,这个地址事实上表示不确定地址,或“所有地址”、“任意地址”*/
sinaddr->sin_addr.s_addr = INADDR_ANY;
/*set the destination address to '0.0.0.0'*/
rt.rt_dst = rtsockaddr;
/*set the netmask to '0.0.0.0'*/
rt.rt_genmask = rtsockaddr;
/* Fill in the other fields. */
rt.rt_flags = (RTF_UP|RTF_GATEWAY);
rt.rt_metric = 1;
rt.rt_dev = pInterface;
/*delete the current default gateway*/
sinaddr->sin_addr.s_addr = DelGateWay;
rt.rt_gateway = rtsockaddr;
if (0 > ioctl(sockfd, S
- 1
- 2
前往页