]> git.ipfire.org Git - thirdparty/sarg.git/blob - ip2name.c
Replaced the ifdef FOPEN64 by a single call to MY_FOPEN for code clarity.
[thirdparty/sarg.git] / ip2name.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27 #include "include/defs.h"
28
29 void ip2name(char *ip,int ip_len)
30 {
31 u_long addr;
32 struct hostent *hp;
33 char **p;
34
35 if ((int)(addr = inet_addr(ip)) == -1)
36 return;
37
38 hp = gethostbyaddr((char *)&addr, sizeof (addr), AF_INET);
39 if (hp == NULL)
40 return;
41
42 for (p = hp->h_addr_list; *p != 0; p++) {
43 struct in_addr in;
44
45 (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
46 strncpy(ip,hp->h_name,ip_len-1);
47 ip[ip_len-1]=0;
48 }
49
50 return;
51 }
52
53 void name2ip(char *name)
54 {
55 struct in_addr ia;
56 struct hostent *hp;
57 char *port;
58 char n1[4];
59 char n2[4];
60 char n3[4];
61 char n4[4];
62
63 port=strchr(name,':');
64 if (port) *port=0;
65
66 if((hp=gethostbyname(name))==NULL)
67 return;
68
69 memcpy(&ia.s_addr,hp->h_addr_list[0],sizeof(ia.s_addr));
70 ia.s_addr=ntohl(ia.s_addr);
71 strcpy(name,inet_ntoa(ia));
72 if (getword(n4,sizeof(n4),name,'.')<0 || getword(n3,sizeof(n3),name,'.')<0 ||
73 getword(n2,sizeof(n2),name,'.')<0 || getword(n1,sizeof(n1),name,0)<0) {
74 printf("SARG: Maybe you have a broken record or garbage in your %s ip address.\n",name);
75 exit(1);
76 }
77 sprintf(name,"%s.%s.%s.%s",n1,n2,n3,n4);
78
79 return;
80 }