新聞中心
科技發(fā)展如此之快,我們身邊已經(jīng)有了許多高科技應(yīng)用了。一直以來,穿越外網(wǎng)IP的迷霧一直是網(wǎng)絡(luò)安全研究的一塊重要課題,基于linux系統(tǒng)的C程序可以幫助你看穿迷霧,獲取到真實IP地址。

針對外網(wǎng)IP獲取,C程序員有幾種不同的方法和方案:
第一種方法是使用Epoll函數(shù)庫,Epoll函數(shù)庫是Linux系統(tǒng)提供的一種高效的事件處理機制,可以判斷系統(tǒng)提交的新數(shù)據(jù),并處理之。在查詢外網(wǎng)IP時,可以使用Epoll函數(shù)庫,使用fd_set函數(shù)集查找Socket,然后使用gethostbyaddr函數(shù)獲取到對應(yīng)的地址信息:
#include
#include
#include
#include
#include
int main()
{
char * host=”xxx.xxx.xxx.xxx”;
int s;
int epfd;
struct hostent *hp;
struct epoll_event ev;
//Obtain a socket for web
int s = socket(AF_INET,SOCK_STREAM,0);
if(s == -1)
{
printf(“Create socket failed!”);
}
//configuring nameservers
struct hostent * hp = gethostbyaddr(host, strlen(host), AF_INET);
//Create epoll instance
epfd = epoll_create1(0);
if(epfd== -1)
{
perror(“epoll_create1() error”);
exit(1);
}
//Add the socket to epoll instance
ev.data.fd = s;
err=epoll_ctl(epfd, EPOLL_CTL_ADD,s, &ev);
if (err ==-1)
{
perror(“epoll_ctl() error”);
exit(1);
}
printf(“Getting External IP: %d.%d.%d.%d\n”, hptr->h_name, ptr->h_addrtype, ptr->h_length);
//Close socket
close(s);
return 0;
}
其次,C程序員也可以使用API或者Socket網(wǎng)絡(luò)編程技術(shù)來實現(xiàn)IP地址查找:在學(xué)習(xí)LinuxSocket編程時,主要通過調(diào)用gethostbyname函數(shù)來查找地址對應(yīng)的IP地址:
#include
#include
#include
#include
int main()
{
char * hostname = “xxx.xxx.xxx.xxx”;
struct hostent*host_entry;
struct in_addr ipaddr;
int ret;
host_entry = gethostbyname(hostname);
ipaddr = *(struct in_addr *)(host_entry->h_addr);
printf(“%s: IP地址: %s\n”, hostname, inet_ntoa(ipaddr));
return 0;
}
同樣,C程序員還可以調(diào)用getaddrinfo函數(shù)來獲取對應(yīng)地址的IP地址,這個函數(shù)比gethostbyname更加靈活,可以在IPV4和IPV6之間設(shè)置類型,使用方式如下:
#include
#include
#include
#include
#include
int main(){
struct addrinfo hints, *res;
struct in_addr addr;
int errcode;
char addrstr[100];
void *ptr;
//Getting IP address from hostname
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
errcode = getaddrinfo(“www.example.com”, NULL, &hints, &res);
if (errcode != 0) {
printf(“getaddrinfo() failed: %s\n”, gai_strerror(errcode));
exit(1);
}
printf(“Host: %s\n”, res->ai_canonname);
ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
inet_ntop(res->ai_family, ptr, addrstr, 100);
printf(“IPv4 address: %s\n”, addrstr);
freeaddrinfo(res);
}
通過以上三種方式,Linux C程序完全可以穿透外網(wǎng)IP的迷霧,查詢真實IP地址。然而,并不是所有的IP地址都可以直接被查詢到,有時候需要經(jīng)過幾次跳轉(zhuǎn)才能查詢到真實的IP地址,這也是想要深入了解IP地址信息時,Linux C程序員必須要加強學(xué)習(xí)網(wǎng)絡(luò)安全技術(shù)的原因吧。
創(chuàng)新互聯(lián)-老牌IDC、云計算及IT信息化服務(wù)領(lǐng)域的服務(wù)供應(yīng)商,業(yè)務(wù)涵蓋IDC(互聯(lián)網(wǎng)數(shù)據(jù)中心)服務(wù)、云計算服務(wù)、IT信息化、AI算力租賃平臺(智算云),軟件開發(fā),網(wǎng)站建設(shè),咨詢熱線:028-86922220
當(dāng)前文章:LinuxC程序員看穿外網(wǎng)IP的迷霧(linuxc外網(wǎng)ip)
URL標(biāo)題:http://www.dlmjj.cn/article/cccdsig.html


咨詢
建站咨詢
