linux下获取本地IP信息的

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
//#include <fcntl.h>

#include <sys/ioctl.h>

//#include <netdb.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <net/if.h>
#include <arpa/inet.h>

//取得本地ip
void GetLocalIp(unsigned long   *pOutIpAddress)
{
 int   fd; 
 struct   ifreq   ifr;  
 struct   sockaddr_in*   sin;   
 fd   =   socket(PF_INET,   SOCK_DGRAM,   0);  
 memset(&ifr,   0x00,   sizeof(ifr));  
 strcpy(ifr.ifr_name,   "eth0");  
 ioctl(fd,   SIOCGIFADDR,   &ifr);  
 close(fd);  
 sin   =   (struct   sockaddr_in*   )&ifr.ifr_addr; 
 *pOutIpAddress = sin->sin_addr.s_addr;
}

unsigned long GetNetworkMask()
{
 int   fd; 
 struct   ifreq   ifr;  
 struct   sockaddr_in*   sin;   
 fd   =   socket(PF_INET,   SOCK_DGRAM,   0);  
 memset(&ifr,   0x00,   sizeof(ifr));  
 strcpy(ifr.ifr_name,   "eth0");  
 ioctl(fd,   SIOCGIFNETMASK,   &ifr);  
 close(fd);  
 sin   =   (struct   sockaddr_in*   )&ifr.ifr_netmask; 
 return  sin->sin_addr.s_addr;
}

unsigned long GetNetworkGateway()
{
    FILE   *stream;
   char   buf[100];
    
   memset( buf, '/0', sizeof(buf) );
   stream = popen( "cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep GATEWAY", "r" );

   fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中
   memmove(buf, buf + 8, 92);
   pclose( stream ); 

   return inet_addr(buf);
}


int GetDHCPType()
{
    FILE   *stream;
   char   buf[20];
    
   memset( buf, '/0', sizeof(buf) );
   stream = popen( "cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep BOOTPROTO", "r" );

   fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中
   pclose( stream ); 
   memmove(buf, buf + 10, 10);
  
   return  strncasecmp(buf, "dhcp", 4);
}

/********************************************************************************/
/**                           获取本机MAC地址和IP                                    */
/********************************************************************************/
void Getipaddr(struct  in_addr *hostIP,unsigned char * HostMac)
{   
    int                sockfd, size = 1;
    struct ifreq *     ifr;
    struct ifconf      ifc;
    struct sockaddr_in sa;
    unsigned char    *u;
    #define IFRSIZE      ( ( int )( size * sizeof( struct ifreq ) ) )
 
 
    if ( 0 > ( sockfd = socket( AF_INET, SOCK_DGRAM, IPPROTO_IP ) ) )
    {
        fprintf( stderr, "Cannot open socket./n" );
        return;
    }
    ifc.ifc_req = NULL;
    do
    {
        ++size;
        /* realloc buffer size until no overflow occurs  */
        if ( NULL == ( ifc.ifc_req = (ifreq *)realloc(ifc.ifc_req, IFRSIZE )))
        {
            fprintf( stderr, "Out of memory./n" );
            return;
        }
        ifc.ifc_len = IFRSIZE;
        if ( ioctl( sockfd, SIOCGIFCONF, &ifc ) )
        {
            perror( "ioctl SIOCFIFCONF" );
            return ;
        }
    } while ( IFRSIZE <= ifc.ifc_len );
    ifr = ifc.ifc_req;
    for ( ; ( char * )ifr < ( char * )ifc.ifc_req + ifc.ifc_len; ++ifr )
    {
        if ( ifr->ifr_addr.sa_data == ( ifr + 1 )->ifr_addr.sa_data )
        {
            continue;  /* duplicate, skip it */
        }
        if ( ioctl( sockfd, SIOCGIFFLAGS, ifr ) )
        {
            continue;  /* failed to get flags, skip it */
        }
  
  if(strcmp(ifr->ifr_name,"eth0") == 0)//,sizeof(ifr.ifr_name)-1打印eth0的IP
  {
            *hostIP = ((struct sockaddr_in*)(&(ifr->ifr_addr)))->sin_addr;
     
  }
  
        if ( 0 == ioctl( sockfd, SIOCGIFHWADDR, ifr ) )
        {
            switch ( ifr->ifr_hwaddr.sa_family )
            {
            case ARPHRD_NETROM:
            case ARPHRD_ETHER:
            case ARPHRD_PPP:
            case ARPHRD_EETHER:
            case ARPHRD_IEEE802:
                break;
            default:
                printf( "/n" );
                continue;
            }
            u = ( unsigned char * )&ifr->ifr_addr.sa_data;       //取得MAC地址
            if ( u[0] + u[1] + u[2] + u[3] + u[4] + u[5] )
            {
                printf( "HW Address: %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x/n",
     u[0], u[1], u[2], u[3], u[4], u[5] );
            }
     if(strcmp(ifr->ifr_name, "eth0") == 0)//,sizeof(ifr.ifr_name)-1打印eth0的IP
  memcpy(HostMac,u,6);
  
        }
    }  /* end of for */
    close( sockfd );
 
 return;
}

int Openintf(char *nt_card)
{
 int fd;
 struct ifreq ifr; /*接口请求结构,用来调用在I/O输入输出时使用*/
 int sign;
 
 fd = socket(AF_INET, SOCK_PACKET, htons(/*0x0806*/0x0003));    //wrtie也出错
 /*0x0800是IP,0x0806是ARP,0x8035是RARP,0x0003是任何都可以*/ 
 if(fd < 0)
 {
  perror("cant get SOCK_PACKET socket");
  return(-1);
 }
 
 strcpy(ifr.ifr_name, nt_card);
 sign = ioctl(fd, SIOCGIFFLAGS, &ifr); /*读取标志位*/
 if(sign < 0)
 {
  close(fd);
  perror("cant get flags");
  return(-1);
 }
 
 ifr.ifr_flags |= IFF_PROMISC;  /*设置标志位为混杂模式,sniffer是在这种模式下工作的*/
 ifr.ifr_flags |= ~IFF_NOARP;            //
 
 sign = ioctl(fd, SIOCSIFFLAGS, &ifr); /*写入标志位*/
 if(sign < 0)
 {
  perror("cant set promiscuous mode");
 }
 
 return fd;
}

int main(int argc, char *argv[])
{
 //get ipaddr
 unsigned long ipAddr;
 GetLocalIp(&ipAddr);
 char strip[5];
 memset(strip,0x0,5);
 memcpy(strip,(void*)&ipAddr,4);
 for(int i = 0 ; i < 4 ; i++ )
 {
  printf(" %3d ",0xff&strip[i]);
 }
 printf("/r/n");
 
 //get networkmask
 ipAddr = GetNetworkMask();
 memset(strip,0x0,5);
 memcpy(strip,(void*)&ipAddr,4);
 for(int i = 0 ; i < 4 ; i++ )
 {
  printf(" %3d ",0xff&strip[i]);
 }
 printf("/r/n");
 
 //get network Gateway
 ipAddr = GetNetworkGateway();
 memset(strip,0x0,5);
 memcpy(strip,(void*)&ipAddr,4);
 for(int i = 0 ; i < 4 ; i++ )
 {
  printf(" %3d ",0xff&strip[i]);
 }
 printf("/r/n");
 
 //get dhcp
 int idhcp = GetDHCPType();
 printf("dhcp = %d /r/n",idhcp);
 
 //get ip and mac
 struct  in_addr hostIP;
 unsigned char HostMac[8];
 Getipaddr(&hostIP,HostMac);
 
 for(int i = 0 ; i < 6 ; i++)
 {
  printf(" %2x ",0xff&HostMac[i]);
 }
 printf("/r/n");
 
 ipAddr = hostIP.s_addr;
 memset(strip,0x0,5);
 memcpy(strip,(void*)&ipAddr,4);
 for(int i = 0 ; i < 4 ; i++ )
 {
  printf(" %3d ",0xff&strip[i]);
 }
 printf("/r/n");
 
 //ctrl eth0
 Openintf("eth0");
 
 return 1;
}

你可能感兴趣的:(linux,struct,socket,Stream,File,include)