+ /* make sure that stdin, stdout an stderr don't stuff things
+ up (library functions, for example) */
+ for (i=0;i<3;i++) {
+ close(i);
+ open("/dev/null", O_RDWR);
+ }
+}
+
+/*******************************************************************
+ return the IP addr of the client as a string
+ ******************************************************************/
+char *client_addr(int fd)
+{
+ struct sockaddr sa;
+ struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
+ int length = sizeof(sa);
+ static char addr_buf[100];
+ static int initialised;
+
+ if (initialised) return addr_buf;
+
+ initialised = 1;
+
+ if (getpeername(fd, &sa, &length)) {
+ exit_cleanup(RERR_SOCKETIO);
+ }
+
+ strlcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf));
+ return addr_buf;
+}
+
+
+/*******************************************************************
+ return the DNS name of the client
+ ******************************************************************/
+char *client_name(int fd)
+{
+ struct sockaddr sa;
+ struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
+ int length = sizeof(sa);
+ static char name_buf[100];
+ struct hostent *hp;
+ char **p;
+ char *def = "UNKNOWN";
+ static int initialised;
+
+ if (initialised) return name_buf;
+
+ initialised = 1;
+
+ strcpy(name_buf,def);
+
+ if (getpeername(fd, &sa, &length)) {
+ exit_cleanup(RERR_SOCKETIO);
+ }
+
+ /* Look up the remote host name. */
+ if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
+ sizeof(sockin->sin_addr),
+ AF_INET))) {
+ strlcpy(name_buf,(char *)hp->h_name,sizeof(name_buf));
+ }
+
+
+ /* do a forward lookup as well to prevent spoofing */
+ hp = gethostbyname(name_buf);
+ if (!hp) {
+ strcpy(name_buf,def);
+ rprintf(FERROR,"reverse name lookup failed\n");
+ } else {
+ for (p=hp->h_addr_list;*p;p++) {
+ if (memcmp(*p, &sockin->sin_addr, hp->h_length) == 0) {
+ break;
+ }
+ }
+ if (!*p) {
+ strcpy(name_buf,def);
+ rprintf(FERROR,"reverse name lookup mismatch - spoofed address?\n");
+ }
+ }
+
+ return name_buf;