bf018214c7ad748b827307be693a5319e1c1d36d
[citadel.git] / webcit / locate_host.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup Hostlookup Examine a socket and determine the name/address of the originating host.
6  */
7 /*@{*/
8
9 #include "webcit.h"
10
11 /**
12  * \brief get a hostname 
13  * \todo buffersize?
14  * \param tbuf the returnbuffer
15  * \param client_socket the sock fd where the client is connected
16  */
17 void locate_host(char *tbuf, int client_socket)
18 {
19         struct sockaddr_in cs;
20         struct hostent *ch;
21         socklen_t len;
22         char *i;
23         int a1, a2, a3, a4;
24
25         len = sizeof(cs);
26         if (getpeername(client_socket, (struct sockaddr *) &cs, &len) < 0) {
27                 strcpy(tbuf, "<unknown>");
28                 return;
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         safestrncpy(tbuf, ch->h_name, 64);
41 }
42
43 /*@}*/