* Removed all of the thread cancellation cruft that is no longer necessary
[citadel.git] / citadel / locate_host.c
1 /*
2  * locate the originating host
3  * $Id$
4  */
5
6 #include "sysdep.h"
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <signal.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <limits.h>
15 #include <netdb.h>
16 #include <string.h>
17 #include "citadel.h"
18 #include "server.h"
19 #include "locate_host.h"
20 #include "config.h"
21 #include "tools.h"
22
23 void locate_host(char *tbuf, const struct in_addr *addr)
24 {
25         struct hostent *ch;
26         char *i;
27         int a1, a2, a3, a4;
28
29 #ifdef HAVE_NONREENTRANT_NETDB
30         begin_critical_section(S_NETDB);
31 #endif
32
33         if ((ch = gethostbyaddr((char *) addr, sizeof(*addr), AF_INET)) ==
34             NULL) {
35               bad_dns:
36                 i = (char *) addr;
37                 a1 = ((*i++) & 0xff);
38                 a2 = ((*i++) & 0xff);
39                 a3 = ((*i++) & 0xff);
40                 a4 = ((*i++) & 0xff);
41                 sprintf(tbuf, "%d.%d.%d.%d", a1, a2, a3, a4);
42                 goto end;       /* because we might need to end the critical
43                                    section */
44         }
45         /* check if the forward DNS agrees; if not, they're spoofing */
46         i = strdoop(ch->h_name);
47         ch = gethostbyname(i);
48         phree(i);
49         if (ch == NULL)
50                 goto bad_dns;
51
52         /* check address for consistency */
53         for (; *ch->h_addr_list; ch->h_addr_list++)
54                 if (!memcmp(*ch->h_addr_list, addr,
55                             sizeof *addr)) {
56                         safestrncpy(tbuf, ch->h_name, 25);
57                         goto end;
58                 }
59         goto bad_dns;           /* they were spoofing. report a numeric IP
60                                    address. */
61
62       end:
63
64 #ifdef HAVE_NONREENTRANT_NETDB
65         end_critical_section(S_NETDB);
66 #endif
67
68         tbuf[25] = 0;
69 }