* acconfig.h, configure.in, locate_host.c, server.h: work around
[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 #ifdef HAVE_PTHREAD_H
18 #include <pthread.h>
19 #endif
20 #include "citadel.h"
21 #include "server.h"
22 #include "locate_host.h"
23 #include "config.h"
24
25 void locate_host(char *tbuf)
26 {
27         struct sockaddr_in      cs;     
28         struct hostent      *   ch;        
29         int                     len;     
30         char *i;
31         int a1,a2,a3,a4;
32        
33     len = sizeof(cs);   
34     if (getpeername(CC->client_socket, (struct sockaddr *)&cs,&len) < 0){   
35         strcpy(tbuf,config.c_fqdn);
36         return;
37         }
38
39 #ifdef HAVE_NONREENTRANT_NETDB
40     begin_critical_section(S_NETDB);
41 #endif
42      
43     if((ch = gethostbyaddr((char *) &cs.sin_addr, sizeof(cs.sin_addr),  
44           AF_INET)) == NULL) { 
45                 i = (char *) &cs.sin_addr;
46                 a1 = ((*i++)&0xff);
47                 a2 = ((*i++)&0xff);
48                 a3 = ((*i++)&0xff);
49                 a4 = ((*i++)&0xff);
50                 sprintf(tbuf,"%d.%d.%d.%d",a1,a2,a3,a4);
51                 return;
52                 }
53
54         strncpy(tbuf,ch->h_name, 24);
55
56 #ifdef HAVE_NONREENTRANT_NETDB
57         end_critical_section(S_NETDB);
58 #endif
59
60         tbuf[24] = 0;
61         }