9ca4e0e8ca2a0a1ab94bfddbbb2501127ab56645
[citadel.git] / citadel / locate_host.c
1 /*
2  * locate the originating host
3  * $Id$
4  */
5
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <limits.h>
14 #include <netdb.h>
15 #include <string.h>
16 #include <pthread.h>
17 #include "sysdep.h"
18 #include "citadel.h"
19 #include "server.h"
20 #include "locate_host.h"
21 #include "config.h"
22
23 void locate_host(char *tbuf)
24 {
25         struct sockaddr_in      cs;     
26         struct hostent      *   ch;        
27         int                     len;     
28         char *i;
29         int a1,a2,a3,a4;
30        
31     len = sizeof(cs);   
32     if (getpeername(CC->client_socket, (struct sockaddr *)&cs,&len) < 0){   
33         strcpy(tbuf,config.c_fqdn);
34         return;
35         }
36      
37     if((ch = gethostbyaddr((char *) &cs.sin_addr, sizeof(cs.sin_addr),  
38           AF_INET)) == NULL) { 
39                 i = (char *) &cs.sin_addr;
40                 a1 = ((*i++)&0xff);
41                 a2 = ((*i++)&0xff);
42                 a3 = ((*i++)&0xff);
43                 a4 = ((*i++)&0xff);
44                 sprintf(tbuf,"%d.%d.%d.%d",a1,a2,a3,a4);
45                 return;
46                 }
47
48         strncpy(tbuf,ch->h_name, 24);
49         tbuf[24] = 0;
50         }