* All OS-level includes are now included from webcit.h instead of from
[citadel.git] / webcit / locate_host.c
1 /*
2  * $Id$
3  *
4  * Examine a socket and determine the name/address of the originating host.
5  */
6
7
8 #include "webcit.h"
9
10 void locate_host(char *tbuf, int client_socket)
11 {
12         struct sockaddr_in cs;
13         struct hostent *ch;
14         int len;
15         char *i;
16         int a1, a2, a3, a4;
17
18         len = sizeof(cs);
19         if (getpeername(client_socket, (struct sockaddr *) &cs, &len) < 0) {
20                 strcpy(tbuf, "<unknown>");
21                 return;
22         }
23         if ((ch = gethostbyaddr((char *) &cs.sin_addr, sizeof(cs.sin_addr),
24                                 AF_INET)) == NULL) {
25                 i = (char *) &cs.sin_addr;
26                 a1 = ((*i++) & 0xff);
27                 a2 = ((*i++) & 0xff);
28                 a3 = ((*i++) & 0xff);
29                 a4 = ((*i++) & 0xff);
30                 sprintf(tbuf, "%d.%d.%d.%d", a1, a2, a3, a4);
31                 return;
32         }
33         safestrncpy(tbuf, ch->h_name, 64);
34 }