* trunk webcit is now IPv6-enabled by default
[citadel.git] / webcit / locate_host.c
1 /*
2  * $Id$
3  *
4  * Given a socket, supply the name of the host at the other end.
5  *
6  * Copyright (c) 1996-2010 by the citadel.org team
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "webcit.h"
24
25
26 /*
27  * IPv6 enabled locate_host()
28  */
29 void locate_host(StrBuf *tbuf, int client_socket)
30 {
31         struct sockaddr_in6 clientaddr;
32         unsigned int addrlen = sizeof(clientaddr);
33         char clienthost[NI_MAXHOST];
34         char clientservice[NI_MAXSERV];
35
36         getpeername(client_socket, (struct sockaddr *)&clientaddr, &addrlen);
37         getnameinfo((struct sockaddr *)&clientaddr, addrlen,
38                 clienthost, sizeof(clienthost),
39                 clientservice, sizeof(clientservice),
40                 0
41         );
42
43         StrBufAppendBufPlain(tbuf, clienthost, -1, 0);
44 }
45
46
47 #if 0
48 /*
49  * IPv4-only locate_host()
50  */
51 void locate_host(StrBuf *tbuf, int client_socket)
52 {
53         struct sockaddr_in cs;
54         struct hostent *ch;
55         socklen_t len;
56         char *i;
57         int a1, a2, a3, a4;
58
59         len = sizeof(cs);
60         if (getpeername(client_socket, (struct sockaddr *) &cs, &len) < 0) {
61                 StrBufAppendBufPlain(tbuf, HKEY("<unknown>"), 0);
62                 return;
63         }
64         if ((ch = gethostbyaddr((char *) &cs.sin_addr, sizeof(cs.sin_addr),
65                                 AF_INET)) == NULL) {
66                 i = (char *) &cs.sin_addr;
67                 a1 = ((*i++) & 0xff);
68                 a2 = ((*i++) & 0xff);
69                 a3 = ((*i++) & 0xff);
70                 a4 = ((*i++) & 0xff);
71                 StrBufPrintf(tbuf, "%d.%d.%d.%d", a1, a2, a3, a4);
72                 return;
73         }
74         StrBufAppendBufPlain(tbuf, ch->h_name, -1, 0);
75 }
76 #endif