home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / rc5clnt.zip / common.c < prev    next >
C/C++ Source or Header  |  1997-03-12  |  2KB  |  116 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/param.h>
  10. #include <sys/time.h>
  11. #include <sys/socket.h>
  12.  
  13. #include <netdb.h>
  14. #include <netinet/in.h>
  15. #include <netinet/in_systm.h>
  16. #define MAXHOSTNAMELEN  20
  17.  
  18. #include "common.h"
  19.  
  20. #ifdef SOCKS
  21. #include "socks.h"
  22. #endif
  23.  
  24. struct in_addr keyserver_addr;
  25. unsigned short keyserver_port = 2000 + RC5_KEYSIZE;
  26.  
  27. unsigned int inet_address(host)
  28.     const char *host;
  29. {
  30.     unsigned int addr;
  31.     struct hostent *hp;
  32.  
  33.     if ((int) (addr = inet_addr(host)) == -1) {
  34.         if (!(hp = gethostbyname(host))) {
  35.             perror("gethostbyname");
  36.             return (-1);
  37.         }
  38.  
  39.         memcpy((void *) &addr, (void *) hp->h_addr, sizeof(addr));
  40.     }
  41.  
  42.     return (addr);
  43. }
  44.  
  45. char *inet_name(struct in_addr in)
  46. {
  47.     register char *cp;
  48.     static char line[50];
  49.     struct hostent *hp;
  50.     static char domain[MAXHOSTNAMELEN + 1];
  51.     static int first = 1;
  52.  
  53.     if (first) {
  54.         first = 0;
  55.         if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
  56.             (cp = strchr(domain, '.')))
  57.             (void) strcpy(domain, cp + 1);
  58.         else
  59.             domain[0] = 0;
  60.     }
  61.  
  62.     cp = 0;
  63.  
  64.     if (in.s_addr != INADDR_ANY) {
  65.         hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
  66.         if (hp) {
  67.             if ((cp = strchr(hp->h_name, '.')) && !strcmp(cp + 1, domain))
  68.                 *cp = 0;
  69.  
  70.             cp = hp->h_name;
  71.         }
  72.     }
  73.  
  74.     if (cp)
  75.         (void) strcpy(line, cp);
  76.     else {
  77.         in.s_addr = ntohl(in.s_addr);
  78.         sprintf(line, "%u.%u.%u.%u",
  79.             (unsigned char) ((in.s_addr >> 24) & 0xFF),
  80.             (unsigned char) ((in.s_addr >> 16) & 0xFF),
  81.             (unsigned char) ((in.s_addr >>  8) & 0xFF),
  82.             (unsigned char) ((in.s_addr      ) & 0xFF));
  83.     }
  84.  
  85.     return (line);
  86. }
  87.  
  88. int open_server()
  89. {
  90.     static int on = 1;
  91.     int sock;
  92.     struct sockaddr_in sin;
  93.  
  94.     memset((void *) &sin, 0, sizeof(sin));
  95.  
  96.     sin.sin_family        = AF_INET;
  97.     sin.sin_addr.s_addr    = keyserver_addr.s_addr;
  98.     sin.sin_port        = htons(keyserver_port);
  99.  
  100.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  101.         return (-1);
  102.  
  103.     if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
  104.         close(sock);
  105.         return (-1);
  106.     }
  107.  
  108.     if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
  109.         close(sock);
  110.         return (-1);
  111.     }
  112.  
  113.     return(sock);
  114. }
  115.  
  116.