* stuff
[citadel.git] / webcit / locate_host.c
1 /*
2  * locate the originating host
3  */
4
5
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <sys/socket.h>
15 #include <sys/time.h>
16 #include <limits.h>
17 #include <netinet/in.h>
18 #include <netdb.h>
19 #include <string.h>
20 #include <pwd.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <pthread.h>
24 #include <signal.h>
25 #include "webcit.h"
26
27
28
29
30 void locate_host(char *tbuf, int client_socket)
31 {
32         struct sockaddr_in cs;
33         struct hostent *ch;
34         int len;
35         char *i;
36         int a1, a2, a3, a4;
37
38         len = sizeof(cs);
39         if (getpeername(client_socket, (struct sockaddr *) &cs, &len) < 0) {
40                 strcpy(tbuf, "<unknown>");
41                 return;
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         safestrncpy(tbuf, ch->h_name, 64);
54 }