]> git.ipfire.org Git - thirdparty/sarg.git/blame - ip2name.c
Fix more text messages
[thirdparty/sarg.git] / ip2name.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
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"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
a1c55d8c 30void ip2name(char *ip,int ip_len)
9c7c6346 31{
72c27633
FM
32#ifdef HAVE_GETNAMEINFO
33 struct sockaddr_storage sa;
34 char host[NI_MAXHOST];
35 int n1,n2,n3,n4,next=0;
36 int error;
37
38 memset(&sa,0,sizeof(sa));
39 if (sscanf(ip,"%d.%d.%d.%d%n",&n1,&n2,&n3,&n4,&next)==4 && ip[next]=='\0') {
40 struct sockaddr_in *s4=(struct sockaddr_in *)&sa;
41 if (inet_pton(AF_INET,ip,&s4->sin_addr)!=1) return;
42 sa.ss_family=AF_INET;
43 } else {
44 struct sockaddr_in6 *s6=(struct sockaddr_in6 *)&sa;
45 if (inet_pton(AF_INET6,ip,&s6->sin6_addr)!=1) return;
46 sa.ss_family=AF_INET6;
47 }
48 error=getnameinfo((struct sockaddr *)&sa,sizeof(sa),host,sizeof(host),NULL,0,0);
49 if (error==0)
50 {
51 strncpy(ip,host,ip_len-1);
52 ip[ip_len-1]='\0';
53 } else {
54 debuga(_("IP to name resolution (getnameinfo) on IP address %s failed with error %d - %s\n"),ip,error,gai_strerror(error));
55 }
56#else
f00518d0 57 struct in_addr addr;
936c9905
FM
58 struct hostent *hp;
59 char **p;
25697a35 60
f00518d0 61 if (inet_aton(ip,&addr) == 0)
25697a35
GS
62 return;
63
f00518d0 64 hp = gethostbyaddr(&addr, sizeof (addr), AF_INET);
936c9905 65 if (hp == NULL)
25697a35
GS
66 return;
67
936c9905
FM
68 for (p = hp->h_addr_list; *p != 0; p++) {
69 struct in_addr in;
25697a35 70
936c9905 71 (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
a1c55d8c
FM
72 strncpy(ip,hp->h_name,ip_len-1);
73 ip[ip_len-1]=0;
936c9905 74 }
72c27633 75#endif
25697a35 76 return;
936c9905 77}
25697a35
GS
78
79void name2ip(char *name)
9c7c6346 80{
25697a35
GS
81 struct in_addr ia;
82 struct hostent *hp;
32e71fa4
FM
83 char *port;
84 char n1[4];
85 char n2[4];
86 char n3[4];
87 char n4[4];
9c7c6346 88 struct getwordstruct gwarea;
25697a35 89
32e71fa4
FM
90 port=strchr(name,':');
91 if (port) *port=0;
25697a35
GS
92
93 if((hp=gethostbyname(name))==NULL)
94 return;
9c7c6346 95
32e71fa4
FM
96 memcpy(&ia.s_addr,hp->h_addr_list[0],sizeof(ia.s_addr));
97 ia.s_addr=ntohl(ia.s_addr);
9c7c6346
FM
98 getword_start(&gwarea,inet_ntoa(ia));
99 if (getword(n4,sizeof(n4),&gwarea,'.')<0 || getword(n3,sizeof(n3),&gwarea,'.')<0 ||
100 getword(n2,sizeof(n2),&gwarea,'.')<0 || getword(n1,sizeof(n1),&gwarea,0)<0) {
101 printf("SARG: Maybe you have a broken record or garbage in your %s ip address.\n",gwarea.beginning);
06b39c87 102 exit(EXIT_FAILURE);
25697a35 103 }
32e71fa4 104 sprintf(name,"%s.%s.%s.%s",n1,n2,n3,n4);
25697a35
GS
105
106 return;
9c7c6346 107}