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