思:获取本地主机外网IP的方法有很多,更简单的办法也很多,这里只是提供一个可行的办法,顺便结合raw socket + icmp报文做个练习。程序已经在centos6.3上测试编译运行通过!
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h> // inet_pton() and inet_ntop()
#include <netinet/in.h> // IPPROTO_RAW, IPPROTO_TCP, IPPROTO_ICMP, IPPROTO_UDP, INET_ADDRSTRLEN
#define __FAVOR_BSD // Use BSD format of TCP header and UDP header
#include <netinet/tcp.h> // struct tcphdr
#include <netinet/ip.h> // struct ip and IP_MAXPACKET (which is 65535)
#include <netinet/ip_icmp.h> // struct icmphdr and ICMP_ECHO
#include <netdb.h> // getaddrinfo
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <netdb.h> // struct addrinfo
#include <math.h> // sqrt
#define DEF_ICMP_LEN 1024
static uint16_t g_pid;
u_short in_cksum(const u_short *addr, register int32_t len, u_short csum)
{
register int32_t nleft = len;
const u_short *w = addr;
register u_short answer;
register int32_t sum = csum;
/*
* * Our algorithm is simple, using a 32 bit accumulator (sum),
* * we add sequential 16 bit words to it, and at the end, fold
* * back all the carry bits from the top 16 bits into the lower
* * 16 bits.
* */
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1)
sum += htons(*(u_char *)w << 8);
/*
* * add back carry outs from top 16 bits to low 16 bits
* */
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return (answer);
}
uint16_t checksum(uint16_t* buffer, int size)
{
unsigned long cksum = 0;
while(size>1)
{
cksum += *buffer++;
size -= sizeof(uint16_t);
}
if(size)
{
cksum += *(unsigned char*)buffer;
}
cksum = (cksum>>16) + (cksum&0xffff);
cksum += (cksum>>16);
return (uint16_t)(~cksum);
}
int32_t init_icmp_socket(void)
{
int32_t fd;
struct timeval tv;
fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(fd < 0){
perror("error on init icmp socket()");
exit(EXIT_FAILURE);
}else{
tv.tv_sec = 2;
tv.tv_usec = 0;
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(tv)); /*set send and receive time out*/
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv));
}
return fd;
}
void init_icmp_packet(unsigned char* pack, uint32_t len)
{
int32_t i;
struct icmphdr *icp = (struct icmphdr *)pack;
g_pid = htons(getpid() & 0xFFFF);
icp->type = ICMP_ECHO;
icp->code = 0;
icp->checksum = 0;
icp->un.echo.sequence = htons(1);
icp->un.echo.id = g_pid;
for(i = sizeof(*icp); i < len; i++){
pack[i] = 0x00;
}
icp->checksum = in_cksum((uint16_t *)pack, len, 0);
}
void get_source_addr(void)
{
int ret;
char* hostname = "www.baidu.com";
char dest_addr[INET_ADDRSTRLEN], source_addr[INET_ADDRSTRLEN];
struct hostent *hptr;
char *ptr, **pptr;
if((hptr=gethostbyname(hostname)) == NULL){
fprintf(stderr, "error gethotbyname()\n");
exit(EXIT_FAILURE);
}
if(hptr->h_addrtype == AF_INET){
pptr=hptr->h_addr_list;
for(;*pptr != NULL; pptr++){
inet_ntop(hptr->h_addrtype, *pptr, dest_addr, INET_ADDRSTRLEN);
}
}
int32_t icmp_socket;
char send_buf[DEF_ICMP_LEN], recv_buf[DEF_ICMP_LEN];
icmp_socket = init_icmp_socket();
init_icmp_packet(send_buf, DEF_ICMP_LEN);
struct sockaddr_in dest_sa;
memset(&dest_sa, 0, sizeof(struct sockaddr_in));
inet_pton(AF_INET, dest_addr, (void*)&dest_sa.sin_addr);
dest_sa.sin_family = AF_INET;
//dest_sa.sin_port = htons(80);
ret = sendto(icmp_socket, send_buf, DEF_ICMP_LEN, 0, (struct sockaddr *)&dest_sa, sizeof(struct sockaddr_in));
if(ret != DEF_ICMP_LEN){
fprintf(stderr, "error on icmp sendto()\n");
exit(EXIT_FAILURE);
}
int status, again_cnt = 0;
struct iphdr *ip;
int32_t ip_hlen;
size_t icmp_len;
struct icmphdr *icmp;
struct timeval tv,now;
gettimeofday(&tv, NULL);
for(;;){
gettimeofday(&now, NULL);
if(now.tv_sec - tv.tv_sec > 10){
fprintf(stderr, "error recv icmp response timeout\n");
exit(EXIT_FAILURE);
}
memset(recv_buf, 0, sizeof(recv_buf));
if((ret = recvfrom(icmp_socket, recv_buf, sizeof(recv_buf), 0, NULL, NULL)) < 0){
status = errno;
if(status == EAGAIN){
again_cnt++;
if(again_cnt == 5){
fprintf(stderr, "error recv icmp response timeout\n");
exit(EXIT_FAILURE);
}
continue;
}else if(status == EINTR){
continue;
}else{
perror("error on recvfrom()");
exit(EXIT_FAILURE);
}
}
again_cnt = 0;
ip = (struct iphdr *)recv_buf;
ip_hlen = ip->ihl << 2;
icmp_len = ret - ip_hlen;
if(icmp_len < sizeof(struct icmphdr)){
continue;
}
icmp = (struct icmphdr *)(recv_buf + ip_hlen);
if(g_pid != icmp->un.echo.id){
continue;
}
if(icmp->type != 0 || icmp->code != 0){
continue;
}
if(inet_ntop(AF_INET, &(ip->daddr), source_addr, INET_ADDRSTRLEN) == NULL){
perror("error on inet_ntop()");
exit(EXIT_FAILURE);
}else{
printf("local addr: %s\n", source_addr);
break;
}
}
close(icmp_socket);
}
int main(int argc ,char* argv[])
{
get_source_addr();
return 0;
}