Fixes for Cygwin (see ChangeLog)
[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     if((ch = gethostbyaddr((char *) &cs.sin_addr, sizeof(cs.sin_addr),  
40           AF_INET)) == NULL) { 
41                 i = (char *) &cs.sin_addr;
42                 a1 = ((*i++)&0xff);
43                 a2 = ((*i++)&0xff);
44                 a3 = ((*i++)&0xff);
45                 a4 = ((*i++)&0xff);
46                 sprintf(tbuf,"%d.%d.%d.%d",a1,a2,a3,a4);
47                 return;
48                 }
49
50         strncpy(tbuf,ch->h_name, 24);
51         tbuf[24] = 0;
52         }