move all source files to src/
[citadel.git] / webcit / src / locate_host.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup Hostlookup Examine a socket and determine the name/address of the originating host.
6  * \ingroup WebcitHttpServer
7  */
8 /*@{*/
9
10 #include "webcit.h"
11
12 /**
13  * \brief get a hostname 
14  * \todo buffersize?
15  * \param tbuf the returnbuffer
16  * \param client_socket the sock fd where the client is connected
17  */
18 void locate_host(char *tbuf, int client_socket)
19 {
20         struct sockaddr_in cs;
21         struct hostent *ch;
22         socklen_t len;
23         char *i;
24         int a1, a2, a3, a4;
25
26         len = sizeof(cs);
27         if (getpeername(client_socket, (struct sockaddr *) &cs, &len) < 0) {
28                 strcpy(tbuf, "<unknown>");
29                 return;
30         }
31         if ((ch = gethostbyaddr((char *) &cs.sin_addr, sizeof(cs.sin_addr),
32                                 AF_INET)) == NULL) {
33                 i = (char *) &cs.sin_addr;
34                 a1 = ((*i++) & 0xff);
35                 a2 = ((*i++) & 0xff);
36                 a3 = ((*i++) & 0xff);
37                 a4 = ((*i++) & 0xff);
38                 sprintf(tbuf, "%d.%d.%d.%d", a1, a2, a3, a4);
39                 return;
40         }
41         safestrncpy(tbuf, ch->h_name, 64);
42 }
43
44 /*@}*/