Added locate_host.c
[citadel.git] / webcit / 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 <limits.h>
13 #include <netdb.h>
14 #include <string.h>
15
16 void locate_host(char *tbuf, int client_socket)
17 {
18         struct sockaddr_in      cs;     
19         struct hostent      *   ch;        
20         int                     len;     
21         char *i;
22         int a1,a2,a3,a4;
23        
24     len = sizeof(cs);   
25     if (getpeername(client_socket, (struct sockaddr *)&cs,&len) < 0) {
26         strcpy(tbuf, "<unknown>");
27         return;
28         }
29      
30     if((ch = gethostbyaddr((char *) &cs.sin_addr, sizeof(cs.sin_addr),  
31           AF_INET)) == NULL) { 
32                 i = (char *) &cs.sin_addr;
33                 a1 = ((*i++)&0xff);
34                 a2 = ((*i++)&0xff);
35                 a3 = ((*i++)&0xff);
36                 a4 = ((*i++)&0xff);
37                 sprintf(tbuf,"%d.%d.%d.%d",a1,a2,a3,a4);
38                 return;
39                 }
40
41         strncpy(tbuf,ch->h_name, 24);
42         tbuf[24] = 0;
43         }