]>
Commit | Line | Data |
---|---|---|
51b166d4 FM |
1 | /* |
2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net | |
110ce984 | 3 | * 1998, 2015 |
51b166d4 FM |
4 | * |
5 | * SARG donations: | |
6 | * please look at http://sarg.sourceforge.net/donations.php | |
7 | * Support: | |
8 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |
9 | * --------------------------------------------------------------------- | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
24 | * | |
25 | */ | |
26 | ||
27 | #include "include/conf.h" | |
28 | #include "include/defs.h" | |
29 | #include "include/ip2name.h" | |
30 | #ifdef HAVE_WS2TCPIP_H | |
31 | #include <ws2tcpip.h> //define getnameinfo on windows | |
32 | #endif | |
33 | ||
34 | static enum ip2name_retcode ip2name_dns(char *ip,int ip_len); | |
35 | ||
36 | //! The functions to resolve an IP address through tne dns. | |
37 | struct Ip2NameProcess Ip2NameDns= | |
38 | { | |
39 | "dns", | |
40 | NULL,//no next item yet | |
41 | NULL,//no configuration | |
42 | ip2name_dns | |
43 | }; | |
44 | ||
45 | /*! | |
46 | Resolve the IP address using a reverse DNS entry. | |
47 | ||
48 | \param ip The IP address. It is replaced by the corresponding name if one | |
49 | can be found. | |
50 | \param ip_len The length of the \c ip buffer. | |
51 | ||
52 | \return One of the ::ip2name_retcode value. | |
53 | */ | |
54 | static enum ip2name_retcode ip2name_dns(char *ip,int ip_len) | |
55 | { | |
56 | #ifdef HAVE_GETNAMEINFO | |
57 | struct sockaddr_storage sa; | |
58 | int sockaddr_size; | |
59 | char host[NI_MAXHOST]; | |
60 | int n1,n2,n3,n4,next=0; | |
61 | int error; | |
62 | ||
63 | memset(&sa,0,sizeof(sa)); | |
64 | if (sscanf(ip,"%d.%d.%d.%d%n",&n1,&n2,&n3,&n4,&next)==4 && ip[next]=='\0') { | |
65 | struct sockaddr_in *s4=(struct sockaddr_in *)&sa; | |
66 | if (inet_pton(AF_INET,ip,&s4->sin_addr)!=1) return(INRC_Error); | |
67 | sa.ss_family=AF_INET; | |
68 | sockaddr_size=sizeof(*s4); | |
69 | } else { | |
70 | struct sockaddr_in6 *s6=(struct sockaddr_in6 *)&sa; | |
71 | if (inet_pton(AF_INET6,ip,&s6->sin6_addr)!=1) return(INRC_Error); | |
72 | sa.ss_family=AF_INET6; | |
73 | sockaddr_size=sizeof(*s6); | |
74 | } | |
75 | #ifdef HAVE_SOCKADDR_SA_LEN | |
76 | sa.ss_len=sockaddr_size; | |
77 | #endif | |
78 | error=getnameinfo((struct sockaddr *)&sa,sockaddr_size,host,sizeof(host),NULL,0,NI_NAMEREQD); | |
79 | if (error==EAI_AGAIN) { | |
80 | /* | |
81 | This is a temporary failure. According to the man page we should try again but | |
82 | it doesn't say if the program should wait before trying again nor how many attempts | |
83 | before it becomes a fatal error. I could find no clues on internet so I try once and | |
84 | leave it at that. Considering the number of IP addresses to resolve and the absence | |
85 | of serious consequences should some IP fail to be resolved properly, it is best | |
86 | not waste too much time on this. | |
87 | */ | |
88 | error=getnameinfo((struct sockaddr *)&sa,sizeof(sa),host,sizeof(host),NULL,0,NI_NAMEREQD); | |
89 | } | |
90 | if (error==EAI_NONAME) | |
91 | return(INRC_NotFound); | |
92 | if (error!=0) { | |
af961877 | 93 | debuga(__FILE__,__LINE__,_("IP to name resolution (getnameinfo) on IP address \"%s\" failed with error %d - %s\n"),ip,error,gai_strerror(error)); |
51b166d4 FM |
94 | return(INRC_Error); |
95 | } | |
96 | safe_strcpy(ip,host,ip_len); | |
97 | #else //HAVE_GETNAMEINFO | |
98 | struct in_addr addr; | |
99 | struct hostent *hp; | |
100 | char **p; | |
5f2ff7a7 | 101 | #ifdef __linux |
51b166d4 | 102 | extern int h_errno; |
5f2ff7a7 | 103 | #endif |
51b166d4 FM |
104 | |
105 | #ifdef HAVE_INET_ATON | |
106 | if (inet_aton(ip,&addr) == 0) | |
107 | return(INRC_Error); | |
108 | #else | |
109 | addr.s_addr=inet_addr(ip); | |
110 | if (addr.s_addr==-1) return(INRC_Error); | |
111 | #endif | |
112 | ||
113 | hp = gethostbyaddr((void *)&addr, sizeof (addr), AF_INET); | |
114 | if (hp == NULL) { | |
115 | if (h_errno==HOST_NOT_FOUND) | |
116 | return(INRC_NotFound); | |
117 | return(INRC_Error); | |
118 | } | |
119 | ||
120 | for (p = hp->h_addr_list; *p != 0; p++) { | |
121 | struct in_addr in; | |
122 | ||
123 | (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr)); | |
124 | safe_strcpy(ip,hp->h_name,ip_len); | |
125 | } | |
126 | #endif | |
127 | return(INRC_Found); | |
128 | } | |
129 |