]>
Commit | Line | Data |
---|---|---|
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" |
e9b33b57 | 29 | #ifdef HAVE_WS2TCPIP_H |
9bd92830 | 30 | #include <ws2tcpip.h> //define getnameinfo on windows |
e9b33b57 | 31 | #endif |
25697a35 | 32 | |
a1c55d8c | 33 | void ip2name(char *ip,int ip_len) |
9c7c6346 | 34 | { |
72c27633 | 35 | #ifdef HAVE_GETNAMEINFO |
9bd92830 FM |
36 | struct sockaddr_storage sa; |
37 | char host[NI_MAXHOST]; | |
38 | int n1,n2,n3,n4,next=0; | |
39 | int error; | |
72c27633 | 40 | |
9bd92830 FM |
41 | memset(&sa,0,sizeof(sa)); |
42 | if (sscanf(ip,"%d.%d.%d.%d%n",&n1,&n2,&n3,&n4,&next)==4 && ip[next]=='\0') { | |
43 | struct sockaddr_in *s4=(struct sockaddr_in *)&sa; | |
44 | if (inet_pton(AF_INET,ip,&s4->sin_addr)!=1) return; | |
45 | sa.ss_family=AF_INET; | |
46 | } else { | |
47 | struct sockaddr_in6 *s6=(struct sockaddr_in6 *)&sa; | |
48 | if (inet_pton(AF_INET6,ip,&s6->sin6_addr)!=1) return; | |
49 | sa.ss_family=AF_INET6; | |
50 | } | |
51 | error=getnameinfo((struct sockaddr *)&sa,sizeof(sa),host,sizeof(host),NULL,0,0); | |
52 | if (error==0) | |
53 | { | |
54 | strncpy(ip,host,ip_len-1); | |
55 | ip[ip_len-1]='\0'; | |
56 | } else { | |
57 | debuga(_("IP to name resolution (getnameinfo) on IP address %s failed with error %d - %s\n"),ip,error,gai_strerror(error)); | |
58 | } | |
72c27633 | 59 | #else |
9bd92830 FM |
60 | struct in_addr addr; |
61 | struct hostent *hp; | |
62 | char **p; | |
25697a35 | 63 | |
e9b33b57 | 64 | #ifdef HAVE_INET_ATON |
9bd92830 FM |
65 | if (inet_aton(ip,&addr) == 0) |
66 | return; | |
e9b33b57 | 67 | #else |
9bd92830 FM |
68 | addr.s_addr=inet_addr(ip); |
69 | if (addr.s_addr==-1) return; | |
e9b33b57 | 70 | #endif |
25697a35 | 71 | |
9bd92830 FM |
72 | hp = gethostbyaddr((void *)&addr, sizeof (addr), AF_INET); |
73 | if (hp == NULL) | |
74 | return; | |
25697a35 | 75 | |
9bd92830 FM |
76 | for (p = hp->h_addr_list; *p != 0; p++) { |
77 | struct in_addr in; | |
25697a35 | 78 | |
9bd92830 FM |
79 | (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr)); |
80 | strncpy(ip,hp->h_name,ip_len-1); | |
81 | ip[ip_len-1]=0; | |
82 | } | |
72c27633 | 83 | #endif |
9bd92830 | 84 | return; |
936c9905 | 85 | } |
25697a35 GS |
86 | |
87 | void name2ip(char *name) | |
9c7c6346 | 88 | { |
9bd92830 FM |
89 | struct in_addr ia; |
90 | struct hostent *hp; | |
91 | char *port; | |
92 | char n1[4]; | |
93 | char n2[4]; | |
94 | char n3[4]; | |
95 | char n4[4]; | |
96 | struct getwordstruct gwarea; | |
25697a35 | 97 | |
9bd92830 FM |
98 | port=strchr(name,':'); |
99 | if (port) *port=0; | |
25697a35 | 100 | |
9bd92830 FM |
101 | if((hp=gethostbyname(name))==NULL) |
102 | return; | |
9c7c6346 | 103 | |
9bd92830 FM |
104 | memcpy(&ia.s_addr,hp->h_addr_list[0],sizeof(ia.s_addr)); |
105 | ia.s_addr=ntohl(ia.s_addr); | |
106 | getword_start(&gwarea,inet_ntoa(ia)); | |
107 | if (getword(n4,sizeof(n4),&gwarea,'.')<0 || getword(n3,sizeof(n3),&gwarea,'.')<0 || | |
108 | getword(n2,sizeof(n2),&gwarea,'.')<0 || getword(n1,sizeof(n1),&gwarea,0)<0) { | |
109 | printf("SARG: Maybe you have a broken record or garbage in your %s ip address.\n",gwarea.beginning); | |
110 | exit(EXIT_FAILURE); | |
111 | } | |
112 | sprintf(name,"%s.%s.%s.%s",n1,n2,n3,n4); | |
25697a35 | 113 | |
9bd92830 | 114 | return; |
9c7c6346 | 115 | } |