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.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 #include "include/dichotomic.h"
31 
34 {
36  const char *Name;
39 };
40 
41 extern struct Ip2NameProcess Ip2NameDns;
42 extern struct Ip2NameProcess Ip2NameExec;
43 
45 static const struct Ip2NameModules ModulesList[]=
46 {
47  {"dns",&Ip2NameDns},
48  {"exec",&Ip2NameExec},
49  {"yes",&Ip2NameDns},//for historical compatibility
50  {"no",NULL},//does nothing for compatibility with previous versions
51 };
52 
54 static struct Ip2NameProcess *FirstModule=NULL;
57 
61 static void ip2name_chainmodule(struct Ip2NameProcess *Module)
62 {
63  struct Ip2NameProcess *Chain;
64  struct Ip2NameProcess *Last;
65 
66  if (debug) debuga(__FILE__,__LINE__,_("Chaining IP resolving module \"%s\"\n"),Module->Name);
67 
68  Last=NULL;
69  for (Chain=FirstModule ; Chain ; Chain=Chain->Next) {
70  if (Chain==Module) {
71  debuga(__FILE__,__LINE__,_("Ignoring duplicate module \"%s\" to resolve an IP address\n"),Module->Name);
72  return;
73  }
74  Last=Chain;
75  }
76 
77  if (Last)
78  Last->Next=Module;
79  else {
80  FirstModule=Module;
81  Ip2Name=true;
82  }
83 }
84 
90 static void ip2name_buildmoduleslist(const char *list)
91 {
92  const char *candidate;
93  int length;
94  int ModuleIdx;
95 
96  while (*list) {
97  candidate=list;
98  while (*candidate && (unsigned char)*candidate<=' ') candidate++;
99  for (length=0 ; (unsigned char)candidate[length]>' ' ; length++);
100  for (ModuleIdx=0 ; ModuleIdx<sizeof(ModulesList)/sizeof(*ModulesList) ; ModuleIdx++) {
101  if (strncasecmp(candidate,ModulesList[ModuleIdx].Name,length)==0 && ModulesList[ModuleIdx].Name[length]=='\0') {
102  // module found
103  if (ModulesList[ModuleIdx].Process)
104  ip2name_chainmodule(ModulesList[ModuleIdx].Process);
105  break;
106  }
107  }
108  if (ModuleIdx>=sizeof(ModulesList)/sizeof(*ModulesList)) {
109  debuga(__FILE__,__LINE__,_("Unknown module \"%.*s\" to resolve the IP addresses\n"),length,candidate);
110  exit(EXIT_FAILURE);
111  }
112  list=candidate+length;
113  }
114 }
115 
122 static void ip2name_configmodule(const char *module)
123 {
124  int length;
125  unsigned int ModuleIdx;
126 
127  for (length=0 ; module[length] && (unsigned char)module[length]>' ' ; length++);
128  for (ModuleIdx=0 ; ModuleIdx<sizeof(ModulesList)/sizeof(*ModulesList) ; ModuleIdx++) {
129  if (strncasecmp(module,ModulesList[ModuleIdx].Name,length)==0 && ModulesList[ModuleIdx].Name[length]=='\0') {
130  // module found
131  if (ModulesList[ModuleIdx].Process) {
132  if (!ModulesList[ModuleIdx].Process->Configure) {
133  debuga(__FILE__,__LINE__,_("No option to configure for module %s\n"),ModulesList[ModuleIdx].Name);
134  exit(EXIT_FAILURE);
135  }
136  while (module[length] && (unsigned char)module[length]<=' ') length++;
137  ModulesList[ModuleIdx].Process->Configure(ModulesList[ModuleIdx].Name,module+length);
138  }
139  break;
140  }
141  }
142 }
143 
153 int ip2name_config(const char *param)
154 {
155  // module to add to the list
156  if (*param==' ') {
158  return(1);
159  }
160 
161  // parameter for a module?
162  if (*param=='_') {
163  ip2name_configmodule(param+1);
164  return(1);
165  }
166 
167  return(0);
168 }
169 
174 {
175  struct Ip2NameProcess *DnsModule=NULL;
176  int i;
177  struct Ip2NameProcess *Chain;
178  struct Ip2NameProcess *Last;
179 
180  // find the dns module
181  for (i=0 ; i<sizeof(ModulesList)/sizeof(*ModulesList) ; i++) {
182  if (strcmp("dns",ModulesList[i].Name)==0) {
183  // module found
184  DnsModule=ModulesList[i].Process;
185  break;
186  }
187  }
188  if (!DnsModule) {
189  debuga(__FILE__,__LINE__,_("No known module to resolve an IP address using the DNS\n"));
190  exit(EXIT_FAILURE);
191  }
192 
193  // add the module to the list if it isn't there yet
194  Last=NULL;
195  for (Chain=FirstModule ; Chain && Chain!=DnsModule ; Chain=Chain->Next) {
196  Last=Chain;
197  }
198  if (debug) debuga(__FILE__,__LINE__,_("Chaining IP resolving module \"%s\"\n"),DnsModule->Name);
199  if (Last)
200  Last->Next=DnsModule;
201  else {
202  FirstModule=DnsModule;
203  Ip2Name=true;
204  }
205 }
206 
216 void ip2name(char *ip,int ip_len)
217 {
218  struct Ip2NameProcess *Module;
219  enum ip2name_retcode Status;
220  const char *Name;
221  char OrigIp[80];
222 
223  if (!KnownIp) {
225  if (!KnownIp) {
226  debuga(__FILE__,__LINE__,_("Not enough memory to store the names corresponding to the IP address\n"));
227  exit(EXIT_FAILURE);
228  }
229  }
230 
232  if (Name) {
233  safe_strcpy(ip,Name,ip_len);
234  return;
235  }
236 
237  safe_strcpy(OrigIp,ip,sizeof(OrigIp));
238  for (Module=FirstModule ; Module ; Module=Module->Next) {
239  if (Module->Resolve) {
240  Status=Module->Resolve(ip,ip_len);
241  if (Status==INRC_Found) break;
242  }
243  }
244  Dichotomic_Insert(KnownIp,OrigIp,ip);
245 }
246 
251 void ip2name_cleanup(void)
252 {
254 }
255 
256 void name2ip(char *name,int name_size)
257 {
258 #ifdef HAVE_GETADDRINFO
259  int error;
260  char *port;
261  struct addrinfo *res=NULL;
262  char *addr;
263 
264  addr=name;
265  if (name[0]=='[') { //IPv6 address
266  port=strchr(name,']');
267  if (port) { //confirmed IPv6 address
268  *port='\0';
269  addr++;
270  }
271  } else { //IPv4 address
272  port=strchr(name,':');
273  if (port) *port='\0';
274  }
275 
276  error=getaddrinfo(addr,NULL,NULL,&res);
277  if (error) {
278  if (res) freeaddrinfo(res);
279  debuga(__FILE__,__LINE__,_("Cannot resolve host name \"%s\": %s\n"),name,gai_strerror(error));
280  } else {
281  if (res->ai_family==AF_INET) {
282  struct sockaddr_in *s4=(struct sockaddr_in *)res->ai_addr;
283  struct in_addr *sa=&s4->sin_addr;
284  if (res->ai_addrlen<sizeof(*s4)) {
285  debuga(__FILE__,__LINE__,_("Short structure returned by getaddrinfo for an IPv4 address: %d bytes instead of %d\n"),res->ai_addrlen,(int)sizeof(*s4));
286  exit(EXIT_FAILURE);
287  }
288  inet_ntop(res->ai_family,sa,name,name_size);
289  } else if (res->ai_family==AF_INET6) {
290  struct sockaddr_in6 *s6=(struct sockaddr_in6 *)res->ai_addr;
291  struct in6_addr *sa6=&s6->sin6_addr;
292  if (res->ai_addrlen<sizeof(*s6)) {
293  debuga(__FILE__,__LINE__,_("Short structure returned by getaddrinfo for an IPv6 address: %d bytes instead of %d\n"),res->ai_addrlen,(int)sizeof(*s6));
294  exit(EXIT_FAILURE);
295  }
296  inet_ntop(res->ai_family,sa6,name,name_size);
297  } else {
298  debuga(__FILE__,__LINE__,_("Invalid address type %d returned when resolving host name \"%s\"\n"),res->ai_family,name);
299  }
300  freeaddrinfo(res);
301  }
302 #else
303  struct in_addr ia;
304  struct hostent *hp;
305  char *port;
306  char n1[4];
307  char n2[4];
308  char n3[4];
309  char n4[4];
310  struct getwordstruct gwarea;
311 
312  port=strchr(name,':');
313  if (port) *port='\0';
314 
315  if ((hp=gethostbyname(name))==NULL)
316  return;
317 
318  memcpy(&ia.s_addr,hp->h_addr_list[0],sizeof(ia.s_addr));
319  ia.s_addr=ntohl(ia.s_addr);
320  getword_start(&gwarea,inet_ntoa(ia));
321  if (getword(n4,sizeof(n4),&gwarea,'.')<0 || getword(n3,sizeof(n3),&gwarea,'.')<0 ||
322  getword(n2,sizeof(n2),&gwarea,'.')<0 || getword(n1,sizeof(n1),&gwarea,0)<0) {
323  debuga(__FILE__,__LINE__,_("Invalid IPv4 address \"%s\"\n"),gwarea.beginning);
324  exit(EXIT_FAILURE);
325  }
326  snprintf(name,name_size,"%s.%s.%s.%s",n1,n2,n3,n4);
327 #endif
328 
329  return;
330 }
name2ip
void name2ip(char *name, int name_size)
Definition: ip2name.c:256
if
if( $written !=strlen( $url))
Definition: sarg-squidguard-block2.php:97
INRC_Found
@ INRC_Found
A match was found.
Definition: ip2name.h:12
Ip2NameProcess
Entry points of the ip2name modules.
Definition: ip2name.h:17
getword_start
void getword_start(struct getwordstruct *gwarea, const char *line)
Definition: util.c:74
debuga
void debuga(const char *File, int Line, const char *msg,...)
Definition: util.c:601
DichotomicStruct
Definition: dichotomic.c:42
Dichotomic_Search
const char * Dichotomic_Search(DichotomicObject Obj, const char *key)
Definition: dichotomic.c:192
ip2name_chainmodule
static void ip2name_chainmodule(struct Ip2NameProcess *Module)
Definition: ip2name.c:61
Ip2NameModules
Associate a name or alias to a module.
Definition: ip2name.c:33
addr
char addr[20000]
Definition: conf.h:439
module
char module[255]
Definition: conf.h:361
_
#define _(String)
Definition: conf.h:155
ip2name.h
Ip2NameModules::Process
struct Ip2NameProcess * Process
The structure to access the module functions.
Definition: ip2name.c:38
Ip2NameProcess::Next
struct Ip2NameProcess * Next
The link to the next module to try if this one fails.
Definition: ip2name.h:22
Dichotomic_Destroy
void Dichotomic_Destroy(DichotomicObject *ObjPtr)
Definition: dichotomic.c:82
dichotomic.h
name
char name[20000]
Definition: conf.h:335
ModulesList
static const struct Ip2NameModules ModulesList[]
The list of the modules available to resolve an IP address into a name.
Definition: ip2name.c:45
ip2name_cleanup
void ip2name_cleanup(void)
Definition: ip2name.c:251
Ip2NameExec
struct Ip2NameProcess Ip2NameExec
The functions to resolve an IP address using an external executable.
Definition: ip2name_exec.c:38
Ip2NameProcess::Resolve
enum ip2name_retcode(* Resolve)(char *ip, int ip_len)
Function to resolve an IP address into a name.
Definition: ip2name.h:26
ip2name_buildmoduleslist
static void ip2name_buildmoduleslist(const char *list)
Definition: ip2name.c:90
Ip2NameDns
struct Ip2NameProcess Ip2NameDns
The functions to resolve an IP address through tne dns.
Definition: ip2name_dns.c:37
getword
int getword(char *word, int limit, struct getwordstruct *gwarea, char stop)
Definition: util.c:90
KnownIp
static DichotomicObject KnownIp
The list of the names found so far.
Definition: ip2name.c:56
conf.h
Include headers and define global variables. */.
getwordstruct::beginning
const char * beginning
Definition: defs.h:29
ip2name_forcedns
void ip2name_forcedns(void)
Definition: ip2name.c:173
ip2name_configmodule
static void ip2name_configmodule(const char *module)
Definition: ip2name.c:122
ip2name
void ip2name(char *ip, int ip_len)
Definition: ip2name.c:216
ip2name_retcode
ip2name_retcode
The possible return code of ip2name subfunctions.
Definition: ip2name.h:5
getwordstruct
Definition: defs.h:26
Dichotomic_Create
DichotomicObject Dichotomic_Create(void)
Definition: dichotomic.c:61
Ip2NameProcess::Name
const char * Name
The real name of the module.
Definition: ip2name.h:20
Ip2NameModules::Name
const char * Name
The name of the module.
Definition: ip2name.c:36
FirstModule
static struct Ip2NameProcess * FirstModule
The chain of the configured modules to try to resolve an IP.
Definition: ip2name.c:54
safe_strcpy
void safe_strcpy(char *dest, const char *src, int length)
Definition: util.c:1550
defs.h
Declaration of the structures and functions.
ip2name_config
int ip2name_config(const char *param)
Definition: ip2name.c:153
Dichotomic_Insert
bool Dichotomic_Insert(DichotomicObject Obj, const char *key, const char *value)
Definition: dichotomic.c:139
Ip2NameProcess::Configure
void(* Configure)(const char *name, const char *param)
The function to configure the module.
Definition: ip2name.h:24
debug
int debug
Definition: conf.h:489
Ip2Name
bool Ip2Name
Definition: conf.h:337