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
}
Cmd_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] = ' + ';
}
/**
* 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))))
{
Cmd_run ("Ifconfig eth0 | grep Mask ", buf, 1024);
}
else if (0 = = (memcmp (Wireless_dev, Net_dev, sizeof (Wireless_dev))))
{
Cmd_run ("Ifconfig ra0 | grep Mask ", buf, 1024);
}
else if (0 = = (memcmp (Pppoe_dev, Net_dev, sizeof (Pppoe_dev))))
{
Cmd_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] = ' + ';
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] = ' + ';
Q = p;
while (*q! = ' \ n ')
{
q++;
count++;
}
i + = (sizeof ("nameserver") + count);
if (1 = = j)
{
memcpy (DNS1, p, count);
dns1[count]= ' + ';
}
else if (2 = = j)
{
memcpy (Dns2, p, count);
dns2[count]= ' + ';
}
}
Else
{
i++;
}
}
return 0;
}
Main.c
void Main (void)
{
#define WIRED_DEV "Eth0"
#define WIRELESS_DEV "RA0"
#define PPPOE_DEV "Ppp0"
Char buf[1024] = {'} '};
extern int get_ipaddr (const char *net_dev, char *ipaddr);
Get_ipaddr (Wired_dev, buf);
printf ("Get_ipaddr:%s \ n", buf);
memset (buf, ' n ', 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, ' n ', sizeof (BUF));
extern void Get_gateway (Iti_char *gateway);
Get_gateway (BUF);
printf ("Get_gateway:%s \ n", buf);
memset (buf, ' n ', sizeof (BUF));
extern int Get_dns (char *dns1, char *dns2);
Get_dns (BUF, &buf[100]);
printf ("Get_dns:%s%s\n", buf, &buf[100]);
memset (buf, ' n ', sizeof (BUF));
}
Makefile
GCC main.c net_util.c-o get_net_info
./get_net_info
Operation 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 Get gateway and subnet Mask program:
static void Get_gateway (const char *net_dev, char *gateway)
{
FILE *FP;
Char buf[1024];
Char iface[16];
unsigned char tmp[100]={' "};
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\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);
}
Linux system obtains network IP, mask, gateway, DNS information applet