- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <net/if.h>
- #include <errno.h>
- #include <features.h>
- #include <netpacket/packet.h>
- #include <net/ethernet.h>
- #include <net/route.h>
- #include <stdio.h>
-
- #define FAILURE 0
- #define SUCCESS 1
- typedef unsigned char UNS8;
- typedef unsigned short UNS16;
- typedef unsigned int UNS32;
- typedef unsigned long long int UNS64;
- typedef char S8;
- typedef short S16;
- typedef int S32;
- typedef long long int S64;
- typedef enum {
- 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)
-
-
- typedef struct _rt {
- UNS32 dst;
- UNS32 mask;
- UNS32 gw;
- S32 flags;
- }RT;
- S32 setDefaultRoute( S32 fd, S8 *interface, UNS32 *route_addr )
- {
- struct rtentry rtent;
- struct sockaddr_in *p;
-
- memset( &rtent,0,sizeof( struct rtentry ) );
- p = ( struct sockaddr_in * ) &rtent.rt_dst;
- p->sin_family = AF_INET;
- p->sin_addr.s_addr = 0;
- p = ( struct sockaddr_in * ) &rtent.rt_gateway;
- p->sin_family = AF_INET;
- p->sin_addr.s_addr = *route_addr;
- p = ( struct sockaddr_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 ) {
-
- memset( &rtent,0,sizeof( struct rtentry ) );
- p = ( struct sockaddr_in * ) &rtent.rt_dst;
- p->sin_family = AF_INET;
- p->sin_addr.s_addr = *route_addr;
- p = ( struct sockaddr_in * ) &rtent.rt_gateway;
- p->sin_family = AF_INET;
- p->sin_addr.s_addr = 0;
- p = ( struct sockaddr_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( struct rtentry ) );
- p = ( struct sockaddr_in * ) &rtent.rt_dst;
- p->sin_family = AF_INET;
- p->sin_addr.s_addr = 0;
- p = ( struct sockaddr_in * ) &rtent.rt_gateway;
- p->sin_family = AF_INET;
- p->sin_addr.s_addr = *route_addr;
- p = ( struct sockaddr_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");
- return FAILURE;
- }
- }
- } else {
- m_ERROR(stderr,"ioctl SIOCADDRT: %m\n" );
- return FAILURE;
- }
- }
- return SUCCESS;
- }
-
- 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");
- return FAILURE;
- }
- 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 ) ) {
-
- m_DEBUG("I am here 3\n\n");
- return FAILURE;
- }
- m_DEBUG("I am here 4\n\n");
- return FAILURE;
- }
-
- if ( !( flgs & RTF_UP ) ) {
-
- m_DEBUG("I am here 5\n\n");
- return get_route( fp,rt );
- }
-
- rt->dst = d;
- rt->mask = m;
- rt->gw = g;
- rt->flags = flgs;
-
- return SUCCESS;
- }
- S32 read_interface( S8 *interface, UNS32 *ipaddr, UNS32 *netmaskaddr, UNS32 *gwaddr )
- {
- S32 fd;
- struct ifreq ifr;
- struct sockaddr_in*our_ip;
- RT rt;
- S32 fret;
- FILE *fp = NULL;
-
- *ipaddr = 0;
- *netmaskaddr = 0;
- *gwaddr = 0;
- memset( &ifr,0,sizeof( struct ifreq ) );
- 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 = ( struct sockaddr_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 );
- return FAILURE;
- }
- }
- if ( netmaskaddr ) {
- bzero(netmaskaddr,sizeof(UNS32));
- if ( ioctl( fd,SIOCGIFNETMASK,&ifr ) == 0 ) {
- our_ip = ( struct sockaddr_in * ) &ifr.ifr_netmask;
- *netmaskaddr = our_ip->sin_addr.s_addr;
- } else {
- m_ERROR(stderr,"SIOCGIFHWADDR failed!: %m" );
- close( fd );
- return FAILURE;
- }
- }
- } else {
- m_ERROR(stderr,"socket failed!: %m" );
- return FAILURE;
- }
- close( fd );
- if ( gwaddr ) {
- bzero(gwaddr,sizeof(UNS32));
- fp = fopen( "/proc/net/route","r" );
- if ( fp == NULL ) {
- return FAILURE;
- }else{
- if ( fscanf( fp,"%*[^\n]\n" ) 0 ) {
-
- m_DEBUG("I am here 2\n\n");
- return FAILURE;
- }
- }
- 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 ) {
- return FAILURE;
- }
- }
-
- return SUCCESS;
- }
-
- S32 write_interface( S8 *interface, UNS32 *ipaddr, UNS32 *netmaskaddr, UNS32 *gwaddr )
- {
- S32 fd;
- struct ifreq ifr;
- struct sockaddr_in*p = ( struct sockaddr_in * ) &( ifr.ifr_addr );
-
- memset( &ifr,0,sizeof( struct ifreq ) );
- if ( interface ) {
- strcpy( ifr.ifr_name,interface );
- } else {
- return FAILURE;
- }
-
-
- 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 ) {
- m_ERROR(stderr,"ioctl SIOCSIFADDR: %m\n" );
- return FAILURE;
- }
- }
- if ( netmaskaddr ) {
- p->sin_addr.s_addr = *netmaskaddr;
- if ( ioctl( fd,SIOCSIFNETMASK,&ifr ) == -1 ) {
- p->sin_addr.s_addr = 0xffffffff;
- if ( ioctl( fd,SIOCSIFNETMASK,&ifr ) == -1 ) {
- m_ERROR(stderr,"ioctl SIOCSIFNETMASK: %m\n" );
- return FAILURE;
- }
- }
- }
- if (ipaddr && netmaskaddr){
- p->sin_addr.s_addr=*ipaddr|(~*netmaskaddr);
- if ( ioctl(fd,SIOCSIFBRDADDR,&ifr) == -1 ) {
- m_ERROR(stderr,"ioctl SIOCSIFBRDADDR: %m\n");
- return FAILURE;
- }
- }
- if ( gwaddr ) {
- return setDefaultRoute( fd,interface,gwaddr );
- }
- close( fd );
- } else {
- m_ERROR(stderr,"socket failed!: %m" );
- return FAILURE;
- }
-
- return SUCCESS;
- }
-
- int main(void)
- {
- S8 *interface="eth0";
-
- struct in_addr ipaddr;
- struct in_addr netmaskaddr;
- struct in_addr gwaddr;
-
- inet_aton("192.168.2.18",&ipaddr);
- inet_aton("255.240.0.0",&netmaskaddr);
- inet_aton("192.168.2.3",&gwaddr);
-
-
- bzero(&ipaddr,sizeof(struct in_addr));
- bzero(&netmaskaddr,sizeof(struct in_addr));
- bzero(&gwaddr,sizeof(struct in_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);
- }
- return SUCCESS;
-
- }
from:
http://read.pudn.com/downloads86/sourcecode/unix_linux/network/332436/ifconf.c__.htm
ipget.c written by myself:
#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
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:获取所有配置接口的信息,返回系统中配置的所有接口的配置信息
posted on 2011-05-20 12:30
chatler 阅读(873)
评论(0) 编辑 收藏 引用 所属分类:
Linux_Coding