remotehost.c 719 B

1234567891011121314151617181920212223242526272829303132
  1. #include <sys/types.h>
  2. #include <sys/socket.h> /* various networking crud */
  3. #include <netinet/in.h>
  4. #include <netinet/tcp.h>
  5. #include <netdb.h>
  6. #include <plan9.h>
  7. void
  8. getremotehostname(char *name, int nname)
  9. {
  10. struct sockaddr_in sock;
  11. struct hostent *hp;
  12. uint len;
  13. int on;
  14. strecpy(name, name+nname, "unknown");
  15. len = sizeof sock;
  16. if(getpeername(0, (struct sockaddr*)&sock, (void*)&len) < 0)
  17. return;
  18. hp = gethostbyaddr((char *)&sock.sin_addr, sizeof (struct in_addr),
  19. sock.sin_family);
  20. if(hp == 0)
  21. return;
  22. strecpy(name, name+nname, hp->h_name);
  23. on = 1;
  24. setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char*)&on, sizeof(on));
  25. on = 1;
  26. setsockopt(0, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on));
  27. }