]> git.ipfire.org Git - thirdparty/sarg.git/blame - ip2name.c
Fix typo and add CR at the end of debuga
[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{
25697a35 32 u_long addr;
936c9905
FM
33 struct hostent *hp;
34 char **p;
25697a35
GS
35
36 if ((int)(addr = inet_addr(ip)) == -1)
37 return;
38
936c9905
FM
39 hp = gethostbyaddr((char *)&addr, sizeof (addr), AF_INET);
40 if (hp == NULL)
25697a35
GS
41 return;
42
936c9905
FM
43 for (p = hp->h_addr_list; *p != 0; p++) {
44 struct in_addr in;
25697a35 45
936c9905 46 (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
a1c55d8c
FM
47 strncpy(ip,hp->h_name,ip_len-1);
48 ip[ip_len-1]=0;
936c9905 49 }
25697a35
GS
50
51 return;
936c9905 52}
25697a35
GS
53
54void name2ip(char *name)
9c7c6346 55{
25697a35
GS
56 struct in_addr ia;
57 struct hostent *hp;
32e71fa4
FM
58 char *port;
59 char n1[4];
60 char n2[4];
61 char n3[4];
62 char n4[4];
9c7c6346 63 struct getwordstruct gwarea;
25697a35 64
32e71fa4
FM
65 port=strchr(name,':');
66 if (port) *port=0;
25697a35
GS
67
68 if((hp=gethostbyname(name))==NULL)
69 return;
9c7c6346 70
32e71fa4
FM
71 memcpy(&ia.s_addr,hp->h_addr_list[0],sizeof(ia.s_addr));
72 ia.s_addr=ntohl(ia.s_addr);
9c7c6346
FM
73 getword_start(&gwarea,inet_ntoa(ia));
74 if (getword(n4,sizeof(n4),&gwarea,'.')<0 || getword(n3,sizeof(n3),&gwarea,'.')<0 ||
75 getword(n2,sizeof(n2),&gwarea,'.')<0 || getword(n1,sizeof(n1),&gwarea,0)<0) {
76 printf("SARG: Maybe you have a broken record or garbage in your %s ip address.\n",gwarea.beginning);
32e71fa4 77 exit(1);
25697a35 78 }
32e71fa4 79 sprintf(name,"%s.%s.%s.%s",n1,n2,n3,n4);
25697a35
GS
80
81 return;
9c7c6346 82}