Linux system obtains network IP addresses, mask, gateway, and DNS information applets

Source: Internet
Author: User
Tags dns2 get ip nameserver

Net_util.c

# Define wired_dev "eth0"
# Define wireless_dev "ra0"
# Define pppoe_dev "ppp0"

# Define debug_prt (FMT, Arg...) printf (FMT, # Arg)

/**
* Get IP address.
* @ Param net_dev net device.
* @ Param ipaddr a pointer to save IP address.
* @ Return 0 success, or fail.
*/
Int get_ipaddr (const char * net_dev, char * ipaddr)
{
Struct ifreq IFR;
Int FD = 0;
Struct sockaddr_in * paddr;

If (null = net_dev) | (null = ipaddr ))
{
Debug_prt ("illegal call function setgeneralip! \ N ");
Return-1;
}

If (FD = socket (af_inet, sock_dgram, 0) <0)
{
Debug_prt ("Open socket failed \ n ");
Return-1;
}

Memset (& IFR, 0, sizeof (IFR ));
Strcpy (IFR. ifr_name, net_dev );

If (IOCTL (FD, siocgifaddr, & IFR) <0)
{
Debug_prt ("siocgifaddr socket failed \ n ");
Close (FD );
Return-1;
}

Paddr = (struct sockaddr_in *) & (IFR. ifr_addr );

Strcpy (ipaddr, inet_ntoa (paddr-> sin_addr ));

Close (FD );

Return 0;
}

/**
* Get gateway.
* @ Param gateway a pointer to save geteway.
* @ Return none.
*/
Void get_gateway (iti_char * gateway)
{
Char Buf [1024] = {0 };
Char * P = NULL;
Char * q = NULL;
Int COUNT = 0;
If (null = gateway)
{
Debug_prt ("Gateway is null \ n ");
Return;
}
Performance_run ("route | grep default", Buf, 1024 );
If (0 = strlen (BUF ))
{
Debug_prt ("Get Gateway error \ n ");
Return;
}

P = strstr2 (BUF, "default ");
Q = P;
While (* q = '')
{
Q ++;
}

P = Q;
While (* P! = '')
{
P ++;
Count ++;
}
If (null! = Q)
{
Memcpy (gateway, Q, count );
}
Gateway [count] = '\ 0 ';
}

/**
* Get mask.
* @ Param net_dev net device.
* @ Param mask a pointer to save mask.
* @ Return none.
*/
Void get_mask (const char * net_dev, iti_char * mask)
{
Char Buf [1024] = {0 };
Char * P = NULL;
If (null = mask)
{
Debug_prt ("Mask is null \ n ");
Return;
}
If (0 = (memcmp (wired_dev, net_dev, sizeof (wired_dev ))))
{
Performance_run ("ifconfig eth0 | grep mask", Buf, 1024 );
}
Else if (0 = (memcmp (wireless_dev, net_dev, sizeof (wireless_dev ))))
{
Performance_run ("ifconfig ra0 | grep mask", Buf, 1024 );
}
Else if (0 = (memcmp (pppoe_dev, net_dev, sizeof (pppoe_dev ))))
{
Performance_run ("ifconfig ppp0 | grep mask", Buf, 1024 );
}
Else
{
Debug_prt ("net device not support \ n ");
Return;
}

If (0 = strlen (BUF ))
{
Debug_prt ("Get mask error \ n ");
Return;
}
P = strstr2 (BUF, "Mask :");
If (null = P)
{
Debug_prt ("Get mask error \ n ");
Return;
}
Strcpy (mask, P );
}

/**
* Get DNS.
* @ Param dns1 a pointer to save first DNS.
* @ Param dns2 a pointer to save second DNS.
* @ Return 0 success, or fail.
*/
Int get_dns (char * dns1, char * dns2)
{
Int FD =-1;
Int size = 0;
Char strbuf [100];
Char tmpbuf [100];
Int buf_size = sizeof (strbuf );
Char * P = NULL;
Char * q = NULL;
Int I = 0;
Int J = 0;
Int COUNT = 0;

FD = open ("/etc/resolv. conf", o_rdonly );
If (-1 = FD)
{
Debug_prt ("% s open error \ n", _ FUNC __);
Return-1;
}
Size = read (FD, strbuf, buf_size );
If (size <0)
{
Debug_prt ("% s Read File Len error \ n", _ FUNC __);
Close (FD );
Return-1;
}
Strbuf [buf_size] = '\ 0 ';
Close (FD );

While (I <buf_size)
{
If (P = strstr2 (& strbuf [I], "nameserver "))! = NULL)
{
J ++;
P + = 1;
Count = 0;

Memset (tmpbuf, 0xff, 100 );
Memcpy (tmpbuf, P, 100 );
Tmpbuf [sizeof (tmpbuf)-1] = '\ 0 ';

Q = P;
While (* Q! = '\ N ')
{
Q ++;
Count ++;
}
I + = (sizeof ("nameserver") + count );

If (1 = J)
{
Memcpy (dns1, P, count );
Dns1 [count] = '\ 0 ';
}
Else if (2 = J)
{
Memcpy (dns2, P, count );
Dns2 [count] = '\ 0 ';
}
}
Else
{
I ++;
}
}

Return 0;
}

Main. c

Void main (void)

{

# Define wired_dev "eth0"
# Define wireless_dev "ra0"
# Define pppoe_dev "ppp0"

Char Buf [1024] = {'\ 0 '};

Extern int get_ipaddr (const char * net_dev, char * ipaddr );
Get_ipaddr (wired_dev, Buf );
Printf ("get_ipaddr: % s \ n", Buf );
Memset (BUF, '\ 0', sizeof (BUF ));

Extern void get_mask (const char * net_dev, iti_char * mask );
Get_mask (wired_dev, Buf );
Printf ("get_mask: % s \ n", Buf );
Memset (BUF, '\ 0', sizeof (BUF ));

Extern void get_gateway (iti_char * gateway );
Get_gateway (BUF );
Printf ("get_gateway: % s \ n", Buf );
Memset (BUF, '\ 0', sizeof (BUF ));

Extern int get_dns (char * dns1, char * dns2 );
Get_dns (BUF, & Buf [1, 100]);
Printf ("get_dns: % S % s \ n", Buf, & Buf [100]);
Memset (BUF, '\ 0', sizeof (BUF ));

}

Makefile:

GCC main. c net_util.c-O get_net_info

./Get_net_info

Running result:

Get_ipaddr: 192.168.9.142
Get_mask: 255.255.255.0
Get_gateway: 192.168.9.254
Get_dns: 192.168.9.11 192.168.9.10

To improve execution efficiency, update the program for obtaining the gateway and subnet mask:

Static void get_gateway (const char * net_dev, char * gateway)
{
File * FP;
Char Buf [1024];
Char iface [16];
Unsigned char TMP [100] = {'\ 0 '};
Unsigned int dest_addr = 0, gate_addr = 0;
If (null = gateway)
{
Debug_prt ("Gateway is null \ n ");
Return;
}
Fp = fopen ("/proc/NET/route", "R ");
If (FP = NULL ){
Debug_prt ("fopen error \ n ");
Return;
}

Fgets (BUF, sizeof (BUF), FP );
While (fgets (BUF, sizeof (BUF), FP ))
{
If (sscanf (BUF, "% s \ t % x", iface, & dest_addr, & gate_addr) = 3)
& (Memcmp (net_dev, iface, strlen (net_dev) = 0)
& Gate_addr! = 0)
{
Memcpy (TMP, (unsigned char *) & gate_addr, 4 );
Sprintf (gateway, "% d. % d. % d. % d ", (unsigned char) * TMP, (unsigned char) * (TMP + 1), (unsigned char) * (TMP + 2), (unsigned char) * (TMP + 3 ));
Break;
}
}

Fclose (FP );
}

Static void get_mask (const char * net_dev, iti_char * mask)
{
Struct sockaddr_in * paddr;
Struct ifreq IFR;
Int sockfd;

Sockfd = socket (af_inet, sock_dgram, 0 );
Memset (& IFR, 0, sizeof (IFR ));
Strcpy (IFR. ifr_name, net_dev );

If (IOCTL (sockfd, siocgifnetmask, & IFR) <0 ){
Debug_prt ("siocgifaddr socket failed \ n ");
Close (sockfd );
Return;
}

Paddr = (struct sockaddr_in *) & (IFR. ifr_addr );
Strcpy (mask, (char *) (inet_ntoa (paddr-> sin_addr )));

Close (sockfd );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.