* wildmat.c, braindamage.c: added
[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         if ((ch = gethostbyaddr((char *) &cs.sin_addr, sizeof(cs.sin_addr),
30                                 AF_INET)) == NULL) {
31                 i = (char *) &cs.sin_addr;
32                 a1 = ((*i++) & 0xff);
33                 a2 = ((*i++) & 0xff);
34                 a3 = ((*i++) & 0xff);
35                 a4 = ((*i++) & 0xff);
36                 sprintf(tbuf, "%d.%d.%d.%d", a1, a2, a3, a4);
37                 return;
38         }
39         strncpy(tbuf, ch->h_name, 24);
40         tbuf[24] = 0;
41 }