IP地址分为IPv4 和IPv6,本文只验证了IPv4的获取,至于IPv6还没有验证。
物候谚语
说明:
family = AT_INET :IPv4
family = AT_INET6:IPv6
address: 提取得到的IP地址所要保存的地址
size =strlen(sampleIP) 192.168.123.128
MAX_IPv4 =15
family = AT_INET :IPv4
family = AT_INET6:IPv6
address: 提取得到的IP地址所要保存的地址
size =strlen(sampleIP) 192.168.123.128
MAX_IPv4 =15
某些头文件应在其他方面,在本程序中没用得上。不过不影响程序的编译。
program list:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
int GetIP_v4_and_v6_linux(int family, char *address, int size)
{
struct ifaddrs *ifap0, *ifap;
char buf[NI_MAXHOST];
char *interface = "eth0";
struct sockaddr_in *addr4;
struct sockaddr_in6 *addr6;
int ret;
if( NULL == address )
{
return -1;
}
if(getifaddrs(&ifap0))
{
{
struct ifaddrs *ifap0, *ifap;
char buf[NI_MAXHOST];
char *interface = "eth0";
struct sockaddr_in *addr4;
struct sockaddr_in6 *addr6;
int ret;
if( NULL == address )
{
return -1;
}
if(getifaddrs(&ifap0))
{
return -1;
}
for( ifap = ifap0; ifap != NULL; ifap=ifap->ifa_next)
{
if(strcmp(interface, ifap->ifa_name) != 0) continue;
if(ifap->ifa_addr==NULL) continue;
if ((ifap->ifa_flags & IFF_UP) == 0) continue;
if(family != ifap->ifa_addr->sa_family) continue;
if(AF_INET == ifap->ifa_addr->sa_family)
{
addr4 = (struct sockaddr_in *)ifap->ifa_addr;
if ( NULL != inet_ntop(ifap->ifa_addr->sa_family,
(void *)&(addr4->sin_addr), buf, NI_MAXHOST) )
{
}
for( ifap = ifap0; ifap != NULL; ifap=ifap->ifa_next)
{
if(strcmp(interface, ifap->ifa_name) != 0) continue;
if(ifap->ifa_addr==NULL) continue;
if ((ifap->ifa_flags & IFF_UP) == 0) continue;
if(family != ifap->ifa_addr->sa_family) continue;
if(AF_INET == ifap->ifa_addr->sa_family)
{
addr4 = (struct sockaddr_in *)ifap->ifa_addr;
if ( NULL != inet_ntop(ifap->ifa_addr->sa_family,
(void *)&(addr4->sin_addr), buf, NI_MAXHOST) )
{
岳阳中考成绩查询 if(size < strlen(buf) ) break;
strcpy(address, buf);
printf("IPv4=%s\n",address);//testing
freeifaddrs(ifap0);
return 0;
}
else break;
}
else if(AF_INET6 == ifap->ifa_addr->sa_family)
{
addr6 = (struct sockaddr_in6 *)ifap->ifa_addr;
if(IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr))
{ continue;
}
strcpy(address, buf);
printf("IPv4=%s\n",address);//testing
freeifaddrs(ifap0);
return 0;
}
else break;
}
else if(AF_INET6 == ifap->ifa_addr->sa_family)
{
addr6 = (struct sockaddr_in6 *)ifap->ifa_addr;
if(IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr))
{ continue;
}
if(IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr))
无法获取ip地址 {continue;
}
关于新型冠状病毒的作文 if(IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr))
{ continue;
}
if(IN6_IS_ADDR_UNSPECIFIED(&addr6->sin6_addr))
{ continue;
移动硬盘分区 }
if(IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr))
{continue;
}
if ( NULL != inet_ntop(ifap->ifa_addr->sa_family,
(void *)&(addr6->sin6_addr), buf, NI_MAXHOST) )
{ if(size < strlen(buf) ) break;
无法获取ip地址 {continue;
}
关于新型冠状病毒的作文 if(IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr))
{ continue;
}
if(IN6_IS_ADDR_UNSPECIFIED(&addr6->sin6_addr))
{ continue;
移动硬盘分区 }
if(IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr))
{continue;
}
if ( NULL != inet_ntop(ifap->ifa_addr->sa_family,
(void *)&(addr6->sin6_addr), buf, NI_MAXHOST) )
{ if(size < strlen(buf) ) break;
strcpy(address, buf);
printf("IPv6=%s\n",address);//testing
freeifaddrs(ifap0);
return 0;
}
else break;
}
}
}
printf("IPv6=%s\n",address);//testing
freeifaddrs(ifap0);
return 0;
}
else break;
}
}
}
void * getIPv4()
{
static int SIZE ;
static char IP[64];
char *sampleIPv4="192.168.123.128";
{
static int SIZE ;
static char IP[64];
char *sampleIPv4="192.168.123.128";
SIZE = strlen(sampleIPv4);
GetIP_v4_and_v6_linux(AF_INET ,IP ,SIZE);
jolin in the house
printf("IP=%s\n",IP);
return IP;
}
GetIP_v4_and_v6_linux(AF_INET ,IP ,SIZE);
jolin in the house
printf("IP=%s\n",IP);
return IP;
}
本程序非原创,也不清楚原创作者是何人。本人只是拿来修改,并应用在实际的工程项目中去。感谢原创作者和网络上的无私奉献的 Linux工作者!让我们共同发扬Linux的精神!
Enjoy it!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论