3 #include <sys/socket.h>
4 #include <netinet/in.h>
9 /*******************************************************************
10 return the IP addr of the client as a string
11 ******************************************************************/
12 char *client_addr(int fd)
15 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
16 int length = sizeof(sa);
17 static char addr_buf[100];
18 static int initialised;
20 if (initialised) return addr_buf;
24 if (getpeername(fd, &sa, &length)) {
29 (char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf));
30 addr_buf[sizeof(addr_buf)-1] = 0;
36 /*******************************************************************
37 return the DNS name of the client
38 ******************************************************************/
39 char *client_name(int fd)
42 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
43 int length = sizeof(sa);
44 static char name_buf[100];
47 char *def = "UNKNOWN";
48 static int initialised;
50 if (initialised) return name_buf;
56 if (getpeername(fd, &sa, &length)) {
60 /* Look up the remote host name. */
61 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
62 sizeof(sockin->sin_addr),
65 strncpy(name_buf,(char *)hp->h_name,sizeof(name_buf));
66 name_buf[sizeof(name_buf)-1] = 0;
70 /* do a forward lookup as well to prevent spoofing */
71 hp = gethostbyname(name_buf);
75 for (p=hp->h_addr_list;*p;p++) {
76 if (memcmp(*p, &sockin->sin_addr, hp->h_length) == 0) {
89 int main(int argc, char *argv[])
91 char *service = argv[1];
93 openlog("socklog", LOG_PID, LOG_DAEMON);
94 syslog(LOG_ERR, "%s connection from %s [%s]",
95 service, client_name(0), client_addr(0));