sarg  2.4.0
About: SARG ia a Squid Analysis Report Generator.
  Fossies Dox: sarg-2.4.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

ip2name_dns.c
Go to the documentation of this file.
1 /*
2  * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3  * 1998, 2015
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 
38 {
39  "dns",
40  NULL,//no next item yet
41  NULL,//no configuration
43 };
44 
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) {
93  debuga(__FILE__,__LINE__,_("IP to name resolution (getnameinfo) on IP address \"%s\" failed with error %d - %s\n"),ip,error,gai_strerror(error));
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;
101 #ifdef __linux
102  extern int h_errno;
103 #endif
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 
INRC_Found
@ INRC_Found
A match was found.
Definition: ip2name.h:12
Ip2NameProcess
Entry points of the ip2name modules.
Definition: ip2name.h:17
debuga
void debuga(const char *File, int Line, const char *msg,...)
Definition: util.c:601
ip2name_dns
static enum ip2name_retcode ip2name_dns(char *ip, int ip_len)
Definition: ip2name_dns.c:54
addr
char addr[20000]
Definition: conf.h:439
_
#define _(String)
Definition: conf.h:155
ip2name.h
conf.h
Include headers and define global variables. */.
ip2name_retcode
ip2name_retcode
The possible return code of ip2name subfunctions.
Definition: ip2name.h:5
Ip2NameDns
struct Ip2NameProcess Ip2NameDns
The functions to resolve an IP address through tne dns.
Definition: ip2name_dns.c:37
INRC_Error
@ INRC_Error
Error encountered during the processing.
Definition: ip2name.h:8
safe_strcpy
void safe_strcpy(char *dest, const char *src, int length)
Definition: util.c:1550
defs.h
Declaration of the structures and functions.
INRC_NotFound
@ INRC_NotFound
No match found.
Definition: ip2name.h:10