#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define FAILURE 0
#define SUCCESS 1
typedefunsignedcharUNS8;/* 8-bit */
typedefunsignedshortUNS16;/* 16-bit */
typedefunsignedintUNS32;/* 32-bit */
typedefunsignedlonglongintUNS64;/* 64-bit */
typedefcharS8;/* 8-bit */
typedefshortS16;/* 16-bit */
typedefintS32;/* 32-bit */
typedeflonglongintS64;/* 64-bit */
typedefenum{
FALSE,
TRUE
} BOOL;
#define m_MSG(format, args...) printf(format, ## args)
#define m_ERROR(terminal,format, args...) fprintf(terminal,format, ## args)
#define m_DEBUG(format, args...) printf(format, ## args)
typedefstruct_rt {
UNS32 dst;
UNS32 mask;
UNS32 gw;
S32 flags;
}RT;
S32 setDefaultRoute( S32 fd, S8 *interface, UNS32 *route_addr )
{
structrtentry rtent;
structsockaddr_in *p;
memset( &rtent,0,sizeof(structrtentry ) );
p = ( structsockaddr_in * ) &rtent.rt_dst;
p->sin_family = AF_INET;
p->sin_addr.s_addr = 0;
p = ( structsockaddr_in * ) &rtent.rt_gateway;
p->sin_family = AF_INET;
p->sin_addr.s_addr = *route_addr;
p = ( structsockaddr_in * ) &rtent.rt_genmask;
p->sin_family = AF_INET;
p->sin_addr.s_addr = 0;
rtent.rt_dev = interface;
rtent.rt_metric = 1;
rtent.rt_window = 0;
rtent.rt_flags = RTF_UP | RTF_GATEWAY;
if( ioctl( fd,SIOCADDRT,&rtent ) == -1 ) {
if( errno == ENETUNREACH )/* possibly gateway is over the bridge */{
/* try adding a route to gateway first */
memset( &rtent,0,sizeof(structrtentry ) );
p = ( structsockaddr_in * ) &rtent.rt_dst;
p->sin_family = AF_INET;
p->sin_addr.s_addr = *route_addr;
p = ( structsockaddr_in * ) &rtent.rt_gateway;
p->sin_family = AF_INET;
p->sin_addr.s_addr = 0;
p = ( structsockaddr_in * ) &rtent.rt_genmask;
p->sin_family = AF_INET;
p->sin_addr.s_addr = 0xffffffff;
rtent.rt_dev = interface;
rtent.rt_metric = 0;
rtent.rt_flags = RTF_UP | RTF_HOST;
if( ioctl( fd,SIOCADDRT,&rtent ) == 0 ) {
memset( &rtent,0,sizeof(structrtentry ) );
p = ( structsockaddr_in * ) &rtent.rt_dst;
p->sin_family = AF_INET;
p->sin_addr.s_addr = 0;
p = ( structsockaddr_in * ) &rtent.rt_gateway;
p->sin_family = AF_INET;
p->sin_addr.s_addr = *route_addr;
p = ( structsockaddr_in * ) &rtent.rt_genmask;
p->sin_family = AF_INET;
p->sin_addr.s_addr = 0;
rtent.rt_dev = interface;
rtent.rt_metric = 1;
rtent.rt_window = 0;
rtent.rt_flags = RTF_UP | RTF_GATEWAY ;
if( ioctl( fd,SIOCADDRT,&rtent ) == -1 ) {
m_ERROR(stderr,"ioctl SIOCADDRT: %m\n");
returnFAILURE;
}
}
} else{
m_ERROR(stderr,"ioctl SIOCADDRT: %m\n");
returnFAILURE;
}
}
returnSUCCESS;
}
//通过读取 /proc/net/route获取路由表
S32 get_route( FILE*fp,RT *rt )
{
S8 devname[64] ;
UNS32 d, g, m;
S32 flgs,ref,use,metric,mtu,win,ir;
S32 r;
if( fp == NULL ) {
m_DEBUG("I am here 1\n\n");
returnFAILURE;
}
r = fscanf( fp,"%63s%lx%lx%X%d%d%d%lx%d%d%d\n",devname,&d,&g,&flgs,&ref,&use,&metric,&m,&mtu,&win,&ir );
if( r != 11 ) {
if( ( r 0 ) && feof( fp ) ) {
/* EOF with no (nonspace) chars read. */
m_DEBUG("I am here 3\n\n");
returnFAILURE;
}
m_DEBUG("I am here 4\n\n");
returnFAILURE;
}
if( !( flgs & RTF_UP ) ) {
/* Skip interfaces that are down. */
m_DEBUG("I am here 5\n\n");
returnget_route( fp,rt );
}
rt->dst = d;
rt->mask = m;
rt->gw = g;
rt->flags = flgs;
returnSUCCESS;
}
S32 read_interface( S8 *interface, UNS32 *ipaddr, UNS32 *netmaskaddr, UNS32 *gwaddr )
{
S32 fd;
structifreq ifr;
structsockaddr_in*our_ip;
RT rt;
S32 fret;
FILE*fp = NULL;
*ipaddr = 0;
*netmaskaddr = 0;
*gwaddr = 0;
memset( &ifr,0,sizeof(structifreq ) );
if( ( fd = socket( AF_INET,SOCK_RAW,IPPROTO_RAW ) ) >= 0 ) {
ifr.ifr_addr.sa_family = AF_INET;
strcpy( ifr.ifr_name,interface );
if( ipaddr ) {
bzero(ipaddr,sizeof(UNS32));
if( ioctl( fd,SIOCGIFADDR,&ifr ) == 0 ) {
our_ip = ( structsockaddr_in * ) &ifr.ifr_addr;
*ipaddr = our_ip->sin_addr.s_addr;
} else{
m_ERROR(stderr, "SIOCGIFADDR failed, is the interface up and configured?: %m");
close( fd );
returnFAILURE;
}
}
if( netmaskaddr ) {
bzero(netmaskaddr,sizeof(UNS32));
if( ioctl( fd,SIOCGIFNETMASK,&ifr ) == 0 ) {
our_ip = ( structsockaddr_in * ) &ifr.ifr_netmask;
*netmaskaddr = our_ip->sin_addr.s_addr;
} else{
m_ERROR(stderr,"SIOCGIFHWADDR failed!: %m");
close( fd );
returnFAILURE;
}
}
} else{
m_ERROR(stderr,"socket failed!: %m");
returnFAILURE;
}
close( fd );
if( gwaddr ) {
bzero(gwaddr,sizeof(UNS32));
fp = fopen( "/proc/net/route","r");
if( fp == NULL ) {
returnFAILURE;
}else{
if( fscanf( fp,"%*[^\n]\n") 0 ) {/* Skip the first line. */
/* Empty or missing line, or read error. */
m_DEBUG("I am here 2\n\n");
returnFAILURE;
}
}
while( ( fret = get_route( fp,&rt ) ) == SUCCESS ) {
m_DEBUG("DST:0x%08x,FLAGS:0x%08x,GW:0x%08x,MASK:0x%08x\n",rt.dst,rt.flags,rt.gw,rt.mask);
if( rt.flags & RTF_GATEWAY ) {
*gwaddr = rt.gw;
break;
}
}
fclose(fp);
if( fret != SUCCESS ) {
returnFAILURE;
}
}
returnSUCCESS;
}
S32 write_interface( S8 *interface, UNS32 *ipaddr, UNS32 *netmaskaddr, UNS32 *gwaddr )
{
S32 fd;
structifreq ifr;
structsockaddr_in*p = (structsockaddr_in * ) &( ifr.ifr_addr );
memset( &ifr,0,sizeof(structifreq ) );
if( interface ) {
strcpy( ifr.ifr_name,interface );
} else{
returnFAILURE;
}
if( ( fd = socket( AF_INET,SOCK_STREAM,0 ) ) >= 0 ) {
ifr.ifr_addr.sa_family = AF_INET;
if( ipaddr ) {
p->sin_addr.s_addr = *ipaddr;
if( ioctl( fd,SIOCSIFADDR,&ifr ) == -1 ) {/* setting IP address */
m_ERROR(stderr,"ioctl SIOCSIFADDR: %m\n");
returnFAILURE;
}
}
if( netmaskaddr ) {
p->sin_addr.s_addr = *netmaskaddr;
if( ioctl( fd,SIOCSIFNETMASK,&ifr ) == -1 ) {/* setting netmask */
p->sin_addr.s_addr = 0xffffffff; /* try 255.255.255.255 */
if( ioctl( fd,SIOCSIFNETMASK,&ifr ) == -1 ) {
m_ERROR(stderr,"ioctl SIOCSIFNETMASK: %m\n");
returnFAILURE;
}
}
}
if(ipaddr && netmaskaddr){
p->sin_addr.s_addr=*ipaddr|(~*netmaskaddr);
if( ioctl(fd,SIOCSIFBRDADDR,&ifr) == -1 ) {/* setting broadcast address */
m_ERROR(stderr,"ioctl SIOCSIFBRDADDR: %m\n");
returnFAILURE;
}
}
if( gwaddr ) {
returnsetDefaultRoute( fd,interface,gwaddr );
}
close( fd );
} else{
m_ERROR(stderr,"socket failed!: %m");
returnFAILURE;
}
returnSUCCESS;
}
intmain(void)
{
S8 *interface="eth0";
structin_addr ipaddr;
structin_addr netmaskaddr;
structin_addr gwaddr;
inet_aton("192.168.2.18",&ipaddr);
inet_aton("255.240.0.0",&netmaskaddr);
inet_aton("192.168.2.3",&gwaddr);
// write_interface(interface,(UNS32 *)&ipaddr,(UNS32 *)&netmaskaddr,(UNS32 *)&gwaddr);
bzero(&ipaddr,sizeof(structin_addr));
bzero(&netmaskaddr,sizeof(structin_addr));
bzero(&gwaddr,sizeof(structin_addr));
while(1){
read_interface(interface,(UNS32 *)&ipaddr,(UNS32 *)&netmaskaddr,(UNS32 *)&gwaddr);
m_DEBUG("ipaddr:%s\n",inet_ntoa(ipaddr));
m_DEBUG("netmask:%s\n",inet_ntoa(netmaskaddr));
m_DEBUG("gateway:%s\n",inet_ntoa(gwaddr));
usleep(500000);
}
returnSUCCESS;
}
from:
http://read.pudn.com/downloads86/sourcecode/unix_linux/network/332436/ifconf.c__.htm
ipget.c written by myself:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main (int argc, char *argv[])
{
int i=0;
int sockfd;
struct ifconf ifconf;
unsigned char buf[512];
struct ifreq *ifreq;
ifconf.ifc_len = 512;
ifconf.ifc_buf = buf;
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0) {
perror("socket");
exit(EXIT_FAILURE);
}
ioctl(sockfd, SIOCGIFCONF, &ifconf);
ifreq = (struct ifreq *)buf;
for(i = ifconf.ifc_len/sizeof(struct ifreq); i > 0; i--)
{
printf("name = [%s]\n", ifreq->ifr_name);
printf("local addr = [%s]\n",
inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));
ifreq++;
}
close(sockfd);
exit(EXIT_SUCCESS);
}
/* Interface request structure used for socket ioctl's. All interface
ioctl's must have parameter definitions which begin with ifr_name.
The remainder may be interface specific. */
struct ifreq
{
#ifndef IFNAMSIZ
#define IFNAMSIZ 16
#endif
char ifr_name[IFNAMSIZ];
union {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
__ulong32_t ifru_flags;
int ifru_metric;
CADdr_t ifru_data;
u_short ifru_site6;
__ulong32_t ifru_mtu;
int ifru_baudrate;
} ifr_ifru;
//Following Macros are provided for convenIEnce
#define ifr_addr ifr_ifru.ifru_addr /* address */
#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */
#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
#define ifr_flags ifr_ifru.ifru_flags /* flags */
#define ifr_metric ifr_ifru.ifru_metric /* metric */
#define ifr_data ifr_ifru.ifru_data /* for use by interface */
#define ifr_site6 ifr_ifru.ifru_site6 /* IPv6 site index */
#define ifr_mtu ifr_ifru.ifru_mtu /* mtu of interface */
#define ifr_isno ifr_ifru.ifru_data /* pointer to if_netopts */
#define ifr_baudrate ifr_ifru.ifru_baudrate /* baudrate of interface */
};
/* Structure used in SIOCGIFCONF request. Used to retrieve interface
configuration for machine (useful for programs which must know all
networks accessible). */
struct ifconf
{
int ifc_len; /* size of associated buffer */
union
{
caddr_t ifcu_buf;
struct ifreq *ifcu_req;
} ifc_ifcu;
Following macros are provided for convenience
#define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
#define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */
};
SIOCGIFADDR:获取接口地址,检索特定接口的地址
SIOCSIFADDR:设置接口地址。ifr.ifr_addr 字段返回地址。
SIOCGIFCONF:获取所有配置接口的信息,返回系统中配置的所有接口的配置信息